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