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, |
5275
|
61 double t, octave_idx_type& 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\ |
4912
|
181 @iftex\n\ |
4115
|
182 @tex\n\ |
4852
|
183 $$ 0 = f (x, \\dot{x}, t) $$\n\ |
4115
|
184 with\n\ |
|
185 $$ x(t_0) = x_0, \\dot{x}(t_0) = \\dot{x}_0 $$\n\ |
|
186 @end tex\n\ |
4912
|
187 @end iftex\n\ |
4115
|
188 @ifinfo\n\ |
|
189 \n\ |
|
190 @example\n\ |
4698
|
191 0 = f (x, xdot, t)\n\ |
4115
|
192 @end example\n\ |
|
193 \n\ |
4912
|
194 @noindent\n\ |
4115
|
195 with\n\ |
|
196 \n\ |
|
197 @example\n\ |
|
198 x(t_0) = x_0, xdot(t_0) = xdot_0\n\ |
|
199 @end example\n\ |
|
200 \n\ |
|
201 @end ifinfo\n\ |
|
202 The solution is returned in the matrices @var{x} and @var{xdot},\n\ |
|
203 with each row in the result matrices corresponding to one of the\n\ |
3373
|
204 elements in the vector @var{t}. The first element of @var{t}\n\ |
4115
|
205 should be @math{t_0} and correspond to the initial state of the\n\ |
|
206 system @var{x_0} and its derivative @var{xdot_0}, so that the first\n\ |
|
207 row of the output @var{x} is @var{x_0} and the first row\n\ |
|
208 of the output @var{xdot} is @var{xdot_0}.\n\ |
3373
|
209 \n\ |
|
210 The first argument, @var{fcn}, is a string that names the function to\n\ |
|
211 call to compute the vector of residuals for the set of equations.\n\ |
|
212 It must have the form\n\ |
|
213 \n\ |
|
214 @example\n\ |
|
215 @var{res} = f (@var{x}, @var{xdot}, @var{t})\n\ |
|
216 @end example\n\ |
2928
|
217 \n\ |
3373
|
218 @noindent\n\ |
4115
|
219 in which @var{x}, @var{xdot}, and @var{res} are vectors, and @var{t} is a\n\ |
3373
|
220 scalar.\n\ |
|
221 \n\ |
4115
|
222 If @var{fcn} is a two-element string array, the first element names\n\ |
|
223 the function @math{f} described above, and the second element names\n\ |
4117
|
224 a function to compute the modified Jacobian\n\ |
4115
|
225 \n\ |
4912
|
226 @iftex\n\ |
4115
|
227 @tex\n\ |
|
228 $$\n\ |
|
229 J = {\\partial f \\over \\partial x}\n\ |
|
230 + c {\\partial f \\over \\partial \\dot{x}}\n\ |
|
231 $$\n\ |
|
232 @end tex\n\ |
4912
|
233 @end iftex\n\ |
|
234 @ifinfo\n\ |
4888
|
235 @example\n\ |
4115
|
236 df df\n\ |
|
237 jac = -- + c ------\n\ |
|
238 dx d xdot\n\ |
|
239 @end example\n\ |
|
240 @end ifinfo\n\ |
|
241 \n\ |
|
242 The modified Jacobian function must have the form\n\ |
|
243 \n\ |
|
244 @example\n\ |
|
245 \n\ |
|
246 @var{jac} = j (@var{x}, @var{xdot}, @var{t}, @var{c})\n\ |
|
247 \n\ |
|
248 @end example\n\ |
|
249 \n\ |
3373
|
250 The second and third arguments to @code{dassl} specify the initial\n\ |
|
251 condition of the states and their derivatives, and the fourth argument\n\ |
4115
|
252 specifies a vector of output times at which the solution is desired,\n\ |
3373
|
253 including the time corresponding to the initial condition.\n\ |
2928
|
254 \n\ |
3373
|
255 The set of initial states and derivatives are not strictly required to\n\ |
|
256 be consistent. In practice, however, @sc{Dassl} is not very good at\n\ |
|
257 determining a consistent set for you, so it is best if you ensure that\n\ |
|
258 the initial values result in the function evaluating to zero.\n\ |
2928
|
259 \n\ |
3373
|
260 The fifth argument is optional, and may be used to specify a set of\n\ |
|
261 times that the DAE solver should not integrate past. It is useful for\n\ |
|
262 avoiding difficulties with singularities and points where there is a\n\ |
|
263 discontinuity in the derivative.\n\ |
3964
|
264 \n\ |
4115
|
265 After a successful computation, the value of @var{istate} will be\n\ |
|
266 greater than zero (consistent with the Fortran version of @sc{Dassl}).\n\ |
|
267 \n\ |
|
268 If the computation is not successful, the value of @var{istate} will be\n\ |
|
269 less than zero and @var{msg} will contain additional information.\n\ |
|
270 \n\ |
3964
|
271 You can use the function @code{dassl_options} to set optional\n\ |
|
272 parameters for @code{dassl}.\n\ |
4115
|
273 @end deftypefn\n\ |
|
274 @seealso{daspk, dasrt, lsode, odessa}") |
2928
|
275 { |
|
276 octave_value_list retval; |
|
277 |
4140
|
278 warned_fcn_imaginary = false; |
|
279 warned_jac_imaginary = false; |
|
280 |
3243
|
281 unwind_protect::begin_frame ("Fdassl"); |
2928
|
282 |
3243
|
283 unwind_protect_int (call_depth); |
|
284 call_depth++; |
2928
|
285 |
3243
|
286 if (call_depth > 1) |
3323
|
287 DASSL_ABORT1 ("invalid recursive call"); |
2928
|
288 |
3243
|
289 int nargin = args.length (); |
|
290 |
3991
|
291 if (nargin > 3 && nargin < 6 && nargout < 5) |
2928
|
292 { |
3991
|
293 dassl_fcn = 0; |
|
294 dassl_jac = 0; |
|
295 |
|
296 octave_value f_arg = args(0); |
|
297 |
|
298 switch (f_arg.rows ()) |
|
299 { |
|
300 case 1: |
|
301 dassl_fcn = extract_function |
|
302 (f_arg, "dassl", "__dassl_fcn__", |
|
303 "function res = __dassl_fcn__ (x, xdot, t) res = ", |
|
304 "; endfunction"); |
|
305 break; |
|
306 |
|
307 case 2: |
|
308 { |
|
309 string_vector tmp = f_arg.all_strings (); |
3243
|
310 |
3991
|
311 if (! error_state) |
|
312 { |
|
313 dassl_fcn = extract_function |
|
314 (tmp(0), "dassl", "__dassl_fcn__", |
|
315 "function res = __dassl_fcn__ (x, xdot, t) res = ", |
|
316 "; endfunction"); |
|
317 |
|
318 if (dassl_fcn) |
|
319 { |
|
320 dassl_jac = extract_function |
|
321 (tmp(1), "dassl", "__dassl_jac__", |
|
322 "function jac = __dassl_jac__ (x, xdot, t, cj) jac = ", |
|
323 "; endfunction"); |
|
324 |
|
325 if (! dassl_jac) |
|
326 dassl_fcn = 0; |
|
327 } |
|
328 } |
|
329 } |
|
330 } |
|
331 |
|
332 if (error_state || ! dassl_fcn) |
3323
|
333 DASSL_ABORT (); |
3243
|
334 |
3419
|
335 ColumnVector state = ColumnVector (args(1).vector_value ()); |
2928
|
336 |
|
337 if (error_state) |
3323
|
338 DASSL_ABORT1 ("expecting state vector as second argument"); |
3243
|
339 |
3419
|
340 ColumnVector deriv (args(2).vector_value ()); |
3243
|
341 |
|
342 if (error_state) |
3323
|
343 DASSL_ABORT1 ("expecting derivative vector as third argument"); |
3243
|
344 |
3419
|
345 ColumnVector out_times (args(3).vector_value ()); |
3243
|
346 |
|
347 if (error_state) |
3323
|
348 DASSL_ABORT1 ("expecting output time vector as fourth argument"); |
2928
|
349 |
3243
|
350 ColumnVector crit_times; |
|
351 int crit_times_set = 0; |
|
352 if (nargin > 4) |
|
353 { |
3419
|
354 crit_times = ColumnVector (args(4).vector_value ()); |
2928
|
355 |
3243
|
356 if (error_state) |
3323
|
357 DASSL_ABORT1 ("expecting critical time vector as fifth argument"); |
2928
|
358 |
3243
|
359 crit_times_set = 1; |
|
360 } |
2928
|
361 |
3243
|
362 if (state.capacity () != deriv.capacity ()) |
3323
|
363 DASSL_ABORT1 ("x and xdot must have the same size"); |
3243
|
364 |
|
365 double tzero = out_times (0); |
2928
|
366 |
3243
|
367 DAEFunc func (dassl_user_function); |
3991
|
368 if (dassl_jac) |
|
369 func.set_jacobian_function (dassl_user_jacobian); |
|
370 |
3243
|
371 DASSL dae (state, deriv, tzero, func); |
3991
|
372 |
4122
|
373 dae.set_options (dassl_opts); |
2928
|
374 |
3243
|
375 Matrix output; |
|
376 Matrix deriv_output; |
|
377 |
|
378 if (crit_times_set) |
|
379 output = dae.integrate (out_times, deriv_output, crit_times); |
|
380 else |
|
381 output = dae.integrate (out_times, deriv_output); |
2928
|
382 |
3243
|
383 if (! error_state) |
|
384 { |
3997
|
385 std::string msg = dae.error_message (); |
|
386 |
|
387 retval(3) = msg; |
|
388 retval(2) = static_cast<double> (dae.integration_state ()); |
|
389 |
|
390 if (dae.integration_ok ()) |
|
391 { |
|
392 retval(1) = deriv_output; |
|
393 retval(0) = output; |
|
394 } |
|
395 else |
|
396 { |
|
397 retval(1) = Matrix (); |
|
398 retval(0) = Matrix (); |
|
399 |
|
400 if (nargout < 3) |
|
401 error ("dassl: %s", msg.c_str ()); |
|
402 } |
3243
|
403 } |
2928
|
404 } |
3243
|
405 else |
|
406 print_usage ("dassl"); |
|
407 |
|
408 unwind_protect::run_frame ("Fdassl"); |
2928
|
409 |
|
410 return retval; |
|
411 } |
|
412 |
|
413 /* |
|
414 ;;; Local Variables: *** |
|
415 ;;; mode: C++ *** |
|
416 ;;; End: *** |
|
417 */ |