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