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