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