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