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