BlockSampledDataDynamicSystem.java

/**
 * $Id$
 *
 * Copyright (C) 2004-2005 Koga Laboratory. All rights reserved.
 */

package org.mklab.tool.control.system.sampled;

import java.text.MessageFormat;
import java.util.List;

import org.mklab.nfc.matrix.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.RealNumericalMatrix;
import org.mklab.nfc.ode.SolverStopException;
import org.mklab.nfc.scalar.ComplexNumericalScalar;
import org.mklab.nfc.scalar.RealNumericalScalar;
import org.mklab.tool.control.system.SystemOperator;
import org.mklab.tool.control.system.continuous.ContinuousExplicitDynamicSystem;
import org.mklab.tool.control.system.discrete.DiscreteDynamicSystem;


/**
 * ブロックサンプル値動的システムを表わすクラスです。
 * 
 * @author koga
 * @version $Revision$
 * @param <RS> type of real scalar
 * @param <RM> type of real matrix
 * @param <CS> type of complex scalar
 * @param <CM> type of complex matrix
 */
public class BlockSampledDataDynamicSystem<RS extends RealNumericalScalar<RS,RM,CS,CM>, RM extends RealNumericalMatrix<RS,RM,CS,CM>, CS extends ComplexNumericalScalar<RS,RM,CS,CM>, CM extends ComplexNumericalMatrix<RS,RM,CS,CM>> extends BlockSampledDataSystem<RS,RM,CS,CM> implements SampledDataDynamicSystem<RS,RM,CS,CM> {

  /** 連続時間システムの状態の数 */
  private int continuousStateSize;
  /** 離散時間システムの状態の数 */
  private int discreteStateSize;

  /**
   * 新しく生成された<code>BlockSampledDataDynamicSystem</code>オブジェクトを初期化します。
   * 
   * @param elements 隣接行列
   * @param inputNodes 入力ノードの番号のリスト(番号は1から始まる)
   * @param outputNodes 出力ノードの番号のリスト(番号は1から始まる)
   * @param sunit unit of scalar
   */
  public BlockSampledDataDynamicSystem(final SystemOperator<RS,RM,CS,CM>[][] elements, final List<Integer> inputNodes, final List<Integer> outputNodes, RS sunit) {
    super(elements, inputNodes, outputNodes, sunit);
    this.continuousStateSize = calcContinuousStateSize();
    this.discreteStateSize = calcDiscreteStateSize();
    setStateSize(this.continuousStateSize + this.discreteStateSize);
    setDynamic(true);
  }

  /**
   * 連続時間システムの状態の数を返します。
   * 
   * @return 連続時間システムの状態の数
   */
  private int calcContinuousStateSize() {
    int count = 0;
    for (final ContinuousExplicitDynamicSystem<RS,RM,CS,CM> system : this.continuousDynamicSystems) {
      count = count + ((SystemOperator<RS,RM,CS,CM>)system).getStateSize();
    }
    return count;
  }

  /**
   * 離散時間システムの状態の数を返します。
   * 
   * @return 離散時間システムの状態の数
   */
  private int calcDiscreteStateSize() {
    int count = 0;

    for (final DiscreteDynamicSystem<RS,RM,CS,CM> system : this.discreteDynamicSystems) {
      count = count + ((SystemOperator<RS,RM,CS,CM>)system).getStateSize();
    }

    return count;
  }

  /**
   * @see org.mklab.tool.control.system.sampled.SampledDataDynamicSystem#getContinuousStateSize()
   */
  public int getContinuousStateSize() {
    return this.continuousStateSize;
  }

  /**
   * @see org.mklab.tool.control.system.sampled.SampledDataDynamicSystem#getDiscreteStateSize()
   */
  public int getDiscreteStateSize() {
    return this.discreteStateSize;
  }

  /**
   * 連続時間システムの状態の数を設定します。
   * 
   * @param stateSize 連続時間システムの状態の数
   */
  protected void setContinuousStateSize(final int stateSize) {
    this.continuousStateSize = stateSize;
  }

  /**
   * 離散時間システムの状態の数を設定します。
   * 
   * @param stateSize 離散時間システムの状態の数
   */
  protected void setDiscreteStateSize(final int stateSize) {
    this.discreteStateSize = stateSize;
  }

  /**
   * @see org.mklab.tool.control.system.sampled.SampledDataDynamicSystem#getContinuousInitialState()
   */
  public RM getContinuousInitialState() {
    RM x = this.sunit.createZeroGrid(0, 1);

    if (this.continuousDynamicSystems == null) {
      return x;
    }

    for (final ContinuousExplicitDynamicSystem<RS,RM,CS,CM> system : this.continuousDynamicSystems) {
      x = x.appendDown(system.getInitialState());
    }
    return x;
  }

