File: ama.c

package info (click to toggle)
plotutils 2.4.1-11
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 11,676 kB
  • ctags: 6,967
  • sloc: ansic: 76,305; sh: 15,172; cpp: 12,403; yacc: 2,604; makefile: 888; lex: 144
file content (235 lines) | stat: -rw-r--r-- 6,103 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "sys-defines.h"
#include "ode.h"
#include "extern.h"
#include "num.h"

#define PASTVAL        (3)	/* past values, val[0] is current value */
#define T_LT_TSTOP (tstep>0 ? t<tstop:t>tstop)
#define NEARSTOP       (tstep > 0 ? \
                        t+0.9375*tstep > tstop && t+0.0625*tstep < tstop : \
                        t+0.9375*tstep < tstop && t+0.0625*tstep > tstop)

/*
 * Adams-Moulton with adaptive step size
 * Copyright Nicholas B. Tufillaro, 1982-1994. All rights reserved.
 * GNU enhancements copyright (C) 1996-1997 Free Software Foundation, Inc.
 */

void
#ifdef _HAVE_PROTOS
ama (void)
#else
ama ()
#endif
{
  bool gdval = true;		/* good value to print ? */
  int overtime = 1;
  long startit = 0;
  double prevstep = 0.0;
  double t = tstart;

 top:
  /* fifth-order Runge-Kutta startup */
  it = startit;
  while (it <= startit + PASTVAL&&(T_LT_TSTOP || overtime--)) 
    {
      symtab->sy_value = t;
      field();
      if (gdval) 
	{
	  for (fsp = symtab; fsp != NULL; fsp = fsp->sy_link) 
	    {
	      int j;
	      
	      for (j = it - startit; j > 0; j--) 
		{
		  fsp->sy_val[j] = fsp->sy_val[j-1];
		  fsp->sy_pri[j] = fsp->sy_pri[j-1];
		}
	      fsp->sy_val[0] = fsp->sy_value;
	      fsp->sy_pri[0] = fsp->sy_prime;
	    }
	  printq();		/* output */
	  if (it == startit + PASTVAL)
	    break;		/* startup complete */
	}
      if (tstep * (t+tstep-tstop) > 0)
	tstep = tstop - t;
      for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link) 
	{
	  fsp->sy_k[0] = tstep * fsp->sy_prime;
	  fsp->sy_value = fsp->sy_val[0] + C20 * fsp->sy_k[0];
	}
      symtab->sy_value = t + C2t * tstep;
      field();
      for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link) 
	{
	  fsp->sy_k[1] = tstep * fsp->sy_prime;
	  fsp->sy_value = fsp->sy_val[0] + C30 * fsp->sy_k[0]
	    + C31 * fsp->sy_k[1];
	}
      symtab->sy_value = t + C3t * tstep;
      field();
      for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link) 
	{
	  fsp->sy_k[2] = tstep * fsp->sy_prime;
	  fsp->sy_value = fsp->sy_val[0] 
	    + (C40 * fsp->sy_k[0]
	       + C41 * fsp->sy_k[1]
	       + C42 * fsp->sy_k[2]);
	}
      symtab->sy_value = t + C4t * tstep;
      field();
      for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link) 
	{
	  fsp->sy_k[3] = tstep * fsp->sy_prime;
	  fsp->sy_value = fsp->sy_val[0] 
	    + (C50 * fsp->sy_k[0]
	       + C51 * fsp->sy_k[1]
	       + C52 * fsp->sy_k[2]
	       + C53 * fsp->sy_k[3]);
	}
      symtab->sy_value = t + tstep;
      field();
      for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link) 
	{
	  fsp->sy_k[4] = tstep * fsp->sy_prime;
	  fsp->sy_value = fsp->sy_val[0] 
	    + (C60 * fsp->sy_k[0]
	       + C61 * fsp->sy_k[1]
	       + C62 * fsp->sy_k[2]
	       + C63 * fsp->sy_k[3]
	       + C64 * fsp->sy_k[4]);
	}
      symtab->sy_value = t + C6t * tstep;
      field();
      for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
	fsp->sy_k[5] = tstep * fsp->sy_prime;
      for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link) 
	{
	  fsp->sy_predi = fsp->sy_val[0] 
	    + (A0 * fsp->sy_k[0]
	       + A2 * fsp->sy_k[2]
	       + A3 * fsp->sy_k[3]
	       + A4 * fsp->sy_k[4]);
	  fsp->sy_value = fsp->sy_val[0] 
	    + (B0 * fsp->sy_k[0]
	       + B2 * fsp->sy_k[2]
	       + B3 * fsp->sy_k[3]
	       + B4 * fsp->sy_k[4]
	       + B5 * fsp->sy_k[5]);
	  if (fsp->sy_value != 0.0)
	    fsp->sy_sserr = fabs(1.0 - fsp->sy_predi /
				 fsp->sy_value);
	  fsp->sy_aberr = fabs(fsp->sy_value - fsp->sy_predi);
	}
      if (!conflag && T_LT_TSTOP) 
	{
	  maxerr();
	  if (hierror()) 
	    { 
	      tstep *= HALF;
	      for (fsp = symtab; fsp != NULL; fsp = fsp->sy_link)
		fsp->sy_value = fsp->sy_val[0];
	      gdval = false;
	      continue;
	    }
	  else if (lowerror() && prevstep != tstep) 
	    {
	      prevstep = tstep; /* prevent infinite loops */
	      tstep *= 2.0;
	      for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
		fsp->sy_value = fsp->sy_val[0];
	      gdval = false;
	      continue;
	    }
	}
      gdval = true;
      ++it;
      t += tstep;	/* the roundoff error is gross */
    }
  /* predictor - corrector */
  while (T_LT_TSTOP ) 
    {
      /* Adams-Bashforth predictor */
      if (tstep*(t+tstep-tstop) > 0)
	{
	  startit = it;
	  goto top;
	}
      for (fsp = dqueue; fsp != NULL ; fsp = fsp->sy_link) 
	{
	  fsp->sy_predi = fsp->sy_value
	    = fsp->sy_val[0] + tstep/24.0 *
	      (55 * fsp->sy_pri[0]
	       -59 * fsp->sy_pri[1]
	       +37 * fsp->sy_pri[2]
	       -9  * fsp->sy_pri[3]);
	}
      ++it;
      symtab->sy_value = t += tstep;
      /* the roundoff error is gross */
      field();
      /* Adams-Moulton corrector */
      for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link) 
	{
	  fsp->sy_value = fsp->sy_val[0] + tstep/24.0 *
	    (9  * fsp->sy_prime
	     +19 * fsp->sy_pri[0]
	     -5  * fsp->sy_pri[1]
	     +     fsp->sy_pri[2]);
	  if (fsp->sy_value != 0.0)
	    fsp->sy_sserr = ECONST *
	      fabs(1.0 - fsp->sy_predi / fsp->sy_value);
	  fsp->sy_aberr = ECONST *
	    fabs (fsp->sy_value - fsp->sy_predi);
	  fsp->sy_value += ECONST * (fsp->sy_predi - fsp->sy_value);
	}
      if (!conflag) 
	{
	  maxerr();
	  if (hierror()) 
	    {
	      tstep *= HALF;
	      t = symtab->sy_val[0];
	      for (fsp = symtab; fsp != NULL; fsp = fsp->sy_link) 
		{
		  fsp->sy_value = fsp->sy_val[0];
		  fsp->sy_prime = fsp->sy_pri[0];
		}
	      startit = --it;
	      gdval = false;
	      goto top;
	    }
	  else if (lowerror()) 
	    {
	      tstep *= TWO;
                                        t = symtab->sy_val[0];
	      for (fsp = symtab; fsp != NULL; fsp = fsp->sy_link) 
		{
		  fsp->sy_value = fsp->sy_val[0];
		  fsp->sy_prime = fsp->sy_pri[0];
		}
	      startit = --it;
	      gdval = false;
	      goto top;
	    }
	}
      field();
      /* cycle indices */
      for (fsp = symtab; fsp != NULL; fsp = fsp->sy_link) 
	{
	  int j;

	  for (j = PASTVAL; j > 0; j--) 
	    {
	      fsp->sy_val[j] = fsp->sy_val[j-1];
	      fsp->sy_pri[j] = fsp->sy_pri[j-1];
	    }
	  fsp->sy_val[0] = fsp->sy_value;
	  fsp->sy_pri[0] = fsp->sy_prime;
	}
      /* output */
      printq();
    }
}