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