gsl_ode-initval__rkck.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* ode-initval/rkck.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(5), Cash-Karp */
  20. /* Reference: Cash, J.R., Karp, A.H., ACM Transactions of Mathematical
  21. Software, vol. 16 (1990) 201-222. */
  22. /* Author: G. Jungman
  23. */
  24. #include "gsl__config.h"
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "gsl_errno.h"
  28. #include "gsl_odeiv.h"
  29. #include "gsl_ode-initval__odeiv_util.h"
  30. /* Cash-Karp constants */
  31. static const double ah[] = { 1.0 / 5.0, 0.3, 3.0 / 5.0, 1.0, 7.0 / 8.0 };
  32. static const double b21 = 1.0 / 5.0;
  33. static const double b3[] = { 3.0 / 40.0, 9.0 / 40.0 };
  34. static const double b4[] = { 0.3, -0.9, 1.2 };
  35. static const double b5[] = { -11.0 / 54.0, 2.5, -70.0 / 27.0, 35.0 / 27.0 };
  36. static const double b6[] =
  37. { 1631.0 / 55296.0, 175.0 / 512.0, 575.0 / 13824.0, 44275.0 / 110592.0,
  38. 253.0 / 4096.0 };
  39. static const double c1 = 37.0 / 378.0;
  40. static const double c3 = 250.0 / 621.0;
  41. static const double c4 = 125.0 / 594.0;
  42. static const double c6 = 512.0 / 1771.0;
  43. /* These are the differences of fifth and fourth order coefficients
  44. for error estimation */
  45. static const double ec[] = { 0.0,
  46. 37.0 / 378.0 - 2825.0 / 27648.0,
  47. 0.0,
  48. 250.0 / 621.0 - 18575.0 / 48384.0,
  49. 125.0 / 594.0 - 13525.0 / 55296.0,
  50. -277.0 / 14336.0,
  51. 512.0 / 1771.0 - 0.25
  52. };
  53. typedef struct
  54. {
  55. double *k1;
  56. double *k2;
  57. double *k3;
  58. double *k4;
  59. double *k5;
  60. double *k6;
  61. double *y0;
  62. double *ytmp;
  63. }
  64. rkck_state_t;
  65. static void *
  66. rkck_alloc (size_t dim)
  67. {
  68. rkck_state_t *state = (rkck_state_t *) malloc (sizeof (rkck_state_t));
  69. if (state == 0)
  70. {
  71. GSL_ERROR_NULL ("failed to allocate space for rkck_state", GSL_ENOMEM);
  72. }
  73. state->k1 = (double *) malloc (dim * sizeof (double));
  74. if (state->k1 == 0)
  75. {
  76. free (state);
  77. GSL_ERROR_NULL ("failed to allocate space for k1", GSL_ENOMEM);
  78. }
  79. state->k2 = (double *) malloc (dim * sizeof (double));
  80. if (state->k2 == 0)
  81. {
  82. free (state->k1);
  83. free (state);
  84. GSL_ERROR_NULL ("failed to allocate space for k2", GSL_ENOMEM);
  85. }
  86. state->k3 = (double *) malloc (dim * sizeof (double));
  87. if (state->k3 == 0)
  88. {
  89. free (state->k2);
  90. free (state->k1);
  91. free (state);
  92. GSL_ERROR_NULL ("failed to allocate space for k3", GSL_ENOMEM);
  93. }
  94. state->k4 = (double *) malloc (dim * sizeof (double));
  95. if (state->k4 == 0)
  96. {
  97. free (state->k3);
  98. free (state->k2);
  99. free (state->k1);
  100. free (state);
  101. GSL_ERROR_NULL ("failed to allocate space for k4", GSL_ENOMEM);
  102. }
  103. state->k5 = (double *) malloc (dim * sizeof (double));
  104. if (state->k5 == 0)
  105. {
  106. free (state->k4);
  107. free (state->k3);
  108. free (state->k2);
  109. free (state->k1);
  110. free (state);
  111. GSL_ERROR_NULL ("failed to allocate space for k5", GSL_ENOMEM);
  112. }
  113. state->k6 = (double *) malloc (dim * sizeof (double));
  114. if (state->k6 == 0)
  115. {
  116. free (state->k5);
  117. free (state->k4);
  118. free (state->k3);
  119. free (state->k2);
  120. free (state->k1);
  121. free (state);
  122. GSL_ERROR_NULL ("failed to allocate space for k6", GSL_ENOMEM);
  123. }
  124. state->y0 = (double *) malloc (dim * sizeof (double));
  125. if (state->y0 == 0)
  126. {
  127. free (state->k6);
  128. free (state->k5);
  129. free (state->k4);
  130. free (state->k3);
  131. free (state->k2);
  132. free (state->k1);
  133. free (state);
  134. GSL_ERROR_NULL ("failed to allocate space for y0", GSL_ENOMEM);
  135. }
  136. state->ytmp = (double *) malloc (dim * sizeof (double));
  137. if (state->ytmp == 0)
  138. {
  139. free (state->y0);
  140. free (state->k6);
  141. free (state->k5);
  142. free (state->k4);
  143. free (state->k3);
  144. free (state->k2);
  145. free (state->k1);
  146. free (state);
  147. GSL_ERROR_NULL ("failed to allocate space for ytmp", GSL_ENOMEM);
  148. }
  149. return state;
  150. }
  151. static int
  152. rkck_apply (void *vstate,
  153. size_t dim,
  154. double t,
  155. double h,
  156. double y[],
  157. double yerr[],
  158. const double dydt_in[],
  159. double dydt_out[], const gsl_odeiv_system * sys)
  160. {
  161. rkck_state_t *state = (rkck_state_t *) vstate;
  162. size_t i;
  163. double *const k1 = state->k1;
  164. double *const k2 = state->k2;
  165. double *const k3 = state->k3;
  166. double *const k4 = state->k4;
  167. double *const k5 = state->k5;
  168. double *const k6 = state->k6;
  169. double *const ytmp = state->ytmp;
  170. double *const y0 = state->y0;
  171. DBL_MEMCPY (y0, y, dim);
  172. /* k1 step */
  173. if (dydt_in != NULL)
  174. {
  175. DBL_MEMCPY (k1, dydt_in, dim);
  176. }
  177. else
  178. {
  179. int s = GSL_ODEIV_FN_EVAL (sys, t, y, k1);
  180. if (s != GSL_SUCCESS)
  181. {
  182. return s;
  183. }
  184. }
  185. for (i = 0; i < dim; i++)
  186. ytmp[i] = y[i] + b21 * h * k1[i];
  187. /* k2 step */
  188. {
  189. int s = GSL_ODEIV_FN_EVAL (sys, t + ah[0] * h, ytmp, k2);
  190. if (s != GSL_SUCCESS)
  191. {
  192. return s;
  193. }
  194. }
  195. for (i = 0; i < dim; i++)
  196. ytmp[i] = y[i] + h * (b3[0] * k1[i] + b3[1] * k2[i]);
  197. /* k3 step */
  198. {
  199. int s = GSL_ODEIV_FN_EVAL (sys, t + ah[1] * h, ytmp, k3);
  200. if (s != GSL_SUCCESS)
  201. {
  202. return s;
  203. }
  204. }
  205. for (i = 0; i < dim; i++)
  206. ytmp[i] = y[i] + h * (b4[0] * k1[i] + b4[1] * k2[i] + b4[2] * k3[i]);
  207. /* k4 step */
  208. {
  209. int s = GSL_ODEIV_FN_EVAL (sys, t + ah[2] * h, ytmp, k4);
  210. if (s != GSL_SUCCESS)
  211. {
  212. return s;
  213. }
  214. }
  215. for (i = 0; i < dim; i++)
  216. ytmp[i] =
  217. y[i] + h * (b5[0] * k1[i] + b5[1] * k2[i] + b5[2] * k3[i] +
  218. b5[3] * k4[i]);
  219. /* k5 step */
  220. {
  221. int s = GSL_ODEIV_FN_EVAL (sys, t + ah[3] * h, ytmp, k5);
  222. if (s != GSL_SUCCESS)
  223. {
  224. return s;
  225. }
  226. }
  227. for (i = 0; i < dim; i++)
  228. ytmp[i] =
  229. y[i] + h * (b6[0] * k1[i] + b6[1] * k2[i] + b6[2] * k3[i] +
  230. b6[3] * k4[i] + b6[4] * k5[i]);
  231. /* k6 step and final sum */
  232. {
  233. int s = GSL_ODEIV_FN_EVAL (sys, t + ah[4] * h, ytmp, k6);
  234. if (s != GSL_SUCCESS)
  235. {
  236. return s;
  237. }
  238. }
  239. for (i = 0; i < dim; i++)
  240. {
  241. const double d_i = c1 * k1[i] + c3 * k3[i] + c4 * k4[i] + c6 * k6[i];
  242. y[i] += h * d_i;
  243. }
  244. /* Evaluate dydt_out[]. */
  245. if (dydt_out != NULL)
  246. {
  247. int s = GSL_ODEIV_FN_EVAL (sys, t + h, y, dydt_out);
  248. if (s != GSL_SUCCESS)
  249. {
  250. /* Restore initial values */
  251. DBL_MEMCPY (y, y0, dim);
  252. return s;
  253. }
  254. }
  255. /* difference between 4th and 5th order */
  256. for (i = 0; i < dim; i++)
  257. {
  258. yerr[i] = h * (ec[1] * k1[i] + ec[3] * k3[i] + ec[4] * k4[i]
  259. + ec[5] * k5[i] + ec[6] * k6[i]);
  260. }
  261. return GSL_SUCCESS;
  262. }
  263. static int
  264. rkck_reset (void *vstate, size_t dim)
  265. {
  266. rkck_state_t *state = (rkck_state_t *) vstate;
  267. DBL_ZERO_MEMSET (state->k1, dim);
  268. DBL_ZERO_MEMSET (state->k2, dim);
  269. DBL_ZERO_MEMSET (state->k3, dim);
  270. DBL_ZERO_MEMSET (state->k4, dim);
  271. DBL_ZERO_MEMSET (state->k5, dim);
  272. DBL_ZERO_MEMSET (state->k6, dim);
  273. DBL_ZERO_MEMSET (state->ytmp, dim);
  274. DBL_ZERO_MEMSET (state->y0, dim);
  275. return GSL_SUCCESS;
  276. }
  277. static unsigned int
  278. rkck_order (void *vstate)
  279. {
  280. rkck_state_t *state = (rkck_state_t *) vstate;
  281. state = 0; /* prevent warnings about unused parameters */
  282. return 5; /* FIXME: should this be 4? */
  283. }
  284. static void
  285. rkck_free (void *vstate)
  286. {
  287. rkck_state_t *state = (rkck_state_t *) vstate;
  288. free (state->ytmp);
  289. free (state->y0);
  290. free (state->k6);
  291. free (state->k5);
  292. free (state->k4);
  293. free (state->k3);
  294. free (state->k2);
  295. free (state->k1);
  296. free (state);
  297. }
  298. static const gsl_odeiv_step_type rkck_type = { "rkck", /* name */
  299. 1, /* can use dydt_in */
  300. 1, /* gives exact dydt_out */
  301. &rkck_alloc,
  302. &rkck_apply,
  303. &rkck_reset,
  304. &rkck_order,
  305. &rkck_free
  306. };
  307. const gsl_odeiv_step_type *gsl_odeiv_step_rkck = &rkck_type;