  /**
   * @see org.mklab.tool.control.system.sampled.SampledDataDynamicSystem#getDiscreteInitialState()
   */
  public RM getDiscreteInitialState() {
    RM x =this.sunit.createZeroGrid(0, 1);

    if (this.discreteDynamicSystems == null) {
      return x;
    }

    for (final DiscreteDynamicSystem<RS,RM,CS,CM> system : this.discreteDynamicSystems) {
      x = x.appendDown(system.getInitialState());
    }
    return x;
  }

  /**
   * @see org.mklab.tool.control.system.sampled.SampledDataDynamicSystem#getContinuousState()
   */
  public RM getContinuousState() {
    RM x = this.sunit.createZeroGrid(0, 1);
    for (final ContinuousExplicitDynamicSystem<RS,RM,CS,CM> system : this.continuousDynamicSystems) {
      x = x.appendDown(system.getState());
    }
    return x;
  }

  /**
   * @see org.mklab.tool.control.system.sampled.SampledDataDynamicSystem#getDiscreteState()
   */
  public RM getDiscreteState() {
    RM x = this.sunit.createZeroGrid(0, 1);
    for (final DiscreteDynamicSystem<RS,RM,CS,CM> system : this.discreteDynamicSystems) {
      x = x.appendDown(system.getState());
    }

    return x;
  }

