| 1 | /* ode-initval/gear2.c |
| 2 | * |
| 3 | * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 18 | */ |
| 19 | |
| 20 | /* Gear 2 */ |
| 21 | |
| 22 | /* Author: G. Jungman |
| 23 | */ |
| 24 | #include "gsl__config.h" |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include "gsl_math.h" |
| 28 | #include "gsl_errno.h" |
| 29 | #include "gsl_ode-initval__odeiv_util.h" |
| 30 | #include "gsl_odeiv.h" |
| 31 | |
| 32 | |
| 33 | /* gear2 state object */ |
| 34 | typedef struct |
| 35 | { |
| 36 | int primed; /* flag indicating that yim1 is ready */ |
| 37 | double t_primed; /* system was primed for this value of t */ |
| 38 | double last_h; /* last step size */ |
| 39 | gsl_odeiv_step *primer; /* stepper to use for priming */ |
| 40 | double *yim1; /* y_{i-1} */ |
| 41 | double *k; /* work space */ |
| 42 | double *y0; /* work space */ |
| 43 | double *y0_orig; |
| 44 | double *y_onestep; |
| 45 | int stutter; |
| 46 | } |
| 47 | gear2_state_t; |
| 48 | |
| 49 | static void * |
| 50 | gear2_alloc (size_t dim) |
| 51 | { |
| 52 | gear2_state_t *state = (gear2_state_t *) malloc (sizeof (gear2_state_t)); |
| 53 | |
| 54 | if (state == 0) |
| 55 | { |
| 56 | GSL_ERROR_NULL ("failed to allocate space for gear2_state", GSL_ENOMEM); |
| 57 | } |
| 58 | |
| 59 | state->yim1 = (double *) malloc (dim * sizeof (double)); |
| 60 | |
| 61 | if (state->yim1 == 0) |
| 62 | { |
| 63 | free (state); |
| 64 | GSL_ERROR_NULL ("failed to allocate space for yim1", GSL_ENOMEM); |
| 65 | } |
| 66 | |
| 67 | state->k = (double *) malloc (dim * sizeof (double)); |
| 68 | |
| 69 | if (state->k == 0) |
| 70 | { |
| 71 | free (state->yim1); |
| 72 | free (state); |
| 73 | GSL_ERROR_NULL ("failed to allocate space for k", GSL_ENOMEM); |
| 74 | } |
| 75 | |
| 76 | state->y0 = (double *) malloc (dim * sizeof (double)); |
| 77 | |
| 78 | if (state->y0 == 0) |
| 79 | { |
| 80 | free (state->k); |
| 81 | free (state->yim1); |
| 82 | free (state); |
| 83 | GSL_ERROR_NULL ("failed to allocate space for y0", GSL_ENOMEM); |
| 84 | } |
| 85 | |
| 86 | state->y0_orig = (double *) malloc (dim * sizeof (double)); |
| 87 | |
| 88 | if (state->y0_orig == 0) |
| 89 | { |
| 90 | free (state->y0); |
| 91 | free (state->k); |
| 92 | free (state->yim1); |
| 93 | free (state); |
| 94 | GSL_ERROR_NULL ("failed to allocate space for y0_orig", GSL_ENOMEM); |
| 95 | } |
| 96 | |
| 97 | state->y_onestep = (double *) malloc (dim * sizeof (double)); |
| 98 | |
| 99 | if (state->y_onestep == 0) |
| 100 | { |
| 101 | free (state->y0_orig); |
| 102 | free (state->y0); |
| 103 | free (state->k); |
| 104 | free (state->yim1); |
| 105 | free (state); |
| 106 | GSL_ERROR_NULL ("failed to allocate space for y0_orig", GSL_ENOMEM); |
| 107 | } |
| 108 | |
| 109 | state->primed = 0; |
| 110 | state->primer = gsl_odeiv_step_alloc (gsl_odeiv_step_rk4imp, dim); |
| 111 | |
| 112 | if (state->primer == 0) |
| 113 | { |
| 114 | free (state->y_onestep); |
| 115 | free (state->y0_orig); |
| 116 | free (state->y0); |
| 117 | free (state->k); |
| 118 | free (state->yim1); |
| 119 | free (state); |
| 120 | GSL_ERROR_NULL ("failed to allocate space for primer", GSL_ENOMEM); |
| 121 | } |
| 122 | |
| 123 | state->last_h = 0.0; |
| 124 | |
| 125 | return state; |
| 126 | } |
| 127 | |
| 128 | static int |
| 129 | gear2_step (double *y, gear2_state_t * state, |
| 130 | const double h, const double t, |
| 131 | const size_t dim, const gsl_odeiv_system * sys) |
| 132 | { |
| 133 | /* Makes a Gear2 advance with step size h. |
| 134 | y0 is the initial values of variables y. |
| 135 | The implicit matrix equations to solve are: |
| 136 | k = y0 + h * f(t + h, k) |
| 137 | y = y0 + h * f(t + h, k) |
| 138 | */ |
| 139 | |
| 140 | const int iter_steps = 3; |
| 141 | int nu; |
| 142 | size_t i; |
| 143 | double *y0 = state->y0; |
| 144 | double *yim1 = state->yim1; |
| 145 | double *k = state->k; |
| 146 | |
| 147 | /* Iterative solution of k = y0 + h * f(t + h, k) |
| 148 | Note: This method does not check for convergence of the |
| 149 | iterative solution! |
| 150 | */ |
| 151 | |
| 152 | for (nu = 0; nu < iter_steps; nu++) |
| 153 | { |
| 154 | int s = GSL_ODEIV_FN_EVAL (sys, t + h, y, k); |
| 155 | |
| 156 | if (s != GSL_SUCCESS) |
| 157 | { |
| 158 | return s; |
| 159 | } |
| 160 | |
| 161 | for (i = 0; i < dim; i++) |
| 162 | { |
| 163 | y[i] = ((4.0 * y0[i] - yim1[i]) + 2.0 * h * k[i]) / 3.0; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return GSL_SUCCESS; |
| 168 | } |
| 169 | |
| 170 | static int |
| 171 | gear2_apply (void *vstate, |
| 172 | size_t dim, |
| 173 | double t, |
| 174 | double h, |
| 175 | double y[], |
| 176 | double yerr[], |
| 177 | const double dydt_in[], |
| 178 | double dydt_out[], const gsl_odeiv_system * sys) |
| 179 | { |
| 180 | gear2_state_t *state = (gear2_state_t *) vstate; |
| 181 | |
| 182 | state->stutter = 0; |
| 183 | |
| 184 | if (state->primed == 0 || t == state->t_primed || h != state->last_h) |
| 185 | { |
| 186 | /* Execute a single-step method to prime the process. Note that |
| 187 | * we do this if the step size changes, so frequent step size |
| 188 | * changes will cause the method to stutter. |
| 189 | * |
| 190 | * Note that we reuse this method if the time has not changed, |
| 191 | * which can occur when the adaptive driver is attempting to find |
| 192 | * an appropriate step-size on its first iteration */ |
| 193 | |
| 194 | int status; |
| 195 | DBL_MEMCPY (state->yim1, y, dim); |
| 196 | |
| 197 | status = |
| 198 | gsl_odeiv_step_apply (state->primer, t, h, y, yerr, dydt_in, dydt_out, |
| 199 | sys); |
| 200 | |
| 201 | /* Make note of step size and indicate readiness for a Gear step. */ |
| 202 | |
| 203 | state->primed = 1; |
| 204 | state->t_primed = t; |
| 205 | state->last_h = h; |
| 206 | state->stutter = 1; |
| 207 | |
| 208 | return status; |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | /* We have a previous y value in the buffer, and the step |
| 213 | * sizes match, so we go ahead with the Gear step. |
| 214 | */ |
| 215 | |
| 216 | double *const k = state->k; |
| 217 | double *const y0 = state->y0; |
| 218 | double *const y0_orig = state->y0_orig; |
| 219 | double *const yim1 = state->yim1; |
| 220 | double *y_onestep = state->y_onestep; |
| 221 | |
| 222 | int s; |
| 223 | size_t i; |
| 224 | |
| 225 | DBL_MEMCPY (y0, y, dim); |
| 226 | |
| 227 | /* iterative solution */ |
| 228 | |
| 229 | if (dydt_out != NULL) |
| 230 | { |
| 231 | DBL_MEMCPY (k, dydt_out, dim); |
| 232 | } |
| 233 | |
| 234 | /* First traverse h with one step (save to y_onestep) */ |
| 235 | |
| 236 | DBL_MEMCPY (y_onestep, y, dim); |
| 237 | |
| 238 | s = gear2_step (y_onestep, state, h, t, dim, sys); |
| 239 | |
| 240 | if (s != GSL_SUCCESS) |
| 241 | { |
| 242 | return s; |
| 243 | } |
| 244 | |
| 245 | /* Then with two steps with half step length (save to y) */ |
| 246 | |
| 247 | s = gear2_step (y, state, h / 2.0, t, dim, sys); |
| 248 | |
| 249 | if (s != GSL_SUCCESS) |
| 250 | { |
| 251 | /* Restore original y vector */ |
| 252 | DBL_MEMCPY (y, y0_orig, dim); |
| 253 | return s; |
| 254 | } |
| 255 | |
| 256 | DBL_MEMCPY (y0, y, dim); |
| 257 | |
| 258 | s = gear2_step (y, state, h / 2.0, t + h / 2.0, dim, sys); |
| 259 | |
| 260 | if (s != GSL_SUCCESS) |
| 261 | { |
| 262 | /* Restore original y vector */ |
| 263 | DBL_MEMCPY (y, y0_orig, dim); |
| 264 | return s; |
| 265 | } |
| 266 | |
| 267 | /* Cleanup update */ |
| 268 | |
| 269 | if (dydt_out != NULL) |
| 270 | { |
| 271 | s = GSL_ODEIV_FN_EVAL (sys, t + h, y, dydt_out); |
| 272 | |
| 273 | if (s != GSL_SUCCESS) |
| 274 | { |
| 275 | /* Restore original y vector */ |
| 276 | DBL_MEMCPY (y, y0_orig, dim); |
| 277 | return s; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* Estimate error and update the state buffer. */ |
| 282 | |
| 283 | for (i = 0; i < dim; i++) |
| 284 | { |
| 285 | yerr[i] = 4.0 * (y[i] - y_onestep[i]); |
| 286 | yim1[i] = y0[i]; |
| 287 | } |
| 288 | |
| 289 | /* Make note of step size. */ |
| 290 | state->last_h = h; |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | static int |
| 297 | gear2_reset (void *vstate, size_t dim) |
| 298 | { |
| 299 | gear2_state_t *state = (gear2_state_t *) vstate; |
| 300 | |
| 301 | DBL_ZERO_MEMSET (state->yim1, dim); |
| 302 | DBL_ZERO_MEMSET (state->k, dim); |
| 303 | DBL_ZERO_MEMSET (state->y0, dim); |
| 304 | |
| 305 | state->primed = 0; |
| 306 | state->last_h = 0.0; |
| 307 | return GSL_SUCCESS; |
| 308 | } |
| 309 | |
| 310 | static unsigned int |
| 311 | gear2_order (void *vstate) |
| 312 | { |
| 313 | return 3; |
| 314 | } |
| 315 | |
| 316 | static void |
| 317 | gear2_free (void *vstate) |
| 318 | { |
| 319 | gear2_state_t *state = (gear2_state_t *) vstate; |
| 320 | |
| 321 | free (state->yim1); |
| 322 | free (state->k); |
| 323 | free (state->y0); |
| 324 | free (state->y0_orig); |
| 325 | free (state->y_onestep); |
| 326 | gsl_odeiv_step_free (state->primer); |
| 327 | |
| 328 | free (state); |
| 329 | } |
| 330 | |
| 331 | static const gsl_odeiv_step_type gear2_type = { "gear2", /* name */ |
| 332 | 1, /* can use dydt_in */ |
| 333 | 0, /* gives exact dydt_out */ |
| 334 | &gear2_alloc, |
| 335 | &gear2_apply, |
| 336 | &gear2_reset, |
| 337 | &gear2_order, |
| 338 | &gear2_free |
| 339 | }; |
| 340 | |
| 341 | const gsl_odeiv_step_type *gsl_odeiv_step_gear2 = &gear2_type; |