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\ |
|
143 @deftypefn {Loadable Function} {[@var{x}, @var{xdot}] =} daspk (@var{fcn}, @var{x0}, @var{xdot0}, @var{t}, @var{t_crit})\n\ |
|
144 Return a matrix of states and their first derivatives with respect to\n\ |
|
145 @var{t}. Each row in the result matrices correspond to one of the\n\ |
|
146 elements in the vector @var{t}. The first element of @var{t}\n\ |
|
147 corresponds to the initial state @var{x0} and derivative @var{xdot0}, so\n\ |
|
148 that the first row of the output @var{x} is @var{x0} and the first row\n\ |
|
149 of the output @var{xdot} is @var{xdot0}.\n\ |
|
150 \n\ |
|
151 The first argument, @var{fcn}, is a string that names the function to\n\ |
|
152 call to compute the vector of residuals for the set of equations.\n\ |
|
153 It must have the form\n\ |
|
154 \n\ |
|
155 @example\n\ |
|
156 @var{res} = f (@var{x}, @var{xdot}, @var{t})\n\ |
|
157 @end example\n\ |
|
158 \n\ |
|
159 @noindent\n\ |
|
160 where @var{x}, @var{xdot}, and @var{res} are vectors, and @var{t} is a\n\ |
|
161 scalar.\n\ |
|
162 \n\ |
|
163 The second and third arguments to @code{daspk} specify the initial\n\ |
|
164 condition of the states and their derivatives, and the fourth argument\n\ |
|
165 specifies a vector of output times at which the solution is desired, \n\ |
|
166 including the time corresponding to the initial condition.\n\ |
|
167 \n\ |
|
168 The set of initial states and derivatives are not strictly required to\n\ |
|
169 be consistent. In practice, however, @sc{Dassl} is not very good at\n\ |
|
170 determining a consistent set for you, so it is best if you ensure that\n\ |
|
171 the initial values result in the function evaluating to zero.\n\ |
|
172 \n\ |
|
173 The fifth argument is optional, and may be used to specify a set of\n\ |
|
174 times that the DAE solver should not integrate past. It is useful for\n\ |
|
175 avoiding difficulties with singularities and points where there is a\n\ |
|
176 discontinuity in the derivative.\n\ |
3964
|
177 \n\ |
|
178 You can use the function @code{daspk_options} to set optional\n\ |
|
179 parameters for @code{daspk}.\n\ |
3912
|
180 @end deftypefn") |
|
181 { |
|
182 octave_value_list retval; |
|
183 |
|
184 unwind_protect::begin_frame ("Fdaspk"); |
|
185 |
|
186 unwind_protect_int (call_depth); |
|
187 call_depth++; |
|
188 |
|
189 if (call_depth > 1) |
|
190 DASPK_ABORT1 ("invalid recursive call"); |
|
191 |
|
192 int nargin = args.length (); |
|
193 |
|
194 if (nargin > 3 && nargin < 6) |
|
195 { |
|
196 daspk_fcn = extract_function |
|
197 (args(0), "daspk", "__daspk_fcn__", |
|
198 "function res = __daspk_fcn__ (x, xdot, t) res = ", |
|
199 "; endfunction"); |
|
200 |
|
201 if (! daspk_fcn) |
|
202 DASPK_ABORT (); |
|
203 |
|
204 ColumnVector state = ColumnVector (args(1).vector_value ()); |
|
205 |
|
206 if (error_state) |
|
207 DASPK_ABORT1 ("expecting state vector as second argument"); |
|
208 |
|
209 ColumnVector deriv (args(2).vector_value ()); |
|
210 |
|
211 if (error_state) |
|
212 DASPK_ABORT1 ("expecting derivative vector as third argument"); |
|
213 |
|
214 ColumnVector out_times (args(3).vector_value ()); |
|
215 |
|
216 if (error_state) |
|
217 DASPK_ABORT1 ("expecting output time vector as fourth argument"); |
|
218 |
|
219 ColumnVector crit_times; |
|
220 int crit_times_set = 0; |
|
221 if (nargin > 4) |
|
222 { |
|
223 crit_times = ColumnVector (args(4).vector_value ()); |
|
224 |
|
225 if (error_state) |
|
226 DASPK_ABORT1 ("expecting critical time vector as fifth argument"); |
|
227 |
|
228 crit_times_set = 1; |
|
229 } |
|
230 |
|
231 if (state.capacity () != deriv.capacity ()) |
|
232 DASPK_ABORT1 ("x and xdot must have the same size"); |
|
233 |
|
234 double tzero = out_times (0); |
|
235 |
|
236 DAEFunc func (daspk_user_function); |
|
237 DASPK dae (state, deriv, tzero, func); |
|
238 dae.copy (daspk_opts); |
|
239 |
|
240 Matrix output; |
|
241 Matrix deriv_output; |
|
242 |
|
243 if (crit_times_set) |
|
244 output = dae.integrate (out_times, deriv_output, crit_times); |
|
245 else |
|
246 output = dae.integrate (out_times, deriv_output); |
|
247 |
|
248 if (! error_state) |
|
249 { |
3997
|
250 std::string msg = dae.error_message (); |
|
251 |
|
252 retval(3) = msg; |
|
253 retval(2) = static_cast<double> (dae.integration_state ()); |
3912
|
254 |
3997
|
255 if (dae.integration_ok ()) |
|
256 { |
|
257 retval(1) = deriv_output; |
|
258 retval(0) = output; |
|
259 } |
|
260 else |
|
261 { |
|
262 retval(1) = Matrix (); |
|
263 retval(0) = Matrix (); |
|
264 |
|
265 if (nargout < 3) |
|
266 error ("daspk: %s", msg.c_str ()); |
|
267 } |
3912
|
268 } |
|
269 } |
|
270 else |
|
271 print_usage ("daspk"); |
|
272 |
|
273 unwind_protect::run_frame ("Fdaspk"); |
|
274 |
|
275 return retval; |
|
276 } |
|
277 |
|
278 /* |
|
279 ;;; Local Variables: *** |
|
280 ;;; mode: C++ *** |
|
281 ;;; End: *** |
|
282 */ |