1 /* ode-initval/rk2simp.c
3 * Copyright (C) 2004 Tuomo Keskitalo
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 2, Gaussian implicit. Also known as implicit midpoint rule.
22 Non-linear equations solved by linearization, LU-decomposition
23 and matrix inversion. For reference, see eg.
25 Ascher, U.M., Petzold, L.R., Computer methods for ordinary
26 differential and differential-algebraic equations, SIAM,
33 #include <gsl/gsl_math.h>
34 #include <gsl/gsl_errno.h>
35 #include <gsl/gsl_odeiv.h>
36 #include <gsl/gsl_linalg.h>
38 #include "odeiv_util.h"
46 double *dfdy; /* Jacobian */
47 double *dfdt; /* time derivatives, not used */
54 rk2simp_alloc (size_t dim)
56 rk2simp_state_t *state =
57 (rk2simp_state_t *) malloc (sizeof (rk2simp_state_t));
61 GSL_ERROR_NULL ("failed to allocate space for rk2simp_state",
65 state->Y1 = (double *) malloc (dim * sizeof (double));
70 GSL_ERROR_NULL ("failed to allocate space for Y1", GSL_ENOMEM);
73 state->y0 = (double *) malloc (dim * sizeof (double));
79 GSL_ERROR_NULL ("failed to allocate space for y0", GSL_ENOMEM);
82 state->y0_orig = (double *) malloc (dim * sizeof (double));
84 if (state->y0_orig == 0)
89 GSL_ERROR_NULL ("failed to allocate space for y0_orig", GSL_ENOMEM);
92 state->ytmp = (double *) malloc (dim * sizeof (double));
98 free (state->y0_orig);
100 GSL_ERROR_NULL ("failed to allocate space for ytmp", GSL_ENOMEM);
103 state->dfdy = (double *) malloc (dim * dim * sizeof (double));
105 if (state->dfdy == 0)
109 free (state->y0_orig);
112 GSL_ERROR_NULL ("failed to allocate space for dfdy", GSL_ENOMEM);
115 state->dfdt = (double *) malloc (dim * sizeof (double));
117 if (state->dfdt == 0)
121 free (state->y0_orig);
125 GSL_ERROR_NULL ("failed to allocate space for dfdt", GSL_ENOMEM);
128 state->y_onestep = (double *) malloc (dim * sizeof (double));
130 if (state->y_onestep == 0)
134 free (state->y0_orig);
139 GSL_ERROR_NULL ("failed to allocate space for y_onestep", GSL_ENOMEM);
142 state->p = gsl_permutation_alloc (dim);
148 free (state->y0_orig);
153 GSL_ERROR_NULL ("failed to allocate space for p", GSL_ENOMEM);
161 rk2simp_step (double *y, rk2simp_state_t * state,
162 const double h, const double t,
163 const size_t dim, const gsl_odeiv_system * sys)
165 /* Makes a Runge-Kutta 2nd order semi-implicit advance with step size h.
166 y0 is initial values of variables y.
168 The linearized semi-implicit equations to calculate are:
170 Y1 = y0 + h/2 * (1 - h/2 * df/dy)^(-1) * f(t + h/2, y0)
172 y = y0 + h * f(t + h/2, Y1)
175 const double *y0 = state->y0;
176 double *Y1 = state->Y1;
177 double *ytmp = state->ytmp;
182 gsl_matrix_view J = gsl_matrix_view_array (state->dfdy, dim, dim);
185 Calculate the inverse matrix (1 - h/2 * df/dy)^-1
188 /* Create matrix to J */
190 s = GSL_ODEIV_JA_EVAL (sys, t, y0, state->dfdy, state->dfdt);
192 if (s != GSL_SUCCESS)
197 gsl_matrix_scale (&J.matrix, -h / 2.0);
198 gsl_matrix_add_diagonal(&J.matrix, 1.0);
200 /* Invert it by LU-decomposition to invmat */
202 s += gsl_linalg_LU_decomp (&J.matrix, state->p, &ps);
204 if (s != GSL_SUCCESS)
209 /* Evaluate f(t + h/2, y0) */
211 s = GSL_ODEIV_FN_EVAL (sys, t + 0.5 * h, y0, ytmp);
213 if (s != GSL_SUCCESS)
218 /* Calculate Y1 = y0 + h/2 * ((1-h/2 * df/dy)^-1) ytmp */
221 gsl_vector_const_view y0_view = gsl_vector_const_view_array(y0, dim);
222 gsl_vector_view ytmp_view = gsl_vector_view_array(ytmp, dim);
223 gsl_vector_view Y1_view = gsl_vector_view_array(Y1, dim);
225 s = gsl_linalg_LU_solve (&J.matrix, state->p,
226 &ytmp_view.vector, &Y1_view.vector);
228 gsl_vector_scale (&Y1_view.vector, 0.5 * h);
229 gsl_vector_add (&Y1_view.vector, &y0_view.vector);
232 /* And finally evaluation of f(t + h/2, Y1) and calculation of y */
234 s = GSL_ODEIV_FN_EVAL (sys, t + 0.5 * h, Y1, ytmp);
236 if (s != GSL_SUCCESS)
241 for (i = 0; i < dim; i++)
243 y[i] = y0[i] + h * ytmp[i];
250 rk2simp_apply (void *vstate, size_t dim, double t, double h,
251 double y[], double yerr[], const double dydt_in[],
252 double dydt_out[], const gsl_odeiv_system * sys)
254 rk2simp_state_t *state = (rk2simp_state_t *) vstate;
258 double *y0 = state->y0;
259 double *y0_orig = state->y0_orig;
260 double *y_onestep = state->y_onestep;
262 /* Error estimation is done by step doubling procedure */
264 DBL_MEMCPY (y0, y, dim);
266 /* Save initial values in case of failure */
267 DBL_MEMCPY (y0_orig, y, dim);
269 /* First traverse h with one step (save to y_onestep) */
270 DBL_MEMCPY (y_onestep, y, dim);
273 int s = rk2simp_step (y_onestep, state, h, t, dim, sys);
275 if (s != GSL_SUCCESS)
281 /* Then with two steps with half step length (save to y) */
284 int s = rk2simp_step (y, state, h / 2.0, t, dim, sys);
286 if (s != GSL_SUCCESS)
288 /* Restore original y vector */
289 DBL_MEMCPY (y, y0_orig, dim);
294 DBL_MEMCPY (y0, y, dim);
297 int s = rk2simp_step (y, state, h / 2.0, t + h / 2.0, dim, sys);
299 if (s != GSL_SUCCESS)
301 /* Restore original y vector */
302 DBL_MEMCPY (y, y0_orig, dim);
307 /* Derivatives at output */
309 if (dydt_out != NULL)
311 int s = GSL_ODEIV_FN_EVAL (sys, t + h, y, dydt_out);
313 if (s != GSL_SUCCESS)
315 /* Restore original y vector */
316 DBL_MEMCPY (y, y0_orig, dim);
321 /* Error estimation */
323 for (i = 0; i < dim; i++)
325 yerr[i] = 4.0 * (y[i] - y_onestep[i]) / 3.0;
333 rk2simp_reset (void *vstate, size_t dim)
335 rk2simp_state_t *state = (rk2simp_state_t *) vstate;
337 DBL_ZERO_MEMSET (state->Y1, dim);
338 DBL_ZERO_MEMSET (state->y0, dim);
339 DBL_ZERO_MEMSET (state->y0_orig, dim);
340 DBL_ZERO_MEMSET (state->ytmp, dim);
341 DBL_ZERO_MEMSET (state->dfdt, dim * dim);
342 DBL_ZERO_MEMSET (state->dfdt, dim);
343 DBL_ZERO_MEMSET (state->y_onestep, dim);
349 rk2simp_order (void *vstate)
351 rk2simp_state_t *state = (rk2simp_state_t *) vstate;
352 state = 0; /* prevent warnings about unused parameters */
357 rk2simp_free (void *vstate)
359 rk2simp_state_t *state = (rk2simp_state_t *) vstate;
362 free (state->y0_orig);
366 free (state->y_onestep);
367 gsl_permutation_free (state->p);
371 static const gsl_odeiv_step_type rk2simp_type = {
372 "rk2simp", /* name */
373 0, /* can use dydt_in? */
374 1, /* gives exact dydt_out? */
382 const gsl_odeiv_step_type *gsl_odeiv_step_rk2simp = &rk2simp_type;