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