/*************************************************************************
* Collection of Visual C++ subroutines to Integrate a System of Ordinary *
* Differential Equations By the Runge-Kutta-Fehlberg method (in double *
* precision). *
* ---------------------------------------------------------------------- *
* REFERENCE: H A Watts and L F Shampine, *
* Sandia Laboratories, *
* Albuquerque, New Mexico. *
* *
* See demonstration program trkf45.cpp. *
* *
* C++ Release 1.1 By J-P Moreau. *
* (www.jpmoreau.fr) *
*************************************************************************/
#include
#include
#define NEQ 5 //Maximum number of equations
#define EPSILON 2.22e-16
#define FALSE 0
#define TRUE 1
// num must be declared in calling program.
extern num; // # of test
// work space for successive calls to rkf45().
double w1[NEQ+1], w3[NEQ+1], w4[NEQ+1], w5[NEQ+1], w6[NEQ+1];
double w7[NEQ+1], w2, w8, w9;
int iw1,iw2,iw3,iw4,iw5;
//User defined system of diff. equations
void f(double t, double *y, double *yp) {
/*-------------------------------------------------------------------
!
! F evaluates the derivative for the ODE (TESTS #1 and #2).
!
!------------------------------------------------------------------*/
switch(num) {
case 1: yp[1] = 0.25 * y[1] * ( 1.0 - y[1] / 20.0); break;
case 2: yp[1] = y[2]; yp[2] = - y[1]; break;
case 3: yp[1] = y[2];
yp[2] = y[3];
yp[3] = y[4];
yp[4] = y[5];
yp[5] = (45.0 * y[3] * y[4] * y[5] - 40.0 * y[4] * y[4] * y[4]) / (9.0 * y[3] * y[3]);
}
}
void fehl(int neqn, double *y, double *t, double *h, double *yp,
double *f1, double *f2, double *f3, double *f4, double *f5, double *s) {
/*********************************************************************************
!
! FEHL takes one Fehlberg fourth-fifth order step (double precision).
!
! Discussion:
!
! FEHL integrates a system of NEQN first order ordinary differential
! equations of the form
! dY(i)/dT = F(T,Y(1),...,Y(NEQN))
! where the initial values Y and the initial derivatives
! YP are specified at the starting point T.
!
! FEHL advances the solution over the fixed step H and returns
! the fifth order (sixth order accurate locally) solution
! approximation at T+H in array S.
!
! The formulas have been grouped to control loss of significance.
! FEHL should be called with an H not smaller than 13 units of
! roundoff in T so that the various independent arguments can be
! distinguished.
!
! Author:
!
! H A Watts and L F Shampine,
! Sandia Laboratories,
! Albuquerque, New Mexico.
!
! RKF45 is primarily designed to solve non-stiff and mildly stiff
! differential equations when derivative evaluations are inexpensive.
!
! Parameters:
!
! External F, a subroutine of the form
! void f(double t,double *y, double *yp)
! to evaluate the derivatives.
! YP[I] = dY[I] / dT;
!
! Input, integer NEQN, the number of equations to be integrated.
!
! Input, double Y(NEQN), the current value of the dependent variable.
!
! Input, double T, the current value of the independent variable.
!
! Input, double H, the step size to take.
!
! Input, double YP(NEQN), the current value of the derivative of the
! dependent variable.
!
! Output, double F1(NEQN), double F2(NEQN), double F3(NEQN), double F4(NEQN),
! double F5(NEQN) are arrays of dimension NEQN which are needed for
! internal storage.
!
! Output, double S(NEQN), the computed estimate of the solution at T+H.
!******************************************************************************/
double ch;
int i;
ch = *h / 4.0;
for (i=1; i<=neqn; i++)
f5[i] = y[i] + ch * yp[i];
f(*t + ch, f5, f1);
ch = 3.0 * (*h) / 32.0;
for (i=1; i<=neqn; i++)
f5[i] = y[i] + ch * (yp[i] + 3.0 * f1[i]);
f(*t + 3.0 * (*h) / 8.0, f5, f2);
ch = *h / 2197.0;
for (i=1; i<=neqn; i++)
f5[i] = y[i] + ch * (1932.0 * yp[i] + (7296.0 * f2[i] - 7200.0 * f1[i]));
f(*t + 12.0 * (*h) / 13.0, f5, f3);
ch = *h / 4104.0;
for (i=1; i<=neqn; i++)
f5[i] = y[i] + ch * ((8341.0 * yp[i] - 845.0 * f3[i]) + (29440.0 * f2[i]
- 32832.0 * f1[i]));
f(*t + *h, f5, f4);
ch = *h / 20520.0;
for (i=1; i<=neqn; i++)
f1[i] = y[i] + ch * ((-6080.0 * yp[i] + (9295.0 * f3[i] - 5643.0 * f4[i]))
+ (41040.0 * f1[i] - 28352.0 * f2[i]));
f(*t + *h / 2.0, f1, f5);
// Ready to compute the approximate solution at T+H
ch = *h / 7618050.0;
for (i=1; i<=neqn; i++)
s[i] = y[i] + ch * ((902880.0 * yp[i] + (3855735.0 * f3[i] - 1371249.0 * f4[i]))
+ (3953664.0 * f2[i] + 277020.0 * f5[i]));
} //fehl()
void rkfs(int neqn, double *y, double *t, double tout, double relerr, double abserr,
int *iflag, double *yp, double *h, double *f1, double *f2, double *f3,
double *f4, double *f5, double *savre, double *savae, int *nfe, int *kop,
int *init, int *jflag, int *kflag);
void rkf45 (int neqn, double *y, double *t, double tout, double relerr, double abserr,
int *iflag) {
/*******************************************************************************
!
! RKF45 carries out the Runge-Kutta-Fehlberg method (double precision).
!
! Author:
!
! H A Watts and L F Shampine,
! Sandia Laboratories,
! Albuquerque, New Mexico.
!
! RKF45 is primarily designed to solve non-stiff and mildly stiff
! differential equations when derivative evaluations are inexpensive.
!
! Abstract:
!
! RKF45 integrates a system of NEQN first order ordinary differential
! equations of the form:
!
! dY(i)/dT = F(T,Y(1),Y(2),...,Y(NEQN))
!
! where the Y(1:NEQN) are given at T.
!
! Typically the subroutine is used to integrate from T to TOUT but it
! can be used as a one-step integrator to advance the solution a
! single step in the direction of TOUT. On return, the parameters in
! the call list are set for continuing the integration. The user has
! only to call RKF45 again (and perhaps define a new value for TOUT).
! Actually, RKF45 is an interfacing routine which calls subroutine
! RKFS for the solution. RKFS in turn calls subroutine FEHL which
! computes an approximate solution over one step.
!
! Reference:
!
! E. Fehlberg,
! Low-order Classical Runge-Kutta Formulas with Stepsize Control,
! NASA Technical Report R-315.
!
! L F Shampine, H A Watts, S Davenport,
! Solving Non-stiff Ordinary Differential Equations - The State of the Art,
! Sandia Laboratories Report SAND75-0182,
! To appear in SIAM Review.
!
! Parameters:
!
! External F, a subroutine of the form
! void f(double t,double *y, double *yp)
! to evaluate the derivatives YP[I] = dY[I] / dT;
!
! Input, integer NEQN, the number of equations to be integrated.
!
! Input/output, double Y(NEQN), the solution vector at T.
!
! Input/output, double T, the independent variable.
!
! Input, double TOUT, the output point at which solution is desired.
!
! Input, double RELERR, ABSERR, the relative and absolute error tolerances
! for the local error test. At each step the code requires:
! abs(local error) <= relerr*abs(y) + abserr
! for each component of the local error and solution vectors
!
! Output, integer IFLAG, indicator for status of integration.
!
! Workspace, double W1(NEQN) to W9(NEQN), arrays to hold information internal
! to RKF45 which is necessary for subsequent calls.
!
! Workspace, integers IW1 to IW5, arrays used to hold information internal
! to RKF45 which is necessary for subsequent calls.
!
! Note: Workspaces are declared here statically in the present module.
!
! first call
!
! The user initializes the following parameters:
!
! neqn -- number of equations to be integrated. (neqn >= 1)
!
! y(neqn) -- vector of initial conditions
!
! t -- starting point of integration , must be a variable
!
! tout -- output point at which solution is desired.
! t=tout is allowed on the first call only, in which case
! rkf45 returns with iflag=2 if continuation is possible.
!
! relerr,abserr -- relative and absolute local error tolerances
! which must be non-negative. relerr must be a variable while
! abserr may be a constant. the code should normally not be
! used with relative error control smaller than about 1.e-8 .
! to avoid limiting precision difficulties the code requires
! relerr to be larger than an internally computed relative
! error parameter which is machine dependent. in particular,
! pure absolute error is not permitted. if a smaller than
! allowable value of relerr is attempted, rkf45 increases
! relerr appropriately and returns control to the user before
! continuing the integration.
!
! iflag -- +1,-1 indicator to initialize the code for each new
! problem. normal input is +1. the user should set iflag=-1
! only when one-step integrator control is essential. in this
! case, rkf45 attempts to advance the solution a single step
! in the direction of tout each time it is called. since this
! mode of operation results in extra computing overhead, it
! should be avoided unless needed.
!
! output from rkf45
!
! y(neqn) -- solution at t
! t -- last point reached in integration.
! iflag = 2 -- integration reached tout. indicates successful retur
! and is the normal mode for continuing integration.
! =-2 -- a single successful step in the direction of tout
! has been taken. normal mode for continuing
! integration one step at a time.
! = 3 -- integration was not completed because relative error
! tolerance was too small. relerr has been increased
! appropriately for continuing.
! = 4 -- integration was not completed because more than
! 3000 derivative evaluations were needed. this
! is approximately 500 steps.
! = 5 -- integration was not completed because solution
! vanished making a pure relative error test
! impossible. must use non-zero abserr to continue.
! using the one-step integration mode for one step
! is a good way to proceed.
! = 6 -- integration was not completed because requested
! accuracy could not be achieved using smallest
! allowable stepsize. user must increase the error
! tolerance before continued integration can be
! attempted.
! = 7 -- it is likely that rkf45 is inefficient for solving
! this problem. too much output is restricting the
! natural stepsize choice. use the one-step integrator
! mode.
! = 8 -- invalid input parameters
! this indicator occurs if any of the following is
! satisfied - neqn <= 0
! t=tout and iflag <> +1 or -1
! relerr or abserr < 0.
! iflag = 0 or < -2 or > 8
! w1..w9, iw1..iw5 -- information which is usually of no interest
! to the user but necessary for subsequent calls.
! w1(neqn) contains the first derivatives of the
! solution vector y at t. w2(1) contains the stepsize
! h to be attempted on the next step. iw1 contains the
! derivative evaluation counter, etc.
!
! subsequent calls:
!
! RKF45 returns with all information needed to continue
! the integration. if the integration reached tout, the user need onl
! define a new tout and call RKF45 again. In the one-step integrator
! mode (iflag=-2) the user must keep in mind that each step taken is
! in the direction of the current tout. Upon reaching tout (indicated
! by changing iflag to 2),the user must then define a new tout and
! reset iflag to -2 to continue in the one-step integrator mode.
!
! If the integration was not completed but the user still wants to
! continue (iflag=3,4 cases), he just calls rkf45 again. with iflag=3
! the relerr parameter has been adjusted appropriately for continuing
! the integration. in the case of iflag=4 the function counter will
! be reset to 0 and another 3000 function evaluations are allowed.
!
! However,in the case iflag=5, the user must first alter the error
! criterion to use a positive value of abserr before integration can
! proceed. if he does not, execution is terminated.
!
! Also,in the case iflag=6, it is necessary for the user to reset
! iflag to 2 (or -2 when the one-step integration mode is being used)
! as well as increasing either abserr,relerr or both before the
! integration can be continued. if this is not done, execution will
! be terminated. the occurrence of iflag=6 indicates a trouble spot
! (solution is changing rapidly,singularity may be present) and it
! often is inadvisable to continue.
!
! If iflag=7 is encountered, the user should use the one-step
! integration mode with the stepsize determined by the code.
! If the user insists upon continuing the integration with RKF45,
! he must reset iflag to 2 before calling RKF45 again. otherwise,
! execution will be terminated.
!
! If iflag=8 is obtained, integration can not be continued unless
! the invalid input parameters are corrected.
!
! The arrays or scalars w1..w9 and integers iw1 to iw5 contain information
! required for subsequent integration, and should not be altered.
!****************************************************************************/
rkfs(neqn, y, t, tout, relerr, abserr, iflag, w1, &w2, w3, w4, w5,
w6, w7, &w8, &w9, &iw1, &iw2, &iw3, &iw4, &iw5);
}
//Emulation of Fortran Intrisic Functions
int ISign(int a, int b) {
if (b<0) return -abs(a);
else return abs(a);
}
double Max(double a, double b) {
if (a>=b) return a;
else return b;
}
double Min(double a, double b) {
if (a<=b) return a;
else return b;
}
double Sign(double a, double b) {
if (b<0) return (-fabs(a));
else return fabs(a);
}
void rkfs(int neqn, double *y, double *t, double tout, double relerr, double abserr,
int *iflag, double *yp, double *h, double *f1, double *f2, double *f3,
double *f4, double *f5, double *savre, double *savae, int *nfe, int *kop,
int *init, int *jflag, int *kflag) {
/**********************************************************************************
!
! RKFS implements the Runge-Kutta-Fehlberg method (double precision).
!
! Discussion:
!
! RKFS integrates a system of first order ordinary differential
! equations as described in the comments for RKF45.
!
! The arrays yp, f1, f2, f3, f4, and f5 (of dimension neqn) and
! the variables h, savre, savae, nfe, kop, init, jflag and kflag are used
! internally by the code and appear in the call list to eliminate
! local retention of variables between calls. Accordingly, they
! should not be altered. Items of possible interest are
!
! YP - the derivative of the solution vector at T;
! H - an appropriate stepsize to be used for the next step;
! NFE - the number of derivative function evaluations.
!
! The expense is controlled by restricting the number
! of function evaluations to be approximately MAXNFE.
! As set, this corresponds to about 500 steps.
!
! REMIN is the minimum acceptable value of RELERR. Attempts
! to obtain higher accuracy with this subroutine are usually
! very expensive and often unsuccessful.
!******************************************************************************/
//Labels: e25,e40,e45,e50,e60,e65,e80,e100,e200,e260
double remin = 1e-12;
int maxnfe = 3000;
double a,ae,dt,ee,eeoet,eps,esttol,et,hmin,rer,s,scale;
double tol,toln,ypk;
bool hfaild,output;
int i,k,mflag;
// Check the input parameters
eps = EPSILON;
if (neqn < 1) {
*iflag = 8;
return;
}
if (relerr < 0.0) {
*iflag = 8;
return;
}
if (abserr < 0.0) {
*iflag = 8;
return;
}
mflag = abs(*iflag);
if (abs(*iflag) < 1 || abs(*iflag) > 8) {
*iflag = 8;
return;
}
// Is this the first call?
if (mflag == 1) goto e50;
// Check continuation possibilities
if (*t==tout && *kflag != 3) {
*iflag = 8;
return;
}
if (mflag != 2) goto e25;
// iflag = +2 or -2
if (*kflag == 3) goto e45;
if (*init == 0) goto e45;
if (*kflag == 4) goto e40;
if (*kflag == 5 && abserr == 0.0) {
printf("\n RKFS: Fatal error.\n");
return;
}
if (*kflag == 6 && relerr <= *savre && abserr <= *savae) {
printf("\n RKFS: Fatal error.\n");
return;
}
goto e50;
// iflag = 3,4,5,6,7 or 8
e25:if (*iflag == 3) goto e45;
if (*iflag == 4) goto e40;
if (*iflag == 5 && abserr > 0.0) goto e45;
// Integration cannot be continued since user did not respond to
// the instructions pertaining to iflag=5,6,7 or 8.
printf("\n RKFS: Fatal error.\n");
return;
// Reset function evaluation counter
e40: *nfe = 0;
if (mflag == 2) goto e50;
// Reset flag value from previous call
e45:*iflag = *jflag;
if (*kflag == 3) mflag = abs(*iflag);
// Save input iflag and set continuation flag for subsequent input checking.
e50: *jflag = *iflag;
*kflag = 0;
// Save relerr and abserr for checking input on subsequent calls.
*savre = relerr;
*savae = abserr;
/* Restrict relative error tolerance to be at least as large as
2*eps+remin to avoid limiting precision difficulties arising
from impossible accuracy requests. */
rer = 2.0 * EPSILON + remin;
// The relative error tolerance is too small.
if (relerr < rer) {
relerr = rer;
*iflag = 3;
*kflag = 3;
return;
}
dt = tout - *t;
if (mflag == 1) goto e60;
if (*init == 0) goto e65;
goto e80;
/* Initialization:
set initialization completion indicator, init
set indicator for too many output points, kop
evaluate initial derivatives
set counter for function evaluations, nfe
evaluate initial derivatives
set counter for function evaluations, nfe
estimate starting stepsize. */
e60: *init = 0;
*kop = 0;
a = *t;
f(a, y, yp);
*nfe = 1;
if (*t == tout) {
*iflag = 2;
return;
}
e65: *init = 1;
*h = fabs(dt);
toln = 0.0;
for (k = 1; k<=neqn; k++) {
tol = relerr * fabs(y[k]) + abserr;
if (tol > 0.0) {
toln = tol;
ypk = fabs(yp[k]);
if (ypk * pow(*h,5.0) > tol)
*h = pow((tol/ypk),0.2);
}
}
if (toln <= 0.0) *h = 0.0;
*h = Max(*h, 26.0 * eps * Max(fabs(*t), fabs(dt)));
*jflag = ISign(2, *iflag);
// Set stepsize for integration in the direction from T to TOUT
e80: *h = Sign(*h,dt);
// Test to see if RKF45 is being severely impacted by too many output points.
if (fabs(*h) >= 2.0 * fabs(dt)) *kop = *kop + 1;
// Unnecessary frequency of output.
if (*kop == 100) {
*kop = 0;
*iflag = 7;
return;
}
// If too close to output point, extrapolate and return.
if (fabs(dt) <= 26.0 * eps * fabs(*t)) {
for (i=1; i<=neqn; i++)
y[i] = y[i] + dt * yp[i];
a = tout;
f(a, y, yp);
*nfe = *nfe + 1;
*t = tout;
*iflag = 2;
return;
}
// Initialize output point indicator
output = FALSE;
// To avoid premature underflow in the error tolerance function,
// scale the error tolerances.
scale = 2.0 / relerr;
ae = scale * abserr;
// Step by step integration
e100: hfaild = FALSE;
// Set smallest allowable stepsize
hmin = 26.0 * eps * fabs(*t);
/* Adjust stepsize if necessary to hit the output point.
Look ahead two steps to avoid drastic changes in the stepsize and
thus lessen the impact of output points on the code. */
dt = tout - *t;
if (fabs(dt) >= 2.0 * fabs(*h)) goto e200;
// The next successful step will complete the integration to the output point.
if (fabs(dt) <= fabs(*h)) {
output = TRUE;
*h = dt;
goto e200;
}
*h = 0.5 * dt; // reduce step
/* Core integrator for taking a single step
!
! The tolerances have been scaled to avoid premature underflow in
! computing the error tolerance function ET.
! To avoid problems with zero crossings, relative error is measured
! using the average of the magnitudes of the solution at the
! beginning and end of a step.
! The error estimate formula has been grouped to control loss of
! significance.
!
! To distinguish the various arguments, H is not permitted
! to become smaller than 26 units of roundoff in T.
! Practical limits on the change in the stepsize are enforced to
! smooth the stepsize selection process and to avoid excessive
! chattering on problems having discontinuities.
! To prevent unnecessary failures, the code uses 9/10 the stepsize
! it estimates will succeed.
!
! After a step failure, the stepsize is not allowed to increase for
! the next attempted step. This makes the code more efficient on
! problems having discontinuities and more effective in general
! since local extrapolation is being used and extra caution seems
! warranted.
!
! Test number of derivative function evaluations.
! If okay, try to advance the integration from T to T+H. */
// Too many function calls.
e200:if (*nfe > maxnfe) {
*iflag = 4;
*kflag = 4;
return;
}
// Advance an approximate solution over one step of length H
fehl(neqn, y, t, h, yp, f1, f2, f3, f4, f5, f1);
*nfe = *nfe + 5;
/* Compute and test allowable tolerances versus local error estimates
and remove scaling of tolerances. Note that relative error is
measured with respect to the average of the magnitudes of the
solution at the beginning and end of the step. */
eeoet = 0.0;
for (k = 1; k<=neqn; k++) {
et = fabs(y[k]) + fabs(f1[k]) + ae;
if (et <= 0.0) {
*iflag = 5;
return;
}
ee = fabs((-2090.0 * yp[k] + (21970.0E+00 * f3[k] - 15048.0 * f4[k]))
+ (22528.0 * f2[k] - 27360.0 * f5[k]));
eeoet = Max(eeoet, ee/et);
}
esttol = fabs(*h) * eeoet * scale / 752400.0;
if (esttol <= 1.0) goto e260;
// Unsuccessful step. Reduce the stepsize, try again.
// The decrease is limited to a factor of 1/10.
hfaild = TRUE;
output = FALSE;
if (esttol < 59049.0)
s = 0.9 / pow(esttol, 0.2);
else
s = 0.1;
*h = s * (*h);
if (fabs(*h) < hmin) {
*iflag = 6;
*kflag = 6;
return;
}
else
goto e200;
// Successful step. Store solution at T+H and evaluate derivatives there.
e260: *t = *t + *h;
for (i=1; i<=neqn; i++) y[i] = f1[i];
a = *t;
f(a, y, yp);
*nfe = *nfe + 1;
// Choose next stepsize. The increase is limited to a factor of 5.
// If step failure has just occurred, next stepsize is not allowed to increase.
if (esttol > 0.0001889568)
s = 0.9 / pow(esttol, 0.2);
else
s = 5.0;
if (hfaild) s = Min(s, 1.0);
*h = Sign(Max(s * fabs(*h), hmin), *h);
// End of core integrator
// Should we take another step ?
if (output) {
*t = tout;
*iflag = 2;
}
if (*iflag > 0) goto e100;
// Integration successfully completed.
// ne-step mode
*iflag = - 2;
}
// end of file rkf45.cpp