|
|
◆ Seulexa()
| Sub Seulexa |
( |
N As |
Long, |
|
|
F As |
LongPtr, |
|
|
Ifcn As |
Long, |
|
|
T As |
Double, |
|
|
Y() As |
Double, |
|
|
Tout As |
Double, |
|
|
Tend As |
Double, |
|
|
RTol() As |
Double, |
|
|
ATol() As |
Double, |
|
|
Mode As |
Long, |
|
|
Info As |
Long, |
|
|
Optional Neval As |
Long, |
|
|
Optional Njac As |
Long, |
|
|
Optional Nstep As |
Long, |
|
|
Optional Naccept As |
Long, |
|
|
Optional Nreject As |
Long, |
|
|
Optional Ndec As |
Long, |
|
|
Optional Nsol As |
Long, |
|
|
Optional Fjac As |
LongPtr = NullPtr, |
|
|
Optional Mljac As |
Long = -1, |
|
|
Optional Mujac As |
Long, |
|
|
Optional Fmas As |
LongPtr = NullPtr, |
|
|
Optional Mlmas As |
Long = -1, |
|
|
Optional Mumas As |
Long, |
|
|
Optional Hes As |
Long, |
|
|
Optional MaxIter As |
Long, |
|
|
Optional Km As |
Long, |
|
|
Optional Nsequ As |
Long, |
|
|
Optional Lambda As |
Long, |
|
|
Optional M1 As |
Long, |
|
|
Optional M2 As |
Long, |
|
|
Optional Cnt As |
Long, |
|
|
Optional Hinit As |
Double, |
|
|
Optional Hmax As |
Double, |
|
|
Optional Thet As |
Double, |
|
|
Optional Fac1 As |
Double, |
|
|
Optional Fac2 As |
Double, |
|
|
Optional Fac3 As |
Double, |
|
|
Optional Fac4 As |
Double, |
|
|
Optional Safe1 As |
Double, |
|
|
Optional Safe2 As |
Double, |
|
|
Optional Wkfcn As |
Double, |
|
|
Optional Wkjac As |
Double, |
|
|
Optional Wkdec As |
Double, |
|
|
Optional Wksol As |
Double |
|
) |
| |
Initial value problem of ordinary differential equations (extrapolation method based on the linearly implicit Euler method)
- Purpose
- This program computes a numerical solution of a stiff (or differential algebraic) system of first order ordinary differential equations of the form
M * dy/dt = f(t, y), y = y0 at t = t0
where y is the n vector and the system consists of n equations. M is the mass matrix. t0 and y0 are the given initial values of t and y, respectively.
This code is the rewritten version of the extrapolation method based on the linearly implicit Euler method code, SEULEX (Reference (1)).
- Parameters
-
| [in] | N | Number of differential equations. (N >= 1) |
| [in] | F | The user supplied subroutine, which calculates the derivatives of the differential equations, defined as follows. Sub F(N As Long, T As Double, Y() As Double, Yp() As Double)
Compute dy/dt and store in Yp().
End Sub
where N is the number of equations, and dy/dt (= f(t, y)) is the computed derivative at given T and Y(). |
| [in] | Ifcn | Specifies whether f(t, y) depends on t.
= 0: f(t, y) is independent of t (autonomous).
= 1: f(t, y) depends on t (non-autonomous). |
| [in,out] | T | Independent variable t. This program integrates from the initial value of t to Tend. Depending on the setting of Mode, this program may return at Tout or every successful step to provide an intermediate result. [in] An initial value of t (t0).
[out] T = Tend if the integration has been completed. Or T = Tout or T of the latest step depending on Mode in the case of intermediate return. |
| [in,out] | Y() | Array Y(LY - 1) (LY >= N)
Dependent variable y.
[in] Initial value of y (y0) at initial t (t0).
[out] The value of y (computed numerical solution) at t = T. |
| [in] | Tout | Set Tout to the point (t) at which an intermediate result is desired in mode = 2 or 3. Tout is not referenced in Mode = 0 or 1.
In Mode 2 or 3, y at Tout is computed and returned with Info = 2 or 3 respectively. To continue the integration to get result at new Tout, it is possible to call this program again with new Tout and without changing other variables including Info.
T < Tout <= Tend is required. However, if the integration is in backward direction, Tend <= Tout < T is required. If Tend is reached before Tout, the integration is terminated immediately and T = Tend and Info = 0 is returned. |
| [in] | Tend | The point at which an integration is completed. If Tend is reached, the integration is terminated and T = Tend and Info = 0 is returned. The integration is possible for both forward (Tend > T) and backward (Tend < T) direction. |
| [in] | Rtol() | Array Rtol(LRtol - 1) (LRtol >= 1) (all elements of Rtol() >= 0)
The relative error tolerance(s) to specify how accurately compute the solution. This parameter may be a scalar or an array according to the values of LRtol and LAtol (Scalar if LRtol < N or LAtol < N, array if LRtol >= N and LAtol >= N).
Rtol is used together with Atol in a local error test at each step. The integration is performed using step sizes which are automatically selected so as to achieve the following criteria.
Scalar case (i = 0 to N - 1):
(local error of Y(i)) <= Rtol(0)*Abs(Y(i)) + Atol(0)
Both Rtol(0) and Atol(0) must not be 0 at the same time.
Array case (i = 0 to N - 1):
(local error of Y(i)) <= Rtol(i)*Abs(Y(i)) + Atol(i)
Both Rtol(i) and Atol(i) must not be 0 at the same time. |
| [in] | ATol() | Array Atol(LAtol - 1) (LAtol >= 1) (all elements of Atol() >= 0)
The absolute error tolerance(s) to specify how accurately compute the solution. This parameter may be a scalar or an array according to the values of LRtol and LAtol (Scalar if LRtol < N or LAtol < N, array if LRtol >= N and LAtol >= N).
Atol is used together with Rtol in a local error test at each step (Refer to Rtol above). |
| [in] | Mode | Mode of operation.
This program integrates from t0 to Tend. Four modes of operation are provided depending on the timing of returning imtermediate results. When Tend is reached, the integration is terminated and Info = 0 is returned in any of four modes. = 0: Not return until Tend. Tout is not referenced.
= 1: Returns at every successful step (Info = 1 is returned). Tout is not referenced. The end point of the last step is returned in T. In the final step, the step size is adjusted to fit into Tend.
= 2: Returns at Tout during the ingegration to provide an intermediate result (T = Tout and Info = 2 are returned). To continue the integration to get result at new Tout, it is possible to call this program again with new Tout. In the final step, the step size is adjusted to fit into Tout.
= 3: Returns at Tout during the ingegration to provide an intermediate result (T = Tout and Info = 3 are returned). To continue the integration to get result at new Tout, it is possible to call this program again with new Tout. Different from Mode = 2, the value of Y() at Tout is computed by interpolation, and the step size is not adjusted even in the last step before Tout. The steps through integration are same with those of Mode = 1. |
| [in,out] | Info | [in] Control code.
= 0: Set Info = 0 on the initial call to start new problem. All variables will be initialized and the computation will begin.
= 1, 2, 3: When returned with Info = 1, 2 or 3, it is possible to call this program again with new Tout and without changing Info to continue the integration.
[out] Return code.
= 0: Successful exit. Integration to Tend completed.
< 0: The (-Info)-th argument is invalid.
= 1: Returned to provide intermediate result in Mode = 1. It is possible to call this program again to forward to the next step.
= 2: Returned at T = Tout to provide intermediate result in Mode = 2. It is possible to call this program again with new Tout.
= 3: Returned at T = Tout to provide intermediate result in Mode = 3. It is possible to call this program again with new Tout.
= 11: (Error) Maximum number of steps exceeded. |
| [out] | Neval | (Optional)
Number of function evaluations. |
| [out] | Njac | (Optional)
Number of Jacobian evaluations (either analytically or numerically). |
| [out] | Nstep | (Optional)
Number of all computed steps. |
| [out] | Naccept | (Optional)
Number of accepted steps. |
| [out] | Nreject | (Optional)
Number of rejected steps. |
| [out] | Ndec | (Optional)
Number of LU decompositions. |
| [out] | Nsol | (Optional)
Number of forward-backward substitutions. |
| [in] | Fjac | (Optional)
The user supplied subroutine, which computes the Jacobian, defined as follows. (default = NullPtr) Sub Fjac(N As Long, T As Double, Y() As Double, Ypd() As Double)
Compute Jacobian at T and Y() and store in Ypd().
End Sub
Jacobian should be stored in Ypd() (two dimensional array) in full matrix form (general n x n two dimensional array) if Mljac = N, or in band matrix form (see details below) if Mljac < N.
If NullPtr is specified as Fjac, the user supplied subroutine is not called and Jacobian is computed by finite difference approximation. |
| [in] | Mljac | (Optional)
The lower bandwidth of Jacobian. (0 <= Mljac <= N) (default = N)
If Mljac = N, Jacobian is stored as full matrix. If Mljac < N, Jacobian is stored in band matrix form. |
| [in] | Mujac | (Optional)
The upper bandwidth of Jacobian. (0 <= Mujac <= N) (default = 0)
If Mljac = N, Mujac is ignored. |
| [in] | Fmas | (Optional)
The user supplied subroutine, which provides the mass matrix M, defined as follows. (default = NullPtr) Sub Fmas(N As Long, M() As Double)
Store mass matrix in M().
End Sub
Mass matrix should be stored in M() (two dimensional array) in full matrix form if Mlmas = N, or in band matrix form if Mlmas < N.
If NullPtr is specified as Fmas, the user supplied subroutine is not called and M is assumed to be an identity matrix. |
| [in] | Mlmas | (Optional)
The lower bandwidth of mass matrix M. (0 <= Mljac <= N) (default = N)
If Mlmas = N, M is stored in full matrix form. If Mlmas < N, M is stored in band matrix form. |
| [in] | Mumas | (Optional)
The upper bandwidth of mass matrix M. (0 <= Mumas <= N) (default = 0)
If Mlmas = N, Mumas is ignored. |
| [in] | Hes | (Optional)
Specifies whether to transform the Jacobian matrix to Hessenberg form. It is advantageous for large systems with full jacobian. Not effective for banded Jacobian nor for the system with IMAS = 1.
= 0: Transform Jacobian.
= 1: Do not transform Jacobian. |
| [in] | Maxiter | (Optional)
Maximum number of allowed steps. (default = 10000) |
| [in] | Km | (Optional)
Maximum number of columns in the extrapolation table. (Km >= 3) (default = 12) (if Km < 3, default value is used.)
|
| [in] | Nsequ | (Optional)
Switch for the step size sequence. (default = 2)
= 1: 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, ...
= 2: 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, ...
= 3: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...
= 4: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... |
| [in] | Lambda | (Optional)
Parameter λ of dense output. (0 or 1) (default = 0) |
| [in] | M1,M2 | (default)
If the differential system has the special structure that
y(i)' = y(i + m2) (i = 1, ..., m1)
with m1 a multiple of m2, and the remaining equations do not explicitly depend on y'(m1), ..., y'(n - 1), a substantial gain in computer time can be achived by setting the parameters M1 = m1 (> 0) and M2 = m2 (> 0) (m1 + m2 <= n). (default: M1 = 0, M2 = M1) |
| [in] | Nsmax,Nsmin | (default)
Minimum number of stages Nsmin and maximum number of stages Nsmax (= 3, 5 or 7). (default: Nsmin = 3, Nsmax = 7)
The number of stages ns correspond to implicit Runge-Kutta method of order 2*ns - 1. |
| [in] | Ns | (default)
Value of ns for the first step. (3, 5 or 7) (default = Nsmin) |
| [in] | Cnt | (Optional)
Specifies when counters (Neval, Nstep, Naccept and Nreject) are reset to zero. (default = 0)
= 0: Reset if Info = 0.
= 1: Do not reset even if Info = 0. |
| [in] | Hinit | (Optional)
Initial step size. (default = estimated by the program) |
| [in] | Hmax | (Optional)
Maximaum step size. (default = Abs(Tend - T)) |
| [in] | Thet | (Optional)
Decides whether the Jacobian should be recomputed. (Thet < 1) (default = 0.001)
Increase this value (e.g. to 0.1) when Jacobian evaluations are costly. For small systems, this value should be smaller (e.g. 0.001). Negative value forces to compute the Jacobian after every accepted step. |
| [in] | Fac1,Fac2 | (Optional)
Parameters for step size selection. (default: Fac1 = 0.1, Fac2 = 4)
The new step size is chosen subject to the restriction facmin/Fac5 <= jth hnew/hold <= 1/facmin, facmin = Fac1^(1/(j - 1)). |
| [in] | Fac3 | (Optional)
Order selection parameter for decreasing order. (default = 0.7) |
| [in] | Fac4 | (Optional)
Order selection parameter for increasing order. (default = 0.9) |
| [in] | Safe1,Safe2 | (Optional)
Safety factors for step control algorithm. (default: Safe1 = 0.6, Safe2 = 0.93)
hnew = h*Safe2*(Safe1*tol/err)^(1/(j - 1)). |
| [in] | Wkfcn,Wkjac,Wkdec,Wksol | (Optional)
Estimated works for a call to F, Fjac, LU decompositions, forward-backward substitutions, respectively.
(default: Wkfcn = 1, Wkjac = 5, Wkdec = 1, Wksol = 1) |
- Reference
- (1) E. Hairer, S.P. Norsett and G. Wanner, "Solving Ordinary Differential Equations II. Stiff and differential-algebraic Problems. 2nd edition", Springer Series in Computational Mathematics, Springer-Verlag (1996)
- Example Program
- Solve the following initial value problem of ordinary differential equations (stiff problem).
dy1/dt = -2*y1 + y2 - cos(t)
dy2/dt = 1998*y1 - 1999*y2 + 1999*cos(t) - sin(t)
(y1 = 1, y2 = 2 at t = 0)
Sub F2(N As Long, T As Double, Y() As Double, Yp() As Double)
Yp(0) = -2 * Y(0) + Y(1) - Cos(T)
Yp(1) = 1998 * Y(0) - 1999 * Y(1) + 1999 * Cos(T) - Sin(T)
End Sub
Sub Ex_Seulexa()
Const N = 2
Dim T As Double, Y(N - 1) As Double, Tend As Double, Tout As Double
Dim Ifcn As Long, RTol(0) As Double, ATol(0) As Double, Mode As Long
Dim Neval As Long, Info As Long
Ifcn = 1
RTol(0) = 0.0000000001 '1.0e-10
ATol(0) = RTol(0)
Mode = 2
T = 0: Y(0) = 1: Y(1) = 2
Tend = 10
Info = 0
Do
Tout = T + 1
Call Seulexa(N, AddressOf F2, Ifcn, T, Y(), Tout, Tend, RTol(), ATol(), Mode, Info, Neval)
Debug.Print T, Y(0), Y(1)
Loop While Info >= 1 And Info <= 3
Debug.Print Neval, Info
End Sub
Sub Seulexa(N As Long, F As LongPtr, Ifcn As Long, T As Double, Y() As Double, Tout As Double, Tend As Double, RTol() As Double, ATol() As Double, Mode As Long, Info As Long, Optional Neval As Long, Optional Njac As Long, Optional Nstep As Long, Optional Naccept As Long, Optional Nreject As Long, Optional Ndec As Long, Optional Nsol As Long, Optional Fjac As LongPtr=NullPtr, Optional Mljac As Long=-1, Optional Mujac As Long, Optional Fmas As LongPtr=NullPtr, Optional Mlmas As Long=-1, Optional Mumas As Long, Optional Hes As Long, Optional MaxIter As Long, Optional Km As Long, Optional Nsequ As Long, Optional Lambda As Long, Optional M1 As Long, Optional M2 As Long, Optional Cnt As Long, Optional Hinit As Double, Optional Hmax As Double, Optional Thet As Double, Optional Fac1 As Double, Optional Fac2 As Double, Optional Fac3 As Double, Optional Fac4 As Double, Optional Safe1 As Double, Optional Safe2 As Double, Optional Wkfcn As Double, Optional Wkjac As Double, Optional Wkdec As Double, Optional Wksol As Double) Initial value problem of ordinary differential equations (extrapolation method based on the linearly ...
Note - If Mode is chenged to 3 instead of 2 in the same program, the dense outout (interpolation) is used.
- Example Results
1 0.367879441171302 0.908181747350055
2 0.135335283240001 -0.280811553307429
3 4.97870683700128E-02 -0.940205428230317
4 1.83156389096303E-02 -0.635327981955837
5 6.73794701199793E-03 0.290400132475296
6 2.4787521833215E-03 0.962649038833673
7 9.11881989233259E-04 0.754814136333544
8 3.35462641107797E-04 -0.145164571166589
9 1.23409810938649E-04 -0.911006852073751
10 4.53999367060503E-05 -0.839026129140682
8417 0
|