3990
|
1 /* |
|
2 |
|
3 Copyright (C) 2002 John W. Eaton |
|
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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
4052
|
27 #include <iostream> |
3990
|
28 #include <string> |
|
29 |
|
30 #include "DASRT.h" |
|
31 #include "lo-mappers.h" |
|
32 |
|
33 #include "defun-dld.h" |
|
34 #include "error.h" |
|
35 #include "gripes.h" |
|
36 #include "oct-obj.h" |
|
37 #include "ov-fcn.h" |
|
38 #include "pager.h" |
|
39 #include "parse.h" |
|
40 #include "unwind-prot.h" |
|
41 #include "utils.h" |
|
42 #include "variables.h" |
|
43 |
3998
|
44 #include "DASRT-opts.cc" |
|
45 |
4115
|
46 // Global pointers for user defined function required by dasrt. |
3990
|
47 static octave_function *dasrt_f; |
|
48 static octave_function *dasrt_j; |
|
49 static octave_function *dasrt_cf; |
|
50 |
4140
|
51 // Have we warned about imaginary values returned from user function? |
|
52 static bool warned_fcn_imaginary = false; |
|
53 static bool warned_jac_imaginary = false; |
|
54 static bool warned_cf_imaginary = false; |
|
55 |
3990
|
56 // Is this a recursive call? |
|
57 static int call_depth = 0; |
|
58 |
|
59 static ColumnVector |
|
60 dasrt_user_f (const ColumnVector& x, const ColumnVector& xprime, |
3993
|
61 double t, int& ires) |
3990
|
62 { |
|
63 ColumnVector retval; |
|
64 |
|
65 octave_value_list args; |
|
66 |
|
67 int n = x.length (); |
|
68 |
3993
|
69 args(2) = t; |
|
70 |
3990
|
71 if (n > 1) |
|
72 { |
3993
|
73 args(1) = xprime; |
3990
|
74 args(0) = x; |
|
75 } |
|
76 else if (n == 1) |
|
77 { |
3993
|
78 args(1) = xprime(0); |
3990
|
79 args(0) = x(0); |
|
80 } |
|
81 else |
|
82 { |
3993
|
83 args(1) = Matrix (); |
3990
|
84 args(0) = Matrix (); |
|
85 } |
|
86 |
|
87 if (dasrt_f) |
|
88 { |
|
89 octave_value_list tmp = dasrt_f->do_multi_index_op (1, args); |
|
90 |
|
91 if (error_state) |
|
92 { |
|
93 gripe_user_supplied_eval ("dasrt"); |
|
94 return retval; |
|
95 } |
|
96 |
|
97 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
98 { |
4140
|
99 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
|
100 { |
|
101 warning ("dasrt: ignoring imaginary part returned from user-supplied function"); |
|
102 warned_fcn_imaginary = true; |
|
103 } |
|
104 |
3990
|
105 retval = ColumnVector (tmp(0).vector_value ()); |
|
106 |
|
107 if (error_state || retval.length () == 0) |
|
108 gripe_user_supplied_eval ("dasrt"); |
|
109 } |
|
110 else |
|
111 gripe_user_supplied_eval ("dasrt"); |
|
112 } |
|
113 |
|
114 return retval; |
|
115 } |
|
116 |
|
117 static ColumnVector |
|
118 dasrt_user_cf (const ColumnVector& x, double t) |
|
119 { |
|
120 ColumnVector retval; |
|
121 |
|
122 octave_value_list args; |
|
123 |
|
124 int n = x.length (); |
|
125 |
|
126 if (n > 1) |
|
127 args(0) = x; |
|
128 else if (n == 1) |
|
129 args(0) = x(0); |
|
130 else |
|
131 args(0) = Matrix (); |
|
132 |
|
133 args(1) = t; |
|
134 |
|
135 if (dasrt_cf) |
|
136 { |
|
137 octave_value_list tmp = dasrt_cf->do_multi_index_op (1, args); |
|
138 |
|
139 if (error_state) |
|
140 { |
|
141 gripe_user_supplied_eval ("dasrt"); |
|
142 return retval; |
|
143 } |
|
144 |
|
145 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
146 { |
4140
|
147 if (! warned_cf_imaginary && tmp(0).is_complex_type ()) |
|
148 { |
|
149 warning ("dasrt: ignoring imaginary part returned from user-supplied constraint function"); |
|
150 warned_cf_imaginary = true; |
|
151 } |
|
152 |
3990
|
153 retval = ColumnVector (tmp(0).vector_value ()); |
|
154 |
|
155 if (error_state || retval.length () == 0) |
|
156 gripe_user_supplied_eval ("dasrt"); |
|
157 } |
|
158 else |
|
159 gripe_user_supplied_eval ("dasrt"); |
|
160 } |
|
161 |
|
162 return retval; |
|
163 } |
|
164 |
|
165 static Matrix |
3993
|
166 dasrt_user_j (const ColumnVector& x, const ColumnVector& xdot, |
|
167 double t, double cj) |
3990
|
168 { |
|
169 Matrix retval; |
|
170 |
3993
|
171 int nstates = x.capacity (); |
|
172 |
|
173 assert (nstates == xdot.capacity ()); |
3990
|
174 |
3993
|
175 octave_value_list args; |
|
176 |
|
177 args(3) = cj; |
|
178 args(2) = t; |
3990
|
179 |
3993
|
180 if (nstates > 1) |
|
181 { |
|
182 Matrix m1 (nstates, 1); |
|
183 Matrix m2 (nstates, 1); |
|
184 for (int i = 0; i < nstates; i++) |
|
185 { |
|
186 m1 (i, 0) = x (i); |
|
187 m2 (i, 0) = xdot (i); |
|
188 } |
|
189 octave_value state (m1); |
|
190 octave_value deriv (m2); |
|
191 args(1) = deriv; |
|
192 args(0) = state; |
|
193 } |
|
194 else |
|
195 { |
|
196 double d1 = x (0); |
|
197 double d2 = xdot (0); |
|
198 octave_value state (d1); |
|
199 octave_value deriv (d2); |
|
200 args(1) = deriv; |
|
201 args(0) = state; |
|
202 } |
3990
|
203 |
3993
|
204 if (dasrt_j) |
|
205 { |
|
206 octave_value_list tmp = dasrt_j->do_multi_index_op (1, args); |
3990
|
207 |
|
208 if (error_state) |
|
209 { |
|
210 gripe_user_supplied_eval ("dasrt"); |
|
211 return retval; |
|
212 } |
|
213 |
3993
|
214 int tlen = tmp.length (); |
|
215 if (tlen > 0 && tmp(0).is_defined ()) |
3990
|
216 { |
4140
|
217 if (! warned_jac_imaginary && tmp(0).is_complex_type ()) |
|
218 { |
|
219 warning ("dasrt: ignoring imaginary part returned from user-supplied jacobian function"); |
|
220 warned_jac_imaginary = true; |
|
221 } |
|
222 |
3990
|
223 retval = tmp(0).matrix_value (); |
|
224 |
|
225 if (error_state || retval.length () == 0) |
|
226 gripe_user_supplied_eval ("dasrt"); |
|
227 } |
|
228 else |
|
229 gripe_user_supplied_eval ("dasrt"); |
|
230 } |
|
231 |
|
232 return retval; |
|
233 } |
|
234 |
|
235 #define DASRT_ABORT \ |
|
236 do \ |
|
237 { \ |
|
238 unwind_protect::run_frame ("Fdasrt"); \ |
|
239 return retval; \ |
|
240 } \ |
|
241 while (0) |
|
242 |
|
243 #define DASRT_ABORT1(msg) \ |
|
244 do \ |
|
245 { \ |
4035
|
246 ::error ("dasrt: " msg); \ |
3990
|
247 DASRT_ABORT; \ |
|
248 } \ |
|
249 while (0) |
|
250 |
|
251 #define DASRT_ABORT2(fmt, arg) \ |
|
252 do \ |
|
253 { \ |
4035
|
254 ::error ("dasrt: " fmt, arg); \ |
3990
|
255 DASRT_ABORT; \ |
|
256 } \ |
|
257 while (0) |
|
258 |
3997
|
259 DEFUN_DLD (dasrt, args, nargout, |
3990
|
260 "-*- texinfo -*-\n\ |
4115
|
261 @deftypefn {Loadable Function} {[@var{x}, @var{xdot}, @var{t_out}, @var{istat}, @var{msg}] =} dasrt (@var{fcn} [, @var{g}], @var{x_0}, @var{xdot_0}, @var{t} [, @var{t_crit}])\n\ |
|
262 Solve the set of differential-algebraic equations\n\ |
|
263 @tex\n\ |
|
264 $$ 0 = f (\\dot{x}, x, t) $$\n\ |
|
265 with\n\ |
|
266 $$ x(t_0) = x_0, \\dot{x}(t_0) = \\dot{x}_0 $$\n\ |
|
267 @end tex\n\ |
|
268 @ifinfo\n\ |
3990
|
269 \n\ |
|
270 @example\n\ |
4115
|
271 0 = f (xdot, x, t)\n\ |
|
272 @end example\n\ |
|
273 \n\ |
|
274 with\n\ |
|
275 \n\ |
|
276 @example\n\ |
|
277 x(t_0) = x_0, xdot(t_0) = xdot_0\n\ |
3990
|
278 @end example\n\ |
|
279 \n\ |
4115
|
280 @end ifinfo\n\ |
|
281 with functional stopping criteria (root solving).\n\ |
|
282 \n\ |
|
283 The solution is returned in the matrices @var{x} and @var{xdot},\n\ |
|
284 with each row in the result matrices corresponding to one of the\n\ |
|
285 elements in the vector @var{t_out}. The first element of @var{t}\n\ |
|
286 should be @math{t_0} and correspond to the initial state of the\n\ |
|
287 system @var{x_0} and its derivative @var{xdot_0}, so that the first\n\ |
|
288 row of the output @var{x} is @var{x_0} and the first row\n\ |
|
289 of the output @var{xdot} is @var{xdot_0}.\n\ |
|
290 \n\ |
|
291 The vector @var{t} provides an upper limit on the length of the\n\ |
|
292 integration. If the stopping condition is met, the vector\n\ |
|
293 @var{t_out} will be shorter than @var{t}, and the final element of\n\ |
|
294 @var{t_out} will be the point at which the stopping condition was met,\n\ |
|
295 and may not correspond to any element of the vector @var{t}.\n\ |
|
296 \n\ |
|
297 The first argument, @var{fcn}, is a string that names the function to\n\ |
|
298 call to compute the vector of residuals for the set of equations.\n\ |
|
299 It must have the form\n\ |
3990
|
300 \n\ |
|
301 @example\n\ |
4115
|
302 @var{res} = f (@var{x}, @var{xdot}, @var{t})\n\ |
3990
|
303 @end example\n\ |
|
304 \n\ |
|
305 @noindent\n\ |
4115
|
306 in which @var{x}, @var{xdot}, and @var{res} are vectors, and @var{t} is a\n\ |
|
307 scalar.\n\ |
|
308 \n\ |
|
309 If @var{fcn} is a two-element string array, the first element names\n\ |
|
310 the function @math{f} described above, and the second element names\n\ |
4117
|
311 a function to compute the modified Jacobian\n\ |
3990
|
312 \n\ |
4115
|
313 @tex\n\ |
|
314 $$\n\ |
|
315 J = {\\partial f \\over \\partial x}\n\ |
|
316 + c {\\partial f \\over \\partial \\dot{x}}\n\ |
|
317 $$\n\ |
|
318 @end tex\n\ |
|
319 @ifinfo\n\ |
3990
|
320 \n\ |
4115
|
321 @example\n\ |
|
322 df df\n\ |
|
323 jac = -- + c ------\n\ |
|
324 dx d xdot\n\ |
|
325 @end example\n\ |
|
326 \n\ |
|
327 @end ifinfo\n\ |
|
328 \n\ |
|
329 The modified Jacobian function must have the form\n\ |
|
330 \n\ |
|
331 @example\n\ |
3990
|
332 \n\ |
4115
|
333 @var{jac} = j (@var{x}, @var{xdot}, @var{t}, @var{c})\n\ |
|
334 \n\ |
|
335 @end example\n\ |
3990
|
336 \n\ |
4115
|
337 The optional second argument names a function that defines the\n\ |
|
338 constraint functions whose roots are desired during the integration.\n\ |
|
339 This function must have the form\n\ |
3990
|
340 \n\ |
4115
|
341 @example\n\ |
|
342 @var{g_out} = g (@var{x}, @var{t})\n\ |
|
343 @end example\n\ |
3990
|
344 \n\ |
4115
|
345 and return a vector of the constraint function values.\n\ |
|
346 If the value of any of the constraint functions changes sign, @sc{Dasrt}\n\ |
|
347 will attempt to stop the integration at the point of the sign change.\n\ |
|
348 \n\ |
|
349 If the name of the constraint function is omitted, @code{dasrt} solves\n\ |
4117
|
350 the saem problem as @code{daspk} or @code{dassl}.\n\ |
3990
|
351 \n\ |
4115
|
352 Note that because of numerical errors in the constraint functions\n\ |
|
353 due to roundoff and integration error, @sc{Dasrt} may return false\n\ |
|
354 roots, or return the same root at two or more nearly equal values of\n\ |
|
355 @var{T}. If such false roots are suspected, the user should consider\n\ |
|
356 smaller error tolerances or higher precision in the evaluation of the\n\ |
|
357 constraint functions.\n\ |
3990
|
358 \n\ |
4115
|
359 If a root of some constraint function defines the end of the problem,\n\ |
|
360 the input to @sc{Dasrt} should nevertheless allow integration to a\n\ |
|
361 point slightly past that root, so that @sc{Dasrt} can locate the root\n\ |
|
362 by interpolation.\n\ |
3990
|
363 \n\ |
4115
|
364 The third and fourth arguments to @code{dasrt} specify the initial\n\ |
|
365 condition of the states and their derivatives, and the fourth argument\n\ |
|
366 specifies a vector of output times at which the solution is desired,\n\ |
|
367 including the time corresponding to the initial condition.\n\ |
|
368 \n\ |
|
369 The set of initial states and derivatives are not strictly required to\n\ |
|
370 be consistent. In practice, however, @sc{Dassl} is not very good at\n\ |
|
371 determining a consistent set for you, so it is best if you ensure that\n\ |
|
372 the initial values result in the function evaluating to zero.\n\ |
3990
|
373 \n\ |
4115
|
374 The sixth argument is optional, and may be used to specify a set of\n\ |
|
375 times that the DAE solver should not integrate past. It is useful for\n\ |
|
376 avoiding difficulties with singularities and points where there is a\n\ |
|
377 discontinuity in the derivative.\n\ |
3990
|
378 \n\ |
4115
|
379 After a successful computation, the value of @var{istate} will be\n\ |
|
380 greater than zero (consistent with the Fortran version of @sc{Dassl}).\n\ |
|
381 \n\ |
|
382 If the computation is not successful, the value of @var{istate} will be\n\ |
|
383 less than zero and @var{msg} will contain additional information.\n\ |
3990
|
384 \n\ |
|
385 You can use the function @code{dasrt_options} to set optional\n\ |
|
386 parameters for @code{dasrt}.\n\ |
4115
|
387 @end deftypefn\n\ |
|
388 @seealso{daspk, dasrt, lsode, odessa}") |
3990
|
389 { |
|
390 octave_value_list retval; |
|
391 |
4140
|
392 warned_fcn_imaginary = false; |
|
393 warned_jac_imaginary = false; |
|
394 warned_cf_imaginary = false; |
|
395 |
3990
|
396 unwind_protect::begin_frame ("Fdasrt"); |
|
397 |
|
398 unwind_protect_int (call_depth); |
|
399 call_depth++; |
|
400 |
|
401 if (call_depth > 1) |
|
402 DASRT_ABORT1 ("invalid recursive call"); |
|
403 |
|
404 int argp = 0; |
|
405 |
|
406 int nargin = args.length (); |
|
407 |
|
408 if (nargin < 4 || nargin > 6) |
|
409 { |
|
410 print_usage ("dasrt"); |
|
411 unwind_protect::run_frame ("Fdasrt"); |
|
412 return retval; |
|
413 } |
|
414 |
|
415 dasrt_f = 0; |
|
416 dasrt_j = 0; |
|
417 dasrt_cf = 0; |
|
418 |
|
419 // Check all the arguments. Are they the right animals? |
|
420 |
|
421 // Here's where I take care of f and j in one shot: |
|
422 |
|
423 octave_value f_arg = args(0); |
|
424 |
|
425 switch (f_arg.rows ()) |
|
426 { |
|
427 case 1: |
|
428 dasrt_f = extract_function |
|
429 (args(0), "dasrt", "__dasrt_fcn__", |
|
430 "function res = __dasrt_fcn__ (x, xdot, t) res = ", |
|
431 "; endfunction"); |
|
432 break; |
|
433 |
|
434 case 2: |
|
435 { |
|
436 string_vector tmp = args(0).all_strings (); |
|
437 |
|
438 if (! error_state) |
|
439 { |
|
440 dasrt_f = extract_function |
|
441 (tmp(0), "dasrt", "__dasrt_fcn__", |
|
442 "function res = __dasrt_fcn__ (x, xdot, t) res = ", |
|
443 "; endfunction"); |
|
444 |
|
445 if (dasrt_f) |
|
446 { |
|
447 dasrt_j = extract_function |
|
448 (tmp(1), "dasrt", "__dasrt_jac__", |
|
449 "function jac = __dasrt_jac__ (x, xdot, t, cj) jac = ", |
|
450 "; endfunction"); |
|
451 |
|
452 if (! dasrt_j) |
|
453 dasrt_f = 0; |
|
454 } |
|
455 } |
|
456 } |
|
457 break; |
|
458 |
|
459 default: |
|
460 DASRT_ABORT1 |
|
461 ("first arg should be a string or 2-element string array"); |
|
462 } |
|
463 |
|
464 if (error_state || (! dasrt_f)) |
|
465 DASRT_ABORT; |
|
466 |
|
467 DAERTFunc func (dasrt_user_f); |
|
468 |
|
469 argp++; |
|
470 |
|
471 if (args(1).is_string ()) |
|
472 { |
|
473 dasrt_cf = is_valid_function (args(1), "dasrt", true); |
|
474 |
|
475 if (! dasrt_cf) |
3992
|
476 DASRT_ABORT1 ("expecting function name as argument 2"); |
3990
|
477 |
|
478 argp++; |
|
479 |
|
480 func.set_constraint_function (dasrt_user_cf); |
|
481 } |
|
482 |
|
483 ColumnVector state (args(argp++).vector_value ()); |
|
484 |
|
485 if (error_state) |
|
486 DASRT_ABORT2 ("expecting state vector as argument %d", argp); |
|
487 |
3992
|
488 ColumnVector stateprime (args(argp++).vector_value ()); |
3990
|
489 |
|
490 if (error_state) |
|
491 DASRT_ABORT2 |
|
492 ("expecting time derivative of state vector as argument %d", argp); |
|
493 |
3994
|
494 ColumnVector out_times (args(argp++).vector_value ()); |
3990
|
495 |
|
496 if (error_state) |
|
497 DASRT_ABORT2 |
|
498 ("expecting output time vector as %s argument %d", argp); |
|
499 |
3994
|
500 double tzero = out_times (0); |
3990
|
501 |
|
502 ColumnVector crit_times; |
|
503 |
|
504 bool crit_times_set = false; |
|
505 |
|
506 if (argp < nargin) |
|
507 { |
|
508 crit_times = ColumnVector (args(argp++).vector_value ()); |
|
509 |
|
510 if (error_state) |
|
511 DASRT_ABORT2 |
|
512 ("expecting critical time vector as argument %d", argp); |
|
513 |
|
514 crit_times_set = true; |
|
515 } |
|
516 |
|
517 if (dasrt_j) |
|
518 func.set_jacobian_function (dasrt_user_j); |
|
519 |
|
520 DASRT_result output; |
|
521 |
3992
|
522 DASRT dae = DASRT (state, stateprime, tzero, func); |
3990
|
523 |
4122
|
524 dae.set_options (dasrt_opts); |
3990
|
525 |
|
526 if (crit_times_set) |
|
527 output = dae.integrate (out_times, crit_times); |
|
528 else |
|
529 output = dae.integrate (out_times); |
|
530 |
|
531 if (! error_state) |
|
532 { |
3997
|
533 std::string msg = dae.error_message (); |
|
534 |
|
535 retval(4) = msg; |
|
536 retval(3) = static_cast<double> (dae.integration_state ()); |
|
537 |
|
538 if (dae.integration_ok ()) |
|
539 { |
|
540 retval(2) = output.times (); |
|
541 retval(1) = output.deriv (); |
|
542 retval(0) = output.state (); |
|
543 } |
|
544 else |
|
545 { |
|
546 retval(2) = Matrix (); |
|
547 retval(1) = Matrix (); |
|
548 retval(0) = Matrix (); |
|
549 |
|
550 if (nargout < 4) |
|
551 error ("dasrt: %s", msg.c_str ()); |
|
552 } |
3990
|
553 } |
|
554 |
|
555 unwind_protect::run_frame ("Fdasrt"); |
|
556 |
|
557 return retval; |
|
558 } |
|
559 |
|
560 /* |
|
561 ;;; Local Variables: *** |
|
562 ;;; mode: C++ *** |
|
563 ;;; End: *** |
|
564 */ |