3912
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997, 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 |
|
27 #include <string> |
|
28 |
|
29 #include <iomanip> |
|
30 #include <iostream> |
|
31 |
|
32 #include "DASPK.h" |
|
33 |
|
34 #include "defun-dld.h" |
|
35 #include "error.h" |
|
36 #include "gripes.h" |
|
37 #include "oct-obj.h" |
|
38 #include "ov-fcn.h" |
|
39 #include "pager.h" |
|
40 #include "unwind-prot.h" |
|
41 #include "utils.h" |
|
42 #include "variables.h" |
|
43 |
3998
|
44 #include "DASPK-opts.cc" |
|
45 |
3912
|
46 // Global pointer for user defined function required by daspk. |
|
47 static octave_function *daspk_fcn; |
|
48 |
4140
|
49 // Global pointer for optional user defined jacobian function. |
|
50 static octave_function *daspk_jac; |
|
51 |
|
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 |
3912
|
56 // Is this a recursive call? |
|
57 static int call_depth = 0; |
|
58 |
|
59 ColumnVector |
|
60 daspk_user_function (const ColumnVector& x, const ColumnVector& xdot, |
|
61 double t, int& ires) |
|
62 { |
|
63 ColumnVector retval; |
|
64 |
4628
|
65 assert (x.capacity () == xdot.capacity ()); |
3912
|
66 |
|
67 octave_value_list args; |
4628
|
68 |
3912
|
69 args(2) = t; |
4628
|
70 args(1) = xdot; |
|
71 args(0) = x; |
3912
|
72 |
|
73 if (daspk_fcn) |
|
74 { |
|
75 octave_value_list tmp = daspk_fcn->do_multi_index_op (1, args); |
|
76 |
|
77 if (error_state) |
|
78 { |
|
79 gripe_user_supplied_eval ("daspk"); |
|
80 return retval; |
|
81 } |
|
82 |
|
83 int tlen = tmp.length (); |
|
84 if (tlen > 0 && tmp(0).is_defined ()) |
|
85 { |
4140
|
86 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
|
87 { |
|
88 warning ("daspk: ignoring imaginary part returned from user-supplied function"); |
|
89 warned_fcn_imaginary = true; |
|
90 } |
|
91 |
3912
|
92 retval = ColumnVector (tmp(0).vector_value ()); |
|
93 |
|
94 if (tlen > 1) |
|
95 ires = tmp(1).int_value (); |
|
96 |
|
97 if (error_state || retval.length () == 0) |
|
98 gripe_user_supplied_eval ("daspk"); |
|
99 } |
|
100 else |
|
101 gripe_user_supplied_eval ("daspk"); |
|
102 } |
|
103 |
|
104 return retval; |
|
105 } |
|
106 |
4140
|
107 Matrix |
|
108 daspk_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 ()); |
4140
|
114 |
|
115 octave_value_list args; |
|
116 |
|
117 args(3) = cj; |
|
118 args(2) = t; |
4628
|
119 args(1) = xdot; |
|
120 args(0) = x; |
4140
|
121 |
|
122 if (daspk_jac) |
|
123 { |
|
124 octave_value_list tmp = daspk_jac->do_multi_index_op (1, args); |
|
125 |
|
126 if (error_state) |
|
127 { |
|
128 gripe_user_supplied_eval ("daspk"); |
|
129 return retval; |
|
130 } |
|
131 |
|
132 int tlen = tmp.length (); |
|
133 if (tlen > 0 && tmp(0).is_defined ()) |
|
134 { |
|
135 if (! warned_jac_imaginary && tmp(0).is_complex_type ()) |
|
136 { |
|
137 warning ("daspk: ignoring imaginary part returned from user-supplied jacobian function"); |
|
138 warned_jac_imaginary = true; |
|
139 } |
|
140 |
|
141 retval = tmp(0).matrix_value (); |
|
142 |
|
143 if (error_state || retval.length () == 0) |
|
144 gripe_user_supplied_eval ("daspk"); |
|
145 } |
|
146 else |
|
147 gripe_user_supplied_eval ("daspk"); |
|
148 } |
|
149 |
|
150 return retval; |
|
151 } |
|
152 |
3912
|
153 #define DASPK_ABORT() \ |
|
154 do \ |
|
155 { \ |
|
156 unwind_protect::run_frame ("Fdaspk"); \ |
|
157 return retval; \ |
|
158 } \ |
|
159 while (0) |
|
160 |
|
161 #define DASPK_ABORT1(msg) \ |
|
162 do \ |
|
163 { \ |
|
164 ::error ("daspk: " msg); \ |
|
165 DASPK_ABORT (); \ |
|
166 } \ |
|
167 while (0) |
|
168 |
|
169 #define DASPK_ABORT2(fmt, arg) \ |
|
170 do \ |
|
171 { \ |
|
172 ::error ("daspk: " fmt, arg); \ |
|
173 DASPK_ABORT (); \ |
|
174 } \ |
|
175 while (0) |
|
176 |
3997
|
177 DEFUN_DLD (daspk, args, nargout, |
3912
|
178 "-*- texinfo -*-\n\ |
4115
|
179 @deftypefn {Loadable Function} {[@var{x}, @var{xdot}, @var{istate}, @var{msg}] =} daspk (@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\ |
4852
|
182 $$ 0 = f (x, \\dot{x}, t) $$\n\ |
4115
|
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\ |
4698
|
189 0 = f (x, xdot, t)\n\ |
4115
|
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\ |
3912
|
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\ |
3912
|
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\ |
|
214 \n\ |
|
215 @noindent\n\ |
4115
|
216 in which @var{x}, @var{xdot}, and @var{res} are vectors, and @var{t} is a\n\ |
3912
|
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 @tex\n\ |
|
223 $$\n\ |
|
224 J = {\\partial f \\over \\partial x}\n\ |
|
225 + c {\\partial f \\over \\partial \\dot{x}}\n\ |
|
226 $$\n\ |
|
227 @end tex\n\ |
|
228 @ifinfo\n\ |
4144
|
229 \n\ |
|
230 @example\n\ |
4115
|
231 df df\n\ |
|
232 jac = -- + c ------\n\ |
|
233 dx d xdot\n\ |
|
234 @end example\n\ |
|
235 @end ifinfo\n\ |
|
236 \n\ |
|
237 The modified Jacobian function must have the form\n\ |
|
238 \n\ |
|
239 @example\n\ |
|
240 \n\ |
|
241 @var{jac} = j (@var{x}, @var{xdot}, @var{t}, @var{c})\n\ |
|
242 \n\ |
|
243 @end example\n\ |
|
244 \n\ |
3912
|
245 The second and third arguments to @code{daspk} specify the initial\n\ |
|
246 condition of the states and their derivatives, and the fourth argument\n\ |
4115
|
247 specifies a vector of output times at which the solution is desired,\n\ |
3912
|
248 including the time corresponding to the initial condition.\n\ |
|
249 \n\ |
|
250 The set of initial states and derivatives are not strictly required to\n\ |
4115
|
251 be consistent. If they are not consistent, you must use the\n\ |
|
252 @code{daspk_options} function to provide additional information so\n\ |
|
253 that @code{daspk} can compute a consistent starting point.\n\ |
3912
|
254 \n\ |
|
255 The fifth argument is optional, and may be used to specify a set of\n\ |
|
256 times that the DAE solver should not integrate past. It is useful for\n\ |
|
257 avoiding difficulties with singularities and points where there is a\n\ |
|
258 discontinuity in the derivative.\n\ |
3964
|
259 \n\ |
4115
|
260 After a successful computation, the value of @var{istate} will be\n\ |
|
261 greater than zero (consistent with the Fortran version of @sc{Daspk}).\n\ |
|
262 \n\ |
|
263 If the computation is not successful, the value of @var{istate} will be\n\ |
|
264 less than zero and @var{msg} will contain additional information.\n\ |
|
265 \n\ |
3964
|
266 You can use the function @code{daspk_options} to set optional\n\ |
|
267 parameters for @code{daspk}.\n\ |
4115
|
268 @end deftypefn\n\ |
|
269 @seealso{dassl}") |
3912
|
270 { |
|
271 octave_value_list retval; |
|
272 |
4140
|
273 warned_fcn_imaginary = false; |
|
274 warned_jac_imaginary = false; |
|
275 |
3912
|
276 unwind_protect::begin_frame ("Fdaspk"); |
|
277 |
|
278 unwind_protect_int (call_depth); |
|
279 call_depth++; |
|
280 |
|
281 if (call_depth > 1) |
|
282 DASPK_ABORT1 ("invalid recursive call"); |
|
283 |
|
284 int nargin = args.length (); |
|
285 |
|
286 if (nargin > 3 && nargin < 6) |
|
287 { |
4140
|
288 daspk_fcn = 0; |
|
289 daspk_jac = 0; |
|
290 |
|
291 octave_value f_arg = args(0); |
|
292 |
|
293 switch (f_arg.rows ()) |
|
294 { |
|
295 case 1: |
|
296 daspk_fcn = extract_function |
|
297 (args(0), "daspk", "__daspk_fcn__", |
|
298 "function res = __daspk_fcn__ (x, xdot, t) res = ", |
|
299 "; endfunction"); |
|
300 break; |
|
301 |
|
302 case 2: |
|
303 { |
|
304 string_vector tmp = f_arg.all_strings (); |
3912
|
305 |
4140
|
306 if (! error_state) |
|
307 { |
|
308 daspk_fcn = extract_function |
|
309 (tmp(0), "daspk", "__daspk_fcn__", |
|
310 "function res = __daspk_fcn__ (x, xdot, t) res = ", |
|
311 "; endfunction"); |
|
312 |
|
313 if (daspk_fcn) |
|
314 { |
|
315 daspk_jac = extract_function |
|
316 (tmp(1), "daspk", "__daspk_jac__", |
|
317 "function jac = __daspk_jac__ (x, xdot, t, cj) jac = ", |
|
318 "; endfunction"); |
|
319 |
|
320 if (! daspk_jac) |
|
321 daspk_fcn = 0; |
|
322 } |
|
323 } |
|
324 } |
|
325 } |
|
326 |
|
327 if (error_state || ! daspk_fcn) |
3912
|
328 DASPK_ABORT (); |
|
329 |
|
330 ColumnVector state = ColumnVector (args(1).vector_value ()); |
|
331 |
|
332 if (error_state) |
|
333 DASPK_ABORT1 ("expecting state vector as second argument"); |
|
334 |
|
335 ColumnVector deriv (args(2).vector_value ()); |
|
336 |
|
337 if (error_state) |
|
338 DASPK_ABORT1 ("expecting derivative vector as third argument"); |
|
339 |
|
340 ColumnVector out_times (args(3).vector_value ()); |
|
341 |
|
342 if (error_state) |
|
343 DASPK_ABORT1 ("expecting output time vector as fourth argument"); |
|
344 |
|
345 ColumnVector crit_times; |
|
346 int crit_times_set = 0; |
|
347 if (nargin > 4) |
|
348 { |
|
349 crit_times = ColumnVector (args(4).vector_value ()); |
|
350 |
|
351 if (error_state) |
|
352 DASPK_ABORT1 ("expecting critical time vector as fifth argument"); |
|
353 |
|
354 crit_times_set = 1; |
|
355 } |
|
356 |
|
357 if (state.capacity () != deriv.capacity ()) |
|
358 DASPK_ABORT1 ("x and xdot must have the same size"); |
|
359 |
|
360 double tzero = out_times (0); |
|
361 |
|
362 DAEFunc func (daspk_user_function); |
4140
|
363 if (daspk_jac) |
|
364 func.set_jacobian_function (daspk_user_jacobian); |
|
365 |
3912
|
366 DASPK dae (state, deriv, tzero, func); |
4122
|
367 dae.set_options (daspk_opts); |
3912
|
368 |
|
369 Matrix output; |
|
370 Matrix deriv_output; |
|
371 |
|
372 if (crit_times_set) |
|
373 output = dae.integrate (out_times, deriv_output, crit_times); |
|
374 else |
|
375 output = dae.integrate (out_times, deriv_output); |
|
376 |
|
377 if (! error_state) |
|
378 { |
3997
|
379 std::string msg = dae.error_message (); |
|
380 |
|
381 retval(3) = msg; |
|
382 retval(2) = static_cast<double> (dae.integration_state ()); |
3912
|
383 |
3997
|
384 if (dae.integration_ok ()) |
|
385 { |
|
386 retval(1) = deriv_output; |
|
387 retval(0) = output; |
|
388 } |
|
389 else |
|
390 { |
|
391 retval(1) = Matrix (); |
|
392 retval(0) = Matrix (); |
|
393 |
|
394 if (nargout < 3) |
|
395 error ("daspk: %s", msg.c_str ()); |
|
396 } |
3912
|
397 } |
|
398 } |
|
399 else |
|
400 print_usage ("daspk"); |
|
401 |
|
402 unwind_protect::run_frame ("Fdaspk"); |
|
403 |
|
404 return retval; |
|
405 } |
|
406 |
|
407 /* |
|
408 ;;; Local Variables: *** |
|
409 ;;; mode: C++ *** |
|
410 ;;; End: *** |
|
411 */ |