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