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