7
|
1 // f-dassl.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 "DAE.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" |
519
|
38 #include "defun-dld.h" |
1
|
39 |
|
40 // Global pointer for user defined function required by dassl. |
488
|
41 static tree_fvc *dassl_fcn; |
1
|
42 |
289
|
43 static ODE_options dassl_opts; |
|
44 |
1
|
45 ColumnVector |
|
46 dassl_user_function (const ColumnVector& x, const ColumnVector& xdot, double t) |
|
47 { |
|
48 ColumnVector retval; |
|
49 |
|
50 int nstates = x.capacity (); |
|
51 |
|
52 assert (nstates == xdot.capacity ()); |
|
53 |
|
54 // tree_constant name (dassl_fcn->name ()); |
497
|
55 Octave_object args (4); |
|
56 // args(0) = name; |
516
|
57 args(3) = t; |
1
|
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.elem (i); |
|
66 m2 (i, 0) = xdot.elem (i); |
|
67 } |
|
68 tree_constant state (m1); |
|
69 tree_constant deriv (m2); |
497
|
70 args(1) = state; |
|
71 args(2) = deriv; |
1
|
72 } |
|
73 else |
|
74 { |
|
75 double d1 = x.elem (0); |
|
76 double d2 = xdot.elem (0); |
|
77 tree_constant state (d1); |
|
78 tree_constant deriv (d2); |
497
|
79 args(1) = state; |
|
80 args(2) = deriv; |
1
|
81 } |
|
82 |
519
|
83 if (dassl_fcn) |
1
|
84 { |
506
|
85 Octave_object tmp = dassl_fcn->eval (0, 1, args); |
256
|
86 |
|
87 if (error_state) |
|
88 { |
|
89 gripe_user_supplied_eval ("dassl"); |
|
90 return retval; |
|
91 } |
|
92 |
497
|
93 if (tmp.length () > 0 && tmp(0).is_defined ()) |
1
|
94 { |
497
|
95 retval = tmp(0).to_vector (); |
256
|
96 |
|
97 if (retval.length () == 0) |
|
98 gripe_user_supplied_eval ("dassl"); |
1
|
99 } |
|
100 else |
497
|
101 gripe_user_supplied_eval ("dassl"); |
1
|
102 } |
|
103 |
|
104 return retval; |
|
105 } |
|
106 |
519
|
107 DEFUN_DLD ("dassl", Fdassl, Sdassl, 5, 1, |
|
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.") |
1
|
117 { |
497
|
118 Octave_object retval; |
1
|
119 |
506
|
120 int nargin = args.length (); |
|
121 |
519
|
122 if (nargin < 5 || nargin > 6) |
|
123 { |
|
124 print_usage ("dassl"); |
|
125 return retval; |
|
126 } |
|
127 |
497
|
128 dassl_fcn = is_valid_function (args(1), "dassl", 1); |
519
|
129 if (! dassl_fcn || takes_correct_nargs (dassl_fcn, 4, "dassl", 1) != 1) |
1
|
130 return retval; |
|
131 |
497
|
132 ColumnVector state = args(2).to_vector (); |
|
133 ColumnVector deriv = args(3).to_vector (); |
|
134 ColumnVector out_times = args(4).to_vector (); |
1
|
135 ColumnVector crit_times; |
|
136 int crit_times_set = 0; |
|
137 if (nargin > 5) |
|
138 { |
497
|
139 crit_times = args(5).to_vector (); |
1
|
140 crit_times_set = 1; |
|
141 } |
|
142 |
|
143 if (state.capacity () != deriv.capacity ()) |
|
144 { |
216
|
145 error ("dassl: x and xdot must have the same size"); |
1
|
146 return retval; |
|
147 } |
|
148 |
|
149 double tzero = out_times.elem (0); |
|
150 |
|
151 DAEFunc func (dassl_user_function); |
|
152 DAE dae (state, deriv, tzero, func); |
289
|
153 dae.copy (dassl_opts); |
1
|
154 |
|
155 Matrix output; |
|
156 Matrix deriv_output; |
|
157 |
|
158 if (crit_times_set) |
|
159 output = dae.integrate (out_times, deriv_output, crit_times); |
|
160 else |
|
161 output = dae.integrate (out_times, deriv_output); |
|
162 |
497
|
163 retval.resize (2); |
516
|
164 retval(0) = output; |
|
165 retval(1) = deriv_output; |
1
|
166 return retval; |
|
167 } |
|
168 |
289
|
169 typedef void (ODE_options::*d_set_opt_mf) (double); |
|
170 typedef double (ODE_options::*d_get_opt_mf) (void); |
|
171 |
|
172 #define MAX_TOKENS 3 |
|
173 |
497
|
174 struct DAE_OPTIONS |
289
|
175 { |
540
|
176 const char *keyword; |
|
177 const char *kw_tok[MAX_TOKENS + 1]; |
289
|
178 int min_len[MAX_TOKENS + 1]; |
|
179 int min_toks_to_match; |
|
180 d_set_opt_mf d_set_fcn; |
|
181 d_get_opt_mf d_get_fcn; |
|
182 }; |
|
183 |
497
|
184 static DAE_OPTIONS dassl_option_table [] = |
289
|
185 { |
|
186 { "absolute tolerance", |
519
|
187 { "absolute", "tolerance", 0, 0, }, |
289
|
188 { 1, 0, 0, 0, }, 1, |
|
189 ODE_options::set_absolute_tolerance, |
|
190 ODE_options::absolute_tolerance, }, |
|
191 |
|
192 { "initial step size", |
519
|
193 { "initial", "step", "size", 0, }, |
289
|
194 { 1, 0, 0, 0, }, 1, |
|
195 ODE_options::set_initial_step_size, |
|
196 ODE_options::initial_step_size, }, |
|
197 |
|
198 { "maximum step size", |
519
|
199 { "maximum", "step", "size", 0, }, |
289
|
200 { 2, 0, 0, 0, }, 1, |
|
201 ODE_options::set_maximum_step_size, |
|
202 ODE_options::maximum_step_size, }, |
|
203 |
|
204 { "relative tolerance", |
519
|
205 { "relative", "tolerance", 0, 0, }, |
289
|
206 { 1, 0, 0, 0, }, 1, |
|
207 ODE_options::set_relative_tolerance, |
|
208 ODE_options::relative_tolerance, }, |
|
209 |
519
|
210 { 0, |
|
211 { 0, 0, 0, 0, }, |
289
|
212 { 0, 0, 0, 0, }, 0, |
519
|
213 0, 0, }, |
289
|
214 }; |
|
215 |
|
216 static void |
|
217 print_dassl_option_list (void) |
|
218 { |
|
219 ostrstream output_buf; |
|
220 |
|
221 print_usage ("dassl_options", 1); |
|
222 |
|
223 output_buf << "\n" |
|
224 << "Options for dassl include:\n\n" |
|
225 << " keyword value\n" |
|
226 << " ------- -----\n\n"; |
|
227 |
497
|
228 DAE_OPTIONS *list = dassl_option_table; |
289
|
229 |
540
|
230 const char *keyword; |
519
|
231 while ((keyword = list->keyword) != 0) |
289
|
232 { |
|
233 output_buf.form (" %-40s ", keyword); |
|
234 |
|
235 double val = (dassl_opts.*list->d_get_fcn) (); |
|
236 if (val < 0.0) |
|
237 output_buf << "computed automatically"; |
|
238 else |
|
239 output_buf << val; |
|
240 |
|
241 output_buf << "\n"; |
|
242 list++; |
|
243 } |
|
244 |
|
245 output_buf << "\n" << ends; |
|
246 maybe_page_output (output_buf); |
|
247 } |
|
248 |
|
249 static void |
|
250 do_dassl_option (char *keyword, double val) |
|
251 { |
497
|
252 DAE_OPTIONS *list = dassl_option_table; |
289
|
253 |
519
|
254 while (list->keyword != 0) |
289
|
255 { |
|
256 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
257 list->min_toks_to_match, MAX_TOKENS)) |
|
258 { |
|
259 (dassl_opts.*list->d_set_fcn) (val); |
|
260 |
|
261 return; |
|
262 } |
|
263 list++; |
|
264 } |
|
265 |
|
266 warning ("dassl_options: no match for `%s'", keyword); |
|
267 } |
|
268 |
519
|
269 DEFUN_DLD ("dassl_options", Fdassl_options, Sdassl_options, -1, 1, |
|
270 "dassl_options (KEYWORD, VALUE)\n\ |
|
271 \n\ |
|
272 Set or show options for dassl. Keywords may be abbreviated\n\ |
|
273 to the shortest match.") |
272
|
274 { |
497
|
275 Octave_object retval; |
272
|
276 |
506
|
277 int nargin = args.length (); |
|
278 |
289
|
279 if (nargin == 1) |
519
|
280 { |
|
281 print_dassl_option_list (); |
|
282 } |
289
|
283 else if (nargin == 3) |
|
284 { |
497
|
285 if (args(1).is_string_type ()) |
289
|
286 { |
497
|
287 char *keyword = args(1).string_value (); |
|
288 double val = args(2).double_value (); |
289
|
289 do_dassl_option (keyword, val); |
|
290 } |
|
291 else |
|
292 print_usage ("dassl_options"); |
|
293 } |
|
294 else |
497
|
295 print_usage ("dassl_options"); |
289
|
296 |
272
|
297 return retval; |
|
298 } |
|
299 |
1
|
300 /* |
|
301 ;;; Local Variables: *** |
|
302 ;;; mode: C++ *** |
|
303 ;;; page-delimiter: "^/\\*" *** |
|
304 ;;; End: *** |
|
305 */ |