Solves a first order differential-algebraic system of equations, g(t, y, yʹ) = 0, with optional additional constraints and user-defined linear system solver.
The type double function is imsl_d_differential_algebraic_eqs.
Required Arguments
int neq (Input) Number of dependent variables, and number of differential/algebraic equations, not counting any additional constraints.
float*t (Input/Output) Set t to the starting value t0 at the first step. On output, t is set to the value to which the integration has advanced. Normally, this new value is tend.
floattend (Input) Final value of the independent variable. Update this value when re-entering after output with ido = 2.
int*ido (Input/Output) Flag indicating the state of the computation.
ido
State
1
Initial entry
2
Normal re-entry after obtaining output
3
Release workspace, last call
The user sets ido = 1 on the first call at t = t0. The function then sets ido =2, and this value is used for all but the last entry, which is made with ido = 3.
floaty[] (Input/Output) Array of length neq containing the dependent variable values, y. On input, y must contain initial values. On output, y contains the computed solution at tend.
floatyprime[] (Input/Output) Array of length neq containing derivative values, yʹ. This array must contain initial values, but they need not be such that g(t, y, yʹ) = 0 at t=t0. See the description of parameter iypr for more information.
voidgcn (intneq, floatt, floaty[], floatyprime[], floatdelta[], floatd[], intldd, int*ires) (Input) User-supplied function to evaluate g(t, y, yʹ), and any constraints. Also partial derivative evaluations and optionally linear solving steps occur here. The equations g(t, y, yʹ) = 0 consist of neq differential-algebraic equations of the form.
The function gcn is also used to evaluate the ncon additional algebraic constraints.
Arguments
intneq (Input) Number of dependent variables, and number of differential‑algebraic equations, not counting any additional constraints.
floatt (Input) Integration variable t.
floaty[] (Input) Array of neq dependent variables, y.
floatyprime[] (Input) Array of neq derivative values, yʹ.
floatdelta[] (Input/Output) Array of length max(neq, ncon) containing residuals, . See parameter ires for a definition.
floatd[] (Input/Output Array of length ldd×neq containing partial derivatives, d See parameter ires for a definition.
intldd (Input) Number of rows in d.
int*ires (Input/Output) Flag indicating what is to be calculated in the user function, gcn.
Note: ires is input only, except when ires = 6. It is input/output when ires = 6. For a detailed description see the table below.
The code calls gcn with ires = 0, 1, 2, 3, 4, 5, 6, or 7, defined as follows:
ires
Description
0
Do initializations which may be required in later calls to gcn. This is a setup flag that is input to gcn just once per problem.
Initializations might be computing parameters to be used internally by gcn or taking any other necessary steps for what may follow in terms of evaluating derivatives or linear solves.
Return and do nothing if no initializations are needed.
1
Compute = , the i‑th residual, for i =1,…,neq.
2
(Required only if iujac =1 and matstr = 0 or 1). Compute
,
the partial derivative matrix. These are derivatives of with respect to , for i =1,…, neq and j = 1,…,neq.
3
(Required only if iujac =1 and matstr = 0 or 1). Compute
,
the partial derivative of with respect to , for i = 1,…,neq and j = 1,…,neq.
4
(Required only if iypr = 2). Compute
,
the partial derivative of Fi with respect to t, for i =1,…, neq.
5
(Required only if ncon > 0). Compute = , the i-th residual in the additional constraints, for i =1,…, ncon, and
,
the partial derivative of with respect to for i =1,…, ncon and j =1,…, neq.
6
(Required only if isolve = 1.) If matstr = 2, the user must compute the matrix
,
where cj = , and save this matrix in any user-defined format. This is for later use when ires = 7. The matrix may also be factored in this step, if desired. The array d is not referenced if matstr = 2.
If matstr = 0 or 1, the A matrix will already be defined and passed to gcn in the array d, which will be in full matrix format if matstr = 0, and band matrix format, if matstr = 1. The user may factor d in this step, if desired.
Note: For matstr = 0, 1, or 2, the user must set ires = 0 to signal that A is nonsingular. If A is nearly singular, leave ires = 6. This results in using a smaller step-size internally.
7
(Required only if isolve = 1.) The user must solve , where is passed to gcn in the vector delta, and x is returned in delta. If matstr = 2, A is the matrix which was computed and saved at the call with ires = 6; if matstr = 0 or 1, A is passed to gcn in the array d. In either case, the A matrix will remain factored if the user factored it when ires = 6.
The initial input values of yprime are already consistent with the input values of Y. That is g(t, y, yʹ) = 0 at t = t0. Any constraints must be satisfied at t = t0.
1
Consistent values of yprime are calculated by Petzold’s original DASSL algorithm.
2
Consistent values of yprime are calculated using a new algorithm [Hanson and Krogh, 2008], which is generally more robust but requires that iujac = 1 and isolve = 0, and additional derivatives corresponding to ires = 4 are to be calculated in gcn.
Default: iypr = 1.
IMSL_JACOBIAN_MATRIX_TYPE, intmatstr (Input) Parameter specifying the Jacobian matrix structure.
matstr
Description
0
The Jacobian matrices (whether iujac = 0 or 1) are to be stored in full storage mode.
1
The Jacobian matrices are to be stored in band storage mode. In this case, if iujac = 1, the partial derivative matrices have their entries for row i and column j, stored as array elements.
This occurs when ires = 2 or 3 in gcn.
2
A user-defined matrix structure is used (see the documentation for 6 for more details). If matstr = 2, isolve and iujac are set to 1 internally.
Default: matstr = 0.
IMSL_METHOD, intisolve (Input) Solve method.
isolve
Description
0
imsl_f_differential_algebraic_eqs solves the linear systems.
1
The user wishes to solve the linear system in function gcn. See parameter gcn for details.
Default: isolve = 0 for matstr = 0 or 1. isolve = 1 for matstr = 2.
IMSL_N_LOWER_DIAG, intml (Input) Number of non-zero diagonals below the main diagonal in the Jacobian matrices when band storage mode is used. ml is ignored if matstr≠ 1.
Default: ml=neq ‑ 1.
IMSL_N_UPPER_DIAG, intmu (Input) Number of non-zero diagonals above the main diagonal in the Jacobian matrices when band storage mode is used. mu is ignored if matstr≠ 1.
Default: mu=neq ‑ 1.
IMSL_RELATIVE_TOLERANCE, floatrtol (Input) Relative error tolerance for solver. The program attempts to maintain a local error in Y(i) less than rtol*∣y[i]∣ + atol[i].
Default: rtol = , where ɛis machine precision.
IMSL_ABSOLUTE_TOLERANCE, floatatol[] (Input) Array of size neq containing absolute error tolerances. See description of rtol.
Default: atol[i] = 0.0.
IMSL_INITIAL_STEPSIZE, float h0 (Input)s Initial stepsize used by the solver. If h0 = 0.0, the function defines the initial stepsize. Default: h0 = 0.0.
IMSL_MAX_STEPSIZE, floathmax (Input) Maximum stepsize used by the solver. If hmax = 0.0, the function defines the maximum stepsize.
Default: hmax = 0.0.
IMSL_MAX_ORDER, intmaxord (Input) Maximum order of the backward difference formulas used. 1 ≤maxord≤ 5.
Default: maxord = 5.
IMSL_MAX_NUMBER_STEPS, intmaxsteps (Input) Maximum number of steps allowed from t to tend. Default: maxsteps= 500.
IMSL_INTEGRATION_LIMIT, floattstop (Input) Integration limit point. For efficiency reasons, the code sometimes integrates past tend and interpolates a solution at tend. If a value for tstop is specified, the code will never integrate past t=tstop.
Default: No tstop value is specified.
IMSL_ORDER_MAGNITUDE_EST, float fmag (Input) Order-of-magnitude estimate. fmag is used as an order-of-magnitude estimate of the magnitude of the functions Fi (see description of gcn), for convergence testing, if iypr=2. fmag is ignored if iypr=0 or 1.
Default: fmag = 1.0.
IMSL_GCN_W_DATA, void gcn(intneq, floatt, floaty[], floatyprime[], floatdelta[], floatd[], intldd, int*ires, void*data), void*data (Input) User-supplied function to evaluate g(t, y, yʹ), and any constraints, which also accepts a pointer to data that is supplied by the user. data is a pointer to the data to be passed to the user-supplied function. Please refer to gcn in the Required Arguments section for more information. See Passing Data to User-Supplied Functions in the introduction to this manual for more details.
Description
Function imsl_f_differential_algebraic_eqs finds an approximation to the solution of a system of differential-algebraic equations with given initial data for and . The function uses BDF formulas, which are appropriate for stiff systems. imsl_f_differential_algebraic_eqs is based on the code DASSL designed by Linda Petzold [1982], and has been modified by Hanson and Krogh [2008] Solving Constrained Differential-Algebraic Systems Using Projections to allow the inclusion of additional constraints, including conservation principles, after each time step. The modified code also provides a more robust algorithm to calculate initial values consistent with the given initial values. This occurs when the initial are not known.
A differential-algebraic system of equations is said to have “index 0” if the Jacobian matrix of partial derivatives of the with respect to the is nonsingular. Thus it is possible to solve for all the initial values of and put the system in the form of a standard ODE system. If it is possible to reduce the system to a system of index 0 by taking first derivatives of some of the equations, the system has index 1, otherwise the index is greater than 1. See Brenan [1989] for a definition of index. imsl_f_differential_algebraic_eqs can generally only solve systems of index 0 or 1; other systems will usually have to be reduced to such a form through differentiation.
Examples
Example 1 – Method of Lines PDE Problem
This example solves the partial differential equation , with initial condition , and boundary conditions , which has exact solution . If we approximate the term using finite differences, where , and , we get:
If Yi(t) = U(xi,t), the first and last equations are algebraic and the others are differential equations, so this is a system of differential-algebraic equations. The system has index = 1, since it could be transformed into an ODE system by differentiating the first and last equations. Note that the Jacobian matrices are banded (tridiagonal), with ml = mu = 1. We use this and specify the option for dealing with banded matrices in imsl_f_differential_algebraic_eqs.
The first-order equations of motion of a point-mass m suspended on a massless wire of length under the influence of gravity, mg, and wire tension, λ , in Cartesian coordinates (p,q) are
The problem above has an index number equal to 3, thus it cannot be solved with imsl_f_differential_algebraic_eqs directly. Unfortunately, the fact that the index is greater than 1 is not obvious, but an attempt to solve it will generally produce an error message stating the corrector equation did not converge, or if iypr = 2 an error message stating that the index appears to be greater than 1 should be issued. The user then differentiates the last equation, which after replacing pʹ by u and qʹ by v, gives pu+qv = 0. This system still has index=2 (again not obvious, the user discovers this by unsuccessfully trying to solve the new system) and the last equation must be differentiated again, to finally (after appropriate substitutions) give the equation of total energy balance:
With initial conditions and appropriate definitions of the dependent variables, the system becomes:
The initial conditions correspond to the pendulum starting in a horizontal position.
Since we have replaced the original constraint, , which requires that the pendulum length be L, by differentiating it twice, this constraint is no longer explicitly enforced, and if we try to solve the above system alone (ie, with ncon=0), the pendulum length drifts substantially from L at larger times. imsl_f_differential_algebraic_eqs therefore allows the user to add additional constraints, to be re-enforced after each time step, so we add this original constraint, as well as the intermediate constraint . Using these two supplementary constraints, (ncon = 2), the pendulum length is constant.
Consider the system of ordinary differential equations, yʹ = By, where B is the bi-diagonal matrix with (‑1, ‑1/2, ‑1/3, …, ‑1/(n‑1), 0) on the main diagonal and with 1’s along the first sub-diagonal. The initial condition is y(0) = (1,0,0,…,0)T, and since yʹ (0) = By(0) = (-1,1,0,…,0)T, yʹ (0) is also known for this problem.
Since BTv = 0, where vi = 1/(i-1)!, v is an eigenvector of BTcorresponding to the eigenvalue 0. Thus
so vTy(t) is constant. Since it has the value vTy(0) = v1 = 1 at t = 0, the constraint vTy(t) = 1 is satisfied for all t. This constraint is imposed in this example.
This example also illustrates how the user can solve his/her own linear systems (matstr=2). Normally, when ires = 6, the matrix
is computed, saved and possibly factored, using a sparse matrix factorization function of the user’s choice. Then when ires=7, the system Ax = delta is solved, using the matrix B saved and factored earlier, and the solution is returned in delta. In this case, B is just a bidiagonal matrix, so there is no need to save or factor A when ires = 6, since a bi-diagonal system can be solved directly using forward substitution, when ires = 7.