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