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 |
3323
|
111 #define DASSL_ABORT() \ |
|
112 do \ |
|
113 { \ |
|
114 unwind_protect::run_frame ("Fdassl"); \ |
|
115 return retval; \ |
|
116 } \ |
|
117 while (0) |
|
118 |
|
119 #define DASSL_ABORT1(msg) \ |
|
120 do \ |
|
121 { \ |
|
122 ::error ("dassl: " ## msg); \ |
|
123 DASSL_ABORT (); \ |
|
124 } \ |
|
125 while (0) |
|
126 |
|
127 #define DASSL_ABORT2(fmt, arg) \ |
|
128 do \ |
|
129 { \ |
|
130 ::error ("dassl: " ## fmt, arg); \ |
|
131 DASSL_ABORT (); \ |
|
132 } \ |
|
133 while (0) |
|
134 |
2928
|
135 DEFUN_DLD (dassl, args, , |
|
136 "dassl (\"function_name\", x_0, xdot_0, t_out)\n\ |
|
137 dassl (F, X_0, XDOT_0, T_OUT, T_CRIT)\n\ |
|
138 \n\ |
|
139 The first argument is the name of the function to call to\n\ |
|
140 compute the vector of residuals. It must have the form\n\ |
|
141 \n\ |
|
142 res = f (x, xdot, t)\n\ |
|
143 \n\ |
|
144 where x, xdot, and res are vectors, and t is a scalar.") |
|
145 { |
|
146 octave_value_list retval; |
|
147 |
3243
|
148 unwind_protect::begin_frame ("Fdassl"); |
2928
|
149 |
3243
|
150 unwind_protect_int (call_depth); |
|
151 call_depth++; |
2928
|
152 |
3243
|
153 if (call_depth > 1) |
3323
|
154 DASSL_ABORT1 ("invalid recursive call"); |
2928
|
155 |
3243
|
156 int nargin = args.length (); |
|
157 |
|
158 if (nargin > 3 && nargin < 6) |
2928
|
159 { |
3243
|
160 dassl_fcn = extract_function |
|
161 (args(0), "dassl", "__dassl_fcn__", |
|
162 "function res = __dassl_fcn__ (x, xdot, t) res = ", |
|
163 "; endfunction"); |
|
164 |
|
165 if (! dassl_fcn) |
3323
|
166 DASSL_ABORT (); |
3243
|
167 |
|
168 ColumnVector state = args(1).vector_value (); |
2928
|
169 |
|
170 if (error_state) |
3323
|
171 DASSL_ABORT1 ("expecting state vector as second argument"); |
3243
|
172 |
|
173 ColumnVector deriv = args(2).vector_value (); |
|
174 |
|
175 if (error_state) |
3323
|
176 DASSL_ABORT1 ("expecting derivative vector as third argument"); |
3243
|
177 |
|
178 ColumnVector out_times = args(3).vector_value (); |
|
179 |
|
180 if (error_state) |
3323
|
181 DASSL_ABORT1 ("expecting output time vector as fourth argument"); |
2928
|
182 |
3243
|
183 ColumnVector crit_times; |
|
184 int crit_times_set = 0; |
|
185 if (nargin > 4) |
|
186 { |
|
187 crit_times = args(4).vector_value (); |
2928
|
188 |
3243
|
189 if (error_state) |
3323
|
190 DASSL_ABORT1 ("expecting critical time vector as fifth argument"); |
2928
|
191 |
3243
|
192 crit_times_set = 1; |
|
193 } |
2928
|
194 |
3243
|
195 if (state.capacity () != deriv.capacity ()) |
3323
|
196 DASSL_ABORT1 ("x and xdot must have the same size"); |
3243
|
197 |
|
198 double tzero = out_times (0); |
2928
|
199 |
3243
|
200 DAEFunc func (dassl_user_function); |
|
201 DASSL dae (state, deriv, tzero, func); |
|
202 dae.copy (dassl_opts); |
2928
|
203 |
3243
|
204 Matrix output; |
|
205 Matrix deriv_output; |
|
206 |
|
207 if (crit_times_set) |
|
208 output = dae.integrate (out_times, deriv_output, crit_times); |
|
209 else |
|
210 output = dae.integrate (out_times, deriv_output); |
2928
|
211 |
3243
|
212 if (! error_state) |
|
213 { |
|
214 retval.resize (2); |
2928
|
215 |
3243
|
216 retval(0) = output; |
|
217 retval(1) = deriv_output; |
|
218 } |
2928
|
219 } |
3243
|
220 else |
|
221 print_usage ("dassl"); |
|
222 |
|
223 unwind_protect::run_frame ("Fdassl"); |
2928
|
224 |
|
225 return retval; |
|
226 } |
|
227 |
|
228 typedef void (DASSL_options::*d_set_opt_mf) (double); |
|
229 typedef double (DASSL_options::*d_get_opt_mf) (void); |
|
230 |
|
231 #define MAX_TOKENS 3 |
|
232 |
|
233 struct DASSL_OPTIONS |
|
234 { |
|
235 const char *keyword; |
|
236 const char *kw_tok[MAX_TOKENS + 1]; |
|
237 int min_len[MAX_TOKENS + 1]; |
|
238 int min_toks_to_match; |
|
239 d_set_opt_mf d_set_fcn; |
|
240 d_get_opt_mf d_get_fcn; |
|
241 }; |
|
242 |
|
243 static DASSL_OPTIONS dassl_option_table [] = |
|
244 { |
|
245 { "absolute tolerance", |
|
246 { "absolute", "tolerance", 0, 0, }, |
|
247 { 1, 0, 0, 0, }, 1, |
3131
|
248 &DASSL_options::set_absolute_tolerance, |
|
249 &DASSL_options::absolute_tolerance, }, |
2928
|
250 |
|
251 { "initial step size", |
|
252 { "initial", "step", "size", 0, }, |
|
253 { 1, 0, 0, 0, }, 1, |
3131
|
254 &DASSL_options::set_initial_step_size, |
|
255 &DASSL_options::initial_step_size, }, |
2928
|
256 |
|
257 { "maximum step size", |
|
258 { "maximum", "step", "size", 0, }, |
|
259 { 2, 0, 0, 0, }, 1, |
3131
|
260 &DASSL_options::set_maximum_step_size, |
|
261 &DASSL_options::maximum_step_size, }, |
2928
|
262 |
|
263 { "relative tolerance", |
|
264 { "relative", "tolerance", 0, 0, }, |
|
265 { 1, 0, 0, 0, }, 1, |
3131
|
266 &DASSL_options::set_relative_tolerance, |
|
267 &DASSL_options::relative_tolerance, }, |
2928
|
268 |
|
269 { 0, |
|
270 { 0, 0, 0, 0, }, |
|
271 { 0, 0, 0, 0, }, 0, |
|
272 0, 0, }, |
|
273 }; |
|
274 |
|
275 static void |
|
276 print_dassl_option_list (ostream& os) |
|
277 { |
|
278 print_usage ("dassl_options", 1); |
|
279 |
|
280 os << "\n" |
|
281 << "Options for dassl include:\n\n" |
|
282 << " keyword value\n" |
|
283 << " ------- -----\n\n"; |
|
284 |
|
285 DASSL_OPTIONS *list = dassl_option_table; |
|
286 |
|
287 const char *keyword; |
|
288 while ((keyword = list->keyword) != 0) |
|
289 { |
|
290 os.form (" %-40s ", keyword); |
|
291 |
|
292 double val = (dassl_opts.*list->d_get_fcn) (); |
|
293 if (val < 0.0) |
|
294 os << "computed automatically"; |
|
295 else |
|
296 os << val; |
|
297 |
|
298 os << "\n"; |
|
299 list++; |
|
300 } |
|
301 |
|
302 os << "\n"; |
|
303 } |
|
304 |
|
305 static void |
|
306 set_dassl_option (const string& keyword, double val) |
|
307 { |
|
308 DASSL_OPTIONS *list = dassl_option_table; |
|
309 |
|
310 while (list->keyword != 0) |
|
311 { |
|
312 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
313 list->min_toks_to_match, MAX_TOKENS)) |
|
314 { |
|
315 (dassl_opts.*list->d_set_fcn) (val); |
|
316 |
|
317 return; |
|
318 } |
|
319 list++; |
|
320 } |
|
321 |
|
322 warning ("dassl_options: no match for `%s'", keyword.c_str ()); |
|
323 } |
|
324 |
|
325 static octave_value_list |
|
326 show_dassl_option (const string& keyword) |
|
327 { |
|
328 octave_value retval; |
|
329 |
|
330 DASSL_OPTIONS *list = dassl_option_table; |
|
331 |
|
332 while (list->keyword != 0) |
|
333 { |
|
334 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
335 list->min_toks_to_match, MAX_TOKENS)) |
|
336 { |
|
337 double val = (dassl_opts.*list->d_get_fcn) (); |
|
338 if (val < 0.0) |
|
339 retval = "computed automatically"; |
|
340 else |
|
341 retval = val; |
|
342 |
|
343 return retval; |
|
344 } |
|
345 list++; |
|
346 } |
|
347 |
|
348 warning ("dassl_options: no match for `%s'", keyword.c_str ()); |
|
349 |
|
350 return retval; |
|
351 } |
|
352 |
|
353 DEFUN_DLD (dassl_options, args, , |
|
354 "dassl_options (KEYWORD, VALUE)\n\ |
|
355 \n\ |
|
356 Set or show options for dassl. Keywords may be abbreviated\n\ |
|
357 to the shortest match.") |
|
358 { |
|
359 octave_value_list retval; |
|
360 |
|
361 int nargin = args.length (); |
|
362 |
|
363 if (nargin == 0) |
|
364 { |
|
365 print_dassl_option_list (octave_stdout); |
|
366 return retval; |
|
367 } |
|
368 else if (nargin == 1 || nargin == 2) |
|
369 { |
|
370 string keyword = args(0).string_value (); |
|
371 |
|
372 if (! error_state) |
|
373 { |
|
374 if (nargin == 1) |
|
375 return show_dassl_option (keyword); |
|
376 else |
|
377 { |
|
378 double val = args(1).double_value (); |
|
379 |
|
380 if (! error_state) |
|
381 { |
|
382 set_dassl_option (keyword, val); |
|
383 return retval; |
|
384 } |
|
385 } |
|
386 } |
|
387 } |
|
388 |
|
389 print_usage ("dassl_options"); |
|
390 |
|
391 return retval; |
|
392 } |
|
393 |
|
394 /* |
|
395 ;;; Local Variables: *** |
|
396 ;;; mode: C++ *** |
|
397 ;;; End: *** |
|
398 */ |