3990
|
1 /* |
|
2 |
|
3 Copyright (C) 2002 John W. Eaton |
|
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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cfloat> |
|
28 #include <cmath> |
|
29 |
|
30 #include "DASRT.h" |
|
31 #include "f77-fcn.h" |
|
32 #include "lo-error.h" |
4051
|
33 #include "lo-sstream.h" |
4180
|
34 #include "quit.h" |
3990
|
35 |
3991
|
36 typedef int (*dasrt_fcn_ptr) (const double&, const double*, const double*, |
|
37 double*, int&, double*, int*); |
|
38 |
|
39 typedef int (*dasrt_jac_ptr) (const double&, const double*, const double*, |
|
40 double*, const double&, double*, int*); |
|
41 |
|
42 typedef int (*dasrt_constr_ptr) (const int&, const double&, const double*, |
|
43 const int&, double*, double*, int*); |
|
44 |
3990
|
45 extern "C" |
4552
|
46 { |
|
47 F77_RET_T |
4576
|
48 F77_FUNC (ddasrt, DDASRT) (dasrt_fcn_ptr, const int&, double&, |
|
49 double*, double*, const double&, int*, |
|
50 const double*, const double*, int&, double*, |
|
51 const int&, int*, const int&, double*, |
|
52 int*, dasrt_jac_ptr, dasrt_constr_ptr, |
|
53 const int&, int*); |
4552
|
54 } |
3990
|
55 |
|
56 static DAEFunc::DAERHSFunc user_fsub; |
|
57 static DAEFunc::DAEJacFunc user_jsub; |
|
58 static DAERTFunc::DAERTConstrFunc user_csub; |
3993
|
59 |
3990
|
60 static int nn; |
|
61 |
|
62 static int |
3991
|
63 ddasrt_f (const double& t, const double *state, const double *deriv, |
4662
|
64 double *delta, int& ires, double *, int *) |
3990
|
65 { |
4180
|
66 BEGIN_INTERRUPT_WITH_EXCEPTIONS; |
|
67 |
3990
|
68 ColumnVector tmp_state (nn); |
4133
|
69 ColumnVector tmp_deriv (nn); |
3990
|
70 |
|
71 for (int i = 0; i < nn; i++) |
4133
|
72 { |
|
73 tmp_state(i) = state[i]; |
|
74 tmp_deriv(i) = deriv[i]; |
|
75 } |
3990
|
76 |
3994
|
77 ColumnVector tmp_fval = (*user_fsub) (tmp_state, tmp_deriv, t, ires); |
3990
|
78 |
|
79 if (tmp_fval.length () == 0) |
|
80 ires = -2; |
|
81 else |
|
82 { |
|
83 for (int i = 0; i < nn; i++) |
|
84 delta[i] = tmp_fval(i); |
|
85 } |
|
86 |
4180
|
87 END_INTERRUPT_WITH_EXCEPTIONS; |
|
88 |
3990
|
89 return 0; |
|
90 } |
|
91 |
3991
|
92 int |
|
93 ddasrt_j (const double& time, const double *state, const double *deriv, |
|
94 double *pd, const double& cj, double *, int *) |
3990
|
95 { |
4180
|
96 BEGIN_INTERRUPT_WITH_EXCEPTIONS; |
|
97 |
3991
|
98 // XXX FIXME XXX -- would be nice to avoid copying the data. |
3990
|
99 |
3991
|
100 ColumnVector tmp_state (nn); |
|
101 ColumnVector tmp_deriv (nn); |
3990
|
102 |
3991
|
103 for (int i = 0; i < nn; i++) |
|
104 { |
|
105 tmp_deriv.elem (i) = deriv [i]; |
|
106 tmp_state.elem (i) = state [i]; |
|
107 } |
3990
|
108 |
3994
|
109 Matrix tmp_pd = (*user_jsub) (tmp_state, tmp_deriv, time, cj); |
3990
|
110 |
|
111 for (int j = 0; j < nn; j++) |
|
112 for (int i = 0; i < nn; i++) |
3991
|
113 pd [nn * j + i] = tmp_pd.elem (i, j); |
3990
|
114 |
4180
|
115 END_INTERRUPT_WITH_EXCEPTIONS; |
|
116 |
3990
|
117 return 0; |
|
118 } |
|
119 |
|
120 static int |
3991
|
121 ddasrt_g (const int& neq, const double& t, const double *state, |
4662
|
122 const int& ng, double *gout, double *, int *) |
3990
|
123 { |
4180
|
124 BEGIN_INTERRUPT_WITH_EXCEPTIONS; |
|
125 |
3990
|
126 int n = neq; |
|
127 |
|
128 ColumnVector tmp_state (n); |
|
129 for (int i = 0; i < n; i++) |
|
130 tmp_state(i) = state[i]; |
|
131 |
3994
|
132 ColumnVector tmp_fval = (*user_csub) (tmp_state, t); |
3990
|
133 |
|
134 for (int i = 0; i < ng; i++) |
|
135 gout[i] = tmp_fval(i); |
|
136 |
4180
|
137 END_INTERRUPT_WITH_EXCEPTIONS; |
|
138 |
3990
|
139 return 0; |
|
140 } |
|
141 |
|
142 void |
|
143 DASRT::integrate (double tout) |
|
144 { |
|
145 DASRT_result retval; |
|
146 |
4049
|
147 // I suppose this is the safe thing to do. If this is the first |
|
148 // call, or if anything about the problem has changed, we should |
|
149 // start completely fresh. |
|
150 |
|
151 if (! initialized || restart |
|
152 || DAEFunc::reset || DAERTFunc::reset || DASRT_options::reset) |
3990
|
153 { |
|
154 integration_error = false; |
|
155 |
4049
|
156 initialized = true; |
|
157 |
|
158 info.resize (15); |
|
159 |
|
160 for (int i = 0; i < 15; i++) |
|
161 info(i) = 0; |
|
162 |
|
163 pinfo = info.fortran_vec (); |
|
164 |
|
165 int n = size (); |
|
166 |
|
167 nn = n; |
3990
|
168 |
4133
|
169 // DAERTFunc |
|
170 |
|
171 user_csub = DAERTFunc::constraint_function (); |
|
172 |
|
173 if (user_csub) |
|
174 { |
|
175 ColumnVector tmp = (*user_csub) (x, t); |
|
176 ng = tmp.length (); |
|
177 } |
|
178 else |
|
179 ng = 0; |
|
180 |
|
181 int maxord = maximum_order (); |
|
182 if (maxord >= 0) |
|
183 { |
|
184 if (maxord > 0 && maxord < 6) |
|
185 { |
|
186 info(8) = 1; |
|
187 iwork(2) = maxord; |
|
188 } |
|
189 else |
|
190 { |
|
191 (*current_liboctave_error_handler) |
|
192 ("dassl: invalid value for maximum order"); |
|
193 integration_error = true; |
|
194 return; |
|
195 } |
|
196 } |
|
197 |
4428
|
198 liw = 21 + n; |
4133
|
199 lrw = 50 + 9*n + n*n + 3*ng; |
4049
|
200 |
|
201 iwork.resize (liw); |
|
202 rwork.resize (lrw); |
|
203 |
|
204 info(0) = 0; |
|
205 |
|
206 if (stop_time_set) |
|
207 { |
|
208 info(3) = 1; |
|
209 rwork(0) = stop_time; |
|
210 } |
3990
|
211 else |
4049
|
212 info(3) = 0; |
3990
|
213 |
3994
|
214 px = x.fortran_vec (); |
|
215 pxdot = xdot.fortran_vec (); |
|
216 |
4049
|
217 piwork = iwork.fortran_vec (); |
|
218 prwork = rwork.fortran_vec (); |
|
219 |
|
220 restart = false; |
3994
|
221 |
4049
|
222 // DAEFunc |
|
223 |
|
224 user_fsub = DAEFunc::function (); |
|
225 user_jsub = DAEFunc::jacobian_function (); |
|
226 |
|
227 if (user_fsub) |
3990
|
228 { |
|
229 int ires = 0; |
|
230 |
3994
|
231 ColumnVector fval = (*user_fsub) (x, xdot, t, ires); |
3990
|
232 |
|
233 if (fval.length () != x.length ()) |
|
234 { |
|
235 (*current_liboctave_error_handler) |
4049
|
236 ("dasrt: inconsistent sizes for state and residual vectors"); |
3990
|
237 |
|
238 integration_error = true; |
|
239 return; |
|
240 } |
4049
|
241 } |
|
242 else |
|
243 { |
|
244 (*current_liboctave_error_handler) |
|
245 ("dasrt: no user supplied RHS subroutine!"); |
3990
|
246 |
4049
|
247 integration_error = true; |
|
248 return; |
|
249 } |
|
250 |
|
251 info(4) = user_jsub ? 1 : 0; |
|
252 |
|
253 DAEFunc::reset = false; |
|
254 |
4625
|
255 jroot.resize (ng, 1); |
4049
|
256 |
|
257 pjroot = jroot.fortran_vec (); |
|
258 |
|
259 DAERTFunc::reset = false; |
|
260 |
|
261 // DASRT_options |
|
262 |
4050
|
263 double mss = maximum_step_size (); |
|
264 if (mss >= 0.0) |
4049
|
265 { |
4050
|
266 rwork(1) = mss; |
4049
|
267 info(6) = 1; |
|
268 } |
|
269 else |
|
270 info(6) = 0; |
3990
|
271 |
4050
|
272 double iss = initial_step_size (); |
|
273 if (iss >= 0.0) |
4049
|
274 { |
4050
|
275 rwork(2) = iss; |
4049
|
276 info(7) = 1; |
|
277 } |
|
278 else |
|
279 info(7) = 0; |
|
280 |
|
281 if (step_limit () >= 0) |
|
282 { |
|
283 info(11) = 1; |
4428
|
284 iwork(20) = step_limit (); |
4049
|
285 } |
|
286 else |
|
287 info(11) = 0; |
3990
|
288 |
|
289 abs_tol = absolute_tolerance (); |
|
290 rel_tol = relative_tolerance (); |
|
291 |
3998
|
292 int abs_tol_len = abs_tol.length (); |
|
293 int rel_tol_len = rel_tol.length (); |
|
294 |
|
295 if (abs_tol_len == 1 && rel_tol_len == 1) |
|
296 { |
|
297 info.elem (1) = 0; |
|
298 } |
|
299 else if (abs_tol_len == n && rel_tol_len == n) |
|
300 { |
|
301 info.elem (1) = 1; |
|
302 } |
|
303 else |
|
304 { |
|
305 (*current_liboctave_error_handler) |
4049
|
306 ("dasrt: inconsistent sizes for tolerance arrays"); |
3998
|
307 |
|
308 integration_error = true; |
|
309 return; |
|
310 } |
|
311 |
|
312 pabs_tol = abs_tol.fortran_vec (); |
|
313 prel_tol = rel_tol.fortran_vec (); |
3990
|
314 |
4049
|
315 DASRT_options::reset = false; |
3990
|
316 } |
|
317 |
4049
|
318 static double *dummy = 0; |
|
319 static int *idummy = 0; |
3990
|
320 |
4575
|
321 F77_XFCN (ddasrt, DDASRT, (ddasrt_f, nn, t, px, pxdot, tout, pinfo, |
|
322 prel_tol, pabs_tol, istate, prwork, lrw, |
|
323 piwork, liw, dummy, idummy, ddasrt_j, |
|
324 ddasrt_g, ng, pjroot)); |
3990
|
325 |
|
326 if (f77_exception_encountered) |
|
327 { |
|
328 integration_error = true; |
4049
|
329 (*current_liboctave_error_handler) ("unrecoverable error in dasrt"); |
3990
|
330 } |
|
331 else |
|
332 { |
3997
|
333 switch (istate) |
3990
|
334 { |
|
335 case 1: // A step was successfully taken in intermediate-output |
|
336 // mode. The code has not yet reached TOUT. |
|
337 case 2: // The integration to TOUT was successfully completed |
|
338 // (T=TOUT) by stepping exactly to TOUT. |
|
339 case 3: // The integration to TOUT was successfully completed |
|
340 // (T=TOUT) by stepping past TOUT. Y(*) is obtained by |
|
341 // interpolation. YPRIME(*) is obtained by interpolation. |
|
342 t = tout; |
|
343 break; |
|
344 |
3996
|
345 case 4: // The integration was successfully completed |
|
346 // by finding one or more roots of G at T. |
3990
|
347 break; |
|
348 |
3996
|
349 case -1: // A large amount of work has been expended. |
3990
|
350 case -2: // The error tolerances are too stringent. |
|
351 case -3: // The local error test cannot be satisfied because you |
|
352 // specified a zero component in ATOL and the |
|
353 // corresponding computed solution component is zero. |
|
354 // Thus, a pure relative error test is impossible for |
|
355 // this component. |
|
356 case -6: // DDASRT had repeated error test failures on the last |
|
357 // attempted step. |
|
358 case -7: // The corrector could not converge. |
|
359 case -8: // The matrix of partial derivatives is singular. |
|
360 case -9: // The corrector could not converge. There were repeated |
|
361 // error test failures in this step. |
|
362 case -10: // The corrector could not converge because IRES was |
|
363 // equal to minus one. |
|
364 case -11: // IRES equal to -2 was encountered and control is being |
|
365 // returned to the calling program. |
|
366 case -12: // DASSL failed to compute the initial YPRIME. |
|
367 case -33: // The code has encountered trouble from which it cannot |
|
368 // recover. A message is printed explaining the trouble |
|
369 // and control is returned to the calling program. For |
|
370 // example, this occurs when invalid input is detected. |
3996
|
371 integration_error = true; |
|
372 break; |
|
373 |
3990
|
374 default: |
|
375 integration_error = true; |
|
376 (*current_liboctave_error_handler) |
3997
|
377 ("unrecognized value of istate (= %d) returned from ddasrt", |
|
378 istate); |
3990
|
379 break; |
|
380 } |
|
381 } |
|
382 } |
|
383 |
|
384 DASRT_result |
|
385 DASRT::integrate (const ColumnVector& tout) |
|
386 { |
|
387 DASRT_result retval; |
|
388 |
|
389 Matrix x_out; |
|
390 Matrix xdot_out; |
3994
|
391 ColumnVector t_out = tout; |
3990
|
392 |
|
393 int n_out = tout.capacity (); |
4049
|
394 int n = size (); |
3990
|
395 |
|
396 if (n_out > 0 && n > 0) |
|
397 { |
|
398 x_out.resize (n_out, n); |
|
399 xdot_out.resize (n_out, n); |
|
400 |
3994
|
401 for (int i = 0; i < n; i++) |
|
402 { |
|
403 x_out(0,i) = x(i); |
|
404 xdot_out(0,i) = xdot(i); |
|
405 } |
|
406 |
|
407 for (int j = 1; j < n_out; j++) |
3990
|
408 { |
|
409 integrate (tout(j)); |
3994
|
410 |
3990
|
411 if (integration_error) |
|
412 { |
|
413 retval = DASRT_result (x_out, xdot_out, t_out); |
|
414 return retval; |
|
415 } |
3992
|
416 |
3997
|
417 if (istate == 4) |
3990
|
418 t_out(j) = t; |
|
419 else |
|
420 t_out(j) = tout(j); |
|
421 |
|
422 for (int i = 0; i < n; i++) |
|
423 { |
3994
|
424 x_out(j,i) = x(i); |
|
425 xdot_out(j,i) = xdot(i); |
3990
|
426 } |
3992
|
427 |
3997
|
428 if (istate == 4) |
3992
|
429 { |
3994
|
430 x_out.resize (j+1, n); |
|
431 xdot_out.resize (j+1, n); |
|
432 t_out.resize (j+1); |
|
433 break; |
3992
|
434 } |
3990
|
435 } |
|
436 } |
|
437 |
|
438 retval = DASRT_result (x_out, xdot_out, t_out); |
|
439 |
|
440 return retval; |
|
441 } |
|
442 |
|
443 DASRT_result |
|
444 DASRT::integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
|
445 { |
|
446 DASRT_result retval; |
|
447 |
|
448 Matrix x_out; |
|
449 Matrix xdot_out; |
3994
|
450 ColumnVector t_outs = tout; |
3990
|
451 |
|
452 int n_out = tout.capacity (); |
4049
|
453 int n = size (); |
3990
|
454 |
|
455 if (n_out > 0 && n > 0) |
|
456 { |
|
457 x_out.resize (n_out, n); |
|
458 xdot_out.resize (n_out, n); |
|
459 |
|
460 int n_crit = tcrit.capacity (); |
|
461 |
|
462 if (n_crit > 0) |
|
463 { |
|
464 int i_crit = 0; |
3994
|
465 int i_out = 1; |
3990
|
466 double next_crit = tcrit(0); |
|
467 double next_out; |
|
468 while (i_out < n_out) |
|
469 { |
|
470 bool do_restart = false; |
|
471 |
|
472 next_out = tout(i_out); |
|
473 if (i_crit < n_crit) |
|
474 next_crit = tcrit(i_crit); |
|
475 |
|
476 int save_output; |
|
477 double t_out; |
|
478 |
|
479 if (next_crit == next_out) |
|
480 { |
|
481 set_stop_time (next_crit); |
|
482 t_out = next_out; |
|
483 save_output = 1; |
|
484 i_out++; |
|
485 i_crit++; |
|
486 do_restart = true; |
|
487 } |
|
488 else if (next_crit < next_out) |
|
489 { |
|
490 if (i_crit < n_crit) |
|
491 { |
|
492 set_stop_time (next_crit); |
|
493 t_out = next_crit; |
|
494 save_output = 0; |
|
495 i_crit++; |
|
496 do_restart = true; |
|
497 } |
|
498 else |
|
499 { |
|
500 clear_stop_time (); |
|
501 t_out = next_out; |
|
502 save_output = 1; |
|
503 i_out++; |
|
504 } |
|
505 } |
|
506 else |
|
507 { |
|
508 set_stop_time (next_crit); |
|
509 t_out = next_out; |
|
510 save_output = 1; |
|
511 i_out++; |
|
512 } |
|
513 |
|
514 integrate (t_out); |
|
515 |
|
516 if (integration_error) |
|
517 { |
|
518 retval = DASRT_result (x_out, xdot_out, t_outs); |
|
519 return retval; |
|
520 } |
3992
|
521 |
3997
|
522 if (istate == 4) |
3990
|
523 t_out = t; |
|
524 |
|
525 if (save_output) |
|
526 { |
|
527 for (int i = 0; i < n; i++) |
|
528 { |
3994
|
529 x_out(i_out-1,i) = x(i); |
|
530 xdot_out(i_out-1,i) = xdot(i); |
3990
|
531 } |
3994
|
532 |
3990
|
533 t_outs(i_out-1) = t_out; |
3994
|
534 |
3997
|
535 if (istate == 4) |
3990
|
536 { |
|
537 x_out.resize (i_out, n); |
|
538 xdot_out.resize (i_out, n); |
|
539 t_outs.resize (i_out); |
|
540 i_out = n_out; |
|
541 } |
|
542 } |
|
543 |
|
544 if (do_restart) |
|
545 force_restart (); |
|
546 } |
|
547 |
|
548 retval = DASRT_result (x_out, xdot_out, t_outs); |
|
549 } |
|
550 else |
|
551 { |
|
552 retval = integrate (tout); |
|
553 |
|
554 if (integration_error) |
|
555 return retval; |
|
556 } |
|
557 } |
|
558 |
|
559 return retval; |
|
560 } |
|
561 |
3995
|
562 std::string |
|
563 DASRT::error_message (void) const |
|
564 { |
|
565 std::string retval; |
|
566 |
4051
|
567 OSSTREAM buf; |
|
568 buf << t << OSSTREAM_ENDS; |
|
569 std::string t_curr = OSSTREAM_STR (buf); |
|
570 OSSTREAM_FREEZE (buf); |
4043
|
571 |
3997
|
572 switch (istate) |
3995
|
573 { |
3996
|
574 case 1: |
|
575 retval = "a step was successfully taken in intermediate-output mode."; |
|
576 break; |
|
577 |
|
578 case 2: |
|
579 retval = "integration completed by stepping exactly to TOUT"; |
|
580 break; |
|
581 |
|
582 case 3: |
|
583 retval = "integration to tout completed by stepping past TOUT"; |
|
584 break; |
|
585 |
|
586 case 4: |
|
587 retval = "integration completed by finding one or more roots of G at T"; |
|
588 break; |
|
589 |
|
590 case -1: |
4043
|
591 retval = std::string ("a large amount of work has been expended (t =") |
|
592 + t_curr + ")"; |
3996
|
593 break; |
|
594 |
|
595 case -2: |
|
596 retval = "the error tolerances are too stringent"; |
|
597 break; |
|
598 |
|
599 case -3: |
4043
|
600 retval = std::string ("error weight became zero during problem. (t = ") |
|
601 + t_curr |
|
602 + "; solution component i vanished, and atol or atol(i) == 0)"; |
3996
|
603 break; |
|
604 |
|
605 case -6: |
4043
|
606 retval = std::string ("repeated error test failures on the last attempted step (t = ") |
|
607 + t_curr + ")"; |
3996
|
608 break; |
|
609 |
|
610 case -7: |
4043
|
611 retval = std::string ("the corrector could not converge (t = ") |
|
612 + t_curr + ")"; |
3996
|
613 break; |
|
614 |
|
615 case -8: |
4043
|
616 retval = std::string ("the matrix of partial derivatives is singular (t = ") |
|
617 + t_curr + ")"; |
3996
|
618 break; |
|
619 |
|
620 case -9: |
4043
|
621 retval = std::string ("the corrector could not converge (t = ") |
|
622 + t_curr + "; repeated test failures)"; |
3996
|
623 break; |
|
624 |
|
625 case -10: |
4043
|
626 retval = std::string ("corrector could not converge because IRES was -1 (t = ") |
|
627 + t_curr + ")"; |
3996
|
628 break; |
|
629 |
|
630 case -11: |
4043
|
631 retval = std::string ("return requested in user-supplied function (t = ") |
|
632 + t_curr + ")"; |
3996
|
633 break; |
|
634 |
|
635 case -12: |
|
636 retval = "failed to compute consistent initial conditions"; |
|
637 break; |
|
638 |
|
639 case -33: |
|
640 retval = "unrecoverable error (see printed message)"; |
|
641 break; |
|
642 |
3995
|
643 default: |
|
644 retval = "unknown error state"; |
|
645 break; |
|
646 } |
|
647 |
|
648 return retval; |
|
649 } |
|
650 |
3990
|
651 /* |
|
652 ;;; Local Variables: *** |
|
653 ;;; mode: C++ *** |
|
654 ;;; End: *** |
|
655 */ |