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 |
|
49 // Is this a recursive call? |
|
50 static int call_depth = 0; |
|
51 |
|
52 ColumnVector |
|
53 daspk_user_function (const ColumnVector& x, const ColumnVector& xdot, |
|
54 double t, int& ires) |
|
55 { |
|
56 ColumnVector retval; |
|
57 |
|
58 int nstates = x.capacity (); |
|
59 |
|
60 assert (nstates == xdot.capacity ()); |
|
61 |
|
62 octave_value_list args; |
|
63 args(2) = t; |
|
64 |
|
65 if (nstates > 1) |
|
66 { |
|
67 Matrix m1 (nstates, 1); |
|
68 Matrix m2 (nstates, 1); |
|
69 for (int i = 0; i < nstates; i++) |
|
70 { |
|
71 m1 (i, 0) = x (i); |
|
72 m2 (i, 0) = xdot (i); |
|
73 } |
|
74 octave_value state (m1); |
|
75 octave_value deriv (m2); |
|
76 args(1) = deriv; |
|
77 args(0) = state; |
|
78 } |
|
79 else |
|
80 { |
|
81 double d1 = x (0); |
|
82 double d2 = xdot (0); |
|
83 octave_value state (d1); |
|
84 octave_value deriv (d2); |
|
85 args(1) = deriv; |
|
86 args(0) = state; |
|
87 } |
|
88 |
|
89 if (daspk_fcn) |
|
90 { |
|
91 octave_value_list tmp = daspk_fcn->do_multi_index_op (1, args); |
|
92 |
|
93 if (error_state) |
|
94 { |
|
95 gripe_user_supplied_eval ("daspk"); |
|
96 return retval; |
|
97 } |
|
98 |
|
99 int tlen = tmp.length (); |
|
100 if (tlen > 0 && tmp(0).is_defined ()) |
|
101 { |
|
102 retval = ColumnVector (tmp(0).vector_value ()); |
|
103 |
|
104 if (tlen > 1) |
|
105 ires = tmp(1).int_value (); |
|
106 |
|
107 if (error_state || retval.length () == 0) |
|
108 gripe_user_supplied_eval ("daspk"); |
|
109 } |
|
110 else |
|
111 gripe_user_supplied_eval ("daspk"); |
|
112 } |
|
113 |
|
114 return retval; |
|
115 } |
|
116 |
|
117 #define DASPK_ABORT() \ |
|
118 do \ |
|
119 { \ |
|
120 unwind_protect::run_frame ("Fdaspk"); \ |
|
121 return retval; \ |
|
122 } \ |
|
123 while (0) |
|
124 |
|
125 #define DASPK_ABORT1(msg) \ |
|
126 do \ |
|
127 { \ |
|
128 ::error ("daspk: " msg); \ |
|
129 DASPK_ABORT (); \ |
|
130 } \ |
|
131 while (0) |
|
132 |
|
133 #define DASPK_ABORT2(fmt, arg) \ |
|
134 do \ |
|
135 { \ |
|
136 ::error ("daspk: " fmt, arg); \ |
|
137 DASPK_ABORT (); \ |
|
138 } \ |
|
139 while (0) |
|
140 |
3997
|
141 DEFUN_DLD (daspk, args, nargout, |
3912
|
142 "-*- texinfo -*-\n\ |
4115
|
143 @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\ |
|
144 Solve the set of differential-algebraic equations\n\ |
|
145 @tex\n\ |
|
146 $$ 0 = f (\\dot{x}, x, t) $$\n\ |
|
147 with\n\ |
|
148 $$ x(t_0) = x_0, \\dot{x}(t_0) = \\dot{x}_0 $$\n\ |
|
149 @end tex\n\ |
|
150 @ifinfo\n\ |
|
151 \n\ |
|
152 @example\n\ |
|
153 0 = f (xdot, x, t)\n\ |
|
154 @end example\n\ |
|
155 \n\ |
|
156 with\n\ |
|
157 \n\ |
|
158 @example\n\ |
|
159 x(t_0) = x_0, xdot(t_0) = xdot_0\n\ |
|
160 @end example\n\ |
|
161 \n\ |
|
162 @end ifinfo\n\ |
|
163 The solution is returned in the matrices @var{x} and @var{xdot},\n\ |
|
164 with each row in the result matrices corresponding to one of the\n\ |
3912
|
165 elements in the vector @var{t}. The first element of @var{t}\n\ |
4115
|
166 should be @math{t_0} and correspond to the initial state of the\n\ |
|
167 system @var{x_0} and its derivative @var{xdot_0}, so that the first\n\ |
|
168 row of the output @var{x} is @var{x_0} and the first row\n\ |
|
169 of the output @var{xdot} is @var{xdot_0}.\n\ |
3912
|
170 \n\ |
|
171 The first argument, @var{fcn}, is a string that names the function to\n\ |
|
172 call to compute the vector of residuals for the set of equations.\n\ |
|
173 It must have the form\n\ |
|
174 \n\ |
|
175 @example\n\ |
|
176 @var{res} = f (@var{x}, @var{xdot}, @var{t})\n\ |
|
177 @end example\n\ |
|
178 \n\ |
|
179 @noindent\n\ |
4115
|
180 in which @var{x}, @var{xdot}, and @var{res} are vectors, and @var{t} is a\n\ |
3912
|
181 scalar.\n\ |
|
182 \n\ |
4115
|
183 If @var{fcn} is a two-element string array, the first element names\n\ |
|
184 the function @math{f} described above, and the second element names\n\ |
4117
|
185 a function to compute the modified Jacobian\n\ |
4115
|
186 \n\ |
|
187 @tex\n\ |
|
188 $$\n\ |
|
189 J = {\\partial f \\over \\partial x}\n\ |
|
190 + c {\\partial f \\over \\partial \\dot{x}}\n\ |
|
191 $$\n\ |
|
192 @end tex\n\ |
|
193 @ifinfo\n\ |
|
194 df df\n\ |
|
195 jac = -- + c ------\n\ |
|
196 dx d xdot\n\ |
|
197 @example\n\ |
|
198 @end example\n\ |
|
199 \n\ |
|
200 @end ifinfo\n\ |
|
201 \n\ |
|
202 The modified Jacobian function must have the form\n\ |
|
203 \n\ |
|
204 @example\n\ |
|
205 \n\ |
|
206 @var{jac} = j (@var{x}, @var{xdot}, @var{t}, @var{c})\n\ |
|
207 \n\ |
|
208 @end example\n\ |
|
209 \n\ |
3912
|
210 The second and third arguments to @code{daspk} specify the initial\n\ |
|
211 condition of the states and their derivatives, and the fourth argument\n\ |
4115
|
212 specifies a vector of output times at which the solution is desired,\n\ |
3912
|
213 including the time corresponding to the initial condition.\n\ |
|
214 \n\ |
|
215 The set of initial states and derivatives are not strictly required to\n\ |
4115
|
216 be consistent. If they are not consistent, you must use the\n\ |
|
217 @code{daspk_options} function to provide additional information so\n\ |
|
218 that @code{daspk} can compute a consistent starting point.\n\ |
3912
|
219 \n\ |
|
220 The fifth argument is optional, and may be used to specify a set of\n\ |
|
221 times that the DAE solver should not integrate past. It is useful for\n\ |
|
222 avoiding difficulties with singularities and points where there is a\n\ |
|
223 discontinuity in the derivative.\n\ |
3964
|
224 \n\ |
4115
|
225 After a successful computation, the value of @var{istate} will be\n\ |
|
226 greater than zero (consistent with the Fortran version of @sc{Daspk}).\n\ |
|
227 \n\ |
|
228 If the computation is not successful, the value of @var{istate} will be\n\ |
|
229 less than zero and @var{msg} will contain additional information.\n\ |
|
230 \n\ |
3964
|
231 You can use the function @code{daspk_options} to set optional\n\ |
|
232 parameters for @code{daspk}.\n\ |
4115
|
233 @end deftypefn\n\ |
|
234 @seealso{dassl}") |
3912
|
235 { |
|
236 octave_value_list retval; |
|
237 |
|
238 unwind_protect::begin_frame ("Fdaspk"); |
|
239 |
|
240 unwind_protect_int (call_depth); |
|
241 call_depth++; |
|
242 |
|
243 if (call_depth > 1) |
|
244 DASPK_ABORT1 ("invalid recursive call"); |
|
245 |
|
246 int nargin = args.length (); |
|
247 |
|
248 if (nargin > 3 && nargin < 6) |
|
249 { |
|
250 daspk_fcn = extract_function |
|
251 (args(0), "daspk", "__daspk_fcn__", |
|
252 "function res = __daspk_fcn__ (x, xdot, t) res = ", |
|
253 "; endfunction"); |
|
254 |
|
255 if (! daspk_fcn) |
|
256 DASPK_ABORT (); |
|
257 |
|
258 ColumnVector state = ColumnVector (args(1).vector_value ()); |
|
259 |
|
260 if (error_state) |
|
261 DASPK_ABORT1 ("expecting state vector as second argument"); |
|
262 |
|
263 ColumnVector deriv (args(2).vector_value ()); |
|
264 |
|
265 if (error_state) |
|
266 DASPK_ABORT1 ("expecting derivative vector as third argument"); |
|
267 |
|
268 ColumnVector out_times (args(3).vector_value ()); |
|
269 |
|
270 if (error_state) |
|
271 DASPK_ABORT1 ("expecting output time vector as fourth argument"); |
|
272 |
|
273 ColumnVector crit_times; |
|
274 int crit_times_set = 0; |
|
275 if (nargin > 4) |
|
276 { |
|
277 crit_times = ColumnVector (args(4).vector_value ()); |
|
278 |
|
279 if (error_state) |
|
280 DASPK_ABORT1 ("expecting critical time vector as fifth argument"); |
|
281 |
|
282 crit_times_set = 1; |
|
283 } |
|
284 |
|
285 if (state.capacity () != deriv.capacity ()) |
|
286 DASPK_ABORT1 ("x and xdot must have the same size"); |
|
287 |
|
288 double tzero = out_times (0); |
|
289 |
|
290 DAEFunc func (daspk_user_function); |
|
291 DASPK dae (state, deriv, tzero, func); |
4122
|
292 dae.set_options (daspk_opts); |
3912
|
293 |
|
294 Matrix output; |
|
295 Matrix deriv_output; |
|
296 |
|
297 if (crit_times_set) |
|
298 output = dae.integrate (out_times, deriv_output, crit_times); |
|
299 else |
|
300 output = dae.integrate (out_times, deriv_output); |
|
301 |
|
302 if (! error_state) |
|
303 { |
3997
|
304 std::string msg = dae.error_message (); |
|
305 |
|
306 retval(3) = msg; |
|
307 retval(2) = static_cast<double> (dae.integration_state ()); |
3912
|
308 |
3997
|
309 if (dae.integration_ok ()) |
|
310 { |
|
311 retval(1) = deriv_output; |
|
312 retval(0) = output; |
|
313 } |
|
314 else |
|
315 { |
|
316 retval(1) = Matrix (); |
|
317 retval(0) = Matrix (); |
|
318 |
|
319 if (nargout < 3) |
|
320 error ("daspk: %s", msg.c_str ()); |
|
321 } |
3912
|
322 } |
|
323 } |
|
324 else |
|
325 print_usage ("daspk"); |
|
326 |
|
327 unwind_protect::run_frame ("Fdaspk"); |
|
328 |
|
329 return retval; |
|
330 } |
|
331 |
|
332 /* |
|
333 ;;; Local Variables: *** |
|
334 ;;; mode: C++ *** |
|
335 ;;; End: *** |
|
336 */ |