SystemSolver.java

/*
 * Created on 2008/11/19
 * Copyright (C) 2008 Koga Laboratory. All rights reserved.
 *
 */
package org.mklab.tool.control.system;

import org.mklab.nfc.matrix.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.RealNumericalMatrix;
import org.mklab.nfc.ode.AlgebraicEquationSolver;
import org.mklab.nfc.ode.ContinuousAlgebraicSystem;
import org.mklab.nfc.ode.ContinuousDiscreteAlgebraicSystem;
import org.mklab.nfc.ode.DifferenceEquationSolver;
import org.mklab.nfc.ode.DifferenceSystem;
import org.mklab.nfc.ode.DifferentialDifferenceSystem;
import org.mklab.nfc.ode.DifferentialEquationAutoSolver;
import org.mklab.nfc.ode.DifferentialEquationSolver;
import org.mklab.nfc.ode.DiscreteAlgebraicSystem;
import org.mklab.nfc.ode.EquationSolver;
import org.mklab.nfc.ode.ExplicitDifferentialSystem;
import org.mklab.nfc.ode.ImplicitDifferentialEquationSolver;
import org.mklab.nfc.ode.ImplicitDifferentialSystem;
import org.mklab.nfc.ode.SolverStopException;
import org.mklab.nfc.scalar.ComplexNumericalScalar;
import org.mklab.nfc.scalar.RealNumericalScalar;
import org.mklab.tool.control.system.continuous.ContinuousExplicitDynamicSystem;
import org.mklab.tool.control.system.continuous.ContinuousImplicitDynamicSystem;
import org.mklab.tool.control.system.continuous.ContinuousStaticSystem;
import org.mklab.tool.control.system.discrete.DiscreteDynamicSystem;
import org.mklab.tool.control.system.discrete.DiscreteStaticSystem;
import org.mklab.tool.control.system.sampled.SampledDataDynamicSystem;
import org.mklab.tool.control.system.sampled.SampledDataStaticSystem;


