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