3
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
3
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
3
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
24 #pragma implementation |
|
25 #endif |
|
26 |
238
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
3
|
29 #endif |
|
30 |
1367
|
31 #include <cfloat> |
|
32 #include <cmath> |
|
33 |
1842
|
34 #include "LSODE.h" |
1847
|
35 #include "f77-fcn.h" |
227
|
36 #include "lo-error.h" |
4051
|
37 #include "lo-sstream.h" |
4180
|
38 #include "quit.h" |
3
|
39 |
3507
|
40 typedef int (*lsode_fcn_ptr) (const int&, const double&, double*, |
|
41 double*, int&); |
|
42 |
|
43 typedef int (*lsode_jac_ptr) (const int&, const double&, double*, |
|
44 const int&, const int&, double*, const |
|
45 int&); |
|
46 |
3
|
47 extern "C" |
4552
|
48 { |
|
49 F77_RET_T |
|
50 F77_FUNC (lsode, LSODE) (lsode_fcn_ptr, int&, double*, double&, |
|
51 double&, int&, double&, const double*, int&, |
|
52 int&, int&, double*, int&, int*, int&, |
|
53 lsode_jac_ptr, int&); |
|
54 } |
3
|
55 |
532
|
56 static ODEFunc::ODERHSFunc user_fun; |
|
57 static ODEFunc::ODEJacFunc user_jac; |
3
|
58 static ColumnVector *tmp_x; |
|
59 |
4049
|
60 static int |
1482
|
61 lsode_f (const int& neq, const double& time, double *, |
1251
|
62 double *deriv, int& ierr) |
3
|
63 { |
4180
|
64 BEGIN_INTERRUPT_WITH_EXCEPTIONS; |
|
65 |
2343
|
66 ColumnVector tmp_deriv; |
3
|
67 |
1360
|
68 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
69 // In that case we have to create a temporary vector object |
|
70 // and copy. |
|
71 |
1251
|
72 tmp_deriv = (*user_fun) (*tmp_x, time); |
3
|
73 |
258
|
74 if (tmp_deriv.length () == 0) |
1251
|
75 ierr = -1; |
258
|
76 else |
|
77 { |
1251
|
78 for (int i = 0; i < neq; i++) |
258
|
79 deriv [i] = tmp_deriv.elem (i); |
|
80 } |
3
|
81 |
4180
|
82 END_INTERRUPT_WITH_EXCEPTIONS; |
|
83 |
3
|
84 return 0; |
|
85 } |
|
86 |
4049
|
87 static int |
1482
|
88 lsode_j (const int& neq, const double& time, double *, |
|
89 const int&, const int&, double *pd, const int& nrowpd) |
3
|
90 { |
4180
|
91 BEGIN_INTERRUPT_WITH_EXCEPTIONS; |
|
92 |
1251
|
93 Matrix tmp_jac (neq, neq); |
3
|
94 |
1360
|
95 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
96 // In that case we have to create a temporary vector object |
|
97 // and copy. |
|
98 |
1251
|
99 tmp_jac = (*user_jac) (*tmp_x, time); |
3
|
100 |
1251
|
101 for (int j = 0; j < neq; j++) |
|
102 for (int i = 0; i < neq; i++) |
|
103 pd [nrowpd * j + i] = tmp_jac (i, j); |
3
|
104 |
4180
|
105 END_INTERRUPT_WITH_EXCEPTIONS; |
|
106 |
3
|
107 return 0; |
|
108 } |
|
109 |
|
110 ColumnVector |
1842
|
111 LSODE::do_integrate (double tout) |
3
|
112 { |
1945
|
113 ColumnVector retval; |
|
114 |
4049
|
115 static int nn = 0; |
|
116 |
|
117 if (! initialized || restart || ODEFunc::reset || LSODE_options::reset) |
1945
|
118 { |
4049
|
119 integration_error = false; |
1945
|
120 |
4049
|
121 initialized = true; |
|
122 |
|
123 istate = 1; |
|
124 |
|
125 int n = size (); |
|
126 |
|
127 nn = n; |
3955
|
128 |
4231
|
129 int max_maxord = 0; |
|
130 |
4049
|
131 if (integration_method () == "stiff") |
|
132 { |
4231
|
133 max_maxord = 5; |
|
134 |
4049
|
135 if (jac) |
|
136 method_flag = 21; |
|
137 else |
|
138 method_flag = 22; |
3955
|
139 |
4049
|
140 liw = 20 + n; |
|
141 lrw = 22 + n * (9 + n); |
|
142 } |
|
143 else |
|
144 { |
4231
|
145 max_maxord = 12; |
|
146 |
4049
|
147 method_flag = 10; |
3955
|
148 |
4049
|
149 liw = 20; |
|
150 lrw = 22 + 16 * n; |
|
151 } |
|
152 |
4231
|
153 maxord = maximum_order (); |
|
154 |
|
155 if (maxord >= 0) |
|
156 { |
|
157 if (maxord > 0 && maxord <= max_maxord) |
|
158 { |
|
159 iwork(4) = maxord; |
|
160 iopt = 1; |
|
161 } |
|
162 else |
|
163 { |
|
164 (*current_liboctave_error_handler) |
|
165 ("lsode: invalid value for maximum order"); |
|
166 integration_error = true; |
|
167 return retval; |
|
168 } |
|
169 } |
|
170 |
1945
|
171 iwork.resize (liw); |
|
172 |
|
173 for (int i = 4; i < 9; i++) |
4049
|
174 iwork(i) = 0; |
1945
|
175 |
|
176 rwork.resize (lrw); |
|
177 |
|
178 for (int i = 4; i < 9; i++) |
4049
|
179 rwork(i) = 0; |
1945
|
180 |
4049
|
181 if (stop_time_set) |
|
182 { |
|
183 itask = 4; |
|
184 rwork(0) = stop_time; |
|
185 iopt = 1; |
|
186 } |
|
187 else |
|
188 { |
|
189 itask = 1; |
|
190 } |
258
|
191 |
4049
|
192 px = x.fortran_vec (); |
3
|
193 |
4049
|
194 piwork = iwork.fortran_vec (); |
|
195 prwork = rwork.fortran_vec (); |
|
196 |
|
197 restart = false; |
|
198 |
|
199 // ODEFunc |
3
|
200 |
4049
|
201 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
202 // In that case we have to create a temporary vector object |
|
203 // and copy. |
3
|
204 |
4049
|
205 tmp_x = &x; |
|
206 |
|
207 user_fun = function (); |
|
208 user_jac = jacobian_function (); |
|
209 |
2343
|
210 ColumnVector xdot = (*user_fun) (x, t); |
|
211 |
|
212 if (x.length () != xdot.length ()) |
|
213 { |
|
214 (*current_liboctave_error_handler) |
|
215 ("lsode: inconsistent sizes for state and derivative vectors"); |
|
216 |
3995
|
217 integration_error = true; |
2343
|
218 return retval; |
|
219 } |
|
220 |
4049
|
221 ODEFunc::reset = false; |
|
222 |
|
223 // LSODE_options |
|
224 |
|
225 rel_tol = relative_tolerance (); |
|
226 abs_tol = absolute_tolerance (); |
|
227 |
|
228 int abs_tol_len = abs_tol.length (); |
|
229 |
|
230 if (abs_tol_len == 1) |
|
231 itol = 1; |
|
232 else if (abs_tol_len == n) |
|
233 itol = 2; |
|
234 else |
|
235 { |
|
236 (*current_liboctave_error_handler) |
|
237 ("lsode: inconsistent sizes for state and absolute tolerance vectors"); |
|
238 |
|
239 integration_error = true; |
|
240 return retval; |
|
241 } |
2343
|
242 |
4049
|
243 double iss = initial_step_size (); |
|
244 if (iss >= 0.0) |
|
245 { |
|
246 rwork(4) = iss; |
|
247 iopt = 1; |
|
248 } |
|
249 |
|
250 double maxss = maximum_step_size (); |
|
251 if (maxss >= 0.0) |
|
252 { |
|
253 rwork(5) = maxss; |
|
254 iopt = 1; |
|
255 } |
|
256 |
|
257 double minss = minimum_step_size (); |
|
258 if (minss >= 0.0) |
|
259 { |
|
260 rwork(6) = minss; |
|
261 iopt = 1; |
|
262 } |
|
263 |
|
264 int sl = step_limit (); |
|
265 if (sl > 0) |
|
266 { |
|
267 iwork(5) = sl; |
|
268 iopt = 1; |
|
269 } |
|
270 |
|
271 pabs_tol = abs_tol.fortran_vec (); |
|
272 |
|
273 LSODE_options::reset = false; |
3
|
274 } |
|
275 |
4049
|
276 F77_XFCN (lsode, LSODE, (lsode_f, nn, px, t, tout, itol, rel_tol, |
3952
|
277 pabs_tol, itask, istate, iopt, prwork, lrw, |
1945
|
278 piwork, liw, lsode_j, method_flag)); |
3
|
279 |
1945
|
280 if (f77_exception_encountered) |
3178
|
281 { |
3995
|
282 integration_error = true; |
3178
|
283 (*current_liboctave_error_handler) ("unrecoverable error in lsode"); |
|
284 } |
1945
|
285 else |
3
|
286 { |
1945
|
287 switch (istate) |
|
288 { |
3996
|
289 case 1: // prior to initial integration step. |
|
290 case 2: // lsode was successful. |
1945
|
291 retval = x; |
|
292 t = tout; |
|
293 break; |
|
294 |
3996
|
295 case -1: // excess work done on this call (perhaps wrong mf). |
|
296 case -2: // excess accuracy requested (tolerances too small). |
|
297 case -3: // illegal input detected (see printed message). |
|
298 case -4: // repeated error test failures (check all inputs). |
|
299 case -5: // repeated convergence failures (perhaps bad jacobian |
|
300 // supplied or wrong choice of mf or tolerances). |
|
301 case -6: // error weight became zero during problem. (solution |
|
302 // component i vanished, and atol or atol(i) = 0.) |
|
303 case -13: // return requested in user-supplied function. |
|
304 integration_error = true; |
|
305 break; |
|
306 |
|
307 default: |
|
308 integration_error = true; |
227
|
309 (*current_liboctave_error_handler) |
3996
|
310 ("unrecognized value of istate (= %d) returned from lsode", |
|
311 istate); |
1945
|
312 break; |
3
|
313 } |
|
314 } |
|
315 |
1945
|
316 return retval; |
3
|
317 } |
|
318 |
3959
|
319 std::string |
|
320 LSODE::error_message (void) const |
|
321 { |
|
322 std::string retval; |
|
323 |
4051
|
324 OSSTREAM buf; |
|
325 buf << t << OSSTREAM_ENDS; |
|
326 std::string t_curr = OSSTREAM_STR (buf); |
|
327 OSSTREAM_FREEZE (buf); |
4042
|
328 |
3959
|
329 switch (istate) |
|
330 { |
|
331 case 1: |
3996
|
332 retval = "prior to initial integration step"; |
3959
|
333 break; |
|
334 |
|
335 case 2: |
|
336 retval = "successful exit"; |
|
337 break; |
|
338 |
|
339 case 3: |
|
340 retval = "prior to continuation call with modified parameters"; |
|
341 break; |
|
342 |
3996
|
343 case -1: |
4042
|
344 retval = std::string ("excess work on this call (t = ") |
4043
|
345 + t_curr + "; perhaps wrong integration method)"; |
3996
|
346 break; |
|
347 |
|
348 case -2: |
|
349 retval = "excess accuracy requested (tolerances too small)"; |
|
350 break; |
|
351 |
|
352 case -3: |
|
353 retval = "invalid input detected (see printed message)"; |
|
354 break; |
|
355 |
|
356 case -4: |
4042
|
357 retval = std::string ("repeated error test failures (t = ") |
4043
|
358 + t_curr + "check all inputs)"; |
3996
|
359 break; |
|
360 |
|
361 case -5: |
4042
|
362 retval = std::string ("repeated convergence failures (t = ") |
4043
|
363 + t_curr |
4042
|
364 + "perhaps bad jacobian supplied or wrong choice of integration method or tolerances)"; |
3996
|
365 break; |
|
366 |
|
367 case -6: |
4042
|
368 retval = std::string ("error weight became zero during problem. (t = ") |
4043
|
369 + t_curr |
4042
|
370 + "; solution component i vanished, and atol or atol(i) == 0)"; |
3996
|
371 break; |
|
372 |
|
373 case -13: |
4042
|
374 retval = "return requested in user-supplied function (t = " |
4043
|
375 + t_curr + ")"; |
3996
|
376 break; |
|
377 |
3959
|
378 default: |
|
379 retval = "unknown error state"; |
|
380 break; |
|
381 } |
|
382 |
|
383 return retval; |
|
384 } |
|
385 |
3
|
386 Matrix |
1842
|
387 LSODE::do_integrate (const ColumnVector& tout) |
3
|
388 { |
|
389 Matrix retval; |
4049
|
390 |
3
|
391 int n_out = tout.capacity (); |
4049
|
392 int n = size (); |
3
|
393 |
|
394 if (n_out > 0 && n > 0) |
|
395 { |
|
396 retval.resize (n_out, n); |
|
397 |
|
398 for (int i = 0; i < n; i++) |
|
399 retval.elem (0, i) = x.elem (i); |
|
400 |
|
401 for (int j = 1; j < n_out; j++) |
|
402 { |
1842
|
403 ColumnVector x_next = do_integrate (tout.elem (j)); |
258
|
404 |
|
405 if (integration_error) |
|
406 return retval; |
|
407 |
1321
|
408 for (int i = 0; i < n; i++) |
3
|
409 retval.elem (j, i) = x_next.elem (i); |
|
410 } |
|
411 } |
|
412 |
|
413 return retval; |
|
414 } |
|
415 |
|
416 Matrix |
3511
|
417 LSODE::do_integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
3
|
418 { |
|
419 Matrix retval; |
4049
|
420 |
3
|
421 int n_out = tout.capacity (); |
4049
|
422 int n = size (); |
3
|
423 |
|
424 if (n_out > 0 && n > 0) |
|
425 { |
|
426 retval.resize (n_out, n); |
|
427 |
|
428 for (int i = 0; i < n; i++) |
|
429 retval.elem (0, i) = x.elem (i); |
|
430 |
|
431 int n_crit = tcrit.capacity (); |
|
432 |
|
433 if (n_crit > 0) |
|
434 { |
|
435 int i_crit = 0; |
|
436 int i_out = 1; |
|
437 double next_crit = tcrit.elem (0); |
|
438 double next_out; |
|
439 while (i_out < n_out) |
|
440 { |
3995
|
441 bool do_restart = false; |
3
|
442 |
|
443 next_out = tout.elem (i_out); |
|
444 if (i_crit < n_crit) |
|
445 next_crit = tcrit.elem (i_crit); |
|
446 |
|
447 int save_output; |
|
448 double t_out; |
|
449 |
|
450 if (next_crit == next_out) |
|
451 { |
|
452 set_stop_time (next_crit); |
|
453 t_out = next_out; |
|
454 save_output = 1; |
|
455 i_out++; |
|
456 i_crit++; |
3995
|
457 do_restart = true; |
3
|
458 } |
|
459 else if (next_crit < next_out) |
|
460 { |
|
461 if (i_crit < n_crit) |
|
462 { |
|
463 set_stop_time (next_crit); |
|
464 t_out = next_crit; |
|
465 save_output = 0; |
|
466 i_crit++; |
3995
|
467 do_restart = true; |
3
|
468 } |
|
469 else |
|
470 { |
|
471 clear_stop_time (); |
|
472 t_out = next_out; |
|
473 save_output = 1; |
|
474 i_out++; |
|
475 } |
|
476 } |
|
477 else |
|
478 { |
|
479 set_stop_time (next_crit); |
|
480 t_out = next_out; |
|
481 save_output = 1; |
|
482 i_out++; |
|
483 } |
|
484 |
1842
|
485 ColumnVector x_next = do_integrate (t_out); |
3
|
486 |
258
|
487 if (integration_error) |
|
488 return retval; |
|
489 |
3
|
490 if (save_output) |
|
491 { |
1321
|
492 for (int i = 0; i < n; i++) |
3
|
493 retval.elem (i_out-1, i) = x_next.elem (i); |
|
494 } |
|
495 |
|
496 if (do_restart) |
|
497 force_restart (); |
|
498 } |
|
499 } |
|
500 else |
258
|
501 { |
1842
|
502 retval = do_integrate (tout); |
258
|
503 |
|
504 if (integration_error) |
|
505 return retval; |
|
506 } |
3
|
507 } |
|
508 |
|
509 return retval; |
|
510 } |
|
511 |
|
512 /* |
|
513 ;;; Local Variables: *** |
|
514 ;;; mode: C++ *** |
|
515 ;;; End: *** |
|
516 */ |