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" |
5729
|
40 #include "ov-cell.h" |
3912
|
41 #include "pager.h" |
|
42 #include "unwind-prot.h" |
|
43 #include "utils.h" |
|
44 #include "variables.h" |
|
45 |
3998
|
46 #include "DASPK-opts.cc" |
|
47 |
3912
|
48 // Global pointer for user defined function required by daspk. |
|
49 static octave_function *daspk_fcn; |
|
50 |
4140
|
51 // Global pointer for optional user defined jacobian function. |
|
52 static octave_function *daspk_jac; |
|
53 |
|
54 // Have we warned about imaginary values returned from user function? |
|
55 static bool warned_fcn_imaginary = false; |
|
56 static bool warned_jac_imaginary = false; |
|
57 |
3912
|
58 // Is this a recursive call? |
|
59 static int call_depth = 0; |
|
60 |
|
61 ColumnVector |
|
62 daspk_user_function (const ColumnVector& x, const ColumnVector& xdot, |
5275
|
63 double t, octave_idx_type& ires) |
3912
|
64 { |
|
65 ColumnVector retval; |
|
66 |
4628
|
67 assert (x.capacity () == xdot.capacity ()); |
3912
|
68 |
|
69 octave_value_list args; |
4628
|
70 |
3912
|
71 args(2) = t; |
4628
|
72 args(1) = xdot; |
|
73 args(0) = x; |
3912
|
74 |
|
75 if (daspk_fcn) |
|
76 { |
|
77 octave_value_list tmp = daspk_fcn->do_multi_index_op (1, args); |
|
78 |
|
79 if (error_state) |
|
80 { |
|
81 gripe_user_supplied_eval ("daspk"); |
|
82 return retval; |
|
83 } |
|
84 |
|
85 int tlen = tmp.length (); |
|
86 if (tlen > 0 && tmp(0).is_defined ()) |
|
87 { |
4140
|
88 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
|
89 { |
|
90 warning ("daspk: ignoring imaginary part returned from user-supplied function"); |
|
91 warned_fcn_imaginary = true; |
|
92 } |
|
93 |
3912
|
94 retval = ColumnVector (tmp(0).vector_value ()); |
|
95 |
|
96 if (tlen > 1) |
|
97 ires = tmp(1).int_value (); |
|
98 |
|
99 if (error_state || retval.length () == 0) |
|
100 gripe_user_supplied_eval ("daspk"); |
|
101 } |
|
102 else |
|
103 gripe_user_supplied_eval ("daspk"); |
|
104 } |
|
105 |
|
106 return retval; |
|
107 } |
|
108 |
4140
|
109 Matrix |
|
110 daspk_user_jacobian (const ColumnVector& x, const ColumnVector& xdot, |
|
111 double t, double cj) |
|
112 { |
|
113 Matrix retval; |
|
114 |
4628
|
115 assert (x.capacity () == xdot.capacity ()); |
4140
|
116 |
|
117 octave_value_list args; |
|
118 |
|
119 args(3) = cj; |
|
120 args(2) = t; |
4628
|
121 args(1) = xdot; |
|
122 args(0) = x; |
4140
|
123 |
|
124 if (daspk_jac) |
|
125 { |
|
126 octave_value_list tmp = daspk_jac->do_multi_index_op (1, args); |
|
127 |
|
128 if (error_state) |
|
129 { |
|
130 gripe_user_supplied_eval ("daspk"); |
|
131 return retval; |
|
132 } |
|
133 |
|
134 int tlen = tmp.length (); |
|
135 if (tlen > 0 && tmp(0).is_defined ()) |
|
136 { |
|
137 if (! warned_jac_imaginary && tmp(0).is_complex_type ()) |
|
138 { |
|
139 warning ("daspk: ignoring imaginary part returned from user-supplied jacobian function"); |
|
140 warned_jac_imaginary = true; |
|
141 } |
|
142 |
|
143 retval = tmp(0).matrix_value (); |
|
144 |
|
145 if (error_state || retval.length () == 0) |
|
146 gripe_user_supplied_eval ("daspk"); |
|
147 } |
|
148 else |
|
149 gripe_user_supplied_eval ("daspk"); |
|
150 } |
|
151 |
|
152 return retval; |
|
153 } |
|
154 |
3912
|
155 #define DASPK_ABORT() \ |
|
156 do \ |
|
157 { \ |
|
158 unwind_protect::run_frame ("Fdaspk"); \ |
|
159 return retval; \ |
|
160 } \ |
|
161 while (0) |
|
162 |
|
163 #define DASPK_ABORT1(msg) \ |
|
164 do \ |
|
165 { \ |
|
166 ::error ("daspk: " msg); \ |
|
167 DASPK_ABORT (); \ |
|
168 } \ |
|
169 while (0) |
|
170 |
|
171 #define DASPK_ABORT2(fmt, arg) \ |
|
172 do \ |
|
173 { \ |
|
174 ::error ("daspk: " fmt, arg); \ |
|
175 DASPK_ABORT (); \ |
|
176 } \ |
|
177 while (0) |
|
178 |
3997
|
179 DEFUN_DLD (daspk, args, nargout, |
3912
|
180 "-*- texinfo -*-\n\ |
4115
|
181 @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\ |
|
182 Solve the set of differential-algebraic equations\n\ |
|
183 @tex\n\ |
4852
|
184 $$ 0 = f (x, \\dot{x}, t) $$\n\ |
4115
|
185 with\n\ |
|
186 $$ x(t_0) = x_0, \\dot{x}(t_0) = \\dot{x}_0 $$\n\ |
|
187 @end tex\n\ |
|
188 @ifinfo\n\ |
|
189 \n\ |
|
190 @example\n\ |
4698
|
191 0 = f (x, xdot, t)\n\ |
4115
|
192 @end example\n\ |
|
193 \n\ |
|
194 with\n\ |
|
195 \n\ |
|
196 @example\n\ |
|
197 x(t_0) = x_0, xdot(t_0) = xdot_0\n\ |
|
198 @end example\n\ |
|
199 \n\ |
|
200 @end ifinfo\n\ |
|
201 The solution is returned in the matrices @var{x} and @var{xdot},\n\ |
|
202 with each row in the result matrices corresponding to one of the\n\ |
3912
|
203 elements in the vector @var{t}. The first element of @var{t}\n\ |
4115
|
204 should be @math{t_0} and correspond to the initial state of the\n\ |
|
205 system @var{x_0} and its derivative @var{xdot_0}, so that the first\n\ |
|
206 row of the output @var{x} is @var{x_0} and the first row\n\ |
|
207 of the output @var{xdot} is @var{xdot_0}.\n\ |
3912
|
208 \n\ |
5729
|
209 The first argument, @var{fcn}, is a string or a two element cell array\n\ |
|
210 of strings, inline or function handle, that names the function, to call\n\ |
|
211 to compute the vector of residuals for the set of equations. It must\n\ |
|
212 have the form\n\ |
3912
|
213 \n\ |
|
214 @example\n\ |
|
215 @var{res} = f (@var{x}, @var{xdot}, @var{t})\n\ |
|
216 @end example\n\ |
|
217 \n\ |
|
218 @noindent\n\ |
4115
|
219 in which @var{x}, @var{xdot}, and @var{res} are vectors, and @var{t} is a\n\ |
3912
|
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 @tex\n\ |
|
226 $$\n\ |
|
227 J = {\\partial f \\over \\partial x}\n\ |
|
228 + c {\\partial f \\over \\partial \\dot{x}}\n\ |
|
229 $$\n\ |
|
230 @end tex\n\ |
|
231 @ifinfo\n\ |
4144
|
232 \n\ |
|
233 @example\n\ |
4115
|
234 df df\n\ |
|
235 jac = -- + c ------\n\ |
|
236 dx d xdot\n\ |
|
237 @end example\n\ |
|
238 @end ifinfo\n\ |
|
239 \n\ |
|
240 The modified Jacobian function must have the form\n\ |
|
241 \n\ |
|
242 @example\n\ |
|
243 \n\ |
|
244 @var{jac} = j (@var{x}, @var{xdot}, @var{t}, @var{c})\n\ |
|
245 \n\ |
|
246 @end example\n\ |
|
247 \n\ |
3912
|
248 The second and third arguments to @code{daspk} specify the initial\n\ |
|
249 condition of the states and their derivatives, and the fourth argument\n\ |
4115
|
250 specifies a vector of output times at which the solution is desired,\n\ |
3912
|
251 including the time corresponding to the initial condition.\n\ |
|
252 \n\ |
|
253 The set of initial states and derivatives are not strictly required to\n\ |
4115
|
254 be consistent. If they are not consistent, you must use the\n\ |
|
255 @code{daspk_options} function to provide additional information so\n\ |
|
256 that @code{daspk} can compute a consistent starting point.\n\ |
3912
|
257 \n\ |
|
258 The fifth argument is optional, and may be used to specify a set of\n\ |
|
259 times that the DAE solver should not integrate past. It is useful for\n\ |
|
260 avoiding difficulties with singularities and points where there is a\n\ |
|
261 discontinuity in the derivative.\n\ |
3964
|
262 \n\ |
4115
|
263 After a successful computation, the value of @var{istate} will be\n\ |
|
264 greater than zero (consistent with the Fortran version of @sc{Daspk}).\n\ |
|
265 \n\ |
|
266 If the computation is not successful, the value of @var{istate} will be\n\ |
|
267 less than zero and @var{msg} will contain additional information.\n\ |
|
268 \n\ |
3964
|
269 You can use the function @code{daspk_options} to set optional\n\ |
|
270 parameters for @code{daspk}.\n\ |
5642
|
271 @seealso{dassl}\n\ |
|
272 @end deftypefn") |
3912
|
273 { |
|
274 octave_value_list retval; |
|
275 |
4140
|
276 warned_fcn_imaginary = false; |
|
277 warned_jac_imaginary = false; |
|
278 |
3912
|
279 unwind_protect::begin_frame ("Fdaspk"); |
|
280 |
|
281 unwind_protect_int (call_depth); |
|
282 call_depth++; |
|
283 |
|
284 if (call_depth > 1) |
|
285 DASPK_ABORT1 ("invalid recursive call"); |
|
286 |
|
287 int nargin = args.length (); |
|
288 |
|
289 if (nargin > 3 && nargin < 6) |
|
290 { |
5729
|
291 std::string fcn_name, fname, jac_name, jname; |
4140
|
292 daspk_fcn = 0; |
|
293 daspk_jac = 0; |
|
294 |
|
295 octave_value f_arg = args(0); |
|
296 |
5729
|
297 if (f_arg.is_cell ()) |
|
298 { |
|
299 Cell c = f_arg.cell_value (); |
|
300 if (c.length() == 1) |
|
301 f_arg = c(0); |
|
302 else if (c.length() == 2) |
|
303 { |
|
304 if (c(0).is_function_handle () || c(0).is_inline_function ()) |
|
305 daspk_fcn = c(0).function_value (); |
|
306 else |
|
307 { |
|
308 fcn_name = unique_symbol_name ("__daspk_fcn__"); |
|
309 fname = "function y = "; |
|
310 fname.append (fcn_name); |
|
311 fname.append (" (x, xdot, t) y = "); |
|
312 daspk_fcn = extract_function |
|
313 (c(0), "daspk", fcn_name, fname, "; endfunction"); |
|
314 } |
|
315 |
|
316 if (daspk_fcn) |
|
317 { |
|
318 if (c(1).is_function_handle () || c(1).is_inline_function ()) |
|
319 daspk_jac = c(1).function_value (); |
|
320 else |
|
321 { |
|
322 jac_name = unique_symbol_name ("__daspk_jac__"); |
|
323 jname = "function jac = "; |
|
324 jname.append(jac_name); |
|
325 jname.append (" (x, xdot, t, cj) jac = "); |
|
326 daspk_jac = extract_function |
|
327 (c(1), "daspk", jac_name, jname, "; endfunction"); |
4140
|
328 |
5729
|
329 if (!daspk_jac) |
|
330 { |
|
331 if (fcn_name.length()) |
|
332 clear_function (fcn_name); |
|
333 daspk_fcn = 0; |
|
334 } |
|
335 } |
|
336 } |
|
337 } |
|
338 else |
|
339 DASPK_ABORT1 ("incorrect number of elements in cell array"); |
|
340 } |
3912
|
341 |
5729
|
342 if (!daspk_fcn && ! f_arg.is_cell()) |
|
343 { |
|
344 if (f_arg.is_function_handle () || f_arg.is_inline_function ()) |
|
345 daspk_fcn = f_arg.function_value (); |
|
346 else |
|
347 { |
|
348 switch (f_arg.rows ()) |
|
349 { |
|
350 case 1: |
|
351 do |
|
352 { |
|
353 fcn_name = unique_symbol_name ("__daspk_fcn__"); |
|
354 fname = "function y = "; |
|
355 fname.append (fcn_name); |
|
356 fname.append (" (x, xdot, t) y = "); |
|
357 daspk_fcn = extract_function |
|
358 (f_arg, "daspk", fcn_name, fname, "; endfunction"); |
|
359 } |
|
360 while (0); |
|
361 break; |
4140
|
362 |
5729
|
363 case 2: |
4140
|
364 { |
5729
|
365 string_vector tmp = f_arg.all_strings (); |
|
366 |
|
367 if (! error_state) |
|
368 { |
|
369 fcn_name = unique_symbol_name ("__daspk_fcn__"); |
|
370 fname = "function y = "; |
|
371 fname.append (fcn_name); |
|
372 fname.append (" (x, xdot, t) y = "); |
|
373 daspk_fcn = extract_function |
|
374 (tmp(0), "daspk", fcn_name, fname, "; endfunction"); |
4140
|
375 |
5729
|
376 if (daspk_fcn) |
|
377 { |
|
378 jac_name = unique_symbol_name ("__daspk_jac__"); |
|
379 jname = "function jac = "; |
|
380 jname.append(jac_name); |
|
381 jname.append (" (x, xdot, t, cj) jac = "); |
|
382 daspk_jac = extract_function |
|
383 (tmp(1), "daspk", jac_name, jname, |
|
384 "; endfunction"); |
|
385 |
|
386 if (!daspk_jac) |
|
387 { |
|
388 if (fcn_name.length()) |
|
389 clear_function (fcn_name); |
|
390 daspk_fcn = 0; |
|
391 } |
|
392 } |
|
393 } |
4140
|
394 } |
5729
|
395 } |
|
396 } |
4140
|
397 } |
|
398 |
|
399 if (error_state || ! daspk_fcn) |
3912
|
400 DASPK_ABORT (); |
|
401 |
|
402 ColumnVector state = ColumnVector (args(1).vector_value ()); |
|
403 |
|
404 if (error_state) |
|
405 DASPK_ABORT1 ("expecting state vector as second argument"); |
|
406 |
|
407 ColumnVector deriv (args(2).vector_value ()); |
|
408 |
|
409 if (error_state) |
|
410 DASPK_ABORT1 ("expecting derivative vector as third argument"); |
|
411 |
|
412 ColumnVector out_times (args(3).vector_value ()); |
|
413 |
|
414 if (error_state) |
|
415 DASPK_ABORT1 ("expecting output time vector as fourth argument"); |
|
416 |
|
417 ColumnVector crit_times; |
|
418 int crit_times_set = 0; |
|
419 if (nargin > 4) |
|
420 { |
|
421 crit_times = ColumnVector (args(4).vector_value ()); |
|
422 |
|
423 if (error_state) |
|
424 DASPK_ABORT1 ("expecting critical time vector as fifth argument"); |
|
425 |
|
426 crit_times_set = 1; |
|
427 } |
|
428 |
|
429 if (state.capacity () != deriv.capacity ()) |
|
430 DASPK_ABORT1 ("x and xdot must have the same size"); |
|
431 |
|
432 double tzero = out_times (0); |
|
433 |
|
434 DAEFunc func (daspk_user_function); |
4140
|
435 if (daspk_jac) |
|
436 func.set_jacobian_function (daspk_user_jacobian); |
|
437 |
3912
|
438 DASPK dae (state, deriv, tzero, func); |
4122
|
439 dae.set_options (daspk_opts); |
3912
|
440 |
|
441 Matrix output; |
|
442 Matrix deriv_output; |
|
443 |
|
444 if (crit_times_set) |
|
445 output = dae.integrate (out_times, deriv_output, crit_times); |
|
446 else |
|
447 output = dae.integrate (out_times, deriv_output); |
|
448 |
5729
|
449 if (fcn_name.length()) |
|
450 clear_function (fcn_name); |
|
451 if (jac_name.length()) |
|
452 clear_function (jac_name); |
|
453 |
3912
|
454 if (! error_state) |
|
455 { |
3997
|
456 std::string msg = dae.error_message (); |
|
457 |
|
458 retval(3) = msg; |
|
459 retval(2) = static_cast<double> (dae.integration_state ()); |
3912
|
460 |
3997
|
461 if (dae.integration_ok ()) |
|
462 { |
|
463 retval(1) = deriv_output; |
|
464 retval(0) = output; |
|
465 } |
|
466 else |
|
467 { |
|
468 retval(1) = Matrix (); |
|
469 retval(0) = Matrix (); |
|
470 |
|
471 if (nargout < 3) |
|
472 error ("daspk: %s", msg.c_str ()); |
|
473 } |
3912
|
474 } |
|
475 } |
|
476 else |
5823
|
477 print_usage (); |
3912
|
478 |
|
479 unwind_protect::run_frame ("Fdaspk"); |
|
480 |
|
481 return retval; |
|
482 } |
|
483 |
|
484 /* |
|
485 ;;; Local Variables: *** |
|
486 ;;; mode: C++ *** |
|
487 ;;; End: *** |
|
488 */ |