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