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