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 |
3523
|
29 #include <iostream> |
2928
|
30 |
|
31 #include "LSODE.h" |
|
32 #include "lo-mappers.h" |
|
33 |
|
34 #include "defun-dld.h" |
|
35 #include "error.h" |
|
36 #include "gripes.h" |
|
37 #include "oct-obj.h" |
2968
|
38 #include "ov-fcn.h" |
2928
|
39 #include "pager.h" |
3243
|
40 #include "unwind-prot.h" |
2928
|
41 #include "utils.h" |
|
42 #include "variables.h" |
|
43 |
|
44 // Global pointer for user defined function required by lsode. |
2968
|
45 static octave_function *lsode_fcn; |
2928
|
46 |
|
47 // Global pointer for optional user defined jacobian function used by lsode. |
2968
|
48 static octave_function *lsode_jac; |
2928
|
49 |
|
50 static LSODE_options lsode_opts; |
|
51 |
3243
|
52 // Is this a recursive call? |
|
53 static int call_depth = 0; |
|
54 |
2928
|
55 ColumnVector |
|
56 lsode_user_function (const ColumnVector& x, double t) |
|
57 { |
|
58 ColumnVector retval; |
|
59 |
|
60 int nstates = x.capacity (); |
|
61 |
|
62 octave_value_list args; |
|
63 args(1) = t; |
|
64 |
|
65 Matrix m (nstates, 1); |
|
66 for (int i = 0; i < nstates; i++) |
|
67 m (i, 0) = x (i); |
|
68 octave_value state (m); |
|
69 args(0) = state; |
|
70 |
|
71 if (lsode_fcn) |
|
72 { |
2968
|
73 octave_value_list tmp = lsode_fcn->do_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 { |
3419
|
83 retval = ColumnVector (tmp(0).vector_value ()); |
2928
|
84 |
|
85 if (error_state || retval.length () == 0) |
|
86 gripe_user_supplied_eval ("lsode"); |
|
87 } |
|
88 else |
|
89 gripe_user_supplied_eval ("lsode"); |
|
90 } |
|
91 |
|
92 return retval; |
|
93 } |
|
94 |
|
95 Matrix |
|
96 lsode_user_jacobian (const ColumnVector& x, double t) |
|
97 { |
|
98 Matrix retval; |
|
99 |
|
100 int nstates = x.capacity (); |
|
101 |
|
102 octave_value_list args; |
|
103 args(1) = t; |
|
104 |
|
105 Matrix m (nstates, 1); |
|
106 for (int i = 0; i < nstates; i++) |
|
107 m (i, 0) = x (i); |
|
108 octave_value state (m); |
|
109 args(0) = state; |
|
110 |
|
111 if (lsode_jac) |
|
112 { |
2968
|
113 octave_value_list tmp = lsode_jac->do_index_op (1, args); |
2928
|
114 |
|
115 if (error_state) |
|
116 { |
|
117 gripe_user_supplied_eval ("lsode"); |
|
118 return retval; |
|
119 } |
|
120 |
|
121 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
122 { |
|
123 retval = tmp(0).matrix_value (); |
|
124 |
|
125 if (error_state || retval.length () == 0) |
|
126 gripe_user_supplied_eval ("lsode"); |
|
127 } |
|
128 else |
|
129 gripe_user_supplied_eval ("lsode"); |
|
130 } |
|
131 |
|
132 return retval; |
|
133 } |
|
134 |
3323
|
135 #define LSODE_ABORT() \ |
|
136 do \ |
|
137 { \ |
|
138 unwind_protect::run_frame ("Flsode"); \ |
|
139 return retval; \ |
|
140 } \ |
|
141 while (0) |
|
142 |
|
143 #define LSODE_ABORT1(msg) \ |
|
144 do \ |
|
145 { \ |
|
146 ::error ("lsode: " ## msg); \ |
|
147 LSODE_ABORT (); \ |
|
148 } \ |
|
149 while (0) |
|
150 |
|
151 #define LSODE_ABORT2(fmt, arg) \ |
|
152 do \ |
|
153 { \ |
|
154 ::error ("lsode: " ## fmt, arg); \ |
|
155 LSODE_ABORT (); \ |
|
156 } \ |
|
157 while (0) |
|
158 |
2928
|
159 DEFUN_DLD (lsode, args, nargout, |
3373
|
160 "-*- texinfo -*-\n\ |
|
161 @deftypefn {Loadable Function} {} lsode (@var{fcn}, @var{x0}, @var{t}, @var{t_crit})\n\ |
|
162 Return a matrix of @var{x} as a function of @var{t}, given the initial\n\ |
|
163 state of the system @var{x0}. Each row in the result matrix corresponds\n\ |
|
164 to one of the elements in the vector @var{t}. The first element of\n\ |
|
165 @var{t} corresponds to the initial state @var{x0}, so that the first row\n\ |
|
166 of the output is @var{x0}.\n\ |
|
167 \n\ |
|
168 The first argument, @var{fcn}, is a string that names the function to\n\ |
|
169 call to compute the vector of right hand sides for the set of equations.\n\ |
|
170 It must have the form\n\ |
2928
|
171 \n\ |
3373
|
172 @example\n\ |
|
173 @var{xdot} = f (@var{x}, @var{t})\n\ |
|
174 @end example\n\ |
2928
|
175 \n\ |
3373
|
176 @noindent\n\ |
|
177 where @var{xdot} and @var{x} are vectors and @var{t} is a scalar.\n\ |
2928
|
178 \n\ |
3373
|
179 The fourth argument is optional, and may be used to specify a set of\n\ |
|
180 times that the ODE solver should not integrate past. It is useful for\n\ |
|
181 avoiding difficulties with singularities and points where there is a\n\ |
|
182 discontinuity in the derivative.\n\ |
|
183 @end deftypefn") |
2928
|
184 { |
|
185 octave_value_list retval; |
|
186 |
3243
|
187 unwind_protect::begin_frame ("Flsode"); |
2928
|
188 |
3243
|
189 unwind_protect_int (call_depth); |
|
190 call_depth++; |
|
191 |
|
192 if (call_depth > 1) |
3323
|
193 LSODE_ABORT1 ("invalid recursive call"); |
2928
|
194 |
3243
|
195 int nargin = args.length (); |
2928
|
196 |
3243
|
197 if (nargin > 2 && nargin < 5 && nargout < 2) |
2928
|
198 { |
3243
|
199 octave_value f_arg = args(0); |
2928
|
200 |
3243
|
201 switch (f_arg.rows ()) |
|
202 { |
|
203 case 1: |
|
204 lsode_fcn = extract_function |
|
205 (args(0), "lsode", "__lsode_fcn__", |
|
206 "function xdot = __lsode_fcn__ (x, t) xdot = ", |
|
207 "; endfunction"); |
|
208 break; |
2928
|
209 |
3243
|
210 case 2: |
2928
|
211 { |
3243
|
212 string_vector tmp = args(0).all_strings (); |
2928
|
213 |
3243
|
214 if (! error_state) |
2928
|
215 { |
3243
|
216 lsode_fcn = extract_function |
|
217 (tmp(0), "lsode", "__lsode_fcn__", |
|
218 "function xdot = __lsode_fcn__ (x, t) xdot = ", |
2928
|
219 "; endfunction"); |
|
220 |
3243
|
221 if (lsode_fcn) |
|
222 { |
|
223 lsode_jac = extract_function |
|
224 (tmp(1), "lsode", "__lsode_jac__", |
|
225 "function jac = __lsode_jac__ (x, t) jac = ", |
|
226 "; endfunction"); |
|
227 |
|
228 if (! lsode_jac) |
|
229 lsode_fcn = 0; |
|
230 } |
2928
|
231 } |
|
232 } |
3243
|
233 break; |
2928
|
234 |
3243
|
235 default: |
3323
|
236 LSODE_ABORT1 |
|
237 ("first arg should be a string or 2-element string array"); |
3243
|
238 } |
2928
|
239 |
3243
|
240 if (error_state || ! lsode_fcn) |
3323
|
241 LSODE_ABORT (); |
2928
|
242 |
3419
|
243 ColumnVector state (args(1).vector_value ()); |
2928
|
244 |
|
245 if (error_state) |
3323
|
246 LSODE_ABORT1 ("expecting state vector as second argument"); |
3243
|
247 |
3419
|
248 ColumnVector out_times (args(2).vector_value ()); |
3243
|
249 |
|
250 if (error_state) |
3323
|
251 LSODE_ABORT1 ("expecting output time vector as third argument"); |
2928
|
252 |
3243
|
253 ColumnVector crit_times; |
2928
|
254 |
3243
|
255 int crit_times_set = 0; |
|
256 if (nargin > 3) |
|
257 { |
3419
|
258 crit_times = ColumnVector (args(3).vector_value ()); |
2928
|
259 |
3243
|
260 if (error_state) |
3323
|
261 LSODE_ABORT1 ("expecting critical time vector as fourth argument"); |
2928
|
262 |
3243
|
263 crit_times_set = 1; |
|
264 } |
2928
|
265 |
3243
|
266 double tzero = out_times (0); |
|
267 int nsteps = out_times.capacity (); |
|
268 |
|
269 ODEFunc func (lsode_user_function); |
|
270 if (lsode_jac) |
|
271 func.set_jacobian_function (lsode_user_jacobian); |
2928
|
272 |
3243
|
273 LSODE ode (state, tzero, func); |
|
274 |
|
275 ode.copy (lsode_opts); |
|
276 |
|
277 int nstates = state.capacity (); |
|
278 Matrix output (nsteps, nstates + 1); |
2928
|
279 |
3243
|
280 if (crit_times_set) |
|
281 output = ode.integrate (out_times, crit_times); |
|
282 else |
|
283 output = ode.integrate (out_times); |
|
284 |
|
285 if (! error_state) |
|
286 { |
|
287 retval.resize (1); |
|
288 retval(0) = output; |
|
289 } |
|
290 } |
2928
|
291 else |
3243
|
292 print_usage ("lsode"); |
2928
|
293 |
3243
|
294 unwind_protect::run_frame ("Flsode"); |
2928
|
295 |
|
296 return retval; |
|
297 } |
|
298 |
|
299 typedef void (LSODE_options::*d_set_opt_mf) (double); |
|
300 typedef void (LSODE_options::*i_set_opt_mf) (int); |
|
301 typedef double (LSODE_options::*d_get_opt_mf) (void); |
|
302 typedef int (LSODE_options::*i_get_opt_mf) (void); |
|
303 |
|
304 #define MAX_TOKENS 3 |
|
305 |
|
306 struct LSODE_OPTIONS |
|
307 { |
|
308 const char *keyword; |
|
309 const char *kw_tok[MAX_TOKENS + 1]; |
|
310 int min_len[MAX_TOKENS + 1]; |
|
311 int min_toks_to_match; |
|
312 d_set_opt_mf d_set_fcn; |
|
313 i_set_opt_mf i_set_fcn; |
|
314 d_get_opt_mf d_get_fcn; |
|
315 i_get_opt_mf i_get_fcn; |
|
316 }; |
|
317 |
|
318 static LSODE_OPTIONS lsode_option_table [] = |
|
319 { |
|
320 { "absolute tolerance", |
|
321 { "absolute", "tolerance", 0, 0, }, |
|
322 { 1, 0, 0, 0, }, 1, |
3131
|
323 &LSODE_options::set_absolute_tolerance, 0, |
|
324 &LSODE_options::absolute_tolerance, 0, }, |
2928
|
325 |
|
326 { "initial step size", |
|
327 { "initial", "step", "size", 0, }, |
|
328 { 1, 0, 0, 0, }, 1, |
3131
|
329 &LSODE_options::set_initial_step_size, 0, |
|
330 &LSODE_options::initial_step_size, 0, }, |
2928
|
331 |
|
332 { "maximum step size", |
|
333 { "maximum", "step", "size", 0, }, |
|
334 { 2, 0, 0, 0, }, 1, |
3131
|
335 &LSODE_options::set_maximum_step_size, 0, |
|
336 &LSODE_options::maximum_step_size, 0, }, |
2928
|
337 |
|
338 { "minimum step size", |
|
339 { "minimum", "step", "size", 0, }, |
|
340 { 2, 0, 0, 0, }, 1, |
3131
|
341 &LSODE_options::set_minimum_step_size, 0, |
|
342 &LSODE_options::minimum_step_size, 0, }, |
2928
|
343 |
|
344 { "relative tolerance", |
|
345 { "relative", "tolerance", 0, 0, }, |
|
346 { 1, 0, 0, 0, }, 1, |
3131
|
347 &LSODE_options::set_relative_tolerance, 0, |
|
348 &LSODE_options::relative_tolerance, 0, }, |
2928
|
349 |
|
350 { "step limit", |
|
351 { "step", "limit", 0, 0, }, |
|
352 { 1, 0, 0, 0, }, 1, |
3131
|
353 0, &LSODE_options::set_step_limit, |
|
354 0, &LSODE_options::step_limit, }, |
2928
|
355 |
|
356 { 0, |
|
357 { 0, 0, 0, 0, }, |
|
358 { 0, 0, 0, 0, }, 0, |
|
359 0, 0, 0, 0, }, |
|
360 }; |
|
361 |
|
362 static void |
3523
|
363 print_lsode_option_list (std::ostream& os) |
2928
|
364 { |
|
365 print_usage ("lsode_options", 1); |
|
366 |
|
367 os << "\n" |
|
368 << "Options for lsode include:\n\n" |
|
369 << " keyword value\n" |
|
370 << " ------- -----\n\n"; |
|
371 |
|
372 LSODE_OPTIONS *list = lsode_option_table; |
|
373 |
|
374 const char *keyword; |
|
375 while ((keyword = list->keyword) != 0) |
|
376 { |
|
377 os.form (" %-40s ", keyword); |
|
378 if (list->d_get_fcn) |
|
379 { |
|
380 double val = (lsode_opts.*list->d_get_fcn) (); |
|
381 if (val < 0.0) |
|
382 os << "computed automatically"; |
|
383 else |
|
384 os << val; |
|
385 } |
|
386 else |
|
387 { |
|
388 int val = (lsode_opts.*list->i_get_fcn) (); |
|
389 if (val < 0) |
|
390 os << "infinite"; |
|
391 else |
|
392 os << val; |
|
393 } |
|
394 os << "\n"; |
|
395 list++; |
|
396 } |
|
397 |
|
398 os << "\n"; |
|
399 } |
|
400 |
|
401 static void |
3523
|
402 set_lsode_option (const std::string& keyword, double val) |
2928
|
403 { |
|
404 LSODE_OPTIONS *list = lsode_option_table; |
|
405 |
|
406 while (list->keyword != 0) |
|
407 { |
|
408 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
409 list->min_toks_to_match, MAX_TOKENS)) |
|
410 { |
|
411 if (list->d_set_fcn) |
|
412 (lsode_opts.*list->d_set_fcn) (val); |
|
413 else |
|
414 { |
|
415 if (xisnan (val)) |
|
416 { |
|
417 error ("lsode_options: %s: expecting integer, found NaN", |
|
418 keyword.c_str ()); |
|
419 } |
|
420 else |
|
421 (lsode_opts.*list->i_set_fcn) (NINT (val)); |
|
422 } |
|
423 return; |
|
424 } |
|
425 list++; |
|
426 } |
|
427 |
|
428 warning ("lsode_options: no match for `%s'", keyword.c_str ()); |
|
429 } |
|
430 |
|
431 static octave_value_list |
3523
|
432 show_lsode_option (const std::string& keyword) |
2928
|
433 { |
|
434 octave_value retval; |
|
435 |
|
436 LSODE_OPTIONS *list = lsode_option_table; |
|
437 |
|
438 while (list->keyword != 0) |
|
439 { |
|
440 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
441 list->min_toks_to_match, MAX_TOKENS)) |
|
442 { |
|
443 if (list->d_get_fcn) |
|
444 { |
|
445 double val = (lsode_opts.*list->d_get_fcn) (); |
|
446 if (val < 0.0) |
|
447 retval = "computed automatically"; |
|
448 else |
|
449 retval = val; |
|
450 } |
|
451 else |
|
452 { |
|
453 int val = (lsode_opts.*list->i_get_fcn) (); |
|
454 if (val < 0) |
|
455 retval = "infinite"; |
|
456 else |
|
457 retval = static_cast<double> (val); |
|
458 } |
|
459 |
|
460 return retval; |
|
461 } |
|
462 list++; |
|
463 } |
|
464 |
|
465 warning ("lsode_options: no match for `%s'", keyword.c_str ()); |
|
466 |
|
467 return retval; |
|
468 } |
|
469 |
|
470 DEFUN_DLD (lsode_options, args, , |
3373
|
471 "-*- texinfo -*-\n\ |
|
472 @deftypefn {Loadable Function} {} lsode_options (@var{opt}, @var{val})\n\ |
|
473 When called with two arguments, this function allows you set options\n\ |
|
474 parameters for the function @code{lsode}. Given one argument,\n\ |
|
475 @code{lsode_options} returns the value of the corresponding option. If\n\ |
|
476 no arguments are supplied, the names of all the available options and\n\ |
|
477 their current values are displayed.\n\ |
|
478 @end deftypefn") |
2928
|
479 { |
|
480 octave_value_list retval; |
|
481 |
|
482 int nargin = args.length (); |
|
483 |
|
484 if (nargin == 0) |
|
485 { |
|
486 print_lsode_option_list (octave_stdout); |
|
487 return retval; |
|
488 } |
|
489 else if (nargin == 1 || nargin == 2) |
|
490 { |
3523
|
491 std::string keyword = args(0).string_value (); |
2928
|
492 |
|
493 if (! error_state) |
|
494 { |
|
495 if (nargin == 1) |
|
496 return show_lsode_option (keyword); |
|
497 else |
|
498 { |
|
499 double val = args(1).double_value (); |
|
500 |
|
501 if (! error_state) |
|
502 { |
|
503 set_lsode_option (keyword, val); |
|
504 return retval; |
|
505 } |
|
506 } |
|
507 } |
|
508 } |
|
509 |
|
510 print_usage ("lsode_options"); |
|
511 |
|
512 return retval; |
|
513 } |
|
514 |
|
515 /* |
|
516 ;;; Local Variables: *** |
|
517 ;;; mode: C++ *** |
|
518 ;;; End: *** |
|
519 */ |