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