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 |
|
29 #include <iostream.h> |
|
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 { |
|
83 retval = tmp(0).vector_value (); |
|
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 |
|
135 DEFUN_DLD (lsode, args, nargout, |
|
136 "lsode (F, X0, T_OUT, T_CRIT)\n\ |
|
137 \n\ |
|
138 The first argument is the name of the function to call to\n\ |
|
139 compute the vector of right hand sides. It must have the form\n\ |
|
140 \n\ |
|
141 xdot = f (x, t)\n\ |
|
142 \n\ |
|
143 where xdot and x are vectors and t is a scalar.\n") |
|
144 { |
|
145 octave_value_list retval; |
|
146 |
3243
|
147 unwind_protect::begin_frame ("Flsode"); |
2928
|
148 |
3243
|
149 unwind_protect_int (call_depth); |
|
150 call_depth++; |
|
151 |
|
152 if (call_depth > 1) |
2928
|
153 { |
3243
|
154 error ("lsode: invalid recursive call"); |
2928
|
155 return retval; |
|
156 } |
|
157 |
3243
|
158 int nargin = args.length (); |
2928
|
159 |
3243
|
160 if (nargin > 2 && nargin < 5 && nargout < 2) |
2928
|
161 { |
3243
|
162 octave_value f_arg = args(0); |
2928
|
163 |
3243
|
164 switch (f_arg.rows ()) |
|
165 { |
|
166 case 1: |
|
167 lsode_fcn = extract_function |
|
168 (args(0), "lsode", "__lsode_fcn__", |
|
169 "function xdot = __lsode_fcn__ (x, t) xdot = ", |
|
170 "; endfunction"); |
|
171 break; |
2928
|
172 |
3243
|
173 case 2: |
2928
|
174 { |
3243
|
175 string_vector tmp = args(0).all_strings (); |
2928
|
176 |
3243
|
177 if (! error_state) |
2928
|
178 { |
3243
|
179 lsode_fcn = extract_function |
|
180 (tmp(0), "lsode", "__lsode_fcn__", |
|
181 "function xdot = __lsode_fcn__ (x, t) xdot = ", |
2928
|
182 "; endfunction"); |
|
183 |
3243
|
184 if (lsode_fcn) |
|
185 { |
|
186 lsode_jac = extract_function |
|
187 (tmp(1), "lsode", "__lsode_jac__", |
|
188 "function jac = __lsode_jac__ (x, t) jac = ", |
|
189 "; endfunction"); |
|
190 |
|
191 if (! lsode_jac) |
|
192 lsode_fcn = 0; |
|
193 } |
2928
|
194 } |
|
195 } |
3243
|
196 break; |
2928
|
197 |
3243
|
198 default: |
|
199 error ("lsode: first arg should be a string or 2-element string array"); |
|
200 break; |
|
201 } |
2928
|
202 |
3243
|
203 if (error_state || ! lsode_fcn) |
|
204 return retval; |
2928
|
205 |
3243
|
206 ColumnVector state = args(1).vector_value (); |
2928
|
207 |
|
208 if (error_state) |
|
209 { |
3243
|
210 error ("lsode: expecting state vector as second argument"); |
|
211 return retval; |
|
212 } |
|
213 |
|
214 ColumnVector out_times = args(2).vector_value (); |
|
215 |
|
216 if (error_state) |
|
217 { |
|
218 error ("lsode: expecting output time vector as third argument"); |
2928
|
219 return retval; |
|
220 } |
|
221 |
3243
|
222 ColumnVector crit_times; |
2928
|
223 |
3243
|
224 int crit_times_set = 0; |
|
225 if (nargin > 3) |
|
226 { |
|
227 crit_times = args(3).vector_value (); |
2928
|
228 |
3243
|
229 if (error_state) |
|
230 { |
|
231 error ("lsode: expecting critical time vector as fourth argument"); |
|
232 return retval; |
|
233 } |
2928
|
234 |
3243
|
235 crit_times_set = 1; |
|
236 } |
2928
|
237 |
3243
|
238 double tzero = out_times (0); |
|
239 int nsteps = out_times.capacity (); |
|
240 |
|
241 ODEFunc func (lsode_user_function); |
|
242 if (lsode_jac) |
|
243 func.set_jacobian_function (lsode_user_jacobian); |
2928
|
244 |
3243
|
245 LSODE ode (state, tzero, func); |
|
246 |
|
247 ode.copy (lsode_opts); |
|
248 |
|
249 int nstates = state.capacity (); |
|
250 Matrix output (nsteps, nstates + 1); |
2928
|
251 |
3243
|
252 if (crit_times_set) |
|
253 output = ode.integrate (out_times, crit_times); |
|
254 else |
|
255 output = ode.integrate (out_times); |
|
256 |
|
257 if (! error_state) |
|
258 { |
|
259 retval.resize (1); |
|
260 retval(0) = output; |
|
261 } |
|
262 } |
2928
|
263 else |
3243
|
264 print_usage ("lsode"); |
2928
|
265 |
3243
|
266 unwind_protect::run_frame ("Flsode"); |
2928
|
267 |
|
268 return retval; |
|
269 } |
|
270 |
|
271 typedef void (LSODE_options::*d_set_opt_mf) (double); |
|
272 typedef void (LSODE_options::*i_set_opt_mf) (int); |
|
273 typedef double (LSODE_options::*d_get_opt_mf) (void); |
|
274 typedef int (LSODE_options::*i_get_opt_mf) (void); |
|
275 |
|
276 #define MAX_TOKENS 3 |
|
277 |
|
278 struct LSODE_OPTIONS |
|
279 { |
|
280 const char *keyword; |
|
281 const char *kw_tok[MAX_TOKENS + 1]; |
|
282 int min_len[MAX_TOKENS + 1]; |
|
283 int min_toks_to_match; |
|
284 d_set_opt_mf d_set_fcn; |
|
285 i_set_opt_mf i_set_fcn; |
|
286 d_get_opt_mf d_get_fcn; |
|
287 i_get_opt_mf i_get_fcn; |
|
288 }; |
|
289 |
|
290 static LSODE_OPTIONS lsode_option_table [] = |
|
291 { |
|
292 { "absolute tolerance", |
|
293 { "absolute", "tolerance", 0, 0, }, |
|
294 { 1, 0, 0, 0, }, 1, |
3131
|
295 &LSODE_options::set_absolute_tolerance, 0, |
|
296 &LSODE_options::absolute_tolerance, 0, }, |
2928
|
297 |
|
298 { "initial step size", |
|
299 { "initial", "step", "size", 0, }, |
|
300 { 1, 0, 0, 0, }, 1, |
3131
|
301 &LSODE_options::set_initial_step_size, 0, |
|
302 &LSODE_options::initial_step_size, 0, }, |
2928
|
303 |
|
304 { "maximum step size", |
|
305 { "maximum", "step", "size", 0, }, |
|
306 { 2, 0, 0, 0, }, 1, |
3131
|
307 &LSODE_options::set_maximum_step_size, 0, |
|
308 &LSODE_options::maximum_step_size, 0, }, |
2928
|
309 |
|
310 { "minimum step size", |
|
311 { "minimum", "step", "size", 0, }, |
|
312 { 2, 0, 0, 0, }, 1, |
3131
|
313 &LSODE_options::set_minimum_step_size, 0, |
|
314 &LSODE_options::minimum_step_size, 0, }, |
2928
|
315 |
|
316 { "relative tolerance", |
|
317 { "relative", "tolerance", 0, 0, }, |
|
318 { 1, 0, 0, 0, }, 1, |
3131
|
319 &LSODE_options::set_relative_tolerance, 0, |
|
320 &LSODE_options::relative_tolerance, 0, }, |
2928
|
321 |
|
322 { "step limit", |
|
323 { "step", "limit", 0, 0, }, |
|
324 { 1, 0, 0, 0, }, 1, |
3131
|
325 0, &LSODE_options::set_step_limit, |
|
326 0, &LSODE_options::step_limit, }, |
2928
|
327 |
|
328 { 0, |
|
329 { 0, 0, 0, 0, }, |
|
330 { 0, 0, 0, 0, }, 0, |
|
331 0, 0, 0, 0, }, |
|
332 }; |
|
333 |
|
334 static void |
|
335 print_lsode_option_list (ostream& os) |
|
336 { |
|
337 print_usage ("lsode_options", 1); |
|
338 |
|
339 os << "\n" |
|
340 << "Options for lsode include:\n\n" |
|
341 << " keyword value\n" |
|
342 << " ------- -----\n\n"; |
|
343 |
|
344 LSODE_OPTIONS *list = lsode_option_table; |
|
345 |
|
346 const char *keyword; |
|
347 while ((keyword = list->keyword) != 0) |
|
348 { |
|
349 os.form (" %-40s ", keyword); |
|
350 if (list->d_get_fcn) |
|
351 { |
|
352 double val = (lsode_opts.*list->d_get_fcn) (); |
|
353 if (val < 0.0) |
|
354 os << "computed automatically"; |
|
355 else |
|
356 os << val; |
|
357 } |
|
358 else |
|
359 { |
|
360 int val = (lsode_opts.*list->i_get_fcn) (); |
|
361 if (val < 0) |
|
362 os << "infinite"; |
|
363 else |
|
364 os << val; |
|
365 } |
|
366 os << "\n"; |
|
367 list++; |
|
368 } |
|
369 |
|
370 os << "\n"; |
|
371 } |
|
372 |
|
373 static void |
|
374 set_lsode_option (const string& keyword, double val) |
|
375 { |
|
376 LSODE_OPTIONS *list = lsode_option_table; |
|
377 |
|
378 while (list->keyword != 0) |
|
379 { |
|
380 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
381 list->min_toks_to_match, MAX_TOKENS)) |
|
382 { |
|
383 if (list->d_set_fcn) |
|
384 (lsode_opts.*list->d_set_fcn) (val); |
|
385 else |
|
386 { |
|
387 if (xisnan (val)) |
|
388 { |
|
389 error ("lsode_options: %s: expecting integer, found NaN", |
|
390 keyword.c_str ()); |
|
391 } |
|
392 else |
|
393 (lsode_opts.*list->i_set_fcn) (NINT (val)); |
|
394 } |
|
395 return; |
|
396 } |
|
397 list++; |
|
398 } |
|
399 |
|
400 warning ("lsode_options: no match for `%s'", keyword.c_str ()); |
|
401 } |
|
402 |
|
403 static octave_value_list |
|
404 show_lsode_option (const string& keyword) |
|
405 { |
|
406 octave_value retval; |
|
407 |
|
408 LSODE_OPTIONS *list = lsode_option_table; |
|
409 |
|
410 while (list->keyword != 0) |
|
411 { |
|
412 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
413 list->min_toks_to_match, MAX_TOKENS)) |
|
414 { |
|
415 if (list->d_get_fcn) |
|
416 { |
|
417 double val = (lsode_opts.*list->d_get_fcn) (); |
|
418 if (val < 0.0) |
|
419 retval = "computed automatically"; |
|
420 else |
|
421 retval = val; |
|
422 } |
|
423 else |
|
424 { |
|
425 int val = (lsode_opts.*list->i_get_fcn) (); |
|
426 if (val < 0) |
|
427 retval = "infinite"; |
|
428 else |
|
429 retval = static_cast<double> (val); |
|
430 } |
|
431 |
|
432 return retval; |
|
433 } |
|
434 list++; |
|
435 } |
|
436 |
|
437 warning ("lsode_options: no match for `%s'", keyword.c_str ()); |
|
438 |
|
439 return retval; |
|
440 } |
|
441 |
|
442 DEFUN_DLD (lsode_options, args, , |
|
443 "lsode_options (KEYWORD, VALUE)\n\ |
|
444 \n\ |
|
445 Set or show options for lsode. Keywords may be abbreviated\n\ |
|
446 to the shortest match.") |
|
447 { |
|
448 octave_value_list retval; |
|
449 |
|
450 int nargin = args.length (); |
|
451 |
|
452 if (nargin == 0) |
|
453 { |
|
454 print_lsode_option_list (octave_stdout); |
|
455 return retval; |
|
456 } |
|
457 else if (nargin == 1 || nargin == 2) |
|
458 { |
|
459 string keyword = args(0).string_value (); |
|
460 |
|
461 if (! error_state) |
|
462 { |
|
463 if (nargin == 1) |
|
464 return show_lsode_option (keyword); |
|
465 else |
|
466 { |
|
467 double val = args(1).double_value (); |
|
468 |
|
469 if (! error_state) |
|
470 { |
|
471 set_lsode_option (keyword, val); |
|
472 return retval; |
|
473 } |
|
474 } |
|
475 } |
|
476 } |
|
477 |
|
478 print_usage ("lsode_options"); |
|
479 |
|
480 return retval; |
|
481 } |
|
482 |
|
483 /* |
|
484 ;;; Local Variables: *** |
|
485 ;;; mode: C++ *** |
|
486 ;;; End: *** |
|
487 */ |