gsl_ode-initval__rk4imp.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* ode-initval/rk4imp.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. /* Runge-Kutta 4, Gaussian implicit */
  20. /* Author: G. Jungman
  21. */
  22. /* Error estimation by step doubling, see eg. Ascher, U.M., Petzold,
  23. L.R., Computer methods for ordinary differential and
  24. differential-algebraic equations, SIAM, Philadelphia, 1998.
  25. Method coefficients can also be found in it.
  26. */
  27. #include "gsl__config.h"
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "gsl_math.h"
  31. #include "gsl_errno.h"
  32. #include "gsl_odeiv.h"
  33. #include "gsl_ode-initval__odeiv_util.h"
  34. typedef struct
  35. {
  36. double *k1nu;
  37. double *k2nu;
  38. double *ytmp1;
  39. double *ytmp2;
  40. double *y0;
  41. double *y0_orig;
  42. double *y_onestep;
  43. }
  44. rk4imp_state_t;
  45. static void *
  46. rk4imp_alloc (size_t dim)
  47. {
  48. rk4imp_state_t *state = (rk4imp_state_t *) malloc (sizeof (rk4imp_state_t));
  49. if (state == 0)
  50. {
  51. GSL_ERROR_NULL ("failed to allocate space for rk4imp_state",
  52. GSL_ENOMEM);
  53. }
  54. state->k1nu = (double *) malloc (dim * sizeof (double));
  55. if (state->k1nu == 0)
  56. {
  57. free (state);
  58. GSL_ERROR_NULL ("failed to allocate space for k1nu", GSL_ENOMEM);
  59. }
  60. state->k2nu = (double *) malloc (dim * sizeof (double));
  61. if (state->k2nu == 0)
  62. {
  63. free (state->k1nu);
  64. free (state);
  65. GSL_ERROR_NULL ("failed to allocate space for k2nu", GSL_ENOMEM);
  66. }
  67. state->ytmp1 = (double *) malloc (dim * sizeof (double));
  68. if (state->ytmp1 == 0)
  69. {
  70. free (state->k2nu);
  71. free (state->k1nu);
  72. free (state);
  73. GSL_ERROR_NULL ("failed to allocate space for ytmp1", GSL_ENOMEM);
  74. }
  75. state->ytmp2 = (double *) malloc (dim * sizeof (double));
  76. if (state->ytmp2 == 0)
  77. {
  78. free (state->ytmp1);
  79. free (state->k2nu);
  80. free (state->k1nu);
  81. free (state);
  82. GSL_ERROR_NULL ("failed to allocate space for ytmp2", GSL_ENOMEM);
  83. }
  84. state->y0 = (double *) malloc (dim * sizeof (double));
  85. if (state->y0 == 0)
  86. {
  87. free (state->ytmp2);
  88. free (state->ytmp1);
  89. free (state->k2nu);
  90. free (state->k1nu);
  91. free (state);
  92. GSL_ERROR_NULL ("failed to allocate space for y0", GSL_ENOMEM);
  93. }
  94. state->y0_orig = (double *) malloc (dim * sizeof (double));
  95. if (state->y0_orig == 0)
  96. {
  97. free (state->y0);
  98. free (state->ytmp2);
  99. free (state->ytmp1);
  100. free (state->k2nu);
  101. free (state->k1nu);
  102. free (state);
  103. GSL_ERROR_NULL ("failed to allocate space for y0_orig", GSL_ENOMEM);
  104. }
  105. state->y_onestep = (double *) malloc (dim * sizeof (double));
  106. if (state->y_onestep == 0)
  107. {
  108. free (state->y0_orig);
  109. free (state->y0);
  110. free (state->ytmp2);
  111. free (state->ytmp1);
  112. free (state->k2nu);
  113. free (state->k1nu);
  114. free (state);
  115. GSL_ERROR_NULL ("failed to allocate space for y_onestep", GSL_ENOMEM);
  116. }
  117. return state;
  118. }
  119. static int
  120. rk4imp_step (double *y, rk4imp_state_t *state,
  121. const double h, const double t,
  122. const size_t dim, const gsl_odeiv_system *sys)
  123. {
  124. /* Makes a Runge-Kutta 4th order implicit advance with step size h.
  125. y0 is initial values of variables y.
  126. The implicit matrix equations to solve are:
  127. Y1 = y0 + h * a11 * f(t + h * c1, Y1) + h * a12 * f(t + h * c2, Y2)
  128. Y2 = y0 + h * a21 * f(t + h * c1, Y1) + h * a22 * f(t + h * c2, Y2)
  129. y = y0 + h * b1 * f(t + h * c1, Y1) + h * b2 * f(t + h * c2, Y2)
  130. with constant coefficients a, b and c. For this method
  131. they are: b=[0.5 0.5] c=[(3-sqrt(3))/6 (3+sqrt(3))/6]
  132. a11=1/4, a12=(3-2*sqrt(3))/12, a21=(3+2*sqrt(3))/12 and a22=1/4
  133. */
  134. const double ir3 = 1.0 / M_SQRT3;
  135. const int iter_steps = 3;
  136. int nu;
  137. size_t i;
  138. double *const k1nu = state->k1nu;
  139. double *const k2nu = state->k2nu;
  140. double *const ytmp1 = state->ytmp1;
  141. double *const ytmp2 = state->ytmp2;
  142. /* iterative solution of Y1 and Y2.
  143. Note: This method does not check for convergence of the
  144. iterative solution!
  145. */
  146. for (nu = 0; nu < iter_steps; nu++)
  147. {
  148. for (i = 0; i < dim; i++)
  149. {
  150. ytmp1[i] =
  151. y[i] + h * (0.25 * k1nu[i] + 0.5 * (0.5 - ir3) * k2nu[i]);
  152. ytmp2[i] =
  153. y[i] + h * (0.25 * k2nu[i] + 0.5 * (0.5 + ir3) * k1nu[i]);
  154. }
  155. {
  156. int s =
  157. GSL_ODEIV_FN_EVAL (sys, t + 0.5 * h * (1.0 - ir3), ytmp1, k1nu);
  158. if (s != GSL_SUCCESS)
  159. {
  160. return s;
  161. }
  162. }
  163. {
  164. int s =
  165. GSL_ODEIV_FN_EVAL (sys, t + 0.5 * h * (1.0 + ir3), ytmp2, k2nu);
  166. if (s != GSL_SUCCESS)
  167. {
  168. return s;
  169. }
  170. }
  171. }
  172. /* assignment */
  173. for (i = 0; i < dim; i++)
  174. {
  175. const double d_i = 0.5 * (k1nu[i] + k2nu[i]);
  176. y[i] += h * d_i;
  177. }
  178. return GSL_SUCCESS;
  179. }
  180. static int
  181. rk4imp_apply (void *vstate,
  182. size_t dim,
  183. double t,
  184. double h,
  185. double y[],
  186. double yerr[],
  187. const double dydt_in[],
  188. double dydt_out[],
  189. const gsl_odeiv_system * sys)
  190. {
  191. rk4imp_state_t *state = (rk4imp_state_t *) vstate;
  192. size_t i;
  193. double *y0 = state->y0;
  194. double *y0_orig = state->y0_orig;
  195. double *y_onestep = state->y_onestep;
  196. double *k1nu = state->k1nu;
  197. double *k2nu = state->k2nu;
  198. /* Initialization step */
  199. DBL_MEMCPY (y0, y, dim);
  200. /* Save initial values in case of failure */
  201. DBL_MEMCPY (y0_orig, y, dim);
  202. if (dydt_in != 0)
  203. {
  204. DBL_MEMCPY (k1nu, dydt_in, dim);
  205. }
  206. else
  207. {
  208. int s = GSL_ODEIV_FN_EVAL (sys, t, y, k1nu);
  209. if (s != GSL_SUCCESS)
  210. {
  211. return s;
  212. }
  213. }
  214. DBL_MEMCPY (k2nu, k1nu, dim);
  215. /* First traverse h with one step (save to y_onestep) */
  216. DBL_MEMCPY (y_onestep, y, dim);
  217. {
  218. int s = rk4imp_step (y_onestep, state, h, t, dim, sys);
  219. if (s != GSL_SUCCESS)
  220. {
  221. return s;
  222. }
  223. }
  224. /* Then with two steps with half step length (save to y) */
  225. {
  226. int s = rk4imp_step (y, state, h/2.0, t, dim, sys);
  227. if (s != GSL_SUCCESS)
  228. {
  229. /* Restore original y vector */
  230. DBL_MEMCPY (y, y0_orig, dim);
  231. return s;
  232. }
  233. }
  234. DBL_MEMCPY (y0, y, dim);
  235. {
  236. int s = GSL_ODEIV_FN_EVAL (sys, t + h/2.0, y, k1nu);
  237. if (s != GSL_SUCCESS)
  238. {
  239. /* Restore original y vector */
  240. DBL_MEMCPY (y, y0_orig, dim);
  241. return s;
  242. }
  243. }
  244. DBL_MEMCPY (k2nu, k1nu, dim);
  245. {
  246. int s = rk4imp_step (y, state, h/2.0, t + h/2.0, dim, sys);
  247. if (s != GSL_SUCCESS)
  248. {
  249. /* Restore original y vector */
  250. DBL_MEMCPY (y, y0_orig, dim);
  251. return s;
  252. }
  253. }
  254. /* Derivatives at output */
  255. if (dydt_out != NULL)
  256. {
  257. int s = GSL_ODEIV_FN_EVAL (sys, t + h, y, dydt_out);
  258. if (s != GSL_SUCCESS) {
  259. /* Restore original y vector */
  260. DBL_MEMCPY (y, y0_orig, dim);
  261. return s;
  262. }
  263. }
  264. /* Error estimation */
  265. /* Denominator in step doubling error equation
  266. * yerr = 0.5 * | y(onestep) - y(twosteps) | / (2^order - 1)
  267. */
  268. for (i = 0; i < dim; i++)
  269. {
  270. yerr[i] = 8.0 * 0.5 * (y[i] - y_onestep[i]) / 15.0;
  271. }
  272. return GSL_SUCCESS;
  273. }
  274. static int
  275. rk4imp_reset (void *vstate, size_t dim)
  276. {
  277. rk4imp_state_t *state = (rk4imp_state_t *) vstate;
  278. DBL_ZERO_MEMSET (state->y_onestep, dim);
  279. DBL_ZERO_MEMSET (state->y0_orig, dim);
  280. DBL_ZERO_MEMSET (state->y0, dim);
  281. DBL_ZERO_MEMSET (state->k1nu, dim);
  282. DBL_ZERO_MEMSET (state->k2nu, dim);
  283. DBL_ZERO_MEMSET (state->ytmp1, dim);
  284. DBL_ZERO_MEMSET (state->ytmp2, dim);
  285. return GSL_SUCCESS;
  286. }
  287. static unsigned int
  288. rk4imp_order (void *vstate)
  289. {
  290. rk4imp_state_t *state = (rk4imp_state_t *) vstate;
  291. state = 0; /* prevent warnings about unused parameters */
  292. return 4;
  293. }
  294. static void
  295. rk4imp_free (void *vstate)
  296. {
  297. rk4imp_state_t *state = (rk4imp_state_t *) vstate;
  298. free (state->y_onestep);
  299. free (state->y0_orig);
  300. free (state->y0);
  301. free (state->k1nu);
  302. free (state->k2nu);
  303. free (state->ytmp1);
  304. free (state->ytmp2);
  305. free (state);
  306. }
  307. static const gsl_odeiv_step_type rk4imp_type = { "rk4imp", /* name */
  308. 1, /* can use dydt_in? */
  309. 1, /* gives exact dydt_out? */
  310. &rk4imp_alloc,
  311. &rk4imp_apply,
  312. &rk4imp_reset,
  313. &rk4imp_order,
  314. &rk4imp_free
  315. };
  316. const gsl_odeiv_step_type *gsl_odeiv_step_rk4imp = &rk4imp_type;