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