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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
3912
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <string> |
|
29 |
|
30 #include <iomanip> |
|
31 #include <iostream> |
|
32 |
|
33 #include "DASPK.h" |
|
34 |
|
35 #include "defun-dld.h" |
|
36 #include "error.h" |
|
37 #include "gripes.h" |
|
38 #include "oct-obj.h" |
|
39 #include "ov-fcn.h" |
|
40 #include "pager.h" |
|
41 #include "unwind-prot.h" |
|
42 #include "utils.h" |
|
43 #include "variables.h" |
|
44 |
3998
|
45 #include "DASPK-opts.cc" |
|
46 |
3912
|
47 // Global pointer for user defined function required by daspk. |
|
48 static octave_function *daspk_fcn; |
|
49 |
4140
|
50 // Global pointer for optional user defined jacobian function. |
|
51 static octave_function *daspk_jac; |
|
52 |
|
53 // Have we warned about imaginary values returned from user function? |
|
54 static bool warned_fcn_imaginary = false; |
|
55 static bool warned_jac_imaginary = false; |
|
56 |
3912
|
57 // Is this a recursive call? |
|
58 static int call_depth = 0; |
|
59 |
|
60 ColumnVector |
|
61 daspk_user_function (const ColumnVector& x, const ColumnVector& xdot, |
5275
|
62 double t, octave_idx_type& ires) |
3912
|
63 { |
|
64 ColumnVector retval; |
|
65 |
4628
|
66 assert (x.capacity () == xdot.capacity ()); |
3912
|
67 |
|
68 octave_value_list args; |
4628
|
69 |
3912
|
70 args(2) = t; |
4628
|
71 args(1) = xdot; |
|
72 args(0) = x; |
3912
|
73 |
|
74 if (daspk_fcn) |
|
75 { |
|
76 octave_value_list tmp = daspk_fcn->do_multi_index_op (1, args); |
|
77 |
|
78 if (error_state) |
|
79 { |
|
80 gripe_user_supplied_eval ("daspk"); |
|
81 return retval; |
|
82 } |
|
83 |
|
84 int tlen = tmp.length (); |
|
85 if (tlen > 0 && tmp(0).is_defined ()) |
|
86 { |
4140
|
87 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
|
88 { |
|
89 warning ("daspk: ignoring imaginary part returned from user-supplied function"); |
|
90 warned_fcn_imaginary = true; |
|
91 } |
|
92 |
3912
|
93 retval = ColumnVector (tmp(0).vector_value ()); |
|
94 |
|
95 if (tlen > 1) |
|
96 ires = tmp(1).int_value (); |
|
97 |
|
98 if (error_state || retval.length () == 0) |
|
99 gripe_user_supplied_eval ("daspk"); |
|
100 } |
|
101 else |
|
102 gripe_user_supplied_eval ("daspk"); |
|
103 } |
|
104 |
|
105 return retval; |
|
106 } |
|
107 |
4140
|
108 Matrix |
|
109 daspk_user_jacobian (const ColumnVector& x, const ColumnVector& xdot, |
|
110 double t, double cj) |
|
111 { |
|
112 Matrix retval; |
|
113 |
4628
|
114 assert (x.capacity () == xdot.capacity ()); |
4140
|
115 |
|
116 octave_value_list args; |
|
117 |
|
118 args(3) = cj; |
|
119 args(2) = t; |
4628
|
120 args(1) = xdot; |
|
121 args(0) = x; |
4140
|
122 |
|
123 if (daspk_jac) |
|
124 { |
|
125 octave_value_list tmp = daspk_jac->do_multi_index_op (1, args); |
|
126 |
|
127 if (error_state) |
|
128 { |
|
129 gripe_user_supplied_eval ("daspk"); |
|
130 return retval; |
|
131 } |
|
132 |
|
133 int tlen = tmp.length (); |
|
134 if (tlen > 0 && tmp(0).is_defined ()) |
|
135 { |
|
136 if (! warned_jac_imaginary && tmp(0).is_complex_type ()) |
|
137 { |
|
138 warning ("daspk: ignoring imaginary part returned from user-supplied jacobian function"); |
|
139 warned_jac_imaginary = true; |
|
140 } |
|
141 |
|
142 retval = tmp(0).matrix_value (); |
|
143 |
|
144 if (error_state || retval.length () == 0) |
|
145 gripe_user_supplied_eval ("daspk"); |
|
146 } |
|
147 else |
|
148 gripe_user_supplied_eval ("daspk"); |
|
149 } |
|
150 |
|
151 return retval; |
|
152 } |
|
153 |
3912
|
154 #define DASPK_ABORT() \ |
|
155 do \ |
|
156 { \ |
|
157 unwind_protect::run_frame ("Fdaspk"); \ |
|
158 return retval; \ |
|
159 } \ |
|
160 while (0) |
|
161 |
|
162 #define DASPK_ABORT1(msg) \ |
|
163 do \ |
|
164 { \ |
|
165 ::error ("daspk: " msg); \ |
|
166 DASPK_ABORT (); \ |
|
167 } \ |
|
168 while (0) |
|
169 |
|
170 #define DASPK_ABORT2(fmt, arg) \ |
|
171 do \ |
|
172 { \ |
|
173 ::error ("daspk: " fmt, arg); \ |
|
174 DASPK_ABORT (); \ |
|
175 } \ |
|
176 while (0) |
|
177 |
3997
|
178 DEFUN_DLD (daspk, args, nargout, |
3912
|
179 "-*- texinfo -*-\n\ |
4115
|
180 @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\ |
|
181 Solve the set of differential-algebraic equations\n\ |
|
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\ |
|
187 @ifinfo\n\ |
|
188 \n\ |
|
189 @example\n\ |
4698
|
190 0 = f (x, xdot, t)\n\ |
4115
|
191 @end example\n\ |
|
192 \n\ |
|
193 with\n\ |
|
194 \n\ |
|
195 @example\n\ |
|
196 x(t_0) = x_0, xdot(t_0) = xdot_0\n\ |
|
197 @end example\n\ |
|
198 \n\ |
|
199 @end ifinfo\n\ |
|
200 The solution is returned in the matrices @var{x} and @var{xdot},\n\ |
|
201 with each row in the result matrices corresponding to one of the\n\ |
3912
|
202 elements in the vector @var{t}. The first element of @var{t}\n\ |
4115
|
203 should be @math{t_0} and correspond to the initial state of the\n\ |
|
204 system @var{x_0} and its derivative @var{xdot_0}, so that the first\n\ |
|
205 row of the output @var{x} is @var{x_0} and the first row\n\ |
|
206 of the output @var{xdot} is @var{xdot_0}.\n\ |
3912
|
207 \n\ |
|
208 The first argument, @var{fcn}, is a string that names the function to\n\ |
|
209 call to compute the vector of residuals for the set of equations.\n\ |
|
210 It must have the form\n\ |
|
211 \n\ |
|
212 @example\n\ |
|
213 @var{res} = f (@var{x}, @var{xdot}, @var{t})\n\ |
|
214 @end example\n\ |
|
215 \n\ |
|
216 @noindent\n\ |
4115
|
217 in which @var{x}, @var{xdot}, and @var{res} are vectors, and @var{t} is a\n\ |
3912
|
218 scalar.\n\ |
|
219 \n\ |
4115
|
220 If @var{fcn} is a two-element string array, the first element names\n\ |
|
221 the function @math{f} described above, and the second element names\n\ |
4117
|
222 a function to compute the modified Jacobian\n\ |
4115
|
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\ |
4144
|
230 \n\ |
|
231 @example\n\ |
4115
|
232 df df\n\ |
|
233 jac = -- + c ------\n\ |
|
234 dx d xdot\n\ |
|
235 @end example\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\ |
3912
|
246 The second and third arguments to @code{daspk} 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\ |
3912
|
249 including the time corresponding to the initial condition.\n\ |
|
250 \n\ |
|
251 The set of initial states and derivatives are not strictly required to\n\ |
4115
|
252 be consistent. If they are not consistent, you must use the\n\ |
|
253 @code{daspk_options} function to provide additional information so\n\ |
|
254 that @code{daspk} can compute a consistent starting point.\n\ |
3912
|
255 \n\ |
|
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{Daspk}).\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{daspk_options} to set optional\n\ |
|
268 parameters for @code{daspk}.\n\ |
4115
|
269 @end deftypefn\n\ |
|
270 @seealso{dassl}") |
3912
|
271 { |
|
272 octave_value_list retval; |
|
273 |
4140
|
274 warned_fcn_imaginary = false; |
|
275 warned_jac_imaginary = false; |
|
276 |
3912
|
277 unwind_protect::begin_frame ("Fdaspk"); |
|
278 |
|
279 unwind_protect_int (call_depth); |
|
280 call_depth++; |
|
281 |
|
282 if (call_depth > 1) |
|
283 DASPK_ABORT1 ("invalid recursive call"); |
|
284 |
|
285 int nargin = args.length (); |
|
286 |
|
287 if (nargin > 3 && nargin < 6) |
|
288 { |
4140
|
289 daspk_fcn = 0; |
|
290 daspk_jac = 0; |
|
291 |
|
292 octave_value f_arg = args(0); |
|
293 |
|
294 switch (f_arg.rows ()) |
|
295 { |
|
296 case 1: |
|
297 daspk_fcn = extract_function |
|
298 (args(0), "daspk", "__daspk_fcn__", |
|
299 "function res = __daspk_fcn__ (x, xdot, t) res = ", |
|
300 "; endfunction"); |
|
301 break; |
|
302 |
|
303 case 2: |
|
304 { |
|
305 string_vector tmp = f_arg.all_strings (); |
3912
|
306 |
4140
|
307 if (! error_state) |
|
308 { |
|
309 daspk_fcn = extract_function |
|
310 (tmp(0), "daspk", "__daspk_fcn__", |
|
311 "function res = __daspk_fcn__ (x, xdot, t) res = ", |
|
312 "; endfunction"); |
|
313 |
|
314 if (daspk_fcn) |
|
315 { |
|
316 daspk_jac = extract_function |
|
317 (tmp(1), "daspk", "__daspk_jac__", |
|
318 "function jac = __daspk_jac__ (x, xdot, t, cj) jac = ", |
|
319 "; endfunction"); |
|
320 |
|
321 if (! daspk_jac) |
|
322 daspk_fcn = 0; |
|
323 } |
|
324 } |
|
325 } |
|
326 } |
|
327 |
|
328 if (error_state || ! daspk_fcn) |
3912
|
329 DASPK_ABORT (); |
|
330 |
|
331 ColumnVector state = ColumnVector (args(1).vector_value ()); |
|
332 |
|
333 if (error_state) |
|
334 DASPK_ABORT1 ("expecting state vector as second argument"); |
|
335 |
|
336 ColumnVector deriv (args(2).vector_value ()); |
|
337 |
|
338 if (error_state) |
|
339 DASPK_ABORT1 ("expecting derivative vector as third argument"); |
|
340 |
|
341 ColumnVector out_times (args(3).vector_value ()); |
|
342 |
|
343 if (error_state) |
|
344 DASPK_ABORT1 ("expecting output time vector as fourth argument"); |
|
345 |
|
346 ColumnVector crit_times; |
|
347 int crit_times_set = 0; |
|
348 if (nargin > 4) |
|
349 { |
|
350 crit_times = ColumnVector (args(4).vector_value ()); |
|
351 |
|
352 if (error_state) |
|
353 DASPK_ABORT1 ("expecting critical time vector as fifth argument"); |
|
354 |
|
355 crit_times_set = 1; |
|
356 } |
|
357 |
|
358 if (state.capacity () != deriv.capacity ()) |
|
359 DASPK_ABORT1 ("x and xdot must have the same size"); |
|
360 |
|
361 double tzero = out_times (0); |
|
362 |
|
363 DAEFunc func (daspk_user_function); |
4140
|
364 if (daspk_jac) |
|
365 func.set_jacobian_function (daspk_user_jacobian); |
|
366 |
3912
|
367 DASPK dae (state, deriv, tzero, func); |
4122
|
368 dae.set_options (daspk_opts); |
3912
|
369 |
|
370 Matrix output; |
|
371 Matrix deriv_output; |
|
372 |
|
373 if (crit_times_set) |
|
374 output = dae.integrate (out_times, deriv_output, crit_times); |
|
375 else |
|
376 output = dae.integrate (out_times, deriv_output); |
|
377 |
|
378 if (! error_state) |
|
379 { |
3997
|
380 std::string msg = dae.error_message (); |
|
381 |
|
382 retval(3) = msg; |
|
383 retval(2) = static_cast<double> (dae.integration_state ()); |
3912
|
384 |
3997
|
385 if (dae.integration_ok ()) |
|
386 { |
|
387 retval(1) = deriv_output; |
|
388 retval(0) = output; |
|
389 } |
|
390 else |
|
391 { |
|
392 retval(1) = Matrix (); |
|
393 retval(0) = Matrix (); |
|
394 |
|
395 if (nargout < 3) |
|
396 error ("daspk: %s", msg.c_str ()); |
|
397 } |
3912
|
398 } |
|
399 } |
|
400 else |
|
401 print_usage ("daspk"); |
|
402 |
|
403 unwind_protect::run_frame ("Fdaspk"); |
|
404 |
|
405 return retval; |
|
406 } |
|
407 |
|
408 /* |
|
409 ;;; Local Variables: *** |
|
410 ;;; mode: C++ *** |
|
411 ;;; End: *** |
|
412 */ |