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