gsl_ode-initval__rk4.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* ode-initval/rk4.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 4th order, Classical */
  20. /* Author: G. Jungman
  21. */
  22. /* Reference: Abramowitz & Stegun, section 25.5. equation 25.5.10
  23. Error estimation by step doubling, see eg. Ascher, U.M., Petzold,
  24. L.R., Computer methods for ordinary differential and
  25. differential-algebraic equations, SIAM, Philadelphia, 1998.
  26. */
  27. #include "gsl__config.h"
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "gsl_errno.h"
  31. #include "gsl_odeiv.h"
  32. #include "gsl_ode-initval__odeiv_util.h"
  33. typedef struct
  34. {
  35. double *k;
  36. double *k1;
  37. double *y0;
  38. double *ytmp;
  39. double *y_onestep;
  40. }
  41. rk4_state_t;
  42. static void *
  43. rk4_alloc (size_t dim)
  44. {
  45. rk4_state_t *state = (rk4_state_t *) malloc (sizeof (rk4_state_t));
  46. if (state == 0)
  47. {
  48. GSL_ERROR_NULL ("failed to allocate space for rk4_state", GSL_ENOMEM);
  49. }
  50. state->k = (double *) malloc (dim * sizeof (double));
  51. if (state->k == 0)
  52. {
  53. free (state);
  54. GSL_ERROR_NULL ("failed to allocate space for k", GSL_ENOMEM);
  55. }
  56. state->k1 = (double *) malloc (dim * sizeof (double));
  57. if (state->k1 == 0)
  58. {
  59. free (state);
  60. free (state->k);
  61. GSL_ERROR_NULL ("failed to allocate space for k1", GSL_ENOMEM);
  62. }
  63. state->y0 = (double *) malloc (dim * sizeof (double));
  64. if (state->y0 == 0)
  65. {
  66. free (state->k);
  67. free (state->k1);
  68. free (state);
  69. GSL_ERROR_NULL ("failed to allocate space for y0", GSL_ENOMEM);
  70. }
  71. state->ytmp = (double *) malloc (dim * sizeof (double));
  72. if (state->ytmp == 0)
  73. {
  74. free (state->y0);
  75. free (state->k);
  76. free (state->k1);
  77. free (state);
  78. GSL_ERROR_NULL ("failed to allocate space for ytmp", GSL_ENOMEM);
  79. }
  80. state->y_onestep = (double *) malloc (dim * sizeof (double));
  81. if (state->y_onestep == 0)
  82. {
  83. free (state->ytmp);
  84. free (state->y0);
  85. free (state->k);
  86. free (state->k1);
  87. free (state);
  88. GSL_ERROR_NULL ("failed to allocate space for ytmp", GSL_ENOMEM);
  89. }
  90. return state;
  91. }
  92. static int
  93. rk4_step (double *y, const rk4_state_t *state,
  94. const double h, const double t, const size_t dim,
  95. const gsl_odeiv_system *sys)
  96. {
  97. /* Makes a Runge-Kutta 4th order advance with step size h. */
  98. /* initial values of variables y. */
  99. const double *y0 = state->y0;
  100. /* work space */
  101. double *ytmp = state->ytmp;
  102. /* Runge-Kutta coefficients. Contains values of coefficient k1
  103. in the beginning
  104. */
  105. double *k = state->k;
  106. size_t i;
  107. /* k1 step */
  108. for (i = 0; i < dim; i++)
  109. {
  110. y[i] += h / 6.0 * k[i];
  111. ytmp[i] = y0[i] + 0.5 * h * k[i];
  112. }
  113. /* k2 step */
  114. {
  115. int s = GSL_ODEIV_FN_EVAL (sys, t + 0.5 * h, ytmp, k);
  116. if (s != GSL_SUCCESS)
  117. {
  118. return s;
  119. }
  120. }
  121. for (i = 0; i < dim; i++)
  122. {
  123. y[i] += h / 3.0 * k[i];
  124. ytmp[i] = y0[i] + 0.5 * h * k[i];
  125. }
  126. /* k3 step */
  127. {
  128. int s = GSL_ODEIV_FN_EVAL (sys, t + 0.5 * h, ytmp, k);
  129. if (s != GSL_SUCCESS)
  130. {
  131. return s;
  132. }
  133. }
  134. for (i = 0; i < dim; i++)
  135. {
  136. y[i] += h / 3.0 * k[i];
  137. ytmp[i] = y0[i] + h * k[i];
  138. }
  139. /* k4 step */
  140. {
  141. int s = GSL_ODEIV_FN_EVAL (sys, t + h, ytmp, k);
  142. if (s != GSL_SUCCESS)
  143. {
  144. return s;
  145. }
  146. }
  147. for (i = 0; i < dim; i++)
  148. {
  149. y[i] += h / 6.0 * k[i];
  150. }
  151. return GSL_SUCCESS;
  152. }
  153. static int
  154. rk4_apply (void *vstate,
  155. size_t dim,
  156. double t,
  157. double h,
  158. double y[],
  159. double yerr[],
  160. const double dydt_in[],
  161. double dydt_out[],
  162. const gsl_odeiv_system * sys)
  163. {
  164. rk4_state_t *state = (rk4_state_t *) vstate;
  165. size_t i;
  166. double *const k = state->k;
  167. double *const k1 = state->k1;
  168. double *const y0 = state->y0;
  169. double *const y_onestep = state->y_onestep;
  170. DBL_MEMCPY (y0, y, dim);
  171. if (dydt_in != NULL)
  172. {
  173. DBL_MEMCPY (k, dydt_in, dim);
  174. }
  175. else
  176. {
  177. int s = GSL_ODEIV_FN_EVAL (sys, t, y0, k);
  178. if (s != GSL_SUCCESS)
  179. {
  180. return s;
  181. }
  182. }
  183. /* Error estimation is done by step doubling procedure */
  184. /* Save first point derivatives*/
  185. DBL_MEMCPY (k1, k, dim);
  186. /* First traverse h with one step (save to y_onestep) */
  187. DBL_MEMCPY (y_onestep, y, dim);
  188. {
  189. int s = rk4_step (y_onestep, state, h, t, dim, sys);
  190. if (s != GSL_SUCCESS)
  191. {
  192. return s;
  193. }
  194. }
  195. /* Then with two steps with half step length (save to y) */
  196. DBL_MEMCPY (k, k1, dim);
  197. {
  198. int s = rk4_step (y, state, h/2.0, t, dim, sys);
  199. if (s != GSL_SUCCESS)
  200. {
  201. /* Restore original values */
  202. DBL_MEMCPY (y, y0, dim);
  203. return s;
  204. }
  205. }
  206. /* Update before second step */
  207. {
  208. int s = GSL_ODEIV_FN_EVAL (sys, t + h/2.0, y, k);
  209. if (s != GSL_SUCCESS)
  210. {
  211. /* Restore original values */
  212. DBL_MEMCPY (y, y0, dim);
  213. return s;
  214. }
  215. }
  216. /* Save original y0 to k1 for possible failures */
  217. DBL_MEMCPY (k1, y0, dim);
  218. /* Update y0 for second step */
  219. DBL_MEMCPY (y0, y, dim);
  220. {
  221. int s = rk4_step (y, state, h/2.0, t + h/2.0, dim, sys);
  222. if (s != GSL_SUCCESS)
  223. {
  224. /* Restore original values */
  225. DBL_MEMCPY (y, k1, dim);
  226. return s;
  227. }
  228. }
  229. /* Derivatives at output */
  230. if (dydt_out != NULL) {
  231. int s = GSL_ODEIV_FN_EVAL (sys, t + h, y, dydt_out);
  232. if (s != GSL_SUCCESS)
  233. {
  234. /* Restore original values */
  235. DBL_MEMCPY (y, k1, dim);
  236. return s;
  237. }
  238. }
  239. /* Error estimation
  240. yerr = C * 0.5 * | y(onestep) - y(twosteps) | / (2^order - 1)
  241. constant C is approximately 8.0 to ensure 90% of samples lie within
  242. the error (assuming a gaussian distribution with prior p(sigma)=1/sigma.)
  243. */
  244. for (i = 0; i < dim; i++)
  245. {
  246. yerr[i] = 4.0 * (y[i] - y_onestep[i]) / 15.0;
  247. }
  248. return GSL_SUCCESS;
  249. }
  250. static int
  251. rk4_reset (void *vstate, size_t dim)
  252. {
  253. rk4_state_t *state = (rk4_state_t *) vstate;
  254. DBL_ZERO_MEMSET (state->k, dim);
  255. DBL_ZERO_MEMSET (state->k1, dim);
  256. DBL_ZERO_MEMSET (state->y0, dim);
  257. DBL_ZERO_MEMSET (state->ytmp, dim);
  258. DBL_ZERO_MEMSET (state->y_onestep, dim);
  259. return GSL_SUCCESS;
  260. }
  261. static unsigned int
  262. rk4_order (void *vstate)
  263. {
  264. rk4_state_t *state = (rk4_state_t *) vstate;
  265. state = 0; /* prevent warnings about unused parameters */
  266. return 4;
  267. }
  268. static void
  269. rk4_free (void *vstate)
  270. {
  271. rk4_state_t *state = (rk4_state_t *) vstate;
  272. free (state->k);
  273. free (state->k1);
  274. free (state->y0);
  275. free (state->ytmp);
  276. free (state->y_onestep);
  277. free (state);
  278. }
  279. static const gsl_odeiv_step_type rk4_type = { "rk4", /* name */
  280. 1, /* can use dydt_in */
  281. 1, /* gives exact dydt_out */
  282. &rk4_alloc,
  283. &rk4_apply,
  284. &rk4_reset,
  285. &rk4_order,
  286. &rk4_free
  287. };
  288. const gsl_odeiv_step_type *gsl_odeiv_step_rk4 = &rk4_type;