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\ |
4115
|
163 @deftypefn {Loadable Function} {[@var{x}, @var{istate}, @var{msg}]} lsode (@var{fcn}, @var{x_0}, @var{t}, @var{t_crit})\n\ |
|
164 Solve the set of differential equations\n\ |
|
165 @tex\n\ |
|
166 $$ {dx \\over dt} = f (x, t) $$\n\ |
|
167 with\n\ |
|
168 $$ x(t_0) = x_0 $$\n\ |
|
169 @end tex\n\ |
|
170 @ifinfo\n\ |
|
171 \n\ |
|
172 @example\n\ |
|
173 dx\n\ |
|
174 -- = f(x, t)\n\ |
|
175 dt |
|
176 @end example\n\ |
|
177 \n\ |
|
178 with\n\ |
|
179 \n\ |
|
180 @example\n\ |
|
181 x(t_0) = x_0\n\ |
|
182 @end example\n\ |
|
183 \n\ |
|
184 @end ifinfo\n\ |
|
185 The solution is returned in the matrix @var{x}, with each row\n\ |
|
186 corresponding to an element of the vector @var{t}. The first element\n\ |
|
187 of @var{t} should be @math{t_0} and should correspond to the initial\n\ |
|
188 state of the system @var{x_0}, so that the first row of the output\n\ |
|
189 is @var{x_0}.\n\ |
3373
|
190 \n\ |
|
191 The first argument, @var{fcn}, is a string that names the function to\n\ |
|
192 call to compute the vector of right hand sides for the set of equations.\n\ |
4115
|
193 The function must have the form\n\ |
2928
|
194 \n\ |
3373
|
195 @example\n\ |
|
196 @var{xdot} = f (@var{x}, @var{t})\n\ |
|
197 @end example\n\ |
2928
|
198 \n\ |
3373
|
199 @noindent\n\ |
4115
|
200 in which @var{xdot} and @var{x} are vectors and @var{t} is a scalar.\n\ |
|
201 \n\ |
|
202 If @var{fcn} is a two-element string array, the first element names the\n\ |
|
203 function @math{f} described above, and the second element names a function\n\ |
|
204 to compute the Jacobian of @math{f}. The Jacobian function must have the\n\ |
|
205 form\n\ |
|
206 \n\ |
|
207 @example\n\ |
|
208 @var{jac} = j (@var{x}, @var{t})\n\ |
|
209 @end example\n\ |
|
210 \n\ |
|
211 in which @var{jac} is the matrix of partial derivatives\n\ |
|
212 @tex\n\ |
|
213 $$ J = {\\partial f_i \\over \\partial x_j} = \\left[\\matrix{\n\ |
|
214 {\\partial f_1 \\over \\partial x_1}\n\ |
|
215 & {\\partial f_1 \\over \\partial x_2}\n\ |
|
216 & \\cdots\n\ |
|
217 & {\\partial f_1 \\over \\partial x_N} \\cr\n\ |
|
218 {\\partial f_2 \\over \\partial x_1}\n\ |
|
219 & {\\partial f_2 \\over \\partial x_2}\n\ |
|
220 & \\cdots\n\ |
|
221 & {\\partial f_2 \\over \\partial x_N} \\cr\n\ |
|
222 \\vdots & \\vdots & \\ddots & \\vdots \\cr\n\ |
|
223 {\\partial f_3 \\over \\partial x_1}\n\ |
|
224 & {\\partial f_3 \\over \\partial x_2}\n\ |
|
225 & \\cdots\n\ |
|
226 & {\\partial f_3 \\over \\partial x_N} \\cr}\\right]$$\n\ |
|
227 @end tex\n\ |
|
228 @ifinfo\n\ |
|
229 \n\ |
|
230 @example\n\ |
|
231 | df_1 df_1 df_1 |\n\ |
|
232 | ---- ---- ... ---- |\n\ |
|
233 | dx_1 dx_2 dx_N |\n\ |
|
234 | |\n\ |
|
235 | df_2 df_2 df_2 |\n\ |
|
236 | ---- ---- ... ---- |\n\ |
|
237 df_i | dx_1 dx_2 dx_N |\n\ |
|
238 jac = ---- = | |\n\ |
|
239 dx_j | . . . . |\n\ |
|
240 | . . . . |\n\ |
|
241 | . . . . |\n\ |
|
242 | |\n\ |
|
243 | df_N df_N df_N |\n\ |
|
244 | ---- ---- ... ---- |\n\ |
|
245 | dx_1 dx_2 dx_N |\n\ |
|
246 @end example\n\ |
|
247 \n\ |
|
248 @end ifinfo\n\ |
|
249 \n\ |
|
250 The second and third arguments specify the intial state of the system,\n\ |
|
251 @math{x_0}, and the initial value of the independent variable @math{t_0}.\n\ |
2928
|
252 \n\ |
3373
|
253 The fourth argument is optional, and may be used to specify a set of\n\ |
|
254 times that the ODE solver should not integrate past. It is useful for\n\ |
|
255 avoiding difficulties with singularities and points where there is a\n\ |
|
256 discontinuity in the derivative.\n\ |
3964
|
257 \n\ |
4115
|
258 After a successful computation, the value of @var{istate} will be 2\n\ |
|
259 (consistent with the Fortran version of @sc{Lsode}).\n\ |
|
260 \n\ |
|
261 If the computation is not successful, @var{istate} will be something\n\ |
|
262 other than 2 and @var{msg} will contain additional information.\n\ |
|
263 \n\ |
3964
|
264 You can use the function @code{lsode_options} to set optional\n\ |
|
265 parameters for @code{lsode}.\n\ |
4115
|
266 @end deftypefn\n\ |
|
267 @seealso{daspk, dassl, dasrt, odessa}") |
2928
|
268 { |
|
269 octave_value_list retval; |
|
270 |
3243
|
271 unwind_protect::begin_frame ("Flsode"); |
2928
|
272 |
3243
|
273 unwind_protect_int (call_depth); |
|
274 call_depth++; |
|
275 |
|
276 if (call_depth > 1) |
3323
|
277 LSODE_ABORT1 ("invalid recursive call"); |
2928
|
278 |
3243
|
279 int nargin = args.length (); |
2928
|
280 |
3959
|
281 if (nargin > 2 && nargin < 5 && nargout < 4) |
2928
|
282 { |
3991
|
283 lsode_fcn = 0; |
|
284 lsode_jac = 0; |
|
285 |
3243
|
286 octave_value f_arg = args(0); |
2928
|
287 |
3243
|
288 switch (f_arg.rows ()) |
|
289 { |
|
290 case 1: |
|
291 lsode_fcn = extract_function |
3991
|
292 (f_arg, "lsode", "__lsode_fcn__", |
3243
|
293 "function xdot = __lsode_fcn__ (x, t) xdot = ", |
|
294 "; endfunction"); |
|
295 break; |
2928
|
296 |
3243
|
297 case 2: |
2928
|
298 { |
3991
|
299 string_vector tmp = f_arg.all_strings (); |
2928
|
300 |
3243
|
301 if (! error_state) |
2928
|
302 { |
3243
|
303 lsode_fcn = extract_function |
|
304 (tmp(0), "lsode", "__lsode_fcn__", |
|
305 "function xdot = __lsode_fcn__ (x, t) xdot = ", |
2928
|
306 "; endfunction"); |
|
307 |
3243
|
308 if (lsode_fcn) |
|
309 { |
|
310 lsode_jac = extract_function |
|
311 (tmp(1), "lsode", "__lsode_jac__", |
|
312 "function jac = __lsode_jac__ (x, t) jac = ", |
|
313 "; endfunction"); |
|
314 |
|
315 if (! lsode_jac) |
|
316 lsode_fcn = 0; |
|
317 } |
2928
|
318 } |
|
319 } |
3243
|
320 break; |
2928
|
321 |
3243
|
322 default: |
3323
|
323 LSODE_ABORT1 |
|
324 ("first arg should be a string or 2-element string array"); |
3243
|
325 } |
2928
|
326 |
3243
|
327 if (error_state || ! lsode_fcn) |
3323
|
328 LSODE_ABORT (); |
2928
|
329 |
3419
|
330 ColumnVector state (args(1).vector_value ()); |
2928
|
331 |
|
332 if (error_state) |
3323
|
333 LSODE_ABORT1 ("expecting state vector as second argument"); |
3243
|
334 |
3419
|
335 ColumnVector out_times (args(2).vector_value ()); |
3243
|
336 |
|
337 if (error_state) |
3323
|
338 LSODE_ABORT1 ("expecting output time vector as third argument"); |
2928
|
339 |
3243
|
340 ColumnVector crit_times; |
2928
|
341 |
3243
|
342 int crit_times_set = 0; |
|
343 if (nargin > 3) |
|
344 { |
3419
|
345 crit_times = ColumnVector (args(3).vector_value ()); |
2928
|
346 |
3243
|
347 if (error_state) |
3323
|
348 LSODE_ABORT1 ("expecting critical time vector as fourth argument"); |
2928
|
349 |
3243
|
350 crit_times_set = 1; |
|
351 } |
2928
|
352 |
3243
|
353 double tzero = out_times (0); |
|
354 |
|
355 ODEFunc func (lsode_user_function); |
|
356 if (lsode_jac) |
|
357 func.set_jacobian_function (lsode_user_jacobian); |
2928
|
358 |
3243
|
359 LSODE ode (state, tzero, func); |
|
360 |
|
361 ode.copy (lsode_opts); |
|
362 |
3859
|
363 Matrix output; |
3243
|
364 if (crit_times_set) |
|
365 output = ode.integrate (out_times, crit_times); |
|
366 else |
|
367 output = ode.integrate (out_times); |
|
368 |
|
369 if (! error_state) |
|
370 { |
3997
|
371 std::string msg = ode.error_message (); |
|
372 |
|
373 retval(2) = msg; |
3959
|
374 retval(1) = static_cast<double> (ode.integration_state ()); |
|
375 |
|
376 if (ode.integration_ok ()) |
3971
|
377 retval(0) = output; |
3959
|
378 else |
|
379 { |
3971
|
380 retval(0) = Matrix (); |
3959
|
381 |
|
382 if (nargout < 2) |
3997
|
383 error ("lsode: %s", msg.c_str ()); |
3959
|
384 } |
3243
|
385 } |
|
386 } |
2928
|
387 else |
3243
|
388 print_usage ("lsode"); |
2928
|
389 |
3243
|
390 unwind_protect::run_frame ("Flsode"); |
2928
|
391 |
|
392 return retval; |
|
393 } |
|
394 |
|
395 /* |
|
396 ;;; Local Variables: *** |
|
397 ;;; mode: C++ *** |
|
398 ;;; End: *** |
|
399 */ |