3
|
1 // ODE.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #include <iostream.h> |
|
29 #include "ODE.h" |
|
30 #include "f77-uscore.h" |
|
31 |
|
32 extern "C" |
|
33 { |
|
34 int F77_FCN (lsode) (int (*)(), int *, double *, double *, double *, |
|
35 int *, double *, double *, int *, int *, int *, |
|
36 double *, int *, int *, int *, int (*)(), int *); |
|
37 } |
|
38 |
|
39 static ColumnVector (*user_fun) (ColumnVector&, double); |
|
40 static Matrix (*user_jac) (ColumnVector&, double); |
|
41 static ColumnVector *tmp_x; |
|
42 |
|
43 ODE::ODE (void) |
|
44 { |
|
45 n = 0; |
|
46 t = 0.0; |
|
47 |
|
48 absolute_tolerance = 1.0e-6; |
|
49 relative_tolerance = 1.0e-6; |
|
50 |
|
51 stop_time_set = 0; |
|
52 stop_time = 0.0; |
|
53 |
|
54 restart = 1; |
|
55 |
|
56 istate = 1; |
|
57 itol = 1; |
|
58 itask = 1; |
|
59 iopt = 0; |
|
60 |
|
61 liw = 20 + n; |
|
62 lrw = 22 + n * (9 + n); |
|
63 |
|
64 iwork = new int [liw]; |
|
65 rwork = new double [lrw]; |
|
66 for (int i = 4; i < 9; i++) |
|
67 { |
|
68 iwork[i] = 0; |
|
69 rwork[i] = 0.0; |
|
70 } |
|
71 |
|
72 fun = NULL; |
|
73 jac = NULL; |
|
74 } |
|
75 |
|
76 ODE::ODE (int size) |
|
77 { |
|
78 n = size; |
|
79 t = 0.0; |
|
80 |
|
81 absolute_tolerance = 1.0e-6; |
|
82 relative_tolerance = 1.0e-6; |
|
83 |
|
84 stop_time_set = 0; |
|
85 stop_time = 0.0; |
|
86 |
|
87 restart = 1; |
|
88 |
|
89 istate = 1; |
|
90 itol = 1; |
|
91 itask = 1; |
|
92 iopt = 0; |
|
93 |
|
94 liw = 20 + n; |
|
95 lrw = 22 + n * (9 + n); |
|
96 |
|
97 iwork = new int [liw]; |
|
98 rwork = new double [lrw]; |
|
99 for (int i = 4; i < 9; i++) |
|
100 { |
|
101 iwork[i] = 0; |
|
102 rwork[i] = 0.0; |
|
103 } |
|
104 |
|
105 fun = NULL; |
|
106 jac = NULL; |
|
107 } |
|
108 |
|
109 ODE::ODE (const ColumnVector& state, double time, const ODEFunc& f) |
|
110 { |
|
111 n = state.capacity (); |
|
112 t = time; |
|
113 x = state; |
|
114 |
|
115 absolute_tolerance = 1.0e-6; |
|
116 relative_tolerance = 1.0e-6; |
|
117 |
|
118 stop_time_set = 0; |
|
119 stop_time = 0.0; |
|
120 |
|
121 restart = 1; |
|
122 |
|
123 istate = 1; |
|
124 itol = 1; |
|
125 itask = 1; |
|
126 iopt = 0; |
|
127 |
|
128 liw = 20 + n; |
|
129 lrw = 22 + n * (9 + n); |
|
130 |
|
131 iwork = new int [liw]; |
|
132 rwork = new double [lrw]; |
|
133 for (int i = 4; i < 9; i++) |
|
134 { |
|
135 iwork[i] = 0; |
|
136 rwork[i] = 0.0; |
|
137 } |
|
138 |
|
139 fun = f.function (); |
|
140 jac = f.jacobian_function (); |
|
141 } |
|
142 |
|
143 ODE::~ODE (void) |
|
144 { |
|
145 delete [] rwork; |
|
146 delete [] iwork; |
|
147 } |
|
148 |
|
149 int |
|
150 lsode_f (int *neq, double *time, double *state, double *deriv) |
|
151 { |
|
152 int nn = *neq; |
|
153 ColumnVector tmp_deriv (nn); |
|
154 |
|
155 /* |
|
156 * NOTE: this won't work if LSODE passes copies of the state vector. |
|
157 * In that case we have to create a temporary vector object |
|
158 * and copy. |
|
159 */ |
|
160 tmp_deriv = (*user_fun) (*tmp_x, *time); |
|
161 |
|
162 for (int i = 0; i < nn; i++) |
|
163 deriv [i] = tmp_deriv.elem (i); |
|
164 |
|
165 return 0; |
|
166 } |
|
167 |
|
168 int |
|
169 lsode_j (int *neq, double *time, double *state, int *ml, int *mu, |
|
170 double *pd, int *nrowpd) |
|
171 { |
|
172 int nn = *neq; |
|
173 Matrix tmp_jac (nn, nn); |
|
174 |
|
175 /* |
|
176 * NOTE: this won't work if LSODE passes copies of the state vector. |
|
177 * In that case we have to create a temporary vector object |
|
178 * and copy. |
|
179 */ |
|
180 tmp_jac = (*user_jac) (*tmp_x, *time); |
|
181 |
|
182 for (int j = 0; j < nn; j++) |
|
183 for (int i = 0; i < nn; i++) |
|
184 pd [*nrowpd * j + i] = tmp_jac (i, j); |
|
185 |
|
186 return 0; |
|
187 } |
|
188 |
|
189 ColumnVector |
|
190 ODE::integrate (double tout) |
|
191 { |
|
192 if (jac == NULL) |
|
193 method_flag = 22; |
|
194 else |
|
195 method_flag = 21; |
|
196 |
|
197 double *xp = x.fortran_vec (); |
|
198 |
|
199 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
200 // In that case we have to create a temporary vector object |
|
201 // and copy. |
|
202 |
|
203 tmp_x = &x; |
|
204 user_fun = fun; |
|
205 user_jac = jac; |
|
206 |
|
207 // Try 5000 steps before giving up. |
|
208 |
|
209 iwork[5] = 5000; |
|
210 int working_too_hard = 0; |
|
211 |
|
212 if (stop_time_set) |
|
213 { |
|
214 iopt = 1; |
|
215 itask = 4; |
|
216 rwork [0] = stop_time; |
|
217 } |
|
218 else |
|
219 { |
|
220 iopt = 0; |
|
221 itask = 1; |
|
222 } |
|
223 |
|
224 if (restart) |
|
225 { |
|
226 restart = 0; |
|
227 istate = 1; |
|
228 } |
|
229 |
|
230 again: |
|
231 |
|
232 (void) F77_FCN (lsode) (lsode_f, &n, xp, &t, &tout, &itol, |
|
233 &relative_tolerance, &absolute_tolerance, |
|
234 &itask, &istate, &iopt, rwork, &lrw, iwork, |
|
235 &liw, lsode_j, &method_flag); |
|
236 |
|
237 switch (istate) |
|
238 { |
|
239 case -6: // error weight became zero during problem. (solution |
|
240 // component i vanished, and atol or atol(i) = 0.) |
|
241 break; |
|
242 case -5: // repeated convergence failures (perhaps bad jacobian |
|
243 // supplied or wrong choice of mf or tolerances). |
|
244 break; |
|
245 case -4: // repeated error test failures (check all inputs). |
|
246 break; |
|
247 case -3: // illegal input detected (see printed message). |
|
248 break; |
|
249 case -2: // excess accuracy requested (tolerances too small). |
|
250 break; |
|
251 case -1: // excess work done on this call (perhaps wrong mf). |
|
252 working_too_hard++; |
|
253 if (working_too_hard > 20) |
|
254 { |
|
255 cerr << "Shut 'er down Slim! She's a suckin' mud!\n"; |
|
256 exit (1); |
|
257 } |
|
258 else |
|
259 { |
|
260 istate = 2; |
|
261 goto again; |
|
262 } |
|
263 break; |
|
264 case 2: // lsode was successful |
|
265 break; |
|
266 default: |
|
267 // Error? |
|
268 break; |
|
269 } |
|
270 |
|
271 t = tout; |
|
272 |
|
273 return x; |
|
274 } |
|
275 |
|
276 void |
|
277 ODE::integrate (int nsteps, double tstep, ostream& s) |
|
278 { |
|
279 int time_to_quit = 0; |
|
280 double tout = t; |
|
281 |
|
282 s << t << " " << x << "\n"; |
|
283 |
|
284 for (int i = 0; i < nsteps; i++) |
|
285 { |
|
286 tout += tstep; |
|
287 if (stop_time_set && tout > stop_time) |
|
288 { |
|
289 tout = stop_time; |
|
290 time_to_quit = 1; |
|
291 } |
|
292 |
|
293 x = integrate (tout); |
|
294 |
|
295 s << t << " " << x << "\n"; |
|
296 |
|
297 if (time_to_quit) |
|
298 return; |
|
299 } |
|
300 } |
|
301 |
|
302 Matrix |
|
303 ODE::integrate (const ColumnVector& tout) |
|
304 { |
|
305 Matrix retval; |
|
306 int n_out = tout.capacity (); |
|
307 |
|
308 if (n_out > 0 && n > 0) |
|
309 { |
|
310 retval.resize (n_out, n); |
|
311 |
|
312 for (int i = 0; i < n; i++) |
|
313 retval.elem (0, i) = x.elem (i); |
|
314 |
|
315 for (int j = 1; j < n_out; j++) |
|
316 { |
|
317 ColumnVector x_next = integrate (tout.elem (j)); |
|
318 for (i = 0; i < n; i++) |
|
319 retval.elem (j, i) = x_next.elem (i); |
|
320 } |
|
321 } |
|
322 |
|
323 return retval; |
|
324 } |
|
325 |
|
326 Matrix |
|
327 ODE::integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
|
328 { |
|
329 Matrix retval; |
|
330 int n_out = tout.capacity (); |
|
331 |
|
332 if (n_out > 0 && n > 0) |
|
333 { |
|
334 retval.resize (n_out, n); |
|
335 |
|
336 for (int i = 0; i < n; i++) |
|
337 retval.elem (0, i) = x.elem (i); |
|
338 |
|
339 int n_crit = tcrit.capacity (); |
|
340 |
|
341 if (n_crit > 0) |
|
342 { |
|
343 int i_crit = 0; |
|
344 int i_out = 1; |
|
345 double next_crit = tcrit.elem (0); |
|
346 double next_out; |
|
347 while (i_out < n_out) |
|
348 { |
|
349 int do_restart = 0; |
|
350 |
|
351 next_out = tout.elem (i_out); |
|
352 if (i_crit < n_crit) |
|
353 next_crit = tcrit.elem (i_crit); |
|
354 |
|
355 int save_output; |
|
356 double t_out; |
|
357 |
|
358 if (next_crit == next_out) |
|
359 { |
|
360 set_stop_time (next_crit); |
|
361 t_out = next_out; |
|
362 save_output = 1; |
|
363 i_out++; |
|
364 i_crit++; |
|
365 do_restart = 1; |
|
366 } |
|
367 else if (next_crit < next_out) |
|
368 { |
|
369 if (i_crit < n_crit) |
|
370 { |
|
371 set_stop_time (next_crit); |
|
372 t_out = next_crit; |
|
373 save_output = 0; |
|
374 i_crit++; |
|
375 do_restart = 1; |
|
376 } |
|
377 else |
|
378 { |
|
379 clear_stop_time (); |
|
380 t_out = next_out; |
|
381 save_output = 1; |
|
382 i_out++; |
|
383 } |
|
384 } |
|
385 else |
|
386 { |
|
387 set_stop_time (next_crit); |
|
388 t_out = next_out; |
|
389 save_output = 1; |
|
390 i_out++; |
|
391 } |
|
392 |
|
393 ColumnVector x_next = integrate (t_out); |
|
394 |
|
395 if (save_output) |
|
396 { |
|
397 for (i = 0; i < n; i++) |
|
398 retval.elem (i_out-1, i) = x_next.elem (i); |
|
399 } |
|
400 |
|
401 if (do_restart) |
|
402 force_restart (); |
|
403 } |
|
404 } |
|
405 else |
|
406 retval = integrate (tout); |
|
407 } |
|
408 |
|
409 return retval; |
|
410 } |
|
411 |
|
412 int |
|
413 ODE::size (void) const |
|
414 { |
|
415 return n; |
|
416 } |
|
417 |
|
418 ColumnVector |
|
419 ODE::state (void) const |
|
420 { |
|
421 return x; |
|
422 } |
|
423 |
|
424 double ODE::time (void) const |
|
425 { |
|
426 return t; |
|
427 } |
|
428 |
|
429 void |
|
430 ODE::force_restart (void) |
|
431 { |
|
432 restart = 1; |
|
433 } |
|
434 |
|
435 void |
|
436 ODE::initialize (const ColumnVector& state, double time) |
|
437 { |
|
438 restart = 1; |
|
439 x = state; |
|
440 t = time; |
|
441 } |
|
442 |
|
443 void |
|
444 ODE::set_stop_time (double time) |
|
445 { |
|
446 stop_time_set = 1; |
|
447 stop_time = time; |
|
448 } |
|
449 |
|
450 void |
|
451 ODE::clear_stop_time (void) |
|
452 { |
|
453 stop_time_set = 0; |
|
454 } |
|
455 |
|
456 /* |
|
457 ;;; Local Variables: *** |
|
458 ;;; mode: C++ *** |
|
459 ;;; page-delimiter: "^/\\*" *** |
|
460 ;;; End: *** |
|
461 */ |