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