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