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