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