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