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 (); |
|
175 } |
|
176 |
|
177 int |
|
178 ddaspk_j (const double& time, const double *, const double *, double *pd, |
|
179 const double& cj, double *, int *) |
|
180 { |
|
181 ColumnVector tmp_state (nn); |
|
182 ColumnVector tmp_deriv (nn); |
|
183 |
|
184 // XXX FIXME XXX |
|
185 |
|
186 Matrix tmp_dfdxdot (nn, nn); |
|
187 Matrix tmp_dfdx (nn, nn); |
|
188 |
|
189 DAEFunc::DAEJac tmp_jac; |
|
190 tmp_jac.dfdxdot = &tmp_dfdxdot; |
|
191 tmp_jac.dfdx = &tmp_dfdx; |
|
192 |
|
193 tmp_jac = user_jac (tmp_state, tmp_deriv, time); |
|
194 |
|
195 // Fix up the matrix of partial derivatives for daspk. |
|
196 |
|
197 tmp_dfdx = tmp_dfdx + cj * tmp_dfdxdot; |
|
198 |
|
199 for (int j = 0; j < nn; j++) |
|
200 for (int i = 0; i < nn; i++) |
|
201 pd [nn * j + i] = tmp_dfdx.elem (i, j); |
|
202 |
|
203 return 0; |
|
204 } |
|
205 |
|
206 ColumnVector |
|
207 DASPK::do_integrate (double tout) |
|
208 { |
|
209 ColumnVector retval; |
|
210 |
|
211 if (restart) |
|
212 { |
|
213 restart = 0; |
|
214 info.elem (0) = 0; |
|
215 } |
|
216 |
|
217 liw = 40 + n; |
|
218 if (info(9) == 1 || info(9) == 3) |
|
219 liw += n; |
|
220 if (info (10) == 1 || info(15) == 1) |
|
221 liw += n; |
|
222 |
|
223 lrw = 50 + 9*n; |
|
224 if (info(5) == 0) |
|
225 lrw += n*n; |
|
226 if (info(15) == 1) |
|
227 lrw += n; |
|
228 |
|
229 if (iwork.length () != liw) |
|
230 iwork.resize (liw); |
|
231 |
|
232 if (rwork.length () != lrw) |
|
233 rwork.resize (lrw); |
|
234 |
|
235 integration_error = 0; |
|
236 |
|
237 if (DAEFunc::jacobian_function ()) |
|
238 info.elem (4) = 1; |
|
239 else |
|
240 info.elem (4) = 0; |
|
241 |
|
242 double *px = x.fortran_vec (); |
|
243 double *pxdot = xdot.fortran_vec (); |
|
244 |
|
245 nn = n; |
|
246 user_fun = DAEFunc::fun; |
|
247 user_jac = DAEFunc::jac; |
|
248 |
|
249 if (! sanity_checked) |
|
250 { |
|
251 int ires = 0; |
|
252 |
|
253 ColumnVector res = (*user_fun) (x, xdot, t, ires); |
|
254 |
|
255 if (res.length () != x.length ()) |
|
256 { |
|
257 (*current_liboctave_error_handler) |
|
258 ("daspk: inconsistent sizes for state and residual vectors"); |
|
259 |
|
260 integration_error = 1; |
|
261 return retval; |
|
262 } |
|
263 |
|
264 sanity_checked = 1; |
|
265 } |
|
266 |
|
267 if (stop_time_set) |
|
268 { |
|
269 rwork.elem (0) = stop_time; |
|
270 info.elem (3) = 1; |
|
271 } |
|
272 else |
|
273 info.elem (3) = 0; |
|
274 |
|
275 double abs_tol = absolute_tolerance (); |
|
276 double rel_tol = relative_tolerance (); |
|
277 |
|
278 if (initial_step_size () >= 0.0) |
|
279 { |
|
280 rwork.elem (2) = initial_step_size (); |
|
281 info.elem (7) = 1; |
|
282 } |
|
283 else |
|
284 info.elem (7) = 0; |
|
285 |
|
286 if (maximum_step_size () >= 0.0) |
|
287 { |
|
288 rwork.elem (1) = maximum_step_size (); |
|
289 info.elem (6) = 1; |
|
290 } |
|
291 else |
|
292 info.elem (6) = 0; |
|
293 |
|
294 double *dummy = 0; |
|
295 int *idummy = 0; |
|
296 |
|
297 int *pinfo = info.fortran_vec (); |
|
298 int *piwork = iwork.fortran_vec (); |
|
299 double *prwork = rwork.fortran_vec (); |
|
300 |
|
301 // again: |
|
302 |
|
303 F77_XFCN (ddaspk, DDASPK, (ddaspk_f, n, t, px, pxdot, tout, pinfo, |
|
304 rel_tol, abs_tol, idid, prwork, lrw, |
|
305 piwork, liw, dummy, idummy, ddaspk_j, |
|
306 ddaspk_psol)); |
|
307 |
|
308 if (f77_exception_encountered) |
|
309 { |
|
310 integration_error = 1; |
|
311 (*current_liboctave_error_handler) ("unrecoverable error in daspk"); |
|
312 } |
|
313 else |
|
314 { |
|
315 switch (idid) |
|
316 { |
|
317 case 1: // A step was successfully taken in intermediate-output |
|
318 // mode. The code has not yet reached TOUT. |
|
319 case 2: // The integration to TSTOP was successfully completed |
|
320 // (T=TSTOP) by stepping exactly to TSTOP. |
|
321 case 3: // The integration to TOUT was successfully completed |
|
322 // (T=TOUT) by stepping past TOUT. Y(*) is obtained by |
|
323 // interpolation. YPRIME(*) is obtained by interpolation. |
|
324 |
|
325 retval = x; |
|
326 t = tout; |
|
327 break; |
|
328 |
|
329 case -1: // A large amount of work has been expended. (~500 steps). |
|
330 case -2: // The error tolerances are too stringent. |
|
331 case -3: // The local error test cannot be satisfied because you |
|
332 // specified a zero component in ATOL and the |
|
333 // corresponding computed solution component is zero. |
|
334 // Thus, a pure relative error test is impossible for |
|
335 // this component. |
|
336 case -6: // DDASPK had repeated error test failures on the last |
|
337 // attempted step. |
|
338 case -7: // The corrector could not converge. |
|
339 case -8: // The matrix of partial derivatives is singular. |
|
340 case -9: // The corrector could not converge. There were repeated |
|
341 // error test failures in this step. |
|
342 case -10: // The corrector could not converge because IRES was |
|
343 // equal to minus one. |
|
344 case -11: // IRES equal to -2 was encountered and control is being |
|
345 // returned to the calling program. |
|
346 case -12: // DDASPK failed to compute the initial YPRIME. |
|
347 case -33: // The code has encountered trouble from which it cannot |
|
348 // recover. A message is printed explaining the trouble |
|
349 // and control is returned to the calling program. For |
|
350 // example, this occurs when invalid input is detected. |
|
351 default: |
|
352 integration_error = 1; |
|
353 break; |
|
354 } |
|
355 } |
|
356 |
|
357 return retval; |
|
358 } |
|
359 |
|
360 Matrix |
|
361 DASPK::do_integrate (const ColumnVector& tout) |
|
362 { |
|
363 Matrix dummy; |
|
364 return integrate (tout, dummy); |
|
365 } |
|
366 |
|
367 Matrix |
|
368 DASPK::integrate (const ColumnVector& tout, Matrix& xdot_out) |
|
369 { |
|
370 Matrix retval; |
|
371 int n_out = tout.capacity (); |
|
372 |
|
373 if (n_out > 0 && n > 0) |
|
374 { |
|
375 retval.resize (n_out, n); |
|
376 xdot_out.resize (n_out, n); |
|
377 |
|
378 for (int i = 0; i < n; i++) |
|
379 { |
|
380 retval.elem (0, i) = x.elem (i); |
|
381 xdot_out.elem (0, i) = xdot.elem (i); |
|
382 } |
|
383 |
|
384 for (int j = 1; j < n_out; j++) |
|
385 { |
|
386 ColumnVector x_next = do_integrate (tout.elem (j)); |
|
387 |
|
388 if (integration_error) |
|
389 return retval; |
|
390 |
|
391 for (int i = 0; i < n; i++) |
|
392 { |
|
393 retval.elem (j, i) = x_next.elem (i); |
|
394 xdot_out.elem (j, i) = xdot.elem (i); |
|
395 } |
|
396 } |
|
397 } |
|
398 |
|
399 return retval; |
|
400 } |
|
401 |
|
402 Matrix |
|
403 DASPK::do_integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
|
404 { |
|
405 Matrix dummy; |
|
406 return integrate (tout, dummy, tcrit); |
|
407 } |
|
408 |
|
409 Matrix |
|
410 DASPK::integrate (const ColumnVector& tout, Matrix& xdot_out, |
|
411 const ColumnVector& tcrit) |
|
412 { |
|
413 Matrix retval; |
|
414 int n_out = tout.capacity (); |
|
415 |
|
416 if (n_out > 0 && n > 0) |
|
417 { |
|
418 retval.resize (n_out, n); |
|
419 xdot_out.resize (n_out, n); |
|
420 |
|
421 for (int i = 0; i < n; i++) |
|
422 { |
|
423 retval.elem (0, i) = x.elem (i); |
|
424 xdot_out.elem (0, i) = xdot.elem (i); |
|
425 } |
|
426 |
|
427 int n_crit = tcrit.capacity (); |
|
428 |
|
429 if (n_crit > 0) |
|
430 { |
|
431 int i_crit = 0; |
|
432 int i_out = 1; |
|
433 double next_crit = tcrit.elem (0); |
|
434 double next_out; |
|
435 while (i_out < n_out) |
|
436 { |
|
437 bool do_restart = false; |
|
438 |
|
439 next_out = tout.elem (i_out); |
|
440 if (i_crit < n_crit) |
|
441 next_crit = tcrit.elem (i_crit); |
|
442 |
|
443 bool save_output; |
|
444 double t_out; |
|
445 |
|
446 if (next_crit == next_out) |
|
447 { |
|
448 set_stop_time (next_crit); |
|
449 t_out = next_out; |
|
450 save_output = true; |
|
451 i_out++; |
|
452 i_crit++; |
|
453 do_restart = true; |
|
454 } |
|
455 else if (next_crit < next_out) |
|
456 { |
|
457 if (i_crit < n_crit) |
|
458 { |
|
459 set_stop_time (next_crit); |
|
460 t_out = next_crit; |
|
461 save_output = false; |
|
462 i_crit++; |
|
463 do_restart = true; |
|
464 } |
|
465 else |
|
466 { |
|
467 clear_stop_time (); |
|
468 t_out = next_out; |
|
469 save_output = true; |
|
470 i_out++; |
|
471 } |
|
472 } |
|
473 else |
|
474 { |
|
475 set_stop_time (next_crit); |
|
476 t_out = next_out; |
|
477 save_output = true; |
|
478 i_out++; |
|
479 } |
|
480 |
|
481 ColumnVector x_next = do_integrate (t_out); |
|
482 |
|
483 if (integration_error) |
|
484 return retval; |
|
485 |
|
486 if (save_output) |
|
487 { |
|
488 for (int i = 0; i < n; i++) |
|
489 { |
|
490 retval.elem (i_out-1, i) = x_next.elem (i); |
|
491 xdot_out.elem (i_out-1, i) = xdot.elem (i); |
|
492 } |
|
493 } |
|
494 |
|
495 if (do_restart) |
|
496 force_restart (); |
|
497 } |
|
498 } |
|
499 else |
|
500 { |
|
501 retval = integrate (tout, xdot_out); |
|
502 |
|
503 if (integration_error) |
|
504 return retval; |
|
505 } |
|
506 } |
|
507 |
|
508 return retval; |
|
509 } |
|
510 |
|
511 /* |
|
512 ;;; Local Variables: *** |
|
513 ;;; mode: C++ *** |
|
514 ;;; End: *** |
|
515 */ |