1842
|
1 // LSODE.cc -*- C++ -*- |
3
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
3
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
3
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
238
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
3
|
30 #endif |
|
31 |
1367
|
32 #include <cfloat> |
|
33 #include <cmath> |
|
34 |
461
|
35 #include <iostream.h> |
289
|
36 |
1842
|
37 #include "LSODE.h" |
1847
|
38 #include "f77-fcn.h" |
227
|
39 #include "lo-error.h" |
3
|
40 |
|
41 extern "C" |
|
42 { |
1253
|
43 int F77_FCN (lsode, LSODE) (int (*)(const int&, const double&, |
|
44 double*, double*, int&), |
|
45 int&, double*, double&, double&, int&, |
|
46 double&, double&, int&, int&, int&, |
|
47 double*, int&, int*, int&, |
|
48 int (*)(const int&, const double&, |
|
49 double*, const int&, const int&, |
|
50 double*, const int&), |
|
51 int&); |
3
|
52 } |
|
53 |
532
|
54 static ODEFunc::ODERHSFunc user_fun; |
|
55 static ODEFunc::ODEJacFunc user_jac; |
3
|
56 static ColumnVector *tmp_x; |
|
57 |
1842
|
58 LSODE::LSODE (void) : ODE (), LSODE_options () |
3
|
59 { |
1842
|
60 n = size (); |
3
|
61 |
|
62 stop_time_set = 0; |
|
63 stop_time = 0.0; |
|
64 |
258
|
65 integration_error = 0; |
3
|
66 restart = 1; |
|
67 |
|
68 istate = 1; |
|
69 itol = 1; |
|
70 itask = 1; |
|
71 iopt = 0; |
|
72 |
|
73 liw = 20 + n; |
|
74 lrw = 22 + n * (9 + n); |
|
75 |
|
76 iwork = new int [liw]; |
|
77 rwork = new double [lrw]; |
|
78 for (int i = 4; i < 9; i++) |
|
79 { |
|
80 iwork[i] = 0; |
|
81 rwork[i] = 0.0; |
|
82 } |
|
83 } |
|
84 |
1842
|
85 LSODE::LSODE (const ColumnVector& state, double time, const ODEFunc& f) |
|
86 : ODE (state, time, f), LSODE_options () |
3
|
87 { |
1842
|
88 n = size (); |
3
|
89 |
|
90 stop_time_set = 0; |
|
91 stop_time = 0.0; |
|
92 |
258
|
93 integration_error = 0; |
3
|
94 restart = 1; |
|
95 |
|
96 istate = 1; |
|
97 itol = 1; |
|
98 itask = 1; |
|
99 iopt = 0; |
|
100 |
|
101 liw = 20 + n; |
|
102 lrw = 22 + n * (9 + n); |
|
103 |
|
104 iwork = new int [liw]; |
|
105 rwork = new double [lrw]; |
|
106 for (int i = 4; i < 9; i++) |
|
107 { |
|
108 iwork[i] = 0; |
|
109 rwork[i] = 0.0; |
|
110 } |
|
111 } |
|
112 |
1842
|
113 LSODE::~LSODE (void) |
3
|
114 { |
|
115 delete [] rwork; |
|
116 delete [] iwork; |
|
117 } |
|
118 |
1842
|
119 void |
|
120 LSODE::force_restart (void) |
|
121 { |
|
122 restart = 1; |
|
123 } |
|
124 |
|
125 void |
|
126 LSODE::set_stop_time (double time) |
|
127 { |
|
128 stop_time_set = 1; |
|
129 stop_time = time; |
|
130 } |
|
131 |
|
132 void |
|
133 LSODE::clear_stop_time (void) |
|
134 { |
|
135 stop_time_set = 0; |
|
136 } |
|
137 |
3
|
138 int |
1482
|
139 lsode_f (const int& neq, const double& time, double *, |
1251
|
140 double *deriv, int& ierr) |
3
|
141 { |
1251
|
142 ColumnVector tmp_deriv (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_deriv = (*user_fun) (*tmp_x, time); |
3
|
149 |
258
|
150 if (tmp_deriv.length () == 0) |
1251
|
151 ierr = -1; |
258
|
152 else |
|
153 { |
1251
|
154 for (int i = 0; i < neq; i++) |
258
|
155 deriv [i] = tmp_deriv.elem (i); |
|
156 } |
3
|
157 |
|
158 return 0; |
|
159 } |
|
160 |
|
161 int |
1482
|
162 lsode_j (const int& neq, const double& time, double *, |
|
163 const int&, const int&, double *pd, const int& nrowpd) |
3
|
164 { |
1251
|
165 Matrix tmp_jac (neq, neq); |
3
|
166 |
1360
|
167 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
168 // In that case we have to create a temporary vector object |
|
169 // and copy. |
|
170 |
1251
|
171 tmp_jac = (*user_jac) (*tmp_x, time); |
3
|
172 |
1251
|
173 for (int j = 0; j < neq; j++) |
|
174 for (int i = 0; i < neq; i++) |
|
175 pd [nrowpd * j + i] = tmp_jac (i, j); |
3
|
176 |
|
177 return 0; |
|
178 } |
|
179 |
|
180 ColumnVector |
1842
|
181 LSODE::do_integrate (double tout) |
3
|
182 { |
461
|
183 if (jac) |
|
184 method_flag = 21; |
|
185 else |
3
|
186 method_flag = 22; |
|
187 |
258
|
188 integration_error = 0; |
|
189 |
3
|
190 double *xp = x.fortran_vec (); |
|
191 |
1360
|
192 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
193 // In that case we have to create a temporary vector object |
|
194 // and copy. |
3
|
195 |
|
196 tmp_x = &x; |
1842
|
197 user_fun = function (); |
|
198 user_jac = jacobian_function (); |
3
|
199 |
1360
|
200 // Try 5000 steps before giving up. |
3
|
201 |
|
202 iwork[5] = 5000; |
|
203 int working_too_hard = 0; |
|
204 |
|
205 if (stop_time_set) |
|
206 { |
|
207 itask = 4; |
|
208 rwork [0] = stop_time; |
|
209 } |
|
210 else |
|
211 { |
|
212 itask = 1; |
|
213 } |
|
214 |
289
|
215 double abs_tol = absolute_tolerance (); |
|
216 double rel_tol = relative_tolerance (); |
|
217 |
|
218 rwork[4] = (initial_step_size () >= 0.0) ? initial_step_size () : 0.0; |
|
219 rwork[5] = (maximum_step_size () >= 0.0) ? maximum_step_size () : 0.0; |
|
220 rwork[6] = (minimum_step_size () >= 0.0) ? minimum_step_size () : 0.0; |
|
221 |
3
|
222 if (restart) |
|
223 { |
|
224 restart = 0; |
|
225 istate = 1; |
|
226 } |
|
227 |
|
228 again: |
|
229 |
1253
|
230 F77_FCN (lsode, LSODE) (lsode_f, n, xp, t, tout, itol, rel_tol, |
1251
|
231 abs_tol, itask, istate, iopt, rwork, lrw, |
|
232 iwork, liw, lsode_j, method_flag); |
3
|
233 |
|
234 switch (istate) |
|
235 { |
258
|
236 case -13: // Return requested in user-supplied function. |
1360
|
237 case -6: // error weight became zero during problem. (solution |
|
238 // component i vanished, and atol or atol(i) = 0.) |
|
239 case -5: // repeated convergence failures (perhaps bad jacobian |
|
240 // supplied or wrong choice of mf or tolerances). |
|
241 case -4: // repeated error test failures (check all inputs). |
|
242 case -3: // illegal input detected (see printed message). |
|
243 case -2: // excess accuracy requested (tolerances too small). |
258
|
244 integration_error = 1; |
|
245 return ColumnVector (); |
3
|
246 break; |
1360
|
247 |
|
248 case -1: // excess work done on this call (perhaps wrong mf). |
3
|
249 working_too_hard++; |
|
250 if (working_too_hard > 20) |
|
251 { |
227
|
252 (*current_liboctave_error_handler) |
|
253 ("giving up after more than %d steps attempted in lsode", |
|
254 iwork[5] * 20); |
258
|
255 integration_error = 1; |
227
|
256 return ColumnVector (); |
3
|
257 } |
|
258 else |
|
259 { |
|
260 istate = 2; |
|
261 goto again; |
|
262 } |
|
263 break; |
1360
|
264 |
|
265 case 2: // lsode was successful |
3
|
266 break; |
1360
|
267 |
3
|
268 default: |
|
269 // Error? |
|
270 break; |
|
271 } |
|
272 |
|
273 t = tout; |
|
274 |
|
275 return x; |
|
276 } |
|
277 |
1842
|
278 #if 0 |
3
|
279 void |
1842
|
280 LSODE::integrate (int nsteps, double tstep, ostream& s) |
3
|
281 { |
|
282 int time_to_quit = 0; |
|
283 double tout = t; |
|
284 |
|
285 s << t << " " << x << "\n"; |
|
286 |
|
287 for (int i = 0; i < nsteps; i++) |
|
288 { |
|
289 tout += tstep; |
|
290 if (stop_time_set && tout > stop_time) |
|
291 { |
|
292 tout = stop_time; |
|
293 time_to_quit = 1; |
|
294 } |
|
295 |
|
296 x = integrate (tout); |
|
297 |
|
298 s << t << " " << x << "\n"; |
|
299 |
|
300 if (time_to_quit) |
|
301 return; |
|
302 } |
|
303 } |
1842
|
304 #endif |
3
|
305 |
|
306 Matrix |
1842
|
307 LSODE::do_integrate (const ColumnVector& tout) |
3
|
308 { |
|
309 Matrix retval; |
|
310 int n_out = tout.capacity (); |
|
311 |
|
312 if (n_out > 0 && n > 0) |
|
313 { |
|
314 retval.resize (n_out, n); |
|
315 |
|
316 for (int i = 0; i < n; i++) |
|
317 retval.elem (0, i) = x.elem (i); |
|
318 |
|
319 for (int j = 1; j < n_out; j++) |
|
320 { |
1842
|
321 ColumnVector x_next = do_integrate (tout.elem (j)); |
258
|
322 |
|
323 if (integration_error) |
|
324 return retval; |
|
325 |
1321
|
326 for (int i = 0; i < n; i++) |
3
|
327 retval.elem (j, i) = x_next.elem (i); |
|
328 } |
|
329 } |
|
330 |
|
331 return retval; |
|
332 } |
|
333 |
|
334 Matrix |
1842
|
335 LSODE::integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
3
|
336 { |
|
337 Matrix retval; |
|
338 int n_out = tout.capacity (); |
|
339 |
|
340 if (n_out > 0 && n > 0) |
|
341 { |
|
342 retval.resize (n_out, n); |
|
343 |
|
344 for (int i = 0; i < n; i++) |
|
345 retval.elem (0, i) = x.elem (i); |
|
346 |
|
347 int n_crit = tcrit.capacity (); |
|
348 |
|
349 if (n_crit > 0) |
|
350 { |
|
351 int i_crit = 0; |
|
352 int i_out = 1; |
|
353 double next_crit = tcrit.elem (0); |
|
354 double next_out; |
|
355 while (i_out < n_out) |
|
356 { |
|
357 int do_restart = 0; |
|
358 |
|
359 next_out = tout.elem (i_out); |
|
360 if (i_crit < n_crit) |
|
361 next_crit = tcrit.elem (i_crit); |
|
362 |
|
363 int save_output; |
|
364 double t_out; |
|
365 |
|
366 if (next_crit == next_out) |
|
367 { |
|
368 set_stop_time (next_crit); |
|
369 t_out = next_out; |
|
370 save_output = 1; |
|
371 i_out++; |
|
372 i_crit++; |
|
373 do_restart = 1; |
|
374 } |
|
375 else if (next_crit < next_out) |
|
376 { |
|
377 if (i_crit < n_crit) |
|
378 { |
|
379 set_stop_time (next_crit); |
|
380 t_out = next_crit; |
|
381 save_output = 0; |
|
382 i_crit++; |
|
383 do_restart = 1; |
|
384 } |
|
385 else |
|
386 { |
|
387 clear_stop_time (); |
|
388 t_out = next_out; |
|
389 save_output = 1; |
|
390 i_out++; |
|
391 } |
|
392 } |
|
393 else |
|
394 { |
|
395 set_stop_time (next_crit); |
|
396 t_out = next_out; |
|
397 save_output = 1; |
|
398 i_out++; |
|
399 } |
|
400 |
1842
|
401 ColumnVector x_next = do_integrate (t_out); |
3
|
402 |
258
|
403 if (integration_error) |
|
404 return retval; |
|
405 |
3
|
406 if (save_output) |
|
407 { |
1321
|
408 for (int i = 0; i < n; i++) |
3
|
409 retval.elem (i_out-1, i) = x_next.elem (i); |
|
410 } |
|
411 |
|
412 if (do_restart) |
|
413 force_restart (); |
|
414 } |
|
415 } |
|
416 else |
258
|
417 { |
1842
|
418 retval = do_integrate (tout); |
258
|
419 |
|
420 if (integration_error) |
|
421 return retval; |
|
422 } |
3
|
423 } |
|
424 |
|
425 return retval; |
|
426 } |
|
427 |
|
428 /* |
|
429 ;;; Local Variables: *** |
|
430 ;;; mode: C++ *** |
|
431 ;;; page-delimiter: "^/\\*" *** |
|
432 ;;; End: *** |
|
433 */ |