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 |
|
38 extern "C" |
|
39 { |
1253
|
40 int F77_FCN (ddassl, DDASSL) (int (*)(const double&, double*, |
|
41 double*, double*, int&, |
|
42 double*, int*), |
|
43 const int&, double&, double*, double*, |
|
44 double&, const int*, const double&, |
|
45 const double&, int&, double*, |
|
46 const int&, int*, const int&, |
|
47 const double*, const int*, |
|
48 int (*)(const double&, double*, |
|
49 double*, double*, const |
|
50 double&, double*, int*)); |
3
|
51 } |
|
52 |
532
|
53 static DAEFunc::DAERHSFunc user_fun; |
|
54 static DAEFunc::DAEJacFunc user_jac; |
3
|
55 static int nn; |
|
56 |
1842
|
57 DASSL::DASSL (void) : DAE () |
3
|
58 { |
|
59 stop_time_set = 0; |
|
60 stop_time = 0.0; |
|
61 |
|
62 liw = 0; |
|
63 lrw = 0; |
|
64 |
2344
|
65 sanity_checked = 0; |
|
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 |
|
78 stop_time_set = 0; |
|
79 stop_time = 0.0; |
|
80 |
|
81 liw = 20 + n; |
|
82 lrw = 40 + 9*n + n*n; |
|
83 |
2344
|
84 sanity_checked = 0; |
|
85 |
1945
|
86 info.resize (15); |
3
|
87 |
|
88 for (int i = 0; i < 15; i++) |
1945
|
89 info.elem (i) = 0; |
3
|
90 } |
|
91 |
1842
|
92 DASSL::DASSL (const ColumnVector& state, const ColumnVector& deriv, |
|
93 double time, DAEFunc& f) |
|
94 : DAE (state, deriv, time, f) |
3
|
95 { |
1842
|
96 n = size (); |
3
|
97 |
|
98 stop_time_set = 0; |
|
99 stop_time = 0.0; |
|
100 |
|
101 DAEFunc::set_function (f.function ()); |
|
102 DAEFunc::set_jacobian_function (f.jacobian_function ()); |
|
103 |
|
104 liw = 20 + n; |
|
105 lrw = 40 + 9*n + n*n; |
|
106 |
2344
|
107 sanity_checked = 0; |
|
108 |
1945
|
109 info.resize (15); |
3
|
110 |
|
111 for (int i = 0; i < 15; i++) |
1945
|
112 info.elem (i) = 0; |
3
|
113 } |
|
114 |
1842
|
115 void |
|
116 DASSL::force_restart (void) |
3
|
117 { |
1842
|
118 restart = 1; |
|
119 integration_error = 0; |
3
|
120 } |
|
121 |
|
122 void |
1842
|
123 DASSL::set_stop_time (double t) |
3
|
124 { |
1842
|
125 stop_time_set = 1; |
|
126 stop_time = t; |
3
|
127 } |
|
128 |
|
129 void |
1842
|
130 DASSL::clear_stop_time (void) |
3
|
131 { |
1842
|
132 stop_time_set = 0; |
3
|
133 } |
|
134 |
|
135 int |
1251
|
136 ddassl_f (const double& time, double *state, double *deriv, |
1482
|
137 double *delta, int& ires, double *, int *) |
3
|
138 { |
1546
|
139 ColumnVector tmp_deriv (nn); |
|
140 ColumnVector tmp_state (nn); |
|
141 ColumnVector tmp_delta (nn); |
3
|
142 |
|
143 for (int i = 0; i < nn; i++) |
|
144 { |
|
145 tmp_deriv.elem (i) = deriv [i]; |
|
146 tmp_state.elem (i) = state [i]; |
|
147 } |
|
148 |
1251
|
149 tmp_delta = user_fun (tmp_state, tmp_deriv, time); |
3
|
150 |
256
|
151 if (tmp_delta.length () == 0) |
1251
|
152 ires = -2; |
256
|
153 else |
|
154 { |
1321
|
155 for (int i = 0; i < nn; i++) |
256
|
156 delta [i] = tmp_delta.elem (i); |
|
157 } |
3
|
158 |
|
159 return 0; |
|
160 } |
|
161 |
|
162 int |
1482
|
163 ddassl_j (const double& time, double *, double *, double *pd, const |
|
164 double& cj, double *, int *) |
3
|
165 { |
1546
|
166 ColumnVector tmp_state (nn); |
|
167 ColumnVector tmp_deriv (nn); |
3
|
168 |
1360
|
169 // XXX FIXME XXX |
3
|
170 |
|
171 Matrix tmp_dfdxdot (nn, nn); |
|
172 Matrix tmp_dfdx (nn, nn); |
|
173 |
532
|
174 DAEFunc::DAEJac tmp_jac; |
3
|
175 tmp_jac.dfdxdot = &tmp_dfdxdot; |
|
176 tmp_jac.dfdx = &tmp_dfdx; |
|
177 |
1251
|
178 tmp_jac = user_jac (tmp_state, tmp_deriv, time); |
3
|
179 |
|
180 // Fix up the matrix of partial derivatives for dassl. |
|
181 |
1251
|
182 tmp_dfdx = tmp_dfdx + cj * tmp_dfdxdot; |
3
|
183 |
|
184 for (int j = 0; j < nn; j++) |
|
185 for (int i = 0; i < nn; i++) |
|
186 pd [nn * j + i] = tmp_dfdx.elem (i, j); |
|
187 |
|
188 return 0; |
|
189 } |
|
190 |
1546
|
191 ColumnVector |
1842
|
192 DASSL::do_integrate (double tout) |
3
|
193 { |
1945
|
194 ColumnVector retval; |
|
195 |
|
196 if (restart) |
|
197 { |
|
198 restart = 0; |
|
199 info.elem (0) = 0; |
|
200 } |
|
201 |
|
202 if (iwork.length () != liw) |
|
203 iwork.resize (liw); |
|
204 |
|
205 if (rwork.length () != lrw) |
|
206 rwork.resize (lrw); |
|
207 |
256
|
208 integration_error = 0; |
|
209 |
532
|
210 if (DAEFunc::jacobian_function ()) |
3312
|
211 info.elem (4) = 1; |
465
|
212 else |
3312
|
213 info.elem (4) = 0; |
3
|
214 |
|
215 double *px = x.fortran_vec (); |
|
216 double *pxdot = xdot.fortran_vec (); |
|
217 |
|
218 nn = n; |
|
219 user_fun = DAEFunc::fun; |
|
220 user_jac = DAEFunc::jac; |
|
221 |
2344
|
222 if (! sanity_checked) |
|
223 { |
|
224 ColumnVector res = (*user_fun) (x, xdot, t); |
|
225 |
|
226 if (res.length () != x.length ()) |
|
227 { |
|
228 (*current_liboctave_error_handler) |
|
229 ("dassl: inconsistent sizes for state and residual vectors"); |
|
230 |
|
231 integration_error = 1; |
|
232 return retval; |
|
233 } |
|
234 |
|
235 sanity_checked = 1; |
|
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 { |
|
280 integration_error = 1; |
|
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. |
1360
|
294 |
1945
|
295 retval = x; |
|
296 t = tout; |
|
297 break; |
1360
|
298 |
1945
|
299 case -1: // A large amount of work has been expended. (~500 steps). |
|
300 case -2: // The error tolerances are too stringent. |
|
301 case -3: // The local error test cannot be satisfied because you |
|
302 // specified a zero component in ATOL and the |
|
303 // corresponding computed solution component is zero. |
|
304 // Thus, a pure relative error test is impossible for |
|
305 // this component. |
|
306 case -6: // DDASSL had repeated error test failures on the last |
|
307 // attempted step. |
|
308 case -7: // The corrector could not converge. |
|
309 case -8: // The matrix of partial derivatives is singular. |
|
310 case -9: // The corrector could not converge. There were repeated |
|
311 // error test failures in this step. |
|
312 case -10: // The corrector could not converge because IRES was |
|
313 // equal to minus one. |
|
314 case -11: // IRES equal to -2 was encountered and control is being |
|
315 // returned to the calling program. |
|
316 case -12: // DDASSL failed to compute the initial YPRIME. |
|
317 case -33: // The code has encountered trouble from which it cannot |
|
318 // recover. A message is printed explaining the trouble |
|
319 // and control is returned to the calling program. For |
|
320 // example, this occurs when invalid input is detected. |
|
321 default: |
|
322 integration_error = 1; |
|
323 break; |
|
324 } |
3
|
325 } |
|
326 |
1945
|
327 return retval; |
3
|
328 } |
|
329 |
|
330 Matrix |
1842
|
331 DASSL::do_integrate (const ColumnVector& tout) |
|
332 { |
|
333 Matrix dummy; |
|
334 return integrate (tout, dummy); |
|
335 } |
|
336 |
|
337 Matrix |
|
338 DASSL::integrate (const ColumnVector& tout, Matrix& xdot_out) |
3
|
339 { |
|
340 Matrix retval; |
|
341 int n_out = tout.capacity (); |
|
342 |
|
343 if (n_out > 0 && n > 0) |
|
344 { |
|
345 retval.resize (n_out, n); |
|
346 xdot_out.resize (n_out, n); |
|
347 |
|
348 for (int i = 0; i < n; i++) |
|
349 { |
|
350 retval.elem (0, i) = x.elem (i); |
|
351 xdot_out.elem (0, i) = xdot.elem (i); |
|
352 } |
|
353 |
|
354 for (int j = 1; j < n_out; j++) |
|
355 { |
1842
|
356 ColumnVector x_next = do_integrate (tout.elem (j)); |
256
|
357 |
|
358 if (integration_error) |
|
359 return retval; |
|
360 |
1321
|
361 for (int i = 0; i < n; i++) |
3
|
362 { |
|
363 retval.elem (j, i) = x_next.elem (i); |
|
364 xdot_out.elem (j, i) = xdot.elem (i); |
|
365 } |
|
366 } |
|
367 } |
|
368 |
|
369 return retval; |
|
370 } |
|
371 |
|
372 Matrix |
1842
|
373 DASSL::integrate (const ColumnVector& tout, Matrix& xdot_out, |
|
374 const ColumnVector& tcrit) |
3
|
375 { |
|
376 Matrix retval; |
|
377 int n_out = tout.capacity (); |
|
378 |
|
379 if (n_out > 0 && n > 0) |
|
380 { |
|
381 retval.resize (n_out, n); |
|
382 xdot_out.resize (n_out, n); |
|
383 |
|
384 for (int i = 0; i < n; i++) |
|
385 { |
|
386 retval.elem (0, i) = x.elem (i); |
|
387 xdot_out.elem (0, i) = xdot.elem (i); |
|
388 } |
|
389 |
|
390 int n_crit = tcrit.capacity (); |
|
391 |
|
392 if (n_crit > 0) |
|
393 { |
|
394 int i_crit = 0; |
|
395 int i_out = 1; |
|
396 double next_crit = tcrit.elem (0); |
|
397 double next_out; |
|
398 while (i_out < n_out) |
|
399 { |
|
400 int do_restart = 0; |
|
401 |
|
402 next_out = tout.elem (i_out); |
|
403 if (i_crit < n_crit) |
|
404 next_crit = tcrit.elem (i_crit); |
|
405 |
|
406 int save_output; |
|
407 double t_out; |
|
408 |
|
409 if (next_crit == next_out) |
|
410 { |
|
411 set_stop_time (next_crit); |
|
412 t_out = next_out; |
|
413 save_output = 1; |
|
414 i_out++; |
|
415 i_crit++; |
|
416 do_restart = 1; |
|
417 } |
|
418 else if (next_crit < next_out) |
|
419 { |
|
420 if (i_crit < n_crit) |
|
421 { |
|
422 set_stop_time (next_crit); |
|
423 t_out = next_crit; |
|
424 save_output = 0; |
|
425 i_crit++; |
|
426 do_restart = 1; |
|
427 } |
|
428 else |
|
429 { |
|
430 clear_stop_time (); |
|
431 t_out = next_out; |
|
432 save_output = 1; |
|
433 i_out++; |
|
434 } |
|
435 } |
|
436 else |
|
437 { |
|
438 set_stop_time (next_crit); |
|
439 t_out = next_out; |
|
440 save_output = 1; |
|
441 i_out++; |
|
442 } |
|
443 |
1842
|
444 ColumnVector x_next = do_integrate (t_out); |
3
|
445 |
256
|
446 if (integration_error) |
|
447 return retval; |
|
448 |
3
|
449 if (save_output) |
|
450 { |
1321
|
451 for (int i = 0; i < n; i++) |
3
|
452 { |
|
453 retval.elem (i_out-1, i) = x_next.elem (i); |
|
454 xdot_out.elem (i_out-1, i) = xdot.elem (i); |
|
455 } |
|
456 } |
|
457 |
|
458 if (do_restart) |
|
459 force_restart (); |
|
460 } |
|
461 } |
|
462 else |
256
|
463 { |
|
464 retval = integrate (tout, xdot_out); |
|
465 |
|
466 if (integration_error) |
|
467 return retval; |
|
468 } |
3
|
469 } |
|
470 |
|
471 return retval; |
|
472 } |
289
|
473 |
|
474 /* |
|
475 ;;; Local Variables: *** |
|
476 ;;; mode: C++ *** |
|
477 ;;; End: *** |
|
478 */ |