gsl_odeiv.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* ode-initval/gsl_odeiv.h
  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. /* Author: G. Jungman
  20. */
  21. #ifndef __GSL_ODEIV_H__
  22. #define __GSL_ODEIV_H__
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include "gsl_types.h"
  26. #undef __BEGIN_DECLS
  27. #undef __END_DECLS
  28. #ifdef __cplusplus
  29. # define __BEGIN_DECLS extern "C" {
  30. # define __END_DECLS }
  31. #else
  32. # define __BEGIN_DECLS /* empty */
  33. # define __END_DECLS /* empty */
  34. #endif
  35. __BEGIN_DECLS
  36. /* Description of a system of ODEs.
  37. *
  38. * y' = f(t,y) = dydt(t, y)
  39. *
  40. * The system is specified by giving the right-hand-side
  41. * of the equation and possibly a jacobian function.
  42. *
  43. * Some methods require the jacobian function, which calculates
  44. * the matrix dfdy and the vector dfdt. The matrix dfdy conforms
  45. * to the GSL standard, being a continuous range of floating point
  46. * values, in row-order.
  47. *
  48. * As with GSL function objects, user-supplied parameter
  49. * data is also present.
  50. */
  51. typedef struct
  52. {
  53. int (* function) (double t, const double y[], double dydt[], void * params);
  54. int (* jacobian) (double t, const double y[], double * dfdy, double dfdt[], void * params);
  55. size_t dimension;
  56. void * params;
  57. }
  58. gsl_odeiv_system;
  59. #define GSL_ODEIV_FN_EVAL(S,t,y,f) (*((S)->function))(t,y,f,(S)->params)
  60. #define GSL_ODEIV_JA_EVAL(S,t,y,dfdy,dfdt) (*((S)->jacobian))(t,y,dfdy,dfdt,(S)->params)
  61. /* General stepper object.
  62. *
  63. * Opaque object for stepping an ODE system from t to t+h.
  64. * In general the object has some state which facilitates
  65. * iterating the stepping operation.
  66. */
  67. typedef struct
  68. {
  69. const char * name;
  70. int can_use_dydt_in;
  71. int gives_exact_dydt_out;
  72. void * (*alloc) (size_t dim);
  73. int (*apply) (void * state, size_t dim, double t, double h, double y[], double yerr[], const double dydt_in[], double dydt_out[], const gsl_odeiv_system * dydt);
  74. int (*reset) (void * state, size_t dim);
  75. unsigned int (*order) (void * state);
  76. void (*free) (void * state);
  77. }
  78. gsl_odeiv_step_type;
  79. typedef struct {
  80. const gsl_odeiv_step_type * type;
  81. size_t dimension;
  82. void * state;
  83. }
  84. gsl_odeiv_step;
  85. /* Available stepper types.
  86. *
  87. * rk2 : embedded 2nd(3rd) Runge-Kutta
  88. * rk4 : 4th order (classical) Runge-Kutta
  89. * rkck : embedded 4th(5th) Runge-Kutta, Cash-Karp
  90. * rk8pd : embedded 8th(9th) Runge-Kutta, Prince-Dormand
  91. * rk2imp : implicit 2nd order Runge-Kutta at Gaussian points
  92. * rk4imp : implicit 4th order Runge-Kutta at Gaussian points
  93. * gear1 : M=1 implicit Gear method
  94. * gear2 : M=2 implicit Gear method
  95. */
  96. GSL_VAR const gsl_odeiv_step_type *gsl_odeiv_step_rk2;
  97. GSL_VAR const gsl_odeiv_step_type *gsl_odeiv_step_rk4;
  98. GSL_VAR const gsl_odeiv_step_type *gsl_odeiv_step_rkf45;
  99. GSL_VAR const gsl_odeiv_step_type *gsl_odeiv_step_rkck;
  100. GSL_VAR const gsl_odeiv_step_type *gsl_odeiv_step_rk8pd;
  101. GSL_VAR const gsl_odeiv_step_type *gsl_odeiv_step_rk2imp;
  102. GSL_VAR const gsl_odeiv_step_type *gsl_odeiv_step_rk2simp;
  103. GSL_VAR const gsl_odeiv_step_type *gsl_odeiv_step_rk4imp;
  104. GSL_VAR const gsl_odeiv_step_type *gsl_odeiv_step_bsimp;
  105. GSL_VAR const gsl_odeiv_step_type *gsl_odeiv_step_gear1;
  106. GSL_VAR const gsl_odeiv_step_type *gsl_odeiv_step_gear2;
  107. /* Constructor for specialized stepper objects.
  108. */
  109. gsl_odeiv_step * gsl_odeiv_step_alloc(const gsl_odeiv_step_type * T, size_t dim);
  110. int gsl_odeiv_step_reset(gsl_odeiv_step * s);
  111. void gsl_odeiv_step_free(gsl_odeiv_step * s);
  112. /* General stepper object methods.
  113. */
  114. const char * gsl_odeiv_step_name(const gsl_odeiv_step *);
  115. unsigned int gsl_odeiv_step_order(const gsl_odeiv_step * s);
  116. int gsl_odeiv_step_apply(gsl_odeiv_step *, double t, double h, double y[], double yerr[], const double dydt_in[], double dydt_out[], const gsl_odeiv_system * dydt);
  117. /* General step size control object.
  118. *
  119. * The hadjust() method controls the adjustment of
  120. * step size given the result of a step and the error.
  121. * Valid hadjust() methods must return one of the codes below.
  122. *
  123. * The general data can be used by specializations
  124. * to store state and control their heuristics.
  125. */
  126. typedef struct
  127. {
  128. const char * name;
  129. void * (*alloc) (void);
  130. int (*init) (void * state, double eps_abs, double eps_rel, double a_y, double a_dydt);
  131. int (*hadjust) (void * state, size_t dim, unsigned int ord, const double y[], const double yerr[], const double yp[], double * h);
  132. void (*free) (void * state);
  133. }
  134. gsl_odeiv_control_type;
  135. typedef struct
  136. {
  137. const gsl_odeiv_control_type * type;
  138. void * state;
  139. }
  140. gsl_odeiv_control;
  141. /* Possible return values for an hadjust() evolution method.
  142. */
  143. #define GSL_ODEIV_HADJ_INC 1 /* step was increased */
  144. #define GSL_ODEIV_HADJ_NIL 0 /* step unchanged */
  145. #define GSL_ODEIV_HADJ_DEC (-1) /* step decreased */
  146. gsl_odeiv_control * gsl_odeiv_control_alloc(const gsl_odeiv_control_type * T);
  147. int gsl_odeiv_control_init(gsl_odeiv_control * c, double eps_abs, double eps_rel, double a_y, double a_dydt);
  148. void gsl_odeiv_control_free(gsl_odeiv_control * c);
  149. int gsl_odeiv_control_hadjust (gsl_odeiv_control * c, gsl_odeiv_step * s, const double y[], const double yerr[], const double dydt[], double * h);
  150. const char * gsl_odeiv_control_name(const gsl_odeiv_control * c);
  151. /* Available control object constructors.
  152. *
  153. * The standard control object is a four parameter heuristic
  154. * defined as follows:
  155. * D0 = eps_abs + eps_rel * (a_y |y| + a_dydt h |y'|)
  156. * D1 = |yerr|
  157. * q = consistency order of method (q=4 for 4(5) embedded RK)
  158. * S = safety factor (0.9 say)
  159. *
  160. * / (D0/D1)^(1/(q+1)) D0 >= D1
  161. * h_NEW = S h_OLD * |
  162. * \ (D0/D1)^(1/q) D0 < D1
  163. *
  164. * This encompasses all the standard error scaling methods.
  165. *
  166. * The y method is the standard method with a_y=1, a_dydt=0.
  167. * The yp method is the standard method with a_y=0, a_dydt=1.
  168. */
  169. gsl_odeiv_control * gsl_odeiv_control_standard_new(double eps_abs, double eps_rel, double a_y, double a_dydt);
  170. gsl_odeiv_control * gsl_odeiv_control_y_new(double eps_abs, double eps_rel);
  171. gsl_odeiv_control * gsl_odeiv_control_yp_new(double eps_abs, double eps_rel);
  172. /* This controller computes errors using different absolute errors for
  173. * each component
  174. *
  175. * D0 = eps_abs * scale_abs[i] + eps_rel * (a_y |y| + a_dydt h |y'|)
  176. */
  177. gsl_odeiv_control * gsl_odeiv_control_scaled_new(double eps_abs, double eps_rel, double a_y, double a_dydt, const double scale_abs[], size_t dim);
  178. /* General evolution object.
  179. */
  180. typedef struct {
  181. size_t dimension;
  182. double * y0;
  183. double * yerr;
  184. double * dydt_in;
  185. double * dydt_out;
  186. double last_step;
  187. unsigned long int count;
  188. unsigned long int failed_steps;
  189. }
  190. gsl_odeiv_evolve;
  191. /* Evolution object methods.
  192. */
  193. gsl_odeiv_evolve * gsl_odeiv_evolve_alloc(size_t dim);
  194. int gsl_odeiv_evolve_apply(gsl_odeiv_evolve *, gsl_odeiv_control * con, gsl_odeiv_step * step, const gsl_odeiv_system * dydt, double * t, double t1, double * h, double y[]);
  195. int gsl_odeiv_evolve_reset(gsl_odeiv_evolve *);
  196. void gsl_odeiv_evolve_free(gsl_odeiv_evolve *);
  197. __END_DECLS
  198. #endif /* __GSL_ODEIV_H__ */