7
|
1 // f-lsode.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
296
|
4 Copyright (C) 1993, 1994 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 |
|
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 |
|
53 // tree_constant name (lsode_fcn->name ()); |
565
|
54 Octave_object args; |
|
55 args(2) = t; |
497
|
56 // args(0) = name; |
1
|
57 |
|
58 if (nstates > 1) |
|
59 { |
|
60 Matrix m (nstates, 1); |
|
61 for (int i = 0; i < nstates; i++) |
|
62 m (i, 0) = x.elem (i); |
|
63 tree_constant state (m); |
497
|
64 args(1) = state; |
1
|
65 } |
|
66 else |
|
67 { |
|
68 double d = x.elem (0); |
|
69 tree_constant state (d); |
497
|
70 args(1) = state; |
1
|
71 } |
|
72 |
519
|
73 if (lsode_fcn) |
1
|
74 { |
506
|
75 Octave_object tmp = lsode_fcn->eval (0, 1, args); |
258
|
76 |
|
77 if (error_state) |
|
78 { |
|
79 gripe_user_supplied_eval ("lsode"); |
|
80 return retval; |
|
81 } |
|
82 |
497
|
83 if (tmp.length () > 0 && tmp(0).is_defined ()) |
1
|
84 { |
628
|
85 retval = tmp(0).vector_value (); |
258
|
86 |
|
87 if (retval.length () == 0) |
|
88 gripe_user_supplied_eval ("lsode"); |
1
|
89 } |
|
90 else |
497
|
91 gripe_user_supplied_eval ("lsode"); |
1
|
92 } |
|
93 |
|
94 return retval; |
|
95 } |
|
96 |
519
|
97 DEFUN_DLD ("lsode", Flsode, Slsode, 6, 1, |
|
98 "lsode (F, X0, T_OUT, T_CRIT)\n\ |
|
99 \n\ |
|
100 The first argument is the name of the function to call to\n\ |
|
101 compute the vector of right hand sides. It must have the form\n\ |
|
102 \n\ |
|
103 xdot = f (x, t)\n\ |
|
104 \n\ |
|
105 where xdot and x are vectors and t is a scalar.\n") |
1
|
106 { |
497
|
107 Octave_object retval; |
1
|
108 |
506
|
109 int nargin = args.length (); |
|
110 |
519
|
111 if (nargin < 4 || nargin > 5 || nargout > 1) |
|
112 { |
|
113 print_usage ("lsode"); |
|
114 return retval; |
|
115 } |
|
116 |
497
|
117 lsode_fcn = is_valid_function (args(1), "lsode", 1); |
519
|
118 if (! lsode_fcn || takes_correct_nargs (lsode_fcn, 3, "lsode", 1) != 1) |
1
|
119 return retval; |
|
120 |
628
|
121 ColumnVector state = args(2).vector_value (); |
|
122 ColumnVector out_times = args(3).vector_value (); |
1
|
123 ColumnVector crit_times; |
|
124 int crit_times_set = 0; |
|
125 if (nargin > 4) |
|
126 { |
628
|
127 crit_times = args(4).vector_value (); |
1
|
128 crit_times_set = 1; |
|
129 } |
|
130 |
|
131 double tzero = out_times.elem (0); |
|
132 int nsteps = out_times.capacity (); |
|
133 |
|
134 ODEFunc func (lsode_user_function); |
|
135 ODE ode (state, tzero, func); |
289
|
136 ode.copy (lsode_opts); |
1
|
137 |
|
138 int nstates = state.capacity (); |
|
139 Matrix output (nsteps, nstates + 1); |
|
140 |
|
141 if (crit_times_set) |
|
142 output = ode.integrate (out_times, crit_times); |
|
143 else |
|
144 output = ode.integrate (out_times); |
|
145 |
497
|
146 retval.resize (1); |
516
|
147 retval(0) = output; |
1
|
148 return retval; |
|
149 } |
|
150 |
289
|
151 typedef void (ODE_options::*d_set_opt_mf) (double); |
|
152 typedef double (ODE_options::*d_get_opt_mf) (void); |
|
153 |
|
154 #define MAX_TOKENS 3 |
|
155 |
|
156 struct ODE_OPTIONS |
|
157 { |
540
|
158 const char *keyword; |
|
159 const char *kw_tok[MAX_TOKENS + 1]; |
289
|
160 int min_len[MAX_TOKENS + 1]; |
|
161 int min_toks_to_match; |
|
162 d_set_opt_mf d_set_fcn; |
|
163 d_get_opt_mf d_get_fcn; |
|
164 }; |
|
165 |
497
|
166 static ODE_OPTIONS lsode_option_table [] = |
289
|
167 { |
|
168 { "absolute tolerance", |
519
|
169 { "absolute", "tolerance", 0, 0, }, |
289
|
170 { 1, 0, 0, 0, }, 1, |
|
171 ODE_options::set_absolute_tolerance, |
|
172 ODE_options::absolute_tolerance, }, |
|
173 |
|
174 { "initial step size", |
519
|
175 { "initial", "step", "size", 0, }, |
289
|
176 { 1, 0, 0, 0, }, 1, |
|
177 ODE_options::set_initial_step_size, |
|
178 ODE_options::initial_step_size, }, |
|
179 |
|
180 { "maximum step size", |
519
|
181 { "maximum", "step", "size", 0, }, |
289
|
182 { 2, 0, 0, 0, }, 1, |
|
183 ODE_options::set_maximum_step_size, |
|
184 ODE_options::maximum_step_size, }, |
|
185 |
|
186 { "minimum step size", |
519
|
187 { "minimum", "step", "size", 0, }, |
289
|
188 { 2, 0, 0, 0, }, 1, |
|
189 ODE_options::set_minimum_step_size, |
|
190 ODE_options::minimum_step_size, }, |
|
191 |
|
192 { "relative tolerance", |
519
|
193 { "relative", "tolerance", 0, 0, }, |
289
|
194 { 1, 0, 0, 0, }, 1, |
|
195 ODE_options::set_relative_tolerance, |
|
196 ODE_options::relative_tolerance, }, |
|
197 |
519
|
198 { 0, |
|
199 { 0, 0, 0, 0, }, |
289
|
200 { 0, 0, 0, 0, }, 0, |
519
|
201 0, 0, }, |
289
|
202 }; |
|
203 |
|
204 static void |
|
205 print_lsode_option_list (void) |
|
206 { |
|
207 ostrstream output_buf; |
|
208 |
|
209 print_usage ("lsode_options", 1); |
|
210 |
|
211 output_buf << "\n" |
|
212 << "Options for lsode include:\n\n" |
|
213 << " keyword value\n" |
|
214 << " ------- -----\n\n"; |
|
215 |
|
216 ODE_OPTIONS *list = lsode_option_table; |
|
217 |
540
|
218 const char *keyword; |
519
|
219 while ((keyword = list->keyword) != 0) |
289
|
220 { |
|
221 output_buf.form (" %-40s ", keyword); |
|
222 |
|
223 double val = (lsode_opts.*list->d_get_fcn) (); |
|
224 if (val < 0.0) |
|
225 output_buf << "computed automatically"; |
|
226 else |
|
227 output_buf << val; |
|
228 |
|
229 output_buf << "\n"; |
|
230 list++; |
|
231 } |
|
232 |
|
233 output_buf << "\n" << ends; |
|
234 maybe_page_output (output_buf); |
|
235 } |
|
236 |
|
237 static void |
|
238 do_lsode_option (char *keyword, double val) |
|
239 { |
|
240 ODE_OPTIONS *list = lsode_option_table; |
|
241 |
519
|
242 while (list->keyword != 0) |
289
|
243 { |
|
244 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
245 list->min_toks_to_match, MAX_TOKENS)) |
|
246 { |
|
247 (lsode_opts.*list->d_set_fcn) (val); |
|
248 |
|
249 return; |
|
250 } |
|
251 list++; |
|
252 } |
|
253 |
|
254 warning ("lsode_options: no match for `%s'", keyword); |
|
255 } |
|
256 |
519
|
257 DEFUN_DLD ("lsode_options", Flsode_options, Slsode_options, -1, 1, |
|
258 "lsode_options (KEYWORD, VALUE)\n\ |
|
259 \n\ |
|
260 Set or show options for lsode. Keywords may be abbreviated\n\ |
|
261 to the shortest match.") |
272
|
262 { |
497
|
263 Octave_object retval; |
272
|
264 |
506
|
265 int nargin = args.length (); |
|
266 |
289
|
267 if (nargin == 1) |
519
|
268 { |
|
269 print_lsode_option_list (); |
|
270 } |
289
|
271 else if (nargin == 3) |
|
272 { |
610
|
273 if (args(1).is_string ()) |
289
|
274 { |
497
|
275 char *keyword = args(1).string_value (); |
|
276 double val = args(2).double_value (); |
289
|
277 do_lsode_option (keyword, val); |
|
278 } |
|
279 else |
|
280 print_usage ("lsode_options"); |
|
281 } |
|
282 else |
497
|
283 print_usage ("lsode_options"); |
289
|
284 |
272
|
285 return retval; |
|
286 } |
|
287 |
1
|
288 /* |
|
289 ;;; Local Variables: *** |
|
290 ;;; mode: C++ *** |
|
291 ;;; page-delimiter: "^/\\*" *** |
|
292 ;;; End: *** |
|
293 */ |