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