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