1 /* ode-initval/rk4imp.c
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 /* Runge-Kutta 4, Gaussian implicit */
25 /* Error estimation by step doubling, see eg. Ascher, U.M., Petzold,
26 L.R., Computer methods for ordinary differential and
27 differential-algebraic equations, SIAM, Philadelphia, 1998.
28 Method coefficients can also be found in it.
34 #include <gsl/gsl_math.h>
35 #include <gsl/gsl_errno.h>
36 #include <gsl/gsl_odeiv.h>
38 #include "odeiv_util.h"
53 rk4imp_alloc (size_t dim)
55 rk4imp_state_t *state = (rk4imp_state_t *) malloc (sizeof (rk4imp_state_t));
59 GSL_ERROR_NULL ("failed to allocate space for rk4imp_state",
63 state->k1nu = (double *) malloc (dim * sizeof (double));
68 GSL_ERROR_NULL ("failed to allocate space for k1nu", GSL_ENOMEM);
71 state->k2nu = (double *) malloc (dim * sizeof (double));
77 GSL_ERROR_NULL ("failed to allocate space for k2nu", GSL_ENOMEM);
80 state->ytmp1 = (double *) malloc (dim * sizeof (double));
82 if (state->ytmp1 == 0)
87 GSL_ERROR_NULL ("failed to allocate space for ytmp1", GSL_ENOMEM);
90 state->ytmp2 = (double *) malloc (dim * sizeof (double));
92 if (state->ytmp2 == 0)
98 GSL_ERROR_NULL ("failed to allocate space for ytmp2", GSL_ENOMEM);
101 state->y0 = (double *) malloc (dim * sizeof (double));
110 GSL_ERROR_NULL ("failed to allocate space for y0", GSL_ENOMEM);
113 state->y0_orig = (double *) malloc (dim * sizeof (double));
115 if (state->y0_orig == 0)
123 GSL_ERROR_NULL ("failed to allocate space for y0_orig", GSL_ENOMEM);
126 state->y_onestep = (double *) malloc (dim * sizeof (double));
128 if (state->y_onestep == 0)
130 free (state->y0_orig);
137 GSL_ERROR_NULL ("failed to allocate space for y_onestep", GSL_ENOMEM);
144 rk4imp_step (double *y, rk4imp_state_t *state,
145 const double h, const double t,
146 const size_t dim, const gsl_odeiv_system *sys)
148 /* Makes a Runge-Kutta 4th order implicit advance with step size h.
149 y0 is initial values of variables y.
151 The implicit matrix equations to solve are:
153 Y1 = y0 + h * a11 * f(t + h * c1, Y1) + h * a12 * f(t + h * c2, Y2)
154 Y2 = y0 + h * a21 * f(t + h * c1, Y1) + h * a22 * f(t + h * c2, Y2)
156 y = y0 + h * b1 * f(t + h * c1, Y1) + h * b2 * f(t + h * c2, Y2)
158 with constant coefficients a, b and c. For this method
159 they are: b=[0.5 0.5] c=[(3-sqrt(3))/6 (3+sqrt(3))/6]
160 a11=1/4, a12=(3-2*sqrt(3))/12, a21=(3+2*sqrt(3))/12 and a22=1/4
163 const double ir3 = 1.0 / M_SQRT3;
164 const int iter_steps = 3;
168 double *const k1nu = state->k1nu;
169 double *const k2nu = state->k2nu;
170 double *const ytmp1 = state->ytmp1;
171 double *const ytmp2 = state->ytmp2;
173 /* iterative solution of Y1 and Y2.
175 Note: This method does not check for convergence of the
179 for (nu = 0; nu < iter_steps; nu++)
181 for (i = 0; i < dim; i++)
184 y[i] + h * (0.25 * k1nu[i] + 0.5 * (0.5 - ir3) * k2nu[i]);
186 y[i] + h * (0.25 * k2nu[i] + 0.5 * (0.5 + ir3) * k1nu[i]);
190 GSL_ODEIV_FN_EVAL (sys, t + 0.5 * h * (1.0 - ir3), ytmp1, k1nu);
192 if (s != GSL_SUCCESS)
199 GSL_ODEIV_FN_EVAL (sys, t + 0.5 * h * (1.0 + ir3), ytmp2, k2nu);
201 if (s != GSL_SUCCESS)
210 for (i = 0; i < dim; i++)
212 const double d_i = 0.5 * (k1nu[i] + k2nu[i]);
220 rk4imp_apply (void *vstate,
226 const double dydt_in[],
228 const gsl_odeiv_system * sys)
230 rk4imp_state_t *state = (rk4imp_state_t *) vstate;
234 double *y0 = state->y0;
235 double *y0_orig = state->y0_orig;
236 double *y_onestep = state->y_onestep;
237 double *k1nu = state->k1nu;
238 double *k2nu = state->k2nu;
240 /* Initialization step */
241 DBL_MEMCPY (y0, y, dim);
243 /* Save initial values in case of failure */
244 DBL_MEMCPY (y0_orig, y, dim);
248 DBL_MEMCPY (k1nu, dydt_in, dim);
252 int s = GSL_ODEIV_FN_EVAL (sys, t, y, k1nu);
254 if (s != GSL_SUCCESS)
260 DBL_MEMCPY (k2nu, k1nu, dim);
262 /* First traverse h with one step (save to y_onestep) */
264 DBL_MEMCPY (y_onestep, y, dim);
267 int s = rk4imp_step (y_onestep, state, h, t, dim, sys);
269 if (s != GSL_SUCCESS)
275 /* Then with two steps with half step length (save to y) */
278 int s = rk4imp_step (y, state, h/2.0, t, dim, sys);
280 if (s != GSL_SUCCESS)
282 /* Restore original y vector */
283 DBL_MEMCPY (y, y0_orig, dim);
288 DBL_MEMCPY (y0, y, dim);
291 int s = GSL_ODEIV_FN_EVAL (sys, t + h/2.0, y, k1nu);
293 if (s != GSL_SUCCESS)
295 /* Restore original y vector */
296 DBL_MEMCPY (y, y0_orig, dim);
301 DBL_MEMCPY (k2nu, k1nu, dim);
304 int s = rk4imp_step (y, state, h/2.0, t + h/2.0, dim, sys);
306 if (s != GSL_SUCCESS)
308 /* Restore original y vector */
309 DBL_MEMCPY (y, y0_orig, dim);
314 /* Derivatives at output */
316 if (dydt_out != NULL)
318 int s = GSL_ODEIV_FN_EVAL (sys, t + h, y, dydt_out);
320 if (s != GSL_SUCCESS) {
321 /* Restore original y vector */
322 DBL_MEMCPY (y, y0_orig, dim);
327 /* Error estimation */
329 /* Denominator in step doubling error equation
330 * yerr = 0.5 * | y(onestep) - y(twosteps) | / (2^order - 1)
333 for (i = 0; i < dim; i++)
335 yerr[i] = 8.0 * 0.5 * (y[i] - y_onestep[i]) / 15.0;
342 rk4imp_reset (void *vstate, size_t dim)
344 rk4imp_state_t *state = (rk4imp_state_t *) vstate;
346 DBL_ZERO_MEMSET (state->y_onestep, dim);
347 DBL_ZERO_MEMSET (state->y0_orig, dim);
348 DBL_ZERO_MEMSET (state->y0, dim);
349 DBL_ZERO_MEMSET (state->k1nu, dim);
350 DBL_ZERO_MEMSET (state->k2nu, dim);
351 DBL_ZERO_MEMSET (state->ytmp1, dim);
352 DBL_ZERO_MEMSET (state->ytmp2, dim);
358 rk4imp_order (void *vstate)
360 rk4imp_state_t *state = (rk4imp_state_t *) vstate;
361 state = 0; /* prevent warnings about unused parameters */
366 rk4imp_free (void *vstate)
368 rk4imp_state_t *state = (rk4imp_state_t *) vstate;
370 free (state->y_onestep);
371 free (state->y0_orig);
380 static const gsl_odeiv_step_type rk4imp_type = { "rk4imp", /* name */
381 1, /* can use dydt_in? */
382 1, /* gives exact dydt_out? */
390 const gsl_odeiv_step_type *gsl_odeiv_step_rk4imp = &rk4imp_type;