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