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 "LSODE.h" |
|
33 #include "lo-mappers.h" |
|
34 |
|
35 #include "defun-dld.h" |
|
36 #include "error.h" |
|
37 #include "gripes.h" |
|
38 #include "oct-obj.h" |
2968
|
39 #include "ov-fcn.h" |
2928
|
40 #include "pager.h" |
3952
|
41 #include "pr-output.h" |
3243
|
42 #include "unwind-prot.h" |
2928
|
43 #include "utils.h" |
|
44 #include "variables.h" |
|
45 |
3998
|
46 #include "LSODE-opts.cc" |
|
47 |
2928
|
48 // Global pointer for user defined function required by lsode. |
2968
|
49 static octave_function *lsode_fcn; |
2928
|
50 |
|
51 // Global pointer for optional user defined jacobian function used by lsode. |
2968
|
52 static octave_function *lsode_jac; |
2928
|
53 |
3243
|
54 // Is this a recursive call? |
|
55 static int call_depth = 0; |
|
56 |
2928
|
57 ColumnVector |
|
58 lsode_user_function (const ColumnVector& x, double t) |
|
59 { |
|
60 ColumnVector retval; |
|
61 |
|
62 int nstates = x.capacity (); |
|
63 |
|
64 octave_value_list args; |
|
65 args(1) = t; |
|
66 |
|
67 Matrix m (nstates, 1); |
|
68 for (int i = 0; i < nstates; i++) |
|
69 m (i, 0) = x (i); |
|
70 octave_value state (m); |
|
71 args(0) = state; |
|
72 |
|
73 if (lsode_fcn) |
|
74 { |
3544
|
75 octave_value_list tmp = lsode_fcn->do_multi_index_op (1, args); |
2928
|
76 |
|
77 if (error_state) |
|
78 { |
|
79 gripe_user_supplied_eval ("lsode"); |
|
80 return retval; |
|
81 } |
|
82 |
|
83 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
84 { |
3419
|
85 retval = ColumnVector (tmp(0).vector_value ()); |
2928
|
86 |
|
87 if (error_state || retval.length () == 0) |
|
88 gripe_user_supplied_eval ("lsode"); |
|
89 } |
|
90 else |
|
91 gripe_user_supplied_eval ("lsode"); |
|
92 } |
|
93 |
|
94 return retval; |
|
95 } |
|
96 |
|
97 Matrix |
|
98 lsode_user_jacobian (const ColumnVector& x, double t) |
|
99 { |
|
100 Matrix retval; |
|
101 |
|
102 int nstates = x.capacity (); |
|
103 |
|
104 octave_value_list args; |
|
105 args(1) = t; |
|
106 |
|
107 Matrix m (nstates, 1); |
|
108 for (int i = 0; i < nstates; i++) |
|
109 m (i, 0) = x (i); |
|
110 octave_value state (m); |
|
111 args(0) = state; |
|
112 |
|
113 if (lsode_jac) |
|
114 { |
3544
|
115 octave_value_list tmp = lsode_jac->do_multi_index_op (1, args); |
2928
|
116 |
|
117 if (error_state) |
|
118 { |
|
119 gripe_user_supplied_eval ("lsode"); |
|
120 return retval; |
|
121 } |
|
122 |
|
123 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
124 { |
|
125 retval = tmp(0).matrix_value (); |
|
126 |
|
127 if (error_state || retval.length () == 0) |
|
128 gripe_user_supplied_eval ("lsode"); |
|
129 } |
|
130 else |
|
131 gripe_user_supplied_eval ("lsode"); |
|
132 } |
|
133 |
|
134 return retval; |
|
135 } |
|
136 |
3323
|
137 #define LSODE_ABORT() \ |
|
138 do \ |
|
139 { \ |
|
140 unwind_protect::run_frame ("Flsode"); \ |
|
141 return retval; \ |
|
142 } \ |
|
143 while (0) |
|
144 |
|
145 #define LSODE_ABORT1(msg) \ |
|
146 do \ |
|
147 { \ |
3747
|
148 ::error ("lsode: " msg); \ |
3323
|
149 LSODE_ABORT (); \ |
|
150 } \ |
|
151 while (0) |
|
152 |
|
153 #define LSODE_ABORT2(fmt, arg) \ |
|
154 do \ |
|
155 { \ |
3747
|
156 ::error ("lsode: " fmt, arg); \ |
3323
|
157 LSODE_ABORT (); \ |
|
158 } \ |
|
159 while (0) |
|
160 |
2928
|
161 DEFUN_DLD (lsode, args, nargout, |
3373
|
162 "-*- texinfo -*-\n\ |
3959
|
163 @deftypefn {Loadable Function} {[@var{x}, @var{istate}, @var{msg}]} lsode (@var{fcn}, @var{x0}, @var{t}, @var{t_crit})\n\ |
3373
|
164 Return a matrix of @var{x} as a function of @var{t}, given the initial\n\ |
|
165 state of the system @var{x0}. Each row in the result matrix corresponds\n\ |
|
166 to one of the elements in the vector @var{t}. The first element of\n\ |
|
167 @var{t} corresponds to the initial state @var{x0}, so that the first row\n\ |
|
168 of the output is @var{x0}.\n\ |
|
169 \n\ |
|
170 The first argument, @var{fcn}, is a string that names the function to\n\ |
|
171 call to compute the vector of right hand sides for the set of equations.\n\ |
|
172 It must have the form\n\ |
2928
|
173 \n\ |
3373
|
174 @example\n\ |
|
175 @var{xdot} = f (@var{x}, @var{t})\n\ |
|
176 @end example\n\ |
2928
|
177 \n\ |
3373
|
178 @noindent\n\ |
|
179 where @var{xdot} and @var{x} are vectors and @var{t} is a scalar.\n\ |
2928
|
180 \n\ |
3373
|
181 The fourth argument is optional, and may be used to specify a set of\n\ |
|
182 times that the ODE solver should not integrate past. It is useful for\n\ |
|
183 avoiding difficulties with singularities and points where there is a\n\ |
|
184 discontinuity in the derivative.\n\ |
3964
|
185 \n\ |
|
186 You can use the function @code{lsode_options} to set optional\n\ |
|
187 parameters for @code{lsode}.\n\ |
3373
|
188 @end deftypefn") |
2928
|
189 { |
|
190 octave_value_list retval; |
|
191 |
3243
|
192 unwind_protect::begin_frame ("Flsode"); |
2928
|
193 |
3243
|
194 unwind_protect_int (call_depth); |
|
195 call_depth++; |
|
196 |
|
197 if (call_depth > 1) |
3323
|
198 LSODE_ABORT1 ("invalid recursive call"); |
2928
|
199 |
3243
|
200 int nargin = args.length (); |
2928
|
201 |
3959
|
202 if (nargin > 2 && nargin < 5 && nargout < 4) |
2928
|
203 { |
3991
|
204 lsode_fcn = 0; |
|
205 lsode_jac = 0; |
|
206 |
3243
|
207 octave_value f_arg = args(0); |
2928
|
208 |
3243
|
209 switch (f_arg.rows ()) |
|
210 { |
|
211 case 1: |
|
212 lsode_fcn = extract_function |
3991
|
213 (f_arg, "lsode", "__lsode_fcn__", |
3243
|
214 "function xdot = __lsode_fcn__ (x, t) xdot = ", |
|
215 "; endfunction"); |
|
216 break; |
2928
|
217 |
3243
|
218 case 2: |
2928
|
219 { |
3991
|
220 string_vector tmp = f_arg.all_strings (); |
2928
|
221 |
3243
|
222 if (! error_state) |
2928
|
223 { |
3243
|
224 lsode_fcn = extract_function |
|
225 (tmp(0), "lsode", "__lsode_fcn__", |
|
226 "function xdot = __lsode_fcn__ (x, t) xdot = ", |
2928
|
227 "; endfunction"); |
|
228 |
3243
|
229 if (lsode_fcn) |
|
230 { |
|
231 lsode_jac = extract_function |
|
232 (tmp(1), "lsode", "__lsode_jac__", |
|
233 "function jac = __lsode_jac__ (x, t) jac = ", |
|
234 "; endfunction"); |
|
235 |
|
236 if (! lsode_jac) |
|
237 lsode_fcn = 0; |
|
238 } |
2928
|
239 } |
|
240 } |
3243
|
241 break; |
2928
|
242 |
3243
|
243 default: |
3323
|
244 LSODE_ABORT1 |
|
245 ("first arg should be a string or 2-element string array"); |
3243
|
246 } |
2928
|
247 |
3243
|
248 if (error_state || ! lsode_fcn) |
3323
|
249 LSODE_ABORT (); |
2928
|
250 |
3419
|
251 ColumnVector state (args(1).vector_value ()); |
2928
|
252 |
|
253 if (error_state) |
3323
|
254 LSODE_ABORT1 ("expecting state vector as second argument"); |
3243
|
255 |
3419
|
256 ColumnVector out_times (args(2).vector_value ()); |
3243
|
257 |
|
258 if (error_state) |
3323
|
259 LSODE_ABORT1 ("expecting output time vector as third argument"); |
2928
|
260 |
3243
|
261 ColumnVector crit_times; |
2928
|
262 |
3243
|
263 int crit_times_set = 0; |
|
264 if (nargin > 3) |
|
265 { |
3419
|
266 crit_times = ColumnVector (args(3).vector_value ()); |
2928
|
267 |
3243
|
268 if (error_state) |
3323
|
269 LSODE_ABORT1 ("expecting critical time vector as fourth argument"); |
2928
|
270 |
3243
|
271 crit_times_set = 1; |
|
272 } |
2928
|
273 |
3243
|
274 double tzero = out_times (0); |
|
275 |
|
276 ODEFunc func (lsode_user_function); |
|
277 if (lsode_jac) |
|
278 func.set_jacobian_function (lsode_user_jacobian); |
2928
|
279 |
3243
|
280 LSODE ode (state, tzero, func); |
|
281 |
|
282 ode.copy (lsode_opts); |
|
283 |
3859
|
284 Matrix output; |
3243
|
285 if (crit_times_set) |
|
286 output = ode.integrate (out_times, crit_times); |
|
287 else |
|
288 output = ode.integrate (out_times); |
|
289 |
|
290 if (! error_state) |
|
291 { |
3997
|
292 std::string msg = ode.error_message (); |
|
293 |
|
294 retval(2) = msg; |
3959
|
295 retval(1) = static_cast<double> (ode.integration_state ()); |
|
296 |
|
297 if (ode.integration_ok ()) |
3971
|
298 retval(0) = output; |
3959
|
299 else |
|
300 { |
3971
|
301 retval(0) = Matrix (); |
3959
|
302 |
|
303 if (nargout < 2) |
3997
|
304 error ("lsode: %s", msg.c_str ()); |
3959
|
305 } |
3243
|
306 } |
|
307 } |
2928
|
308 else |
3243
|
309 print_usage ("lsode"); |
2928
|
310 |
3243
|
311 unwind_protect::run_frame ("Flsode"); |
2928
|
312 |
|
313 return retval; |
|
314 } |
|
315 |
|
316 /* |
|
317 ;;; Local Variables: *** |
|
318 ;;; mode: C++ *** |
|
319 ;;; End: *** |
|
320 */ |