7
|
1 // f-dassl.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
1884
|
4 Copyright (C) 1996 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 |
1728
|
28 #include <string> |
|
29 |
289
|
30 #include <strstream.h> |
|
31 |
1839
|
32 #include "DASSL.h" |
1
|
33 |
1352
|
34 #include "defun-dld.h" |
1
|
35 #include "error.h" |
1352
|
36 #include "gripes.h" |
|
37 #include "help.h" |
1740
|
38 #include "oct-obj.h" |
289
|
39 #include "pager.h" |
1352
|
40 #include "utils.h" |
|
41 #include "variables.h" |
1
|
42 |
|
43 // Global pointer for user defined function required by dassl. |
488
|
44 static tree_fvc *dassl_fcn; |
1
|
45 |
1839
|
46 static DASSL_options dassl_opts; |
289
|
47 |
1
|
48 ColumnVector |
|
49 dassl_user_function (const ColumnVector& x, const ColumnVector& xdot, double t) |
|
50 { |
|
51 ColumnVector retval; |
|
52 |
|
53 int nstates = x.capacity (); |
|
54 |
|
55 assert (nstates == xdot.capacity ()); |
|
56 |
565
|
57 Octave_object args; |
712
|
58 args(2) = t; |
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); |
712
|
71 args(1) = deriv; |
|
72 args(0) = state; |
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); |
712
|
80 args(1) = deriv; |
|
81 args(0) = state; |
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 { |
628
|
96 retval = tmp(0).vector_value (); |
256
|
97 |
636
|
98 if (error_state || retval.length () == 0) |
256
|
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 |
1684
|
108 DEFUN_DLD_BUILTIN ("dassl", Fdassl, Sdassl, FSdassl, 10, |
519
|
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 |
712
|
123 if (nargin < 4 || nargin > 5) |
519
|
124 { |
|
125 print_usage ("dassl"); |
|
126 return retval; |
|
127 } |
|
128 |
712
|
129 dassl_fcn = is_valid_function (args(0), "dassl", 1); |
1488
|
130 if (! dassl_fcn) |
1
|
131 return retval; |
|
132 |
712
|
133 ColumnVector state = args(1).vector_value (); |
636
|
134 |
|
135 if (error_state) |
|
136 { |
|
137 error ("dassl: expecting state vector as second argument"); |
|
138 return retval; |
|
139 } |
|
140 |
712
|
141 ColumnVector deriv = args(2).vector_value (); |
636
|
142 |
|
143 if (error_state) |
|
144 { |
|
145 error ("dassl: expecting derivative vector as third argument"); |
|
146 return retval; |
|
147 } |
|
148 |
712
|
149 ColumnVector out_times = args(3).vector_value (); |
636
|
150 |
|
151 if (error_state) |
|
152 { |
|
153 error ("dassl: expecting output time vector as fourth argument"); |
|
154 return retval; |
|
155 } |
|
156 |
1
|
157 ColumnVector crit_times; |
|
158 int crit_times_set = 0; |
712
|
159 if (nargin > 4) |
1
|
160 { |
712
|
161 crit_times = args(4).vector_value (); |
636
|
162 |
|
163 if (error_state) |
|
164 { |
|
165 error ("dassl: expecting critical time vector as fifth argument"); |
|
166 return retval; |
|
167 } |
|
168 |
1
|
169 crit_times_set = 1; |
|
170 } |
|
171 |
|
172 if (state.capacity () != deriv.capacity ()) |
|
173 { |
216
|
174 error ("dassl: x and xdot must have the same size"); |
1
|
175 return retval; |
|
176 } |
|
177 |
|
178 double tzero = out_times.elem (0); |
|
179 |
|
180 DAEFunc func (dassl_user_function); |
1839
|
181 DASSL dae (state, deriv, tzero, func); |
289
|
182 dae.copy (dassl_opts); |
1
|
183 |
|
184 Matrix output; |
|
185 Matrix deriv_output; |
|
186 |
|
187 if (crit_times_set) |
|
188 output = dae.integrate (out_times, deriv_output, crit_times); |
|
189 else |
|
190 output = dae.integrate (out_times, deriv_output); |
|
191 |
497
|
192 retval.resize (2); |
516
|
193 retval(0) = output; |
|
194 retval(1) = deriv_output; |
1
|
195 return retval; |
|
196 } |
|
197 |
1839
|
198 typedef void (DASSL_options::*d_set_opt_mf) (double); |
|
199 typedef double (DASSL_options::*d_get_opt_mf) (void); |
289
|
200 |
|
201 #define MAX_TOKENS 3 |
|
202 |
1839
|
203 struct DASSL_OPTIONS |
289
|
204 { |
540
|
205 const char *keyword; |
|
206 const char *kw_tok[MAX_TOKENS + 1]; |
289
|
207 int min_len[MAX_TOKENS + 1]; |
|
208 int min_toks_to_match; |
|
209 d_set_opt_mf d_set_fcn; |
|
210 d_get_opt_mf d_get_fcn; |
|
211 }; |
|
212 |
1839
|
213 static DASSL_OPTIONS dassl_option_table [] = |
289
|
214 { |
|
215 { "absolute tolerance", |
519
|
216 { "absolute", "tolerance", 0, 0, }, |
289
|
217 { 1, 0, 0, 0, }, 1, |
1839
|
218 DASSL_options::set_absolute_tolerance, |
|
219 DASSL_options::absolute_tolerance, }, |
289
|
220 |
|
221 { "initial step size", |
519
|
222 { "initial", "step", "size", 0, }, |
289
|
223 { 1, 0, 0, 0, }, 1, |
1839
|
224 DASSL_options::set_initial_step_size, |
|
225 DASSL_options::initial_step_size, }, |
289
|
226 |
|
227 { "maximum step size", |
519
|
228 { "maximum", "step", "size", 0, }, |
289
|
229 { 2, 0, 0, 0, }, 1, |
1839
|
230 DASSL_options::set_maximum_step_size, |
|
231 DASSL_options::maximum_step_size, }, |
289
|
232 |
|
233 { "relative tolerance", |
519
|
234 { "relative", "tolerance", 0, 0, }, |
289
|
235 { 1, 0, 0, 0, }, 1, |
1839
|
236 DASSL_options::set_relative_tolerance, |
|
237 DASSL_options::relative_tolerance, }, |
289
|
238 |
519
|
239 { 0, |
|
240 { 0, 0, 0, 0, }, |
289
|
241 { 0, 0, 0, 0, }, 0, |
519
|
242 0, 0, }, |
289
|
243 }; |
|
244 |
|
245 static void |
|
246 print_dassl_option_list (void) |
|
247 { |
|
248 ostrstream output_buf; |
|
249 |
|
250 print_usage ("dassl_options", 1); |
|
251 |
|
252 output_buf << "\n" |
|
253 << "Options for dassl include:\n\n" |
|
254 << " keyword value\n" |
|
255 << " ------- -----\n\n"; |
|
256 |
1839
|
257 DASSL_OPTIONS *list = dassl_option_table; |
289
|
258 |
540
|
259 const char *keyword; |
519
|
260 while ((keyword = list->keyword) != 0) |
289
|
261 { |
|
262 output_buf.form (" %-40s ", keyword); |
|
263 |
|
264 double val = (dassl_opts.*list->d_get_fcn) (); |
|
265 if (val < 0.0) |
|
266 output_buf << "computed automatically"; |
|
267 else |
|
268 output_buf << val; |
|
269 |
|
270 output_buf << "\n"; |
|
271 list++; |
|
272 } |
|
273 |
|
274 output_buf << "\n" << ends; |
|
275 maybe_page_output (output_buf); |
|
276 } |
|
277 |
|
278 static void |
1755
|
279 set_dassl_option (const string& keyword, double val) |
289
|
280 { |
1839
|
281 DASSL_OPTIONS *list = dassl_option_table; |
289
|
282 |
519
|
283 while (list->keyword != 0) |
289
|
284 { |
|
285 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
286 list->min_toks_to_match, MAX_TOKENS)) |
|
287 { |
|
288 (dassl_opts.*list->d_set_fcn) (val); |
|
289 |
|
290 return; |
|
291 } |
|
292 list++; |
|
293 } |
|
294 |
1755
|
295 warning ("dassl_options: no match for `%s'", keyword.c_str ()); |
289
|
296 } |
|
297 |
1329
|
298 static Octave_object |
1755
|
299 show_dassl_option (const string& keyword) |
1329
|
300 { |
|
301 Octave_object retval; |
|
302 |
1839
|
303 DASSL_OPTIONS *list = dassl_option_table; |
1329
|
304 |
|
305 while (list->keyword != 0) |
|
306 { |
|
307 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
308 list->min_toks_to_match, MAX_TOKENS)) |
|
309 { |
|
310 return (dassl_opts.*list->d_get_fcn) (); |
|
311 } |
|
312 list++; |
|
313 } |
|
314 |
1755
|
315 warning ("dassl_options: no match for `%s'", keyword.c_str ()); |
1329
|
316 |
|
317 return retval; |
|
318 } |
|
319 |
1684
|
320 DEFUN_DLD_BUILTIN ("dassl_options", Fdassl_options, Sdassl_options, |
|
321 FSdassl_options, 10, |
519
|
322 "dassl_options (KEYWORD, VALUE)\n\ |
|
323 \n\ |
|
324 Set or show options for dassl. Keywords may be abbreviated\n\ |
|
325 to the shortest match.") |
272
|
326 { |
497
|
327 Octave_object retval; |
272
|
328 |
506
|
329 int nargin = args.length (); |
|
330 |
712
|
331 if (nargin == 0) |
519
|
332 { |
|
333 print_dassl_option_list (); |
636
|
334 return retval; |
519
|
335 } |
1329
|
336 else if (nargin == 1 || nargin == 2) |
289
|
337 { |
1755
|
338 string keyword = args(0).string_value (); |
636
|
339 |
|
340 if (! error_state) |
289
|
341 { |
1329
|
342 if (nargin == 1) |
|
343 return show_dassl_option (keyword); |
|
344 else |
|
345 { |
|
346 double val = args(1).double_value (); |
636
|
347 |
1329
|
348 if (! error_state) |
|
349 { |
|
350 set_dassl_option (keyword, val); |
|
351 return retval; |
|
352 } |
636
|
353 } |
289
|
354 } |
|
355 } |
636
|
356 |
|
357 print_usage ("dassl_options"); |
289
|
358 |
272
|
359 return retval; |
|
360 } |
|
361 |
1
|
362 /* |
|
363 ;;; Local Variables: *** |
|
364 ;;; mode: C++ *** |
|
365 ;;; page-delimiter: "^/\\*" *** |
|
366 ;;; End: *** |
|
367 */ |