3912
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997, 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 <cfloat> |
|
32 #include <cmath> |
|
33 |
|
34 #include "DASPK.h" |
|
35 #include "f77-fcn.h" |
|
36 #include "lo-error.h" |
|
37 |
|
38 typedef int (*daspk_fcn_ptr) (const double&, const double*, |
|
39 const double*, const double&, |
|
40 double*, int&, double*, int*); |
|
41 |
|
42 typedef int (*daspk_jac_ptr) (const double&, const double*, |
|
43 const double*, double*, |
|
44 const double&, double*, int*); |
|
45 |
|
46 typedef int (*daspk_psol_ptr) (const int&, const double&, |
|
47 const double*, const double*, |
|
48 const double*, const double&, |
|
49 const double*, double*, int*, |
|
50 double*, const double&, int&, |
|
51 double*, int*); |
|
52 |
|
53 extern "C" |
|
54 int F77_FUNC (ddaspk, DDASPK) (daspk_fcn_ptr, const int&, double&, |
|
55 double*, double*, double&, const int*, |
|
56 const double&, const double&, int&, |
|
57 double*, const int&, int*, const int&, |
|
58 const double*, const int*, |
|
59 daspk_jac_ptr, daspk_psol_ptr); |
|
60 |
|
61 static DAEFunc::DAERHSFunc user_fun; |
|
62 static DAEFunc::DAEJacFunc user_jac; |
|
63 static int nn; |
|
64 |
|
65 DASPK::DASPK (void) : DAE () |
|
66 { |
|
67 stop_time_set = 0; |
|
68 stop_time = 0.0; |
|
69 |
|
70 sanity_checked = 0; |
|
71 |
|
72 info.resize (15); |
|
73 |
|
74 for (int i = 0; i < 15; i++) |
|
75 info.elem (i) = 0; |
|
76 } |
|
77 |
|
78 DASPK::DASPK (const ColumnVector& state, double time, DAEFunc& f) |
|
79 : DAE (state, time, f) |
|
80 { |
|
81 n = size (); |
|
82 |
|
83 stop_time_set = 0; |
|
84 stop_time = 0.0; |
|
85 |
|
86 sanity_checked = 0; |
|
87 |
|
88 info.resize (20); |
|
89 |
|
90 for (int i = 0; i < 20; i++) |
|
91 info.elem (i) = 0; |
|
92 } |
|
93 |
|
94 DASPK::DASPK (const ColumnVector& state, const ColumnVector& deriv, |
|
95 double time, DAEFunc& f) |
|
96 : DAE (state, deriv, time, f) |
|
97 { |
|
98 n = size (); |
|
99 |
|
100 stop_time_set = 0; |
|
101 stop_time = 0.0; |
|
102 |
|
103 DAEFunc::set_function (f.function ()); |
|
104 DAEFunc::set_jacobian_function (f.jacobian_function ()); |
|
105 |
|
106 sanity_checked = 0; |
|
107 |
|
108 info.resize (20); |
|
109 |
|
110 for (int i = 0; i < 20; i++) |
|
111 info.elem (i) = 0; |
|
112 } |
|
113 |
|
114 void |
|
115 DASPK::force_restart (void) |
|
116 { |
|
117 restart = 1; |
|
118 integration_error = 0; |
|
119 } |
|
120 |
|
121 void |
|
122 DASPK::set_stop_time (double tt) |
|
123 { |
|
124 stop_time_set = 1; |
|
125 stop_time = tt; |
|
126 } |
|
127 |
|
128 void |
|
129 DASPK::clear_stop_time (void) |
|
130 { |
|
131 stop_time_set = 0; |
|
132 } |
|
133 |
|
134 int |
|
135 ddaspk_f (const double& time, const double *state, const double *deriv, |
|
136 const double&, double *delta, int& ires, double *, int *) |
|
137 { |
|
138 ColumnVector tmp_deriv (nn); |
|
139 ColumnVector tmp_state (nn); |
|
140 ColumnVector tmp_delta (nn); |
|
141 |
|
142 for (int i = 0; i < nn; i++) |
|
143 { |
|
144 tmp_deriv.elem (i) = deriv [i]; |
|
145 tmp_state.elem (i) = state [i]; |
|
146 } |
|
147 |
|
148 tmp_delta = user_fun (tmp_state, tmp_deriv, time, ires); |
|
149 |
|
150 if (ires >= 0) |
|
151 { |
|
152 if (tmp_delta.length () == 0) |
|
153 ires = -2; |
|
154 else |
|
155 { |
|
156 for (int i = 0; i < nn; i++) |
|
157 delta [i] = tmp_delta.elem (i); |
|
158 } |
|
159 } |
|
160 |
|
161 return 0; |
|
162 } |
|
163 |
|
164 //NEQ, T, Y, YPRIME, SAVR, WK, CJ, WGHT, |
|
165 //C WP, IWP, B, EPLIN, IER, RPAR, IPAR) |
|
166 |
|
167 int |
|
168 ddaspk_psol (const int& neq, const double& time, const double *state, |
|
169 const double *deriv, const double *savr, |
|
170 const double& cj, const double *wght, double *wp, |
|
171 int *iwp, double *b, const double& eplin, int& ier, |
|
172 double *, int*) |
|
173 { |
|
174 abort (); |
3946
|
175 return 0; |
3912
|
176 } |
|
177 |
3991
|
178 |
3912
|
179 int |
3991
|
180 ddaspk_j (const double& time, const double *state, const double *deriv, |
|
181 double *pd, const double& cj, double *, int *) |
3912
|
182 { |
3991
|
183 // XXX FIXME XXX -- would be nice to avoid copying the data. |
|
184 |
3912
|
185 ColumnVector tmp_state (nn); |
|
186 ColumnVector tmp_deriv (nn); |
|
187 |
3991
|
188 for (int i = 0; i < nn; i++) |
|
189 { |
|
190 tmp_deriv.elem (i) = deriv [i]; |
|
191 tmp_state.elem (i) = state [i]; |
|
192 } |
3912
|
193 |
3991
|
194 Matrix tmp_pd = user_jac (tmp_state, tmp_deriv, time, cj); |
3912
|
195 |
|
196 for (int j = 0; j < nn; j++) |
|
197 for (int i = 0; i < nn; i++) |
3991
|
198 pd [nn * j + i] = tmp_pd.elem (i, j); |
3912
|
199 |
|
200 return 0; |
|
201 } |
|
202 |
|
203 ColumnVector |
|
204 DASPK::do_integrate (double tout) |
|
205 { |
|
206 ColumnVector retval; |
|
207 |
|
208 if (restart) |
|
209 { |
|
210 restart = 0; |
|
211 info.elem (0) = 0; |
|
212 } |
|
213 |
|
214 liw = 40 + n; |
|
215 if (info(9) == 1 || info(9) == 3) |
|
216 liw += n; |
|
217 if (info (10) == 1 || info(15) == 1) |
|
218 liw += n; |
|
219 |
|
220 lrw = 50 + 9*n; |
|
221 if (info(5) == 0) |
|
222 lrw += n*n; |
|
223 if (info(15) == 1) |
|
224 lrw += n; |
|
225 |
|
226 if (iwork.length () != liw) |
|
227 iwork.resize (liw); |
|
228 |
|
229 if (rwork.length () != lrw) |
|
230 rwork.resize (lrw); |
|
231 |
|
232 integration_error = 0; |
|
233 |
|
234 if (DAEFunc::jacobian_function ()) |
|
235 info.elem (4) = 1; |
|
236 else |
|
237 info.elem (4) = 0; |
|
238 |
|
239 double *px = x.fortran_vec (); |
|
240 double *pxdot = xdot.fortran_vec (); |
|
241 |
|
242 nn = n; |
|
243 user_fun = DAEFunc::fun; |
|
244 user_jac = DAEFunc::jac; |
|
245 |
|
246 if (! sanity_checked) |
|
247 { |
|
248 int ires = 0; |
|
249 |
|
250 ColumnVector res = (*user_fun) (x, xdot, t, ires); |
|
251 |
|
252 if (res.length () != x.length ()) |
|
253 { |
|
254 (*current_liboctave_error_handler) |
|
255 ("daspk: inconsistent sizes for state and residual vectors"); |
|
256 |
|
257 integration_error = 1; |
|
258 return retval; |
|
259 } |
|
260 |
|
261 sanity_checked = 1; |
|
262 } |
|
263 |
|
264 if (stop_time_set) |
|
265 { |
|
266 rwork.elem (0) = stop_time; |
|
267 info.elem (3) = 1; |
|
268 } |
|
269 else |
|
270 info.elem (3) = 0; |
|
271 |
|
272 double abs_tol = absolute_tolerance (); |
|
273 double rel_tol = relative_tolerance (); |
|
274 |
|
275 if (initial_step_size () >= 0.0) |
|
276 { |
|
277 rwork.elem (2) = initial_step_size (); |
|
278 info.elem (7) = 1; |
|
279 } |
|
280 else |
|
281 info.elem (7) = 0; |
|
282 |
|
283 if (maximum_step_size () >= 0.0) |
|
284 { |
|
285 rwork.elem (1) = maximum_step_size (); |
|
286 info.elem (6) = 1; |
|
287 } |
|
288 else |
|
289 info.elem (6) = 0; |
|
290 |
|
291 double *dummy = 0; |
|
292 int *idummy = 0; |
|
293 |
|
294 int *pinfo = info.fortran_vec (); |
|
295 int *piwork = iwork.fortran_vec (); |
|
296 double *prwork = rwork.fortran_vec (); |
|
297 |
|
298 // again: |
|
299 |
|
300 F77_XFCN (ddaspk, DDASPK, (ddaspk_f, n, t, px, pxdot, tout, pinfo, |
|
301 rel_tol, abs_tol, idid, prwork, lrw, |
|
302 piwork, liw, dummy, idummy, ddaspk_j, |
|
303 ddaspk_psol)); |
|
304 |
|
305 if (f77_exception_encountered) |
|
306 { |
|
307 integration_error = 1; |
|
308 (*current_liboctave_error_handler) ("unrecoverable error in daspk"); |
|
309 } |
|
310 else |
|
311 { |
|
312 switch (idid) |
|
313 { |
|
314 case 1: // A step was successfully taken in intermediate-output |
|
315 // mode. The code has not yet reached TOUT. |
|
316 case 2: // The integration to TSTOP was successfully completed |
|
317 // (T=TSTOP) by stepping exactly to TSTOP. |
|
318 case 3: // The integration to TOUT was successfully completed |
|
319 // (T=TOUT) by stepping past TOUT. Y(*) is obtained by |
|
320 // interpolation. YPRIME(*) is obtained by interpolation. |
|
321 |
|
322 retval = x; |
|
323 t = tout; |
|
324 break; |
|
325 |
|
326 case -1: // A large amount of work has been expended. (~500 steps). |
|
327 case -2: // The error tolerances are too stringent. |
|
328 case -3: // The local error test cannot be satisfied because you |
|
329 // specified a zero component in ATOL and the |
|
330 // corresponding computed solution component is zero. |
|
331 // Thus, a pure relative error test is impossible for |
|
332 // this component. |
|
333 case -6: // DDASPK had repeated error test failures on the last |
|
334 // attempted step. |
|
335 case -7: // The corrector could not converge. |
|
336 case -8: // The matrix of partial derivatives is singular. |
|
337 case -9: // The corrector could not converge. There were repeated |
|
338 // error test failures in this step. |
|
339 case -10: // The corrector could not converge because IRES was |
|
340 // equal to minus one. |
|
341 case -11: // IRES equal to -2 was encountered and control is being |
|
342 // returned to the calling program. |
|
343 case -12: // DDASPK failed to compute the initial YPRIME. |
|
344 case -33: // The code has encountered trouble from which it cannot |
|
345 // recover. A message is printed explaining the trouble |
|
346 // and control is returned to the calling program. For |
|
347 // example, this occurs when invalid input is detected. |
|
348 default: |
|
349 integration_error = 1; |
|
350 break; |
|
351 } |
|
352 } |
|
353 |
|
354 return retval; |
|
355 } |
|
356 |
|
357 Matrix |
|
358 DASPK::do_integrate (const ColumnVector& tout) |
|
359 { |
|
360 Matrix dummy; |
|
361 return integrate (tout, dummy); |
|
362 } |
|
363 |
|
364 Matrix |
|
365 DASPK::integrate (const ColumnVector& tout, Matrix& xdot_out) |
|
366 { |
|
367 Matrix retval; |
|
368 int n_out = tout.capacity (); |
|
369 |
|
370 if (n_out > 0 && n > 0) |
|
371 { |
|
372 retval.resize (n_out, n); |
|
373 xdot_out.resize (n_out, n); |
|
374 |
|
375 for (int i = 0; i < n; i++) |
|
376 { |
|
377 retval.elem (0, i) = x.elem (i); |
|
378 xdot_out.elem (0, i) = xdot.elem (i); |
|
379 } |
|
380 |
|
381 for (int j = 1; j < n_out; j++) |
|
382 { |
|
383 ColumnVector x_next = do_integrate (tout.elem (j)); |
|
384 |
|
385 if (integration_error) |
|
386 return retval; |
|
387 |
|
388 for (int i = 0; i < n; i++) |
|
389 { |
|
390 retval.elem (j, i) = x_next.elem (i); |
|
391 xdot_out.elem (j, i) = xdot.elem (i); |
|
392 } |
|
393 } |
|
394 } |
|
395 |
|
396 return retval; |
|
397 } |
|
398 |
|
399 Matrix |
|
400 DASPK::do_integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
|
401 { |
|
402 Matrix dummy; |
|
403 return integrate (tout, dummy, tcrit); |
|
404 } |
|
405 |
|
406 Matrix |
|
407 DASPK::integrate (const ColumnVector& tout, Matrix& xdot_out, |
|
408 const ColumnVector& tcrit) |
|
409 { |
|
410 Matrix retval; |
|
411 int n_out = tout.capacity (); |
|
412 |
|
413 if (n_out > 0 && n > 0) |
|
414 { |
|
415 retval.resize (n_out, n); |
|
416 xdot_out.resize (n_out, n); |
|
417 |
|
418 for (int i = 0; i < n; i++) |
|
419 { |
|
420 retval.elem (0, i) = x.elem (i); |
|
421 xdot_out.elem (0, i) = xdot.elem (i); |
|
422 } |
|
423 |
|
424 int n_crit = tcrit.capacity (); |
|
425 |
|
426 if (n_crit > 0) |
|
427 { |
|
428 int i_crit = 0; |
|
429 int i_out = 1; |
|
430 double next_crit = tcrit.elem (0); |
|
431 double next_out; |
|
432 while (i_out < n_out) |
|
433 { |
|
434 bool do_restart = false; |
|
435 |
|
436 next_out = tout.elem (i_out); |
|
437 if (i_crit < n_crit) |
|
438 next_crit = tcrit.elem (i_crit); |
|
439 |
|
440 bool save_output; |
|
441 double t_out; |
|
442 |
|
443 if (next_crit == next_out) |
|
444 { |
|
445 set_stop_time (next_crit); |
|
446 t_out = next_out; |
|
447 save_output = true; |
|
448 i_out++; |
|
449 i_crit++; |
|
450 do_restart = true; |
|
451 } |
|
452 else if (next_crit < next_out) |
|
453 { |
|
454 if (i_crit < n_crit) |
|
455 { |
|
456 set_stop_time (next_crit); |
|
457 t_out = next_crit; |
|
458 save_output = false; |
|
459 i_crit++; |
|
460 do_restart = true; |
|
461 } |
|
462 else |
|
463 { |
|
464 clear_stop_time (); |
|
465 t_out = next_out; |
|
466 save_output = true; |
|
467 i_out++; |
|
468 } |
|
469 } |
|
470 else |
|
471 { |
|
472 set_stop_time (next_crit); |
|
473 t_out = next_out; |
|
474 save_output = true; |
|
475 i_out++; |
|
476 } |
|
477 |
|
478 ColumnVector x_next = do_integrate (t_out); |
|
479 |
|
480 if (integration_error) |
|
481 return retval; |
|
482 |
|
483 if (save_output) |
|
484 { |
|
485 for (int i = 0; i < n; i++) |
|
486 { |
|
487 retval.elem (i_out-1, i) = x_next.elem (i); |
|
488 xdot_out.elem (i_out-1, i) = xdot.elem (i); |
|
489 } |
|
490 } |
|
491 |
|
492 if (do_restart) |
|
493 force_restart (); |
|
494 } |
|
495 } |
|
496 else |
|
497 { |
|
498 retval = integrate (tout, xdot_out); |
|
499 |
|
500 if (integration_error) |
|
501 return retval; |
|
502 } |
|
503 } |
|
504 |
|
505 return retval; |
|
506 } |
|
507 |
|
508 /* |
|
509 ;;; Local Variables: *** |
|
510 ;;; mode: C++ *** |
|
511 ;;; End: *** |
|
512 */ |