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