/**
 * {@link SystemOperator}で表されるシステムの時間応答を求めるクラスです。
 * 
 * @author koga
 * @version $Revision$, 2008/11/19
 * @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 SystemSolver<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>> {

  /** 方程式のソルバー */
  private EquationSolver<RS,RM,CS,CM> solver;

  /**
   * 新しく生成された<code>SystemSolver</code>オブジェクトを初期化します。
   * 
   * @param solver 方程式のソルバー
   */
  public SystemSolver(final EquationSolver<RS,RM,CS,CM> solver) {
    this.solver = solver;
  }

  /**
   * 固定刻み幅でシミュレーション計算を行います。
   * 
   * @param system シミュレーション対象
   * @param t0 シミュレーション開始時刻
   * @param t1 シミュレーション終了時刻
   * @exception SolverStopException ソルバーが停止された場合
   */
  public void solve(SystemOperator<RS,RM,CS,CM> system, RS t0, RS t1) throws SolverStopException {
    system.initialize();
    
    if (system instanceof SampledDataStaticSystem && this.solver instanceof AlgebraicEquationSolver) {
      ((AlgebraicEquationSolver<RS,RM,CS,CM>)this.solver).solve((ContinuousDiscreteAlgebraicSystem<RS,RM,CS,CM>)system, t0, t1);
      return;
    }

    if (system instanceof ContinuousStaticSystem && this.solver instanceof AlgebraicEquationSolver) {
      ((AlgebraicEquationSolver<RS,RM,CS,CM>)this.solver).solve((ContinuousAlgebraicSystem<RS,RM,CS,CM>)system, t0, t1);
      return;
    }

    if (system instanceof DiscreteStaticSystem && this.solver instanceof AlgebraicEquationSolver) {
      ((AlgebraicEquationSolver<RS,RM,CS,CM>)this.solver).solve((DiscreteAlgebraicSystem<RS,RM,CS,CM>)system, t0, t1);
      return;
    }

    if (system instanceof DiscreteDynamicSystem && this.solver instanceof DifferenceEquationSolver) {
      RM xd0 = ((DiscreteDynamicSystem<RS,RM,CS,CM>)system).getInitialState();
      ((DifferenceEquationSolver<RS,RM,CS,CM>)this.solver).solve((DifferenceSystem<RS,RM,CS,CM>)system, t0, t1, xd0);
      return;
    }

    if (system instanceof ContinuousExplicitDynamicSystem && this.solver instanceof DifferentialEquationSolver) {
      final RM xc0 = ((ContinuousExplicitDynamicSystem<RS,RM,CS,CM>)system).getInitialState();
      ((DifferentialEquationSolver<RS,RM,CS,CM>)this.solver).solve((ExplicitDifferentialSystem<RS,RM,CS,CM>)system, t0, t1, xc0);
      return;
    }
    
    if (system instanceof ContinuousImplicitDynamicSystem && this.solver instanceof DifferentialEquationSolver) {
      final RM xc0 = ((ContinuousImplicitDynamicSystem<RS,RM,CS,CM>)system).getInitialState();
      ((ImplicitDifferentialEquationSolver<RS,RM,CS,CM>)this.solver).solve((ImplicitDifferentialSystem<RS,RM,CS,CM>)system, t0, t1, xc0);
      return;
    }

    if (system instanceof SampledDataDynamicSystem && this.solver instanceof DifferentialEquationSolver) {
      final RM xc0 = ((SampledDataDynamicSystem<RS,RM,CS,CM>)system).getContinuousInitialState();
      final RM xd0 = ((SampledDataDynamicSystem<RS,RM,CS,CM>)system).getDiscreteInitialState();
      ((DifferentialEquationSolver<RS,RM,CS,CM>)this.solver).solve((DifferentialDifferenceSystem<RS,RM,CS,CM>)system, t0, t1, xc0, xd0);
      return;
    }

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

  /**
   * 指定された許容誤差でシミュレーション計算を行います。
   * 
   * @param system シミュレーション対象
   * @param t0 シミュレーション開始時刻
   * @param t1 シミュレーション終了時刻
   */
  public void solveAuto(SystemOperator<RS,RM,CS,CM> system, RS t0, RS t1) {
    system.initialize();

    if (system instanceof ContinuousExplicitDynamicSystem && this.solver instanceof DifferentialEquationAutoSolver) {
      final RM xc0 = ((ContinuousExplicitDynamicSystem<RS,RM,CS,CM>)system).getInitialState();
      ((DifferentialEquationAutoSolver<RS,RM,CS,CM>)this.solver).solveAuto((ExplicitDifferentialSystem<RS,RM,CS,CM>)system, t0, t1, xc0);
      return;
    }
    
    if (system instanceof ContinuousImplicitDynamicSystem && this.solver instanceof DifferentialEquationAutoSolver) {
      final RM xc0 = ((ContinuousImplicitDynamicSystem<RS,RM,CS,CM>)system).getInitialState();
      ((ImplicitDifferentialEquationSolver<RS,RM,CS,CM>)this.solver).solveAuto((ImplicitDifferentialSystem<RS,RM,CS,CM>)system, t0, t1, xc0);
      return;
    }

    if (system instanceof SampledDataDynamicSystem && this.solver instanceof DifferentialEquationAutoSolver) {
      final RM xc0 = ((SampledDataDynamicSystem<RS,RM,CS,CM>)system).getContinuousInitialState();
      final RM xd0 = ((SampledDataDynamicSystem<RS,RM,CS,CM>)system).getDiscreteInitialState();
      ((DifferentialEquationAutoSolver<RS,RM,CS,CM>)this.solver).solveAuto((DifferentialDifferenceSystem<RS,RM,CS,CM>)system, t0, t1, xc0, xd0);
      return;
    }

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