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