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