  /**
   * {@inheritDoc}
   */
  public void setContinuousInitialState(final RM initialState) {
    if (this.continuousDynamicSystems == null) {
      return;
    }

    int stateSize = 0;
    int offset = 1;
    for (final ContinuousExplicitDynamicSystem<RS,RM,CS,CM> system : this.continuousDynamicSystems) {
      final int size = ((SystemOperator<RS,RM,CS,CM>)system).getStateSize();
      if (size > 0) {
        final int end = offset + size - 1;
        if (end > initialState.getRowSize()) {
          throw new IllegalArgumentException(MessageFormat.format(Messages.getString("BlockSampledDataDynamicSystem.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("BlockSampledDynamicSystem.1"), Integer.valueOf(initialState.getRowSize()), Integer.valueOf(stateSize))); //$NON-NLS-1$
    }
  }

  /**
   * {@inheritDoc}
   */
  public void setDiscreteInitialState(final RM initialState) {
    if (this.discreteDynamicSystems == null) {
      return;
    }

    int stateSize = 0;
    int offset = 1;
    for (final DiscreteDynamicSystem<RS,RM,CS,CM> system : this.discreteDynamicSystems) {
      final int size = ((SystemOperator<RS,RM,CS,CM>)system).getStateSize();
      if (size > 0) {
        final int end = offset + size - 1;
        if (end > initialState.getRowSize()) {
          throw new IllegalArgumentException(MessageFormat.format(Messages.getString("BlockSampledDataDynamicSystem.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("BlockSampledDynamicSystem.1"), Integer.valueOf(initialState.getRowSize()), Integer.valueOf(stateSize))); //$NON-NLS-1$
    }
  }

  /**
   * {@inheritDoc}
   */
  public void setContinuousState(final RM state) {
    if (this.continuousDynamicSystems == null) {
      return;
    }

    int stateSize = 0;
    int offset = 1;
    for (final ContinuousExplicitDynamicSystem<RS,RM,CS,CM> system : this.continuousDynamicSystems) {
      final int size = ((SystemOperator<RS,RM,CS,CM>)system).getStateSize();
      if (size > 0) {
        final int end = offset + size - 1;
        if (end > state.getRowSize()) {
          throw new IllegalArgumentException(MessageFormat.format(Messages.getString("BlockSampledDataDynamicSystem.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("BlockSampledDynamicSystem.1"), Integer.valueOf(state.getRowSize()), Integer.valueOf(stateSize))); //$NON-NLS-1$
    }
  }

  /**
   * {@inheritDoc}
   */
  public void setDiscreteState(final RM state) {
    if (this.discreteDynamicSystems == null) {
      return;
    }

    int stateSize = 0;
    int offset = 1;
    for (final DiscreteDynamicSystem<RS,RM,CS,CM> system : this.discreteDynamicSystems) {
      final int size = ((SystemOperator<RS,RM,CS,CM>)system).getStateSize();
      if (size > 0) {
        final int end = offset + size - 1;
        if (end > state.getRowSize()) {
          throw new IllegalArgumentException(MessageFormat.format(Messages.getString("BlockSampledDataDynamicSystem.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("BlockSampledDynamicSystem.1"), Integer.valueOf(state.getRowSize()), Integer.valueOf(stateSize))); //$NON-NLS-1$
    }
  }

  /**
   * 次のサンプリング周期で状態を更新すべき離散時間システムの状態を設定します。
   * 
   * @param state 現在の全離散時間システムの状態
   */
  private void setDiscreteStateForUpdating(final RM state) {
    int offset = 1;
    for (final DiscreteDynamicSystem<RS,RM,CS,CM> system : this.discreteDynamicSystems) {
      final int size = ((SystemOperator<RS,RM,CS,CM>)system).getStateSize();
      if (size > 0 && this.discreteDynamicSystemsUpdatedAtNextSamplingPoint.contains(system)) {
        system.setState(state.getSubVector(offset, offset + size - 1));
        offset += size;
      }
    }
  }

  /**
   * {@inheritDoc}
   */
  public RM continuousStateEquation(final RS t, final RM xc, final RM xd,  final RM inputOutput) throws SolverStopException {
    setContinuousState(xc);
    setDiscreteStateForUpdating(xd);

    RM dx = this.sunit.createZeroGrid(0, 1);
    for (final ContinuousExplicitDynamicSystem<RS,RM,CS,CM> system : this.continuousDynamicSystems) {
      dx = dx.appendDown(system.stateEquation(t, system.getState(), getInputNodeValueOf((SystemOperator<RS,RM,CS,CM>)system)));
    }

    return dx;
  }

  /**
   * {@inheritDoc}
   */
  public RM discreteStateEquation(final RS t, final RM xc, final RM xd,  final RM inputOutput) throws SolverStopException {
    setContinuousState(xc);
    setDiscreteStateForUpdating(xd);

    RM xNext = this.sunit.createZeroGrid(0, 1);
    for (final DiscreteDynamicSystem<RS,RM,CS,CM> system : this.discreteDynamicSystems) {
      if (this.discreteDynamicSystemsUpdatedAtNextSamplingPoint.contains(system)) {
        final int k = (int)t.divide(((Sampler<RS,RM,CS,CM>)system).getSamplingInterval()).round().toDouble();
        final RM nextX = system.stateEquation(k, system.getState(), getInputNodeValueOf((SystemOperator<RS,RM,CS,CM>)system));
        system.setState(nextX); // ??
        xNext = xNext.appendDown(nextX);
      } else {
        xNext = xNext.appendDown(system.getState());
      }
    }

    return xNext;
  }

  /**
   * {@inheritDoc}
   */
  public RM outputEquation(final RS t, final RM xc, final RM xd) throws SolverStopException {
    setContinuousState(xc);
    setDiscreteStateForUpdating(xd);

    resetNodeValue();
    setInputNodeValue(this.sunit.createZeroGrid(getInputSize(), 1));
    calcNodeValue(t);

    return getOutputNodeValue();
  }

  /**
   * {@inheritDoc}
   */
  public RM outputEquation(final RS t, final RM xc, final RM xd, final RM input) throws SolverStopException {
    setContinuousState(xc);
    setDiscreteStateForUpdating(xd);

    resetNodeValue();
    setInputNodeValue(input);
    calcNodeValue(t);

    return getOutputNodeValue();
  }

  /**
   * {@inheritDoc}
   */
  public RM differentialEquation(final RS t, final RM xc, final RM xd, final RM inputOutput) throws SolverStopException {
    return continuousStateEquation(t, xc, xd, inputOutput);
  }

  /**
   * {@inheritDoc}
   */
  public RM differenceEquation(final RS t, final RM xc, final RM xd, final RM inputOutput) throws SolverStopException {
    return discreteStateEquation(t, xc, xd, inputOutput);
  }

  /**
   * {@inheritDoc}
   */
  public RM inputOutputEquation(final RS t, final RM xc, final RM xd) throws SolverStopException {
    final RM u = this.sunit.createZeroGrid(getInputSize(), 1);
    final RM y = outputEquation(t, xc, xd);
    return u.appendDown(y);
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#initialize()
   */
  @Override
  public void initialize() {
    super.initialize();
    if (this.continuousDynamicSystems != null) {
      setContinuousState(getContinuousInitialState());
    }
    if (this.discreteDynamicSystems != null) {
      setDiscreteState(getDiscreteInitialState());
    }
  }
}