1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1728
|
27 #include <string> |
|
28 |
2095
|
29 #include <iostream.h> |
289
|
30 |
1839
|
31 #include "DASSL.h" |
1
|
32 |
1352
|
33 #include "defun-dld.h" |
1
|
34 #include "error.h" |
1352
|
35 #include "gripes.h" |
|
36 #include "help.h" |
1740
|
37 #include "oct-obj.h" |
289
|
38 #include "pager.h" |
1352
|
39 #include "utils.h" |
|
40 #include "variables.h" |
1
|
41 |
|
42 // Global pointer for user defined function required by dassl. |
488
|
43 static tree_fvc *dassl_fcn; |
1
|
44 |
1839
|
45 static DASSL_options dassl_opts; |
289
|
46 |
1
|
47 ColumnVector |
|
48 dassl_user_function (const ColumnVector& x, const ColumnVector& xdot, double t) |
|
49 { |
|
50 ColumnVector retval; |
|
51 |
|
52 int nstates = x.capacity (); |
|
53 |
|
54 assert (nstates == xdot.capacity ()); |
|
55 |
2086
|
56 octave_value_list args; |
712
|
57 args(2) = 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 } |
2086
|
68 octave_value state (m1); |
|
69 octave_value deriv (m2); |
712
|
70 args(1) = deriv; |
|
71 args(0) = state; |
1
|
72 } |
|
73 else |
|
74 { |
|
75 double d1 = x.elem (0); |
|
76 double d2 = xdot.elem (0); |
2086
|
77 octave_value state (d1); |
|
78 octave_value deriv (d2); |
712
|
79 args(1) = deriv; |
|
80 args(0) = state; |
1
|
81 } |
|
82 |
519
|
83 if (dassl_fcn) |
1
|
84 { |
2086
|
85 octave_value_list 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 { |
628
|
95 retval = tmp(0).vector_value (); |
256
|
96 |
636
|
97 if (error_state || retval.length () == 0) |
256
|
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 |
1957
|
107 DEFUN_DLD_BUILTIN (dassl, args, , |
519
|
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 { |
2086
|
118 octave_value_list retval; |
1
|
119 |
506
|
120 int nargin = args.length (); |
|
121 |
712
|
122 if (nargin < 4 || nargin > 5) |
519
|
123 { |
|
124 print_usage ("dassl"); |
|
125 return retval; |
|
126 } |
|
127 |
712
|
128 dassl_fcn = is_valid_function (args(0), "dassl", 1); |
1488
|
129 if (! dassl_fcn) |
1
|
130 return retval; |
|
131 |
712
|
132 ColumnVector state = args(1).vector_value (); |
636
|
133 |
|
134 if (error_state) |
|
135 { |
|
136 error ("dassl: expecting state vector as second argument"); |
|
137 return retval; |
|
138 } |
|
139 |
712
|
140 ColumnVector deriv = args(2).vector_value (); |
636
|
141 |
|
142 if (error_state) |
|
143 { |
|
144 error ("dassl: expecting derivative vector as third argument"); |
|
145 return retval; |
|
146 } |
|
147 |
712
|
148 ColumnVector out_times = args(3).vector_value (); |
636
|
149 |
|
150 if (error_state) |
|
151 { |
|
152 error ("dassl: expecting output time vector as fourth argument"); |
|
153 return retval; |
|
154 } |
|
155 |
1
|
156 ColumnVector crit_times; |
|
157 int crit_times_set = 0; |
712
|
158 if (nargin > 4) |
1
|
159 { |
712
|
160 crit_times = args(4).vector_value (); |
636
|
161 |
|
162 if (error_state) |
|
163 { |
|
164 error ("dassl: expecting critical time vector as fifth argument"); |
|
165 return retval; |
|
166 } |
|
167 |
1
|
168 crit_times_set = 1; |
|
169 } |
|
170 |
|
171 if (state.capacity () != deriv.capacity ()) |
|
172 { |
216
|
173 error ("dassl: x and xdot must have the same size"); |
1
|
174 return retval; |
|
175 } |
|
176 |
|
177 double tzero = out_times.elem (0); |
|
178 |
|
179 DAEFunc func (dassl_user_function); |
1839
|
180 DASSL dae (state, deriv, tzero, func); |
289
|
181 dae.copy (dassl_opts); |
1
|
182 |
|
183 Matrix output; |
|
184 Matrix deriv_output; |
|
185 |
|
186 if (crit_times_set) |
|
187 output = dae.integrate (out_times, deriv_output, crit_times); |
|
188 else |
|
189 output = dae.integrate (out_times, deriv_output); |
|
190 |
497
|
191 retval.resize (2); |
516
|
192 retval(0) = output; |
|
193 retval(1) = deriv_output; |
1
|
194 return retval; |
|
195 } |
|
196 |
1839
|
197 typedef void (DASSL_options::*d_set_opt_mf) (double); |
|
198 typedef double (DASSL_options::*d_get_opt_mf) (void); |
289
|
199 |
|
200 #define MAX_TOKENS 3 |
|
201 |
1839
|
202 struct DASSL_OPTIONS |
289
|
203 { |
540
|
204 const char *keyword; |
|
205 const char *kw_tok[MAX_TOKENS + 1]; |
289
|
206 int min_len[MAX_TOKENS + 1]; |
|
207 int min_toks_to_match; |
|
208 d_set_opt_mf d_set_fcn; |
|
209 d_get_opt_mf d_get_fcn; |
|
210 }; |
|
211 |
1839
|
212 static DASSL_OPTIONS dassl_option_table [] = |
289
|
213 { |
|
214 { "absolute tolerance", |
519
|
215 { "absolute", "tolerance", 0, 0, }, |
289
|
216 { 1, 0, 0, 0, }, 1, |
1839
|
217 DASSL_options::set_absolute_tolerance, |
|
218 DASSL_options::absolute_tolerance, }, |
289
|
219 |
|
220 { "initial step size", |
519
|
221 { "initial", "step", "size", 0, }, |
289
|
222 { 1, 0, 0, 0, }, 1, |
1839
|
223 DASSL_options::set_initial_step_size, |
|
224 DASSL_options::initial_step_size, }, |
289
|
225 |
|
226 { "maximum step size", |
519
|
227 { "maximum", "step", "size", 0, }, |
289
|
228 { 2, 0, 0, 0, }, 1, |
1839
|
229 DASSL_options::set_maximum_step_size, |
|
230 DASSL_options::maximum_step_size, }, |
289
|
231 |
|
232 { "relative tolerance", |
519
|
233 { "relative", "tolerance", 0, 0, }, |
289
|
234 { 1, 0, 0, 0, }, 1, |
1839
|
235 DASSL_options::set_relative_tolerance, |
|
236 DASSL_options::relative_tolerance, }, |
289
|
237 |
519
|
238 { 0, |
|
239 { 0, 0, 0, 0, }, |
289
|
240 { 0, 0, 0, 0, }, 0, |
519
|
241 0, 0, }, |
289
|
242 }; |
|
243 |
|
244 static void |
2095
|
245 print_dassl_option_list (ostream& os) |
289
|
246 { |
|
247 print_usage ("dassl_options", 1); |
|
248 |
2095
|
249 os << "\n" |
|
250 << "Options for dassl include:\n\n" |
|
251 << " keyword value\n" |
|
252 << " ------- -----\n\n"; |
289
|
253 |
1839
|
254 DASSL_OPTIONS *list = dassl_option_table; |
289
|
255 |
540
|
256 const char *keyword; |
519
|
257 while ((keyword = list->keyword) != 0) |
289
|
258 { |
2095
|
259 os.form (" %-40s ", keyword); |
289
|
260 |
|
261 double val = (dassl_opts.*list->d_get_fcn) (); |
|
262 if (val < 0.0) |
2095
|
263 os << "computed automatically"; |
289
|
264 else |
2095
|
265 os << val; |
289
|
266 |
2095
|
267 os << "\n"; |
289
|
268 list++; |
|
269 } |
|
270 |
2095
|
271 os << "\n"; |
289
|
272 } |
|
273 |
|
274 static void |
1755
|
275 set_dassl_option (const string& keyword, double val) |
289
|
276 { |
1839
|
277 DASSL_OPTIONS *list = dassl_option_table; |
289
|
278 |
519
|
279 while (list->keyword != 0) |
289
|
280 { |
|
281 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
282 list->min_toks_to_match, MAX_TOKENS)) |
|
283 { |
|
284 (dassl_opts.*list->d_set_fcn) (val); |
|
285 |
|
286 return; |
|
287 } |
|
288 list++; |
|
289 } |
|
290 |
1755
|
291 warning ("dassl_options: no match for `%s'", keyword.c_str ()); |
289
|
292 } |
|
293 |
2086
|
294 static octave_value_list |
1755
|
295 show_dassl_option (const string& keyword) |
1329
|
296 { |
2086
|
297 octave_value_list retval; |
1329
|
298 |
1839
|
299 DASSL_OPTIONS *list = dassl_option_table; |
1329
|
300 |
|
301 while (list->keyword != 0) |
|
302 { |
|
303 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
304 list->min_toks_to_match, MAX_TOKENS)) |
|
305 { |
|
306 return (dassl_opts.*list->d_get_fcn) (); |
|
307 } |
|
308 list++; |
|
309 } |
|
310 |
1755
|
311 warning ("dassl_options: no match for `%s'", keyword.c_str ()); |
1329
|
312 |
|
313 return retval; |
|
314 } |
|
315 |
1957
|
316 DEFUN_DLD_BUILTIN (dassl_options, args, , |
519
|
317 "dassl_options (KEYWORD, VALUE)\n\ |
|
318 \n\ |
|
319 Set or show options for dassl. Keywords may be abbreviated\n\ |
|
320 to the shortest match.") |
272
|
321 { |
2086
|
322 octave_value_list retval; |
272
|
323 |
506
|
324 int nargin = args.length (); |
|
325 |
712
|
326 if (nargin == 0) |
519
|
327 { |
2095
|
328 print_dassl_option_list (octave_stdout); |
636
|
329 return retval; |
519
|
330 } |
1329
|
331 else if (nargin == 1 || nargin == 2) |
289
|
332 { |
1755
|
333 string keyword = args(0).string_value (); |
636
|
334 |
|
335 if (! error_state) |
289
|
336 { |
1329
|
337 if (nargin == 1) |
|
338 return show_dassl_option (keyword); |
|
339 else |
|
340 { |
|
341 double val = args(1).double_value (); |
636
|
342 |
1329
|
343 if (! error_state) |
|
344 { |
|
345 set_dassl_option (keyword, val); |
|
346 return retval; |
|
347 } |
636
|
348 } |
289
|
349 } |
|
350 } |
636
|
351 |
|
352 print_usage ("dassl_options"); |
289
|
353 |
272
|
354 return retval; |
|
355 } |
|
356 |
1
|
357 /* |
|
358 ;;; Local Variables: *** |
|
359 ;;; mode: C++ *** |
|
360 ;;; End: *** |
|
361 */ |