1842
|
1 // DASSL.cc -*- C++ -*- |
3
|
2 /* |
|
3 |
1842
|
4 Copyright (C) 1996 John W. Eaton |
3
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
3
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
238
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
3
|
30 #endif |
|
31 |
1842
|
32 #include <cfloat> |
|
33 #include <cmath> |
|
34 |
|
35 #include "DASSL.h" |
1847
|
36 #include "f77-fcn.h" |
227
|
37 #include "lo-error.h" |
3
|
38 |
|
39 extern "C" |
|
40 { |
1253
|
41 int F77_FCN (ddassl, DDASSL) (int (*)(const double&, double*, |
|
42 double*, double*, int&, |
|
43 double*, int*), |
|
44 const int&, double&, double*, double*, |
|
45 double&, const int*, const double&, |
|
46 const double&, int&, double*, |
|
47 const int&, int*, const int&, |
|
48 const double*, const int*, |
|
49 int (*)(const double&, double*, |
|
50 double*, double*, const |
|
51 double&, double*, int*)); |
3
|
52 } |
|
53 |
532
|
54 static DAEFunc::DAERHSFunc user_fun; |
|
55 static DAEFunc::DAEJacFunc user_jac; |
3
|
56 static int nn; |
|
57 |
1842
|
58 DASSL::DASSL (void) : DAE () |
3
|
59 { |
|
60 stop_time_set = 0; |
|
61 stop_time = 0.0; |
|
62 |
|
63 liw = 0; |
|
64 lrw = 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 |
1945
|
83 info.resize (15); |
3
|
84 |
|
85 for (int i = 0; i < 15; i++) |
1945
|
86 info.elem (i) = 0; |
3
|
87 } |
|
88 |
1842
|
89 DASSL::DASSL (const ColumnVector& state, const ColumnVector& deriv, |
|
90 double time, DAEFunc& f) |
|
91 : DAE (state, deriv, time, f) |
3
|
92 { |
1842
|
93 n = size (); |
3
|
94 |
|
95 stop_time_set = 0; |
|
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 |
1945
|
104 info.resize (15); |
3
|
105 |
|
106 for (int i = 0; i < 15; i++) |
1945
|
107 info.elem (i) = 0; |
3
|
108 } |
|
109 |
1842
|
110 void |
|
111 DASSL::force_restart (void) |
3
|
112 { |
1842
|
113 restart = 1; |
|
114 integration_error = 0; |
3
|
115 } |
|
116 |
|
117 void |
1842
|
118 DASSL::set_stop_time (double t) |
3
|
119 { |
1842
|
120 stop_time_set = 1; |
|
121 stop_time = t; |
3
|
122 } |
|
123 |
|
124 void |
1842
|
125 DASSL::clear_stop_time (void) |
3
|
126 { |
1842
|
127 stop_time_set = 0; |
3
|
128 } |
|
129 |
|
130 int |
1251
|
131 ddassl_f (const double& time, double *state, double *deriv, |
1482
|
132 double *delta, int& ires, double *, int *) |
3
|
133 { |
1546
|
134 ColumnVector tmp_deriv (nn); |
|
135 ColumnVector tmp_state (nn); |
|
136 ColumnVector tmp_delta (nn); |
3
|
137 |
|
138 for (int i = 0; i < nn; i++) |
|
139 { |
|
140 tmp_deriv.elem (i) = deriv [i]; |
|
141 tmp_state.elem (i) = state [i]; |
|
142 } |
|
143 |
1251
|
144 tmp_delta = user_fun (tmp_state, tmp_deriv, time); |
3
|
145 |
256
|
146 if (tmp_delta.length () == 0) |
1251
|
147 ires = -2; |
256
|
148 else |
|
149 { |
1321
|
150 for (int i = 0; i < nn; i++) |
256
|
151 delta [i] = tmp_delta.elem (i); |
|
152 } |
3
|
153 |
|
154 return 0; |
|
155 } |
|
156 |
|
157 int |
1482
|
158 ddassl_j (const double& time, double *, double *, double *pd, const |
|
159 double& cj, double *, int *) |
3
|
160 { |
1546
|
161 ColumnVector tmp_state (nn); |
|
162 ColumnVector tmp_deriv (nn); |
3
|
163 |
1360
|
164 // XXX FIXME XXX |
3
|
165 |
|
166 Matrix tmp_dfdxdot (nn, nn); |
|
167 Matrix tmp_dfdx (nn, nn); |
|
168 |
532
|
169 DAEFunc::DAEJac tmp_jac; |
3
|
170 tmp_jac.dfdxdot = &tmp_dfdxdot; |
|
171 tmp_jac.dfdx = &tmp_dfdx; |
|
172 |
1251
|
173 tmp_jac = user_jac (tmp_state, tmp_deriv, time); |
3
|
174 |
|
175 // Fix up the matrix of partial derivatives for dassl. |
|
176 |
1251
|
177 tmp_dfdx = tmp_dfdx + cj * tmp_dfdxdot; |
3
|
178 |
|
179 for (int j = 0; j < nn; j++) |
|
180 for (int i = 0; i < nn; i++) |
|
181 pd [nn * j + i] = tmp_dfdx.elem (i, j); |
|
182 |
|
183 return 0; |
|
184 } |
|
185 |
1546
|
186 ColumnVector |
1842
|
187 DASSL::do_integrate (double tout) |
3
|
188 { |
1945
|
189 ColumnVector retval; |
|
190 |
|
191 if (restart) |
|
192 { |
|
193 restart = 0; |
|
194 info.elem (0) = 0; |
|
195 } |
|
196 |
|
197 if (iwork.length () != liw) |
|
198 iwork.resize (liw); |
|
199 |
|
200 if (rwork.length () != lrw) |
|
201 rwork.resize (lrw); |
|
202 |
256
|
203 integration_error = 0; |
|
204 |
532
|
205 if (DAEFunc::jacobian_function ()) |
1945
|
206 iwork.elem (4) = 1; |
465
|
207 else |
1945
|
208 iwork.elem (4) = 0; |
3
|
209 |
|
210 double *px = x.fortran_vec (); |
|
211 double *pxdot = xdot.fortran_vec (); |
|
212 |
|
213 nn = n; |
|
214 user_fun = DAEFunc::fun; |
|
215 user_jac = DAEFunc::jac; |
|
216 |
|
217 if (stop_time_set) |
|
218 { |
1945
|
219 info.elem (3) = 1; |
|
220 rwork.elem (0) = stop_time; |
3
|
221 } |
|
222 else |
1945
|
223 info.elem (3) = 0; |
3
|
224 |
289
|
225 double abs_tol = absolute_tolerance (); |
|
226 double rel_tol = relative_tolerance (); |
|
227 |
|
228 if (initial_step_size () >= 0.0) |
|
229 { |
1945
|
230 rwork.elem (2) = initial_step_size (); |
|
231 info.elem (7) = 1; |
289
|
232 } |
|
233 else |
1945
|
234 info.elem (7) = 0; |
289
|
235 |
|
236 if (maximum_step_size () >= 0.0) |
|
237 { |
1945
|
238 rwork.elem (2) = maximum_step_size (); |
|
239 info.elem (6) = 1; |
289
|
240 } |
|
241 else |
1945
|
242 info.elem (6) = 0; |
289
|
243 |
1365
|
244 double *dummy = 0; |
|
245 int *idummy = 0; |
3
|
246 |
1945
|
247 int *pinfo = info.fortran_vec (); |
|
248 int *piwork = iwork.fortran_vec (); |
|
249 double *prwork = rwork.fortran_vec (); |
3
|
250 |
465
|
251 // again: |
3
|
252 |
1945
|
253 F77_XFCN (ddassl, DDASSL, (ddassl_f, n, t, px, pxdot, tout, pinfo, |
|
254 rel_tol, abs_tol, idid, prwork, lrw, |
|
255 piwork, liw, dummy, idummy, ddassl_j)); |
3
|
256 |
1945
|
257 if (f77_exception_encountered) |
|
258 (*current_liboctave_error_handler) ("unrecoverable error in dassl"); |
|
259 else |
3
|
260 { |
1945
|
261 switch (idid) |
|
262 { |
|
263 case 1: // A step was successfully taken in intermediate-output |
|
264 // mode. The code has not yet reached TOUT. |
|
265 case 2: // The integration to TSTOP was successfully completed |
|
266 // (T=TSTOP) by stepping exactly to TSTOP. |
|
267 case 3: // The integration to TOUT was successfully completed |
|
268 // (T=TOUT) by stepping past TOUT. Y(*) is obtained by |
|
269 // interpolation. YPRIME(*) is obtained by interpolation. |
1360
|
270 |
1945
|
271 retval = x; |
|
272 t = tout; |
|
273 break; |
1360
|
274 |
1945
|
275 case -1: // A large amount of work has been expended. (~500 steps). |
|
276 case -2: // The error tolerances are too stringent. |
|
277 case -3: // The local error test cannot be satisfied because you |
|
278 // specified a zero component in ATOL and the |
|
279 // corresponding computed solution component is zero. |
|
280 // Thus, a pure relative error test is impossible for |
|
281 // this component. |
|
282 case -6: // DDASSL had repeated error test failures on the last |
|
283 // attempted step. |
|
284 case -7: // The corrector could not converge. |
|
285 case -8: // The matrix of partial derivatives is singular. |
|
286 case -9: // The corrector could not converge. There were repeated |
|
287 // error test failures in this step. |
|
288 case -10: // The corrector could not converge because IRES was |
|
289 // equal to minus one. |
|
290 case -11: // IRES equal to -2 was encountered and control is being |
|
291 // returned to the calling program. |
|
292 case -12: // DDASSL failed to compute the initial YPRIME. |
|
293 case -33: // The code has encountered trouble from which it cannot |
|
294 // recover. A message is printed explaining the trouble |
|
295 // and control is returned to the calling program. For |
|
296 // example, this occurs when invalid input is detected. |
|
297 default: |
|
298 integration_error = 1; |
|
299 break; |
|
300 } |
3
|
301 } |
|
302 |
1945
|
303 return retval; |
3
|
304 } |
|
305 |
|
306 Matrix |
1842
|
307 DASSL::do_integrate (const ColumnVector& tout) |
|
308 { |
|
309 Matrix dummy; |
|
310 return integrate (tout, dummy); |
|
311 } |
|
312 |
|
313 Matrix |
|
314 DASSL::integrate (const ColumnVector& tout, Matrix& xdot_out) |
3
|
315 { |
|
316 Matrix retval; |
|
317 int n_out = tout.capacity (); |
|
318 |
|
319 if (n_out > 0 && n > 0) |
|
320 { |
|
321 retval.resize (n_out, n); |
|
322 xdot_out.resize (n_out, n); |
|
323 |
|
324 for (int i = 0; i < n; i++) |
|
325 { |
|
326 retval.elem (0, i) = x.elem (i); |
|
327 xdot_out.elem (0, i) = xdot.elem (i); |
|
328 } |
|
329 |
|
330 for (int j = 1; j < n_out; j++) |
|
331 { |
1842
|
332 ColumnVector x_next = do_integrate (tout.elem (j)); |
256
|
333 |
|
334 if (integration_error) |
|
335 return retval; |
|
336 |
1321
|
337 for (int i = 0; i < n; i++) |
3
|
338 { |
|
339 retval.elem (j, i) = x_next.elem (i); |
|
340 xdot_out.elem (j, i) = xdot.elem (i); |
|
341 } |
|
342 } |
|
343 } |
|
344 |
|
345 return retval; |
|
346 } |
|
347 |
|
348 Matrix |
1842
|
349 DASSL::integrate (const ColumnVector& tout, Matrix& xdot_out, |
|
350 const ColumnVector& tcrit) |
3
|
351 { |
|
352 Matrix retval; |
|
353 int n_out = tout.capacity (); |
|
354 |
|
355 if (n_out > 0 && n > 0) |
|
356 { |
|
357 retval.resize (n_out, n); |
|
358 xdot_out.resize (n_out, n); |
|
359 |
|
360 for (int i = 0; i < n; i++) |
|
361 { |
|
362 retval.elem (0, i) = x.elem (i); |
|
363 xdot_out.elem (0, i) = xdot.elem (i); |
|
364 } |
|
365 |
|
366 int n_crit = tcrit.capacity (); |
|
367 |
|
368 if (n_crit > 0) |
|
369 { |
|
370 int i_crit = 0; |
|
371 int i_out = 1; |
|
372 double next_crit = tcrit.elem (0); |
|
373 double next_out; |
|
374 while (i_out < n_out) |
|
375 { |
|
376 int do_restart = 0; |
|
377 |
|
378 next_out = tout.elem (i_out); |
|
379 if (i_crit < n_crit) |
|
380 next_crit = tcrit.elem (i_crit); |
|
381 |
|
382 int save_output; |
|
383 double t_out; |
|
384 |
|
385 if (next_crit == next_out) |
|
386 { |
|
387 set_stop_time (next_crit); |
|
388 t_out = next_out; |
|
389 save_output = 1; |
|
390 i_out++; |
|
391 i_crit++; |
|
392 do_restart = 1; |
|
393 } |
|
394 else if (next_crit < next_out) |
|
395 { |
|
396 if (i_crit < n_crit) |
|
397 { |
|
398 set_stop_time (next_crit); |
|
399 t_out = next_crit; |
|
400 save_output = 0; |
|
401 i_crit++; |
|
402 do_restart = 1; |
|
403 } |
|
404 else |
|
405 { |
|
406 clear_stop_time (); |
|
407 t_out = next_out; |
|
408 save_output = 1; |
|
409 i_out++; |
|
410 } |
|
411 } |
|
412 else |
|
413 { |
|
414 set_stop_time (next_crit); |
|
415 t_out = next_out; |
|
416 save_output = 1; |
|
417 i_out++; |
|
418 } |
|
419 |
1842
|
420 ColumnVector x_next = do_integrate (t_out); |
3
|
421 |
256
|
422 if (integration_error) |
|
423 return retval; |
|
424 |
3
|
425 if (save_output) |
|
426 { |
1321
|
427 for (int i = 0; i < n; i++) |
3
|
428 { |
|
429 retval.elem (i_out-1, i) = x_next.elem (i); |
|
430 xdot_out.elem (i_out-1, i) = xdot.elem (i); |
|
431 } |
|
432 } |
|
433 |
|
434 if (do_restart) |
|
435 force_restart (); |
|
436 } |
|
437 } |
|
438 else |
256
|
439 { |
|
440 retval = integrate (tout, xdot_out); |
|
441 |
|
442 if (integration_error) |
|
443 return retval; |
|
444 } |
3
|
445 } |
|
446 |
|
447 return retval; |
|
448 } |
289
|
449 |
|
450 /* |
|
451 ;;; Local Variables: *** |
|
452 ;;; mode: C++ *** |
|
453 ;;; page-delimiter: "^/\\*" *** |
|
454 ;;; End: *** |
|
455 */ |