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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
2928
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
2928
|
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" |
5729
|
40 #include "ov-cell.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\ |
6755
|
166 @deftypefn {Loadable Function} {[@var{x}, @var{istate}, @var{msg}] =} lsode (@var{fcn}, @var{x_0}, @var{t}, @var{t_crit})\n\ |
4115
|
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\ |
5729
|
194 The first argument, @var{fcn}, is a string, or cell array of strings,\n\ |
|
195 inline or function handles, that names the function to call to compute\n\ |
|
196 the vector of right hand sides for the set of equations. The function\n\ |
|
197 must have the form\n\ |
2928
|
198 \n\ |
3373
|
199 @example\n\ |
|
200 @var{xdot} = f (@var{x}, @var{t})\n\ |
|
201 @end example\n\ |
2928
|
202 \n\ |
3373
|
203 @noindent\n\ |
4115
|
204 in which @var{xdot} and @var{x} are vectors and @var{t} is a scalar.\n\ |
|
205 \n\ |
|
206 If @var{fcn} is a two-element string array, the first element names the\n\ |
|
207 function @math{f} described above, and the second element names a function\n\ |
|
208 to compute the Jacobian of @math{f}. The Jacobian function must have the\n\ |
|
209 form\n\ |
|
210 \n\ |
|
211 @example\n\ |
|
212 @var{jac} = j (@var{x}, @var{t})\n\ |
|
213 @end example\n\ |
|
214 \n\ |
|
215 in which @var{jac} is the matrix of partial derivatives\n\ |
|
216 @tex\n\ |
|
217 $$ J = {\\partial f_i \\over \\partial x_j} = \\left[\\matrix{\n\ |
|
218 {\\partial f_1 \\over \\partial x_1}\n\ |
|
219 & {\\partial f_1 \\over \\partial x_2}\n\ |
|
220 & \\cdots\n\ |
|
221 & {\\partial f_1 \\over \\partial x_N} \\cr\n\ |
|
222 {\\partial f_2 \\over \\partial x_1}\n\ |
|
223 & {\\partial f_2 \\over \\partial x_2}\n\ |
|
224 & \\cdots\n\ |
|
225 & {\\partial f_2 \\over \\partial x_N} \\cr\n\ |
|
226 \\vdots & \\vdots & \\ddots & \\vdots \\cr\n\ |
|
227 {\\partial f_3 \\over \\partial x_1}\n\ |
|
228 & {\\partial f_3 \\over \\partial x_2}\n\ |
|
229 & \\cdots\n\ |
|
230 & {\\partial f_3 \\over \\partial x_N} \\cr}\\right]$$\n\ |
|
231 @end tex\n\ |
|
232 @ifinfo\n\ |
|
233 \n\ |
|
234 @example\n\ |
|
235 | df_1 df_1 df_1 |\n\ |
|
236 | ---- ---- ... ---- |\n\ |
|
237 | dx_1 dx_2 dx_N |\n\ |
|
238 | |\n\ |
|
239 | df_2 df_2 df_2 |\n\ |
|
240 | ---- ---- ... ---- |\n\ |
|
241 df_i | dx_1 dx_2 dx_N |\n\ |
|
242 jac = ---- = | |\n\ |
|
243 dx_j | . . . . |\n\ |
|
244 | . . . . |\n\ |
|
245 | . . . . |\n\ |
|
246 | |\n\ |
|
247 | df_N df_N df_N |\n\ |
|
248 | ---- ---- ... ---- |\n\ |
|
249 | dx_1 dx_2 dx_N |\n\ |
|
250 @end example\n\ |
|
251 \n\ |
|
252 @end ifinfo\n\ |
|
253 \n\ |
7001
|
254 The second and third arguments specify the initial state of the system,\n\ |
4115
|
255 @math{x_0}, and the initial value of the independent variable @math{t_0}.\n\ |
2928
|
256 \n\ |
3373
|
257 The fourth argument is optional, and may be used to specify a set of\n\ |
|
258 times that the ODE 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 2\n\ |
|
263 (consistent with the Fortran version of @sc{Lsode}).\n\ |
|
264 \n\ |
|
265 If the computation is not successful, @var{istate} will be something\n\ |
|
266 other than 2 and @var{msg} will contain additional information.\n\ |
|
267 \n\ |
3964
|
268 You can use the function @code{lsode_options} to set optional\n\ |
|
269 parameters for @code{lsode}.\n\ |
5694
|
270 @seealso{daspk, dassl, dasrt}\n\ |
5646
|
271 @end deftypefn") |
2928
|
272 { |
|
273 octave_value_list retval; |
|
274 |
4140
|
275 warned_fcn_imaginary = false; |
|
276 warned_jac_imaginary = false; |
|
277 |
3243
|
278 unwind_protect::begin_frame ("Flsode"); |
2928
|
279 |
3243
|
280 unwind_protect_int (call_depth); |
|
281 call_depth++; |
|
282 |
|
283 if (call_depth > 1) |
3323
|
284 LSODE_ABORT1 ("invalid recursive call"); |
2928
|
285 |
3243
|
286 int nargin = args.length (); |
2928
|
287 |
3959
|
288 if (nargin > 2 && nargin < 5 && nargout < 4) |
2928
|
289 { |
5729
|
290 std::string fcn_name, fname, jac_name, jname; |
3991
|
291 lsode_fcn = 0; |
|
292 lsode_jac = 0; |
|
293 |
3243
|
294 octave_value f_arg = args(0); |
2928
|
295 |
5729
|
296 if (f_arg.is_cell ()) |
|
297 { |
|
298 Cell c = f_arg.cell_value (); |
|
299 if (c.length() == 1) |
|
300 f_arg = c(0); |
|
301 else if (c.length() == 2) |
|
302 { |
|
303 if (c(0).is_function_handle () || c(0).is_inline_function ()) |
|
304 lsode_fcn = c(0).function_value (); |
|
305 else |
|
306 { |
|
307 fcn_name = unique_symbol_name ("__lsode_fcn__"); |
|
308 fname = "function y = "; |
|
309 fname.append (fcn_name); |
|
310 fname.append (" (x, t) y = "); |
|
311 lsode_fcn = extract_function |
|
312 (c(0), "lsode", fcn_name, fname, "; endfunction"); |
|
313 } |
|
314 |
|
315 if (lsode_fcn) |
|
316 { |
|
317 if (c(1).is_function_handle () || c(1).is_inline_function ()) |
|
318 lsode_jac = c(1).function_value (); |
|
319 else |
|
320 { |
|
321 jac_name = unique_symbol_name ("__lsode_jac__"); |
|
322 jname = "function jac = "; |
|
323 jname.append(jac_name); |
|
324 jname.append (" (x, t) jac = "); |
|
325 lsode_jac = extract_function |
|
326 (c(1), "lsode", jac_name, jname, "; endfunction"); |
2928
|
327 |
5729
|
328 if (!lsode_jac) |
|
329 { |
|
330 if (fcn_name.length()) |
|
331 clear_function (fcn_name); |
|
332 lsode_fcn = 0; |
|
333 } |
|
334 } |
|
335 } |
|
336 } |
|
337 else |
|
338 LSODE_ABORT1 ("incorrect number of elements in cell array"); |
|
339 } |
2928
|
340 |
5729
|
341 if (!lsode_fcn && ! f_arg.is_cell()) |
|
342 { |
|
343 if (f_arg.is_function_handle () || f_arg.is_inline_function ()) |
|
344 lsode_fcn = f_arg.function_value (); |
|
345 else |
|
346 { |
|
347 switch (f_arg.rows ()) |
|
348 { |
|
349 case 1: |
|
350 do |
|
351 { |
|
352 fcn_name = unique_symbol_name ("__lsode_fcn__"); |
|
353 fname = "function y = "; |
|
354 fname.append (fcn_name); |
|
355 fname.append (" (x, t) y = "); |
|
356 lsode_fcn = extract_function |
|
357 (f_arg, "lsode", fcn_name, fname, "; endfunction"); |
|
358 } |
|
359 while (0); |
|
360 break; |
2928
|
361 |
5729
|
362 case 2: |
3243
|
363 { |
5729
|
364 string_vector tmp = f_arg.all_strings (); |
|
365 |
|
366 if (! error_state) |
|
367 { |
|
368 fcn_name = unique_symbol_name ("__lsode_fcn__"); |
|
369 fname = "function y = "; |
|
370 fname.append (fcn_name); |
|
371 fname.append (" (x, t) y = "); |
|
372 lsode_fcn = extract_function |
|
373 (tmp(0), "lsode", fcn_name, fname, "; endfunction"); |
3243
|
374 |
5729
|
375 if (lsode_fcn) |
|
376 { |
|
377 jac_name = unique_symbol_name ("__lsode_jac__"); |
|
378 jname = "function jac = "; |
|
379 jname.append(jac_name); |
|
380 jname.append (" (x, t) jac = "); |
|
381 lsode_jac = extract_function |
|
382 (tmp(1), "lsode", jac_name, jname, |
|
383 "; endfunction"); |
|
384 |
|
385 if (!lsode_jac) |
|
386 { |
|
387 if (fcn_name.length()) |
|
388 clear_function (fcn_name); |
|
389 lsode_fcn = 0; |
|
390 } |
|
391 } |
|
392 } |
3243
|
393 } |
5729
|
394 break; |
2928
|
395 |
5729
|
396 default: |
|
397 LSODE_ABORT1 |
|
398 ("first arg should be a string or 2-element string array"); |
|
399 } |
|
400 } |
3243
|
401 } |
2928
|
402 |
3243
|
403 if (error_state || ! lsode_fcn) |
3323
|
404 LSODE_ABORT (); |
2928
|
405 |
3419
|
406 ColumnVector state (args(1).vector_value ()); |
2928
|
407 |
|
408 if (error_state) |
3323
|
409 LSODE_ABORT1 ("expecting state vector as second argument"); |
3243
|
410 |
3419
|
411 ColumnVector out_times (args(2).vector_value ()); |
3243
|
412 |
|
413 if (error_state) |
3323
|
414 LSODE_ABORT1 ("expecting output time vector as third argument"); |
2928
|
415 |
3243
|
416 ColumnVector crit_times; |
2928
|
417 |
3243
|
418 int crit_times_set = 0; |
|
419 if (nargin > 3) |
|
420 { |
3419
|
421 crit_times = ColumnVector (args(3).vector_value ()); |
2928
|
422 |
3243
|
423 if (error_state) |
3323
|
424 LSODE_ABORT1 ("expecting critical time vector as fourth argument"); |
2928
|
425 |
3243
|
426 crit_times_set = 1; |
|
427 } |
2928
|
428 |
3243
|
429 double tzero = out_times (0); |
|
430 |
|
431 ODEFunc func (lsode_user_function); |
|
432 if (lsode_jac) |
|
433 func.set_jacobian_function (lsode_user_jacobian); |
2928
|
434 |
3243
|
435 LSODE ode (state, tzero, func); |
|
436 |
4122
|
437 ode.set_options (lsode_opts); |
3243
|
438 |
3859
|
439 Matrix output; |
3243
|
440 if (crit_times_set) |
|
441 output = ode.integrate (out_times, crit_times); |
|
442 else |
|
443 output = ode.integrate (out_times); |
|
444 |
5729
|
445 if (fcn_name.length()) |
|
446 clear_function (fcn_name); |
|
447 if (jac_name.length()) |
|
448 clear_function (jac_name); |
|
449 |
3243
|
450 if (! error_state) |
|
451 { |
3997
|
452 std::string msg = ode.error_message (); |
|
453 |
|
454 retval(2) = msg; |
3959
|
455 retval(1) = static_cast<double> (ode.integration_state ()); |
|
456 |
|
457 if (ode.integration_ok ()) |
3971
|
458 retval(0) = output; |
3959
|
459 else |
|
460 { |
3971
|
461 retval(0) = Matrix (); |
3959
|
462 |
|
463 if (nargout < 2) |
3997
|
464 error ("lsode: %s", msg.c_str ()); |
3959
|
465 } |
3243
|
466 } |
|
467 } |
2928
|
468 else |
5823
|
469 print_usage (); |
2928
|
470 |
3243
|
471 unwind_protect::run_frame ("Flsode"); |
2928
|
472 |
|
473 return retval; |
|
474 } |
|
475 |
|
476 /* |
|
477 ;;; Local Variables: *** |
|
478 ;;; mode: C++ *** |
|
479 ;;; End: *** |
|
480 */ |