gsl_ode-initval__cstd.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* ode-initval/cstd.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. #include "gsl__config.h"
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include "gsl_errno.h"
  23. #include "gsl_math.h"
  24. #include "gsl_odeiv.h"
  25. typedef struct
  26. {
  27. double eps_abs;
  28. double eps_rel;
  29. double a_y;
  30. double a_dydt;
  31. }
  32. std_control_state_t;
  33. static void *
  34. std_control_alloc (void)
  35. {
  36. std_control_state_t * s =
  37. (std_control_state_t *) malloc (sizeof(std_control_state_t));
  38. if (s == 0)
  39. {
  40. GSL_ERROR_NULL ("failed to allocate space for std_control_state",
  41. GSL_ENOMEM);
  42. }
  43. return s;
  44. }
  45. static int
  46. std_control_init (void * vstate,
  47. double eps_abs, double eps_rel, double a_y, double a_dydt)
  48. {
  49. std_control_state_t * s = (std_control_state_t *) vstate;
  50. if (eps_abs < 0)
  51. {
  52. GSL_ERROR ("eps_abs is negative", GSL_EINVAL);
  53. }
  54. else if (eps_rel < 0)
  55. {
  56. GSL_ERROR ("eps_rel is negative", GSL_EINVAL);
  57. }
  58. else if (a_y < 0)
  59. {
  60. GSL_ERROR ("a_y is negative", GSL_EINVAL);
  61. }
  62. else if (a_dydt < 0)
  63. {
  64. GSL_ERROR ("a_dydt is negative", GSL_EINVAL);
  65. }
  66. s->eps_rel = eps_rel;
  67. s->eps_abs = eps_abs;
  68. s->a_y = a_y;
  69. s->a_dydt = a_dydt;
  70. return GSL_SUCCESS;
  71. }
  72. static int
  73. std_control_hadjust(void * vstate, size_t dim, unsigned int ord, const double y[], const double yerr[], const double yp[], double * h)
  74. {
  75. std_control_state_t *state = (std_control_state_t *) vstate;
  76. const double eps_abs = state->eps_abs;
  77. const double eps_rel = state->eps_rel;
  78. const double a_y = state->a_y;
  79. const double a_dydt = state->a_dydt;
  80. const double S = 0.9;
  81. const double h_old = *h;
  82. double rmax = DBL_MIN;
  83. size_t i;
  84. for(i=0; i<dim; i++) {
  85. const double D0 =
  86. eps_rel * (a_y * fabs(y[i]) + a_dydt * fabs(h_old * yp[i])) + eps_abs;
  87. const double r = fabs(yerr[i]) / fabs(D0);
  88. rmax = GSL_MAX_DBL(r, rmax);
  89. }
  90. if(rmax > 1.1) {
  91. /* decrease step, no more than factor of 5, but a fraction S more
  92. than scaling suggests (for better accuracy) */
  93. double r = S / pow(rmax, 1.0/ord);
  94. if (r < 0.2)
  95. r = 0.2;
  96. *h = r * h_old;
  97. return GSL_ODEIV_HADJ_DEC;
  98. }
  99. else if(rmax < 0.5) {
  100. /* increase step, no more than factor of 5 */
  101. double r = S / pow(rmax, 1.0/(ord+1.0));
  102. if (r > 5.0)
  103. r = 5.0;
  104. if (r < 1.0) /* don't allow any decrease caused by S<1 */
  105. r = 1.0;
  106. *h = r * h_old;
  107. return GSL_ODEIV_HADJ_INC;
  108. }
  109. else {
  110. /* no change */
  111. return GSL_ODEIV_HADJ_NIL;
  112. }
  113. }
  114. static void
  115. std_control_free (void * vstate)
  116. {
  117. std_control_state_t *state = (std_control_state_t *) vstate;
  118. free (state);
  119. }
  120. static const gsl_odeiv_control_type std_control_type =
  121. {"standard", /* name */
  122. &std_control_alloc,
  123. &std_control_init,
  124. &std_control_hadjust,
  125. &std_control_free};
  126. const gsl_odeiv_control_type *gsl_odeiv_control_standard = &std_control_type;
  127. gsl_odeiv_control *
  128. gsl_odeiv_control_standard_new(double eps_abs, double eps_rel,
  129. double a_y, double a_dydt)
  130. {
  131. gsl_odeiv_control * c =
  132. gsl_odeiv_control_alloc (gsl_odeiv_control_standard);
  133. int status = gsl_odeiv_control_init (c, eps_abs, eps_rel, a_y, a_dydt);
  134. if (status != GSL_SUCCESS)
  135. {
  136. gsl_odeiv_control_free (c);
  137. GSL_ERROR_NULL ("error trying to initialize control", status);
  138. }
  139. return c;
  140. }
  141. gsl_odeiv_control *
  142. gsl_odeiv_control_y_new(double eps_abs, double eps_rel)
  143. {
  144. return gsl_odeiv_control_standard_new(eps_abs, eps_rel, 1.0, 0.0);
  145. }
  146. gsl_odeiv_control *
  147. gsl_odeiv_control_yp_new(double eps_abs, double eps_rel)
  148. {
  149. return gsl_odeiv_control_standard_new(eps_abs, eps_rel, 0.0, 1.0);
  150. }