2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <string> |
|
28 |
|
29 #include <iostream.h> |
|
30 |
|
31 #include "DASSL.h" |
|
32 |
|
33 #include "defun-dld.h" |
|
34 #include "error.h" |
|
35 #include "gripes.h" |
|
36 #include "oct-obj.h" |
2968
|
37 #include "ov-fcn.h" |
2928
|
38 #include "pager.h" |
3243
|
39 #include "unwind-prot.h" |
2928
|
40 #include "utils.h" |
|
41 #include "variables.h" |
|
42 |
|
43 // Global pointer for user defined function required by dassl. |
2968
|
44 static octave_function *dassl_fcn; |
2928
|
45 |
|
46 static DASSL_options dassl_opts; |
|
47 |
3243
|
48 // Is this a recursive call? |
|
49 static int call_depth = 0; |
|
50 |
2928
|
51 ColumnVector |
|
52 dassl_user_function (const ColumnVector& x, const ColumnVector& xdot, double t) |
|
53 { |
|
54 ColumnVector retval; |
|
55 |
|
56 int nstates = x.capacity (); |
|
57 |
|
58 assert (nstates == xdot.capacity ()); |
|
59 |
|
60 octave_value_list args; |
|
61 args(2) = t; |
|
62 |
|
63 if (nstates > 1) |
|
64 { |
|
65 Matrix m1 (nstates, 1); |
|
66 Matrix m2 (nstates, 1); |
|
67 for (int i = 0; i < nstates; i++) |
|
68 { |
|
69 m1 (i, 0) = x (i); |
|
70 m2 (i, 0) = xdot (i); |
|
71 } |
|
72 octave_value state (m1); |
|
73 octave_value deriv (m2); |
|
74 args(1) = deriv; |
|
75 args(0) = state; |
|
76 } |
|
77 else |
|
78 { |
|
79 double d1 = x (0); |
|
80 double d2 = xdot (0); |
|
81 octave_value state (d1); |
|
82 octave_value deriv (d2); |
|
83 args(1) = deriv; |
|
84 args(0) = state; |
|
85 } |
|
86 |
|
87 if (dassl_fcn) |
|
88 { |
2968
|
89 octave_value_list tmp = dassl_fcn->do_index_op (1, args); |
2928
|
90 |
|
91 if (error_state) |
|
92 { |
|
93 gripe_user_supplied_eval ("dassl"); |
|
94 return retval; |
|
95 } |
|
96 |
|
97 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
98 { |
|
99 retval = tmp(0).vector_value (); |
|
100 |
|
101 if (error_state || retval.length () == 0) |
|
102 gripe_user_supplied_eval ("dassl"); |
|
103 } |
|
104 else |
|
105 gripe_user_supplied_eval ("dassl"); |
|
106 } |
|
107 |
|
108 return retval; |
|
109 } |
|
110 |
|
111 DEFUN_DLD (dassl, args, , |
|
112 "dassl (\"function_name\", x_0, xdot_0, t_out)\n\ |
|
113 dassl (F, X_0, XDOT_0, T_OUT, T_CRIT)\n\ |
|
114 \n\ |
|
115 The first argument is the name of the function to call to\n\ |
|
116 compute the vector of residuals. It must have the form\n\ |
|
117 \n\ |
|
118 res = f (x, xdot, t)\n\ |
|
119 \n\ |
|
120 where x, xdot, and res are vectors, and t is a scalar.") |
|
121 { |
|
122 octave_value_list retval; |
|
123 |
3243
|
124 unwind_protect::begin_frame ("Fdassl"); |
2928
|
125 |
3243
|
126 unwind_protect_int (call_depth); |
|
127 call_depth++; |
2928
|
128 |
3243
|
129 if (call_depth > 1) |
2928
|
130 { |
3243
|
131 error ("dassl: invalid recursive call"); |
2928
|
132 return retval; |
|
133 } |
|
134 |
3243
|
135 int nargin = args.length (); |
|
136 |
|
137 if (nargin > 3 && nargin < 6) |
2928
|
138 { |
3243
|
139 dassl_fcn = extract_function |
|
140 (args(0), "dassl", "__dassl_fcn__", |
|
141 "function res = __dassl_fcn__ (x, xdot, t) res = ", |
|
142 "; endfunction"); |
|
143 |
|
144 if (! dassl_fcn) |
|
145 return retval; |
|
146 |
|
147 ColumnVector state = args(1).vector_value (); |
2928
|
148 |
|
149 if (error_state) |
|
150 { |
3243
|
151 error ("dassl: expecting state vector as second argument"); |
|
152 return retval; |
|
153 } |
|
154 |
|
155 ColumnVector deriv = args(2).vector_value (); |
|
156 |
|
157 if (error_state) |
|
158 { |
|
159 error ("dassl: expecting derivative vector as third argument"); |
|
160 return retval; |
|
161 } |
|
162 |
|
163 ColumnVector out_times = args(3).vector_value (); |
|
164 |
|
165 if (error_state) |
|
166 { |
|
167 error ("dassl: expecting output time vector as fourth argument"); |
2928
|
168 return retval; |
|
169 } |
|
170 |
3243
|
171 ColumnVector crit_times; |
|
172 int crit_times_set = 0; |
|
173 if (nargin > 4) |
|
174 { |
|
175 crit_times = args(4).vector_value (); |
2928
|
176 |
3243
|
177 if (error_state) |
|
178 { |
|
179 error ("dassl: expecting critical time vector as fifth argument"); |
|
180 return retval; |
|
181 } |
2928
|
182 |
3243
|
183 crit_times_set = 1; |
|
184 } |
2928
|
185 |
3243
|
186 if (state.capacity () != deriv.capacity ()) |
|
187 { |
|
188 error ("dassl: x and xdot must have the same size"); |
|
189 return retval; |
|
190 } |
|
191 |
|
192 double tzero = out_times (0); |
2928
|
193 |
3243
|
194 DAEFunc func (dassl_user_function); |
|
195 DASSL dae (state, deriv, tzero, func); |
|
196 dae.copy (dassl_opts); |
2928
|
197 |
3243
|
198 Matrix output; |
|
199 Matrix deriv_output; |
|
200 |
|
201 if (crit_times_set) |
|
202 output = dae.integrate (out_times, deriv_output, crit_times); |
|
203 else |
|
204 output = dae.integrate (out_times, deriv_output); |
2928
|
205 |
3243
|
206 if (! error_state) |
|
207 { |
|
208 retval.resize (2); |
2928
|
209 |
3243
|
210 retval(0) = output; |
|
211 retval(1) = deriv_output; |
|
212 } |
2928
|
213 } |
3243
|
214 else |
|
215 print_usage ("dassl"); |
|
216 |
|
217 unwind_protect::run_frame ("Fdassl"); |
2928
|
218 |
|
219 return retval; |
|
220 } |
|
221 |
|
222 typedef void (DASSL_options::*d_set_opt_mf) (double); |
|
223 typedef double (DASSL_options::*d_get_opt_mf) (void); |
|
224 |
|
225 #define MAX_TOKENS 3 |
|
226 |
|
227 struct DASSL_OPTIONS |
|
228 { |
|
229 const char *keyword; |
|
230 const char *kw_tok[MAX_TOKENS + 1]; |
|
231 int min_len[MAX_TOKENS + 1]; |
|
232 int min_toks_to_match; |
|
233 d_set_opt_mf d_set_fcn; |
|
234 d_get_opt_mf d_get_fcn; |
|
235 }; |
|
236 |
|
237 static DASSL_OPTIONS dassl_option_table [] = |
|
238 { |
|
239 { "absolute tolerance", |
|
240 { "absolute", "tolerance", 0, 0, }, |
|
241 { 1, 0, 0, 0, }, 1, |
3131
|
242 &DASSL_options::set_absolute_tolerance, |
|
243 &DASSL_options::absolute_tolerance, }, |
2928
|
244 |
|
245 { "initial step size", |
|
246 { "initial", "step", "size", 0, }, |
|
247 { 1, 0, 0, 0, }, 1, |
3131
|
248 &DASSL_options::set_initial_step_size, |
|
249 &DASSL_options::initial_step_size, }, |
2928
|
250 |
|
251 { "maximum step size", |
|
252 { "maximum", "step", "size", 0, }, |
|
253 { 2, 0, 0, 0, }, 1, |
3131
|
254 &DASSL_options::set_maximum_step_size, |
|
255 &DASSL_options::maximum_step_size, }, |
2928
|
256 |
|
257 { "relative tolerance", |
|
258 { "relative", "tolerance", 0, 0, }, |
|
259 { 1, 0, 0, 0, }, 1, |
3131
|
260 &DASSL_options::set_relative_tolerance, |
|
261 &DASSL_options::relative_tolerance, }, |
2928
|
262 |
|
263 { 0, |
|
264 { 0, 0, 0, 0, }, |
|
265 { 0, 0, 0, 0, }, 0, |
|
266 0, 0, }, |
|
267 }; |
|
268 |
|
269 static void |
|
270 print_dassl_option_list (ostream& os) |
|
271 { |
|
272 print_usage ("dassl_options", 1); |
|
273 |
|
274 os << "\n" |
|
275 << "Options for dassl include:\n\n" |
|
276 << " keyword value\n" |
|
277 << " ------- -----\n\n"; |
|
278 |
|
279 DASSL_OPTIONS *list = dassl_option_table; |
|
280 |
|
281 const char *keyword; |
|
282 while ((keyword = list->keyword) != 0) |
|
283 { |
|
284 os.form (" %-40s ", keyword); |
|
285 |
|
286 double val = (dassl_opts.*list->d_get_fcn) (); |
|
287 if (val < 0.0) |
|
288 os << "computed automatically"; |
|
289 else |
|
290 os << val; |
|
291 |
|
292 os << "\n"; |
|
293 list++; |
|
294 } |
|
295 |
|
296 os << "\n"; |
|
297 } |
|
298 |
|
299 static void |
|
300 set_dassl_option (const string& keyword, double val) |
|
301 { |
|
302 DASSL_OPTIONS *list = dassl_option_table; |
|
303 |
|
304 while (list->keyword != 0) |
|
305 { |
|
306 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
307 list->min_toks_to_match, MAX_TOKENS)) |
|
308 { |
|
309 (dassl_opts.*list->d_set_fcn) (val); |
|
310 |
|
311 return; |
|
312 } |
|
313 list++; |
|
314 } |
|
315 |
|
316 warning ("dassl_options: no match for `%s'", keyword.c_str ()); |
|
317 } |
|
318 |
|
319 static octave_value_list |
|
320 show_dassl_option (const string& keyword) |
|
321 { |
|
322 octave_value retval; |
|
323 |
|
324 DASSL_OPTIONS *list = dassl_option_table; |
|
325 |
|
326 while (list->keyword != 0) |
|
327 { |
|
328 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
329 list->min_toks_to_match, MAX_TOKENS)) |
|
330 { |
|
331 double val = (dassl_opts.*list->d_get_fcn) (); |
|
332 if (val < 0.0) |
|
333 retval = "computed automatically"; |
|
334 else |
|
335 retval = val; |
|
336 |
|
337 return retval; |
|
338 } |
|
339 list++; |
|
340 } |
|
341 |
|
342 warning ("dassl_options: no match for `%s'", keyword.c_str ()); |
|
343 |
|
344 return retval; |
|
345 } |
|
346 |
|
347 DEFUN_DLD (dassl_options, args, , |
|
348 "dassl_options (KEYWORD, VALUE)\n\ |
|
349 \n\ |
|
350 Set or show options for dassl. Keywords may be abbreviated\n\ |
|
351 to the shortest match.") |
|
352 { |
|
353 octave_value_list retval; |
|
354 |
|
355 int nargin = args.length (); |
|
356 |
|
357 if (nargin == 0) |
|
358 { |
|
359 print_dassl_option_list (octave_stdout); |
|
360 return retval; |
|
361 } |
|
362 else if (nargin == 1 || nargin == 2) |
|
363 { |
|
364 string keyword = args(0).string_value (); |
|
365 |
|
366 if (! error_state) |
|
367 { |
|
368 if (nargin == 1) |
|
369 return show_dassl_option (keyword); |
|
370 else |
|
371 { |
|
372 double val = args(1).double_value (); |
|
373 |
|
374 if (! error_state) |
|
375 { |
|
376 set_dassl_option (keyword, val); |
|
377 return retval; |
|
378 } |
|
379 } |
|
380 } |
|
381 } |
|
382 |
|
383 print_usage ("dassl_options"); |
|
384 |
|
385 return retval; |
|
386 } |
|
387 |
|
388 /* |
|
389 ;;; Local Variables: *** |
|
390 ;;; mode: C++ *** |
|
391 ;;; End: *** |
|
392 */ |