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