| #region Copyright © 2009, De Santiago-Castillo JA. All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
| #endregion
|
|
|
|
|
| using System;
|
| using System.Collections.Generic;
|
| using System.Text;
|
|
|
| using DotNumerics.ODE.Dopri5;
|
| using DotNumerics.ODE.Radau5;
|
|
|
| namespace DotNumerics.ODE
|
| {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| internal class RKSolOut: ISOLOUT, ISOLOUTR
|
| {
|
|
|
| protected enum SolutionOutType { Array, Delegate }
|
|
|
|
|
| #region Fields
|
|
|
| CONTD5 _Contd5ExpicitRK;
|
|
|
| CONTR5 _Contr5ImplicitRK;
|
|
|
|
|
|
|
|
|
| protected int _NEquations;
|
|
|
|
|
|
|
|
|
| protected double[] _Y0;
|
|
|
|
|
|
|
|
|
| protected bool _IsTimeArrayUsed = false;
|
|
|
|
|
|
|
|
|
| protected double _T0 = 0;
|
|
|
|
|
|
|
| protected double _TF = 1;
|
|
|
| protected double _DeltaT = 1;
|
|
|
|
|
|
|
| protected double[] _TSpan;
|
|
|
|
|
|
|
|
|
| protected int _MaxIndex = 0;
|
|
|
|
|
|
|
|
|
| protected double[,] _SolutionArray;
|
|
|
|
|
|
|
|
|
| protected OdeSolution _SolutionOut;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| protected SolutionOutType _SolutionOutType;
|
|
|
|
|
|
|
|
|
| protected double[] _TemporalSolution;
|
|
|
|
|
|
|
|
|
| protected bool _isDeltaPositive = true;
|
|
|
|
|
| protected int _Index = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| protected double _T = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #endregion
|
|
|
| #region Constructor
|
|
|
| public RKSolOut(CONTD5 contd5ExpicitRK)
|
| {
|
| this._Contd5ExpicitRK = contd5ExpicitRK;
|
| }
|
|
|
|
|
| public RKSolOut(CONTR5 contr5ImplicitRK)
|
| {
|
| this._Contr5ImplicitRK = contr5ImplicitRK;
|
| }
|
|
|
|
|
| #endregion
|
|
|
|
|
| #region Methods
|
|
|
| internal void Initialize(double[] y0, double t0, double deltaT, double tf, OdeSolution solution)
|
| {
|
| this._SolutionOutType = SolutionOutType.Delegate;
|
| this.InitializeValues(y0, t0, deltaT, tf);
|
|
|
| this._SolutionOut = solution;
|
|
|
| }
|
|
|
| internal void Initialize(double[] y0, double t0, double deltaT, double tf, out double[,] solutionArray)
|
| {
|
| this._SolutionOutType = SolutionOutType.Array;
|
| this.InitializeValues(y0, t0, deltaT, tf);
|
|
|
| int NCols = y0.Length + 1;
|
| solutionArray = new double[this._MaxIndex, NCols];
|
|
|
| this._SolutionArray = solutionArray;
|
|
|
| }
|
|
|
| internal void Initialize(double[] y0, double[] tspan, OdeSolution solution)
|
| {
|
| this._SolutionOutType = SolutionOutType.Delegate;
|
| this.InitializeValues(y0, tspan);
|
|
|
| this._SolutionOut = solution;
|
|
|
| }
|
|
|
| internal void Initialize(double[] y0, double[] tspan, out double[,] solutionArray)
|
| {
|
| this._SolutionOutType = SolutionOutType.Array;
|
| this.InitializeValues(y0, tspan);
|
|
|
| int NCols = y0.Length + 1;
|
| solutionArray = new double[this._MaxIndex, NCols];
|
|
|
| this._SolutionArray = solutionArray;
|
|
|
| }
|
|
|
|
|
| private void InitializeValues(double[] y0, double t0, double deltaT, double tf)
|
| {
|
| this._IsTimeArrayUsed = false;
|
|
|
| this._NEquations = y0.Length;
|
| this._Y0 = y0;
|
| this._T0 = t0;
|
| this._DeltaT = deltaT;
|
| this._TF = tf;
|
| this._MaxIndex = (int)(Math.Abs(tf - t0) / Math.Abs(deltaT)) + 1;
|
|
|
| if (deltaT > 0) this._isDeltaPositive = true;
|
| else this._isDeltaPositive = false;
|
|
|
| this._TemporalSolution = new double[y0.Length];
|
|
|
| }
|
|
|
|
|
| private void InitializeValues(double[] y0, double[] tspan)
|
| {
|
| this._IsTimeArrayUsed = true;
|
|
|
| this._NEquations = y0.Length;
|
| this._Y0 = y0;
|
|
|
| this._TSpan = tspan;
|
| this._MaxIndex = tspan.Length;
|
|
|
|
|
| if ((tspan[1] - tspan[0]) > 0) this._isDeltaPositive = true;
|
| else this._isDeltaPositive = false;
|
|
|
| this._TemporalSolution = new double[y0.Length];
|
|
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| protected double GetTime(int index)
|
| {
|
| double t = 0;
|
|
|
| if (this._IsTimeArrayUsed == true)
|
| {
|
| t = this._TSpan[index];
|
| }
|
| else
|
| {
|
| t = _T0 + _DeltaT * index;
|
| }
|
|
|
| return t;
|
| }
|
|
|
| #endregion
|
|
|
|
|
|
|
|
|
| #region ISOLOUT Members
|
|
|
| void ISOLOUT.Run(int NR, double XOLD, double X, double[] Y, int o_y, int N, double[] CON,
|
| int o_con, int[] ICOMP, int o_icomp, int ND, double[] RPAR, int o_rpar, int IPAR, int IRTRN)
|
| {
|
|
|
|
|
| #region Array Index Correction
|
|
|
|
|
|
|
|
|
| int c_y = -1 + o_y; int c_con = -1 + o_con; int c_icomp = -1 + o_icomp; int c_rpar = -1 + o_rpar;
|
|
|
| #endregion
|
|
|
|
|
| if (NR == 1)
|
| {
|
| if (this._SolutionOutType == SolutionOutType.Array)
|
| {
|
| this._SolutionArray[0, 0] = this._T0;
|
| for (int j = 1; j <= this._NEquations; j++)
|
| {
|
| this._SolutionArray[0, j] = this._Y0[j - 1];
|
| }
|
| }
|
| else
|
| {
|
| this._SolutionOut(this._T0, this._Y0);
|
| }
|
|
|
| this._Index = 1;
|
| }
|
|
|
|
|
| bool MyContinue = true;
|
|
|
| while (MyContinue && this._Index < this._MaxIndex)
|
| {
|
| this._T = this.GetTime(this._Index);
|
|
|
| if ((this._isDeltaPositive && X >= this._T) || (this._isDeltaPositive == false && X <= this._T))
|
| {
|
| if (this._SolutionOutType == SolutionOutType.Array)
|
| {
|
| this._SolutionArray[this._Index, 0] = this._T;
|
| for (int j = 1; j <= this._NEquations; j++)
|
| {
|
| this._SolutionArray[this._Index, j] = this._Contd5ExpicitRK.Run(j, this._T, CON, o_con, ICOMP, o_icomp, ND);
|
| }
|
| }
|
| else
|
| {
|
| for (int j = 0; j < this._NEquations; j++)
|
| {
|
| this._TemporalSolution[j] = this._Contd5ExpicitRK.Run(j + 1, this._T, CON, o_con, ICOMP, o_icomp, ND);
|
| }
|
| this._SolutionOut(this._T, this._TemporalSolution);
|
| }
|
|
|
| this._Index++;
|
| }
|
| else MyContinue = false;
|
| }
|
| return;
|
| }
|
|
|
| #endregion
|
|
|
| #region ISOLOUTR Members
|
|
|
| void ISOLOUTR.Run(int NR, double XOLD, double X, double[] Y, int o_y, double[] CONT, int o_cont,
|
| int LRC, int N, double RPAR, int IPAR, int IRTRN)
|
| {
|
|
|
| #region Array Index Correction
|
|
|
|
|
|
|
|
|
| int c_y = -1 + o_y; int c_cont = -1 + o_cont;
|
|
|
| #endregion
|
|
|
|
|
| if (NR == 1)
|
| {
|
| if (this._SolutionOutType == SolutionOutType.Array)
|
| {
|
| this._SolutionArray[0, 0] = this._T0;
|
| for (int j = 1; j <= this._NEquations; j++)
|
| {
|
| this._SolutionArray[0, j] = this._Y0[j - 1];
|
| }
|
| }
|
| else
|
| {
|
| this._SolutionOut(this._T0, this._Y0);
|
| }
|
|
|
| this._Index = 1;
|
| }
|
|
|
|
|
| bool MyContinue = true;
|
|
|
| while (MyContinue && this._Index < this._MaxIndex)
|
| {
|
| this._T = this.GetTime(this._Index);
|
|
|
| if ((this._isDeltaPositive && X >= this._T) || (this._isDeltaPositive == false && X <= this._T))
|
| {
|
| if (this._SolutionOutType == SolutionOutType.Array)
|
| {
|
| this._SolutionArray[this._Index, 0] = this._T;
|
| for (int j = 1; j <= this._NEquations; j++)
|
| {
|
| this._SolutionArray[this._Index, j] = this._Contr5ImplicitRK.Run(j, this._T, CONT, o_cont, LRC);
|
| }
|
| }
|
| else
|
| {
|
| for (int j = 0; j < this._NEquations; j++)
|
| {
|
| this._TemporalSolution[j] = this._Contr5ImplicitRK.Run(j + 1, this._T, CONT, o_cont, LRC);
|
| }
|
| this._SolutionOut(this._T, this._TemporalSolution);
|
| }
|
|
|
| this._Index++;
|
| }
|
| else MyContinue = false;
|
| }
|
| return;
|
|
|
|
|
| }
|
|
|
| #endregion
|
| }
|
| }
|
|
|