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 "help.h" |
|
38 #include "oct-obj.h" |
|
39 #include "oct-sym.h" |
|
40 #include "pager.h" |
|
41 #include "utils.h" |
|
42 #include "variables.h" |
|
43 |
|
44 // Global pointer for user defined function required by lsode. |
|
45 static octave_symbol *lsode_fcn; |
|
46 |
|
47 // Global pointer for optional user defined jacobian function used by lsode. |
|
48 static octave_symbol *lsode_jac; |
|
49 |
|
50 static LSODE_options lsode_opts; |
|
51 |
|
52 ColumnVector |
|
53 lsode_user_function (const ColumnVector& x, double t) |
|
54 { |
|
55 ColumnVector retval; |
|
56 |
|
57 int nstates = x.capacity (); |
|
58 |
|
59 octave_value_list args; |
|
60 args(1) = t; |
|
61 |
|
62 Matrix m (nstates, 1); |
|
63 for (int i = 0; i < nstates; i++) |
|
64 m (i, 0) = x (i); |
|
65 octave_value state (m); |
|
66 args(0) = state; |
|
67 |
|
68 if (lsode_fcn) |
|
69 { |
|
70 octave_value_list tmp = lsode_fcn->eval (1, args); |
|
71 |
|
72 if (error_state) |
|
73 { |
|
74 gripe_user_supplied_eval ("lsode"); |
|
75 return retval; |
|
76 } |
|
77 |
|
78 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
79 { |
|
80 retval = tmp(0).vector_value (); |
|
81 |
|
82 if (error_state || retval.length () == 0) |
|
83 gripe_user_supplied_eval ("lsode"); |
|
84 } |
|
85 else |
|
86 gripe_user_supplied_eval ("lsode"); |
|
87 } |
|
88 |
|
89 return retval; |
|
90 } |
|
91 |
|
92 Matrix |
|
93 lsode_user_jacobian (const ColumnVector& x, double t) |
|
94 { |
|
95 Matrix retval; |
|
96 |
|
97 int nstates = x.capacity (); |
|
98 |
|
99 octave_value_list args; |
|
100 args(1) = t; |
|
101 |
|
102 Matrix m (nstates, 1); |
|
103 for (int i = 0; i < nstates; i++) |
|
104 m (i, 0) = x (i); |
|
105 octave_value state (m); |
|
106 args(0) = state; |
|
107 |
|
108 if (lsode_jac) |
|
109 { |
|
110 octave_value_list tmp = lsode_jac->eval (1, args); |
|
111 |
|
112 if (error_state) |
|
113 { |
|
114 gripe_user_supplied_eval ("lsode"); |
|
115 return retval; |
|
116 } |
|
117 |
|
118 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
119 { |
|
120 retval = tmp(0).matrix_value (); |
|
121 |
|
122 if (error_state || retval.length () == 0) |
|
123 gripe_user_supplied_eval ("lsode"); |
|
124 } |
|
125 else |
|
126 gripe_user_supplied_eval ("lsode"); |
|
127 } |
|
128 |
|
129 return retval; |
|
130 } |
|
131 |
|
132 DEFUN_DLD (lsode, args, nargout, |
|
133 "lsode (F, X0, T_OUT, T_CRIT)\n\ |
|
134 \n\ |
|
135 The first argument is the name of the function to call to\n\ |
|
136 compute the vector of right hand sides. It must have the form\n\ |
|
137 \n\ |
|
138 xdot = f (x, t)\n\ |
|
139 \n\ |
|
140 where xdot and x are vectors and t is a scalar.\n") |
|
141 { |
|
142 octave_value_list retval; |
|
143 |
|
144 int nargin = args.length (); |
|
145 |
|
146 if (nargin < 3 || nargin > 4 || nargout > 1) |
|
147 { |
|
148 print_usage ("lsode"); |
|
149 return retval; |
|
150 } |
|
151 |
|
152 octave_value f_arg = args(0); |
|
153 |
|
154 switch (f_arg.rows ()) |
|
155 { |
|
156 case 1: |
|
157 lsode_fcn = extract_function |
|
158 (args(0), "lsode", "__lsode_fcn__", |
|
159 "function xdot = __lsode_fcn__ (x, t) xdot = ", |
|
160 "; endfunction"); |
|
161 break; |
|
162 |
|
163 case 2: |
|
164 { |
|
165 string_vector tmp = args(0).all_strings (); |
|
166 |
|
167 if (! error_state) |
|
168 { |
|
169 lsode_fcn = extract_function |
|
170 (tmp(0), "lsode", "__lsode_fcn__", |
|
171 "function xdot = __lsode_fcn__ (x, t) xdot = ", |
|
172 "; endfunction"); |
|
173 |
|
174 if (lsode_fcn) |
|
175 { |
|
176 lsode_jac = extract_function |
|
177 (tmp(1), "lsode", "__lsode_jac__", |
|
178 "function jac = __lsode_jac__ (x, t) jac = ", |
|
179 "; endfunction"); |
|
180 |
|
181 if (! lsode_jac) |
|
182 lsode_fcn = 0; |
|
183 } |
|
184 } |
|
185 } |
|
186 break; |
|
187 |
|
188 default: |
|
189 error ("lsode: second arg should be a string or 2-element string array"); |
|
190 break; |
|
191 } |
|
192 |
|
193 if (error_state || ! lsode_fcn) |
|
194 return retval; |
|
195 |
|
196 ColumnVector state = args(1).vector_value (); |
|
197 |
|
198 if (error_state) |
|
199 { |
|
200 error ("lsode: expecting state vector as second argument"); |
|
201 return retval; |
|
202 } |
|
203 |
|
204 ColumnVector out_times = args(2).vector_value (); |
|
205 |
|
206 if (error_state) |
|
207 { |
|
208 error ("lsode: expecting output time vector as third argument"); |
|
209 return retval; |
|
210 } |
|
211 |
|
212 ColumnVector crit_times; |
|
213 |
|
214 int crit_times_set = 0; |
|
215 if (nargin > 3) |
|
216 { |
|
217 crit_times = args(3).vector_value (); |
|
218 |
|
219 if (error_state) |
|
220 { |
|
221 error ("lsode: expecting critical time vector as fourth argument"); |
|
222 return retval; |
|
223 } |
|
224 |
|
225 crit_times_set = 1; |
|
226 } |
|
227 |
|
228 double tzero = out_times (0); |
|
229 int nsteps = out_times.capacity (); |
|
230 |
|
231 ODEFunc func (lsode_user_function); |
|
232 if (lsode_jac) |
|
233 func.set_jacobian_function (lsode_user_jacobian); |
|
234 |
|
235 LSODE ode (state, tzero, func); |
|
236 |
|
237 ode.copy (lsode_opts); |
|
238 |
|
239 int nstates = state.capacity (); |
|
240 Matrix output (nsteps, nstates + 1); |
|
241 |
|
242 if (crit_times_set) |
|
243 output = ode.integrate (out_times, crit_times); |
|
244 else |
|
245 output = ode.integrate (out_times); |
|
246 |
|
247 if (! error_state) |
|
248 { |
|
249 retval.resize (1); |
|
250 retval(0) = output; |
|
251 } |
|
252 |
|
253 return retval; |
|
254 } |
|
255 |
|
256 typedef void (LSODE_options::*d_set_opt_mf) (double); |
|
257 typedef void (LSODE_options::*i_set_opt_mf) (int); |
|
258 typedef double (LSODE_options::*d_get_opt_mf) (void); |
|
259 typedef int (LSODE_options::*i_get_opt_mf) (void); |
|
260 |
|
261 #define MAX_TOKENS 3 |
|
262 |
|
263 struct LSODE_OPTIONS |
|
264 { |
|
265 const char *keyword; |
|
266 const char *kw_tok[MAX_TOKENS + 1]; |
|
267 int min_len[MAX_TOKENS + 1]; |
|
268 int min_toks_to_match; |
|
269 d_set_opt_mf d_set_fcn; |
|
270 i_set_opt_mf i_set_fcn; |
|
271 d_get_opt_mf d_get_fcn; |
|
272 i_get_opt_mf i_get_fcn; |
|
273 }; |
|
274 |
|
275 static LSODE_OPTIONS lsode_option_table [] = |
|
276 { |
|
277 { "absolute tolerance", |
|
278 { "absolute", "tolerance", 0, 0, }, |
|
279 { 1, 0, 0, 0, }, 1, |
|
280 LSODE_options::set_absolute_tolerance, 0, |
|
281 LSODE_options::absolute_tolerance, 0, }, |
|
282 |
|
283 { "initial step size", |
|
284 { "initial", "step", "size", 0, }, |
|
285 { 1, 0, 0, 0, }, 1, |
|
286 LSODE_options::set_initial_step_size, 0, |
|
287 LSODE_options::initial_step_size, 0, }, |
|
288 |
|
289 { "maximum step size", |
|
290 { "maximum", "step", "size", 0, }, |
|
291 { 2, 0, 0, 0, }, 1, |
|
292 LSODE_options::set_maximum_step_size, 0, |
|
293 LSODE_options::maximum_step_size, 0, }, |
|
294 |
|
295 { "minimum step size", |
|
296 { "minimum", "step", "size", 0, }, |
|
297 { 2, 0, 0, 0, }, 1, |
|
298 LSODE_options::set_minimum_step_size, 0, |
|
299 LSODE_options::minimum_step_size, 0, }, |
|
300 |
|
301 { "relative tolerance", |
|
302 { "relative", "tolerance", 0, 0, }, |
|
303 { 1, 0, 0, 0, }, 1, |
|
304 LSODE_options::set_relative_tolerance, 0, |
|
305 LSODE_options::relative_tolerance, 0, }, |
|
306 |
|
307 { "step limit", |
|
308 { "step", "limit", 0, 0, }, |
|
309 { 1, 0, 0, 0, }, 1, |
|
310 0, LSODE_options::set_step_limit, |
|
311 0, LSODE_options::step_limit, }, |
|
312 |
|
313 { 0, |
|
314 { 0, 0, 0, 0, }, |
|
315 { 0, 0, 0, 0, }, 0, |
|
316 0, 0, 0, 0, }, |
|
317 }; |
|
318 |
|
319 static void |
|
320 print_lsode_option_list (ostream& os) |
|
321 { |
|
322 print_usage ("lsode_options", 1); |
|
323 |
|
324 os << "\n" |
|
325 << "Options for lsode include:\n\n" |
|
326 << " keyword value\n" |
|
327 << " ------- -----\n\n"; |
|
328 |
|
329 LSODE_OPTIONS *list = lsode_option_table; |
|
330 |
|
331 const char *keyword; |
|
332 while ((keyword = list->keyword) != 0) |
|
333 { |
|
334 os.form (" %-40s ", keyword); |
|
335 if (list->d_get_fcn) |
|
336 { |
|
337 double val = (lsode_opts.*list->d_get_fcn) (); |
|
338 if (val < 0.0) |
|
339 os << "computed automatically"; |
|
340 else |
|
341 os << val; |
|
342 } |
|
343 else |
|
344 { |
|
345 int val = (lsode_opts.*list->i_get_fcn) (); |
|
346 if (val < 0) |
|
347 os << "infinite"; |
|
348 else |
|
349 os << val; |
|
350 } |
|
351 os << "\n"; |
|
352 list++; |
|
353 } |
|
354 |
|
355 os << "\n"; |
|
356 } |
|
357 |
|
358 static void |
|
359 set_lsode_option (const string& keyword, double val) |
|
360 { |
|
361 LSODE_OPTIONS *list = lsode_option_table; |
|
362 |
|
363 while (list->keyword != 0) |
|
364 { |
|
365 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
366 list->min_toks_to_match, MAX_TOKENS)) |
|
367 { |
|
368 if (list->d_set_fcn) |
|
369 (lsode_opts.*list->d_set_fcn) (val); |
|
370 else |
|
371 { |
|
372 if (xisnan (val)) |
|
373 { |
|
374 error ("lsode_options: %s: expecting integer, found NaN", |
|
375 keyword.c_str ()); |
|
376 } |
|
377 else |
|
378 (lsode_opts.*list->i_set_fcn) (NINT (val)); |
|
379 } |
|
380 return; |
|
381 } |
|
382 list++; |
|
383 } |
|
384 |
|
385 warning ("lsode_options: no match for `%s'", keyword.c_str ()); |
|
386 } |
|
387 |
|
388 static octave_value_list |
|
389 show_lsode_option (const string& keyword) |
|
390 { |
|
391 octave_value retval; |
|
392 |
|
393 LSODE_OPTIONS *list = lsode_option_table; |
|
394 |
|
395 while (list->keyword != 0) |
|
396 { |
|
397 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
398 list->min_toks_to_match, MAX_TOKENS)) |
|
399 { |
|
400 if (list->d_get_fcn) |
|
401 { |
|
402 double val = (lsode_opts.*list->d_get_fcn) (); |
|
403 if (val < 0.0) |
|
404 retval = "computed automatically"; |
|
405 else |
|
406 retval = val; |
|
407 } |
|
408 else |
|
409 { |
|
410 int val = (lsode_opts.*list->i_get_fcn) (); |
|
411 if (val < 0) |
|
412 retval = "infinite"; |
|
413 else |
|
414 retval = static_cast<double> (val); |
|
415 } |
|
416 |
|
417 return retval; |
|
418 } |
|
419 list++; |
|
420 } |
|
421 |
|
422 warning ("lsode_options: no match for `%s'", keyword.c_str ()); |
|
423 |
|
424 return retval; |
|
425 } |
|
426 |
|
427 DEFUN_DLD (lsode_options, args, , |
|
428 "lsode_options (KEYWORD, VALUE)\n\ |
|
429 \n\ |
|
430 Set or show options for lsode. Keywords may be abbreviated\n\ |
|
431 to the shortest match.") |
|
432 { |
|
433 octave_value_list retval; |
|
434 |
|
435 int nargin = args.length (); |
|
436 |
|
437 if (nargin == 0) |
|
438 { |
|
439 print_lsode_option_list (octave_stdout); |
|
440 return retval; |
|
441 } |
|
442 else if (nargin == 1 || nargin == 2) |
|
443 { |
|
444 string keyword = args(0).string_value (); |
|
445 |
|
446 if (! error_state) |
|
447 { |
|
448 if (nargin == 1) |
|
449 return show_lsode_option (keyword); |
|
450 else |
|
451 { |
|
452 double val = args(1).double_value (); |
|
453 |
|
454 if (! error_state) |
|
455 { |
|
456 set_lsode_option (keyword, val); |
|
457 return retval; |
|
458 } |
|
459 } |
|
460 } |
|
461 } |
|
462 |
|
463 print_usage ("lsode_options"); |
|
464 |
|
465 return retval; |
|
466 } |
|
467 |
|
468 /* |
|
469 ;;; Local Variables: *** |
|
470 ;;; mode: C++ *** |
|
471 ;;; End: *** |
|
472 */ |