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