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 |
4042
|
34 #include <strstream> |
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 |
4049
|
57 static int |
1482
|
58 lsode_f (const int& neq, const double& time, double *, |
1251
|
59 double *deriv, int& ierr) |
3
|
60 { |
2343
|
61 ColumnVector tmp_deriv; |
3
|
62 |
1360
|
63 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
64 // In that case we have to create a temporary vector object |
|
65 // and copy. |
|
66 |
1251
|
67 tmp_deriv = (*user_fun) (*tmp_x, time); |
3
|
68 |
258
|
69 if (tmp_deriv.length () == 0) |
1251
|
70 ierr = -1; |
258
|
71 else |
|
72 { |
1251
|
73 for (int i = 0; i < neq; i++) |
258
|
74 deriv [i] = tmp_deriv.elem (i); |
|
75 } |
3
|
76 |
|
77 return 0; |
|
78 } |
|
79 |
4049
|
80 static int |
1482
|
81 lsode_j (const int& neq, const double& time, double *, |
|
82 const int&, const int&, double *pd, const int& nrowpd) |
3
|
83 { |
1251
|
84 Matrix tmp_jac (neq, neq); |
3
|
85 |
1360
|
86 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
87 // In that case we have to create a temporary vector object |
|
88 // and copy. |
|
89 |
1251
|
90 tmp_jac = (*user_jac) (*tmp_x, time); |
3
|
91 |
1251
|
92 for (int j = 0; j < neq; j++) |
|
93 for (int i = 0; i < neq; i++) |
|
94 pd [nrowpd * j + i] = tmp_jac (i, j); |
3
|
95 |
|
96 return 0; |
|
97 } |
|
98 |
|
99 ColumnVector |
1842
|
100 LSODE::do_integrate (double tout) |
3
|
101 { |
1945
|
102 ColumnVector retval; |
|
103 |
4049
|
104 static int nn = 0; |
|
105 |
|
106 if (! initialized || restart || ODEFunc::reset || LSODE_options::reset) |
1945
|
107 { |
4049
|
108 integration_error = false; |
1945
|
109 |
4049
|
110 initialized = true; |
|
111 |
|
112 istate = 1; |
|
113 |
|
114 int n = size (); |
|
115 |
|
116 nn = n; |
3955
|
117 |
4049
|
118 if (integration_method () == "stiff") |
|
119 { |
|
120 if (jac) |
|
121 method_flag = 21; |
|
122 else |
|
123 method_flag = 22; |
3955
|
124 |
4049
|
125 liw = 20 + n; |
|
126 lrw = 22 + n * (9 + n); |
|
127 } |
|
128 else |
|
129 { |
|
130 method_flag = 10; |
3955
|
131 |
4049
|
132 liw = 20; |
|
133 lrw = 22 + 16 * n; |
|
134 } |
|
135 |
1945
|
136 iwork.resize (liw); |
|
137 |
|
138 for (int i = 4; i < 9; i++) |
4049
|
139 iwork(i) = 0; |
1945
|
140 |
|
141 rwork.resize (lrw); |
|
142 |
|
143 for (int i = 4; i < 9; i++) |
4049
|
144 rwork(i) = 0; |
1945
|
145 |
4049
|
146 if (stop_time_set) |
|
147 { |
|
148 itask = 4; |
|
149 rwork(0) = stop_time; |
|
150 iopt = 1; |
|
151 } |
|
152 else |
|
153 { |
|
154 itask = 1; |
|
155 } |
258
|
156 |
4049
|
157 px = x.fortran_vec (); |
3
|
158 |
4049
|
159 piwork = iwork.fortran_vec (); |
|
160 prwork = rwork.fortran_vec (); |
|
161 |
|
162 restart = false; |
|
163 |
|
164 // ODEFunc |
3
|
165 |
4049
|
166 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
167 // In that case we have to create a temporary vector object |
|
168 // and copy. |
3
|
169 |
4049
|
170 tmp_x = &x; |
|
171 |
|
172 user_fun = function (); |
|
173 user_jac = jacobian_function (); |
|
174 |
2343
|
175 ColumnVector xdot = (*user_fun) (x, t); |
|
176 |
|
177 if (x.length () != xdot.length ()) |
|
178 { |
|
179 (*current_liboctave_error_handler) |
|
180 ("lsode: inconsistent sizes for state and derivative vectors"); |
|
181 |
3995
|
182 integration_error = true; |
2343
|
183 return retval; |
|
184 } |
|
185 |
4049
|
186 ODEFunc::reset = false; |
|
187 |
|
188 // LSODE_options |
|
189 |
|
190 rel_tol = relative_tolerance (); |
|
191 abs_tol = absolute_tolerance (); |
|
192 |
|
193 int abs_tol_len = abs_tol.length (); |
|
194 |
|
195 if (abs_tol_len == 1) |
|
196 itol = 1; |
|
197 else if (abs_tol_len == n) |
|
198 itol = 2; |
|
199 else |
|
200 { |
|
201 (*current_liboctave_error_handler) |
|
202 ("lsode: inconsistent sizes for state and absolute tolerance vectors"); |
|
203 |
|
204 integration_error = true; |
|
205 return retval; |
|
206 } |
2343
|
207 |
4049
|
208 double iss = initial_step_size (); |
|
209 if (iss >= 0.0) |
|
210 { |
|
211 rwork(4) = iss; |
|
212 iopt = 1; |
|
213 } |
|
214 |
|
215 double maxss = maximum_step_size (); |
|
216 if (maxss >= 0.0) |
|
217 { |
|
218 rwork(5) = maxss; |
|
219 iopt = 1; |
|
220 } |
|
221 |
|
222 double minss = minimum_step_size (); |
|
223 if (minss >= 0.0) |
|
224 { |
|
225 rwork(6) = minss; |
|
226 iopt = 1; |
|
227 } |
|
228 |
|
229 int sl = step_limit (); |
|
230 if (sl > 0) |
|
231 { |
|
232 iwork(5) = sl; |
|
233 iopt = 1; |
|
234 } |
|
235 |
|
236 pabs_tol = abs_tol.fortran_vec (); |
|
237 |
|
238 LSODE_options::reset = false; |
3
|
239 } |
|
240 |
4049
|
241 F77_XFCN (lsode, LSODE, (lsode_f, nn, px, t, tout, itol, rel_tol, |
3952
|
242 pabs_tol, itask, istate, iopt, prwork, lrw, |
1945
|
243 piwork, liw, lsode_j, method_flag)); |
3
|
244 |
1945
|
245 if (f77_exception_encountered) |
3178
|
246 { |
3995
|
247 integration_error = true; |
3178
|
248 (*current_liboctave_error_handler) ("unrecoverable error in lsode"); |
|
249 } |
1945
|
250 else |
3
|
251 { |
1945
|
252 switch (istate) |
|
253 { |
3996
|
254 case 1: // prior to initial integration step. |
|
255 case 2: // lsode was successful. |
1945
|
256 retval = x; |
|
257 t = tout; |
|
258 break; |
|
259 |
3996
|
260 case -1: // excess work done on this call (perhaps wrong mf). |
|
261 case -2: // excess accuracy requested (tolerances too small). |
|
262 case -3: // illegal input detected (see printed message). |
|
263 case -4: // repeated error test failures (check all inputs). |
|
264 case -5: // repeated convergence failures (perhaps bad jacobian |
|
265 // supplied or wrong choice of mf or tolerances). |
|
266 case -6: // error weight became zero during problem. (solution |
|
267 // component i vanished, and atol or atol(i) = 0.) |
|
268 case -13: // return requested in user-supplied function. |
|
269 integration_error = true; |
|
270 break; |
|
271 |
|
272 default: |
|
273 integration_error = true; |
227
|
274 (*current_liboctave_error_handler) |
3996
|
275 ("unrecognized value of istate (= %d) returned from lsode", |
|
276 istate); |
1945
|
277 break; |
3
|
278 } |
|
279 } |
|
280 |
1945
|
281 return retval; |
3
|
282 } |
|
283 |
3959
|
284 std::string |
|
285 LSODE::error_message (void) const |
|
286 { |
|
287 std::string retval; |
|
288 |
4042
|
289 std::ostrstream buf; |
|
290 buf << t << ends; |
|
291 const char *t = buf.str (); |
4043
|
292 std::string t_curr = t; |
4042
|
293 delete [] t; |
|
294 |
3959
|
295 switch (istate) |
|
296 { |
|
297 case 1: |
3996
|
298 retval = "prior to initial integration step"; |
3959
|
299 break; |
|
300 |
|
301 case 2: |
|
302 retval = "successful exit"; |
|
303 break; |
|
304 |
|
305 case 3: |
|
306 retval = "prior to continuation call with modified parameters"; |
|
307 break; |
|
308 |
3996
|
309 case -1: |
4042
|
310 retval = std::string ("excess work on this call (t = ") |
4043
|
311 + t_curr + "; perhaps wrong integration method)"; |
3996
|
312 break; |
|
313 |
|
314 case -2: |
|
315 retval = "excess accuracy requested (tolerances too small)"; |
|
316 break; |
|
317 |
|
318 case -3: |
|
319 retval = "invalid input detected (see printed message)"; |
|
320 break; |
|
321 |
|
322 case -4: |
4042
|
323 retval = std::string ("repeated error test failures (t = ") |
4043
|
324 + t_curr + "check all inputs)"; |
3996
|
325 break; |
|
326 |
|
327 case -5: |
4042
|
328 retval = std::string ("repeated convergence failures (t = ") |
4043
|
329 + t_curr |
4042
|
330 + "perhaps bad jacobian supplied or wrong choice of integration method or tolerances)"; |
3996
|
331 break; |
|
332 |
|
333 case -6: |
4042
|
334 retval = std::string ("error weight became zero during problem. (t = ") |
4043
|
335 + t_curr |
4042
|
336 + "; solution component i vanished, and atol or atol(i) == 0)"; |
3996
|
337 break; |
|
338 |
|
339 case -13: |
4042
|
340 retval = "return requested in user-supplied function (t = " |
4043
|
341 + t_curr + ")"; |
3996
|
342 break; |
|
343 |
3959
|
344 default: |
|
345 retval = "unknown error state"; |
|
346 break; |
|
347 } |
|
348 |
|
349 return retval; |
|
350 } |
|
351 |
3
|
352 Matrix |
1842
|
353 LSODE::do_integrate (const ColumnVector& tout) |
3
|
354 { |
|
355 Matrix retval; |
4049
|
356 |
3
|
357 int n_out = tout.capacity (); |
4049
|
358 int n = size (); |
3
|
359 |
|
360 if (n_out > 0 && n > 0) |
|
361 { |
|
362 retval.resize (n_out, n); |
|
363 |
|
364 for (int i = 0; i < n; i++) |
|
365 retval.elem (0, i) = x.elem (i); |
|
366 |
|
367 for (int j = 1; j < n_out; j++) |
|
368 { |
1842
|
369 ColumnVector x_next = do_integrate (tout.elem (j)); |
258
|
370 |
|
371 if (integration_error) |
|
372 return retval; |
|
373 |
1321
|
374 for (int i = 0; i < n; i++) |
3
|
375 retval.elem (j, i) = x_next.elem (i); |
|
376 } |
|
377 } |
|
378 |
|
379 return retval; |
|
380 } |
|
381 |
|
382 Matrix |
3511
|
383 LSODE::do_integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
3
|
384 { |
|
385 Matrix retval; |
4049
|
386 |
3
|
387 int n_out = tout.capacity (); |
4049
|
388 int n = size (); |
3
|
389 |
|
390 if (n_out > 0 && n > 0) |
|
391 { |
|
392 retval.resize (n_out, n); |
|
393 |
|
394 for (int i = 0; i < n; i++) |
|
395 retval.elem (0, i) = x.elem (i); |
|
396 |
|
397 int n_crit = tcrit.capacity (); |
|
398 |
|
399 if (n_crit > 0) |
|
400 { |
|
401 int i_crit = 0; |
|
402 int i_out = 1; |
|
403 double next_crit = tcrit.elem (0); |
|
404 double next_out; |
|
405 while (i_out < n_out) |
|
406 { |
3995
|
407 bool do_restart = false; |
3
|
408 |
|
409 next_out = tout.elem (i_out); |
|
410 if (i_crit < n_crit) |
|
411 next_crit = tcrit.elem (i_crit); |
|
412 |
|
413 int save_output; |
|
414 double t_out; |
|
415 |
|
416 if (next_crit == next_out) |
|
417 { |
|
418 set_stop_time (next_crit); |
|
419 t_out = next_out; |
|
420 save_output = 1; |
|
421 i_out++; |
|
422 i_crit++; |
3995
|
423 do_restart = true; |
3
|
424 } |
|
425 else if (next_crit < next_out) |
|
426 { |
|
427 if (i_crit < n_crit) |
|
428 { |
|
429 set_stop_time (next_crit); |
|
430 t_out = next_crit; |
|
431 save_output = 0; |
|
432 i_crit++; |
3995
|
433 do_restart = true; |
3
|
434 } |
|
435 else |
|
436 { |
|
437 clear_stop_time (); |
|
438 t_out = next_out; |
|
439 save_output = 1; |
|
440 i_out++; |
|
441 } |
|
442 } |
|
443 else |
|
444 { |
|
445 set_stop_time (next_crit); |
|
446 t_out = next_out; |
|
447 save_output = 1; |
|
448 i_out++; |
|
449 } |
|
450 |
1842
|
451 ColumnVector x_next = do_integrate (t_out); |
3
|
452 |
258
|
453 if (integration_error) |
|
454 return retval; |
|
455 |
3
|
456 if (save_output) |
|
457 { |
1321
|
458 for (int i = 0; i < n; i++) |
3
|
459 retval.elem (i_out-1, i) = x_next.elem (i); |
|
460 } |
|
461 |
|
462 if (do_restart) |
|
463 force_restart (); |
|
464 } |
|
465 } |
|
466 else |
258
|
467 { |
1842
|
468 retval = do_integrate (tout); |
258
|
469 |
|
470 if (integration_error) |
|
471 return retval; |
|
472 } |
3
|
473 } |
|
474 |
|
475 return retval; |
|
476 } |
|
477 |
|
478 /* |
|
479 ;;; Local Variables: *** |
|
480 ;;; mode: C++ *** |
|
481 ;;; End: *** |
|
482 */ |