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