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