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 |
289
|
28 #include <math.h> |
|
29 #include <float.h> |
|
30 |
3
|
31 #include "ODE.h" |
|
32 #include "f77-uscore.h" |
227
|
33 #include "lo-error.h" |
3
|
34 |
|
35 extern "C" |
|
36 { |
258
|
37 int F77_FCN (lsode) (int (*)(int*, double*, double*, double*, int*), |
|
38 int *, double *, double *, double *, |
3
|
39 int *, double *, double *, int *, int *, int *, |
258
|
40 double *, int *, int *, int *, |
|
41 int (*)(int*, double*, double*, int*, int*, |
|
42 double*, int*), int *); |
3
|
43 } |
|
44 |
|
45 static ColumnVector (*user_fun) (ColumnVector&, double); |
|
46 static Matrix (*user_jac) (ColumnVector&, double); |
|
47 static ColumnVector *tmp_x; |
|
48 |
|
49 ODE::ODE (void) |
|
50 { |
|
51 n = 0; |
|
52 t = 0.0; |
|
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 stop_time_set = 0; |
|
86 stop_time = 0.0; |
|
87 |
258
|
88 integration_error = 0; |
3
|
89 restart = 1; |
|
90 |
|
91 istate = 1; |
|
92 itol = 1; |
|
93 itask = 1; |
|
94 iopt = 0; |
|
95 |
|
96 liw = 20 + n; |
|
97 lrw = 22 + n * (9 + n); |
|
98 |
|
99 iwork = new int [liw]; |
|
100 rwork = new double [lrw]; |
|
101 for (int i = 4; i < 9; i++) |
|
102 { |
|
103 iwork[i] = 0; |
|
104 rwork[i] = 0.0; |
|
105 } |
|
106 |
|
107 fun = NULL; |
|
108 jac = NULL; |
|
109 } |
|
110 |
|
111 ODE::ODE (const ColumnVector& state, double time, const ODEFunc& f) |
|
112 { |
|
113 n = state.capacity (); |
|
114 t = time; |
|
115 x = state; |
|
116 |
|
117 stop_time_set = 0; |
|
118 stop_time = 0.0; |
|
119 |
258
|
120 integration_error = 0; |
3
|
121 restart = 1; |
|
122 |
|
123 istate = 1; |
|
124 itol = 1; |
|
125 itask = 1; |
289
|
126 iopt = 1; |
3
|
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 |
258
|
150 lsode_f (int *neq, double *time, double *state, double *deriv, int *ierr) |
3
|
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 |
258
|
162 if (tmp_deriv.length () == 0) |
|
163 *ierr = -1; |
|
164 else |
|
165 { |
|
166 for (int i = 0; i < nn; i++) |
|
167 deriv [i] = tmp_deriv.elem (i); |
|
168 } |
3
|
169 |
|
170 return 0; |
|
171 } |
|
172 |
|
173 int |
|
174 lsode_j (int *neq, double *time, double *state, int *ml, int *mu, |
|
175 double *pd, int *nrowpd) |
|
176 { |
|
177 int nn = *neq; |
|
178 Matrix tmp_jac (nn, nn); |
|
179 |
|
180 /* |
|
181 * NOTE: this won't work if LSODE passes copies of the state vector. |
|
182 * In that case we have to create a temporary vector object |
|
183 * and copy. |
|
184 */ |
|
185 tmp_jac = (*user_jac) (*tmp_x, *time); |
|
186 |
|
187 for (int j = 0; j < nn; j++) |
|
188 for (int i = 0; i < nn; i++) |
|
189 pd [*nrowpd * j + i] = tmp_jac (i, j); |
|
190 |
|
191 return 0; |
|
192 } |
|
193 |
|
194 ColumnVector |
|
195 ODE::integrate (double tout) |
|
196 { |
|
197 if (jac == NULL) |
|
198 method_flag = 22; |
|
199 else |
|
200 method_flag = 21; |
|
201 |
258
|
202 integration_error = 0; |
|
203 |
3
|
204 double *xp = x.fortran_vec (); |
|
205 |
|
206 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
207 // In that case we have to create a temporary vector object |
|
208 // and copy. |
|
209 |
|
210 tmp_x = &x; |
|
211 user_fun = fun; |
|
212 user_jac = jac; |
|
213 |
|
214 // Try 5000 steps before giving up. |
|
215 |
|
216 iwork[5] = 5000; |
|
217 int working_too_hard = 0; |
|
218 |
|
219 if (stop_time_set) |
|
220 { |
|
221 itask = 4; |
|
222 rwork [0] = stop_time; |
|
223 } |
|
224 else |
|
225 { |
|
226 itask = 1; |
|
227 } |
|
228 |
289
|
229 double abs_tol = absolute_tolerance (); |
|
230 double rel_tol = relative_tolerance (); |
|
231 |
|
232 rwork[4] = (initial_step_size () >= 0.0) ? initial_step_size () : 0.0; |
|
233 rwork[5] = (maximum_step_size () >= 0.0) ? maximum_step_size () : 0.0; |
|
234 rwork[6] = (minimum_step_size () >= 0.0) ? minimum_step_size () : 0.0; |
|
235 |
3
|
236 if (restart) |
|
237 { |
|
238 restart = 0; |
|
239 istate = 1; |
|
240 } |
|
241 |
|
242 again: |
|
243 |
|
244 (void) F77_FCN (lsode) (lsode_f, &n, xp, &t, &tout, &itol, |
289
|
245 &rel_tol, &abs_tol, &itask, &istate, &iopt, |
|
246 rwork, &lrw, iwork, &liw, lsode_j, |
|
247 &method_flag); |
3
|
248 |
|
249 switch (istate) |
|
250 { |
258
|
251 case -13: // Return requested in user-supplied function. |
3
|
252 case -6: // error weight became zero during problem. (solution |
|
253 // component i vanished, and atol or atol(i) = 0.) |
|
254 case -5: // repeated convergence failures (perhaps bad jacobian |
|
255 // supplied or wrong choice of mf or tolerances). |
|
256 case -4: // repeated error test failures (check all inputs). |
|
257 case -3: // illegal input detected (see printed message). |
|
258 case -2: // excess accuracy requested (tolerances too small). |
258
|
259 integration_error = 1; |
|
260 return ColumnVector (); |
3
|
261 break; |
|
262 case -1: // excess work done on this call (perhaps wrong mf). |
|
263 working_too_hard++; |
|
264 if (working_too_hard > 20) |
|
265 { |
227
|
266 (*current_liboctave_error_handler) |
|
267 ("giving up after more than %d steps attempted in lsode", |
|
268 iwork[5] * 20); |
258
|
269 integration_error = 1; |
227
|
270 return ColumnVector (); |
3
|
271 } |
|
272 else |
|
273 { |
|
274 istate = 2; |
|
275 goto again; |
|
276 } |
|
277 break; |
|
278 case 2: // lsode was successful |
|
279 break; |
|
280 default: |
|
281 // Error? |
|
282 break; |
|
283 } |
|
284 |
|
285 t = tout; |
|
286 |
|
287 return x; |
|
288 } |
|
289 |
|
290 void |
|
291 ODE::integrate (int nsteps, double tstep, ostream& s) |
|
292 { |
|
293 int time_to_quit = 0; |
|
294 double tout = t; |
|
295 |
|
296 s << t << " " << x << "\n"; |
|
297 |
|
298 for (int i = 0; i < nsteps; i++) |
|
299 { |
|
300 tout += tstep; |
|
301 if (stop_time_set && tout > stop_time) |
|
302 { |
|
303 tout = stop_time; |
|
304 time_to_quit = 1; |
|
305 } |
|
306 |
|
307 x = integrate (tout); |
|
308 |
|
309 s << t << " " << x << "\n"; |
|
310 |
|
311 if (time_to_quit) |
|
312 return; |
|
313 } |
|
314 } |
|
315 |
|
316 Matrix |
|
317 ODE::integrate (const ColumnVector& tout) |
|
318 { |
|
319 Matrix retval; |
|
320 int n_out = tout.capacity (); |
|
321 |
|
322 if (n_out > 0 && n > 0) |
|
323 { |
|
324 retval.resize (n_out, n); |
|
325 |
|
326 for (int i = 0; i < n; i++) |
|
327 retval.elem (0, i) = x.elem (i); |
|
328 |
|
329 for (int j = 1; j < n_out; j++) |
|
330 { |
|
331 ColumnVector x_next = integrate (tout.elem (j)); |
258
|
332 |
|
333 if (integration_error) |
|
334 return retval; |
|
335 |
3
|
336 for (i = 0; i < n; i++) |
|
337 retval.elem (j, i) = x_next.elem (i); |
|
338 } |
|
339 } |
|
340 |
|
341 return retval; |
|
342 } |
|
343 |
|
344 Matrix |
|
345 ODE::integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
|
346 { |
|
347 Matrix retval; |
|
348 int n_out = tout.capacity (); |
|
349 |
|
350 if (n_out > 0 && n > 0) |
|
351 { |
|
352 retval.resize (n_out, n); |
|
353 |
|
354 for (int i = 0; i < n; i++) |
|
355 retval.elem (0, i) = x.elem (i); |
|
356 |
|
357 int n_crit = tcrit.capacity (); |
|
358 |
|
359 if (n_crit > 0) |
|
360 { |
|
361 int i_crit = 0; |
|
362 int i_out = 1; |
|
363 double next_crit = tcrit.elem (0); |
|
364 double next_out; |
|
365 while (i_out < n_out) |
|
366 { |
|
367 int do_restart = 0; |
|
368 |
|
369 next_out = tout.elem (i_out); |
|
370 if (i_crit < n_crit) |
|
371 next_crit = tcrit.elem (i_crit); |
|
372 |
|
373 int save_output; |
|
374 double t_out; |
|
375 |
|
376 if (next_crit == next_out) |
|
377 { |
|
378 set_stop_time (next_crit); |
|
379 t_out = next_out; |
|
380 save_output = 1; |
|
381 i_out++; |
|
382 i_crit++; |
|
383 do_restart = 1; |
|
384 } |
|
385 else if (next_crit < next_out) |
|
386 { |
|
387 if (i_crit < n_crit) |
|
388 { |
|
389 set_stop_time (next_crit); |
|
390 t_out = next_crit; |
|
391 save_output = 0; |
|
392 i_crit++; |
|
393 do_restart = 1; |
|
394 } |
|
395 else |
|
396 { |
|
397 clear_stop_time (); |
|
398 t_out = next_out; |
|
399 save_output = 1; |
|
400 i_out++; |
|
401 } |
|
402 } |
|
403 else |
|
404 { |
|
405 set_stop_time (next_crit); |
|
406 t_out = next_out; |
|
407 save_output = 1; |
|
408 i_out++; |
|
409 } |
|
410 |
|
411 ColumnVector x_next = integrate (t_out); |
|
412 |
258
|
413 if (integration_error) |
|
414 return retval; |
|
415 |
3
|
416 if (save_output) |
|
417 { |
|
418 for (i = 0; i < n; i++) |
|
419 retval.elem (i_out-1, i) = x_next.elem (i); |
|
420 } |
|
421 |
|
422 if (do_restart) |
|
423 force_restart (); |
|
424 } |
|
425 } |
|
426 else |
258
|
427 { |
|
428 retval = integrate (tout); |
|
429 |
|
430 if (integration_error) |
|
431 return retval; |
|
432 } |
3
|
433 } |
|
434 |
|
435 return retval; |
|
436 } |
|
437 |
|
438 int |
|
439 ODE::size (void) const |
|
440 { |
|
441 return n; |
|
442 } |
|
443 |
|
444 ColumnVector |
|
445 ODE::state (void) const |
|
446 { |
|
447 return x; |
|
448 } |
|
449 |
|
450 double ODE::time (void) const |
|
451 { |
|
452 return t; |
|
453 } |
|
454 |
|
455 void |
|
456 ODE::force_restart (void) |
|
457 { |
|
458 restart = 1; |
|
459 } |
|
460 |
|
461 void |
|
462 ODE::initialize (const ColumnVector& state, double time) |
|
463 { |
|
464 restart = 1; |
|
465 x = state; |
|
466 t = time; |
|
467 } |
|
468 |
|
469 void |
|
470 ODE::set_stop_time (double time) |
|
471 { |
|
472 stop_time_set = 1; |
|
473 stop_time = time; |
|
474 } |
|
475 |
|
476 void |
|
477 ODE::clear_stop_time (void) |
|
478 { |
|
479 stop_time_set = 0; |
|
480 } |
|
481 |
289
|
482 ODE_options::ODE_options (void) |
|
483 { |
|
484 init (); |
|
485 } |
|
486 |
|
487 ODE_options::ODE_options (const ODE_options& opt) |
|
488 { |
|
489 copy (opt); |
|
490 } |
|
491 |
|
492 ODE_options& |
|
493 ODE_options::operator = (const ODE_options& opt) |
|
494 { |
|
495 if (this != &opt) |
|
496 copy (opt); |
|
497 |
|
498 return *this; |
|
499 } |
|
500 |
|
501 ODE_options::~ODE_options (void) |
|
502 { |
|
503 } |
|
504 |
|
505 void |
|
506 ODE_options::init (void) |
|
507 { |
|
508 double sqrt_eps = sqrt (DBL_EPSILON); |
|
509 x_absolute_tolerance = sqrt_eps; |
|
510 x_initial_step_size = -1.0; |
|
511 x_maximum_step_size = -1.0; |
|
512 x_minimum_step_size = 0.0; |
|
513 x_relative_tolerance = sqrt_eps; |
|
514 } |
|
515 |
|
516 void |
|
517 ODE_options::copy (const ODE_options& opt) |
|
518 { |
|
519 x_absolute_tolerance = opt.x_absolute_tolerance; |
|
520 x_initial_step_size = opt.x_initial_step_size; |
|
521 x_maximum_step_size = opt.x_maximum_step_size; |
|
522 x_minimum_step_size = opt.x_minimum_step_size; |
|
523 x_relative_tolerance = opt.x_relative_tolerance; |
|
524 } |
|
525 |
|
526 void |
|
527 ODE_options::set_default_options (void) |
|
528 { |
|
529 init (); |
|
530 } |
|
531 |
|
532 void |
|
533 ODE_options::set_absolute_tolerance (double val) |
|
534 { |
|
535 x_absolute_tolerance = (val > 0.0) ? val : sqrt (DBL_EPSILON); |
|
536 } |
|
537 |
|
538 void |
|
539 ODE_options::set_initial_step_size (double val) |
|
540 { |
|
541 x_initial_step_size = (val >= 0.0) ? val : -1.0; |
|
542 } |
|
543 |
|
544 void |
|
545 ODE_options::set_maximum_step_size (double val) |
|
546 { |
|
547 x_maximum_step_size = (val >= 0.0) ? val : -1.0; |
|
548 } |
|
549 |
|
550 void |
|
551 ODE_options::set_minimum_step_size (double val) |
|
552 { |
|
553 x_minimum_step_size = (val >= 0.0) ? val : 0.0; |
|
554 } |
|
555 |
|
556 void |
|
557 ODE_options::set_relative_tolerance (double val) |
|
558 { |
|
559 x_relative_tolerance = (val > 0.0) ? val : sqrt (DBL_EPSILON); |
|
560 } |
|
561 |
|
562 double |
|
563 ODE_options::absolute_tolerance (void) |
|
564 { |
|
565 return x_absolute_tolerance; |
|
566 } |
|
567 |
|
568 double |
|
569 ODE_options::initial_step_size (void) |
|
570 { |
|
571 return x_initial_step_size; |
|
572 } |
|
573 |
|
574 double |
|
575 ODE_options::maximum_step_size (void) |
|
576 { |
|
577 return x_maximum_step_size; |
|
578 } |
|
579 |
|
580 double |
|
581 ODE_options::minimum_step_size (void) |
|
582 { |
|
583 return x_minimum_step_size; |
|
584 } |
|
585 |
|
586 double |
|
587 ODE_options::relative_tolerance (void) |
|
588 { |
|
589 return x_relative_tolerance; |
|
590 } |
|
591 |
3
|
592 /* |
|
593 ;;; Local Variables: *** |
|
594 ;;; mode: C++ *** |
|
595 ;;; page-delimiter: "^/\\*" *** |
|
596 ;;; End: *** |
|
597 */ |