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 |
3523
|
29 #include <iostream> |
2928
|
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 { |
3419
|
99 retval = ColumnVector (tmp(0).vector_value ()); |
2928
|
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, , |
3373
|
136 "-*- texinfo -*-\n\ |
|
137 @deftypefn {Loadable Function} {[@var{x}, @var{xdot}] =} dassl (@var{fcn}, @var{x0}, @var{xdot0}, @var{t}, @var{t_crit})\n\ |
|
138 Return a matrix of states and their first derivatives with respect to\n\ |
|
139 @var{t}. Each row in the result matrices correspond to one of the\n\ |
|
140 elements in the vector @var{t}. The first element of @var{t}\n\ |
|
141 corresponds to the initial state @var{x0} and derivative @var{xdot0}, so\n\ |
|
142 that the first row of the output @var{x} is @var{x0} and the first row\n\ |
|
143 of the output @var{xdot} is @var{xdot0}.\n\ |
|
144 \n\ |
|
145 The first argument, @var{fcn}, is a string that names the function to\n\ |
|
146 call to compute the vector of residuals for the set of equations.\n\ |
|
147 It must have the form\n\ |
|
148 \n\ |
|
149 @example\n\ |
|
150 @var{res} = f (@var{x}, @var{xdot}, @var{t})\n\ |
|
151 @end example\n\ |
2928
|
152 \n\ |
3373
|
153 @noindent\n\ |
|
154 where @var{x}, @var{xdot}, and @var{res} are vectors, and @var{t} is a\n\ |
|
155 scalar.\n\ |
|
156 \n\ |
|
157 The second and third arguments to @code{dassl} specify the initial\n\ |
|
158 condition of the states and their derivatives, and the fourth argument\n\ |
|
159 specifies a vector of output times at which the solution is desired, \n\ |
|
160 including the time corresponding to the initial condition.\n\ |
2928
|
161 \n\ |
3373
|
162 The set of initial states and derivatives are not strictly required to\n\ |
|
163 be consistent. In practice, however, @sc{Dassl} is not very good at\n\ |
|
164 determining a consistent set for you, so it is best if you ensure that\n\ |
|
165 the initial values result in the function evaluating to zero.\n\ |
2928
|
166 \n\ |
3373
|
167 The fifth argument is optional, and may be used to specify a set of\n\ |
|
168 times that the DAE solver should not integrate past. It is useful for\n\ |
|
169 avoiding difficulties with singularities and points where there is a\n\ |
|
170 discontinuity in the derivative.\n\ |
|
171 @end deftypefn") |
2928
|
172 { |
|
173 octave_value_list retval; |
|
174 |
3243
|
175 unwind_protect::begin_frame ("Fdassl"); |
2928
|
176 |
3243
|
177 unwind_protect_int (call_depth); |
|
178 call_depth++; |
2928
|
179 |
3243
|
180 if (call_depth > 1) |
3323
|
181 DASSL_ABORT1 ("invalid recursive call"); |
2928
|
182 |
3243
|
183 int nargin = args.length (); |
|
184 |
|
185 if (nargin > 3 && nargin < 6) |
2928
|
186 { |
3243
|
187 dassl_fcn = extract_function |
|
188 (args(0), "dassl", "__dassl_fcn__", |
|
189 "function res = __dassl_fcn__ (x, xdot, t) res = ", |
|
190 "; endfunction"); |
|
191 |
|
192 if (! dassl_fcn) |
3323
|
193 DASSL_ABORT (); |
3243
|
194 |
3419
|
195 ColumnVector state = ColumnVector (args(1).vector_value ()); |
2928
|
196 |
|
197 if (error_state) |
3323
|
198 DASSL_ABORT1 ("expecting state vector as second argument"); |
3243
|
199 |
3419
|
200 ColumnVector deriv (args(2).vector_value ()); |
3243
|
201 |
|
202 if (error_state) |
3323
|
203 DASSL_ABORT1 ("expecting derivative vector as third argument"); |
3243
|
204 |
3419
|
205 ColumnVector out_times (args(3).vector_value ()); |
3243
|
206 |
|
207 if (error_state) |
3323
|
208 DASSL_ABORT1 ("expecting output time vector as fourth argument"); |
2928
|
209 |
3243
|
210 ColumnVector crit_times; |
|
211 int crit_times_set = 0; |
|
212 if (nargin > 4) |
|
213 { |
3419
|
214 crit_times = ColumnVector (args(4).vector_value ()); |
2928
|
215 |
3243
|
216 if (error_state) |
3323
|
217 DASSL_ABORT1 ("expecting critical time vector as fifth argument"); |
2928
|
218 |
3243
|
219 crit_times_set = 1; |
|
220 } |
2928
|
221 |
3243
|
222 if (state.capacity () != deriv.capacity ()) |
3323
|
223 DASSL_ABORT1 ("x and xdot must have the same size"); |
3243
|
224 |
|
225 double tzero = out_times (0); |
2928
|
226 |
3243
|
227 DAEFunc func (dassl_user_function); |
|
228 DASSL dae (state, deriv, tzero, func); |
|
229 dae.copy (dassl_opts); |
2928
|
230 |
3243
|
231 Matrix output; |
|
232 Matrix deriv_output; |
|
233 |
|
234 if (crit_times_set) |
|
235 output = dae.integrate (out_times, deriv_output, crit_times); |
|
236 else |
|
237 output = dae.integrate (out_times, deriv_output); |
2928
|
238 |
3243
|
239 if (! error_state) |
|
240 { |
|
241 retval.resize (2); |
2928
|
242 |
3243
|
243 retval(0) = output; |
|
244 retval(1) = deriv_output; |
|
245 } |
2928
|
246 } |
3243
|
247 else |
|
248 print_usage ("dassl"); |
|
249 |
|
250 unwind_protect::run_frame ("Fdassl"); |
2928
|
251 |
|
252 return retval; |
|
253 } |
|
254 |
|
255 typedef void (DASSL_options::*d_set_opt_mf) (double); |
|
256 typedef double (DASSL_options::*d_get_opt_mf) (void); |
|
257 |
|
258 #define MAX_TOKENS 3 |
|
259 |
|
260 struct DASSL_OPTIONS |
|
261 { |
|
262 const char *keyword; |
|
263 const char *kw_tok[MAX_TOKENS + 1]; |
|
264 int min_len[MAX_TOKENS + 1]; |
|
265 int min_toks_to_match; |
|
266 d_set_opt_mf d_set_fcn; |
|
267 d_get_opt_mf d_get_fcn; |
|
268 }; |
|
269 |
|
270 static DASSL_OPTIONS dassl_option_table [] = |
|
271 { |
|
272 { "absolute tolerance", |
|
273 { "absolute", "tolerance", 0, 0, }, |
|
274 { 1, 0, 0, 0, }, 1, |
3131
|
275 &DASSL_options::set_absolute_tolerance, |
|
276 &DASSL_options::absolute_tolerance, }, |
2928
|
277 |
|
278 { "initial step size", |
|
279 { "initial", "step", "size", 0, }, |
|
280 { 1, 0, 0, 0, }, 1, |
3131
|
281 &DASSL_options::set_initial_step_size, |
|
282 &DASSL_options::initial_step_size, }, |
2928
|
283 |
|
284 { "maximum step size", |
|
285 { "maximum", "step", "size", 0, }, |
|
286 { 2, 0, 0, 0, }, 1, |
3131
|
287 &DASSL_options::set_maximum_step_size, |
|
288 &DASSL_options::maximum_step_size, }, |
2928
|
289 |
|
290 { "relative tolerance", |
|
291 { "relative", "tolerance", 0, 0, }, |
|
292 { 1, 0, 0, 0, }, 1, |
3131
|
293 &DASSL_options::set_relative_tolerance, |
|
294 &DASSL_options::relative_tolerance, }, |
2928
|
295 |
|
296 { 0, |
|
297 { 0, 0, 0, 0, }, |
|
298 { 0, 0, 0, 0, }, 0, |
|
299 0, 0, }, |
|
300 }; |
|
301 |
|
302 static void |
3523
|
303 print_dassl_option_list (std::ostream& os) |
2928
|
304 { |
|
305 print_usage ("dassl_options", 1); |
|
306 |
|
307 os << "\n" |
|
308 << "Options for dassl include:\n\n" |
|
309 << " keyword value\n" |
|
310 << " ------- -----\n\n"; |
|
311 |
|
312 DASSL_OPTIONS *list = dassl_option_table; |
|
313 |
|
314 const char *keyword; |
|
315 while ((keyword = list->keyword) != 0) |
|
316 { |
|
317 os.form (" %-40s ", keyword); |
|
318 |
|
319 double val = (dassl_opts.*list->d_get_fcn) (); |
|
320 if (val < 0.0) |
|
321 os << "computed automatically"; |
|
322 else |
|
323 os << val; |
|
324 |
|
325 os << "\n"; |
|
326 list++; |
|
327 } |
|
328 |
|
329 os << "\n"; |
|
330 } |
|
331 |
|
332 static void |
3523
|
333 set_dassl_option (const std::string& keyword, double val) |
2928
|
334 { |
|
335 DASSL_OPTIONS *list = dassl_option_table; |
|
336 |
|
337 while (list->keyword != 0) |
|
338 { |
|
339 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
340 list->min_toks_to_match, MAX_TOKENS)) |
|
341 { |
|
342 (dassl_opts.*list->d_set_fcn) (val); |
|
343 |
|
344 return; |
|
345 } |
|
346 list++; |
|
347 } |
|
348 |
|
349 warning ("dassl_options: no match for `%s'", keyword.c_str ()); |
|
350 } |
|
351 |
|
352 static octave_value_list |
3523
|
353 show_dassl_option (const std::string& keyword) |
2928
|
354 { |
|
355 octave_value retval; |
|
356 |
|
357 DASSL_OPTIONS *list = dassl_option_table; |
|
358 |
|
359 while (list->keyword != 0) |
|
360 { |
|
361 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
362 list->min_toks_to_match, MAX_TOKENS)) |
|
363 { |
|
364 double val = (dassl_opts.*list->d_get_fcn) (); |
|
365 if (val < 0.0) |
|
366 retval = "computed automatically"; |
|
367 else |
|
368 retval = val; |
|
369 |
|
370 return retval; |
|
371 } |
|
372 list++; |
|
373 } |
|
374 |
|
375 warning ("dassl_options: no match for `%s'", keyword.c_str ()); |
|
376 |
|
377 return retval; |
|
378 } |
|
379 |
|
380 DEFUN_DLD (dassl_options, args, , |
3373
|
381 "-*- texinfo -*-\n\ |
|
382 @deftypefn {Loadable Function} {} dassl_options (@var{opt}, @var{val})\n\ |
|
383 When called with two arguments, this function allows you set options\n\ |
|
384 parameters for the function @code{lsode}. Given one argument,\n\ |
|
385 @code{dassl_options} returns the value of the corresponding option. If\n\ |
|
386 no arguments are supplied, the names of all the available options and\n\ |
|
387 their current values are displayed.\n\ |
|
388 @end deftypefn") |
2928
|
389 { |
|
390 octave_value_list retval; |
|
391 |
|
392 int nargin = args.length (); |
|
393 |
|
394 if (nargin == 0) |
|
395 { |
|
396 print_dassl_option_list (octave_stdout); |
|
397 return retval; |
|
398 } |
|
399 else if (nargin == 1 || nargin == 2) |
|
400 { |
3523
|
401 std::string keyword = args(0).string_value (); |
2928
|
402 |
|
403 if (! error_state) |
|
404 { |
|
405 if (nargin == 1) |
|
406 return show_dassl_option (keyword); |
|
407 else |
|
408 { |
|
409 double val = args(1).double_value (); |
|
410 |
|
411 if (! error_state) |
|
412 { |
|
413 set_dassl_option (keyword, val); |
|
414 return retval; |
|
415 } |
|
416 } |
|
417 } |
|
418 } |
|
419 |
|
420 print_usage ("dassl_options"); |
|
421 |
|
422 return retval; |
|
423 } |
|
424 |
|
425 /* |
|
426 ;;; Local Variables: *** |
|
427 ;;; mode: C++ *** |
|
428 ;;; End: *** |
|
429 */ |