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