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 2(3), Euler-Cauchy */
25 /* Reference: Abramowitz & Stegun, section 25.5. Runge-Kutta 2nd (25.5.7)
26 and 3rd (25.5.8) order methods */
31 #include <gsl/gsl_errno.h>
32 #include <gsl/gsl_odeiv.h>
34 #include "odeiv_util.h"
46 rk2_alloc (size_t dim)
48 rk2_state_t *state = (rk2_state_t *) malloc (sizeof (rk2_state_t));
52 GSL_ERROR_NULL ("failed to allocate space for rk2_state", GSL_ENOMEM);
55 state->k1 = (double *) malloc (dim * sizeof (double));
60 GSL_ERROR_NULL ("failed to allocate space for k1", GSL_ENOMEM);
63 state->k2 = (double *) malloc (dim * sizeof (double));
69 GSL_ERROR_NULL ("failed to allocate space for k2", GSL_ENOMEM);
72 state->k3 = (double *) malloc (dim * sizeof (double));
79 GSL_ERROR_NULL ("failed to allocate space for k2", GSL_ENOMEM);
82 state->ytmp = (double *) malloc (dim * sizeof (double));
90 GSL_ERROR_NULL ("failed to allocate space for k2", GSL_ENOMEM);
98 rk2_apply (void *vstate,
104 const double dydt_in[],
106 const gsl_odeiv_system * sys)
108 rk2_state_t *state = (rk2_state_t *) vstate;
112 double *const k1 = state->k1;
113 double *const k2 = state->k2;
114 double *const k3 = state->k3;
115 double *const ytmp = state->ytmp;
122 DBL_MEMCPY (k1, dydt_in, dim);
126 int s = GSL_ODEIV_FN_EVAL (sys, t, y, k1);
128 if (s != GSL_SUCCESS)
135 /* k2 = f(t + 0.5*h, y + 0.5*k1) */
137 for (i = 0; i < dim; i++)
139 ytmp[i] = y[i] + 0.5 * h * k1[i];
143 int s = GSL_ODEIV_FN_EVAL (sys, t + 0.5 * h, ytmp, k2);
145 if (s != GSL_SUCCESS)
152 /* for 3rd order estimates, is used for error estimation
153 k3 = f(t + h, y - k1 + 2*k2) */
155 for (i = 0; i < dim; i++)
157 ytmp[i] = y[i] + h * (-k1[i] + 2.0 * k2[i]);
161 int s = GSL_ODEIV_FN_EVAL (sys, t + h, ytmp, k3);
163 if (s != GSL_SUCCESS)
171 for (i = 0; i < dim; i++)
173 /* Save original values if derivative evaluation below fails */
177 const double ksum3 = (k1[i] + 4.0 * k2[i] + k3[i]) / 6.0;
182 /* Derivatives at output */
184 if (dydt_out != NULL)
186 int s = GSL_ODEIV_FN_EVAL (sys, t + h, y, dydt_out);
188 if (s != GSL_SUCCESS)
190 /* Restore original values */
191 DBL_MEMCPY (y, ytmp, dim);
197 /* Error estimation */
199 for (i = 0; i < dim; i++)
201 const double ksum3 = (k1[i] + 4.0 * k2[i] + k3[i]) / 6.0;
202 yerr[i] = h * (k2[i] - ksum3);
209 rk2_reset (void *vstate, size_t dim)
211 rk2_state_t *state = (rk2_state_t *) vstate;
213 DBL_ZERO_MEMSET (state->k1, dim);
214 DBL_ZERO_MEMSET (state->k2, dim);
215 DBL_ZERO_MEMSET (state->k3, dim);
216 DBL_ZERO_MEMSET (state->ytmp, dim);
222 rk2_order (void *vstate)
224 rk2_state_t *state = (rk2_state_t *) vstate;
225 state = 0; /* prevent warnings about unused parameters */
230 rk2_free (void *vstate)
232 rk2_state_t *state = (rk2_state_t *) vstate;
240 static const gsl_odeiv_step_type rk2_type = { "rk2", /* name */
241 1, /* can use dydt_in */
242 1, /* gives exact dydt_out */
250 const gsl_odeiv_step_type *gsl_odeiv_step_rk2 = &rk2_type;