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