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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2928
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <string> |
|
29 |
3567
|
30 #include <iomanip> |
3523
|
31 #include <iostream> |
2928
|
32 |
|
33 #include "LSODE.h" |
|
34 #include "lo-mappers.h" |
|
35 |
|
36 #include "defun-dld.h" |
|
37 #include "error.h" |
|
38 #include "gripes.h" |
|
39 #include "oct-obj.h" |
2968
|
40 #include "ov-fcn.h" |
2928
|
41 #include "pager.h" |
3952
|
42 #include "pr-output.h" |
3243
|
43 #include "unwind-prot.h" |
2928
|
44 #include "utils.h" |
|
45 #include "variables.h" |
|
46 |
3998
|
47 #include "LSODE-opts.cc" |
|
48 |
2928
|
49 // Global pointer for user defined function required by lsode. |
2968
|
50 static octave_function *lsode_fcn; |
2928
|
51 |
|
52 // Global pointer for optional user defined jacobian function used by lsode. |
2968
|
53 static octave_function *lsode_jac; |
2928
|
54 |
4140
|
55 // Have we warned about imaginary values returned from user function? |
|
56 static bool warned_fcn_imaginary = false; |
|
57 static bool warned_jac_imaginary = false; |
|
58 |
3243
|
59 // Is this a recursive call? |
|
60 static int call_depth = 0; |
|
61 |
2928
|
62 ColumnVector |
|
63 lsode_user_function (const ColumnVector& x, double t) |
|
64 { |
|
65 ColumnVector retval; |
|
66 |
|
67 octave_value_list args; |
|
68 args(1) = t; |
4628
|
69 args(0) = x; |
2928
|
70 |
|
71 if (lsode_fcn) |
|
72 { |
3544
|
73 octave_value_list tmp = lsode_fcn->do_multi_index_op (1, args); |
2928
|
74 |
|
75 if (error_state) |
|
76 { |
|
77 gripe_user_supplied_eval ("lsode"); |
|
78 return retval; |
|
79 } |
|
80 |
|
81 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
82 { |
4140
|
83 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
|
84 { |
|
85 warning ("lsode: ignoring imaginary part returned from user-supplied function"); |
|
86 warned_fcn_imaginary = true; |
|
87 } |
|
88 |
3419
|
89 retval = ColumnVector (tmp(0).vector_value ()); |
2928
|
90 |
|
91 if (error_state || retval.length () == 0) |
|
92 gripe_user_supplied_eval ("lsode"); |
|
93 } |
|
94 else |
|
95 gripe_user_supplied_eval ("lsode"); |
|
96 } |
|
97 |
|
98 return retval; |
|
99 } |
|
100 |
|
101 Matrix |
|
102 lsode_user_jacobian (const ColumnVector& x, double t) |
|
103 { |
|
104 Matrix retval; |
|
105 |
|
106 octave_value_list args; |
|
107 args(1) = t; |
4628
|
108 args(0) = x; |
2928
|
109 |
|
110 if (lsode_jac) |
|
111 { |
3544
|
112 octave_value_list tmp = lsode_jac->do_multi_index_op (1, args); |
2928
|
113 |
|
114 if (error_state) |
|
115 { |
|
116 gripe_user_supplied_eval ("lsode"); |
|
117 return retval; |
|
118 } |
|
119 |
|
120 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
121 { |
4140
|
122 if (! warned_jac_imaginary && tmp(0).is_complex_type ()) |
|
123 { |
|
124 warning ("lsode: ignoring imaginary part returned from user-supplied jacobian function"); |
|
125 warned_jac_imaginary = true; |
|
126 } |
|
127 |
2928
|
128 retval = tmp(0).matrix_value (); |
|
129 |
|
130 if (error_state || retval.length () == 0) |
|
131 gripe_user_supplied_eval ("lsode"); |
|
132 } |
|
133 else |
|
134 gripe_user_supplied_eval ("lsode"); |
|
135 } |
|
136 |
|
137 return retval; |
|
138 } |
|
139 |
3323
|
140 #define LSODE_ABORT() \ |
|
141 do \ |
|
142 { \ |
|
143 unwind_protect::run_frame ("Flsode"); \ |
|
144 return retval; \ |
|
145 } \ |
|
146 while (0) |
|
147 |
|
148 #define LSODE_ABORT1(msg) \ |
|
149 do \ |
|
150 { \ |
3747
|
151 ::error ("lsode: " msg); \ |
3323
|
152 LSODE_ABORT (); \ |
|
153 } \ |
|
154 while (0) |
|
155 |
|
156 #define LSODE_ABORT2(fmt, arg) \ |
|
157 do \ |
|
158 { \ |
3747
|
159 ::error ("lsode: " fmt, arg); \ |
3323
|
160 LSODE_ABORT (); \ |
|
161 } \ |
|
162 while (0) |
|
163 |
2928
|
164 DEFUN_DLD (lsode, args, nargout, |
3373
|
165 "-*- texinfo -*-\n\ |
4115
|
166 @deftypefn {Loadable Function} {[@var{x}, @var{istate}, @var{msg}]} lsode (@var{fcn}, @var{x_0}, @var{t}, @var{t_crit})\n\ |
|
167 Solve the set of differential equations\n\ |
|
168 @tex\n\ |
|
169 $$ {dx \\over dt} = f (x, t) $$\n\ |
|
170 with\n\ |
|
171 $$ x(t_0) = x_0 $$\n\ |
|
172 @end tex\n\ |
|
173 @ifinfo\n\ |
|
174 \n\ |
|
175 @example\n\ |
|
176 dx\n\ |
|
177 -- = f(x, t)\n\ |
4117
|
178 dt\n\ |
4115
|
179 @end example\n\ |
|
180 \n\ |
|
181 with\n\ |
|
182 \n\ |
|
183 @example\n\ |
|
184 x(t_0) = x_0\n\ |
|
185 @end example\n\ |
|
186 \n\ |
|
187 @end ifinfo\n\ |
|
188 The solution is returned in the matrix @var{x}, with each row\n\ |
|
189 corresponding to an element of the vector @var{t}. The first element\n\ |
|
190 of @var{t} should be @math{t_0} and should correspond to the initial\n\ |
|
191 state of the system @var{x_0}, so that the first row of the output\n\ |
|
192 is @var{x_0}.\n\ |
3373
|
193 \n\ |
|
194 The first argument, @var{fcn}, is a string that names the function to\n\ |
|
195 call to compute the vector of right hand sides for the set of equations.\n\ |
4115
|
196 The function must have the form\n\ |
2928
|
197 \n\ |
3373
|
198 @example\n\ |
|
199 @var{xdot} = f (@var{x}, @var{t})\n\ |
|
200 @end example\n\ |
2928
|
201 \n\ |
3373
|
202 @noindent\n\ |
4115
|
203 in which @var{xdot} and @var{x} are vectors and @var{t} is a scalar.\n\ |
|
204 \n\ |
|
205 If @var{fcn} is a two-element string array, the first element names the\n\ |
|
206 function @math{f} described above, and the second element names a function\n\ |
|
207 to compute the Jacobian of @math{f}. The Jacobian function must have the\n\ |
|
208 form\n\ |
|
209 \n\ |
|
210 @example\n\ |
|
211 @var{jac} = j (@var{x}, @var{t})\n\ |
|
212 @end example\n\ |
|
213 \n\ |
|
214 in which @var{jac} is the matrix of partial derivatives\n\ |
|
215 @tex\n\ |
|
216 $$ J = {\\partial f_i \\over \\partial x_j} = \\left[\\matrix{\n\ |
|
217 {\\partial f_1 \\over \\partial x_1}\n\ |
|
218 & {\\partial f_1 \\over \\partial x_2}\n\ |
|
219 & \\cdots\n\ |
|
220 & {\\partial f_1 \\over \\partial x_N} \\cr\n\ |
|
221 {\\partial f_2 \\over \\partial x_1}\n\ |
|
222 & {\\partial f_2 \\over \\partial x_2}\n\ |
|
223 & \\cdots\n\ |
|
224 & {\\partial f_2 \\over \\partial x_N} \\cr\n\ |
|
225 \\vdots & \\vdots & \\ddots & \\vdots \\cr\n\ |
|
226 {\\partial f_3 \\over \\partial x_1}\n\ |
|
227 & {\\partial f_3 \\over \\partial x_2}\n\ |
|
228 & \\cdots\n\ |
|
229 & {\\partial f_3 \\over \\partial x_N} \\cr}\\right]$$\n\ |
|
230 @end tex\n\ |
|
231 @ifinfo\n\ |
|
232 \n\ |
|
233 @example\n\ |
|
234 | df_1 df_1 df_1 |\n\ |
|
235 | ---- ---- ... ---- |\n\ |
|
236 | dx_1 dx_2 dx_N |\n\ |
|
237 | |\n\ |
|
238 | df_2 df_2 df_2 |\n\ |
|
239 | ---- ---- ... ---- |\n\ |
|
240 df_i | dx_1 dx_2 dx_N |\n\ |
|
241 jac = ---- = | |\n\ |
|
242 dx_j | . . . . |\n\ |
|
243 | . . . . |\n\ |
|
244 | . . . . |\n\ |
|
245 | |\n\ |
|
246 | df_N df_N df_N |\n\ |
|
247 | ---- ---- ... ---- |\n\ |
|
248 | dx_1 dx_2 dx_N |\n\ |
|
249 @end example\n\ |
|
250 \n\ |
|
251 @end ifinfo\n\ |
|
252 \n\ |
|
253 The second and third arguments specify the intial state of the system,\n\ |
|
254 @math{x_0}, and the initial value of the independent variable @math{t_0}.\n\ |
2928
|
255 \n\ |
3373
|
256 The fourth argument is optional, and may be used to specify a set of\n\ |
|
257 times that the ODE solver should not integrate past. It is useful for\n\ |
|
258 avoiding difficulties with singularities and points where there is a\n\ |
|
259 discontinuity in the derivative.\n\ |
3964
|
260 \n\ |
4115
|
261 After a successful computation, the value of @var{istate} will be 2\n\ |
|
262 (consistent with the Fortran version of @sc{Lsode}).\n\ |
|
263 \n\ |
|
264 If the computation is not successful, @var{istate} will be something\n\ |
|
265 other than 2 and @var{msg} will contain additional information.\n\ |
|
266 \n\ |
3964
|
267 You can use the function @code{lsode_options} to set optional\n\ |
|
268 parameters for @code{lsode}.\n\ |
5694
|
269 @seealso{daspk, dassl, dasrt}\n\ |
5646
|
270 @end deftypefn") |
2928
|
271 { |
|
272 octave_value_list retval; |
|
273 |
4140
|
274 warned_fcn_imaginary = false; |
|
275 warned_jac_imaginary = false; |
|
276 |
3243
|
277 unwind_protect::begin_frame ("Flsode"); |
2928
|
278 |
3243
|
279 unwind_protect_int (call_depth); |
|
280 call_depth++; |
|
281 |
|
282 if (call_depth > 1) |
3323
|
283 LSODE_ABORT1 ("invalid recursive call"); |
2928
|
284 |
3243
|
285 int nargin = args.length (); |
2928
|
286 |
3959
|
287 if (nargin > 2 && nargin < 5 && nargout < 4) |
2928
|
288 { |
3991
|
289 lsode_fcn = 0; |
|
290 lsode_jac = 0; |
|
291 |
3243
|
292 octave_value f_arg = args(0); |
2928
|
293 |
3243
|
294 switch (f_arg.rows ()) |
|
295 { |
|
296 case 1: |
|
297 lsode_fcn = extract_function |
3991
|
298 (f_arg, "lsode", "__lsode_fcn__", |
3243
|
299 "function xdot = __lsode_fcn__ (x, t) xdot = ", |
|
300 "; endfunction"); |
|
301 break; |
2928
|
302 |
3243
|
303 case 2: |
2928
|
304 { |
3991
|
305 string_vector tmp = f_arg.all_strings (); |
2928
|
306 |
3243
|
307 if (! error_state) |
2928
|
308 { |
3243
|
309 lsode_fcn = extract_function |
|
310 (tmp(0), "lsode", "__lsode_fcn__", |
|
311 "function xdot = __lsode_fcn__ (x, t) xdot = ", |
2928
|
312 "; endfunction"); |
|
313 |
3243
|
314 if (lsode_fcn) |
|
315 { |
|
316 lsode_jac = extract_function |
|
317 (tmp(1), "lsode", "__lsode_jac__", |
|
318 "function jac = __lsode_jac__ (x, t) jac = ", |
|
319 "; endfunction"); |
|
320 |
|
321 if (! lsode_jac) |
|
322 lsode_fcn = 0; |
|
323 } |
2928
|
324 } |
|
325 } |
3243
|
326 break; |
2928
|
327 |
3243
|
328 default: |
3323
|
329 LSODE_ABORT1 |
|
330 ("first arg should be a string or 2-element string array"); |
3243
|
331 } |
2928
|
332 |
3243
|
333 if (error_state || ! lsode_fcn) |
3323
|
334 LSODE_ABORT (); |
2928
|
335 |
3419
|
336 ColumnVector state (args(1).vector_value ()); |
2928
|
337 |
|
338 if (error_state) |
3323
|
339 LSODE_ABORT1 ("expecting state vector as second argument"); |
3243
|
340 |
3419
|
341 ColumnVector out_times (args(2).vector_value ()); |
3243
|
342 |
|
343 if (error_state) |
3323
|
344 LSODE_ABORT1 ("expecting output time vector as third argument"); |
2928
|
345 |
3243
|
346 ColumnVector crit_times; |
2928
|
347 |
3243
|
348 int crit_times_set = 0; |
|
349 if (nargin > 3) |
|
350 { |
3419
|
351 crit_times = ColumnVector (args(3).vector_value ()); |
2928
|
352 |
3243
|
353 if (error_state) |
3323
|
354 LSODE_ABORT1 ("expecting critical time vector as fourth argument"); |
2928
|
355 |
3243
|
356 crit_times_set = 1; |
|
357 } |
2928
|
358 |
3243
|
359 double tzero = out_times (0); |
|
360 |
|
361 ODEFunc func (lsode_user_function); |
|
362 if (lsode_jac) |
|
363 func.set_jacobian_function (lsode_user_jacobian); |
2928
|
364 |
3243
|
365 LSODE ode (state, tzero, func); |
|
366 |
4122
|
367 ode.set_options (lsode_opts); |
3243
|
368 |
3859
|
369 Matrix output; |
3243
|
370 if (crit_times_set) |
|
371 output = ode.integrate (out_times, crit_times); |
|
372 else |
|
373 output = ode.integrate (out_times); |
|
374 |
|
375 if (! error_state) |
|
376 { |
3997
|
377 std::string msg = ode.error_message (); |
|
378 |
|
379 retval(2) = msg; |
3959
|
380 retval(1) = static_cast<double> (ode.integration_state ()); |
|
381 |
|
382 if (ode.integration_ok ()) |
3971
|
383 retval(0) = output; |
3959
|
384 else |
|
385 { |
3971
|
386 retval(0) = Matrix (); |
3959
|
387 |
|
388 if (nargout < 2) |
3997
|
389 error ("lsode: %s", msg.c_str ()); |
3959
|
390 } |
3243
|
391 } |
|
392 } |
2928
|
393 else |
3243
|
394 print_usage ("lsode"); |
2928
|
395 |
3243
|
396 unwind_protect::run_frame ("Flsode"); |
2928
|
397 |
|
398 return retval; |
|
399 } |
|
400 |
|
401 /* |
|
402 ;;; Local Variables: *** |
|
403 ;;; mode: C++ *** |
|
404 ;;; End: *** |
|
405 */ |