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 |
3990
|
46 // Global pointers for user defined function required by dassl. |
|
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\ |
|
238 @deftypefn {Loadable Function} {[@var{x}, @var{xdot}, @var{t}] =} dasrt (@var{fj} [, @var{g}], @var{x_0}, @var{xdot_0}, @var{t_out} [, @var{t_crit}])\n\ |
|
239 Solve a system of differential/algebraic equations with functional\n\ |
|
240 stopping criteria.\n\ |
|
241 \n\ |
|
242 The function to be integrated must be of the form:\n\ |
|
243 @example\n\ |
|
244 @var{res} = f (@var{x}, @var{xdot}, @var{t}) = 0\n\ |
|
245 @end example\n\ |
|
246 \n\ |
|
247 The stopping condition must be of the form:\n\ |
|
248 \n\ |
|
249 @example\n\ |
|
250 @var{res} = g (@var{x}, @var{t}) = 0\n\ |
|
251 @end example\n\ |
|
252 \n\ |
|
253 The Jacobian (if included) must be of the form:\n\ |
|
254 \n\ |
|
255 @example\n\ |
|
256 @var{jac} = j (@var{x}, @var{xdot}, @var{t}, @var{cj})\n\ |
|
257 = df/dx + cj*df/dxdot\n\ |
|
258 @end example\n\ |
|
259 \n\ |
|
260 @noindent\n\ |
|
261 The following inputs are entered:\n\ |
|
262 \n\ |
|
263 @table @var\n\ |
|
264 @item fj\n\ |
|
265 The string vector containing either @var{f} or both @var{f} and @var{j}.\n\ |
|
266 \n\ |
|
267 @item f\n\ |
|
268 The function to be integrated.\n\ |
|
269 \n\ |
|
270 @item g\n\ |
|
271 The function with the stopping conditions.\n\ |
|
272 \n\ |
|
273 @item j\n\ |
|
274 The optional Jacobian function. If not included, it will be approximated\n\ |
|
275 by finite differences.\n\ |
|
276 \n\ |
|
277 @item x_0\n\ |
|
278 The initial state.\n\ |
|
279 \n\ |
|
280 @item xdot_0\n\ |
|
281 The time derivative of the initial state.\n\ |
|
282 \n\ |
|
283 @item t_out\n\ |
|
284 The times at which the solution should be returned. This vector should\n\ |
|
285 include the initial time a the first value.\n\ |
|
286 \n\ |
|
287 @item t_crit\n\ |
|
288 The times at which the function is non-smooth or poorly behaved.\n\ |
|
289 \n\ |
|
290 @end table\n\ |
|
291 \n\ |
|
292 @noindent\n\ |
|
293 The following outputs are returned:\n\ |
|
294 \n\ |
|
295 @table @var\n\ |
|
296 @item x\n\ |
|
297 The states at each time in @var{t}.\n\ |
|
298 \n\ |
|
299 @item xdot\n\ |
|
300 The time derivative of the states at each time in @var{t}.\n\ |
|
301 \n\ |
|
302 @item t\n\ |
|
303 All the times in the requested output time vector up to the stopping\n\ |
|
304 criteria. The time at which the stopping criteria is achieved is returned\n\ |
|
305 as the last time in the vector.\n\ |
|
306 @end table\n\ |
|
307 \n\ |
|
308 You can use the function @code{dasrt_options} to set optional\n\ |
|
309 parameters for @code{dasrt}.\n\ |
|
310 @end deftypefn") |
|
311 { |
|
312 octave_value_list retval; |
|
313 |
|
314 unwind_protect::begin_frame ("Fdasrt"); |
|
315 |
|
316 unwind_protect_int (call_depth); |
|
317 call_depth++; |
|
318 |
|
319 if (call_depth > 1) |
|
320 DASRT_ABORT1 ("invalid recursive call"); |
|
321 |
|
322 int argp = 0; |
|
323 |
|
324 int nargin = args.length (); |
|
325 |
|
326 if (nargin < 4 || nargin > 6) |
|
327 { |
|
328 print_usage ("dasrt"); |
|
329 unwind_protect::run_frame ("Fdasrt"); |
|
330 return retval; |
|
331 } |
|
332 |
|
333 dasrt_f = 0; |
|
334 dasrt_j = 0; |
|
335 dasrt_cf = 0; |
|
336 |
|
337 // Check all the arguments. Are they the right animals? |
|
338 |
|
339 // Here's where I take care of f and j in one shot: |
|
340 |
|
341 octave_value f_arg = args(0); |
|
342 |
|
343 switch (f_arg.rows ()) |
|
344 { |
|
345 case 1: |
|
346 dasrt_f = extract_function |
|
347 (args(0), "dasrt", "__dasrt_fcn__", |
|
348 "function res = __dasrt_fcn__ (x, xdot, t) res = ", |
|
349 "; endfunction"); |
|
350 break; |
|
351 |
|
352 case 2: |
|
353 { |
|
354 string_vector tmp = args(0).all_strings (); |
|
355 |
|
356 if (! error_state) |
|
357 { |
|
358 dasrt_f = extract_function |
|
359 (tmp(0), "dasrt", "__dasrt_fcn__", |
|
360 "function res = __dasrt_fcn__ (x, xdot, t) res = ", |
|
361 "; endfunction"); |
|
362 |
|
363 if (dasrt_f) |
|
364 { |
|
365 dasrt_j = extract_function |
|
366 (tmp(1), "dasrt", "__dasrt_jac__", |
|
367 "function jac = __dasrt_jac__ (x, xdot, t, cj) jac = ", |
|
368 "; endfunction"); |
|
369 |
|
370 if (! dasrt_j) |
|
371 dasrt_f = 0; |
|
372 } |
|
373 } |
|
374 } |
|
375 break; |
|
376 |
|
377 default: |
|
378 DASRT_ABORT1 |
|
379 ("first arg should be a string or 2-element string array"); |
|
380 } |
|
381 |
|
382 if (error_state || (! dasrt_f)) |
|
383 DASRT_ABORT; |
|
384 |
|
385 DAERTFunc func (dasrt_user_f); |
|
386 |
|
387 argp++; |
|
388 |
|
389 if (args(1).is_string ()) |
|
390 { |
|
391 dasrt_cf = is_valid_function (args(1), "dasrt", true); |
|
392 |
|
393 if (! dasrt_cf) |
3992
|
394 DASRT_ABORT1 ("expecting function name as argument 2"); |
3990
|
395 |
|
396 argp++; |
|
397 |
|
398 func.set_constraint_function (dasrt_user_cf); |
|
399 } |
|
400 |
|
401 ColumnVector state (args(argp++).vector_value ()); |
|
402 |
|
403 if (error_state) |
|
404 DASRT_ABORT2 ("expecting state vector as argument %d", argp); |
|
405 |
3992
|
406 ColumnVector stateprime (args(argp++).vector_value ()); |
3990
|
407 |
|
408 if (error_state) |
|
409 DASRT_ABORT2 |
|
410 ("expecting time derivative of state vector as argument %d", argp); |
|
411 |
3994
|
412 ColumnVector out_times (args(argp++).vector_value ()); |
3990
|
413 |
|
414 if (error_state) |
|
415 DASRT_ABORT2 |
|
416 ("expecting output time vector as %s argument %d", argp); |
|
417 |
3994
|
418 double tzero = out_times (0); |
3990
|
419 |
|
420 ColumnVector crit_times; |
|
421 |
|
422 bool crit_times_set = false; |
|
423 |
|
424 if (argp < nargin) |
|
425 { |
|
426 crit_times = ColumnVector (args(argp++).vector_value ()); |
|
427 |
|
428 if (error_state) |
|
429 DASRT_ABORT2 |
|
430 ("expecting critical time vector as argument %d", argp); |
|
431 |
|
432 crit_times_set = true; |
|
433 } |
|
434 |
|
435 if (dasrt_j) |
|
436 func.set_jacobian_function (dasrt_user_j); |
|
437 |
|
438 DASRT_result output; |
|
439 |
3992
|
440 DASRT dae = DASRT (state, stateprime, tzero, func); |
3990
|
441 |
|
442 dae.copy (dasrt_opts); |
|
443 |
|
444 if (crit_times_set) |
|
445 output = dae.integrate (out_times, crit_times); |
|
446 else |
|
447 output = dae.integrate (out_times); |
|
448 |
|
449 if (! error_state) |
|
450 { |
3997
|
451 std::string msg = dae.error_message (); |
|
452 |
|
453 retval(4) = msg; |
|
454 retval(3) = static_cast<double> (dae.integration_state ()); |
|
455 |
|
456 if (dae.integration_ok ()) |
|
457 { |
|
458 retval(2) = output.times (); |
|
459 retval(1) = output.deriv (); |
|
460 retval(0) = output.state (); |
|
461 } |
|
462 else |
|
463 { |
|
464 retval(2) = Matrix (); |
|
465 retval(1) = Matrix (); |
|
466 retval(0) = Matrix (); |
|
467 |
|
468 if (nargout < 4) |
|
469 error ("dasrt: %s", msg.c_str ()); |
|
470 } |
3990
|
471 } |
|
472 |
|
473 unwind_protect::run_frame ("Fdasrt"); |
|
474 |
|
475 return retval; |
|
476 } |
|
477 |
|
478 /* |
|
479 ;;; Local Variables: *** |
|
480 ;;; mode: C++ *** |
|
481 ;;; End: *** |
|
482 */ |