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 |
|
248 F77_XFCN (lsode, LSODE, (lsode_f, n, xp, t, tout, itol, rel_tol, |
|
249 abs_tol, itask, istate, iopt, prwork, lrw, |
|
250 piwork, liw, lsode_j, method_flag)); |
3
|
251 |
1945
|
252 if (f77_exception_encountered) |
3178
|
253 { |
|
254 integration_error = 1; |
|
255 (*current_liboctave_error_handler) ("unrecoverable error in lsode"); |
|
256 } |
1945
|
257 else |
3
|
258 { |
1945
|
259 switch (istate) |
|
260 { |
|
261 case -13: // Return requested in user-supplied function. |
|
262 case -6: // error weight became zero during problem. (solution |
|
263 // component i vanished, and atol or atol(i) = 0.) |
|
264 case -5: // repeated convergence failures (perhaps bad jacobian |
|
265 // supplied or wrong choice of mf or tolerances). |
|
266 case -4: // repeated error test failures (check all inputs). |
|
267 case -3: // illegal input detected (see printed message). |
|
268 case -2: // excess accuracy requested (tolerances too small). |
|
269 integration_error = 1; |
|
270 break; |
1360
|
271 |
1945
|
272 case -1: // excess work done on this call (perhaps wrong mf). |
3189
|
273 (*current_liboctave_error_handler) |
|
274 ("giving up after more than %d steps attempted in lsode", |
|
275 step_limit ()); |
|
276 integration_error = 1; |
1945
|
277 break; |
|
278 |
|
279 case 2: // lsode was successful |
|
280 retval = x; |
|
281 t = tout; |
|
282 break; |
|
283 |
|
284 default: // Error? |
227
|
285 (*current_liboctave_error_handler) |
1945
|
286 ("unrecognized value of istate returned from lsode"); |
|
287 break; |
3
|
288 } |
|
289 } |
|
290 |
1945
|
291 return retval; |
3
|
292 } |
|
293 |
1842
|
294 #if 0 |
3
|
295 void |
1842
|
296 LSODE::integrate (int nsteps, double tstep, ostream& s) |
3
|
297 { |
|
298 int time_to_quit = 0; |
|
299 double tout = t; |
|
300 |
|
301 s << t << " " << x << "\n"; |
|
302 |
|
303 for (int i = 0; i < nsteps; i++) |
|
304 { |
|
305 tout += tstep; |
|
306 if (stop_time_set && tout > stop_time) |
|
307 { |
|
308 tout = stop_time; |
|
309 time_to_quit = 1; |
|
310 } |
|
311 |
|
312 x = integrate (tout); |
|
313 |
|
314 s << t << " " << x << "\n"; |
|
315 |
|
316 if (time_to_quit) |
|
317 return; |
|
318 } |
|
319 } |
1842
|
320 #endif |
3
|
321 |
|
322 Matrix |
1842
|
323 LSODE::do_integrate (const ColumnVector& tout) |
3
|
324 { |
|
325 Matrix retval; |
|
326 int n_out = tout.capacity (); |
|
327 |
|
328 if (n_out > 0 && n > 0) |
|
329 { |
|
330 retval.resize (n_out, n); |
|
331 |
|
332 for (int i = 0; i < n; i++) |
|
333 retval.elem (0, i) = x.elem (i); |
|
334 |
|
335 for (int j = 1; j < n_out; j++) |
|
336 { |
1842
|
337 ColumnVector x_next = do_integrate (tout.elem (j)); |
258
|
338 |
|
339 if (integration_error) |
|
340 return retval; |
|
341 |
1321
|
342 for (int i = 0; i < n; i++) |
3
|
343 retval.elem (j, i) = x_next.elem (i); |
|
344 } |
|
345 } |
|
346 |
|
347 return retval; |
|
348 } |
|
349 |
|
350 Matrix |
1842
|
351 LSODE::integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
3
|
352 { |
|
353 Matrix retval; |
|
354 int n_out = tout.capacity (); |
|
355 |
|
356 if (n_out > 0 && n > 0) |
|
357 { |
|
358 retval.resize (n_out, n); |
|
359 |
|
360 for (int i = 0; i < n; i++) |
|
361 retval.elem (0, i) = x.elem (i); |
|
362 |
|
363 int n_crit = tcrit.capacity (); |
|
364 |
|
365 if (n_crit > 0) |
|
366 { |
|
367 int i_crit = 0; |
|
368 int i_out = 1; |
|
369 double next_crit = tcrit.elem (0); |
|
370 double next_out; |
|
371 while (i_out < n_out) |
|
372 { |
|
373 int do_restart = 0; |
|
374 |
|
375 next_out = tout.elem (i_out); |
|
376 if (i_crit < n_crit) |
|
377 next_crit = tcrit.elem (i_crit); |
|
378 |
|
379 int save_output; |
|
380 double t_out; |
|
381 |
|
382 if (next_crit == next_out) |
|
383 { |
|
384 set_stop_time (next_crit); |
|
385 t_out = next_out; |
|
386 save_output = 1; |
|
387 i_out++; |
|
388 i_crit++; |
|
389 do_restart = 1; |
|
390 } |
|
391 else if (next_crit < next_out) |
|
392 { |
|
393 if (i_crit < n_crit) |
|
394 { |
|
395 set_stop_time (next_crit); |
|
396 t_out = next_crit; |
|
397 save_output = 0; |
|
398 i_crit++; |
|
399 do_restart = 1; |
|
400 } |
|
401 else |
|
402 { |
|
403 clear_stop_time (); |
|
404 t_out = next_out; |
|
405 save_output = 1; |
|
406 i_out++; |
|
407 } |
|
408 } |
|
409 else |
|
410 { |
|
411 set_stop_time (next_crit); |
|
412 t_out = next_out; |
|
413 save_output = 1; |
|
414 i_out++; |
|
415 } |
|
416 |
1842
|
417 ColumnVector x_next = do_integrate (t_out); |
3
|
418 |
258
|
419 if (integration_error) |
|
420 return retval; |
|
421 |
3
|
422 if (save_output) |
|
423 { |
1321
|
424 for (int i = 0; i < n; i++) |
3
|
425 retval.elem (i_out-1, i) = x_next.elem (i); |
|
426 } |
|
427 |
|
428 if (do_restart) |
|
429 force_restart (); |
|
430 } |
|
431 } |
|
432 else |
258
|
433 { |
1842
|
434 retval = do_integrate (tout); |
258
|
435 |
|
436 if (integration_error) |
|
437 return retval; |
|
438 } |
3
|
439 } |
|
440 |
|
441 return retval; |
|
442 } |
|
443 |
|
444 /* |
|
445 ;;; Local Variables: *** |
|
446 ;;; mode: C++ *** |
|
447 ;;; End: *** |
|
448 */ |