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