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