3912
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997, 2002 John W. Eaton |
|
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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include <cfloat> |
|
32 #include <cmath> |
|
33 |
|
34 #include "DASPK.h" |
|
35 #include "f77-fcn.h" |
|
36 #include "lo-error.h" |
|
37 |
|
38 typedef int (*daspk_fcn_ptr) (const double&, const double*, |
|
39 const double*, const double&, |
|
40 double*, int&, double*, int*); |
|
41 |
|
42 typedef int (*daspk_jac_ptr) (const double&, const double*, |
|
43 const double*, double*, |
|
44 const double&, double*, int*); |
|
45 |
|
46 typedef int (*daspk_psol_ptr) (const int&, const double&, |
|
47 const double*, const double*, |
|
48 const double*, const double&, |
|
49 const double*, double*, int*, |
|
50 double*, const double&, int&, |
|
51 double*, int*); |
|
52 |
|
53 extern "C" |
|
54 int F77_FUNC (ddaspk, DDASPK) (daspk_fcn_ptr, const int&, double&, |
|
55 double*, double*, double&, const int*, |
|
56 const double&, const double&, int&, |
|
57 double*, const int&, int*, const int&, |
|
58 const double*, const int*, |
|
59 daspk_jac_ptr, daspk_psol_ptr); |
|
60 |
|
61 static DAEFunc::DAERHSFunc user_fun; |
|
62 static DAEFunc::DAEJacFunc user_jac; |
|
63 static int nn; |
|
64 |
|
65 DASPK::DASPK (void) : DAE () |
|
66 { |
|
67 sanity_checked = 0; |
|
68 |
|
69 info.resize (15); |
|
70 |
|
71 for (int i = 0; i < 15; i++) |
|
72 info.elem (i) = 0; |
|
73 } |
|
74 |
|
75 DASPK::DASPK (const ColumnVector& state, double time, DAEFunc& f) |
|
76 : DAE (state, time, f) |
|
77 { |
|
78 n = size (); |
|
79 |
|
80 sanity_checked = 0; |
|
81 |
|
82 info.resize (20); |
|
83 |
|
84 for (int i = 0; i < 20; i++) |
|
85 info.elem (i) = 0; |
|
86 } |
|
87 |
|
88 DASPK::DASPK (const ColumnVector& state, const ColumnVector& deriv, |
|
89 double time, DAEFunc& f) |
|
90 : DAE (state, deriv, time, f) |
|
91 { |
|
92 n = size (); |
|
93 |
|
94 DAEFunc::set_function (f.function ()); |
|
95 DAEFunc::set_jacobian_function (f.jacobian_function ()); |
|
96 |
|
97 sanity_checked = 0; |
|
98 |
|
99 info.resize (20); |
|
100 |
|
101 for (int i = 0; i < 20; i++) |
|
102 info.elem (i) = 0; |
|
103 } |
|
104 |
|
105 int |
|
106 ddaspk_f (const double& time, const double *state, const double *deriv, |
|
107 const double&, double *delta, int& ires, double *, int *) |
|
108 { |
|
109 ColumnVector tmp_deriv (nn); |
|
110 ColumnVector tmp_state (nn); |
|
111 ColumnVector tmp_delta (nn); |
|
112 |
|
113 for (int i = 0; i < nn; i++) |
|
114 { |
|
115 tmp_deriv.elem (i) = deriv [i]; |
|
116 tmp_state.elem (i) = state [i]; |
|
117 } |
|
118 |
|
119 tmp_delta = user_fun (tmp_state, tmp_deriv, time, ires); |
|
120 |
|
121 if (ires >= 0) |
|
122 { |
|
123 if (tmp_delta.length () == 0) |
|
124 ires = -2; |
|
125 else |
|
126 { |
|
127 for (int i = 0; i < nn; i++) |
|
128 delta [i] = tmp_delta.elem (i); |
|
129 } |
|
130 } |
|
131 |
|
132 return 0; |
|
133 } |
|
134 |
|
135 //NEQ, T, Y, YPRIME, SAVR, WK, CJ, WGHT, |
|
136 //C WP, IWP, B, EPLIN, IER, RPAR, IPAR) |
|
137 |
|
138 int |
|
139 ddaspk_psol (const int& neq, const double& time, const double *state, |
|
140 const double *deriv, const double *savr, |
|
141 const double& cj, const double *wght, double *wp, |
|
142 int *iwp, double *b, const double& eplin, int& ier, |
|
143 double *, int*) |
|
144 { |
|
145 abort (); |
3946
|
146 return 0; |
3912
|
147 } |
|
148 |
3991
|
149 |
3912
|
150 int |
3991
|
151 ddaspk_j (const double& time, const double *state, const double *deriv, |
|
152 double *pd, const double& cj, double *, int *) |
3912
|
153 { |
3991
|
154 // XXX FIXME XXX -- would be nice to avoid copying the data. |
|
155 |
3912
|
156 ColumnVector tmp_state (nn); |
|
157 ColumnVector tmp_deriv (nn); |
|
158 |
3991
|
159 for (int i = 0; i < nn; i++) |
|
160 { |
|
161 tmp_deriv.elem (i) = deriv [i]; |
|
162 tmp_state.elem (i) = state [i]; |
|
163 } |
3912
|
164 |
3991
|
165 Matrix tmp_pd = user_jac (tmp_state, tmp_deriv, time, cj); |
3912
|
166 |
|
167 for (int j = 0; j < nn; j++) |
|
168 for (int i = 0; i < nn; i++) |
3991
|
169 pd [nn * j + i] = tmp_pd.elem (i, j); |
3912
|
170 |
|
171 return 0; |
|
172 } |
|
173 |
|
174 ColumnVector |
|
175 DASPK::do_integrate (double tout) |
|
176 { |
|
177 ColumnVector retval; |
|
178 |
|
179 if (restart) |
|
180 { |
3995
|
181 restart = false; |
3912
|
182 info.elem (0) = 0; |
|
183 } |
|
184 |
|
185 liw = 40 + n; |
|
186 if (info(9) == 1 || info(9) == 3) |
|
187 liw += n; |
|
188 if (info (10) == 1 || info(15) == 1) |
|
189 liw += n; |
|
190 |
|
191 lrw = 50 + 9*n; |
|
192 if (info(5) == 0) |
|
193 lrw += n*n; |
|
194 if (info(15) == 1) |
|
195 lrw += n; |
|
196 |
|
197 if (iwork.length () != liw) |
|
198 iwork.resize (liw); |
|
199 |
|
200 if (rwork.length () != lrw) |
|
201 rwork.resize (lrw); |
|
202 |
3995
|
203 integration_error = false; |
3912
|
204 |
|
205 if (DAEFunc::jacobian_function ()) |
|
206 info.elem (4) = 1; |
|
207 else |
|
208 info.elem (4) = 0; |
|
209 |
|
210 double *px = x.fortran_vec (); |
|
211 double *pxdot = xdot.fortran_vec (); |
|
212 |
|
213 nn = n; |
|
214 user_fun = DAEFunc::fun; |
|
215 user_jac = DAEFunc::jac; |
|
216 |
|
217 if (! sanity_checked) |
|
218 { |
|
219 int ires = 0; |
|
220 |
|
221 ColumnVector res = (*user_fun) (x, xdot, t, ires); |
|
222 |
|
223 if (res.length () != x.length ()) |
|
224 { |
|
225 (*current_liboctave_error_handler) |
|
226 ("daspk: inconsistent sizes for state and residual vectors"); |
|
227 |
3995
|
228 integration_error = true; |
3912
|
229 return retval; |
|
230 } |
|
231 |
|
232 sanity_checked = 1; |
|
233 } |
|
234 |
|
235 if (stop_time_set) |
|
236 { |
|
237 rwork.elem (0) = stop_time; |
|
238 info.elem (3) = 1; |
|
239 } |
|
240 else |
|
241 info.elem (3) = 0; |
|
242 |
|
243 double abs_tol = absolute_tolerance (); |
|
244 double rel_tol = relative_tolerance (); |
|
245 |
|
246 if (initial_step_size () >= 0.0) |
|
247 { |
|
248 rwork.elem (2) = initial_step_size (); |
|
249 info.elem (7) = 1; |
|
250 } |
|
251 else |
|
252 info.elem (7) = 0; |
|
253 |
|
254 if (maximum_step_size () >= 0.0) |
|
255 { |
|
256 rwork.elem (1) = maximum_step_size (); |
|
257 info.elem (6) = 1; |
|
258 } |
|
259 else |
|
260 info.elem (6) = 0; |
|
261 |
|
262 double *dummy = 0; |
|
263 int *idummy = 0; |
|
264 |
|
265 int *pinfo = info.fortran_vec (); |
|
266 int *piwork = iwork.fortran_vec (); |
|
267 double *prwork = rwork.fortran_vec (); |
|
268 |
|
269 // again: |
|
270 |
|
271 F77_XFCN (ddaspk, DDASPK, (ddaspk_f, n, t, px, pxdot, tout, pinfo, |
|
272 rel_tol, abs_tol, idid, prwork, lrw, |
|
273 piwork, liw, dummy, idummy, ddaspk_j, |
|
274 ddaspk_psol)); |
|
275 |
|
276 if (f77_exception_encountered) |
|
277 { |
3995
|
278 integration_error = true; |
3912
|
279 (*current_liboctave_error_handler) ("unrecoverable error in daspk"); |
|
280 } |
|
281 else |
|
282 { |
|
283 switch (idid) |
|
284 { |
|
285 case 1: // A step was successfully taken in intermediate-output |
|
286 // mode. The code has not yet reached TOUT. |
|
287 case 2: // The integration to TSTOP was successfully completed |
|
288 // (T=TSTOP) by stepping exactly to TSTOP. |
|
289 case 3: // The integration to TOUT was successfully completed |
|
290 // (T=TOUT) by stepping past TOUT. Y(*) is obtained by |
|
291 // interpolation. YPRIME(*) is obtained by interpolation. |
|
292 |
|
293 retval = x; |
|
294 t = tout; |
|
295 break; |
|
296 |
|
297 case -1: // A large amount of work has been expended. (~500 steps). |
|
298 case -2: // The error tolerances are too stringent. |
|
299 case -3: // The local error test cannot be satisfied because you |
|
300 // specified a zero component in ATOL and the |
|
301 // corresponding computed solution component is zero. |
|
302 // Thus, a pure relative error test is impossible for |
|
303 // this component. |
|
304 case -6: // DDASPK had repeated error test failures on the last |
|
305 // attempted step. |
|
306 case -7: // The corrector could not converge. |
|
307 case -8: // The matrix of partial derivatives is singular. |
|
308 case -9: // The corrector could not converge. There were repeated |
|
309 // error test failures in this step. |
|
310 case -10: // The corrector could not converge because IRES was |
|
311 // equal to minus one. |
|
312 case -11: // IRES equal to -2 was encountered and control is being |
|
313 // returned to the calling program. |
|
314 case -12: // DDASPK failed to compute the initial YPRIME. |
|
315 case -33: // The code has encountered trouble from which it cannot |
|
316 // recover. A message is printed explaining the trouble |
|
317 // and control is returned to the calling program. For |
|
318 // example, this occurs when invalid input is detected. |
|
319 default: |
3995
|
320 integration_error = true; |
3912
|
321 break; |
|
322 } |
|
323 } |
|
324 |
|
325 return retval; |
|
326 } |
|
327 |
|
328 Matrix |
|
329 DASPK::do_integrate (const ColumnVector& tout) |
|
330 { |
|
331 Matrix dummy; |
|
332 return integrate (tout, dummy); |
|
333 } |
|
334 |
|
335 Matrix |
|
336 DASPK::integrate (const ColumnVector& tout, Matrix& xdot_out) |
|
337 { |
|
338 Matrix retval; |
|
339 int n_out = tout.capacity (); |
|
340 |
|
341 if (n_out > 0 && n > 0) |
|
342 { |
|
343 retval.resize (n_out, n); |
|
344 xdot_out.resize (n_out, n); |
|
345 |
|
346 for (int i = 0; i < n; i++) |
|
347 { |
|
348 retval.elem (0, i) = x.elem (i); |
|
349 xdot_out.elem (0, i) = xdot.elem (i); |
|
350 } |
|
351 |
|
352 for (int j = 1; j < n_out; j++) |
|
353 { |
|
354 ColumnVector x_next = do_integrate (tout.elem (j)); |
|
355 |
|
356 if (integration_error) |
|
357 return retval; |
|
358 |
|
359 for (int i = 0; i < n; i++) |
|
360 { |
|
361 retval.elem (j, i) = x_next.elem (i); |
|
362 xdot_out.elem (j, i) = xdot.elem (i); |
|
363 } |
|
364 } |
|
365 } |
|
366 |
|
367 return retval; |
|
368 } |
|
369 |
|
370 Matrix |
|
371 DASPK::do_integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
|
372 { |
|
373 Matrix dummy; |
|
374 return integrate (tout, dummy, tcrit); |
|
375 } |
|
376 |
|
377 Matrix |
|
378 DASPK::integrate (const ColumnVector& tout, Matrix& xdot_out, |
|
379 const ColumnVector& tcrit) |
|
380 { |
|
381 Matrix retval; |
|
382 int n_out = tout.capacity (); |
|
383 |
|
384 if (n_out > 0 && n > 0) |
|
385 { |
|
386 retval.resize (n_out, n); |
|
387 xdot_out.resize (n_out, n); |
|
388 |
|
389 for (int i = 0; i < n; i++) |
|
390 { |
|
391 retval.elem (0, i) = x.elem (i); |
|
392 xdot_out.elem (0, i) = xdot.elem (i); |
|
393 } |
|
394 |
|
395 int n_crit = tcrit.capacity (); |
|
396 |
|
397 if (n_crit > 0) |
|
398 { |
|
399 int i_crit = 0; |
|
400 int i_out = 1; |
|
401 double next_crit = tcrit.elem (0); |
|
402 double next_out; |
|
403 while (i_out < n_out) |
|
404 { |
|
405 bool do_restart = false; |
|
406 |
|
407 next_out = tout.elem (i_out); |
|
408 if (i_crit < n_crit) |
|
409 next_crit = tcrit.elem (i_crit); |
|
410 |
|
411 bool save_output; |
|
412 double t_out; |
|
413 |
|
414 if (next_crit == next_out) |
|
415 { |
|
416 set_stop_time (next_crit); |
|
417 t_out = next_out; |
|
418 save_output = true; |
|
419 i_out++; |
|
420 i_crit++; |
|
421 do_restart = true; |
|
422 } |
|
423 else if (next_crit < next_out) |
|
424 { |
|
425 if (i_crit < n_crit) |
|
426 { |
|
427 set_stop_time (next_crit); |
|
428 t_out = next_crit; |
|
429 save_output = false; |
|
430 i_crit++; |
|
431 do_restart = true; |
|
432 } |
|
433 else |
|
434 { |
|
435 clear_stop_time (); |
|
436 t_out = next_out; |
|
437 save_output = true; |
|
438 i_out++; |
|
439 } |
|
440 } |
|
441 else |
|
442 { |
|
443 set_stop_time (next_crit); |
|
444 t_out = next_out; |
|
445 save_output = true; |
|
446 i_out++; |
|
447 } |
|
448 |
|
449 ColumnVector x_next = do_integrate (t_out); |
|
450 |
|
451 if (integration_error) |
|
452 return retval; |
|
453 |
|
454 if (save_output) |
|
455 { |
|
456 for (int i = 0; i < n; i++) |
|
457 { |
|
458 retval.elem (i_out-1, i) = x_next.elem (i); |
|
459 xdot_out.elem (i_out-1, i) = xdot.elem (i); |
|
460 } |
|
461 } |
|
462 |
|
463 if (do_restart) |
|
464 force_restart (); |
|
465 } |
|
466 } |
|
467 else |
|
468 { |
|
469 retval = integrate (tout, xdot_out); |
|
470 |
|
471 if (integration_error) |
|
472 return retval; |
|
473 } |
|
474 } |
|
475 |
|
476 return retval; |
|
477 } |
|
478 |
3995
|
479 std::string |
|
480 DASPK::error_message (void) const |
|
481 { |
|
482 std::string retval; |
|
483 |
|
484 switch (idid) |
|
485 { |
|
486 default: |
|
487 retval = "unknown error state"; |
|
488 break; |
|
489 } |
|
490 |
|
491 return retval; |
|
492 } |
|
493 |
3912
|
494 /* |
|
495 ;;; Local Variables: *** |
|
496 ;;; mode: C++ *** |
|
497 ;;; End: *** |
|
498 */ |