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