2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <string> |
|
28 |
|
29 #include <iostream.h> |
|
30 |
|
31 #include "DASSL.h" |
|
32 |
|
33 #include "defun-dld.h" |
|
34 #include "error.h" |
|
35 #include "gripes.h" |
|
36 #include "help.h" |
|
37 #include "oct-obj.h" |
2968
|
38 #include "ov-fcn.h" |
2928
|
39 #include "pager.h" |
|
40 #include "utils.h" |
|
41 #include "variables.h" |
|
42 |
|
43 // Global pointer for user defined function required by dassl. |
2968
|
44 static octave_function *dassl_fcn; |
2928
|
45 |
|
46 static DASSL_options dassl_opts; |
|
47 |
|
48 ColumnVector |
|
49 dassl_user_function (const ColumnVector& x, const ColumnVector& xdot, double t) |
|
50 { |
|
51 ColumnVector retval; |
|
52 |
|
53 int nstates = x.capacity (); |
|
54 |
|
55 assert (nstates == xdot.capacity ()); |
|
56 |
|
57 octave_value_list args; |
|
58 args(2) = t; |
|
59 |
|
60 if (nstates > 1) |
|
61 { |
|
62 Matrix m1 (nstates, 1); |
|
63 Matrix m2 (nstates, 1); |
|
64 for (int i = 0; i < nstates; i++) |
|
65 { |
|
66 m1 (i, 0) = x (i); |
|
67 m2 (i, 0) = xdot (i); |
|
68 } |
|
69 octave_value state (m1); |
|
70 octave_value deriv (m2); |
|
71 args(1) = deriv; |
|
72 args(0) = state; |
|
73 } |
|
74 else |
|
75 { |
|
76 double d1 = x (0); |
|
77 double d2 = xdot (0); |
|
78 octave_value state (d1); |
|
79 octave_value deriv (d2); |
|
80 args(1) = deriv; |
|
81 args(0) = state; |
|
82 } |
|
83 |
|
84 if (dassl_fcn) |
|
85 { |
2968
|
86 octave_value_list tmp = dassl_fcn->do_index_op (1, args); |
2928
|
87 |
|
88 if (error_state) |
|
89 { |
|
90 gripe_user_supplied_eval ("dassl"); |
|
91 return retval; |
|
92 } |
|
93 |
|
94 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
95 { |
|
96 retval = tmp(0).vector_value (); |
|
97 |
|
98 if (error_state || retval.length () == 0) |
|
99 gripe_user_supplied_eval ("dassl"); |
|
100 } |
|
101 else |
|
102 gripe_user_supplied_eval ("dassl"); |
|
103 } |
|
104 |
|
105 return retval; |
|
106 } |
|
107 |
|
108 DEFUN_DLD (dassl, args, , |
|
109 "dassl (\"function_name\", x_0, xdot_0, t_out)\n\ |
|
110 dassl (F, X_0, XDOT_0, T_OUT, T_CRIT)\n\ |
|
111 \n\ |
|
112 The first argument is the name of the function to call to\n\ |
|
113 compute the vector of residuals. It must have the form\n\ |
|
114 \n\ |
|
115 res = f (x, xdot, t)\n\ |
|
116 \n\ |
|
117 where x, xdot, and res are vectors, and t is a scalar.") |
|
118 { |
|
119 octave_value_list retval; |
|
120 |
|
121 int nargin = args.length (); |
|
122 |
|
123 if (nargin < 4 || nargin > 5) |
|
124 { |
|
125 print_usage ("dassl"); |
|
126 return retval; |
|
127 } |
|
128 |
|
129 dassl_fcn = extract_function |
|
130 (args(0), "dassl", "__dassl_fcn__", |
|
131 "function res = __dassl_fcn__ (x, xdot, t) res = ", |
|
132 "; endfunction"); |
|
133 |
|
134 if (! dassl_fcn) |
|
135 return retval; |
|
136 |
|
137 ColumnVector state = args(1).vector_value (); |
|
138 |
|
139 if (error_state) |
|
140 { |
|
141 error ("dassl: expecting state vector as second argument"); |
|
142 return retval; |
|
143 } |
|
144 |
|
145 ColumnVector deriv = args(2).vector_value (); |
|
146 |
|
147 if (error_state) |
|
148 { |
|
149 error ("dassl: expecting derivative vector as third argument"); |
|
150 return retval; |
|
151 } |
|
152 |
|
153 ColumnVector out_times = args(3).vector_value (); |
|
154 |
|
155 if (error_state) |
|
156 { |
|
157 error ("dassl: expecting output time vector as fourth argument"); |
|
158 return retval; |
|
159 } |
|
160 |
|
161 ColumnVector crit_times; |
|
162 int crit_times_set = 0; |
|
163 if (nargin > 4) |
|
164 { |
|
165 crit_times = args(4).vector_value (); |
|
166 |
|
167 if (error_state) |
|
168 { |
|
169 error ("dassl: expecting critical time vector as fifth argument"); |
|
170 return retval; |
|
171 } |
|
172 |
|
173 crit_times_set = 1; |
|
174 } |
|
175 |
|
176 if (state.capacity () != deriv.capacity ()) |
|
177 { |
|
178 error ("dassl: x and xdot must have the same size"); |
|
179 return retval; |
|
180 } |
|
181 |
|
182 double tzero = out_times (0); |
|
183 |
|
184 DAEFunc func (dassl_user_function); |
|
185 DASSL dae (state, deriv, tzero, func); |
|
186 dae.copy (dassl_opts); |
|
187 |
|
188 Matrix output; |
|
189 Matrix deriv_output; |
|
190 |
|
191 if (crit_times_set) |
|
192 output = dae.integrate (out_times, deriv_output, crit_times); |
|
193 else |
|
194 output = dae.integrate (out_times, deriv_output); |
|
195 |
|
196 if (! error_state) |
|
197 { |
|
198 retval.resize (2); |
|
199 |
|
200 retval(0) = output; |
|
201 retval(1) = deriv_output; |
|
202 } |
|
203 |
|
204 return retval; |
|
205 } |
|
206 |
|
207 typedef void (DASSL_options::*d_set_opt_mf) (double); |
|
208 typedef double (DASSL_options::*d_get_opt_mf) (void); |
|
209 |
|
210 #define MAX_TOKENS 3 |
|
211 |
|
212 struct DASSL_OPTIONS |
|
213 { |
|
214 const char *keyword; |
|
215 const char *kw_tok[MAX_TOKENS + 1]; |
|
216 int min_len[MAX_TOKENS + 1]; |
|
217 int min_toks_to_match; |
|
218 d_set_opt_mf d_set_fcn; |
|
219 d_get_opt_mf d_get_fcn; |
|
220 }; |
|
221 |
|
222 static DASSL_OPTIONS dassl_option_table [] = |
|
223 { |
|
224 { "absolute tolerance", |
|
225 { "absolute", "tolerance", 0, 0, }, |
|
226 { 1, 0, 0, 0, }, 1, |
|
227 DASSL_options::set_absolute_tolerance, |
|
228 DASSL_options::absolute_tolerance, }, |
|
229 |
|
230 { "initial step size", |
|
231 { "initial", "step", "size", 0, }, |
|
232 { 1, 0, 0, 0, }, 1, |
|
233 DASSL_options::set_initial_step_size, |
|
234 DASSL_options::initial_step_size, }, |
|
235 |
|
236 { "maximum step size", |
|
237 { "maximum", "step", "size", 0, }, |
|
238 { 2, 0, 0, 0, }, 1, |
|
239 DASSL_options::set_maximum_step_size, |
|
240 DASSL_options::maximum_step_size, }, |
|
241 |
|
242 { "relative tolerance", |
|
243 { "relative", "tolerance", 0, 0, }, |
|
244 { 1, 0, 0, 0, }, 1, |
|
245 DASSL_options::set_relative_tolerance, |
|
246 DASSL_options::relative_tolerance, }, |
|
247 |
|
248 { 0, |
|
249 { 0, 0, 0, 0, }, |
|
250 { 0, 0, 0, 0, }, 0, |
|
251 0, 0, }, |
|
252 }; |
|
253 |
|
254 static void |
|
255 print_dassl_option_list (ostream& os) |
|
256 { |
|
257 print_usage ("dassl_options", 1); |
|
258 |
|
259 os << "\n" |
|
260 << "Options for dassl include:\n\n" |
|
261 << " keyword value\n" |
|
262 << " ------- -----\n\n"; |
|
263 |
|
264 DASSL_OPTIONS *list = dassl_option_table; |
|
265 |
|
266 const char *keyword; |
|
267 while ((keyword = list->keyword) != 0) |
|
268 { |
|
269 os.form (" %-40s ", keyword); |
|
270 |
|
271 double val = (dassl_opts.*list->d_get_fcn) (); |
|
272 if (val < 0.0) |
|
273 os << "computed automatically"; |
|
274 else |
|
275 os << val; |
|
276 |
|
277 os << "\n"; |
|
278 list++; |
|
279 } |
|
280 |
|
281 os << "\n"; |
|
282 } |
|
283 |
|
284 static void |
|
285 set_dassl_option (const string& keyword, double val) |
|
286 { |
|
287 DASSL_OPTIONS *list = dassl_option_table; |
|
288 |
|
289 while (list->keyword != 0) |
|
290 { |
|
291 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
292 list->min_toks_to_match, MAX_TOKENS)) |
|
293 { |
|
294 (dassl_opts.*list->d_set_fcn) (val); |
|
295 |
|
296 return; |
|
297 } |
|
298 list++; |
|
299 } |
|
300 |
|
301 warning ("dassl_options: no match for `%s'", keyword.c_str ()); |
|
302 } |
|
303 |
|
304 static octave_value_list |
|
305 show_dassl_option (const string& keyword) |
|
306 { |
|
307 octave_value retval; |
|
308 |
|
309 DASSL_OPTIONS *list = dassl_option_table; |
|
310 |
|
311 while (list->keyword != 0) |
|
312 { |
|
313 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
314 list->min_toks_to_match, MAX_TOKENS)) |
|
315 { |
|
316 double val = (dassl_opts.*list->d_get_fcn) (); |
|
317 if (val < 0.0) |
|
318 retval = "computed automatically"; |
|
319 else |
|
320 retval = val; |
|
321 |
|
322 return retval; |
|
323 } |
|
324 list++; |
|
325 } |
|
326 |
|
327 warning ("dassl_options: no match for `%s'", keyword.c_str ()); |
|
328 |
|
329 return retval; |
|
330 } |
|
331 |
|
332 DEFUN_DLD (dassl_options, args, , |
|
333 "dassl_options (KEYWORD, VALUE)\n\ |
|
334 \n\ |
|
335 Set or show options for dassl. Keywords may be abbreviated\n\ |
|
336 to the shortest match.") |
|
337 { |
|
338 octave_value_list retval; |
|
339 |
|
340 int nargin = args.length (); |
|
341 |
|
342 if (nargin == 0) |
|
343 { |
|
344 print_dassl_option_list (octave_stdout); |
|
345 return retval; |
|
346 } |
|
347 else if (nargin == 1 || nargin == 2) |
|
348 { |
|
349 string keyword = args(0).string_value (); |
|
350 |
|
351 if (! error_state) |
|
352 { |
|
353 if (nargin == 1) |
|
354 return show_dassl_option (keyword); |
|
355 else |
|
356 { |
|
357 double val = args(1).double_value (); |
|
358 |
|
359 if (! error_state) |
|
360 { |
|
361 set_dassl_option (keyword, val); |
|
362 return retval; |
|
363 } |
|
364 } |
|
365 } |
|
366 } |
|
367 |
|
368 print_usage ("dassl_options"); |
|
369 |
|
370 return retval; |
|
371 } |
|
372 |
|
373 /* |
|
374 ;;; Local Variables: *** |
|
375 ;;; mode: C++ *** |
|
376 ;;; End: *** |
|
377 */ |