7
|
1 // f-lsode.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1993, 1994, 1995 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
289
|
28 #include <strstream.h> |
|
29 |
1
|
30 #include "ODE.h" |
|
31 |
|
32 #include "tree-const.h" |
|
33 #include "variables.h" |
|
34 #include "gripes.h" |
|
35 #include "error.h" |
|
36 #include "utils.h" |
289
|
37 #include "pager.h" |
544
|
38 #include "help.h" |
519
|
39 #include "defun-dld.h" |
1
|
40 |
|
41 // Global pointer for user defined function required by lsode. |
488
|
42 static tree_fvc *lsode_fcn; |
1
|
43 |
289
|
44 static ODE_options lsode_opts; |
|
45 |
1
|
46 ColumnVector |
|
47 lsode_user_function (const ColumnVector& x, double t) |
|
48 { |
|
49 ColumnVector retval; |
|
50 |
|
51 int nstates = x.capacity (); |
|
52 |
565
|
53 Octave_object args; |
712
|
54 args(1) = t; |
1
|
55 |
|
56 if (nstates > 1) |
|
57 { |
|
58 Matrix m (nstates, 1); |
|
59 for (int i = 0; i < nstates; i++) |
|
60 m (i, 0) = x.elem (i); |
|
61 tree_constant state (m); |
712
|
62 args(0) = state; |
1
|
63 } |
|
64 else |
|
65 { |
|
66 double d = x.elem (0); |
|
67 tree_constant state (d); |
712
|
68 args(0) = state; |
1
|
69 } |
|
70 |
519
|
71 if (lsode_fcn) |
1
|
72 { |
506
|
73 Octave_object tmp = lsode_fcn->eval (0, 1, args); |
258
|
74 |
|
75 if (error_state) |
|
76 { |
|
77 gripe_user_supplied_eval ("lsode"); |
|
78 return retval; |
|
79 } |
|
80 |
497
|
81 if (tmp.length () > 0 && tmp(0).is_defined ()) |
1
|
82 { |
628
|
83 retval = tmp(0).vector_value (); |
258
|
84 |
636
|
85 if (error_state || retval.length () == 0) |
258
|
86 gripe_user_supplied_eval ("lsode"); |
1
|
87 } |
|
88 else |
497
|
89 gripe_user_supplied_eval ("lsode"); |
1
|
90 } |
|
91 |
|
92 return retval; |
|
93 } |
|
94 |
718
|
95 DEFUN_DLD_BUILTIN ("lsode", Flsode, Slsode, 4, 1, |
519
|
96 "lsode (F, X0, T_OUT, T_CRIT)\n\ |
|
97 \n\ |
|
98 The first argument is the name of the function to call to\n\ |
|
99 compute the vector of right hand sides. It must have the form\n\ |
|
100 \n\ |
|
101 xdot = f (x, t)\n\ |
|
102 \n\ |
|
103 where xdot and x are vectors and t is a scalar.\n") |
1
|
104 { |
497
|
105 Octave_object retval; |
1
|
106 |
506
|
107 int nargin = args.length (); |
|
108 |
712
|
109 if (nargin < 3 || nargin > 4 || nargout > 1) |
519
|
110 { |
|
111 print_usage ("lsode"); |
|
112 return retval; |
|
113 } |
|
114 |
712
|
115 lsode_fcn = is_valid_function (args(0), "lsode", 1); |
718
|
116 if (! lsode_fcn || takes_correct_nargs (lsode_fcn, 2, "lsode", 1) != 1) |
1
|
117 return retval; |
|
118 |
712
|
119 ColumnVector state = args(1).vector_value (); |
636
|
120 |
|
121 if (error_state) |
|
122 { |
|
123 error ("lsode: expecting state vector as second argument"); |
|
124 return retval; |
|
125 } |
|
126 |
712
|
127 ColumnVector out_times = args(2).vector_value (); |
636
|
128 |
|
129 if (error_state) |
|
130 { |
|
131 error ("lsode: expecting output time vector as third argument"); |
|
132 return retval; |
|
133 } |
|
134 |
1
|
135 ColumnVector crit_times; |
636
|
136 |
1
|
137 int crit_times_set = 0; |
712
|
138 if (nargin > 3) |
1
|
139 { |
712
|
140 crit_times = args(3).vector_value (); |
636
|
141 |
|
142 if (error_state) |
|
143 { |
|
144 error ("lsode: expecting critical time vector as fourth argument"); |
|
145 return retval; |
|
146 } |
|
147 |
1
|
148 crit_times_set = 1; |
|
149 } |
|
150 |
|
151 double tzero = out_times.elem (0); |
|
152 int nsteps = out_times.capacity (); |
|
153 |
|
154 ODEFunc func (lsode_user_function); |
|
155 ODE ode (state, tzero, func); |
289
|
156 ode.copy (lsode_opts); |
1
|
157 |
|
158 int nstates = state.capacity (); |
|
159 Matrix output (nsteps, nstates + 1); |
|
160 |
|
161 if (crit_times_set) |
|
162 output = ode.integrate (out_times, crit_times); |
|
163 else |
|
164 output = ode.integrate (out_times); |
|
165 |
497
|
166 retval.resize (1); |
516
|
167 retval(0) = output; |
1
|
168 return retval; |
|
169 } |
|
170 |
289
|
171 typedef void (ODE_options::*d_set_opt_mf) (double); |
|
172 typedef double (ODE_options::*d_get_opt_mf) (void); |
|
173 |
|
174 #define MAX_TOKENS 3 |
|
175 |
|
176 struct ODE_OPTIONS |
|
177 { |
540
|
178 const char *keyword; |
|
179 const char *kw_tok[MAX_TOKENS + 1]; |
289
|
180 int min_len[MAX_TOKENS + 1]; |
|
181 int min_toks_to_match; |
|
182 d_set_opt_mf d_set_fcn; |
|
183 d_get_opt_mf d_get_fcn; |
|
184 }; |
|
185 |
497
|
186 static ODE_OPTIONS lsode_option_table [] = |
289
|
187 { |
|
188 { "absolute tolerance", |
519
|
189 { "absolute", "tolerance", 0, 0, }, |
289
|
190 { 1, 0, 0, 0, }, 1, |
|
191 ODE_options::set_absolute_tolerance, |
|
192 ODE_options::absolute_tolerance, }, |
|
193 |
|
194 { "initial step size", |
519
|
195 { "initial", "step", "size", 0, }, |
289
|
196 { 1, 0, 0, 0, }, 1, |
|
197 ODE_options::set_initial_step_size, |
|
198 ODE_options::initial_step_size, }, |
|
199 |
|
200 { "maximum step size", |
519
|
201 { "maximum", "step", "size", 0, }, |
289
|
202 { 2, 0, 0, 0, }, 1, |
|
203 ODE_options::set_maximum_step_size, |
|
204 ODE_options::maximum_step_size, }, |
|
205 |
|
206 { "minimum step size", |
519
|
207 { "minimum", "step", "size", 0, }, |
289
|
208 { 2, 0, 0, 0, }, 1, |
|
209 ODE_options::set_minimum_step_size, |
|
210 ODE_options::minimum_step_size, }, |
|
211 |
|
212 { "relative tolerance", |
519
|
213 { "relative", "tolerance", 0, 0, }, |
289
|
214 { 1, 0, 0, 0, }, 1, |
|
215 ODE_options::set_relative_tolerance, |
|
216 ODE_options::relative_tolerance, }, |
|
217 |
519
|
218 { 0, |
|
219 { 0, 0, 0, 0, }, |
289
|
220 { 0, 0, 0, 0, }, 0, |
519
|
221 0, 0, }, |
289
|
222 }; |
|
223 |
|
224 static void |
|
225 print_lsode_option_list (void) |
|
226 { |
|
227 ostrstream output_buf; |
|
228 |
|
229 print_usage ("lsode_options", 1); |
|
230 |
|
231 output_buf << "\n" |
|
232 << "Options for lsode include:\n\n" |
|
233 << " keyword value\n" |
|
234 << " ------- -----\n\n"; |
|
235 |
|
236 ODE_OPTIONS *list = lsode_option_table; |
|
237 |
540
|
238 const char *keyword; |
519
|
239 while ((keyword = list->keyword) != 0) |
289
|
240 { |
|
241 output_buf.form (" %-40s ", keyword); |
|
242 |
|
243 double val = (lsode_opts.*list->d_get_fcn) (); |
|
244 if (val < 0.0) |
|
245 output_buf << "computed automatically"; |
|
246 else |
|
247 output_buf << val; |
|
248 |
|
249 output_buf << "\n"; |
|
250 list++; |
|
251 } |
|
252 |
|
253 output_buf << "\n" << ends; |
|
254 maybe_page_output (output_buf); |
|
255 } |
|
256 |
|
257 static void |
|
258 do_lsode_option (char *keyword, double val) |
|
259 { |
|
260 ODE_OPTIONS *list = lsode_option_table; |
|
261 |
519
|
262 while (list->keyword != 0) |
289
|
263 { |
|
264 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
265 list->min_toks_to_match, MAX_TOKENS)) |
|
266 { |
|
267 (lsode_opts.*list->d_set_fcn) (val); |
|
268 |
|
269 return; |
|
270 } |
|
271 list++; |
|
272 } |
|
273 |
|
274 warning ("lsode_options: no match for `%s'", keyword); |
|
275 } |
|
276 |
701
|
277 DEFUN_DLD_BUILTIN ("lsode_options", Flsode_options, Slsode_options, -1, 1, |
519
|
278 "lsode_options (KEYWORD, VALUE)\n\ |
|
279 \n\ |
|
280 Set or show options for lsode. Keywords may be abbreviated\n\ |
|
281 to the shortest match.") |
272
|
282 { |
497
|
283 Octave_object retval; |
272
|
284 |
506
|
285 int nargin = args.length (); |
|
286 |
712
|
287 if (nargin == 0) |
519
|
288 { |
|
289 print_lsode_option_list (); |
636
|
290 return retval; |
519
|
291 } |
712
|
292 else if (nargin == 2) |
289
|
293 { |
712
|
294 char *keyword = args(0).string_value (); |
636
|
295 |
|
296 if (! error_state) |
289
|
297 { |
712
|
298 double val = args(1).double_value (); |
636
|
299 |
|
300 if (! error_state) |
|
301 { |
|
302 do_lsode_option (keyword, val); |
|
303 return retval; |
|
304 } |
289
|
305 } |
|
306 } |
636
|
307 |
|
308 print_usage ("lsode_options"); |
289
|
309 |
272
|
310 return retval; |
|
311 } |
|
312 |
1
|
313 /* |
|
314 ;;; Local Variables: *** |
|
315 ;;; mode: C++ *** |
|
316 ;;; page-delimiter: "^/\\*" *** |
|
317 ;;; End: *** |
|
318 */ |