Mercurial > octave-antonio
annotate src/DLD-FUNCTIONS/dasrt.cc @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | 5b23faa8113c |
children | 7c02ec148a3c |
rev | line source |
---|---|
3990 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2009 John W. Eaton |
3990 | 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
3990 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
3990 | 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" | |
5729 | 38 #include "ov-cell.h" |
3990 | 39 #include "pager.h" |
40 #include "parse.h" | |
41 #include "unwind-prot.h" | |
42 #include "utils.h" | |
43 #include "variables.h" | |
44 | |
3998 | 45 #include "DASRT-opts.cc" |
46 | |
4115 | 47 // Global pointers for user defined function required by dasrt. |
3990 | 48 static octave_function *dasrt_f; |
49 static octave_function *dasrt_j; | |
50 static octave_function *dasrt_cf; | |
51 | |
4140 | 52 // Have we warned about imaginary values returned from user function? |
53 static bool warned_fcn_imaginary = false; | |
54 static bool warned_jac_imaginary = false; | |
55 static bool warned_cf_imaginary = false; | |
56 | |
3990 | 57 // Is this a recursive call? |
58 static int call_depth = 0; | |
59 | |
60 static ColumnVector | |
4628 | 61 dasrt_user_f (const ColumnVector& x, const ColumnVector& xdot, |
5275 | 62 double t, octave_idx_type&) |
3990 | 63 { |
64 ColumnVector retval; | |
65 | |
4628 | 66 assert (x.capacity () == xdot.capacity ()); |
3990 | 67 |
4628 | 68 octave_value_list args; |
3990 | 69 |
3993 | 70 args(2) = t; |
4628 | 71 args(1) = xdot; |
72 args(0) = x; | |
3990 | 73 |
74 if (dasrt_f) | |
75 { | |
76 octave_value_list tmp = dasrt_f->do_multi_index_op (1, args); | |
77 | |
78 if (error_state) | |
79 { | |
80 gripe_user_supplied_eval ("dasrt"); | |
81 return retval; | |
82 } | |
83 | |
84 if (tmp.length () > 0 && tmp(0).is_defined ()) | |
85 { | |
4140 | 86 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
87 { | |
88 warning ("dasrt: ignoring imaginary part returned from user-supplied function"); | |
89 warned_fcn_imaginary = true; | |
90 } | |
91 | |
3990 | 92 retval = ColumnVector (tmp(0).vector_value ()); |
93 | |
94 if (error_state || retval.length () == 0) | |
95 gripe_user_supplied_eval ("dasrt"); | |
96 } | |
97 else | |
98 gripe_user_supplied_eval ("dasrt"); | |
99 } | |
100 | |
101 return retval; | |
102 } | |
103 | |
104 static ColumnVector | |
105 dasrt_user_cf (const ColumnVector& x, double t) | |
106 { | |
107 ColumnVector retval; | |
108 | |
109 octave_value_list args; | |
110 | |
111 args(1) = t; | |
4628 | 112 args(0) = x; |
3990 | 113 |
114 if (dasrt_cf) | |
115 { | |
116 octave_value_list tmp = dasrt_cf->do_multi_index_op (1, args); | |
117 | |
118 if (error_state) | |
119 { | |
120 gripe_user_supplied_eval ("dasrt"); | |
121 return retval; | |
122 } | |
123 | |
124 if (tmp.length () > 0 && tmp(0).is_defined ()) | |
125 { | |
4140 | 126 if (! warned_cf_imaginary && tmp(0).is_complex_type ()) |
127 { | |
128 warning ("dasrt: ignoring imaginary part returned from user-supplied constraint function"); | |
129 warned_cf_imaginary = true; | |
130 } | |
131 | |
3990 | 132 retval = ColumnVector (tmp(0).vector_value ()); |
133 | |
134 if (error_state || retval.length () == 0) | |
135 gripe_user_supplied_eval ("dasrt"); | |
136 } | |
137 else | |
138 gripe_user_supplied_eval ("dasrt"); | |
139 } | |
140 | |
141 return retval; | |
142 } | |
143 | |
144 static Matrix | |
3993 | 145 dasrt_user_j (const ColumnVector& x, const ColumnVector& xdot, |
146 double t, double cj) | |
3990 | 147 { |
148 Matrix retval; | |
149 | |
4628 | 150 assert (x.capacity () == xdot.capacity ()); |
3990 | 151 |
3993 | 152 octave_value_list args; |
153 | |
154 args(3) = cj; | |
155 args(2) = t; | |
4628 | 156 args(1) = xdot; |
157 args(0) = x; | |
3990 | 158 |
3993 | 159 if (dasrt_j) |
160 { | |
161 octave_value_list tmp = dasrt_j->do_multi_index_op (1, args); | |
3990 | 162 |
163 if (error_state) | |
164 { | |
165 gripe_user_supplied_eval ("dasrt"); | |
166 return retval; | |
167 } | |
168 | |
3993 | 169 int tlen = tmp.length (); |
170 if (tlen > 0 && tmp(0).is_defined ()) | |
3990 | 171 { |
4140 | 172 if (! warned_jac_imaginary && tmp(0).is_complex_type ()) |
173 { | |
174 warning ("dasrt: ignoring imaginary part returned from user-supplied jacobian function"); | |
175 warned_jac_imaginary = true; | |
176 } | |
177 | |
3990 | 178 retval = tmp(0).matrix_value (); |
179 | |
180 if (error_state || retval.length () == 0) | |
181 gripe_user_supplied_eval ("dasrt"); | |
182 } | |
183 else | |
184 gripe_user_supplied_eval ("dasrt"); | |
185 } | |
186 | |
187 return retval; | |
188 } | |
189 | |
190 #define DASRT_ABORT \ | |
191 do \ | |
192 { \ | |
193 unwind_protect::run_frame ("Fdasrt"); \ | |
194 return retval; \ | |
195 } \ | |
196 while (0) | |
197 | |
198 #define DASRT_ABORT1(msg) \ | |
199 do \ | |
200 { \ | |
4035 | 201 ::error ("dasrt: " msg); \ |
3990 | 202 DASRT_ABORT; \ |
203 } \ | |
204 while (0) | |
205 | |
206 #define DASRT_ABORT2(fmt, arg) \ | |
207 do \ | |
208 { \ | |
4035 | 209 ::error ("dasrt: " fmt, arg); \ |
3990 | 210 DASRT_ABORT; \ |
211 } \ | |
212 while (0) | |
213 | |
3997 | 214 DEFUN_DLD (dasrt, args, nargout, |
3990 | 215 "-*- texinfo -*-\n\ |
4115 | 216 @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\ |
217 Solve the set of differential-algebraic equations\n\ | |
218 @tex\n\ | |
4852 | 219 $$ 0 = f (x, \\dot{x}, t) $$\n\ |
4115 | 220 with\n\ |
221 $$ x(t_0) = x_0, \\dot{x}(t_0) = \\dot{x}_0 $$\n\ | |
222 @end tex\n\ | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
223 @ifnottex\n\ |
3990 | 224 \n\ |
225 @example\n\ | |
4852 | 226 0 = f (x, xdot, t)\n\ |
4115 | 227 @end example\n\ |
228 \n\ | |
229 with\n\ | |
230 \n\ | |
231 @example\n\ | |
232 x(t_0) = x_0, xdot(t_0) = xdot_0\n\ | |
3990 | 233 @end example\n\ |
234 \n\ | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
235 @end ifnottex\n\ |
4115 | 236 with functional stopping criteria (root solving).\n\ |
237 \n\ | |
238 The solution is returned in the matrices @var{x} and @var{xdot},\n\ | |
239 with each row in the result matrices corresponding to one of the\n\ | |
240 elements in the vector @var{t_out}. The first element of @var{t}\n\ | |
241 should be @math{t_0} and correspond to the initial state of the\n\ | |
242 system @var{x_0} and its derivative @var{xdot_0}, so that the first\n\ | |
243 row of the output @var{x} is @var{x_0} and the first row\n\ | |
244 of the output @var{xdot} is @var{xdot_0}.\n\ | |
245 \n\ | |
246 The vector @var{t} provides an upper limit on the length of the\n\ | |
247 integration. If the stopping condition is met, the vector\n\ | |
248 @var{t_out} will be shorter than @var{t}, and the final element of\n\ | |
249 @var{t_out} will be the point at which the stopping condition was met,\n\ | |
250 and may not correspond to any element of the vector @var{t}.\n\ | |
251 \n\ | |
8787 | 252 The first argument, @var{fcn}, is a string, inline, or function handle\n\ |
253 that names the function @math{f} to call to compute the vector of\n\ | |
254 residuals for the set of equations. It must have the form\n\ | |
3990 | 255 \n\ |
256 @example\n\ | |
4115 | 257 @var{res} = f (@var{x}, @var{xdot}, @var{t})\n\ |
3990 | 258 @end example\n\ |
259 \n\ | |
260 @noindent\n\ | |
4115 | 261 in which @var{x}, @var{xdot}, and @var{res} are vectors, and @var{t} is a\n\ |
262 scalar.\n\ | |
263 \n\ | |
8787 | 264 If @var{fcn} is a two-element string array or a two-element cell array\n\ |
265 of strings, inlines, or function handles, the first element names\n\ | |
266 the function @math{f} described above, and the second element names a\n\ | |
267 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\ | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
275 @ifnottex\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\ | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
283 @end ifnottex\n\ |
4115 | 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 { | |
5823 | 366 print_usage (); |
3990 | 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 */ |