ama.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* This file is part of the GNU plotutils package. */
  2. /*
  3. * Copyright (C) 1982-1994, Nicholas B. Tufillaro. All rights reserved.
  4. *
  5. * GNU enhancements Copyright (C) 1996, 1997, 2005, 2008, Free Software
  6. * Foundation, Inc.
  7. */
  8. /*
  9. * Adams-Moulton with adaptive step size
  10. */
  11. #include "sys-defines.h"
  12. #include "ode.h"
  13. #include "extern.h"
  14. #include "num.h"
  15. #define PASTVAL (3) /* past values, val[0] is current value */
  16. #define T_LT_TSTOP (tstep>0 ? t<tstop:t>tstop)
  17. #define NEARSTOP (tstep > 0 ? \
  18. t+0.9375*tstep > tstop && t+0.0625*tstep < tstop : \
  19. t+0.9375*tstep < tstop && t+0.0625*tstep > tstop)
  20. void
  21. ama (void)
  22. {
  23. bool gdval = true; /* good value to print ? */
  24. int overtime = 1;
  25. long startit = 0;
  26. double prevstep = 0.0;
  27. double t = tstart;
  28. top:
  29. /* fifth-order Runge-Kutta startup */
  30. it = startit;
  31. while (it <= startit + PASTVAL&&(T_LT_TSTOP || overtime--))
  32. {
  33. symtab->sy_value = t;
  34. field();
  35. if (gdval)
  36. {
  37. for (fsp = symtab; fsp != NULL; fsp = fsp->sy_link)
  38. {
  39. int j;
  40. for (j = it - startit; j > 0; j--)
  41. {
  42. fsp->sy_val[j] = fsp->sy_val[j-1];
  43. fsp->sy_pri[j] = fsp->sy_pri[j-1];
  44. }
  45. fsp->sy_val[0] = fsp->sy_value;
  46. fsp->sy_pri[0] = fsp->sy_prime;
  47. }
  48. printq(); /* output */
  49. if (it == startit + PASTVAL)
  50. break; /* startup complete */
  51. }
  52. if (tstep * (t+tstep-tstop) > 0)
  53. tstep = tstop - t;
  54. for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
  55. {
  56. fsp->sy_k[0] = tstep * fsp->sy_prime;
  57. fsp->sy_value = fsp->sy_val[0] + C20 * fsp->sy_k[0];
  58. }
  59. symtab->sy_value = t + C2t * tstep;
  60. field();
  61. for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
  62. {
  63. fsp->sy_k[1] = tstep * fsp->sy_prime;
  64. fsp->sy_value = fsp->sy_val[0] + C30 * fsp->sy_k[0]
  65. + C31 * fsp->sy_k[1];
  66. }
  67. symtab->sy_value = t + C3t * tstep;
  68. field();
  69. for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
  70. {
  71. fsp->sy_k[2] = tstep * fsp->sy_prime;
  72. fsp->sy_value = fsp->sy_val[0]
  73. + (C40 * fsp->sy_k[0]
  74. + C41 * fsp->sy_k[1]
  75. + C42 * fsp->sy_k[2]);
  76. }
  77. symtab->sy_value = t + C4t * tstep;
  78. field();
  79. for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
  80. {
  81. fsp->sy_k[3] = tstep * fsp->sy_prime;
  82. fsp->sy_value = fsp->sy_val[0]
  83. + (C50 * fsp->sy_k[0]
  84. + C51 * fsp->sy_k[1]
  85. + C52 * fsp->sy_k[2]
  86. + C53 * fsp->sy_k[3]);
  87. }
  88. symtab->sy_value = t + tstep;
  89. field();
  90. for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
  91. {
  92. fsp->sy_k[4] = tstep * fsp->sy_prime;
  93. fsp->sy_value = fsp->sy_val[0]
  94. + (C60 * fsp->sy_k[0]
  95. + C61 * fsp->sy_k[1]
  96. + C62 * fsp->sy_k[2]
  97. + C63 * fsp->sy_k[3]
  98. + C64 * fsp->sy_k[4]);
  99. }
  100. symtab->sy_value = t + C6t * tstep;
  101. field();
  102. for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
  103. fsp->sy_k[5] = tstep * fsp->sy_prime;
  104. for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
  105. {
  106. fsp->sy_predi = fsp->sy_val[0]
  107. + (A0 * fsp->sy_k[0]
  108. + A2 * fsp->sy_k[2]
  109. + A3 * fsp->sy_k[3]
  110. + A4 * fsp->sy_k[4]);
  111. fsp->sy_value = fsp->sy_val[0]
  112. + (B0 * fsp->sy_k[0]
  113. + B2 * fsp->sy_k[2]
  114. + B3 * fsp->sy_k[3]
  115. + B4 * fsp->sy_k[4]
  116. + B5 * fsp->sy_k[5]);
  117. if (fsp->sy_value != 0.0)
  118. fsp->sy_sserr = fabs(1.0 - fsp->sy_predi /
  119. fsp->sy_value);
  120. fsp->sy_aberr = fabs(fsp->sy_value - fsp->sy_predi);
  121. }
  122. if (!conflag && T_LT_TSTOP)
  123. {
  124. maxerr();
  125. if (hierror())
  126. {
  127. tstep *= HALF;
  128. for (fsp = symtab; fsp != NULL; fsp = fsp->sy_link)
  129. fsp->sy_value = fsp->sy_val[0];
  130. gdval = false;
  131. continue;
  132. }
  133. else if (lowerror() && prevstep != tstep)
  134. {
  135. prevstep = tstep; /* prevent infinite loops */
  136. tstep *= 2.0;
  137. for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
  138. fsp->sy_value = fsp->sy_val[0];
  139. gdval = false;
  140. continue;
  141. }
  142. }
  143. gdval = true;
  144. ++it;
  145. t += tstep; /* the roundoff error is gross */
  146. }
  147. /* predictor - corrector */
  148. while (T_LT_TSTOP )
  149. {
  150. /* Adams-Bashforth predictor */
  151. if (tstep*(t+tstep-tstop) > 0)
  152. {
  153. startit = it;
  154. goto top;
  155. }
  156. for (fsp = dqueue; fsp != NULL ; fsp = fsp->sy_link)
  157. {
  158. fsp->sy_predi = fsp->sy_value
  159. = fsp->sy_val[0] + tstep/24.0 *
  160. (55 * fsp->sy_pri[0]
  161. -59 * fsp->sy_pri[1]
  162. +37 * fsp->sy_pri[2]
  163. -9 * fsp->sy_pri[3]);
  164. }
  165. ++it;
  166. symtab->sy_value = t += tstep;
  167. /* the roundoff error is gross */
  168. field();
  169. /* Adams-Moulton corrector */
  170. for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
  171. {
  172. fsp->sy_value = fsp->sy_val[0] + tstep/24.0 *
  173. (9 * fsp->sy_prime
  174. +19 * fsp->sy_pri[0]
  175. -5 * fsp->sy_pri[1]
  176. + fsp->sy_pri[2]);
  177. if (fsp->sy_value != 0.0)
  178. fsp->sy_sserr = ECONST *
  179. fabs(1.0 - fsp->sy_predi / fsp->sy_value);
  180. fsp->sy_aberr = ECONST *
  181. fabs (fsp->sy_value - fsp->sy_predi);
  182. fsp->sy_value += ECONST * (fsp->sy_predi - fsp->sy_value);
  183. }
  184. if (!conflag)
  185. {
  186. maxerr();
  187. if (hierror())
  188. {
  189. tstep *= HALF;
  190. t = symtab->sy_val[0];
  191. for (fsp = symtab; fsp != NULL; fsp = fsp->sy_link)
  192. {
  193. fsp->sy_value = fsp->sy_val[0];
  194. fsp->sy_prime = fsp->sy_pri[0];
  195. }
  196. startit = --it;
  197. gdval = false;
  198. goto top;
  199. }
  200. else if (lowerror())
  201. {
  202. tstep *= TWO;
  203. t = symtab->sy_val[0];
  204. for (fsp = symtab; fsp != NULL; fsp = fsp->sy_link)
  205. {
  206. fsp->sy_value = fsp->sy_val[0];
  207. fsp->sy_prime = fsp->sy_pri[0];
  208. }
  209. startit = --it;
  210. gdval = false;
  211. goto top;
  212. }
  213. }
  214. field();
  215. /* cycle indices */
  216. for (fsp = symtab; fsp != NULL; fsp = fsp->sy_link)
  217. {
  218. int j;
  219. for (j = PASTVAL; j > 0; j--)
  220. {
  221. fsp->sy_val[j] = fsp->sy_val[j-1];
  222. fsp->sy_pri[j] = fsp->sy_pri[j-1];
  223. }
  224. fsp->sy_val[0] = fsp->sy_value;
  225. fsp->sy_pri[0] = fsp->sy_prime;
  226. }
  227. /* output */
  228. printq();
  229. }
  230. }