Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/daspk.cc @ 10155:d0ce5e973937
DLD-FUNCTIONS/*.cc: delete Emacs local-variables settings
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 20 Jan 2010 17:40:53 -0500 |
parents | 40dfc0c99116 |
children | 89f4d7e294cc |
rev | line source |
---|---|
3912 | 1 /* |
2 | |
8920 | 3 Copyright (C) 1996, 1997, 2002, 2003, 2004, 2005, 2006, 2007, 2009 |
7017 | 4 John W. Eaton |
3912 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
3912 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
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, | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
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) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
80 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
81 gripe_user_supplied_eval ("daspk"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
82 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
83 } |
3912 | 84 |
85 int tlen = tmp.length (); | |
86 if (tlen > 0 && tmp(0).is_defined ()) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
87 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
88 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
89 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
90 warning ("daspk: ignoring imaginary part returned from user-supplied function"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
91 warned_fcn_imaginary = true; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
92 } |
4140 | 93 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
94 retval = ColumnVector (tmp(0).vector_value ()); |
3912 | 95 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
96 if (tlen > 1) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
97 ires = tmp(1).int_value (); |
3912 | 98 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
99 if (error_state || retval.length () == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
100 gripe_user_supplied_eval ("daspk"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
101 } |
3912 | 102 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
103 gripe_user_supplied_eval ("daspk"); |
3912 | 104 } |
105 | |
106 return retval; | |
107 } | |
108 | |
4140 | 109 Matrix |
110 daspk_user_jacobian (const ColumnVector& x, const ColumnVector& xdot, | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
111 double t, double cj) |
4140 | 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) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
129 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
130 gripe_user_supplied_eval ("daspk"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
131 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
132 } |
4140 | 133 |
134 int tlen = tmp.length (); | |
135 if (tlen > 0 && tmp(0).is_defined ()) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
136 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
137 if (! warned_jac_imaginary && tmp(0).is_complex_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
138 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
139 warning ("daspk: ignoring imaginary part returned from user-supplied jacobian function"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
140 warned_jac_imaginary = true; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
141 } |
4140 | 142 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
143 retval = tmp(0).matrix_value (); |
4140 | 144 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
145 if (error_state || retval.length () == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
146 gripe_user_supplied_eval ("daspk"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
147 } |
4140 | 148 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
149 gripe_user_supplied_eval ("daspk"); |
4140 | 150 } |
151 | |
152 return retval; | |
153 } | |
154 | |
3912 | 155 #define DASPK_ABORT() \ |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
156 return retval |
3912 | 157 |
158 #define DASPK_ABORT1(msg) \ | |
159 do \ | |
160 { \ | |
161 ::error ("daspk: " msg); \ | |
162 DASPK_ABORT (); \ | |
163 } \ | |
164 while (0) | |
165 | |
166 #define DASPK_ABORT2(fmt, arg) \ | |
167 do \ | |
168 { \ | |
169 ::error ("daspk: " fmt, arg); \ | |
170 DASPK_ABORT (); \ | |
171 } \ | |
172 while (0) | |
173 | |
3997 | 174 DEFUN_DLD (daspk, args, nargout, |
3912 | 175 "-*- texinfo -*-\n\ |
4115 | 176 @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\ |
177 Solve the set of differential-algebraic equations\n\ | |
178 @tex\n\ | |
4852 | 179 $$ 0 = f (x, \\dot{x}, t) $$\n\ |
4115 | 180 with\n\ |
181 $$ x(t_0) = x_0, \\dot{x}(t_0) = \\dot{x}_0 $$\n\ | |
182 @end tex\n\ | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
183 @ifnottex\n\ |
4115 | 184 \n\ |
185 @example\n\ | |
4698 | 186 0 = f (x, xdot, t)\n\ |
4115 | 187 @end example\n\ |
188 \n\ | |
189 with\n\ | |
190 \n\ | |
191 @example\n\ | |
192 x(t_0) = x_0, xdot(t_0) = xdot_0\n\ | |
193 @end example\n\ | |
194 \n\ | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
195 @end ifnottex\n\ |
4115 | 196 The solution is returned in the matrices @var{x} and @var{xdot},\n\ |
197 with each row in the result matrices corresponding to one of the\n\ | |
3912 | 198 elements in the vector @var{t}. The first element of @var{t}\n\ |
4115 | 199 should be @math{t_0} and correspond to the initial state of the\n\ |
200 system @var{x_0} and its derivative @var{xdot_0}, so that the first\n\ | |
201 row of the output @var{x} is @var{x_0} and the first row\n\ | |
202 of the output @var{xdot} is @var{xdot_0}.\n\ | |
3912 | 203 \n\ |
8787 | 204 The first argument, @var{fcn}, is a string, inline, or function handle\n\ |
205 that names the function @math{f} to call to compute the vector of\n\ | |
206 residuals for the set of equations. It must have the form\n\ | |
3912 | 207 \n\ |
208 @example\n\ | |
209 @var{res} = f (@var{x}, @var{xdot}, @var{t})\n\ | |
210 @end example\n\ | |
211 \n\ | |
212 @noindent\n\ | |
4115 | 213 in which @var{x}, @var{xdot}, and @var{res} are vectors, and @var{t} is a\n\ |
3912 | 214 scalar.\n\ |
215 \n\ | |
8787 | 216 If @var{fcn} is a two-element string array or a two-element cell array\n\ |
9067
8970b4b10e9f
Cleanup documentation for quad.texi and diffeq.texi
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
217 of strings, inline functions, or function handles, the first element names\n\ |
8787 | 218 the function @math{f} described above, and the second element names a\n\ |
219 function to compute the modified Jacobian\n\ | |
4115 | 220 @tex\n\ |
221 $$\n\ | |
222 J = {\\partial f \\over \\partial x}\n\ | |
223 + c {\\partial f \\over \\partial \\dot{x}}\n\ | |
224 $$\n\ | |
225 @end tex\n\ | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
226 @ifnottex\n\ |
4144 | 227 \n\ |
228 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
229 @group\n\ |
4115 | 230 df df\n\ |
231 jac = -- + c ------\n\ | |
232 dx d xdot\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
233 @end group\n\ |
4115 | 234 @end example\n\ |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7017
diff
changeset
|
235 @end ifnottex\n\ |
4115 | 236 \n\ |
237 The modified Jacobian function must have the form\n\ | |
238 \n\ | |
239 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
240 @group\n\ |
4115 | 241 \n\ |
242 @var{jac} = j (@var{x}, @var{xdot}, @var{t}, @var{c})\n\ | |
243 \n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
244 @end group\n\ |
4115 | 245 @end example\n\ |
246 \n\ | |
3912 | 247 The second and third arguments to @code{daspk} specify the initial\n\ |
248 condition of the states and their derivatives, and the fourth argument\n\ | |
4115 | 249 specifies a vector of output times at which the solution is desired,\n\ |
3912 | 250 including the time corresponding to the initial condition.\n\ |
251 \n\ | |
252 The set of initial states and derivatives are not strictly required to\n\ | |
4115 | 253 be consistent. If they are not consistent, you must use the\n\ |
254 @code{daspk_options} function to provide additional information so\n\ | |
255 that @code{daspk} can compute a consistent starting point.\n\ | |
3912 | 256 \n\ |
257 The fifth argument is optional, and may be used to specify a set of\n\ | |
258 times that the DAE solver should not integrate past. It is useful for\n\ | |
259 avoiding difficulties with singularities and points where there is a\n\ | |
260 discontinuity in the derivative.\n\ | |
3964 | 261 \n\ |
4115 | 262 After a successful computation, the value of @var{istate} will be\n\ |
263 greater than zero (consistent with the Fortran version of @sc{Daspk}).\n\ | |
264 \n\ | |
265 If the computation is not successful, the value of @var{istate} will be\n\ | |
266 less than zero and @var{msg} will contain additional information.\n\ | |
267 \n\ | |
3964 | 268 You can use the function @code{daspk_options} to set optional\n\ |
269 parameters for @code{daspk}.\n\ | |
5642 | 270 @seealso{dassl}\n\ |
271 @end deftypefn") | |
3912 | 272 { |
273 octave_value_list retval; | |
274 | |
4140 | 275 warned_fcn_imaginary = false; |
276 warned_jac_imaginary = false; | |
277 | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
278 unwind_protect frame; |
3912 | 279 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
280 frame.protect_var (call_depth); |
3912 | 281 call_depth++; |
282 | |
283 if (call_depth > 1) | |
284 DASPK_ABORT1 ("invalid recursive call"); | |
285 | |
286 int nargin = args.length (); | |
287 | |
288 if (nargin > 3 && nargin < 6) | |
289 { | |
5729 | 290 std::string fcn_name, fname, jac_name, jname; |
4140 | 291 daspk_fcn = 0; |
292 daspk_jac = 0; | |
293 | |
294 octave_value f_arg = args(0); | |
295 | |
5729 | 296 if (f_arg.is_cell ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
297 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
298 Cell c = f_arg.cell_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
299 if (c.length() == 1) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
300 f_arg = c(0); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
301 else if (c.length() == 2) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
302 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
303 if (c(0).is_function_handle () || c(0).is_inline_function ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
304 daspk_fcn = c(0).function_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
305 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
306 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
307 fcn_name = unique_symbol_name ("__daspk_fcn__"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
308 fname = "function y = "; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
309 fname.append (fcn_name); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
310 fname.append (" (x, xdot, t) y = "); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
311 daspk_fcn = extract_function |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
312 (c(0), "daspk", fcn_name, fname, "; endfunction"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
313 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
314 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
315 if (daspk_fcn) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
316 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
317 if (c(1).is_function_handle () || c(1).is_inline_function ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
318 daspk_jac = c(1).function_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
319 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
320 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
321 jac_name = unique_symbol_name ("__daspk_jac__"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
322 jname = "function jac = "; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
323 jname.append(jac_name); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
324 jname.append (" (x, xdot, t, cj) jac = "); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
325 daspk_jac = extract_function |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
326 (c(1), "daspk", jac_name, jname, "; endfunction"); |
4140 | 327 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
328 if (!daspk_jac) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
329 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
330 if (fcn_name.length()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
331 clear_function (fcn_name); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
332 daspk_fcn = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
333 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
334 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
335 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
336 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
337 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
338 DASPK_ABORT1 ("incorrect number of elements in cell array"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
339 } |
3912 | 340 |
5729 | 341 if (!daspk_fcn && ! f_arg.is_cell()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
342 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
343 if (f_arg.is_function_handle () || f_arg.is_inline_function ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
344 daspk_fcn = f_arg.function_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
345 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
346 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
347 switch (f_arg.rows ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
348 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
349 case 1: |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
350 do |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
351 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
352 fcn_name = unique_symbol_name ("__daspk_fcn__"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
353 fname = "function y = "; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
354 fname.append (fcn_name); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
355 fname.append (" (x, xdot, t) y = "); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
356 daspk_fcn = extract_function |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
357 (f_arg, "daspk", fcn_name, fname, "; endfunction"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
358 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
359 while (0); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
360 break; |
4140 | 361 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
362 case 2: |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
363 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
364 string_vector tmp = f_arg.all_strings (); |
5729 | 365 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
366 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
367 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
368 fcn_name = unique_symbol_name ("__daspk_fcn__"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
369 fname = "function y = "; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
370 fname.append (fcn_name); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
371 fname.append (" (x, xdot, t) y = "); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
372 daspk_fcn = extract_function |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
373 (tmp(0), "daspk", fcn_name, fname, "; endfunction"); |
4140 | 374 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
375 if (daspk_fcn) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
376 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
377 jac_name = unique_symbol_name ("__daspk_jac__"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
378 jname = "function jac = "; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
379 jname.append(jac_name); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
380 jname.append (" (x, xdot, t, cj) jac = "); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
381 daspk_jac = extract_function |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
382 (tmp(1), "daspk", jac_name, jname, |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
383 "; endfunction"); |
5729 | 384 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
385 if (!daspk_jac) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
386 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
387 if (fcn_name.length()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
388 clear_function (fcn_name); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
389 daspk_fcn = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
390 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
391 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
392 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
393 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
394 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
395 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
396 } |
4140 | 397 |
398 if (error_state || ! daspk_fcn) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
399 DASPK_ABORT (); |
3912 | 400 |
401 ColumnVector state = ColumnVector (args(1).vector_value ()); | |
402 | |
403 if (error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
404 DASPK_ABORT1 ("expecting state vector as second argument"); |
3912 | 405 |
406 ColumnVector deriv (args(2).vector_value ()); | |
407 | |
408 if (error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
409 DASPK_ABORT1 ("expecting derivative vector as third argument"); |
3912 | 410 |
411 ColumnVector out_times (args(3).vector_value ()); | |
412 | |
413 if (error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
414 DASPK_ABORT1 ("expecting output time vector as fourth argument"); |
3912 | 415 |
416 ColumnVector crit_times; | |
417 int crit_times_set = 0; | |
418 if (nargin > 4) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
419 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
420 crit_times = ColumnVector (args(4).vector_value ()); |
3912 | 421 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
422 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
423 DASPK_ABORT1 ("expecting critical time vector as fifth argument"); |
3912 | 424 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
425 crit_times_set = 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
426 } |
3912 | 427 |
428 if (state.capacity () != deriv.capacity ()) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
429 DASPK_ABORT1 ("x and xdot must have the same size"); |
3912 | 430 |
431 double tzero = out_times (0); | |
432 | |
433 DAEFunc func (daspk_user_function); | |
4140 | 434 if (daspk_jac) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
435 func.set_jacobian_function (daspk_user_jacobian); |
4140 | 436 |
3912 | 437 DASPK dae (state, deriv, tzero, func); |
4122 | 438 dae.set_options (daspk_opts); |
3912 | 439 |
440 Matrix output; | |
441 Matrix deriv_output; | |
442 | |
443 if (crit_times_set) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
444 output = dae.integrate (out_times, deriv_output, crit_times); |
3912 | 445 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
446 output = dae.integrate (out_times, deriv_output); |
3912 | 447 |
5729 | 448 if (fcn_name.length()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
449 clear_function (fcn_name); |
5729 | 450 if (jac_name.length()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
451 clear_function (jac_name); |
5729 | 452 |
3912 | 453 if (! error_state) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
454 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
455 std::string msg = dae.error_message (); |
3997 | 456 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
457 retval(3) = msg; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
458 retval(2) = static_cast<double> (dae.integration_state ()); |
3912 | 459 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
460 if (dae.integration_ok ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
461 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
462 retval(1) = deriv_output; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
463 retval(0) = output; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
464 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
465 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
466 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
467 retval(1) = Matrix (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
468 retval(0) = Matrix (); |
3997 | 469 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
470 if (nargout < 3) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
471 error ("daspk: %s", msg.c_str ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
472 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
473 } |
3912 | 474 } |
475 else | |
5823 | 476 print_usage (); |
3912 | 477 |
478 return retval; | |
479 } |