gsl_ode-initval__gear2.c 8.3 KB

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