1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 John W. Eaton |
1
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1728
|
27 #include <string> |
|
28 |
2095
|
29 #include <iostream.h> |
289
|
30 |
1839
|
31 #include "LSODE.h" |
1
|
32 |
1352
|
33 #include "defun-dld.h" |
1
|
34 #include "error.h" |
1352
|
35 #include "gripes.h" |
|
36 #include "help.h" |
1740
|
37 #include "oct-obj.h" |
289
|
38 #include "pager.h" |
1352
|
39 #include "utils.h" |
|
40 #include "variables.h" |
1
|
41 |
|
42 // Global pointer for user defined function required by lsode. |
488
|
43 static tree_fvc *lsode_fcn; |
1
|
44 |
1839
|
45 static LSODE_options lsode_opts; |
289
|
46 |
1
|
47 ColumnVector |
|
48 lsode_user_function (const ColumnVector& x, double t) |
|
49 { |
|
50 ColumnVector retval; |
|
51 |
|
52 int nstates = x.capacity (); |
|
53 |
2086
|
54 octave_value_list args; |
712
|
55 args(1) = t; |
1
|
56 |
|
57 if (nstates > 1) |
|
58 { |
|
59 Matrix m (nstates, 1); |
|
60 for (int i = 0; i < nstates; i++) |
2305
|
61 m (i, 0) = x (i); |
2086
|
62 octave_value state (m); |
712
|
63 args(0) = state; |
1
|
64 } |
|
65 else |
|
66 { |
2305
|
67 double d = x (0); |
2086
|
68 octave_value state (d); |
712
|
69 args(0) = state; |
1
|
70 } |
|
71 |
519
|
72 if (lsode_fcn) |
1
|
73 { |
2086
|
74 octave_value_list 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 { |
628
|
84 retval = tmp(0).vector_value (); |
258
|
85 |
636
|
86 if (error_state || retval.length () == 0) |
258
|
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 |
1957
|
96 DEFUN_DLD_BUILTIN (lsode, args, nargout, |
519
|
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 { |
2086
|
106 octave_value_list retval; |
1
|
107 |
506
|
108 int nargin = args.length (); |
|
109 |
712
|
110 if (nargin < 3 || nargin > 4 || nargout > 1) |
519
|
111 { |
|
112 print_usage ("lsode"); |
|
113 return retval; |
|
114 } |
|
115 |
712
|
116 lsode_fcn = is_valid_function (args(0), "lsode", 1); |
1488
|
117 if (! lsode_fcn) |
1
|
118 return retval; |
|
119 |
712
|
120 ColumnVector state = args(1).vector_value (); |
636
|
121 |
|
122 if (error_state) |
|
123 { |
|
124 error ("lsode: expecting state vector as second argument"); |
|
125 return retval; |
|
126 } |
|
127 |
712
|
128 ColumnVector out_times = args(2).vector_value (); |
636
|
129 |
|
130 if (error_state) |
|
131 { |
|
132 error ("lsode: expecting output time vector as third argument"); |
|
133 return retval; |
|
134 } |
|
135 |
1
|
136 ColumnVector crit_times; |
636
|
137 |
1
|
138 int crit_times_set = 0; |
712
|
139 if (nargin > 3) |
1
|
140 { |
712
|
141 crit_times = args(3).vector_value (); |
636
|
142 |
|
143 if (error_state) |
|
144 { |
|
145 error ("lsode: expecting critical time vector as fourth argument"); |
|
146 return retval; |
|
147 } |
|
148 |
1
|
149 crit_times_set = 1; |
|
150 } |
|
151 |
2305
|
152 double tzero = out_times (0); |
1
|
153 int nsteps = out_times.capacity (); |
|
154 |
|
155 ODEFunc func (lsode_user_function); |
1839
|
156 LSODE ode (state, tzero, func); |
289
|
157 ode.copy (lsode_opts); |
1
|
158 |
|
159 int nstates = state.capacity (); |
|
160 Matrix output (nsteps, nstates + 1); |
|
161 |
|
162 if (crit_times_set) |
|
163 output = ode.integrate (out_times, crit_times); |
|
164 else |
|
165 output = ode.integrate (out_times); |
|
166 |
497
|
167 retval.resize (1); |
516
|
168 retval(0) = output; |
1
|
169 return retval; |
|
170 } |
|
171 |
1839
|
172 typedef void (LSODE_options::*d_set_opt_mf) (double); |
|
173 typedef double (LSODE_options::*d_get_opt_mf) (void); |
289
|
174 |
|
175 #define MAX_TOKENS 3 |
|
176 |
1839
|
177 struct LSODE_OPTIONS |
289
|
178 { |
540
|
179 const char *keyword; |
|
180 const char *kw_tok[MAX_TOKENS + 1]; |
289
|
181 int min_len[MAX_TOKENS + 1]; |
|
182 int min_toks_to_match; |
|
183 d_set_opt_mf d_set_fcn; |
|
184 d_get_opt_mf d_get_fcn; |
|
185 }; |
|
186 |
1839
|
187 static LSODE_OPTIONS lsode_option_table [] = |
289
|
188 { |
|
189 { "absolute tolerance", |
519
|
190 { "absolute", "tolerance", 0, 0, }, |
289
|
191 { 1, 0, 0, 0, }, 1, |
1839
|
192 LSODE_options::set_absolute_tolerance, |
|
193 LSODE_options::absolute_tolerance, }, |
289
|
194 |
|
195 { "initial step size", |
519
|
196 { "initial", "step", "size", 0, }, |
289
|
197 { 1, 0, 0, 0, }, 1, |
1839
|
198 LSODE_options::set_initial_step_size, |
|
199 LSODE_options::initial_step_size, }, |
289
|
200 |
|
201 { "maximum step size", |
519
|
202 { "maximum", "step", "size", 0, }, |
289
|
203 { 2, 0, 0, 0, }, 1, |
1839
|
204 LSODE_options::set_maximum_step_size, |
|
205 LSODE_options::maximum_step_size, }, |
289
|
206 |
|
207 { "minimum step size", |
519
|
208 { "minimum", "step", "size", 0, }, |
289
|
209 { 2, 0, 0, 0, }, 1, |
1839
|
210 LSODE_options::set_minimum_step_size, |
|
211 LSODE_options::minimum_step_size, }, |
289
|
212 |
|
213 { "relative tolerance", |
519
|
214 { "relative", "tolerance", 0, 0, }, |
289
|
215 { 1, 0, 0, 0, }, 1, |
1839
|
216 LSODE_options::set_relative_tolerance, |
|
217 LSODE_options::relative_tolerance, }, |
289
|
218 |
519
|
219 { 0, |
|
220 { 0, 0, 0, 0, }, |
289
|
221 { 0, 0, 0, 0, }, 0, |
519
|
222 0, 0, }, |
289
|
223 }; |
|
224 |
|
225 static void |
2095
|
226 print_lsode_option_list (ostream& os) |
289
|
227 { |
|
228 print_usage ("lsode_options", 1); |
|
229 |
2095
|
230 os << "\n" |
|
231 << "Options for lsode include:\n\n" |
|
232 << " keyword value\n" |
|
233 << " ------- -----\n\n"; |
289
|
234 |
1839
|
235 LSODE_OPTIONS *list = lsode_option_table; |
289
|
236 |
540
|
237 const char *keyword; |
519
|
238 while ((keyword = list->keyword) != 0) |
289
|
239 { |
2095
|
240 os.form (" %-40s ", keyword); |
289
|
241 |
|
242 double val = (lsode_opts.*list->d_get_fcn) (); |
|
243 if (val < 0.0) |
2095
|
244 os << "computed automatically"; |
289
|
245 else |
2095
|
246 os << val; |
289
|
247 |
2095
|
248 os << "\n"; |
289
|
249 list++; |
|
250 } |
|
251 |
2095
|
252 os << "\n"; |
289
|
253 } |
|
254 |
|
255 static void |
1755
|
256 set_lsode_option (const string& keyword, double val) |
289
|
257 { |
1839
|
258 LSODE_OPTIONS *list = lsode_option_table; |
289
|
259 |
519
|
260 while (list->keyword != 0) |
289
|
261 { |
|
262 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
263 list->min_toks_to_match, MAX_TOKENS)) |
|
264 { |
|
265 (lsode_opts.*list->d_set_fcn) (val); |
|
266 |
|
267 return; |
|
268 } |
|
269 list++; |
|
270 } |
|
271 |
1755
|
272 warning ("lsode_options: no match for `%s'", keyword.c_str ()); |
289
|
273 } |
|
274 |
2086
|
275 static octave_value_list |
1755
|
276 show_lsode_option (const string& keyword) |
1329
|
277 { |
2086
|
278 octave_value_list retval; |
1329
|
279 |
1839
|
280 LSODE_OPTIONS *list = lsode_option_table; |
1329
|
281 |
|
282 while (list->keyword != 0) |
|
283 { |
|
284 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
285 list->min_toks_to_match, MAX_TOKENS)) |
|
286 { |
|
287 return (lsode_opts.*list->d_get_fcn) (); |
|
288 } |
|
289 list++; |
|
290 } |
|
291 |
1755
|
292 warning ("lsode_options: no match for `%s'", keyword.c_str ()); |
1329
|
293 |
|
294 return retval; |
|
295 } |
|
296 |
1957
|
297 DEFUN_DLD_BUILTIN (lsode_options, args, , |
519
|
298 "lsode_options (KEYWORD, VALUE)\n\ |
|
299 \n\ |
|
300 Set or show options for lsode. Keywords may be abbreviated\n\ |
|
301 to the shortest match.") |
272
|
302 { |
2086
|
303 octave_value_list retval; |
272
|
304 |
506
|
305 int nargin = args.length (); |
|
306 |
712
|
307 if (nargin == 0) |
519
|
308 { |
2095
|
309 print_lsode_option_list (octave_stdout); |
636
|
310 return retval; |
519
|
311 } |
1329
|
312 else if (nargin == 1 || nargin == 2) |
289
|
313 { |
1755
|
314 string keyword = args(0).string_value (); |
636
|
315 |
|
316 if (! error_state) |
289
|
317 { |
1329
|
318 if (nargin == 1) |
|
319 return show_lsode_option (keyword); |
|
320 else |
|
321 { |
|
322 double val = args(1).double_value (); |
636
|
323 |
1329
|
324 if (! error_state) |
|
325 { |
|
326 set_lsode_option (keyword, val); |
|
327 return retval; |
|
328 } |
636
|
329 } |
289
|
330 } |
|
331 } |
636
|
332 |
|
333 print_usage ("lsode_options"); |
289
|
334 |
272
|
335 return retval; |
|
336 } |
|
337 |
1
|
338 /* |
|
339 ;;; Local Variables: *** |
|
340 ;;; mode: C++ *** |
|
341 ;;; End: *** |
|
342 */ |