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 |
4043
|
34 #include <strstream> |
|
35 |
3912
|
36 #include "DASPK.h" |
|
37 #include "f77-fcn.h" |
|
38 #include "lo-error.h" |
|
39 |
|
40 typedef int (*daspk_fcn_ptr) (const double&, const double*, |
|
41 const double*, const double&, |
|
42 double*, int&, double*, int*); |
|
43 |
|
44 typedef int (*daspk_jac_ptr) (const double&, const double*, |
|
45 const double*, double*, |
|
46 const double&, double*, int*); |
|
47 |
|
48 typedef int (*daspk_psol_ptr) (const int&, const double&, |
|
49 const double*, const double*, |
|
50 const double*, const double&, |
|
51 const double*, double*, int*, |
|
52 double*, const double&, int&, |
|
53 double*, int*); |
|
54 |
|
55 extern "C" |
|
56 int F77_FUNC (ddaspk, DDASPK) (daspk_fcn_ptr, const int&, double&, |
|
57 double*, double*, double&, const int*, |
3998
|
58 const double*, const double*, int&, |
3912
|
59 double*, const int&, int*, const int&, |
|
60 const double*, const int*, |
|
61 daspk_jac_ptr, daspk_psol_ptr); |
|
62 |
|
63 static DAEFunc::DAERHSFunc user_fun; |
|
64 static DAEFunc::DAEJacFunc user_jac; |
|
65 static int nn; |
|
66 |
|
67 DASPK::DASPK (void) : DAE () |
|
68 { |
|
69 sanity_checked = 0; |
|
70 |
|
71 info.resize (15); |
|
72 |
|
73 for (int i = 0; i < 15; i++) |
|
74 info.elem (i) = 0; |
|
75 } |
|
76 |
|
77 DASPK::DASPK (const ColumnVector& state, double time, DAEFunc& f) |
|
78 : DAE (state, time, f) |
|
79 { |
|
80 n = size (); |
|
81 |
|
82 sanity_checked = 0; |
|
83 |
|
84 info.resize (20); |
|
85 |
|
86 for (int i = 0; i < 20; i++) |
|
87 info.elem (i) = 0; |
|
88 } |
|
89 |
|
90 DASPK::DASPK (const ColumnVector& state, const ColumnVector& deriv, |
|
91 double time, DAEFunc& f) |
|
92 : DAE (state, deriv, time, f) |
|
93 { |
|
94 n = size (); |
|
95 |
|
96 DAEFunc::set_function (f.function ()); |
|
97 DAEFunc::set_jacobian_function (f.jacobian_function ()); |
|
98 |
|
99 sanity_checked = 0; |
|
100 |
|
101 info.resize (20); |
|
102 |
|
103 for (int i = 0; i < 20; i++) |
|
104 info.elem (i) = 0; |
|
105 } |
|
106 |
|
107 int |
|
108 ddaspk_f (const double& time, const double *state, const double *deriv, |
|
109 const double&, double *delta, int& ires, double *, int *) |
|
110 { |
|
111 ColumnVector tmp_deriv (nn); |
|
112 ColumnVector tmp_state (nn); |
|
113 ColumnVector tmp_delta (nn); |
|
114 |
|
115 for (int i = 0; i < nn; i++) |
|
116 { |
|
117 tmp_deriv.elem (i) = deriv [i]; |
|
118 tmp_state.elem (i) = state [i]; |
|
119 } |
|
120 |
|
121 tmp_delta = user_fun (tmp_state, tmp_deriv, time, ires); |
|
122 |
|
123 if (ires >= 0) |
|
124 { |
|
125 if (tmp_delta.length () == 0) |
|
126 ires = -2; |
|
127 else |
|
128 { |
|
129 for (int i = 0; i < nn; i++) |
|
130 delta [i] = tmp_delta.elem (i); |
|
131 } |
|
132 } |
|
133 |
|
134 return 0; |
|
135 } |
|
136 |
|
137 //NEQ, T, Y, YPRIME, SAVR, WK, CJ, WGHT, |
|
138 //C WP, IWP, B, EPLIN, IER, RPAR, IPAR) |
|
139 |
|
140 int |
|
141 ddaspk_psol (const int& neq, const double& time, const double *state, |
|
142 const double *deriv, const double *savr, |
|
143 const double& cj, const double *wght, double *wp, |
|
144 int *iwp, double *b, const double& eplin, int& ier, |
|
145 double *, int*) |
|
146 { |
|
147 abort (); |
3946
|
148 return 0; |
3912
|
149 } |
|
150 |
3991
|
151 |
3912
|
152 int |
3991
|
153 ddaspk_j (const double& time, const double *state, const double *deriv, |
|
154 double *pd, const double& cj, double *, int *) |
3912
|
155 { |
3991
|
156 // XXX FIXME XXX -- would be nice to avoid copying the data. |
|
157 |
3912
|
158 ColumnVector tmp_state (nn); |
|
159 ColumnVector tmp_deriv (nn); |
|
160 |
3991
|
161 for (int i = 0; i < nn; i++) |
|
162 { |
|
163 tmp_deriv.elem (i) = deriv [i]; |
|
164 tmp_state.elem (i) = state [i]; |
|
165 } |
3912
|
166 |
3991
|
167 Matrix tmp_pd = user_jac (tmp_state, tmp_deriv, time, cj); |
3912
|
168 |
|
169 for (int j = 0; j < nn; j++) |
|
170 for (int i = 0; i < nn; i++) |
3991
|
171 pd [nn * j + i] = tmp_pd.elem (i, j); |
3912
|
172 |
|
173 return 0; |
|
174 } |
|
175 |
|
176 ColumnVector |
|
177 DASPK::do_integrate (double tout) |
|
178 { |
|
179 ColumnVector retval; |
|
180 |
|
181 if (restart) |
|
182 { |
3995
|
183 restart = false; |
3912
|
184 info.elem (0) = 0; |
|
185 } |
|
186 |
|
187 liw = 40 + n; |
|
188 if (info(9) == 1 || info(9) == 3) |
|
189 liw += n; |
|
190 if (info (10) == 1 || info(15) == 1) |
|
191 liw += n; |
|
192 |
|
193 lrw = 50 + 9*n; |
|
194 if (info(5) == 0) |
|
195 lrw += n*n; |
|
196 if (info(15) == 1) |
|
197 lrw += n; |
|
198 |
|
199 if (iwork.length () != liw) |
|
200 iwork.resize (liw); |
|
201 |
|
202 if (rwork.length () != lrw) |
|
203 rwork.resize (lrw); |
|
204 |
3995
|
205 integration_error = false; |
3912
|
206 |
|
207 if (DAEFunc::jacobian_function ()) |
|
208 info.elem (4) = 1; |
|
209 else |
|
210 info.elem (4) = 0; |
|
211 |
|
212 double *px = x.fortran_vec (); |
|
213 double *pxdot = xdot.fortran_vec (); |
|
214 |
|
215 nn = n; |
|
216 user_fun = DAEFunc::fun; |
|
217 user_jac = DAEFunc::jac; |
|
218 |
|
219 if (! sanity_checked) |
|
220 { |
|
221 int ires = 0; |
|
222 |
|
223 ColumnVector res = (*user_fun) (x, xdot, t, ires); |
|
224 |
|
225 if (res.length () != x.length ()) |
|
226 { |
|
227 (*current_liboctave_error_handler) |
|
228 ("daspk: inconsistent sizes for state and residual vectors"); |
|
229 |
3995
|
230 integration_error = true; |
3912
|
231 return retval; |
|
232 } |
|
233 |
|
234 sanity_checked = 1; |
|
235 } |
|
236 |
|
237 if (stop_time_set) |
|
238 { |
|
239 rwork.elem (0) = stop_time; |
|
240 info.elem (3) = 1; |
|
241 } |
|
242 else |
|
243 info.elem (3) = 0; |
|
244 |
3998
|
245 Array<double> abs_tol = absolute_tolerance (); |
|
246 Array<double> rel_tol = relative_tolerance (); |
|
247 |
|
248 int abs_tol_len = abs_tol.length (); |
|
249 int rel_tol_len = rel_tol.length (); |
|
250 |
|
251 if (abs_tol_len == 1 && rel_tol_len == 1) |
|
252 { |
|
253 info.elem (1) = 0; |
|
254 } |
|
255 else if (abs_tol_len == n && rel_tol_len == n) |
|
256 { |
|
257 info.elem (1) = 1; |
|
258 } |
|
259 else |
|
260 { |
|
261 (*current_liboctave_error_handler) |
|
262 ("dassl: inconsistent sizes for tolerance arrays"); |
|
263 |
|
264 integration_error = true; |
|
265 return retval; |
|
266 } |
3912
|
267 |
|
268 if (initial_step_size () >= 0.0) |
|
269 { |
|
270 rwork.elem (2) = initial_step_size (); |
|
271 info.elem (7) = 1; |
|
272 } |
|
273 else |
|
274 info.elem (7) = 0; |
|
275 |
|
276 if (maximum_step_size () >= 0.0) |
|
277 { |
|
278 rwork.elem (1) = maximum_step_size (); |
|
279 info.elem (6) = 1; |
|
280 } |
|
281 else |
|
282 info.elem (6) = 0; |
|
283 |
|
284 double *dummy = 0; |
|
285 int *idummy = 0; |
|
286 |
|
287 int *pinfo = info.fortran_vec (); |
|
288 int *piwork = iwork.fortran_vec (); |
|
289 double *prwork = rwork.fortran_vec (); |
3998
|
290 double *pabs_tol = abs_tol.fortran_vec (); |
|
291 double *prel_tol = rel_tol.fortran_vec (); |
3912
|
292 |
|
293 // again: |
|
294 |
|
295 F77_XFCN (ddaspk, DDASPK, (ddaspk_f, n, t, px, pxdot, tout, pinfo, |
3998
|
296 prel_tol, pabs_tol, istate, prwork, lrw, |
3912
|
297 piwork, liw, dummy, idummy, ddaspk_j, |
|
298 ddaspk_psol)); |
|
299 |
|
300 if (f77_exception_encountered) |
|
301 { |
3995
|
302 integration_error = true; |
3912
|
303 (*current_liboctave_error_handler) ("unrecoverable error in daspk"); |
|
304 } |
|
305 else |
|
306 { |
3997
|
307 switch (istate) |
3912
|
308 { |
|
309 case 1: // A step was successfully taken in intermediate-output |
|
310 // mode. The code has not yet reached TOUT. |
|
311 case 2: // The integration to TSTOP was successfully completed |
|
312 // (T=TSTOP) by stepping exactly to TSTOP. |
|
313 case 3: // The integration to TOUT was successfully completed |
|
314 // (T=TOUT) by stepping past TOUT. Y(*) is obtained by |
|
315 // interpolation. YPRIME(*) is obtained by interpolation. |
3996
|
316 case 4: // The initial condition calculation, with |
|
317 // INFO(11) > 0, was successful, and INFO(14) = 1. |
|
318 // No integration steps were taken, and the solution |
|
319 // is not considered to have been started. |
3912
|
320 retval = x; |
|
321 t = tout; |
|
322 break; |
|
323 |
|
324 case -1: // A large amount of work has been expended. (~500 steps). |
|
325 case -2: // The error tolerances are too stringent. |
|
326 case -3: // The local error test cannot be satisfied because you |
|
327 // specified a zero component in ATOL and the |
|
328 // corresponding computed solution component is zero. |
|
329 // Thus, a pure relative error test is impossible for |
|
330 // this component. |
|
331 case -6: // DDASPK had repeated error test failures on the last |
|
332 // attempted step. |
|
333 case -7: // The corrector could not converge. |
|
334 case -8: // The matrix of partial derivatives is singular. |
|
335 case -9: // The corrector could not converge. There were repeated |
|
336 // error test failures in this step. |
|
337 case -10: // The corrector could not converge because IRES was |
|
338 // equal to minus one. |
|
339 case -11: // IRES equal to -2 was encountered and control is being |
|
340 // returned to the calling program. |
|
341 case -12: // DDASPK failed to compute the initial YPRIME. |
3996
|
342 case -13: // Unrecoverable error encountered inside user's |
|
343 // PSOL routine, and control is being returned to |
|
344 // the calling program. |
|
345 case -14: // The Krylov linear system solver could not |
|
346 // achieve convergence. |
3912
|
347 case -33: // The code has encountered trouble from which it cannot |
|
348 // recover. A message is printed explaining the trouble |
|
349 // and control is returned to the calling program. For |
|
350 // example, this occurs when invalid input is detected. |
3996
|
351 integration_error = true; |
|
352 break; |
|
353 |
3912
|
354 default: |
3995
|
355 integration_error = true; |
3996
|
356 (*current_liboctave_error_handler) |
3997
|
357 ("unrecognized value of istate (= %d) returned from ddaspk", |
|
358 istate); |
3912
|
359 break; |
|
360 } |
|
361 } |
|
362 |
|
363 return retval; |
|
364 } |
|
365 |
|
366 Matrix |
|
367 DASPK::do_integrate (const ColumnVector& tout) |
|
368 { |
|
369 Matrix dummy; |
|
370 return integrate (tout, dummy); |
|
371 } |
|
372 |
|
373 Matrix |
|
374 DASPK::integrate (const ColumnVector& tout, Matrix& xdot_out) |
|
375 { |
|
376 Matrix retval; |
|
377 int n_out = tout.capacity (); |
|
378 |
|
379 if (n_out > 0 && n > 0) |
|
380 { |
|
381 retval.resize (n_out, n); |
|
382 xdot_out.resize (n_out, n); |
|
383 |
|
384 for (int i = 0; i < n; i++) |
|
385 { |
|
386 retval.elem (0, i) = x.elem (i); |
|
387 xdot_out.elem (0, i) = xdot.elem (i); |
|
388 } |
|
389 |
|
390 for (int j = 1; j < n_out; j++) |
|
391 { |
|
392 ColumnVector x_next = do_integrate (tout.elem (j)); |
|
393 |
|
394 if (integration_error) |
|
395 return retval; |
|
396 |
|
397 for (int i = 0; i < n; i++) |
|
398 { |
|
399 retval.elem (j, i) = x_next.elem (i); |
|
400 xdot_out.elem (j, i) = xdot.elem (i); |
|
401 } |
|
402 } |
|
403 } |
|
404 |
|
405 return retval; |
|
406 } |
|
407 |
|
408 Matrix |
|
409 DASPK::do_integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
|
410 { |
|
411 Matrix dummy; |
|
412 return integrate (tout, dummy, tcrit); |
|
413 } |
|
414 |
|
415 Matrix |
|
416 DASPK::integrate (const ColumnVector& tout, Matrix& xdot_out, |
|
417 const ColumnVector& tcrit) |
|
418 { |
|
419 Matrix retval; |
|
420 int n_out = tout.capacity (); |
|
421 |
|
422 if (n_out > 0 && n > 0) |
|
423 { |
|
424 retval.resize (n_out, n); |
|
425 xdot_out.resize (n_out, n); |
|
426 |
|
427 for (int i = 0; i < n; i++) |
|
428 { |
|
429 retval.elem (0, i) = x.elem (i); |
|
430 xdot_out.elem (0, i) = xdot.elem (i); |
|
431 } |
|
432 |
|
433 int n_crit = tcrit.capacity (); |
|
434 |
|
435 if (n_crit > 0) |
|
436 { |
|
437 int i_crit = 0; |
|
438 int i_out = 1; |
|
439 double next_crit = tcrit.elem (0); |
|
440 double next_out; |
|
441 while (i_out < n_out) |
|
442 { |
|
443 bool do_restart = false; |
|
444 |
|
445 next_out = tout.elem (i_out); |
|
446 if (i_crit < n_crit) |
|
447 next_crit = tcrit.elem (i_crit); |
|
448 |
|
449 bool save_output; |
|
450 double t_out; |
|
451 |
|
452 if (next_crit == next_out) |
|
453 { |
|
454 set_stop_time (next_crit); |
|
455 t_out = next_out; |
|
456 save_output = true; |
|
457 i_out++; |
|
458 i_crit++; |
|
459 do_restart = true; |
|
460 } |
|
461 else if (next_crit < next_out) |
|
462 { |
|
463 if (i_crit < n_crit) |
|
464 { |
|
465 set_stop_time (next_crit); |
|
466 t_out = next_crit; |
|
467 save_output = false; |
|
468 i_crit++; |
|
469 do_restart = true; |
|
470 } |
|
471 else |
|
472 { |
|
473 clear_stop_time (); |
|
474 t_out = next_out; |
|
475 save_output = true; |
|
476 i_out++; |
|
477 } |
|
478 } |
|
479 else |
|
480 { |
|
481 set_stop_time (next_crit); |
|
482 t_out = next_out; |
|
483 save_output = true; |
|
484 i_out++; |
|
485 } |
|
486 |
|
487 ColumnVector x_next = do_integrate (t_out); |
|
488 |
|
489 if (integration_error) |
|
490 return retval; |
|
491 |
|
492 if (save_output) |
|
493 { |
|
494 for (int i = 0; i < n; i++) |
|
495 { |
|
496 retval.elem (i_out-1, i) = x_next.elem (i); |
|
497 xdot_out.elem (i_out-1, i) = xdot.elem (i); |
|
498 } |
|
499 } |
|
500 |
|
501 if (do_restart) |
|
502 force_restart (); |
|
503 } |
|
504 } |
|
505 else |
|
506 { |
|
507 retval = integrate (tout, xdot_out); |
|
508 |
|
509 if (integration_error) |
|
510 return retval; |
|
511 } |
|
512 } |
|
513 |
|
514 return retval; |
|
515 } |
|
516 |
3995
|
517 std::string |
|
518 DASPK::error_message (void) const |
|
519 { |
|
520 std::string retval; |
|
521 |
4043
|
522 std::ostrstream buf; |
|
523 buf << t << ends; |
|
524 const char *t = buf.str (); |
|
525 std::string t_curr = t; |
|
526 delete [] t; |
|
527 |
3997
|
528 switch (istate) |
3995
|
529 { |
3996
|
530 case 1: |
|
531 retval = "a step was successfully taken in intermediate-output mode."; |
|
532 break; |
|
533 |
|
534 case 2: |
|
535 retval = "integration completed by stepping exactly to TOUT"; |
|
536 break; |
|
537 |
|
538 case 3: |
|
539 retval = "integration to tout completed by stepping past TOUT"; |
|
540 break; |
|
541 |
|
542 case 4: |
|
543 retval = "initial condition calculation completed successfully"; |
|
544 break; |
|
545 |
|
546 case -1: |
4043
|
547 retval = std::string ("a large amount of work has been expended (t =") |
|
548 + t_curr + ")"; |
3996
|
549 break; |
|
550 |
|
551 case -2: |
|
552 retval = "the error tolerances are too stringent"; |
|
553 break; |
|
554 |
|
555 case -3: |
4043
|
556 retval = std::string ("error weight became zero during problem. (t = ") |
|
557 + t_curr |
|
558 + "; solution component i vanished, and atol or atol(i) == 0)"; |
3996
|
559 break; |
|
560 |
|
561 case -6: |
4043
|
562 retval = std::string ("repeated error test failures on the last attempted step (t = ") |
|
563 + t_curr + ")"; |
3996
|
564 break; |
|
565 |
|
566 case -7: |
4043
|
567 retval = std::string ("the corrector could not converge (t = ") |
|
568 + t_curr + ")"; |
3996
|
569 break; |
|
570 |
|
571 case -8: |
4043
|
572 retval = std::string ("the matrix of partial derivatives is singular (t = ") |
|
573 + t_curr + ")"; |
3996
|
574 break; |
|
575 |
|
576 case -9: |
4043
|
577 retval = std::string ("the corrector could not converge (t = ") |
|
578 + t_curr + "; repeated test failures)"; |
3996
|
579 break; |
|
580 |
|
581 case -10: |
4043
|
582 retval = std::string ("corrector could not converge because IRES was -1 (t = ") |
|
583 + t_curr + ")"; |
3996
|
584 break; |
|
585 |
|
586 case -11: |
4043
|
587 retval = std::string ("return requested in user-supplied function (t = ") |
|
588 + t_curr + ")"; |
3996
|
589 break; |
|
590 |
|
591 case -12: |
|
592 retval = "failed to compute consistent initial conditions"; |
|
593 break; |
|
594 |
|
595 case -13: |
4043
|
596 retval = std::string ("unrecoverable error encountered inside user's PSOL function (t = ") |
|
597 + t_curr + ")"; |
3996
|
598 break; |
|
599 |
|
600 case -14: |
4043
|
601 retval = std::string ("the Krylov linear system solver failed to converge (t = ") |
|
602 + t_curr + ")"; |
3996
|
603 break; |
|
604 |
|
605 case -33: |
|
606 retval = "unrecoverable error (see printed message)"; |
|
607 break; |
|
608 |
3995
|
609 default: |
|
610 retval = "unknown error state"; |
|
611 break; |
|
612 } |
|
613 |
|
614 return retval; |
|
615 } |
|
616 |
3912
|
617 /* |
|
618 ;;; Local Variables: *** |
|
619 ;;; mode: C++ *** |
|
620 ;;; End: *** |
|
621 */ |