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