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