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