3
|
1 /* |
|
2 |
1842
|
3 Copyright (C) 1996 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 ()) |
1945
|
211 iwork.elem (4) = 1; |
465
|
212 else |
1945
|
213 iwork.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 { |
1945
|
240 info.elem (3) = 1; |
|
241 rwork.elem (0) = stop_time; |
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 { |
1945
|
259 rwork.elem (2) = maximum_step_size (); |
|
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) |
|
279 (*current_liboctave_error_handler) ("unrecoverable error in dassl"); |
|
280 else |
3
|
281 { |
1945
|
282 switch (idid) |
|
283 { |
|
284 case 1: // A step was successfully taken in intermediate-output |
|
285 // mode. The code has not yet reached TOUT. |
|
286 case 2: // The integration to TSTOP was successfully completed |
|
287 // (T=TSTOP) by stepping exactly to TSTOP. |
|
288 case 3: // The integration to TOUT was successfully completed |
|
289 // (T=TOUT) by stepping past TOUT. Y(*) is obtained by |
|
290 // interpolation. YPRIME(*) is obtained by interpolation. |
1360
|
291 |
1945
|
292 retval = x; |
|
293 t = tout; |
|
294 break; |
1360
|
295 |
1945
|
296 case -1: // A large amount of work has been expended. (~500 steps). |
|
297 case -2: // The error tolerances are too stringent. |
|
298 case -3: // The local error test cannot be satisfied because you |
|
299 // specified a zero component in ATOL and the |
|
300 // corresponding computed solution component is zero. |
|
301 // Thus, a pure relative error test is impossible for |
|
302 // this component. |
|
303 case -6: // DDASSL had repeated error test failures on the last |
|
304 // attempted step. |
|
305 case -7: // The corrector could not converge. |
|
306 case -8: // The matrix of partial derivatives is singular. |
|
307 case -9: // The corrector could not converge. There were repeated |
|
308 // error test failures in this step. |
|
309 case -10: // The corrector could not converge because IRES was |
|
310 // equal to minus one. |
|
311 case -11: // IRES equal to -2 was encountered and control is being |
|
312 // returned to the calling program. |
|
313 case -12: // DDASSL failed to compute the initial YPRIME. |
|
314 case -33: // The code has encountered trouble from which it cannot |
|
315 // recover. A message is printed explaining the trouble |
|
316 // and control is returned to the calling program. For |
|
317 // example, this occurs when invalid input is detected. |
|
318 default: |
|
319 integration_error = 1; |
|
320 break; |
|
321 } |
3
|
322 } |
|
323 |
1945
|
324 return retval; |
3
|
325 } |
|
326 |
|
327 Matrix |
1842
|
328 DASSL::do_integrate (const ColumnVector& tout) |
|
329 { |
|
330 Matrix dummy; |
|
331 return integrate (tout, dummy); |
|
332 } |
|
333 |
|
334 Matrix |
|
335 DASSL::integrate (const ColumnVector& tout, Matrix& xdot_out) |
3
|
336 { |
|
337 Matrix retval; |
|
338 int n_out = tout.capacity (); |
|
339 |
|
340 if (n_out > 0 && n > 0) |
|
341 { |
|
342 retval.resize (n_out, n); |
|
343 xdot_out.resize (n_out, n); |
|
344 |
|
345 for (int i = 0; i < n; i++) |
|
346 { |
|
347 retval.elem (0, i) = x.elem (i); |
|
348 xdot_out.elem (0, i) = xdot.elem (i); |
|
349 } |
|
350 |
|
351 for (int j = 1; j < n_out; j++) |
|
352 { |
1842
|
353 ColumnVector x_next = do_integrate (tout.elem (j)); |
256
|
354 |
|
355 if (integration_error) |
|
356 return retval; |
|
357 |
1321
|
358 for (int i = 0; i < n; i++) |
3
|
359 { |
|
360 retval.elem (j, i) = x_next.elem (i); |
|
361 xdot_out.elem (j, i) = xdot.elem (i); |
|
362 } |
|
363 } |
|
364 } |
|
365 |
|
366 return retval; |
|
367 } |
|
368 |
|
369 Matrix |
1842
|
370 DASSL::integrate (const ColumnVector& tout, Matrix& xdot_out, |
|
371 const ColumnVector& tcrit) |
3
|
372 { |
|
373 Matrix retval; |
|
374 int n_out = tout.capacity (); |
|
375 |
|
376 if (n_out > 0 && n > 0) |
|
377 { |
|
378 retval.resize (n_out, n); |
|
379 xdot_out.resize (n_out, n); |
|
380 |
|
381 for (int i = 0; i < n; i++) |
|
382 { |
|
383 retval.elem (0, i) = x.elem (i); |
|
384 xdot_out.elem (0, i) = xdot.elem (i); |
|
385 } |
|
386 |
|
387 int n_crit = tcrit.capacity (); |
|
388 |
|
389 if (n_crit > 0) |
|
390 { |
|
391 int i_crit = 0; |
|
392 int i_out = 1; |
|
393 double next_crit = tcrit.elem (0); |
|
394 double next_out; |
|
395 while (i_out < n_out) |
|
396 { |
|
397 int do_restart = 0; |
|
398 |
|
399 next_out = tout.elem (i_out); |
|
400 if (i_crit < n_crit) |
|
401 next_crit = tcrit.elem (i_crit); |
|
402 |
|
403 int save_output; |
|
404 double t_out; |
|
405 |
|
406 if (next_crit == next_out) |
|
407 { |
|
408 set_stop_time (next_crit); |
|
409 t_out = next_out; |
|
410 save_output = 1; |
|
411 i_out++; |
|
412 i_crit++; |
|
413 do_restart = 1; |
|
414 } |
|
415 else if (next_crit < next_out) |
|
416 { |
|
417 if (i_crit < n_crit) |
|
418 { |
|
419 set_stop_time (next_crit); |
|
420 t_out = next_crit; |
|
421 save_output = 0; |
|
422 i_crit++; |
|
423 do_restart = 1; |
|
424 } |
|
425 else |
|
426 { |
|
427 clear_stop_time (); |
|
428 t_out = next_out; |
|
429 save_output = 1; |
|
430 i_out++; |
|
431 } |
|
432 } |
|
433 else |
|
434 { |
|
435 set_stop_time (next_crit); |
|
436 t_out = next_out; |
|
437 save_output = 1; |
|
438 i_out++; |
|
439 } |
|
440 |
1842
|
441 ColumnVector x_next = do_integrate (t_out); |
3
|
442 |
256
|
443 if (integration_error) |
|
444 return retval; |
|
445 |
3
|
446 if (save_output) |
|
447 { |
1321
|
448 for (int i = 0; i < n; i++) |
3
|
449 { |
|
450 retval.elem (i_out-1, i) = x_next.elem (i); |
|
451 xdot_out.elem (i_out-1, i) = xdot.elem (i); |
|
452 } |
|
453 } |
|
454 |
|
455 if (do_restart) |
|
456 force_restart (); |
|
457 } |
|
458 } |
|
459 else |
256
|
460 { |
|
461 retval = integrate (tout, xdot_out); |
|
462 |
|
463 if (integration_error) |
|
464 return retval; |
|
465 } |
3
|
466 } |
|
467 |
|
468 return retval; |
|
469 } |
289
|
470 |
|
471 /* |
|
472 ;;; Local Variables: *** |
|
473 ;;; mode: C++ *** |
|
474 ;;; End: *** |
|
475 */ |