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