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 restart = false; |
|
151 |
|
152 stop_time_set = false; |
|
153 stop_time = 0.0; |
|
154 |
|
155 sanity_checked = false; |
|
156 |
|
157 info.resize (30, 0); |
|
158 |
3992
|
159 ng = 0; |
3990
|
160 |
|
161 liw = 0; |
|
162 lrw = 0; |
|
163 } |
|
164 |
3994
|
165 DASRT::DASRT (const ColumnVector& state, double time, DAERTFunc& f) |
|
166 : DAERT (state, time, f) |
|
167 { |
|
168 n = size (); |
|
169 |
|
170 initialized = false; |
|
171 restart = false; |
|
172 |
|
173 stop_time_set = false; |
|
174 stop_time = 0.0; |
|
175 |
|
176 liw = 20 + n; |
|
177 lrw = 50 + 9*n + n*n; |
|
178 |
|
179 sanity_checked = false; |
|
180 |
|
181 info.resize (15, 0); |
|
182 |
|
183 DAERTFunc::DAERTConstrFunc tmp_csub = DAERTFunc::constraint_function (); |
|
184 |
|
185 if (tmp_csub) |
|
186 { |
|
187 ColumnVector tmp = tmp_csub (state, time); |
|
188 ng = tmp.length (); |
|
189 } |
|
190 else |
|
191 ng = 0; |
|
192 |
|
193 jroot.resize (ng, 1); |
|
194 } |
|
195 |
3992
|
196 DASRT::DASRT (const ColumnVector& state, const ColumnVector& deriv, |
|
197 double time, DAERTFunc& f) |
3990
|
198 : DAERT (state, deriv, time, f) |
|
199 { |
|
200 n = size (); |
|
201 |
|
202 initialized = false; |
|
203 restart = false; |
|
204 |
|
205 stop_time_set = false; |
|
206 stop_time = 0.0; |
|
207 |
|
208 sanity_checked = false; |
|
209 |
|
210 info.resize (30, 0); |
|
211 |
3992
|
212 DAERTFunc::DAERTConstrFunc tmp_csub = DAERTFunc::constraint_function (); |
|
213 |
|
214 if (tmp_csub) |
|
215 { |
|
216 ColumnVector tmp = tmp_csub (state, time); |
|
217 ng = tmp.length (); |
|
218 } |
|
219 else |
|
220 ng = 0; |
|
221 |
3994
|
222 liw = 20 + n + 1000; |
|
223 lrw = 50 + 9*n + n*n + 1000; |
3990
|
224 |
3994
|
225 jroot.resize (ng, 1); |
3990
|
226 } |
|
227 |
|
228 void |
|
229 DASRT::force_restart (void) |
|
230 { |
|
231 restart = true; |
|
232 integration_error = false; |
|
233 } |
|
234 |
|
235 void |
|
236 DASRT::set_stop_time (double t) |
|
237 { |
|
238 stop_time_set = true; |
|
239 stop_time = t; |
|
240 } |
|
241 |
|
242 void |
|
243 DASRT::clear_stop_time (void) |
|
244 { |
|
245 stop_time_set = false; |
|
246 } |
|
247 |
|
248 void |
|
249 DASRT::integrate (double tout) |
|
250 { |
|
251 DASRT_result retval; |
|
252 |
|
253 if (! initialized) |
|
254 { |
|
255 info(0) = 0; |
|
256 |
|
257 integration_error = false; |
|
258 |
|
259 user_fsub = DAEFunc::function (); |
|
260 user_jsub = DAEFunc::jacobian_function (); |
|
261 user_csub = DAERTFunc::constraint_function (); |
|
262 |
|
263 if (user_jsub) |
|
264 info(4) = 1; |
|
265 else |
|
266 info(4) = 0; |
|
267 |
3994
|
268 px = x.fortran_vec (); |
|
269 pxdot = xdot.fortran_vec (); |
|
270 |
|
271 nn = n; |
|
272 |
3990
|
273 if (! sanity_checked) |
|
274 { |
|
275 int ires = 0; |
|
276 |
3994
|
277 ColumnVector fval = (*user_fsub) (x, xdot, t, ires); |
3990
|
278 |
|
279 if (fval.length () != x.length ()) |
|
280 { |
|
281 (*current_liboctave_error_handler) |
|
282 ("dassl: inconsistent sizes for state and residual vectors"); |
|
283 |
|
284 integration_error = true; |
|
285 return; |
|
286 } |
|
287 |
|
288 sanity_checked = true; |
|
289 } |
|
290 |
|
291 if (iwork.length () != liw) |
|
292 iwork.resize (liw); |
|
293 |
|
294 if (rwork.length () != lrw) |
|
295 rwork.resize (lrw); |
|
296 |
|
297 abs_tol = absolute_tolerance (); |
|
298 rel_tol = relative_tolerance (); |
|
299 |
|
300 if (initial_step_size () >= 0.0) |
|
301 { |
|
302 rwork(2) = initial_step_size (); |
|
303 info(7) = 1; |
|
304 } |
|
305 else |
|
306 info(7) = 0; |
|
307 |
|
308 if (step_limit () >= 0) |
|
309 { |
|
310 info(11) = 1; |
|
311 iwork(18) = step_limit (); |
|
312 } |
|
313 else |
|
314 info(11) = 0; |
|
315 |
|
316 if (maximum_step_size () >= 0.0) |
|
317 { |
|
318 rwork(1) = maximum_step_size (); |
|
319 info(6) = 1; |
|
320 } |
|
321 else |
|
322 info(6) = 0; |
|
323 |
|
324 pinfo = info.fortran_vec (); |
|
325 piwork = iwork.fortran_vec (); |
|
326 prwork = rwork.fortran_vec (); |
|
327 pjroot = jroot.fortran_vec (); |
|
328 |
|
329 info(5) = 0; |
|
330 info(8) = 0; |
|
331 initialized = true; |
|
332 } |
|
333 |
|
334 if (restart) |
|
335 { |
|
336 info(0) = 0; |
|
337 |
|
338 if (stop_time_set) |
|
339 { |
|
340 info(3) = 1; |
|
341 rwork(0) = stop_time; |
|
342 } |
|
343 else |
|
344 info(3) = 0; |
|
345 } |
|
346 |
3994
|
347 double *dummy = 0; |
|
348 int *idummy = 0; |
|
349 |
|
350 F77_XFCN (ddasrt, DASRT, (ddasrt_f, n, t, px, pxdot, tout, pinfo, |
3990
|
351 &rel_tol, &abs_tol, idid, prwork, lrw, |
3994
|
352 piwork, liw, dummy, idummy, ddasrt_j, |
3990
|
353 ddasrt_g, ng, pjroot)); |
|
354 |
|
355 if (f77_exception_encountered) |
|
356 { |
|
357 integration_error = true; |
|
358 (*current_liboctave_error_handler) ("unrecoverable error in dassl"); |
|
359 } |
|
360 else |
|
361 { |
|
362 switch (idid) |
|
363 { |
|
364 case 0: // Initial conditions made consistent. |
|
365 case 1: // A step was successfully taken in intermediate-output |
|
366 // mode. The code has not yet reached TOUT. |
|
367 case 2: // The integration to TOUT was successfully completed |
|
368 // (T=TOUT) by stepping exactly to TOUT. |
|
369 case 3: // The integration to TOUT was successfully completed |
|
370 // (T=TOUT) by stepping past TOUT. Y(*) is obtained by |
|
371 // interpolation. YPRIME(*) is obtained by interpolation. |
|
372 case 5: // The integration to TSTOP was successfully completed |
|
373 // (T=TSTOP) by stepping to TSTOP within the |
|
374 // tolerance. Must restart to continue. |
|
375 t = tout; |
|
376 break; |
|
377 |
|
378 case 4: // We've hit the stopping condition. |
|
379 break; |
|
380 |
|
381 case -1: // A large amount of work has been expended. (~500 steps). |
|
382 case -2: // The error tolerances are too stringent. |
|
383 case -3: // The local error test cannot be satisfied because you |
|
384 // specified a zero component in ATOL and the |
|
385 // corresponding computed solution component is zero. |
|
386 // Thus, a pure relative error test is impossible for |
|
387 // this component. |
|
388 case -6: // DDASRT had repeated error test failures on the last |
|
389 // attempted step. |
|
390 case -7: // The corrector could not converge. |
|
391 case -8: // The matrix of partial derivatives is singular. |
|
392 case -9: // The corrector could not converge. There were repeated |
|
393 // error test failures in this step. |
|
394 case -10: // The corrector could not converge because IRES was |
|
395 // equal to minus one. |
|
396 case -11: // IRES equal to -2 was encountered and control is being |
|
397 // returned to the calling program. |
|
398 case -12: // DASSL failed to compute the initial YPRIME. |
|
399 case -33: // The code has encountered trouble from which it cannot |
|
400 // recover. A message is printed explaining the trouble |
|
401 // and control is returned to the calling program. For |
|
402 // example, this occurs when invalid input is detected. |
|
403 default: |
|
404 integration_error = true; |
|
405 (*current_liboctave_error_handler) |
|
406 ("ddasrt failed with IDID = %d", idid); |
|
407 break; |
|
408 } |
|
409 } |
|
410 } |
|
411 |
|
412 DASRT_result |
|
413 DASRT::integrate (const ColumnVector& tout) |
|
414 { |
|
415 DASRT_result retval; |
|
416 |
|
417 Matrix x_out; |
|
418 Matrix xdot_out; |
3994
|
419 ColumnVector t_out = tout; |
3990
|
420 |
|
421 int oldj = 0; |
|
422 |
|
423 int n_out = tout.capacity (); |
|
424 |
|
425 if (n_out > 0 && n > 0) |
|
426 { |
|
427 x_out.resize (n_out, n); |
|
428 xdot_out.resize (n_out, n); |
|
429 |
3994
|
430 for (int i = 0; i < n; i++) |
|
431 { |
|
432 x_out(0,i) = x(i); |
|
433 xdot_out(0,i) = xdot(i); |
|
434 } |
|
435 |
|
436 for (int j = 1; j < n_out; j++) |
3990
|
437 { |
|
438 integrate (tout(j)); |
3994
|
439 |
3990
|
440 if (integration_error) |
|
441 { |
|
442 retval = DASRT_result (x_out, xdot_out, t_out); |
|
443 return retval; |
|
444 } |
3992
|
445 |
3990
|
446 if (idid == 4) |
|
447 t_out(j) = t; |
|
448 else |
|
449 t_out(j) = tout(j); |
|
450 |
|
451 for (int i = 0; i < n; i++) |
|
452 { |
3994
|
453 x_out(j,i) = x(i); |
|
454 xdot_out(j,i) = xdot(i); |
3990
|
455 } |
3992
|
456 |
|
457 if (idid == 4) |
|
458 { |
3994
|
459 x_out.resize (j+1, n); |
|
460 xdot_out.resize (j+1, n); |
|
461 t_out.resize (j+1); |
|
462 break; |
3992
|
463 } |
3990
|
464 } |
|
465 } |
|
466 |
|
467 retval = DASRT_result (x_out, xdot_out, t_out); |
|
468 |
|
469 return retval; |
|
470 } |
|
471 |
|
472 DASRT_result |
|
473 DASRT::integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
|
474 { |
|
475 DASRT_result retval; |
|
476 |
|
477 Matrix x_out; |
|
478 Matrix xdot_out; |
3994
|
479 ColumnVector t_outs = tout; |
3990
|
480 |
|
481 int n_out = tout.capacity (); |
|
482 |
|
483 if (n_out > 0 && n > 0) |
|
484 { |
|
485 x_out.resize (n_out, n); |
|
486 xdot_out.resize (n_out, n); |
|
487 |
|
488 int n_crit = tcrit.capacity (); |
|
489 |
|
490 if (n_crit > 0) |
|
491 { |
|
492 int i_crit = 0; |
3994
|
493 int i_out = 1; |
3990
|
494 double next_crit = tcrit(0); |
|
495 double next_out; |
|
496 while (i_out < n_out) |
|
497 { |
|
498 bool do_restart = false; |
|
499 |
|
500 next_out = tout(i_out); |
|
501 if (i_crit < n_crit) |
|
502 next_crit = tcrit(i_crit); |
|
503 |
|
504 int save_output; |
|
505 double t_out; |
|
506 |
|
507 if (next_crit == next_out) |
|
508 { |
|
509 set_stop_time (next_crit); |
|
510 t_out = next_out; |
|
511 save_output = 1; |
|
512 i_out++; |
|
513 i_crit++; |
|
514 do_restart = true; |
|
515 } |
|
516 else if (next_crit < next_out) |
|
517 { |
|
518 if (i_crit < n_crit) |
|
519 { |
|
520 set_stop_time (next_crit); |
|
521 t_out = next_crit; |
|
522 save_output = 0; |
|
523 i_crit++; |
|
524 do_restart = true; |
|
525 } |
|
526 else |
|
527 { |
|
528 clear_stop_time (); |
|
529 t_out = next_out; |
|
530 save_output = 1; |
|
531 i_out++; |
|
532 } |
|
533 } |
|
534 else |
|
535 { |
|
536 set_stop_time (next_crit); |
|
537 t_out = next_out; |
|
538 save_output = 1; |
|
539 i_out++; |
|
540 } |
|
541 |
|
542 integrate (t_out); |
|
543 |
|
544 if (integration_error) |
|
545 { |
|
546 retval = DASRT_result (x_out, xdot_out, t_outs); |
|
547 return retval; |
|
548 } |
3992
|
549 |
3990
|
550 if (idid == 4) |
|
551 t_out = t; |
|
552 |
|
553 if (save_output) |
|
554 { |
|
555 for (int i = 0; i < n; i++) |
|
556 { |
3994
|
557 x_out(i_out-1,i) = x(i); |
|
558 xdot_out(i_out-1,i) = xdot(i); |
3990
|
559 } |
3994
|
560 |
3990
|
561 t_outs(i_out-1) = t_out; |
3994
|
562 |
|
563 if (idid == 4) |
3990
|
564 { |
|
565 x_out.resize (i_out, n); |
|
566 xdot_out.resize (i_out, n); |
|
567 t_outs.resize (i_out); |
|
568 i_out = n_out; |
|
569 } |
|
570 } |
|
571 |
|
572 if (do_restart) |
|
573 force_restart (); |
|
574 } |
|
575 |
|
576 retval = DASRT_result (x_out, xdot_out, t_outs); |
|
577 } |
|
578 else |
|
579 { |
|
580 retval = integrate (tout); |
|
581 |
|
582 if (integration_error) |
|
583 return retval; |
|
584 } |
|
585 } |
|
586 |
|
587 return retval; |
|
588 } |
|
589 |
|
590 /* |
|
591 ;;; Local Variables: *** |
|
592 ;;; mode: C++ *** |
|
593 ;;; End: *** |
|
594 */ |