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*, |
3998
|
47 const double*, const double*, int&, |
3991
|
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 |
3998
|
217 Array<double> abs_tol = absolute_tolerance (); |
|
218 Array<double> rel_tol = relative_tolerance (); |
|
219 |
|
220 int abs_tol_len = abs_tol.length (); |
|
221 int rel_tol_len = rel_tol.length (); |
|
222 |
|
223 if (abs_tol_len == 1 && rel_tol_len == 1) |
|
224 { |
|
225 info.elem (1) = 0; |
|
226 } |
|
227 else if (abs_tol_len == n && rel_tol_len == n) |
|
228 { |
|
229 info.elem (1) = 1; |
|
230 } |
|
231 else |
|
232 { |
|
233 (*current_liboctave_error_handler) |
|
234 ("dassl: inconsistent sizes for tolerance arrays"); |
|
235 |
|
236 integration_error = true; |
|
237 return retval; |
|
238 } |
289
|
239 |
|
240 if (initial_step_size () >= 0.0) |
|
241 { |
1945
|
242 rwork.elem (2) = initial_step_size (); |
|
243 info.elem (7) = 1; |
289
|
244 } |
|
245 else |
1945
|
246 info.elem (7) = 0; |
289
|
247 |
|
248 if (maximum_step_size () >= 0.0) |
|
249 { |
3312
|
250 rwork.elem (1) = maximum_step_size (); |
1945
|
251 info.elem (6) = 1; |
289
|
252 } |
|
253 else |
1945
|
254 info.elem (6) = 0; |
289
|
255 |
1365
|
256 double *dummy = 0; |
|
257 int *idummy = 0; |
3
|
258 |
1945
|
259 int *pinfo = info.fortran_vec (); |
|
260 int *piwork = iwork.fortran_vec (); |
|
261 double *prwork = rwork.fortran_vec (); |
3998
|
262 double *pabs_tol = abs_tol.fortran_vec (); |
|
263 double *prel_tol = rel_tol.fortran_vec (); |
3
|
264 |
465
|
265 // again: |
3
|
266 |
1945
|
267 F77_XFCN (ddassl, DDASSL, (ddassl_f, n, t, px, pxdot, tout, pinfo, |
3998
|
268 prel_tol, pabs_tol, istate, prwork, lrw, |
1945
|
269 piwork, liw, dummy, idummy, ddassl_j)); |
3
|
270 |
1945
|
271 if (f77_exception_encountered) |
3178
|
272 { |
3994
|
273 integration_error = true; |
3178
|
274 (*current_liboctave_error_handler) ("unrecoverable error in dassl"); |
|
275 } |
1945
|
276 else |
3
|
277 { |
3997
|
278 switch (istate) |
1945
|
279 { |
|
280 case 1: // A step was successfully taken in intermediate-output |
|
281 // mode. The code has not yet reached TOUT. |
|
282 case 2: // The integration to TSTOP was successfully completed |
|
283 // (T=TSTOP) by stepping exactly to TSTOP. |
|
284 case 3: // The integration to TOUT was successfully completed |
|
285 // (T=TOUT) by stepping past TOUT. Y(*) is obtained by |
|
286 // interpolation. YPRIME(*) is obtained by interpolation. |
|
287 retval = x; |
|
288 t = tout; |
|
289 break; |
1360
|
290 |
1945
|
291 case -1: // A large amount of work has been expended. (~500 steps). |
|
292 case -2: // The error tolerances are too stringent. |
|
293 case -3: // The local error test cannot be satisfied because you |
|
294 // specified a zero component in ATOL and the |
|
295 // corresponding computed solution component is zero. |
|
296 // Thus, a pure relative error test is impossible for |
|
297 // this component. |
|
298 case -6: // DDASSL had repeated error test failures on the last |
|
299 // attempted step. |
|
300 case -7: // The corrector could not converge. |
|
301 case -8: // The matrix of partial derivatives is singular. |
|
302 case -9: // The corrector could not converge. There were repeated |
|
303 // error test failures in this step. |
|
304 case -10: // The corrector could not converge because IRES was |
|
305 // equal to minus one. |
|
306 case -11: // IRES equal to -2 was encountered and control is being |
|
307 // returned to the calling program. |
|
308 case -12: // DDASSL failed to compute the initial YPRIME. |
|
309 case -33: // The code has encountered trouble from which it cannot |
|
310 // recover. A message is printed explaining the trouble |
|
311 // and control is returned to the calling program. For |
|
312 // example, this occurs when invalid input is detected. |
3996
|
313 integration_error = true; |
|
314 break; |
|
315 |
1945
|
316 default: |
3994
|
317 integration_error = true; |
3996
|
318 (*current_liboctave_error_handler) |
3997
|
319 ("unrecognized value of istate (= %d) returned from ddassl", |
|
320 istate); |
1945
|
321 break; |
|
322 } |
3
|
323 } |
|
324 |
1945
|
325 return retval; |
3
|
326 } |
|
327 |
|
328 Matrix |
1842
|
329 DASSL::do_integrate (const ColumnVector& tout) |
|
330 { |
|
331 Matrix dummy; |
|
332 return integrate (tout, dummy); |
|
333 } |
|
334 |
|
335 Matrix |
|
336 DASSL::integrate (const ColumnVector& tout, Matrix& xdot_out) |
3
|
337 { |
|
338 Matrix retval; |
|
339 int n_out = tout.capacity (); |
|
340 |
|
341 if (n_out > 0 && n > 0) |
|
342 { |
|
343 retval.resize (n_out, n); |
|
344 xdot_out.resize (n_out, n); |
|
345 |
|
346 for (int i = 0; i < n; i++) |
|
347 { |
|
348 retval.elem (0, i) = x.elem (i); |
|
349 xdot_out.elem (0, i) = xdot.elem (i); |
|
350 } |
|
351 |
|
352 for (int j = 1; j < n_out; j++) |
|
353 { |
1842
|
354 ColumnVector x_next = do_integrate (tout.elem (j)); |
256
|
355 |
|
356 if (integration_error) |
|
357 return retval; |
|
358 |
1321
|
359 for (int i = 0; i < n; i++) |
3
|
360 { |
|
361 retval.elem (j, i) = x_next.elem (i); |
|
362 xdot_out.elem (j, i) = xdot.elem (i); |
|
363 } |
|
364 } |
|
365 } |
|
366 |
|
367 return retval; |
|
368 } |
|
369 |
|
370 Matrix |
3519
|
371 DASSL::do_integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
|
372 { |
|
373 Matrix dummy; |
|
374 return integrate (tout, dummy, tcrit); |
|
375 } |
|
376 |
|
377 Matrix |
1842
|
378 DASSL::integrate (const ColumnVector& tout, Matrix& xdot_out, |
|
379 const ColumnVector& tcrit) |
3
|
380 { |
|
381 Matrix retval; |
|
382 int n_out = tout.capacity (); |
|
383 |
|
384 if (n_out > 0 && n > 0) |
|
385 { |
|
386 retval.resize (n_out, n); |
|
387 xdot_out.resize (n_out, n); |
|
388 |
|
389 for (int i = 0; i < n; i++) |
|
390 { |
|
391 retval.elem (0, i) = x.elem (i); |
|
392 xdot_out.elem (0, i) = xdot.elem (i); |
|
393 } |
|
394 |
|
395 int n_crit = tcrit.capacity (); |
|
396 |
|
397 if (n_crit > 0) |
|
398 { |
|
399 int i_crit = 0; |
|
400 int i_out = 1; |
|
401 double next_crit = tcrit.elem (0); |
|
402 double next_out; |
|
403 while (i_out < n_out) |
|
404 { |
3487
|
405 bool do_restart = false; |
3
|
406 |
|
407 next_out = tout.elem (i_out); |
|
408 if (i_crit < n_crit) |
|
409 next_crit = tcrit.elem (i_crit); |
|
410 |
3487
|
411 bool save_output; |
3
|
412 double t_out; |
|
413 |
|
414 if (next_crit == next_out) |
|
415 { |
|
416 set_stop_time (next_crit); |
|
417 t_out = next_out; |
3487
|
418 save_output = true; |
3
|
419 i_out++; |
|
420 i_crit++; |
3487
|
421 do_restart = true; |
3
|
422 } |
|
423 else if (next_crit < next_out) |
|
424 { |
|
425 if (i_crit < n_crit) |
|
426 { |
|
427 set_stop_time (next_crit); |
|
428 t_out = next_crit; |
3487
|
429 save_output = false; |
3
|
430 i_crit++; |
3487
|
431 do_restart = true; |
3
|
432 } |
|
433 else |
|
434 { |
|
435 clear_stop_time (); |
|
436 t_out = next_out; |
3487
|
437 save_output = true; |
3
|
438 i_out++; |
|
439 } |
|
440 } |
|
441 else |
|
442 { |
|
443 set_stop_time (next_crit); |
|
444 t_out = next_out; |
3487
|
445 save_output = true; |
3
|
446 i_out++; |
|
447 } |
|
448 |
1842
|
449 ColumnVector x_next = do_integrate (t_out); |
3
|
450 |
256
|
451 if (integration_error) |
|
452 return retval; |
|
453 |
3
|
454 if (save_output) |
|
455 { |
1321
|
456 for (int i = 0; i < n; i++) |
3
|
457 { |
|
458 retval.elem (i_out-1, i) = x_next.elem (i); |
|
459 xdot_out.elem (i_out-1, i) = xdot.elem (i); |
|
460 } |
|
461 } |
|
462 |
|
463 if (do_restart) |
|
464 force_restart (); |
|
465 } |
|
466 } |
|
467 else |
256
|
468 { |
|
469 retval = integrate (tout, xdot_out); |
|
470 |
|
471 if (integration_error) |
|
472 return retval; |
|
473 } |
3
|
474 } |
|
475 |
|
476 return retval; |
|
477 } |
289
|
478 |
3995
|
479 std::string |
|
480 DASSL::error_message (void) const |
|
481 { |
|
482 std::string retval; |
|
483 |
3997
|
484 switch (istate) |
3995
|
485 { |
3996
|
486 case 1: |
|
487 retval = "a step was successfully taken in intermediate-output mode."; |
|
488 break; |
|
489 |
|
490 case 2: |
|
491 retval = "integration completed by stepping exactly to TOUT"; |
|
492 break; |
|
493 |
|
494 case 3: |
|
495 retval = "integration to tout completed by stepping past TOUT"; |
|
496 break; |
|
497 |
|
498 case -1: |
|
499 retval = "a large amount of work has been expended"; |
|
500 break; |
|
501 |
|
502 case -2: |
|
503 retval = "the error tolerances are too stringent"; |
|
504 break; |
|
505 |
|
506 case -3: |
|
507 retval = "error weight became zero during problem.\ |
|
508 (solution component i vanished, and atol or atol(i) == 0)"; |
|
509 break; |
|
510 |
|
511 case -6: |
|
512 retval = "repeated error test failures on the last attempted step"; |
|
513 break; |
|
514 |
|
515 case -7: |
|
516 retval = "the corrector could not converge"; |
|
517 break; |
|
518 |
|
519 case -8: |
|
520 retval = "the matrix of partial derivatives is singular"; |
|
521 break; |
|
522 |
|
523 case -9: |
|
524 retval = "the corrector could not converge (repeated test failures)"; |
|
525 break; |
|
526 |
|
527 case -10: |
|
528 retval = "corrector could not converge because IRES was -1"; |
|
529 break; |
|
530 |
|
531 case -11: |
|
532 retval = "return requested in user-supplied function"; |
|
533 break; |
|
534 |
|
535 case -12: |
|
536 retval = "failed to compute consistent initial conditions"; |
|
537 break; |
|
538 |
|
539 case -33: |
|
540 retval = "unrecoverable error (see printed message)"; |
|
541 break; |
|
542 |
3995
|
543 default: |
|
544 retval = "unknown error state"; |
|
545 break; |
|
546 } |
|
547 |
|
548 return retval; |
|
549 } |
|
550 |
289
|
551 /* |
|
552 ;;; Local Variables: *** |
|
553 ;;; mode: C++ *** |
|
554 ;;; End: *** |
|
555 */ |