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 |
1360
|
163 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
164 // In that case we have to create a temporary vector object |
|
165 // and copy. |
|
166 |
1251
|
167 tmp_deriv = (*user_fun) (*tmp_x, time); |
3
|
168 |
258
|
169 if (tmp_deriv.length () == 0) |
1251
|
170 ierr = -1; |
258
|
171 else |
|
172 { |
1251
|
173 for (int i = 0; i < neq; i++) |
258
|
174 deriv [i] = tmp_deriv.elem (i); |
|
175 } |
3
|
176 |
|
177 return 0; |
|
178 } |
|
179 |
|
180 int |
1251
|
181 lsode_j (const int& neq, const double& time, double *state, |
|
182 const int& ml, const int& mu, double *pd, const int& nrowpd) |
3
|
183 { |
1251
|
184 Matrix tmp_jac (neq, neq); |
3
|
185 |
1360
|
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 |
1251
|
190 tmp_jac = (*user_jac) (*tmp_x, time); |
3
|
191 |
1251
|
192 for (int j = 0; j < neq; j++) |
|
193 for (int i = 0; i < neq; i++) |
|
194 pd [nrowpd * j + i] = tmp_jac (i, j); |
3
|
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 |
1360
|
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. |
3
|
214 |
|
215 tmp_x = &x; |
|
216 user_fun = fun; |
|
217 user_jac = jac; |
|
218 |
1360
|
219 // Try 5000 steps before giving up. |
3
|
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 |
1253
|
249 F77_FCN (lsode, LSODE) (lsode_f, n, xp, t, tout, itol, rel_tol, |
1251
|
250 abs_tol, itask, istate, iopt, rwork, lrw, |
|
251 iwork, liw, lsode_j, method_flag); |
3
|
252 |
|
253 switch (istate) |
|
254 { |
258
|
255 case -13: // Return requested in user-supplied function. |
1360
|
256 case -6: // error weight became zero during problem. (solution |
|
257 // component i vanished, and atol or atol(i) = 0.) |
|
258 case -5: // repeated convergence failures (perhaps bad jacobian |
|
259 // supplied or wrong choice of mf or tolerances). |
|
260 case -4: // repeated error test failures (check all inputs). |
|
261 case -3: // illegal input detected (see printed message). |
|
262 case -2: // excess accuracy requested (tolerances too small). |
258
|
263 integration_error = 1; |
|
264 return ColumnVector (); |
3
|
265 break; |
1360
|
266 |
|
267 case -1: // excess work done on this call (perhaps wrong mf). |
3
|
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; |
1360
|
283 |
|
284 case 2: // lsode was successful |
3
|
285 break; |
1360
|
286 |
3
|
287 default: |
|
288 // Error? |
|
289 break; |
|
290 } |
|
291 |
|
292 t = tout; |
|
293 |
|
294 return x; |
|
295 } |
|
296 |
|
297 void |
|
298 ODE::integrate (int nsteps, double tstep, ostream& s) |
|
299 { |
|
300 int time_to_quit = 0; |
|
301 double tout = t; |
|
302 |
|
303 s << t << " " << x << "\n"; |
|
304 |
|
305 for (int i = 0; i < nsteps; i++) |
|
306 { |
|
307 tout += tstep; |
|
308 if (stop_time_set && tout > stop_time) |
|
309 { |
|
310 tout = stop_time; |
|
311 time_to_quit = 1; |
|
312 } |
|
313 |
|
314 x = integrate (tout); |
|
315 |
|
316 s << t << " " << x << "\n"; |
|
317 |
|
318 if (time_to_quit) |
|
319 return; |
|
320 } |
|
321 } |
|
322 |
|
323 Matrix |
|
324 ODE::integrate (const ColumnVector& tout) |
|
325 { |
|
326 Matrix retval; |
|
327 int n_out = tout.capacity (); |
|
328 |
|
329 if (n_out > 0 && n > 0) |
|
330 { |
|
331 retval.resize (n_out, n); |
|
332 |
|
333 for (int i = 0; i < n; i++) |
|
334 retval.elem (0, i) = x.elem (i); |
|
335 |
|
336 for (int j = 1; j < n_out; j++) |
|
337 { |
|
338 ColumnVector x_next = integrate (tout.elem (j)); |
258
|
339 |
|
340 if (integration_error) |
|
341 return retval; |
|
342 |
1321
|
343 for (int i = 0; i < n; i++) |
3
|
344 retval.elem (j, i) = x_next.elem (i); |
|
345 } |
|
346 } |
|
347 |
|
348 return retval; |
|
349 } |
|
350 |
|
351 Matrix |
|
352 ODE::integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
|
353 { |
|
354 Matrix retval; |
|
355 int n_out = tout.capacity (); |
|
356 |
|
357 if (n_out > 0 && n > 0) |
|
358 { |
|
359 retval.resize (n_out, n); |
|
360 |
|
361 for (int i = 0; i < n; i++) |
|
362 retval.elem (0, i) = x.elem (i); |
|
363 |
|
364 int n_crit = tcrit.capacity (); |
|
365 |
|
366 if (n_crit > 0) |
|
367 { |
|
368 int i_crit = 0; |
|
369 int i_out = 1; |
|
370 double next_crit = tcrit.elem (0); |
|
371 double next_out; |
|
372 while (i_out < n_out) |
|
373 { |
|
374 int do_restart = 0; |
|
375 |
|
376 next_out = tout.elem (i_out); |
|
377 if (i_crit < n_crit) |
|
378 next_crit = tcrit.elem (i_crit); |
|
379 |
|
380 int save_output; |
|
381 double t_out; |
|
382 |
|
383 if (next_crit == next_out) |
|
384 { |
|
385 set_stop_time (next_crit); |
|
386 t_out = next_out; |
|
387 save_output = 1; |
|
388 i_out++; |
|
389 i_crit++; |
|
390 do_restart = 1; |
|
391 } |
|
392 else if (next_crit < next_out) |
|
393 { |
|
394 if (i_crit < n_crit) |
|
395 { |
|
396 set_stop_time (next_crit); |
|
397 t_out = next_crit; |
|
398 save_output = 0; |
|
399 i_crit++; |
|
400 do_restart = 1; |
|
401 } |
|
402 else |
|
403 { |
|
404 clear_stop_time (); |
|
405 t_out = next_out; |
|
406 save_output = 1; |
|
407 i_out++; |
|
408 } |
|
409 } |
|
410 else |
|
411 { |
|
412 set_stop_time (next_crit); |
|
413 t_out = next_out; |
|
414 save_output = 1; |
|
415 i_out++; |
|
416 } |
|
417 |
|
418 ColumnVector x_next = integrate (t_out); |
|
419 |
258
|
420 if (integration_error) |
|
421 return retval; |
|
422 |
3
|
423 if (save_output) |
|
424 { |
1321
|
425 for (int i = 0; i < n; i++) |
3
|
426 retval.elem (i_out-1, i) = x_next.elem (i); |
|
427 } |
|
428 |
|
429 if (do_restart) |
|
430 force_restart (); |
|
431 } |
|
432 } |
|
433 else |
258
|
434 { |
|
435 retval = integrate (tout); |
|
436 |
|
437 if (integration_error) |
|
438 return retval; |
|
439 } |
3
|
440 } |
|
441 |
|
442 return retval; |
|
443 } |
|
444 |
|
445 int |
|
446 ODE::size (void) const |
|
447 { |
|
448 return n; |
|
449 } |
|
450 |
|
451 ColumnVector |
|
452 ODE::state (void) const |
|
453 { |
|
454 return x; |
|
455 } |
|
456 |
|
457 double ODE::time (void) const |
|
458 { |
|
459 return t; |
|
460 } |
|
461 |
|
462 void |
|
463 ODE::force_restart (void) |
|
464 { |
|
465 restart = 1; |
|
466 } |
|
467 |
|
468 void |
|
469 ODE::initialize (const ColumnVector& state, double time) |
|
470 { |
|
471 restart = 1; |
|
472 x = state; |
|
473 t = time; |
|
474 } |
|
475 |
|
476 void |
|
477 ODE::set_stop_time (double time) |
|
478 { |
|
479 stop_time_set = 1; |
|
480 stop_time = time; |
|
481 } |
|
482 |
|
483 void |
|
484 ODE::clear_stop_time (void) |
|
485 { |
|
486 stop_time_set = 0; |
|
487 } |
|
488 |
289
|
489 ODE_options::ODE_options (void) |
|
490 { |
|
491 init (); |
|
492 } |
|
493 |
|
494 ODE_options::ODE_options (const ODE_options& opt) |
|
495 { |
|
496 copy (opt); |
|
497 } |
|
498 |
|
499 ODE_options& |
|
500 ODE_options::operator = (const ODE_options& opt) |
|
501 { |
|
502 if (this != &opt) |
|
503 copy (opt); |
|
504 |
|
505 return *this; |
|
506 } |
|
507 |
|
508 ODE_options::~ODE_options (void) |
|
509 { |
|
510 } |
|
511 |
|
512 void |
|
513 ODE_options::init (void) |
|
514 { |
|
515 double sqrt_eps = sqrt (DBL_EPSILON); |
|
516 x_absolute_tolerance = sqrt_eps; |
|
517 x_initial_step_size = -1.0; |
|
518 x_maximum_step_size = -1.0; |
|
519 x_minimum_step_size = 0.0; |
|
520 x_relative_tolerance = sqrt_eps; |
|
521 } |
|
522 |
|
523 void |
|
524 ODE_options::copy (const ODE_options& opt) |
|
525 { |
|
526 x_absolute_tolerance = opt.x_absolute_tolerance; |
|
527 x_initial_step_size = opt.x_initial_step_size; |
|
528 x_maximum_step_size = opt.x_maximum_step_size; |
|
529 x_minimum_step_size = opt.x_minimum_step_size; |
|
530 x_relative_tolerance = opt.x_relative_tolerance; |
|
531 } |
|
532 |
|
533 void |
|
534 ODE_options::set_default_options (void) |
|
535 { |
|
536 init (); |
|
537 } |
|
538 |
|
539 void |
|
540 ODE_options::set_absolute_tolerance (double val) |
|
541 { |
|
542 x_absolute_tolerance = (val > 0.0) ? val : sqrt (DBL_EPSILON); |
|
543 } |
|
544 |
|
545 void |
|
546 ODE_options::set_initial_step_size (double val) |
|
547 { |
|
548 x_initial_step_size = (val >= 0.0) ? val : -1.0; |
|
549 } |
|
550 |
|
551 void |
|
552 ODE_options::set_maximum_step_size (double val) |
|
553 { |
|
554 x_maximum_step_size = (val >= 0.0) ? val : -1.0; |
|
555 } |
|
556 |
|
557 void |
|
558 ODE_options::set_minimum_step_size (double val) |
|
559 { |
|
560 x_minimum_step_size = (val >= 0.0) ? val : 0.0; |
|
561 } |
|
562 |
|
563 void |
|
564 ODE_options::set_relative_tolerance (double val) |
|
565 { |
|
566 x_relative_tolerance = (val > 0.0) ? val : sqrt (DBL_EPSILON); |
|
567 } |
|
568 |
|
569 double |
|
570 ODE_options::absolute_tolerance (void) |
|
571 { |
|
572 return x_absolute_tolerance; |
|
573 } |
|
574 |
|
575 double |
|
576 ODE_options::initial_step_size (void) |
|
577 { |
|
578 return x_initial_step_size; |
|
579 } |
|
580 |
|
581 double |
|
582 ODE_options::maximum_step_size (void) |
|
583 { |
|
584 return x_maximum_step_size; |
|
585 } |
|
586 |
|
587 double |
|
588 ODE_options::minimum_step_size (void) |
|
589 { |
|
590 return x_minimum_step_size; |
|
591 } |
|
592 |
|
593 double |
|
594 ODE_options::relative_tolerance (void) |
|
595 { |
|
596 return x_relative_tolerance; |
|
597 } |
|
598 |
3
|
599 /* |
|
600 ;;; Local Variables: *** |
|
601 ;;; mode: C++ *** |
|
602 ;;; page-delimiter: "^/\\*" *** |
|
603 ;;; End: *** |
|
604 */ |