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