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