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