2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 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. |
2928
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <string> |
|
29 |
3567
|
30 #include <iomanip> |
3523
|
31 #include <iostream> |
2928
|
32 |
|
33 #include "DASSL.h" |
|
34 |
|
35 #include "defun-dld.h" |
|
36 #include "error.h" |
|
37 #include "gripes.h" |
|
38 #include "oct-obj.h" |
2968
|
39 #include "ov-fcn.h" |
5729
|
40 #include "ov-cell.h" |
2928
|
41 #include "pager.h" |
3243
|
42 #include "unwind-prot.h" |
2928
|
43 #include "utils.h" |
|
44 #include "variables.h" |
|
45 |
3998
|
46 #include "DASSL-opts.cc" |
|
47 |
2928
|
48 // Global pointer for user defined function required by dassl. |
2968
|
49 static octave_function *dassl_fcn; |
2928
|
50 |
3991
|
51 // Global pointer for optional user defined jacobian function. |
|
52 static octave_function *dassl_jac; |
|
53 |
4140
|
54 // Have we warned about imaginary values returned from user function? |
|
55 static bool warned_fcn_imaginary = false; |
|
56 static bool warned_jac_imaginary = false; |
|
57 |
3243
|
58 // Is this a recursive call? |
|
59 static int call_depth = 0; |
|
60 |
2928
|
61 ColumnVector |
3849
|
62 dassl_user_function (const ColumnVector& x, const ColumnVector& xdot, |
5275
|
63 double t, octave_idx_type& ires) |
2928
|
64 { |
|
65 ColumnVector retval; |
|
66 |
4628
|
67 assert (x.capacity () == xdot.capacity ()); |
2928
|
68 |
|
69 octave_value_list args; |
4628
|
70 |
2928
|
71 args(2) = t; |
4628
|
72 args(1) = xdot; |
|
73 args(0) = x; |
2928
|
74 |
|
75 if (dassl_fcn) |
|
76 { |
3544
|
77 octave_value_list tmp = dassl_fcn->do_multi_index_op (1, args); |
2928
|
78 |
|
79 if (error_state) |
|
80 { |
|
81 gripe_user_supplied_eval ("dassl"); |
|
82 return retval; |
|
83 } |
|
84 |
3849
|
85 int tlen = tmp.length (); |
|
86 if (tlen > 0 && tmp(0).is_defined ()) |
2928
|
87 { |
4140
|
88 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
|
89 { |
|
90 warning ("dassl: ignoring imaginary part returned from user-supplied function"); |
|
91 warned_fcn_imaginary = true; |
|
92 } |
|
93 |
3419
|
94 retval = ColumnVector (tmp(0).vector_value ()); |
2928
|
95 |
3849
|
96 if (tlen > 1) |
|
97 ires = tmp(1).int_value (); |
|
98 |
2928
|
99 if (error_state || retval.length () == 0) |
|
100 gripe_user_supplied_eval ("dassl"); |
|
101 } |
|
102 else |
|
103 gripe_user_supplied_eval ("dassl"); |
|
104 } |
|
105 |
|
106 return retval; |
|
107 } |
|
108 |
3991
|
109 Matrix |
|
110 dassl_user_jacobian (const ColumnVector& x, const ColumnVector& xdot, |
|
111 double t, double cj) |
|
112 { |
|
113 Matrix retval; |
|
114 |
4628
|
115 assert (x.capacity () == xdot.capacity ()); |
3991
|
116 |
|
117 octave_value_list args; |
|
118 |
|
119 args(3) = cj; |
|
120 args(2) = t; |
4628
|
121 args(1) = xdot; |
|
122 args(0) = x; |
3991
|
123 |
|
124 if (dassl_jac) |
|
125 { |
|
126 octave_value_list tmp = dassl_jac->do_multi_index_op (1, args); |
|
127 |
|
128 if (error_state) |
|
129 { |
|
130 gripe_user_supplied_eval ("dassl"); |
|
131 return retval; |
|
132 } |
|
133 |
|
134 int tlen = tmp.length (); |
|
135 if (tlen > 0 && tmp(0).is_defined ()) |
|
136 { |
4140
|
137 if (! warned_jac_imaginary && tmp(0).is_complex_type ()) |
|
138 { |
|
139 warning ("dassl: ignoring imaginary part returned from user-supplied jacobian function"); |
|
140 warned_jac_imaginary = true; |
|
141 } |
|
142 |
3991
|
143 retval = tmp(0).matrix_value (); |
|
144 |
|
145 if (error_state || retval.length () == 0) |
|
146 gripe_user_supplied_eval ("dassl"); |
|
147 } |
|
148 else |
|
149 gripe_user_supplied_eval ("dassl"); |
|
150 } |
|
151 |
|
152 return retval; |
|
153 } |
|
154 |
3323
|
155 #define DASSL_ABORT() \ |
|
156 do \ |
|
157 { \ |
|
158 unwind_protect::run_frame ("Fdassl"); \ |
|
159 return retval; \ |
|
160 } \ |
|
161 while (0) |
|
162 |
|
163 #define DASSL_ABORT1(msg) \ |
|
164 do \ |
|
165 { \ |
3747
|
166 ::error ("dassl: " msg); \ |
3323
|
167 DASSL_ABORT (); \ |
|
168 } \ |
|
169 while (0) |
|
170 |
|
171 #define DASSL_ABORT2(fmt, arg) \ |
|
172 do \ |
|
173 { \ |
3747
|
174 ::error ("dassl: " fmt, arg); \ |
3323
|
175 DASSL_ABORT (); \ |
|
176 } \ |
|
177 while (0) |
|
178 |
3991
|
179 DEFUN_DLD (dassl, args, nargout, |
3373
|
180 "-*- texinfo -*-\n\ |
4115
|
181 @deftypefn {Loadable Function} {[@var{x}, @var{xdot}, @var{istate}, @var{msg}] =} dassl (@var{fcn}, @var{x_0}, @var{xdot_0}, @var{t}, @var{t_crit})\n\ |
|
182 Solve the set of differential-algebraic equations\n\ |
4912
|
183 @iftex\n\ |
4115
|
184 @tex\n\ |
4852
|
185 $$ 0 = f (x, \\dot{x}, t) $$\n\ |
4115
|
186 with\n\ |
|
187 $$ x(t_0) = x_0, \\dot{x}(t_0) = \\dot{x}_0 $$\n\ |
|
188 @end tex\n\ |
4912
|
189 @end iftex\n\ |
4115
|
190 @ifinfo\n\ |
|
191 \n\ |
|
192 @example\n\ |
4698
|
193 0 = f (x, xdot, t)\n\ |
4115
|
194 @end example\n\ |
|
195 \n\ |
4912
|
196 @noindent\n\ |
4115
|
197 with\n\ |
|
198 \n\ |
|
199 @example\n\ |
|
200 x(t_0) = x_0, xdot(t_0) = xdot_0\n\ |
|
201 @end example\n\ |
|
202 \n\ |
|
203 @end ifinfo\n\ |
|
204 The solution is returned in the matrices @var{x} and @var{xdot},\n\ |
|
205 with each row in the result matrices corresponding to one of the\n\ |
3373
|
206 elements in the vector @var{t}. The first element of @var{t}\n\ |
4115
|
207 should be @math{t_0} and correspond to the initial state of the\n\ |
|
208 system @var{x_0} and its derivative @var{xdot_0}, so that the first\n\ |
|
209 row of the output @var{x} is @var{x_0} and the first row\n\ |
|
210 of the output @var{xdot} is @var{xdot_0}.\n\ |
3373
|
211 \n\ |
5729
|
212 The first argument, @var{fcn}, is a string or a two element cell array\n\ |
|
213 of strings, inline or function handle, that names the function, to call\n\ |
|
214 to compute the vector of residuals for the set of equations. It must\n\ |
|
215 have the form\n\ |
3373
|
216 \n\ |
|
217 @example\n\ |
|
218 @var{res} = f (@var{x}, @var{xdot}, @var{t})\n\ |
|
219 @end example\n\ |
2928
|
220 \n\ |
3373
|
221 @noindent\n\ |
4115
|
222 in which @var{x}, @var{xdot}, and @var{res} are vectors, and @var{t} is a\n\ |
3373
|
223 scalar.\n\ |
|
224 \n\ |
4115
|
225 If @var{fcn} is a two-element string array, the first element names\n\ |
|
226 the function @math{f} described above, and the second element names\n\ |
4117
|
227 a function to compute the modified Jacobian\n\ |
4115
|
228 \n\ |
4912
|
229 @iftex\n\ |
4115
|
230 @tex\n\ |
|
231 $$\n\ |
|
232 J = {\\partial f \\over \\partial x}\n\ |
|
233 + c {\\partial f \\over \\partial \\dot{x}}\n\ |
|
234 $$\n\ |
|
235 @end tex\n\ |
4912
|
236 @end iftex\n\ |
|
237 @ifinfo\n\ |
4888
|
238 @example\n\ |
4115
|
239 df df\n\ |
|
240 jac = -- + c ------\n\ |
|
241 dx d xdot\n\ |
|
242 @end example\n\ |
|
243 @end ifinfo\n\ |
|
244 \n\ |
|
245 The modified Jacobian function must have the form\n\ |
|
246 \n\ |
|
247 @example\n\ |
|
248 \n\ |
|
249 @var{jac} = j (@var{x}, @var{xdot}, @var{t}, @var{c})\n\ |
|
250 \n\ |
|
251 @end example\n\ |
|
252 \n\ |
3373
|
253 The second and third arguments to @code{dassl} specify the initial\n\ |
|
254 condition of the states and their derivatives, and the fourth argument\n\ |
4115
|
255 specifies a vector of output times at which the solution is desired,\n\ |
3373
|
256 including the time corresponding to the initial condition.\n\ |
2928
|
257 \n\ |
3373
|
258 The set of initial states and derivatives are not strictly required to\n\ |
|
259 be consistent. In practice, however, @sc{Dassl} is not very good at\n\ |
|
260 determining a consistent set for you, so it is best if you ensure that\n\ |
|
261 the initial values result in the function evaluating to zero.\n\ |
2928
|
262 \n\ |
3373
|
263 The fifth argument is optional, and may be used to specify a set of\n\ |
|
264 times that the DAE solver should not integrate past. It is useful for\n\ |
|
265 avoiding difficulties with singularities and points where there is a\n\ |
|
266 discontinuity in the derivative.\n\ |
3964
|
267 \n\ |
4115
|
268 After a successful computation, the value of @var{istate} will be\n\ |
|
269 greater than zero (consistent with the Fortran version of @sc{Dassl}).\n\ |
|
270 \n\ |
|
271 If the computation is not successful, the value of @var{istate} will be\n\ |
|
272 less than zero and @var{msg} will contain additional information.\n\ |
|
273 \n\ |
3964
|
274 You can use the function @code{dassl_options} to set optional\n\ |
|
275 parameters for @code{dassl}.\n\ |
5694
|
276 @seealso{daspk, dasrt, lsode}\n\ |
5642
|
277 @end deftypefn") |
2928
|
278 { |
|
279 octave_value_list retval; |
|
280 |
4140
|
281 warned_fcn_imaginary = false; |
|
282 warned_jac_imaginary = false; |
|
283 |
3243
|
284 unwind_protect::begin_frame ("Fdassl"); |
2928
|
285 |
3243
|
286 unwind_protect_int (call_depth); |
|
287 call_depth++; |
2928
|
288 |
3243
|
289 if (call_depth > 1) |
3323
|
290 DASSL_ABORT1 ("invalid recursive call"); |
2928
|
291 |
3243
|
292 int nargin = args.length (); |
|
293 |
3991
|
294 if (nargin > 3 && nargin < 6 && nargout < 5) |
2928
|
295 { |
5729
|
296 std::string fcn_name, fname, jac_name, jname; |
3991
|
297 dassl_fcn = 0; |
|
298 dassl_jac = 0; |
|
299 |
|
300 octave_value f_arg = args(0); |
|
301 |
5729
|
302 if (f_arg.is_cell ()) |
|
303 { |
|
304 Cell c = f_arg.cell_value (); |
|
305 if (c.length() == 1) |
|
306 f_arg = c(0); |
|
307 else if (c.length() == 2) |
|
308 { |
|
309 if (c(0).is_function_handle () || c(0).is_inline_function ()) |
|
310 dassl_fcn = c(0).function_value (); |
|
311 else |
|
312 { |
|
313 fcn_name = unique_symbol_name ("__dassl_fcn__"); |
|
314 fname = "function y = "; |
|
315 fname.append (fcn_name); |
|
316 fname.append (" (x, xdot, t) y = "); |
|
317 dassl_fcn = extract_function |
|
318 (c(0), "dassl", fcn_name, fname, "; endfunction"); |
|
319 } |
|
320 |
|
321 if (dassl_fcn) |
|
322 { |
|
323 if (c(1).is_function_handle () || c(1).is_inline_function ()) |
|
324 dassl_jac = c(1).function_value (); |
|
325 else |
|
326 { |
|
327 jac_name = unique_symbol_name ("__dassl_jac__"); |
|
328 jname = "function jac = "; |
|
329 jname.append(jac_name); |
|
330 jname.append (" (x, xdot, t, cj) jac = "); |
|
331 dassl_jac = extract_function |
|
332 (c(1), "dassl", jac_name, jname, "; endfunction"); |
3991
|
333 |
5729
|
334 if (!dassl_jac) |
|
335 { |
|
336 if (fcn_name.length()) |
|
337 clear_function (fcn_name); |
|
338 dassl_fcn = 0; |
|
339 } |
|
340 } |
|
341 } |
|
342 } |
|
343 else |
|
344 DASSL_ABORT1 ("incorrect number of elements in cell array"); |
|
345 } |
3243
|
346 |
5729
|
347 if (!dassl_fcn && ! f_arg.is_cell()) |
|
348 { |
|
349 if (f_arg.is_function_handle () || f_arg.is_inline_function ()) |
|
350 dassl_fcn = f_arg.function_value (); |
|
351 else |
|
352 { |
|
353 switch (f_arg.rows ()) |
|
354 { |
|
355 case 1: |
|
356 do |
|
357 { |
|
358 fcn_name = unique_symbol_name ("__dassl_fcn__"); |
|
359 fname = "function y = "; |
|
360 fname.append (fcn_name); |
|
361 fname.append (" (x, xdot, t) y = "); |
|
362 dassl_fcn = extract_function |
|
363 (f_arg, "dassl", fcn_name, fname, "; endfunction"); |
|
364 } |
|
365 while (0); |
|
366 break; |
3991
|
367 |
5729
|
368 case 2: |
3991
|
369 { |
5729
|
370 string_vector tmp = f_arg.all_strings (); |
|
371 |
|
372 if (! error_state) |
|
373 { |
|
374 fcn_name = unique_symbol_name ("__dassl_fcn__"); |
|
375 fname = "function y = "; |
|
376 fname.append (fcn_name); |
|
377 fname.append (" (x, xdot, t) y = "); |
|
378 dassl_fcn = extract_function |
|
379 (tmp(0), "dassl", fcn_name, fname, "; endfunction"); |
3991
|
380 |
5729
|
381 if (dassl_fcn) |
|
382 { |
|
383 jac_name = unique_symbol_name ("__dassl_jac__"); |
|
384 jname = "function jac = "; |
|
385 jname.append(jac_name); |
|
386 jname.append (" (x, xdot, t, cj) jac = "); |
|
387 dassl_jac = extract_function |
|
388 (tmp(1), "dassl", jac_name, jname, |
|
389 "; endfunction"); |
|
390 |
|
391 if (!dassl_jac) |
|
392 { |
|
393 if (fcn_name.length()) |
|
394 clear_function (fcn_name); |
|
395 dassl_fcn = 0; |
|
396 } |
|
397 } |
|
398 } |
3991
|
399 } |
5729
|
400 } |
|
401 } |
3991
|
402 } |
|
403 |
|
404 if (error_state || ! dassl_fcn) |
3323
|
405 DASSL_ABORT (); |
3243
|
406 |
3419
|
407 ColumnVector state = ColumnVector (args(1).vector_value ()); |
2928
|
408 |
|
409 if (error_state) |
3323
|
410 DASSL_ABORT1 ("expecting state vector as second argument"); |
3243
|
411 |
3419
|
412 ColumnVector deriv (args(2).vector_value ()); |
3243
|
413 |
|
414 if (error_state) |
3323
|
415 DASSL_ABORT1 ("expecting derivative vector as third argument"); |
3243
|
416 |
3419
|
417 ColumnVector out_times (args(3).vector_value ()); |
3243
|
418 |
|
419 if (error_state) |
3323
|
420 DASSL_ABORT1 ("expecting output time vector as fourth argument"); |
2928
|
421 |
3243
|
422 ColumnVector crit_times; |
|
423 int crit_times_set = 0; |
|
424 if (nargin > 4) |
|
425 { |
3419
|
426 crit_times = ColumnVector (args(4).vector_value ()); |
2928
|
427 |
3243
|
428 if (error_state) |
3323
|
429 DASSL_ABORT1 ("expecting critical time vector as fifth argument"); |
2928
|
430 |
3243
|
431 crit_times_set = 1; |
|
432 } |
2928
|
433 |
3243
|
434 if (state.capacity () != deriv.capacity ()) |
3323
|
435 DASSL_ABORT1 ("x and xdot must have the same size"); |
3243
|
436 |
|
437 double tzero = out_times (0); |
2928
|
438 |
3243
|
439 DAEFunc func (dassl_user_function); |
3991
|
440 if (dassl_jac) |
|
441 func.set_jacobian_function (dassl_user_jacobian); |
|
442 |
3243
|
443 DASSL dae (state, deriv, tzero, func); |
3991
|
444 |
4122
|
445 dae.set_options (dassl_opts); |
2928
|
446 |
3243
|
447 Matrix output; |
|
448 Matrix deriv_output; |
|
449 |
|
450 if (crit_times_set) |
|
451 output = dae.integrate (out_times, deriv_output, crit_times); |
|
452 else |
|
453 output = dae.integrate (out_times, deriv_output); |
2928
|
454 |
5729
|
455 if (fcn_name.length()) |
|
456 clear_function (fcn_name); |
|
457 if (jac_name.length()) |
|
458 clear_function (jac_name); |
|
459 |
3243
|
460 if (! error_state) |
|
461 { |
3997
|
462 std::string msg = dae.error_message (); |
|
463 |
|
464 retval(3) = msg; |
|
465 retval(2) = static_cast<double> (dae.integration_state ()); |
|
466 |
|
467 if (dae.integration_ok ()) |
|
468 { |
|
469 retval(1) = deriv_output; |
|
470 retval(0) = output; |
|
471 } |
|
472 else |
|
473 { |
|
474 retval(1) = Matrix (); |
|
475 retval(0) = Matrix (); |
|
476 |
|
477 if (nargout < 3) |
|
478 error ("dassl: %s", msg.c_str ()); |
|
479 } |
3243
|
480 } |
2928
|
481 } |
3243
|
482 else |
5823
|
483 print_usage (); |
3243
|
484 |
|
485 unwind_protect::run_frame ("Fdassl"); |
2928
|
486 |
|
487 return retval; |
|
488 } |
|
489 |
|
490 /* |
|
491 ;;; Local Variables: *** |
|
492 ;;; mode: C++ *** |
|
493 ;;; End: *** |
|
494 */ |