1842
|
1 // LSODE.cc -*- C++ -*- |
3
|
2 /* |
|
3 |
1882
|
4 Copyright (C) 1996 John W. Eaton |
3
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
3
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
238
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
3
|
30 #endif |
|
31 |
1367
|
32 #include <cfloat> |
|
33 #include <cmath> |
|
34 |
461
|
35 #include <iostream.h> |
289
|
36 |
1842
|
37 #include "LSODE.h" |
1847
|
38 #include "f77-fcn.h" |
227
|
39 #include "lo-error.h" |
3
|
40 |
|
41 extern "C" |
|
42 { |
1253
|
43 int F77_FCN (lsode, LSODE) (int (*)(const int&, const double&, |
|
44 double*, double*, int&), |
|
45 int&, double*, double&, double&, int&, |
|
46 double&, double&, int&, int&, int&, |
|
47 double*, int&, int*, int&, |
|
48 int (*)(const int&, const double&, |
|
49 double*, const int&, const int&, |
|
50 double*, const int&), |
|
51 int&); |
3
|
52 } |
|
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 stop_time_set = 0; |
|
63 stop_time = 0.0; |
|
64 |
258
|
65 integration_error = 0; |
3
|
66 restart = 1; |
|
67 |
|
68 istate = 1; |
|
69 itol = 1; |
|
70 itask = 1; |
|
71 iopt = 0; |
|
72 |
|
73 liw = 20 + n; |
|
74 lrw = 22 + n * (9 + n); |
|
75 } |
|
76 |
1842
|
77 LSODE::LSODE (const ColumnVector& state, double time, const ODEFunc& f) |
|
78 : ODE (state, time, f), LSODE_options () |
3
|
79 { |
1842
|
80 n = size (); |
3
|
81 |
|
82 stop_time_set = 0; |
|
83 stop_time = 0.0; |
|
84 |
258
|
85 integration_error = 0; |
3
|
86 restart = 1; |
|
87 |
|
88 istate = 1; |
|
89 itol = 1; |
|
90 itask = 1; |
|
91 iopt = 0; |
|
92 |
|
93 liw = 20 + n; |
|
94 lrw = 22 + n * (9 + n); |
|
95 } |
|
96 |
1842
|
97 void |
|
98 LSODE::force_restart (void) |
|
99 { |
|
100 restart = 1; |
|
101 } |
|
102 |
|
103 void |
|
104 LSODE::set_stop_time (double time) |
|
105 { |
|
106 stop_time_set = 1; |
|
107 stop_time = time; |
|
108 } |
|
109 |
|
110 void |
|
111 LSODE::clear_stop_time (void) |
|
112 { |
|
113 stop_time_set = 0; |
|
114 } |
|
115 |
3
|
116 int |
1482
|
117 lsode_f (const int& neq, const double& time, double *, |
1251
|
118 double *deriv, int& ierr) |
3
|
119 { |
1251
|
120 ColumnVector tmp_deriv (neq); |
3
|
121 |
1360
|
122 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
123 // In that case we have to create a temporary vector object |
|
124 // and copy. |
|
125 |
1251
|
126 tmp_deriv = (*user_fun) (*tmp_x, time); |
3
|
127 |
258
|
128 if (tmp_deriv.length () == 0) |
1251
|
129 ierr = -1; |
258
|
130 else |
|
131 { |
1251
|
132 for (int i = 0; i < neq; i++) |
258
|
133 deriv [i] = tmp_deriv.elem (i); |
|
134 } |
3
|
135 |
|
136 return 0; |
|
137 } |
|
138 |
|
139 int |
1482
|
140 lsode_j (const int& neq, const double& time, double *, |
|
141 const int&, const int&, double *pd, const int& nrowpd) |
3
|
142 { |
1251
|
143 Matrix tmp_jac (neq, neq); |
3
|
144 |
1360
|
145 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
146 // In that case we have to create a temporary vector object |
|
147 // and copy. |
|
148 |
1251
|
149 tmp_jac = (*user_jac) (*tmp_x, time); |
3
|
150 |
1251
|
151 for (int j = 0; j < neq; j++) |
|
152 for (int i = 0; i < neq; i++) |
|
153 pd [nrowpd * j + i] = tmp_jac (i, j); |
3
|
154 |
|
155 return 0; |
|
156 } |
|
157 |
|
158 ColumnVector |
1842
|
159 LSODE::do_integrate (double tout) |
3
|
160 { |
1945
|
161 ColumnVector retval; |
|
162 |
|
163 if (restart) |
|
164 { |
|
165 restart = 0; |
|
166 istate = 1; |
|
167 } |
|
168 |
|
169 if (iwork.length () != liw) |
|
170 { |
|
171 iwork.resize (liw); |
|
172 |
|
173 for (int i = 4; i < 9; i++) |
|
174 iwork.elem (i) = 0; |
|
175 } |
|
176 |
|
177 if (rwork.length () != lrw) |
|
178 { |
|
179 rwork.resize (lrw); |
|
180 |
|
181 for (int i = 4; i < 9; i++) |
|
182 rwork.elem (i) = 0; |
|
183 } |
|
184 |
461
|
185 if (jac) |
|
186 method_flag = 21; |
|
187 else |
3
|
188 method_flag = 22; |
|
189 |
258
|
190 integration_error = 0; |
|
191 |
3
|
192 double *xp = x.fortran_vec (); |
|
193 |
1360
|
194 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
195 // In that case we have to create a temporary vector object |
|
196 // and copy. |
3
|
197 |
|
198 tmp_x = &x; |
1842
|
199 user_fun = function (); |
|
200 user_jac = jacobian_function (); |
3
|
201 |
1360
|
202 // Try 5000 steps before giving up. |
3
|
203 |
1945
|
204 iwork.elem (5) = 5000; |
3
|
205 |
|
206 if (stop_time_set) |
|
207 { |
|
208 itask = 4; |
1945
|
209 rwork.elem (0) = stop_time; |
3
|
210 } |
|
211 else |
|
212 { |
|
213 itask = 1; |
|
214 } |
|
215 |
289
|
216 double abs_tol = absolute_tolerance (); |
|
217 double rel_tol = relative_tolerance (); |
|
218 |
1945
|
219 rwork.elem (4) = (initial_step_size () >= 0.0) ? initial_step_size () : 0.0; |
|
220 rwork.elem (5) = (maximum_step_size () >= 0.0) ? maximum_step_size () : 0.0; |
|
221 rwork.elem (6) = (minimum_step_size () >= 0.0) ? minimum_step_size () : 0.0; |
289
|
222 |
1945
|
223 int *piwork = iwork.fortran_vec (); |
|
224 double *prwork = rwork.fortran_vec (); |
|
225 |
|
226 working_too_hard = 0; |
3
|
227 |
|
228 again: |
|
229 |
1945
|
230 F77_XFCN (lsode, LSODE, (lsode_f, n, xp, t, tout, itol, rel_tol, |
|
231 abs_tol, itask, istate, iopt, prwork, lrw, |
|
232 piwork, liw, lsode_j, method_flag)); |
3
|
233 |
1945
|
234 if (f77_exception_encountered) |
|
235 (*current_liboctave_error_handler) ("unrecoverable error in lsode"); |
|
236 else |
3
|
237 { |
1945
|
238 switch (istate) |
|
239 { |
|
240 case -13: // Return requested in user-supplied function. |
|
241 case -6: // error weight became zero during problem. (solution |
|
242 // component i vanished, and atol or atol(i) = 0.) |
|
243 case -5: // repeated convergence failures (perhaps bad jacobian |
|
244 // supplied or wrong choice of mf or tolerances). |
|
245 case -4: // repeated error test failures (check all inputs). |
|
246 case -3: // illegal input detected (see printed message). |
|
247 case -2: // excess accuracy requested (tolerances too small). |
|
248 integration_error = 1; |
|
249 break; |
1360
|
250 |
1945
|
251 case -1: // excess work done on this call (perhaps wrong mf). |
|
252 working_too_hard++; |
|
253 if (working_too_hard > 20) |
|
254 { |
|
255 (*current_liboctave_error_handler) |
|
256 ("giving up after more than %d steps attempted in lsode", |
|
257 iwork.elem (5) * 20); |
|
258 integration_error = 1; |
|
259 } |
|
260 else |
|
261 { |
|
262 istate = 2; |
|
263 goto again; |
|
264 } |
|
265 break; |
|
266 |
|
267 case 2: // lsode was successful |
|
268 retval = x; |
|
269 t = tout; |
|
270 break; |
|
271 |
|
272 default: // Error? |
227
|
273 (*current_liboctave_error_handler) |
1945
|
274 ("unrecognized value of istate returned from lsode"); |
|
275 break; |
3
|
276 } |
|
277 } |
|
278 |
1945
|
279 return retval; |
3
|
280 } |
|
281 |
1842
|
282 #if 0 |
3
|
283 void |
1842
|
284 LSODE::integrate (int nsteps, double tstep, ostream& s) |
3
|
285 { |
|
286 int time_to_quit = 0; |
|
287 double tout = t; |
|
288 |
|
289 s << t << " " << x << "\n"; |
|
290 |
|
291 for (int i = 0; i < nsteps; i++) |
|
292 { |
|
293 tout += tstep; |
|
294 if (stop_time_set && tout > stop_time) |
|
295 { |
|
296 tout = stop_time; |
|
297 time_to_quit = 1; |
|
298 } |
|
299 |
|
300 x = integrate (tout); |
|
301 |
|
302 s << t << " " << x << "\n"; |
|
303 |
|
304 if (time_to_quit) |
|
305 return; |
|
306 } |
|
307 } |
1842
|
308 #endif |
3
|
309 |
|
310 Matrix |
1842
|
311 LSODE::do_integrate (const ColumnVector& tout) |
3
|
312 { |
|
313 Matrix retval; |
|
314 int n_out = tout.capacity (); |
|
315 |
|
316 if (n_out > 0 && n > 0) |
|
317 { |
|
318 retval.resize (n_out, n); |
|
319 |
|
320 for (int i = 0; i < n; i++) |
|
321 retval.elem (0, i) = x.elem (i); |
|
322 |
|
323 for (int j = 1; j < n_out; j++) |
|
324 { |
1842
|
325 ColumnVector x_next = do_integrate (tout.elem (j)); |
258
|
326 |
|
327 if (integration_error) |
|
328 return retval; |
|
329 |
1321
|
330 for (int i = 0; i < n; i++) |
3
|
331 retval.elem (j, i) = x_next.elem (i); |
|
332 } |
|
333 } |
|
334 |
|
335 return retval; |
|
336 } |
|
337 |
|
338 Matrix |
1842
|
339 LSODE::integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
3
|
340 { |
|
341 Matrix retval; |
|
342 int n_out = tout.capacity (); |
|
343 |
|
344 if (n_out > 0 && n > 0) |
|
345 { |
|
346 retval.resize (n_out, n); |
|
347 |
|
348 for (int i = 0; i < n; i++) |
|
349 retval.elem (0, i) = x.elem (i); |
|
350 |
|
351 int n_crit = tcrit.capacity (); |
|
352 |
|
353 if (n_crit > 0) |
|
354 { |
|
355 int i_crit = 0; |
|
356 int i_out = 1; |
|
357 double next_crit = tcrit.elem (0); |
|
358 double next_out; |
|
359 while (i_out < n_out) |
|
360 { |
|
361 int do_restart = 0; |
|
362 |
|
363 next_out = tout.elem (i_out); |
|
364 if (i_crit < n_crit) |
|
365 next_crit = tcrit.elem (i_crit); |
|
366 |
|
367 int save_output; |
|
368 double t_out; |
|
369 |
|
370 if (next_crit == next_out) |
|
371 { |
|
372 set_stop_time (next_crit); |
|
373 t_out = next_out; |
|
374 save_output = 1; |
|
375 i_out++; |
|
376 i_crit++; |
|
377 do_restart = 1; |
|
378 } |
|
379 else if (next_crit < next_out) |
|
380 { |
|
381 if (i_crit < n_crit) |
|
382 { |
|
383 set_stop_time (next_crit); |
|
384 t_out = next_crit; |
|
385 save_output = 0; |
|
386 i_crit++; |
|
387 do_restart = 1; |
|
388 } |
|
389 else |
|
390 { |
|
391 clear_stop_time (); |
|
392 t_out = next_out; |
|
393 save_output = 1; |
|
394 i_out++; |
|
395 } |
|
396 } |
|
397 else |
|
398 { |
|
399 set_stop_time (next_crit); |
|
400 t_out = next_out; |
|
401 save_output = 1; |
|
402 i_out++; |
|
403 } |
|
404 |
1842
|
405 ColumnVector x_next = do_integrate (t_out); |
3
|
406 |
258
|
407 if (integration_error) |
|
408 return retval; |
|
409 |
3
|
410 if (save_output) |
|
411 { |
1321
|
412 for (int i = 0; i < n; i++) |
3
|
413 retval.elem (i_out-1, i) = x_next.elem (i); |
|
414 } |
|
415 |
|
416 if (do_restart) |
|
417 force_restart (); |
|
418 } |
|
419 } |
|
420 else |
258
|
421 { |
1842
|
422 retval = do_integrate (tout); |
258
|
423 |
|
424 if (integration_error) |
|
425 return retval; |
|
426 } |
3
|
427 } |
|
428 |
|
429 return retval; |
|
430 } |
|
431 |
|
432 /* |
|
433 ;;; Local Variables: *** |
|
434 ;;; mode: C++ *** |
|
435 ;;; page-delimiter: "^/\\*" *** |
|
436 ;;; End: *** |
|
437 */ |