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" |
3887
|
48 int F77_FUNC (lsode, LSODE) (lsode_fcn_ptr, int&, double*, double&, |
3952
|
49 double&, int&, double&, const 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 itask = 1; |
|
69 |
|
70 liw = 20 + n; |
|
71 lrw = 22 + n * (9 + n); |
2343
|
72 |
|
73 sanity_checked = 0; |
3
|
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 itask = 1; |
|
89 |
|
90 liw = 20 + n; |
|
91 lrw = 22 + n * (9 + n); |
2343
|
92 |
|
93 sanity_checked = 0; |
3
|
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 { |
2343
|
119 ColumnVector tmp_deriv; |
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 |
3954
|
184 int method_flag = jac ? 21 : 22; |
|
185 int iopt = 0; |
3
|
186 |
258
|
187 integration_error = 0; |
|
188 |
3
|
189 double *xp = x.fortran_vec (); |
|
190 |
1360
|
191 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
192 // In that case we have to create a temporary vector object |
|
193 // and copy. |
3
|
194 |
|
195 tmp_x = &x; |
1842
|
196 user_fun = function (); |
|
197 user_jac = jacobian_function (); |
3
|
198 |
2343
|
199 if (! sanity_checked) |
|
200 { |
|
201 ColumnVector xdot = (*user_fun) (x, t); |
|
202 |
|
203 if (x.length () != xdot.length ()) |
|
204 { |
|
205 (*current_liboctave_error_handler) |
|
206 ("lsode: inconsistent sizes for state and derivative vectors"); |
|
207 |
|
208 integration_error = 1; |
|
209 return retval; |
|
210 } |
|
211 |
|
212 sanity_checked = 1; |
|
213 } |
|
214 |
3
|
215 if (stop_time_set) |
|
216 { |
|
217 itask = 4; |
1945
|
218 rwork.elem (0) = stop_time; |
3243
|
219 iopt = 1; |
3
|
220 } |
|
221 else |
|
222 { |
|
223 itask = 1; |
|
224 } |
|
225 |
289
|
226 double rel_tol = relative_tolerance (); |
|
227 |
3952
|
228 const Array<double> abs_tol = absolute_tolerance (); |
|
229 |
|
230 int abs_tol_len = abs_tol.length (); |
|
231 |
|
232 int itol; |
|
233 |
|
234 if (abs_tol_len == 1) |
|
235 itol = 1; |
|
236 else if (abs_tol_len == n) |
3953
|
237 itol = 2; |
3952
|
238 else |
|
239 { |
|
240 (*current_liboctave_error_handler) |
|
241 ("lsode: inconsistent sizes for state and absolute tolerance vectors"); |
|
242 |
|
243 integration_error = 1; |
|
244 return retval; |
|
245 } |
|
246 |
3243
|
247 if (initial_step_size () >= 0.0) |
|
248 { |
|
249 rwork.elem (4) = initial_step_size (); |
|
250 iopt = 1; |
|
251 } |
|
252 |
|
253 if (maximum_step_size () >= 0.0) |
|
254 { |
|
255 rwork.elem (5) = maximum_step_size (); |
|
256 iopt = 1; |
|
257 } |
|
258 |
|
259 if (minimum_step_size () >= 0.0) |
|
260 { |
|
261 rwork.elem (6) = minimum_step_size (); |
|
262 iopt = 1; |
|
263 } |
289
|
264 |
2850
|
265 if (step_limit () > 0) |
3243
|
266 { |
|
267 iwork.elem (5) = step_limit (); |
|
268 iopt = 1; |
|
269 } |
2850
|
270 |
3952
|
271 const double *pabs_tol = abs_tol.fortran_vec (); |
1945
|
272 int *piwork = iwork.fortran_vec (); |
|
273 double *prwork = rwork.fortran_vec (); |
|
274 |
|
275 F77_XFCN (lsode, LSODE, (lsode_f, n, xp, t, tout, itol, rel_tol, |
3952
|
276 pabs_tol, itask, istate, iopt, prwork, lrw, |
1945
|
277 piwork, liw, lsode_j, method_flag)); |
3
|
278 |
1945
|
279 if (f77_exception_encountered) |
3178
|
280 { |
|
281 integration_error = 1; |
|
282 (*current_liboctave_error_handler) ("unrecoverable error in lsode"); |
|
283 } |
1945
|
284 else |
3
|
285 { |
1945
|
286 switch (istate) |
|
287 { |
|
288 case -13: // Return requested in user-supplied function. |
|
289 case -6: // error weight became zero during problem. (solution |
|
290 // component i vanished, and atol or atol(i) = 0.) |
|
291 case -5: // repeated convergence failures (perhaps bad jacobian |
|
292 // supplied or wrong choice of mf or tolerances). |
|
293 case -4: // repeated error test failures (check all inputs). |
|
294 case -3: // illegal input detected (see printed message). |
|
295 case -2: // excess accuracy requested (tolerances too small). |
|
296 integration_error = 1; |
|
297 break; |
1360
|
298 |
1945
|
299 case -1: // excess work done on this call (perhaps wrong mf). |
3189
|
300 (*current_liboctave_error_handler) |
|
301 ("giving up after more than %d steps attempted in lsode", |
|
302 step_limit ()); |
|
303 integration_error = 1; |
1945
|
304 break; |
|
305 |
|
306 case 2: // lsode was successful |
|
307 retval = x; |
|
308 t = tout; |
|
309 break; |
|
310 |
|
311 default: // Error? |
227
|
312 (*current_liboctave_error_handler) |
1945
|
313 ("unrecognized value of istate returned from lsode"); |
|
314 break; |
3
|
315 } |
|
316 } |
|
317 |
1945
|
318 return retval; |
3
|
319 } |
|
320 |
|
321 Matrix |
1842
|
322 LSODE::do_integrate (const ColumnVector& tout) |
3
|
323 { |
|
324 Matrix retval; |
|
325 int n_out = tout.capacity (); |
|
326 |
|
327 if (n_out > 0 && n > 0) |
|
328 { |
|
329 retval.resize (n_out, n); |
|
330 |
|
331 for (int i = 0; i < n; i++) |
|
332 retval.elem (0, i) = x.elem (i); |
|
333 |
|
334 for (int j = 1; j < n_out; j++) |
|
335 { |
1842
|
336 ColumnVector x_next = do_integrate (tout.elem (j)); |
258
|
337 |
|
338 if (integration_error) |
|
339 return retval; |
|
340 |
1321
|
341 for (int i = 0; i < n; i++) |
3
|
342 retval.elem (j, i) = x_next.elem (i); |
|
343 } |
|
344 } |
|
345 |
|
346 return retval; |
|
347 } |
|
348 |
|
349 Matrix |
3511
|
350 LSODE::do_integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
3
|
351 { |
|
352 Matrix retval; |
|
353 int n_out = tout.capacity (); |
|
354 |
|
355 if (n_out > 0 && n > 0) |
|
356 { |
|
357 retval.resize (n_out, n); |
|
358 |
|
359 for (int i = 0; i < n; i++) |
|
360 retval.elem (0, i) = x.elem (i); |
|
361 |
|
362 int n_crit = tcrit.capacity (); |
|
363 |
|
364 if (n_crit > 0) |
|
365 { |
|
366 int i_crit = 0; |
|
367 int i_out = 1; |
|
368 double next_crit = tcrit.elem (0); |
|
369 double next_out; |
|
370 while (i_out < n_out) |
|
371 { |
|
372 int do_restart = 0; |
|
373 |
|
374 next_out = tout.elem (i_out); |
|
375 if (i_crit < n_crit) |
|
376 next_crit = tcrit.elem (i_crit); |
|
377 |
|
378 int save_output; |
|
379 double t_out; |
|
380 |
|
381 if (next_crit == next_out) |
|
382 { |
|
383 set_stop_time (next_crit); |
|
384 t_out = next_out; |
|
385 save_output = 1; |
|
386 i_out++; |
|
387 i_crit++; |
|
388 do_restart = 1; |
|
389 } |
|
390 else if (next_crit < next_out) |
|
391 { |
|
392 if (i_crit < n_crit) |
|
393 { |
|
394 set_stop_time (next_crit); |
|
395 t_out = next_crit; |
|
396 save_output = 0; |
|
397 i_crit++; |
|
398 do_restart = 1; |
|
399 } |
|
400 else |
|
401 { |
|
402 clear_stop_time (); |
|
403 t_out = next_out; |
|
404 save_output = 1; |
|
405 i_out++; |
|
406 } |
|
407 } |
|
408 else |
|
409 { |
|
410 set_stop_time (next_crit); |
|
411 t_out = next_out; |
|
412 save_output = 1; |
|
413 i_out++; |
|
414 } |
|
415 |
1842
|
416 ColumnVector x_next = do_integrate (t_out); |
3
|
417 |
258
|
418 if (integration_error) |
|
419 return retval; |
|
420 |
3
|
421 if (save_output) |
|
422 { |
1321
|
423 for (int i = 0; i < n; i++) |
3
|
424 retval.elem (i_out-1, i) = x_next.elem (i); |
|
425 } |
|
426 |
|
427 if (do_restart) |
|
428 force_restart (); |
|
429 } |
|
430 } |
|
431 else |
258
|
432 { |
1842
|
433 retval = do_integrate (tout); |
258
|
434 |
|
435 if (integration_error) |
|
436 return retval; |
|
437 } |
3
|
438 } |
|
439 |
|
440 return retval; |
|
441 } |
|
442 |
|
443 /* |
|
444 ;;; Local Variables: *** |
|
445 ;;; mode: C++ *** |
|
446 ;;; End: *** |
|
447 */ |