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" |
3507
|
45 int F77_FCN (ddassl, DDASSL) (dassl_fcn_ptr, const int&, double&, |
|
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 |
1842
|
122 DASSL::set_stop_time (double t) |
3
|
123 { |
1842
|
124 stop_time_set = 1; |
|
125 stop_time = t; |
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 |
1251
|
148 tmp_delta = user_fun (tmp_state, tmp_deriv, time); |
3
|
149 |
256
|
150 if (tmp_delta.length () == 0) |
1251
|
151 ires = -2; |
256
|
152 else |
|
153 { |
1321
|
154 for (int i = 0; i < nn; i++) |
256
|
155 delta [i] = tmp_delta.elem (i); |
|
156 } |
3
|
157 |
|
158 return 0; |
|
159 } |
|
160 |
|
161 int |
1482
|
162 ddassl_j (const double& time, double *, double *, double *pd, const |
|
163 double& cj, double *, int *) |
3
|
164 { |
1546
|
165 ColumnVector tmp_state (nn); |
|
166 ColumnVector tmp_deriv (nn); |
3
|
167 |
1360
|
168 // XXX FIXME XXX |
3
|
169 |
|
170 Matrix tmp_dfdxdot (nn, nn); |
|
171 Matrix tmp_dfdx (nn, nn); |
|
172 |
532
|
173 DAEFunc::DAEJac tmp_jac; |
3
|
174 tmp_jac.dfdxdot = &tmp_dfdxdot; |
|
175 tmp_jac.dfdx = &tmp_dfdx; |
|
176 |
1251
|
177 tmp_jac = user_jac (tmp_state, tmp_deriv, time); |
3
|
178 |
|
179 // Fix up the matrix of partial derivatives for dassl. |
|
180 |
1251
|
181 tmp_dfdx = tmp_dfdx + cj * tmp_dfdxdot; |
3
|
182 |
|
183 for (int j = 0; j < nn; j++) |
|
184 for (int i = 0; i < nn; i++) |
|
185 pd [nn * j + i] = tmp_dfdx.elem (i, j); |
|
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 { |
|
223 ColumnVector res = (*user_fun) (x, xdot, t); |
|
224 |
|
225 if (res.length () != x.length ()) |
|
226 { |
|
227 (*current_liboctave_error_handler) |
|
228 ("dassl: inconsistent sizes for state and residual vectors"); |
|
229 |
|
230 integration_error = 1; |
|
231 return retval; |
|
232 } |
|
233 |
|
234 sanity_checked = 1; |
|
235 } |
|
236 |
3
|
237 if (stop_time_set) |
|
238 { |
3312
|
239 rwork.elem (0) = stop_time; |
1945
|
240 info.elem (3) = 1; |
3
|
241 } |
|
242 else |
1945
|
243 info.elem (3) = 0; |
3
|
244 |
289
|
245 double abs_tol = absolute_tolerance (); |
|
246 double rel_tol = relative_tolerance (); |
|
247 |
|
248 if (initial_step_size () >= 0.0) |
|
249 { |
1945
|
250 rwork.elem (2) = initial_step_size (); |
|
251 info.elem (7) = 1; |
289
|
252 } |
|
253 else |
1945
|
254 info.elem (7) = 0; |
289
|
255 |
|
256 if (maximum_step_size () >= 0.0) |
|
257 { |
3312
|
258 rwork.elem (1) = maximum_step_size (); |
1945
|
259 info.elem (6) = 1; |
289
|
260 } |
|
261 else |
1945
|
262 info.elem (6) = 0; |
289
|
263 |
1365
|
264 double *dummy = 0; |
|
265 int *idummy = 0; |
3
|
266 |
1945
|
267 int *pinfo = info.fortran_vec (); |
|
268 int *piwork = iwork.fortran_vec (); |
|
269 double *prwork = rwork.fortran_vec (); |
3
|
270 |
465
|
271 // again: |
3
|
272 |
1945
|
273 F77_XFCN (ddassl, DDASSL, (ddassl_f, n, t, px, pxdot, tout, pinfo, |
|
274 rel_tol, abs_tol, idid, prwork, lrw, |
|
275 piwork, liw, dummy, idummy, ddassl_j)); |
3
|
276 |
1945
|
277 if (f77_exception_encountered) |
3178
|
278 { |
|
279 integration_error = 1; |
|
280 (*current_liboctave_error_handler) ("unrecoverable error in dassl"); |
|
281 } |
1945
|
282 else |
3
|
283 { |
1945
|
284 switch (idid) |
|
285 { |
|
286 case 1: // A step was successfully taken in intermediate-output |
|
287 // mode. The code has not yet reached TOUT. |
|
288 case 2: // The integration to TSTOP was successfully completed |
|
289 // (T=TSTOP) by stepping exactly to TSTOP. |
|
290 case 3: // The integration to TOUT was successfully completed |
|
291 // (T=TOUT) by stepping past TOUT. Y(*) is obtained by |
|
292 // interpolation. YPRIME(*) is obtained by interpolation. |
1360
|
293 |
1945
|
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: |
|
321 integration_error = 1; |
|
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 |
1842
|
372 DASSL::integrate (const ColumnVector& tout, Matrix& xdot_out, |
|
373 const ColumnVector& tcrit) |
3
|
374 { |
|
375 Matrix retval; |
|
376 int n_out = tout.capacity (); |
|
377 |
|
378 if (n_out > 0 && n > 0) |
|
379 { |
|
380 retval.resize (n_out, n); |
|
381 xdot_out.resize (n_out, n); |
|
382 |
|
383 for (int i = 0; i < n; i++) |
|
384 { |
|
385 retval.elem (0, i) = x.elem (i); |
|
386 xdot_out.elem (0, i) = xdot.elem (i); |
|
387 } |
|
388 |
|
389 int n_crit = tcrit.capacity (); |
|
390 |
|
391 if (n_crit > 0) |
|
392 { |
|
393 int i_crit = 0; |
|
394 int i_out = 1; |
|
395 double next_crit = tcrit.elem (0); |
|
396 double next_out; |
|
397 while (i_out < n_out) |
|
398 { |
3487
|
399 bool do_restart = false; |
3
|
400 |
|
401 next_out = tout.elem (i_out); |
|
402 if (i_crit < n_crit) |
|
403 next_crit = tcrit.elem (i_crit); |
|
404 |
3487
|
405 bool save_output; |
3
|
406 double t_out; |
|
407 |
|
408 if (next_crit == next_out) |
|
409 { |
|
410 set_stop_time (next_crit); |
|
411 t_out = next_out; |
3487
|
412 save_output = true; |
3
|
413 i_out++; |
|
414 i_crit++; |
3487
|
415 do_restart = true; |
3
|
416 } |
|
417 else if (next_crit < next_out) |
|
418 { |
|
419 if (i_crit < n_crit) |
|
420 { |
|
421 set_stop_time (next_crit); |
|
422 t_out = next_crit; |
3487
|
423 save_output = false; |
3
|
424 i_crit++; |
3487
|
425 do_restart = true; |
3
|
426 } |
|
427 else |
|
428 { |
|
429 clear_stop_time (); |
|
430 t_out = next_out; |
3487
|
431 save_output = true; |
3
|
432 i_out++; |
|
433 } |
|
434 } |
|
435 else |
|
436 { |
|
437 set_stop_time (next_crit); |
|
438 t_out = next_out; |
3487
|
439 save_output = true; |
3
|
440 i_out++; |
|
441 } |
|
442 |
1842
|
443 ColumnVector x_next = do_integrate (t_out); |
3
|
444 |
256
|
445 if (integration_error) |
|
446 return retval; |
|
447 |
3
|
448 if (save_output) |
|
449 { |
1321
|
450 for (int i = 0; i < n; i++) |
3
|
451 { |
|
452 retval.elem (i_out-1, i) = x_next.elem (i); |
|
453 xdot_out.elem (i_out-1, i) = xdot.elem (i); |
|
454 } |
|
455 } |
|
456 |
|
457 if (do_restart) |
|
458 force_restart (); |
|
459 } |
|
460 } |
|
461 else |
256
|
462 { |
|
463 retval = integrate (tout, xdot_out); |
|
464 |
|
465 if (integration_error) |
|
466 return retval; |
|
467 } |
3
|
468 } |
|
469 |
|
470 return retval; |
|
471 } |
289
|
472 |
|
473 /* |
|
474 ;;; Local Variables: *** |
|
475 ;;; mode: C++ *** |
|
476 ;;; End: *** |
|
477 */ |