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