3
|
1 // DAE.cc -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
238
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
3
|
26 #endif |
|
27 |
|
28 #include "DAE.h" |
238
|
29 #include "f77-uscore.h" |
227
|
30 #include "lo-error.h" |
3
|
31 |
|
32 extern "C" |
|
33 { |
1253
|
34 int F77_FCN (ddassl, DDASSL) (int (*)(const double&, double*, |
|
35 double*, double*, int&, |
|
36 double*, int*), |
|
37 const int&, double&, double*, double*, |
|
38 double&, const int*, const double&, |
|
39 const double&, int&, double*, |
|
40 const int&, int*, const int&, |
|
41 const double*, const int*, |
|
42 int (*)(const double&, double*, |
|
43 double*, double*, const |
|
44 double&, double*, int*)); |
3
|
45 } |
|
46 |
532
|
47 static DAEFunc::DAERHSFunc user_fun; |
|
48 static DAEFunc::DAEJacFunc user_jac; |
3
|
49 static int nn; |
|
50 |
|
51 DAE::DAE (void) |
|
52 { |
|
53 n = 0; |
|
54 t = 0.0; |
|
55 |
|
56 stop_time_set = 0; |
|
57 stop_time = 0.0; |
|
58 |
256
|
59 integration_error = 0; |
3
|
60 restart = 1; |
|
61 |
465
|
62 DAEFunc::set_function (0); |
|
63 DAEFunc::set_jacobian_function (0); |
3
|
64 |
|
65 liw = 0; |
|
66 lrw = 0; |
|
67 |
|
68 info = new int [15]; |
465
|
69 iwork = (int *) 0; |
|
70 rwork = (double *) 0; |
3
|
71 |
|
72 for (int i = 0; i < 15; i++) |
|
73 info [i] = 0; |
|
74 } |
|
75 |
|
76 DAE::DAE (int size) |
|
77 { |
|
78 n = size; |
|
79 t = 0.0; |
|
80 |
|
81 stop_time_set = 0; |
|
82 stop_time = 0.0; |
|
83 |
256
|
84 integration_error = 0; |
3
|
85 restart = 1; |
|
86 |
465
|
87 DAEFunc::set_function (0); |
|
88 DAEFunc::set_jacobian_function (0); |
3
|
89 |
|
90 liw = 20 + n; |
|
91 lrw = 40 + 9*n + n*n; |
|
92 |
|
93 info = new int [15]; |
|
94 iwork = new int [liw]; |
|
95 rwork = new double [lrw]; |
|
96 |
|
97 for (int i = 0; i < 15; i++) |
|
98 info [i] = 0; |
|
99 } |
|
100 |
302
|
101 DAE::DAE (const Vector& state, double time, DAEFunc& f) |
3
|
102 { |
|
103 n = state.capacity (); |
|
104 t = time; |
|
105 x = state; |
|
106 xdot.resize (n, 0.0); |
|
107 |
|
108 stop_time_set = 0; |
|
109 stop_time = 0.0; |
|
110 |
256
|
111 integration_error = 0; |
3
|
112 restart = 1; |
|
113 |
|
114 DAEFunc::set_function (f.function ()); |
|
115 DAEFunc::set_jacobian_function (f.jacobian_function ()); |
|
116 |
|
117 liw = 20 + n; |
|
118 lrw = 40 + 9*n + n*n; |
|
119 |
|
120 info = new int [15]; |
|
121 iwork = new int [liw]; |
|
122 rwork = new double [lrw]; |
|
123 |
|
124 for (int i = 0; i < 15; i++) |
|
125 info [i] = 0; |
|
126 } |
|
127 |
302
|
128 DAE::DAE (const Vector& state, const Vector& deriv, double time, DAEFunc& f) |
3
|
129 { |
|
130 if (deriv.capacity () != state.capacity ()) |
|
131 { |
227
|
132 (*current_liboctave_error_handler) |
|
133 ("x, xdot size mismatch in DAE constructor"); |
|
134 n = 0; |
|
135 t = 0.0; |
|
136 return; |
3
|
137 } |
|
138 |
|
139 n = state.capacity (); |
|
140 t = time; |
|
141 xdot = deriv; |
|
142 x = state; |
|
143 |
|
144 stop_time_set = 0; |
|
145 stop_time = 0.0; |
|
146 |
|
147 DAEFunc::set_function (f.function ()); |
|
148 DAEFunc::set_jacobian_function (f.jacobian_function ()); |
|
149 |
|
150 liw = 20 + n; |
|
151 lrw = 40 + 9*n + n*n; |
|
152 |
|
153 info = new int [15]; |
|
154 iwork = new int [liw]; |
|
155 rwork = new double [lrw]; |
|
156 |
|
157 for (int i = 0; i < 15; i++) |
|
158 info [i] = 0; |
|
159 } |
|
160 |
|
161 DAE::~DAE (void) |
|
162 { |
|
163 delete info; |
|
164 delete rwork; |
|
165 delete iwork; |
|
166 } |
|
167 |
|
168 Vector |
|
169 DAE::deriv (void) |
|
170 { |
|
171 return xdot; |
|
172 } |
|
173 |
|
174 void |
302
|
175 DAE::initialize (const Vector& state, double time) |
3
|
176 { |
256
|
177 integration_error = 0; |
3
|
178 restart = 1; |
|
179 x = state; |
|
180 int nx = x.capacity (); |
|
181 xdot.resize (nx, 0.0); |
|
182 t = time; |
|
183 } |
|
184 |
|
185 void |
302
|
186 DAE::initialize (const Vector& state, const Vector& deriv, double time) |
3
|
187 { |
256
|
188 integration_error = 0; |
3
|
189 restart = 1; |
|
190 xdot = deriv; |
|
191 x = state; |
|
192 t = time; |
|
193 } |
|
194 |
|
195 int |
1251
|
196 ddassl_f (const double& time, double *state, double *deriv, |
|
197 double *delta, int& ires, double *rpar, int *ipar) |
3
|
198 { |
|
199 Vector tmp_deriv (nn); |
|
200 Vector tmp_state (nn); |
|
201 Vector tmp_delta (nn); |
|
202 |
|
203 for (int i = 0; i < nn; i++) |
|
204 { |
|
205 tmp_deriv.elem (i) = deriv [i]; |
|
206 tmp_state.elem (i) = state [i]; |
|
207 } |
|
208 |
1251
|
209 tmp_delta = user_fun (tmp_state, tmp_deriv, time); |
3
|
210 |
256
|
211 if (tmp_delta.length () == 0) |
1251
|
212 ires = -2; |
256
|
213 else |
|
214 { |
|
215 for (i = 0; i < nn; i++) |
|
216 delta [i] = tmp_delta.elem (i); |
|
217 } |
3
|
218 |
|
219 return 0; |
|
220 } |
|
221 |
|
222 int |
1251
|
223 ddassl_j (const double& time, double *state, double *deriv, double *pd, |
|
224 const double& cj, double *rpar, int *ipar) |
3
|
225 { |
|
226 Vector tmp_state (nn); |
|
227 Vector tmp_deriv (nn); |
|
228 |
|
229 // XXX FIXME XXX |
|
230 |
|
231 Matrix tmp_dfdxdot (nn, nn); |
|
232 Matrix tmp_dfdx (nn, nn); |
|
233 |
532
|
234 DAEFunc::DAEJac tmp_jac; |
3
|
235 tmp_jac.dfdxdot = &tmp_dfdxdot; |
|
236 tmp_jac.dfdx = &tmp_dfdx; |
|
237 |
1251
|
238 tmp_jac = user_jac (tmp_state, tmp_deriv, time); |
3
|
239 |
|
240 // Fix up the matrix of partial derivatives for dassl. |
|
241 |
1251
|
242 tmp_dfdx = tmp_dfdx + cj * tmp_dfdxdot; |
3
|
243 |
|
244 for (int j = 0; j < nn; j++) |
|
245 for (int i = 0; i < nn; i++) |
|
246 pd [nn * j + i] = tmp_dfdx.elem (i, j); |
|
247 |
|
248 return 0; |
|
249 } |
|
250 |
|
251 Vector |
|
252 DAE::integrate (double tout) |
|
253 { |
256
|
254 integration_error = 0; |
|
255 |
532
|
256 if (DAEFunc::jacobian_function ()) |
465
|
257 iwork [4] = 1; |
|
258 else |
3
|
259 iwork [4] = 0; |
|
260 |
|
261 double *px = x.fortran_vec (); |
|
262 double *pxdot = xdot.fortran_vec (); |
|
263 |
|
264 nn = n; |
|
265 user_fun = DAEFunc::fun; |
|
266 user_jac = DAEFunc::jac; |
|
267 |
|
268 if (stop_time_set) |
|
269 { |
|
270 info [3] = 1; |
|
271 rwork [0] = stop_time; |
|
272 } |
|
273 else |
|
274 info [3] = 0; |
|
275 |
289
|
276 double abs_tol = absolute_tolerance (); |
|
277 double rel_tol = relative_tolerance (); |
|
278 |
|
279 if (initial_step_size () >= 0.0) |
|
280 { |
|
281 rwork[2] = initial_step_size (); |
|
282 info[7] = 1; |
|
283 } |
|
284 else |
|
285 info[7] = 0; |
|
286 |
|
287 if (maximum_step_size () >= 0.0) |
|
288 { |
|
289 rwork[2] = maximum_step_size (); |
|
290 info[6] = 1; |
|
291 } |
|
292 else |
|
293 info[6] = 0; |
|
294 |
1251
|
295 double *dummy; |
|
296 int *idummy; |
3
|
297 |
|
298 if (restart) |
|
299 { |
|
300 restart = 0; |
|
301 info[0] = 0; |
|
302 } |
|
303 |
465
|
304 // again: |
3
|
305 |
1253
|
306 F77_FCN (ddassl, DDASSL) (ddassl_f, n, t, px, pxdot, tout, info, |
|
307 rel_tol, abs_tol, idid, rwork, lrw, iwork, |
|
308 liw, dummy, idummy, ddassl_j); |
3
|
309 |
|
310 switch (idid) |
|
311 { |
|
312 case 1: // A step was successfully taken in the |
|
313 // intermediate-output mode. The code has not yet reached |
|
314 // TOUT. |
|
315 break; |
|
316 case 2: // The integration to TSTOP was successfully completed |
|
317 // (T=TSTOP) by stepping exactly to TSTOP. |
|
318 break; |
|
319 case 3: // The integration to TOUT was successfully completed |
|
320 // (T=TOUT) by stepping past TOUT. Y(*) is obtained by |
|
321 // interpolation. YPRIME(*) is obtained by interpolation. |
|
322 break; |
|
323 case -1: // A large amount of work has been expended. (About 500 steps). |
|
324 case -2: // The error tolerances are too stringent. |
|
325 case -3: // The local error test cannot be satisfied because you |
|
326 // specified a zero component in ATOL and the |
|
327 // corresponding computed solution component is zero. |
|
328 // Thus, a pure relative error test is impossible for |
|
329 // this component. |
|
330 case -6: // DDASSL had repeated error test failures on the last |
|
331 // attempted step. |
|
332 case -7: // The corrector could not converge. |
|
333 case -8: // The matrix of partial derivatives is singular. |
|
334 case -9: // The corrector could not converge. There were repeated |
|
335 // error test failures in this step. |
|
336 case -10: // The corrector could not converge because IRES was |
|
337 // equal to minus one. |
|
338 case -11: // IRES equal to -2 was encountered and control is being |
|
339 // returned to the calling program. |
|
340 case -12: // DDASSL failed to compute the initial YPRIME. |
|
341 case -33: // The code has encountered trouble from which it cannot |
|
342 // recover. A message is printed explaining the trouble |
|
343 // and control is returned to the calling program. For |
|
344 // example, this occurs when invalid input is detected. |
|
345 default: |
256
|
346 integration_error = 1; |
3
|
347 break; |
|
348 } |
|
349 |
|
350 t = tout; |
|
351 |
|
352 return x; |
|
353 } |
|
354 |
|
355 Matrix |
305
|
356 DAE::integrate (const Vector& tout, Matrix& xdot_out) |
3
|
357 { |
|
358 Matrix retval; |
|
359 int n_out = tout.capacity (); |
|
360 |
|
361 if (n_out > 0 && n > 0) |
|
362 { |
|
363 retval.resize (n_out, n); |
|
364 xdot_out.resize (n_out, n); |
|
365 |
|
366 for (int i = 0; i < n; i++) |
|
367 { |
|
368 retval.elem (0, i) = x.elem (i); |
|
369 xdot_out.elem (0, i) = xdot.elem (i); |
|
370 } |
|
371 |
|
372 for (int j = 1; j < n_out; j++) |
|
373 { |
|
374 ColumnVector x_next = integrate (tout.elem (j)); |
256
|
375 |
|
376 if (integration_error) |
|
377 return retval; |
|
378 |
3
|
379 for (i = 0; i < n; i++) |
|
380 { |
|
381 retval.elem (j, i) = x_next.elem (i); |
|
382 xdot_out.elem (j, i) = xdot.elem (i); |
|
383 } |
|
384 } |
|
385 } |
|
386 |
|
387 return retval; |
|
388 } |
|
389 |
|
390 Matrix |
305
|
391 DAE::integrate (const Vector& tout, Matrix& xdot_out, const Vector& tcrit) |
3
|
392 { |
|
393 Matrix retval; |
|
394 int n_out = tout.capacity (); |
|
395 |
|
396 if (n_out > 0 && n > 0) |
|
397 { |
|
398 retval.resize (n_out, n); |
|
399 xdot_out.resize (n_out, n); |
|
400 |
|
401 for (int i = 0; i < n; i++) |
|
402 { |
|
403 retval.elem (0, i) = x.elem (i); |
|
404 xdot_out.elem (0, i) = xdot.elem (i); |
|
405 } |
|
406 |
|
407 int n_crit = tcrit.capacity (); |
|
408 |
|
409 if (n_crit > 0) |
|
410 { |
|
411 int i_crit = 0; |
|
412 int i_out = 1; |
|
413 double next_crit = tcrit.elem (0); |
|
414 double next_out; |
|
415 while (i_out < n_out) |
|
416 { |
|
417 int do_restart = 0; |
|
418 |
|
419 next_out = tout.elem (i_out); |
|
420 if (i_crit < n_crit) |
|
421 next_crit = tcrit.elem (i_crit); |
|
422 |
|
423 int save_output; |
|
424 double t_out; |
|
425 |
|
426 if (next_crit == next_out) |
|
427 { |
|
428 set_stop_time (next_crit); |
|
429 t_out = next_out; |
|
430 save_output = 1; |
|
431 i_out++; |
|
432 i_crit++; |
|
433 do_restart = 1; |
|
434 } |
|
435 else if (next_crit < next_out) |
|
436 { |
|
437 if (i_crit < n_crit) |
|
438 { |
|
439 set_stop_time (next_crit); |
|
440 t_out = next_crit; |
|
441 save_output = 0; |
|
442 i_crit++; |
|
443 do_restart = 1; |
|
444 } |
|
445 else |
|
446 { |
|
447 clear_stop_time (); |
|
448 t_out = next_out; |
|
449 save_output = 1; |
|
450 i_out++; |
|
451 } |
|
452 } |
|
453 else |
|
454 { |
|
455 set_stop_time (next_crit); |
|
456 t_out = next_out; |
|
457 save_output = 1; |
|
458 i_out++; |
|
459 } |
|
460 |
|
461 ColumnVector x_next = integrate (t_out); |
|
462 |
256
|
463 if (integration_error) |
|
464 return retval; |
|
465 |
3
|
466 if (save_output) |
|
467 { |
|
468 for (i = 0; i < n; i++) |
|
469 { |
|
470 retval.elem (i_out-1, i) = x_next.elem (i); |
|
471 xdot_out.elem (i_out-1, i) = xdot.elem (i); |
|
472 } |
|
473 } |
|
474 |
|
475 if (do_restart) |
|
476 force_restart (); |
|
477 } |
|
478 } |
|
479 else |
256
|
480 { |
|
481 retval = integrate (tout, xdot_out); |
|
482 |
|
483 if (integration_error) |
|
484 return retval; |
|
485 } |
3
|
486 } |
|
487 |
|
488 return retval; |
|
489 } |
289
|
490 |
|
491 /* |
|
492 ;;; Local Variables: *** |
|
493 ;;; mode: C++ *** |
|
494 ;;; page-delimiter: "^/\\*" *** |
|
495 ;;; End: *** |
|
496 */ |