DoubleBlockDiscreteDynamicSystem.java
/**
* $Id$
*
* Copyright (C) 2004-2005 Koga Laboratory. All rights reserved.
*/
package org.mklab.tool.control.system.discrete;
import java.text.MessageFormat;
import java.util.List;
import org.mklab.nfc.matrix.DoubleMatrix;
import org.mklab.nfc.ode.SolverStopException;
import org.mklab.tool.control.system.DoubleSystemOperator;
import org.mklab.tool.control.system.sampled.DoubleSampler;
/**
* 差分方程式で表現されるブロック離散時間動的システムを表わすクラスです。
*
* @author koga
* @version $Revision$
*/
public class DoubleBlockDiscreteDynamicSystem extends DoubleBlockDiscreteSystem implements DoubleDiscreteDynamicSystem {
/** 結合システムにおける状態の順番 */
private int number;
/**
* 新しく生成された<code>BlockDiscreteDynamicSystem</code>オブジェクトを初期化します。
*
* @param elements 隣接行列
* @param inputNodes 入力ノードの番号のリスト(番号は1から始まる)
* @param outputNodes 出力ノードの番号のリスト(番号は1から始まる)
*/
public DoubleBlockDiscreteDynamicSystem(final DoubleSystemOperator[][] elements, final List<Integer> inputNodes, final List<Integer> outputNodes) {
super(elements, inputNodes, outputNodes);
setStateSize(calcStateSize());
setDynamic(true);
}
/**
* 全システムの状態の数を返します。
*
* @return 全システムの状態の数を返します。
*/
private int calcStateSize() {
int count = 0;
for (final DoubleDiscreteDynamicSystem system : this.discreteDynamicSystems) {
count = count + ((DoubleSystemOperator)system).getStateSize();
}
return count;
}
/**
* @see org.mklab.tool.control.system.DynamicSystem#getInitialState()
*/
public DoubleMatrix getInitialState() {
DoubleMatrix x = new DoubleMatrix(0, 1);
if (this.discreteDynamicSystems == null) {
return x;
}
for (final DoubleDiscreteDynamicSystem system : this.discreteDynamicSystems) {
x = x.appendDown(system.getInitialState());
}
return x;
}
/**
* @see org.mklab.tool.control.system.DynamicSystem#getState()
*/
public DoubleMatrix getState() {
DoubleMatrix x = new DoubleMatrix(0, 1);
if (this.discreteDynamicSystems == null) {
return x;
}
for (final DoubleDiscreteDynamicSystem system : this.discreteDynamicSystems) {
x = x.appendDown(system.getState());
}
return x;
}
/**
* {@inheritDoc}
*/
public void setInitialState(final DoubleMatrix initialState) {
if (this.discreteDynamicSystems == null) {
return;
}
int stateSize = 0;
int offset = 1;
for (final DoubleDiscreteDynamicSystem system : this.discreteDynamicSystems) {
final int size = ((DoubleSystemOperator)system).getStateSize();
if (size > 0) {
final int end = offset + size - 1;
if (end > initialState.getRowSize()) {
throw new IllegalArgumentException(MessageFormat.format(Messages.getString("BlockDiscreteDynamicSystem.0"), system.getClass().getName())); //$NON-NLS-1$
}
system.setInitialState(initialState.getSubVector(offset, end));
offset += size;
stateSize += size;
}
}
if (initialState.getRowSize() != stateSize) {
throw new IllegalArgumentException(MessageFormat.format(Messages.getString("BlockDiscreteDynamicSystem.1"), Integer.valueOf(initialState.getRowSize()), Integer.valueOf(stateSize))); //$NON-NLS-1$
}
}
/**
* {@inheritDoc}
*/
public void setState(final DoubleMatrix state) {
if (this.discreteDynamicSystems == null) {
return;
}
int stateSize = 0;
int offset = 1;
for (final DoubleDiscreteDynamicSystem system : this.discreteDynamicSystems) {
final int size = ((DoubleSystemOperator)system).getStateSize();
if (size > 0) {
final int end = offset + size - 1;
if (end > state.getRowSize()) {
throw new IllegalArgumentException(MessageFormat.format(Messages.getString("BlockDiscreteDynamicSystem.0"), system.getClass().getName())); //$NON-NLS-1$
}
system.setState(state.getSubVector(offset, end));
offset += size;
stateSize += size;
}
}
if (state.getRowSize() != stateSize) {
throw new IllegalArgumentException(MessageFormat.format(Messages.getString("BlockDiscreteDynamicSystem.1"), Integer.valueOf(state.getRowSize()), Integer.valueOf(stateSize))); //$NON-NLS-1$
}
}
/**
* {@inheritDoc}
*/
public DoubleMatrix stateEquation(final int k, final DoubleMatrix x, final DoubleMatrix u) throws SolverStopException {
setState(x);
DoubleMatrix xNext = new DoubleMatrix(0, 1);
for (final DoubleDiscreteDynamicSystem system : this.discreteDynamicSystems) {
final DoubleMatrix nextX = system.stateEquation(k, system.getState(), getInputNodeValueOf((DoubleSystemOperator)system));
system.setState(nextX);
xNext = xNext.appendDown(nextX);
}
return xNext;
}
/**
* {@inheritDoc}
*/
public DoubleMatrix stateEquation(final double t, final DoubleMatrix x, final DoubleMatrix u) throws SolverStopException {
setState(x);
DoubleMatrix xNext = new DoubleMatrix(0, 1);
for (final DoubleDiscreteDynamicSystem system : this.discreteDynamicSystems) {
if (this.discreteDynamicSystemsUpdatedAtNextSamplingPoint.contains(system)) {
final int k = (int)Math.floor(t / ((DoubleSampler)system).getSamplingInterval());
final DoubleMatrix nextX = system.stateEquation(k, system.getState(), getInputNodeValueOf((DoubleSystemOperator)system));
system.setState(nextX);
xNext = xNext.appendDown(nextX);
} else {
xNext = xNext.appendDown(system.getState());
}
}
return xNext;
}
/**
* {@inheritDoc}
*/
public DoubleMatrix outputEquation(final int k, final DoubleMatrix x) throws SolverStopException {
setState(x);
resetNodeValue();
setInputNodeValue(new DoubleMatrix(getInputSize(), 1));
calcNodeValue(k);
return getOutputNodeValue();
}
/**
* {@inheritDoc}
*/
public DoubleMatrix outputEquation(final double t, final DoubleMatrix x) throws SolverStopException {
setState(x);
resetNodeValue();
setInputNodeValue(new DoubleMatrix(getInputSize(), 1));
calcNodeValue(t);
return getOutputNodeValue();
}
/**
* {@inheritDoc}
*/
public DoubleMatrix outputEquation(final int k, final DoubleMatrix x, final DoubleMatrix u) throws SolverStopException {
setState(x);
resetNodeValue();
setInputNodeValue(u);
calcNodeValue(k);
return getOutputNodeValue();
}
/**
* {@inheritDoc}
*/
public DoubleMatrix outputEquation(final double t, final DoubleMatrix x, final DoubleMatrix u) throws SolverStopException {
setState(x);
resetNodeValue();
setInputNodeValue(u);
calcNodeValue(t);
return getOutputNodeValue();
}
/**
* {@inheritDoc}
*/
public DoubleMatrix differenceEquation(final int k, final DoubleMatrix x, final DoubleMatrix inputOutput) throws SolverStopException {
final DoubleMatrix u = inputOutput.getSubVector(1, getInputSize());
return stateEquation(k, x, u);
}
/**
* {@inheritDoc}
*/
public DoubleMatrix differenceEquation(final double t, final DoubleMatrix x, final DoubleMatrix inputOutput) throws SolverStopException {
final DoubleMatrix u = inputOutput.getSubVector(1, getInputSize());
return stateEquation(t, x, u);
}
/**
* {@inheritDoc}
*/
public DoubleMatrix inputOutputEquation(final int k, final DoubleMatrix x) throws SolverStopException {
final DoubleMatrix u = new DoubleMatrix(getInputSize(), 1);
final DoubleMatrix y = outputEquation(k, x);
return u.appendDown(y);
}
/**
* {@inheritDoc}
*/
public DoubleMatrix inputOutputEquation(final double t, final DoubleMatrix x) throws SolverStopException {
final DoubleMatrix u = new DoubleMatrix(getInputSize(), 1);
final DoubleMatrix y = outputEquation(t, x);
return u.appendDown(y);
}
/**
* @see org.mklab.tool.control.system.SystemOperator#initialize()
*/
@Override
public void initialize() {
super.initialize();
if (this.discreteDynamicSystems == null) {
return;
}
setState(getInitialState());
}
/**
* @see org.mklab.tool.control.system.DynamicSystem#getStateNumber()
*/
public int getStateNumber() {
return this.number;
}
/**
* @see org.mklab.tool.control.system.DynamicSystem#setStateNumber(int)
*/
public void setStateNumber(final int number) {
this.number = number;
}
}