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