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