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