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