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" |
|
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\ |
|
223 @ifinfo\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\ |
4115
|
235 @end ifinfo\n\ |
|
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\ |
|
252 The first argument, @var{fcn}, is a string that names the function to\n\ |
|
253 call to compute the vector of residuals for the set of equations.\n\ |
|
254 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\ |
|
264 If @var{fcn} is a two-element string array, the first element names\n\ |
|
265 the function @math{f} described above, and the second element names\n\ |
4117
|
266 a function to compute the modified Jacobian\n\ |
3990
|
267 \n\ |
4115
|
268 @tex\n\ |
|
269 $$\n\ |
|
270 J = {\\partial f \\over \\partial x}\n\ |
|
271 + c {\\partial f \\over \\partial \\dot{x}}\n\ |
|
272 $$\n\ |
|
273 @end tex\n\ |
|
274 @ifinfo\n\ |
3990
|
275 \n\ |
4115
|
276 @example\n\ |
|
277 df df\n\ |
|
278 jac = -- + c ------\n\ |
|
279 dx d xdot\n\ |
|
280 @end example\n\ |
|
281 \n\ |
|
282 @end ifinfo\n\ |
|
283 \n\ |
|
284 The modified Jacobian function must have the form\n\ |
|
285 \n\ |
|
286 @example\n\ |
3990
|
287 \n\ |
4115
|
288 @var{jac} = j (@var{x}, @var{xdot}, @var{t}, @var{c})\n\ |
|
289 \n\ |
|
290 @end example\n\ |
3990
|
291 \n\ |
4115
|
292 The optional second argument names a function that defines the\n\ |
|
293 constraint functions whose roots are desired during the integration.\n\ |
|
294 This function must have the form\n\ |
3990
|
295 \n\ |
4115
|
296 @example\n\ |
|
297 @var{g_out} = g (@var{x}, @var{t})\n\ |
|
298 @end example\n\ |
3990
|
299 \n\ |
4115
|
300 and return a vector of the constraint function values.\n\ |
|
301 If the value of any of the constraint functions changes sign, @sc{Dasrt}\n\ |
|
302 will attempt to stop the integration at the point of the sign change.\n\ |
|
303 \n\ |
|
304 If the name of the constraint function is omitted, @code{dasrt} solves\n\ |
4852
|
305 the same problem as @code{daspk} or @code{dassl}.\n\ |
3990
|
306 \n\ |
4115
|
307 Note that because of numerical errors in the constraint functions\n\ |
|
308 due to roundoff and integration error, @sc{Dasrt} may return false\n\ |
|
309 roots, or return the same root at two or more nearly equal values of\n\ |
|
310 @var{T}. If such false roots are suspected, the user should consider\n\ |
|
311 smaller error tolerances or higher precision in the evaluation of the\n\ |
|
312 constraint functions.\n\ |
3990
|
313 \n\ |
4115
|
314 If a root of some constraint function defines the end of the problem,\n\ |
|
315 the input to @sc{Dasrt} should nevertheless allow integration to a\n\ |
|
316 point slightly past that root, so that @sc{Dasrt} can locate the root\n\ |
|
317 by interpolation.\n\ |
3990
|
318 \n\ |
4115
|
319 The third and fourth arguments to @code{dasrt} specify the initial\n\ |
|
320 condition of the states and their derivatives, and the fourth argument\n\ |
|
321 specifies a vector of output times at which the solution is desired,\n\ |
|
322 including the time corresponding to the initial condition.\n\ |
|
323 \n\ |
|
324 The set of initial states and derivatives are not strictly required to\n\ |
|
325 be consistent. In practice, however, @sc{Dassl} is not very good at\n\ |
|
326 determining a consistent set for you, so it is best if you ensure that\n\ |
|
327 the initial values result in the function evaluating to zero.\n\ |
3990
|
328 \n\ |
4115
|
329 The sixth argument is optional, and may be used to specify a set of\n\ |
|
330 times that the DAE solver should not integrate past. It is useful for\n\ |
|
331 avoiding difficulties with singularities and points where there is a\n\ |
|
332 discontinuity in the derivative.\n\ |
3990
|
333 \n\ |
4115
|
334 After a successful computation, the value of @var{istate} will be\n\ |
|
335 greater than zero (consistent with the Fortran version of @sc{Dassl}).\n\ |
|
336 \n\ |
|
337 If the computation is not successful, the value of @var{istate} will be\n\ |
|
338 less than zero and @var{msg} will contain additional information.\n\ |
3990
|
339 \n\ |
|
340 You can use the function @code{dasrt_options} to set optional\n\ |
|
341 parameters for @code{dasrt}.\n\ |
5642
|
342 @seealso{daspk, dasrt, lsode, odessa}\n\ |
|
343 @end deftypefn") |
3990
|
344 { |
|
345 octave_value_list retval; |
|
346 |
4140
|
347 warned_fcn_imaginary = false; |
|
348 warned_jac_imaginary = false; |
|
349 warned_cf_imaginary = false; |
|
350 |
3990
|
351 unwind_protect::begin_frame ("Fdasrt"); |
|
352 |
|
353 unwind_protect_int (call_depth); |
|
354 call_depth++; |
|
355 |
|
356 if (call_depth > 1) |
|
357 DASRT_ABORT1 ("invalid recursive call"); |
|
358 |
|
359 int argp = 0; |
|
360 |
|
361 int nargin = args.length (); |
|
362 |
|
363 if (nargin < 4 || nargin > 6) |
|
364 { |
|
365 print_usage ("dasrt"); |
|
366 unwind_protect::run_frame ("Fdasrt"); |
|
367 return retval; |
|
368 } |
|
369 |
|
370 dasrt_f = 0; |
|
371 dasrt_j = 0; |
|
372 dasrt_cf = 0; |
|
373 |
|
374 // Check all the arguments. Are they the right animals? |
|
375 |
|
376 // Here's where I take care of f and j in one shot: |
|
377 |
|
378 octave_value f_arg = args(0); |
|
379 |
|
380 switch (f_arg.rows ()) |
|
381 { |
|
382 case 1: |
|
383 dasrt_f = extract_function |
|
384 (args(0), "dasrt", "__dasrt_fcn__", |
|
385 "function res = __dasrt_fcn__ (x, xdot, t) res = ", |
|
386 "; endfunction"); |
|
387 break; |
|
388 |
|
389 case 2: |
|
390 { |
|
391 string_vector tmp = args(0).all_strings (); |
|
392 |
|
393 if (! error_state) |
|
394 { |
|
395 dasrt_f = extract_function |
|
396 (tmp(0), "dasrt", "__dasrt_fcn__", |
|
397 "function res = __dasrt_fcn__ (x, xdot, t) res = ", |
|
398 "; endfunction"); |
|
399 |
|
400 if (dasrt_f) |
|
401 { |
|
402 dasrt_j = extract_function |
|
403 (tmp(1), "dasrt", "__dasrt_jac__", |
|
404 "function jac = __dasrt_jac__ (x, xdot, t, cj) jac = ", |
|
405 "; endfunction"); |
|
406 |
|
407 if (! dasrt_j) |
|
408 dasrt_f = 0; |
|
409 } |
|
410 } |
|
411 } |
|
412 break; |
|
413 |
|
414 default: |
|
415 DASRT_ABORT1 |
|
416 ("first arg should be a string or 2-element string array"); |
|
417 } |
|
418 |
|
419 if (error_state || (! dasrt_f)) |
|
420 DASRT_ABORT; |
|
421 |
|
422 DAERTFunc func (dasrt_user_f); |
|
423 |
|
424 argp++; |
|
425 |
|
426 if (args(1).is_string ()) |
|
427 { |
|
428 dasrt_cf = is_valid_function (args(1), "dasrt", true); |
|
429 |
|
430 if (! dasrt_cf) |
3992
|
431 DASRT_ABORT1 ("expecting function name as argument 2"); |
3990
|
432 |
|
433 argp++; |
|
434 |
|
435 func.set_constraint_function (dasrt_user_cf); |
|
436 } |
|
437 |
|
438 ColumnVector state (args(argp++).vector_value ()); |
|
439 |
|
440 if (error_state) |
|
441 DASRT_ABORT2 ("expecting state vector as argument %d", argp); |
|
442 |
3992
|
443 ColumnVector stateprime (args(argp++).vector_value ()); |
3990
|
444 |
|
445 if (error_state) |
|
446 DASRT_ABORT2 |
|
447 ("expecting time derivative of state vector as argument %d", argp); |
|
448 |
3994
|
449 ColumnVector out_times (args(argp++).vector_value ()); |
3990
|
450 |
|
451 if (error_state) |
|
452 DASRT_ABORT2 |
|
453 ("expecting output time vector as %s argument %d", argp); |
|
454 |
3994
|
455 double tzero = out_times (0); |
3990
|
456 |
|
457 ColumnVector crit_times; |
|
458 |
|
459 bool crit_times_set = false; |
|
460 |
|
461 if (argp < nargin) |
|
462 { |
|
463 crit_times = ColumnVector (args(argp++).vector_value ()); |
|
464 |
|
465 if (error_state) |
|
466 DASRT_ABORT2 |
|
467 ("expecting critical time vector as argument %d", argp); |
|
468 |
|
469 crit_times_set = true; |
|
470 } |
|
471 |
|
472 if (dasrt_j) |
|
473 func.set_jacobian_function (dasrt_user_j); |
|
474 |
|
475 DASRT_result output; |
|
476 |
3992
|
477 DASRT dae = DASRT (state, stateprime, tzero, func); |
3990
|
478 |
4122
|
479 dae.set_options (dasrt_opts); |
3990
|
480 |
|
481 if (crit_times_set) |
|
482 output = dae.integrate (out_times, crit_times); |
|
483 else |
|
484 output = dae.integrate (out_times); |
|
485 |
|
486 if (! error_state) |
|
487 { |
3997
|
488 std::string msg = dae.error_message (); |
|
489 |
|
490 retval(4) = msg; |
|
491 retval(3) = static_cast<double> (dae.integration_state ()); |
|
492 |
|
493 if (dae.integration_ok ()) |
|
494 { |
|
495 retval(2) = output.times (); |
|
496 retval(1) = output.deriv (); |
|
497 retval(0) = output.state (); |
|
498 } |
|
499 else |
|
500 { |
|
501 retval(2) = Matrix (); |
|
502 retval(1) = Matrix (); |
|
503 retval(0) = Matrix (); |
|
504 |
|
505 if (nargout < 4) |
|
506 error ("dasrt: %s", msg.c_str ()); |
|
507 } |
3990
|
508 } |
|
509 |
|
510 unwind_protect::run_frame ("Fdasrt"); |
|
511 |
|
512 return retval; |
|
513 } |
|
514 |
|
515 /* |
|
516 ;;; Local Variables: *** |
|
517 ;;; mode: C++ *** |
|
518 ;;; End: *** |
|
519 */ |