BlockSampledDataSystem.java

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

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

import java.util.ArrayList;
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.DynamicSystem;
import org.mklab.tool.control.system.LinearSystemOperator;
import org.mklab.tool.control.system.StaticSystem;
import org.mklab.tool.control.system.SystemOperator;
import org.mklab.tool.control.system.continuous.ContinuousExplicitDynamicSystem;
import org.mklab.tool.control.system.continuous.ContinuousLinearDynamicSystem;
import org.mklab.tool.control.system.discrete.DiscreteLinearDynamicSystem;


/**
 * サンプル値システムのブロックシステムを表わすクラスです。
 * 
 * @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 BlockSampledDataSystem<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 BlockSamplingSystem<RS,RM,CS,CM> {

  /** 連続時間動的システムのリスト */
  protected List<ContinuousExplicitDynamicSystem<RS,RM,CS,CM>> continuousDynamicSystems = new ArrayList<>();

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

  /**
   * 連続時間動的システムのリストを設定します。
   */
  private void setupContinuousDynamicSystemList() {
    this.continuousDynamicSystems.clear();

    final int size = getNodeSize();

    for (int row = 0; row < size; row++) {
      for (int column = 0; column < size; column++) {
        final SystemOperator<RS,RM,CS,CM> system = getSystemOperator(row, column);
        if (system instanceof ContinuousExplicitDynamicSystem) {
          this.continuousDynamicSystems.add((ContinuousExplicitDynamicSystem<RS,RM,CS,CM>)system);
        }
      }
    }
  }

  /**
   * ノードの値を計算します。
   * 
   * @param t 時間
   * @throws SolverStopException ソルバーが停止された場合
   */
  protected void calcNodeValue(final RS t) throws SolverStopException {
    this.time = t;
    calcNodeValue();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected RM calcOutputOfDirectFeedthroughSystem(final SystemOperator<RS,RM,CS,CM> system, final RM u) throws SolverStopException {
    if (system instanceof DynamicSystem) {
      DynamicSystem<RS,RM,CS,CM> dSystem = (DynamicSystem<RS,RM,CS,CM>)system;
      return dSystem.outputEquation(this.time, dSystem.getState(), u);
    }

    if (system instanceof StaticSystem) {
      return ((StaticSystem<RS,RM,CS,CM>)system).outputEquation(this.time, u);
    }

    throw new RuntimeException(Messages.getString("BlockSampledDataSystem.0")); //$NON-NLS-1$
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected RM calcOutputOfNonDirectFeedthroughSystem(final SystemOperator<RS,RM,CS,CM> system) throws SolverStopException {
    if (system instanceof DynamicSystem) {
      DynamicSystem<RS,RM,CS,CM> dSystem = (DynamicSystem<RS,RM,CS,CM>)system;
      return dSystem.outputEquation(this.time, dSystem.getState());
    }

    if (system instanceof StaticSystem) {
      return ((StaticSystem<RS,RM,CS,CM>)system).outputEquation(this.time);
    }

    throw new RuntimeException(Messages.getString("BlockSampledDataSystem.1")); //$NON-NLS-1$
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected SystemOperator<RS,RM,CS,CM> createStrictlyProperLinearDynamicSystem(final SystemOperator<RS,RM,CS,CM> system) {
    final RM a = ((LinearSystemOperator<RS,RM,CS,CM>)system).getLinearSystem().getA();
    final RM b = ((LinearSystemOperator<RS,RM,CS,CM>)system).getLinearSystem().getB();
    final RM c = ((LinearSystemOperator<RS,RM,CS,CM>)system).getLinearSystem().getC();

    if (system instanceof ContinuousExplicitDynamicSystem) {
      final ContinuousLinearDynamicSystem<RS,RM,CS,CM> newSystem = new ContinuousLinearDynamicSystem<>(a, b, c, this.sunit);
      newSystem.setInitialState(((ContinuousLinearDynamicSystem<RS,RM,CS,CM>)system).getInitialState());
      return newSystem;
    }

    final DiscreteLinearDynamicSystem<RS,RM,CS,CM> newSystem = new DiscreteLinearDynamicSystem<>(a, b, c, this.sunit);
    newSystem.setSamplingInterval(((DiscreteLinearDynamicSystem<RS,RM,CS,CM>)system).getSamplingInterval());
    newSystem.setAtSamplingPoint(((DiscreteLinearDynamicSystem<RS,RM,CS,CM>)system).isAtSamplingPoint());
    newSystem.setInitialState(((DiscreteLinearDynamicSystem<RS,RM,CS,CM>)system).getInitialState());
    return newSystem;
  }

  /**
   * {@inheritDoc}
   */
  @Override  protected boolean replaceDynamicSystemList(final SystemOperator<RS,RM,CS,CM> oldSystem, final SystemOperator<RS,RM,CS,CM> newSystem) {
    if (super.replaceDynamicSystemList(oldSystem, newSystem)) {
      return true;
    }

    if (oldSystem instanceof ContinuousExplicitDynamicSystem) {
      final int index = this.continuousDynamicSystems.indexOf((ContinuousExplicitDynamicSystem<RS,RM,CS,CM>)oldSystem);
      if (index == -1) {
        return false;
      }
      this.continuousDynamicSystems.set(index, (ContinuousExplicitDynamicSystem<RS,RM,CS,CM>)newSystem);
      return true;
    }

    return false;
  }
}