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