BlockContinuousDynamicSystem.java

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

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

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

import org.mklab.nfc.matrix.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.IntMatrix;
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;


/**
 * 微分方程式で表現されるブロック連続時間動的システムを表わすクラスです。
 * 
 * @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 abstract class BlockContinuousDynamicSystem<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 BlockContinuousSystem<RS,RM,CS,CM> implements ContinuousDynamicSystem<RS,RM,CS,CM>  {
  /** 結合システムにおける状態の順番 */
  private int number;

  /**
   * 新しく生成された<code>BlockContinuousDynamicSystem</code>オブジェクトを初期化します。
   * 
   * @param elements 隣接行列
   * @param inputNodes 入力ノードの番号のリスト(番号は1から始まります)
   * @param outputNodes 出力ノードの番号のリスト(番号は1から始まります)
   * @param sunit unit of scalar
   */
  public BlockContinuousDynamicSystem(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 ContinuousDynamicSystem<RS,RM,CS,CM>  system : this.continuousDynamicSystems) {
      count = count + ((SystemOperator<RS,RM,CS,CM> )system).getStateSize();
    }

    return count;
  }

  /**
   * {@inheritDoc}
   */
  public RM getInitialState() {
    RM x = this.sunit.createZeroGrid(0, 1);

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

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

  /**
   * {@inheritDoc}
   */
  public RM getInitialStateDerivative() {
    RM x = this.sunit.createZeroGrid(0, 1);

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

    for (final ContinuousDynamicSystem<RS,RM,CS,CM>  system : this.continuousDynamicSystems) {
      x = x.appendDown(system.getInitialStateDerivative());
    }
    return x;
  }
  
  /**
   * {@inheritDoc}
   */
  public IntMatrix getIndex() {
    IntMatrix index = new IntMatrix(0, 1);

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

    for (final ContinuousDynamicSystem<RS,RM,CS,CM>  system : this.continuousDynamicSystems) {
      index = index.appendDown(system.getIndex());
    }
    return index;
  }

  /**
   * {@inheritDoc}
   */
  public RM getState() {
    RM x = this.sunit.createZeroGrid(0, 1);

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

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

    return x;
  }

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

    int stateSize = 0;
    int offset = 1;
    for (final ContinuousDynamicSystem<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("BlockContinuousDynamicSystem.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("BlockContinuousDynamicSystem.1"), Integer.valueOf(initialState.getRowSize()), Integer.valueOf(stateSize))); //$NON-NLS-1$
    }
  }
  
  /**
   * {@inheritDoc}
   */
  public void setInitialStateDerivative(final RM initialStateDerivative) {
    if (this.continuousDynamicSystems == null) {
      return;
    }

    int stateSize = 0;
    int offset = 1;
    for (final ContinuousDynamicSystem<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 > initialStateDerivative.getRowSize()) {
          throw new IllegalArgumentException(MessageFormat.format(Messages.getString("BlockContinuousDynamicSystem.0"), system.getClass().getName())); //$NON-NLS-1$
        }
        system.setInitialStateDerivative(initialStateDerivative.getSubVector(offset, end));
        offset += size;
        stateSize += size;
      }
    }

    if (initialStateDerivative.getRowSize() != stateSize) {
      throw new IllegalArgumentException(MessageFormat.format(Messages.getString("BlockContinuousDynamicSystem.1"), Integer.valueOf(initialStateDerivative.getRowSize()), Integer.valueOf(stateSize))); //$NON-NLS-1$
    }
  }

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

    int stateSize = 0;
    int offset = 1;
    for (final ContinuousDynamicSystem<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("BlockContinuousDynamicSystem.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("BlockContinuousDynamicSystem.1"), Integer.valueOf(state.getRowSize()), Integer.valueOf(stateSize))); //$NON-NLS-1$
    }
  }

  /**
   * {@inheritDoc}
   */
  public void setIndex(final IntMatrix index) {
    if (this.continuousDynamicSystems == null) {
      return;
    }

    int stateSize = 0;
    int offset = 1;
    for (final ContinuousDynamicSystem<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 > index.getRowSize()) {
          throw new IllegalArgumentException(MessageFormat.format(Messages.getString("BlockContinuousDynamicSystem.2"), system.getClass().getName())); //$NON-NLS-1$
        }
        system.setIndex(index.getSubVector(offset, end));
        offset += size;
        stateSize += size;
      }
    }

    if (index.getRowSize() != stateSize) {
      throw new IllegalArgumentException(MessageFormat.format(Messages.getString("BlockContinuousDynamicSystem.3"), Integer.valueOf(index.getRowSize()), Integer.valueOf(stateSize))); //$NON-NLS-1$
    }
  }

  /**
   * {@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 RS t, final RM x, final RM u) throws SolverStopException {
    setState(x);
    resetNodeValue();
    setInputNodeValue(u);
    calcNodeValue(t);
    return getOutputNodeValue();
  }

  /**
   * {@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);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void initialize() {
    super.initialize();
    setState(getInitialState());
  }

  /**
   * {@inheritDoc}
   */
  public int getStateNumber() {
    return this.number;
  }

  /**
   * {@inheritDoc}
   */
  public void setStateNumber(final int number) {
    this.number = number;
  }
  
  /**
   * {@inheritDoc}
   */
  public RM getStateDerivative() {
    RM x = this.sunit.createZeroGrid(0, 1);

    if (this.continuousDynamicSystems == null) {
      return x;
    }
 
    for (final ContinuousDynamicSystem<RS,RM,CS,CM>  system : this.continuousDynamicSystems) {
      x = x.appendDown(system.getStateDerivative());
    }

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

    int stateSize = 0;
    int offset = 1;
    for (final ContinuousDynamicSystem<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 > stateDerivative.getRowSize()) {
          throw new IllegalArgumentException(MessageFormat.format(Messages.getString("BlockContinuousDynamicSystem.0"), system.getClass().getName())); //$NON-NLS-1$
        }
        system.setStateDerivative(stateDerivative.getSubVector(offset, end));
        offset += size;
        stateSize += size;
      }
    }

    if (stateDerivative.getRowSize() != stateSize) {
      throw new IllegalArgumentException(MessageFormat.format(Messages.getString("BlockContinuousDynamicSystem.1"), Integer.valueOf(stateDerivative.getRowSize()), Integer.valueOf(stateSize))); //$NON-NLS-1$
    }
  }

}