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