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