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