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