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