BlockDiscreteDynamicSystem.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.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.sampled.Sampler;


/**
 * 差分方程式で表現されるブロック離散時間動的システムを表わすクラスです。
 * 
 * @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 BlockDiscreteDynamicSystem<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 BlockDiscreteSystem<RS,RM,CS,CM> implements DiscreteDynamicSystem<RS,RM,CS,CM> {

  /** 結合システムにおける状態の順番 */
  private int number;

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

  /**
   * 全システムの状態の数を返します。
   * 
   * @return 全システムの状態の数を返します。
   */
  private int calcStateSize() {
    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.DynamicSystem#getInitialState()
   */
  public RM getInitialState() {
    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.DynamicSystem#getState()
   */
  public RM getState() {
    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.getState());
    }

    return x;
  }

  /**
   * {@inheritDoc}
   */
  public void setInitialState(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("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 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("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 RM stateEquation(final int k, final RM x,  final RM u) throws SolverStopException {
    setState(x);

    RM xNext = this.sunit.createZeroGrid(0, 1);
    for (final DiscreteDynamicSystem<RS,RM,CS,CM> system : this.discreteDynamicSystems) {
      final RM nextX = system.stateEquation(k, system.getState(), getInputNodeValueOf((SystemOperator<RS,RM,CS,CM>)system));
      system.setState(nextX);
      xNext = xNext.appendDown(nextX);
    }

    return xNext;
  }

  /**
   * {@inheritDoc}
   */
  public RM stateEquation(final RS t, final RM x,  final RM u) throws SolverStopException {
    setState(x);

    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()).floor().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 int k, final RM x) throws SolverStopException {
    setState(x);
    resetNodeValue();
    setInputNodeValue(this.sunit.createZeroGrid(getInputSize(), 1));
    calcNodeValue(k);
    return getOutputNodeValue();
  }

  /**
   * {@inheritDoc}
   */
  public RM outputEquation(final RS t, final RM x) throws SolverStopException {
    setState(x);
    resetNodeValue();
    setInputNodeValue(this.sunit.createZeroGrid(getInputSize(), 1));
    calcNodeValue(t);
    return getOutputNodeValue();
  }

  /**
   * {@inheritDoc}
   */
  public RM outputEquation(final int k, final RM x, final RM u) throws SolverStopException {
    setState(x);
    resetNodeValue();
    setInputNodeValue(u);
    calcNodeValue(k);
    return getOutputNodeValue();
  }

  /**
   * {@inheritDoc}
   */
  public RM outputEquation(final RS t, final RM x, final RM u) throws SolverStopException {
    setState(x);
    resetNodeValue();
    setInputNodeValue(u);
    calcNodeValue(t);
    return getOutputNodeValue();
  }

  /**
   * {@inheritDoc}
   */
  public RM differenceEquation(final int k, final RM x, final RM inputOutput) throws SolverStopException {
    final RM u = inputOutput.getSubVector(1, getInputSize());
    return stateEquation(k, x, u);
  }

  /**
   * {@inheritDoc}
   */
  public RM differenceEquation(final RS t, final RM x, final RM inputOutput) throws SolverStopException {
    final RM u = inputOutput.getSubVector(1, getInputSize());
    return stateEquation(t, x, u);
  }

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

  /**
   * {@inheritDoc}
   */
  public RM inputOutputEquation(final RS t, final RM x) throws SolverStopException {
    final RM u = this.sunit.createZeroGrid(getInputSize(), 1);
    final RM 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;
  }
}