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