gsl_ode-initval__rk2.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* ode-initval/rk2.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 2(3), Euler-Cauchy */
  20. /* Author: G. Jungman
  21. */
  22. /* Reference: Abramowitz & Stegun, section 25.5. Runge-Kutta 2nd (25.5.7)
  23. and 3rd (25.5.8) order methods */
  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. typedef struct
  31. {
  32. double *k1;
  33. double *k2;
  34. double *k3;
  35. double *ytmp;
  36. }
  37. rk2_state_t;
  38. static void *
  39. rk2_alloc (size_t dim)
  40. {
  41. rk2_state_t *state = (rk2_state_t *) malloc (sizeof (rk2_state_t));
  42. if (state == 0)
  43. {
  44. GSL_ERROR_NULL ("failed to allocate space for rk2_state", GSL_ENOMEM);
  45. }
  46. state->k1 = (double *) malloc (dim * sizeof (double));
  47. if (state->k1 == 0)
  48. {
  49. free (state);
  50. GSL_ERROR_NULL ("failed to allocate space for k1", GSL_ENOMEM);
  51. }
  52. state->k2 = (double *) malloc (dim * sizeof (double));
  53. if (state->k2 == 0)
  54. {
  55. free (state->k1);
  56. free (state);
  57. GSL_ERROR_NULL ("failed to allocate space for k2", GSL_ENOMEM);
  58. }
  59. state->k3 = (double *) malloc (dim * sizeof (double));
  60. if (state->k3 == 0)
  61. {
  62. free (state->k2);
  63. free (state->k1);
  64. free (state);
  65. GSL_ERROR_NULL ("failed to allocate space for k2", GSL_ENOMEM);
  66. }
  67. state->ytmp = (double *) malloc (dim * sizeof (double));
  68. if (state->ytmp == 0)
  69. {
  70. free (state->k3);
  71. free (state->k2);
  72. free (state->k1);
  73. free (state);
  74. GSL_ERROR_NULL ("failed to allocate space for k2", GSL_ENOMEM);
  75. }
  76. return state;
  77. }
  78. static int
  79. rk2_apply (void *vstate,
  80. size_t dim,
  81. double t,
  82. double h,
  83. double y[],
  84. double yerr[],
  85. const double dydt_in[],
  86. double dydt_out[],
  87. const gsl_odeiv_system * sys)
  88. {
  89. rk2_state_t *state = (rk2_state_t *) vstate;
  90. size_t i;
  91. double *const k1 = state->k1;
  92. double *const k2 = state->k2;
  93. double *const k3 = state->k3;
  94. double *const ytmp = state->ytmp;
  95. /* k1 step */
  96. /* k1 = f(t,y) */
  97. if (dydt_in != NULL)
  98. {
  99. DBL_MEMCPY (k1, dydt_in, dim);
  100. }
  101. else
  102. {
  103. int s = GSL_ODEIV_FN_EVAL (sys, t, y, k1);
  104. if (s != GSL_SUCCESS)
  105. {
  106. return s;
  107. }
  108. }
  109. /* k2 step */
  110. /* k2 = f(t + 0.5*h, y + 0.5*k1) */
  111. for (i = 0; i < dim; i++)
  112. {
  113. ytmp[i] = y[i] + 0.5 * h * k1[i];
  114. }
  115. {
  116. int s = GSL_ODEIV_FN_EVAL (sys, t + 0.5 * h, ytmp, k2);
  117. if (s != GSL_SUCCESS)
  118. {
  119. return s;
  120. }
  121. }
  122. /* k3 step */
  123. /* for 3rd order estimates, is used for error estimation
  124. k3 = f(t + h, y - k1 + 2*k2) */
  125. for (i = 0; i < dim; i++)
  126. {
  127. ytmp[i] = y[i] + h * (-k1[i] + 2.0 * k2[i]);
  128. }
  129. {
  130. int s = GSL_ODEIV_FN_EVAL (sys, t + h, ytmp, k3);
  131. if (s != GSL_SUCCESS)
  132. {
  133. return s;
  134. }
  135. }
  136. /* final sum */
  137. for (i = 0; i < dim; i++)
  138. {
  139. /* Save original values if derivative evaluation below fails */
  140. ytmp[i] = y[i];
  141. {
  142. const double ksum3 = (k1[i] + 4.0 * k2[i] + k3[i]) / 6.0;
  143. y[i] += h * ksum3;
  144. }
  145. }
  146. /* Derivatives at output */
  147. if (dydt_out != NULL)
  148. {
  149. int s = GSL_ODEIV_FN_EVAL (sys, t + h, y, dydt_out);
  150. if (s != GSL_SUCCESS)
  151. {
  152. /* Restore original values */
  153. DBL_MEMCPY (y, ytmp, dim);
  154. return s;
  155. }
  156. }
  157. /* Error estimation */
  158. for (i = 0; i < dim; i++)
  159. {
  160. const double ksum3 = (k1[i] + 4.0 * k2[i] + k3[i]) / 6.0;
  161. yerr[i] = h * (k2[i] - ksum3);
  162. }
  163. return GSL_SUCCESS;
  164. }
  165. static int
  166. rk2_reset (void *vstate, size_t dim)
  167. {
  168. rk2_state_t *state = (rk2_state_t *) vstate;
  169. DBL_ZERO_MEMSET (state->k1, dim);
  170. DBL_ZERO_MEMSET (state->k2, dim);
  171. DBL_ZERO_MEMSET (state->k3, dim);
  172. DBL_ZERO_MEMSET (state->ytmp, dim);
  173. return GSL_SUCCESS;
  174. }
  175. static unsigned int
  176. rk2_order (void *vstate)
  177. {
  178. rk2_state_t *state = (rk2_state_t *) vstate;
  179. state = 0; /* prevent warnings about unused parameters */
  180. return 2;
  181. }
  182. static void
  183. rk2_free (void *vstate)
  184. {
  185. rk2_state_t *state = (rk2_state_t *) vstate;
  186. free (state->k1);
  187. free (state->k2);
  188. free (state->k3);
  189. free (state->ytmp);
  190. free (state);
  191. }
  192. static const gsl_odeiv_step_type rk2_type = { "rk2", /* name */
  193. 1, /* can use dydt_in */
  194. 1, /* gives exact dydt_out */
  195. &rk2_alloc,
  196. &rk2_apply,
  197. &rk2_reset,
  198. &rk2_order,
  199. &rk2_free
  200. };
  201. const gsl_odeiv_step_type *gsl_odeiv_step_rk2 = &rk2_type;