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