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 |
3567
|
29 #include <iomanip> |
3523
|
30 #include <iostream> |
2928
|
31 |
|
32 #include "DASSL.h" |
|
33 |
|
34 #include "defun-dld.h" |
|
35 #include "error.h" |
|
36 #include "gripes.h" |
|
37 #include "oct-obj.h" |
2968
|
38 #include "ov-fcn.h" |
2928
|
39 #include "pager.h" |
3243
|
40 #include "unwind-prot.h" |
2928
|
41 #include "utils.h" |
|
42 #include "variables.h" |
|
43 |
|
44 // Global pointer for user defined function required by dassl. |
2968
|
45 static octave_function *dassl_fcn; |
2928
|
46 |
|
47 static DASSL_options dassl_opts; |
|
48 |
3243
|
49 // Is this a recursive call? |
|
50 static int call_depth = 0; |
|
51 |
2928
|
52 ColumnVector |
3849
|
53 dassl_user_function (const ColumnVector& x, const ColumnVector& xdot, |
|
54 double t, int& ires) |
2928
|
55 { |
|
56 ColumnVector retval; |
|
57 |
|
58 int nstates = x.capacity (); |
|
59 |
|
60 assert (nstates == xdot.capacity ()); |
|
61 |
|
62 octave_value_list args; |
|
63 args(2) = t; |
|
64 |
|
65 if (nstates > 1) |
|
66 { |
|
67 Matrix m1 (nstates, 1); |
|
68 Matrix m2 (nstates, 1); |
|
69 for (int i = 0; i < nstates; i++) |
|
70 { |
|
71 m1 (i, 0) = x (i); |
|
72 m2 (i, 0) = xdot (i); |
|
73 } |
|
74 octave_value state (m1); |
|
75 octave_value deriv (m2); |
|
76 args(1) = deriv; |
|
77 args(0) = state; |
|
78 } |
|
79 else |
|
80 { |
|
81 double d1 = x (0); |
|
82 double d2 = xdot (0); |
|
83 octave_value state (d1); |
|
84 octave_value deriv (d2); |
|
85 args(1) = deriv; |
|
86 args(0) = state; |
|
87 } |
|
88 |
|
89 if (dassl_fcn) |
|
90 { |
3544
|
91 octave_value_list tmp = dassl_fcn->do_multi_index_op (1, args); |
2928
|
92 |
|
93 if (error_state) |
|
94 { |
|
95 gripe_user_supplied_eval ("dassl"); |
|
96 return retval; |
|
97 } |
|
98 |
3849
|
99 int tlen = tmp.length (); |
|
100 if (tlen > 0 && tmp(0).is_defined ()) |
2928
|
101 { |
3419
|
102 retval = ColumnVector (tmp(0).vector_value ()); |
2928
|
103 |
3849
|
104 if (tlen > 1) |
|
105 ires = tmp(1).int_value (); |
|
106 |
2928
|
107 if (error_state || retval.length () == 0) |
|
108 gripe_user_supplied_eval ("dassl"); |
|
109 } |
|
110 else |
|
111 gripe_user_supplied_eval ("dassl"); |
|
112 } |
|
113 |
|
114 return retval; |
|
115 } |
|
116 |
3323
|
117 #define DASSL_ABORT() \ |
|
118 do \ |
|
119 { \ |
|
120 unwind_protect::run_frame ("Fdassl"); \ |
|
121 return retval; \ |
|
122 } \ |
|
123 while (0) |
|
124 |
|
125 #define DASSL_ABORT1(msg) \ |
|
126 do \ |
|
127 { \ |
3747
|
128 ::error ("dassl: " msg); \ |
3323
|
129 DASSL_ABORT (); \ |
|
130 } \ |
|
131 while (0) |
|
132 |
|
133 #define DASSL_ABORT2(fmt, arg) \ |
|
134 do \ |
|
135 { \ |
3747
|
136 ::error ("dassl: " fmt, arg); \ |
3323
|
137 DASSL_ABORT (); \ |
|
138 } \ |
|
139 while (0) |
|
140 |
2928
|
141 DEFUN_DLD (dassl, args, , |
3373
|
142 "-*- texinfo -*-\n\ |
|
143 @deftypefn {Loadable Function} {[@var{x}, @var{xdot}] =} dassl (@var{fcn}, @var{x0}, @var{xdot0}, @var{t}, @var{t_crit})\n\ |
|
144 Return a matrix of states and their first derivatives with respect to\n\ |
|
145 @var{t}. Each row in the result matrices correspond to one of the\n\ |
|
146 elements in the vector @var{t}. The first element of @var{t}\n\ |
|
147 corresponds to the initial state @var{x0} and derivative @var{xdot0}, so\n\ |
|
148 that the first row of the output @var{x} is @var{x0} and the first row\n\ |
|
149 of the output @var{xdot} is @var{xdot0}.\n\ |
|
150 \n\ |
|
151 The first argument, @var{fcn}, is a string that names the function to\n\ |
|
152 call to compute the vector of residuals for the set of equations.\n\ |
|
153 It must have the form\n\ |
|
154 \n\ |
|
155 @example\n\ |
|
156 @var{res} = f (@var{x}, @var{xdot}, @var{t})\n\ |
|
157 @end example\n\ |
2928
|
158 \n\ |
3373
|
159 @noindent\n\ |
|
160 where @var{x}, @var{xdot}, and @var{res} are vectors, and @var{t} is a\n\ |
|
161 scalar.\n\ |
|
162 \n\ |
|
163 The second and third arguments to @code{dassl} specify the initial\n\ |
|
164 condition of the states and their derivatives, and the fourth argument\n\ |
|
165 specifies a vector of output times at which the solution is desired, \n\ |
|
166 including the time corresponding to the initial condition.\n\ |
2928
|
167 \n\ |
3373
|
168 The set of initial states and derivatives are not strictly required to\n\ |
|
169 be consistent. In practice, however, @sc{Dassl} is not very good at\n\ |
|
170 determining a consistent set for you, so it is best if you ensure that\n\ |
|
171 the initial values result in the function evaluating to zero.\n\ |
2928
|
172 \n\ |
3373
|
173 The fifth argument is optional, and may be used to specify a set of\n\ |
|
174 times that the DAE solver should not integrate past. It is useful for\n\ |
|
175 avoiding difficulties with singularities and points where there is a\n\ |
|
176 discontinuity in the derivative.\n\ |
3964
|
177 \n\ |
|
178 You can use the function @code{dassl_options} to set optional\n\ |
|
179 parameters for @code{dassl}.\n\ |
3373
|
180 @end deftypefn") |
2928
|
181 { |
|
182 octave_value_list retval; |
|
183 |
3243
|
184 unwind_protect::begin_frame ("Fdassl"); |
2928
|
185 |
3243
|
186 unwind_protect_int (call_depth); |
|
187 call_depth++; |
2928
|
188 |
3243
|
189 if (call_depth > 1) |
3323
|
190 DASSL_ABORT1 ("invalid recursive call"); |
2928
|
191 |
3243
|
192 int nargin = args.length (); |
|
193 |
|
194 if (nargin > 3 && nargin < 6) |
2928
|
195 { |
3243
|
196 dassl_fcn = extract_function |
|
197 (args(0), "dassl", "__dassl_fcn__", |
|
198 "function res = __dassl_fcn__ (x, xdot, t) res = ", |
|
199 "; endfunction"); |
|
200 |
|
201 if (! dassl_fcn) |
3323
|
202 DASSL_ABORT (); |
3243
|
203 |
3419
|
204 ColumnVector state = ColumnVector (args(1).vector_value ()); |
2928
|
205 |
|
206 if (error_state) |
3323
|
207 DASSL_ABORT1 ("expecting state vector as second argument"); |
3243
|
208 |
3419
|
209 ColumnVector deriv (args(2).vector_value ()); |
3243
|
210 |
|
211 if (error_state) |
3323
|
212 DASSL_ABORT1 ("expecting derivative vector as third argument"); |
3243
|
213 |
3419
|
214 ColumnVector out_times (args(3).vector_value ()); |
3243
|
215 |
|
216 if (error_state) |
3323
|
217 DASSL_ABORT1 ("expecting output time vector as fourth argument"); |
2928
|
218 |
3243
|
219 ColumnVector crit_times; |
|
220 int crit_times_set = 0; |
|
221 if (nargin > 4) |
|
222 { |
3419
|
223 crit_times = ColumnVector (args(4).vector_value ()); |
2928
|
224 |
3243
|
225 if (error_state) |
3323
|
226 DASSL_ABORT1 ("expecting critical time vector as fifth argument"); |
2928
|
227 |
3243
|
228 crit_times_set = 1; |
|
229 } |
2928
|
230 |
3243
|
231 if (state.capacity () != deriv.capacity ()) |
3323
|
232 DASSL_ABORT1 ("x and xdot must have the same size"); |
3243
|
233 |
|
234 double tzero = out_times (0); |
2928
|
235 |
3243
|
236 DAEFunc func (dassl_user_function); |
|
237 DASSL dae (state, deriv, tzero, func); |
|
238 dae.copy (dassl_opts); |
2928
|
239 |
3243
|
240 Matrix output; |
|
241 Matrix deriv_output; |
|
242 |
|
243 if (crit_times_set) |
|
244 output = dae.integrate (out_times, deriv_output, crit_times); |
|
245 else |
|
246 output = dae.integrate (out_times, deriv_output); |
2928
|
247 |
3243
|
248 if (! error_state) |
|
249 { |
|
250 retval.resize (2); |
2928
|
251 |
3243
|
252 retval(0) = output; |
|
253 retval(1) = deriv_output; |
|
254 } |
2928
|
255 } |
3243
|
256 else |
|
257 print_usage ("dassl"); |
|
258 |
|
259 unwind_protect::run_frame ("Fdassl"); |
2928
|
260 |
|
261 return retval; |
|
262 } |
|
263 |
|
264 typedef void (DASSL_options::*d_set_opt_mf) (double); |
|
265 typedef double (DASSL_options::*d_get_opt_mf) (void); |
|
266 |
|
267 #define MAX_TOKENS 3 |
|
268 |
|
269 struct DASSL_OPTIONS |
|
270 { |
|
271 const char *keyword; |
|
272 const char *kw_tok[MAX_TOKENS + 1]; |
|
273 int min_len[MAX_TOKENS + 1]; |
|
274 int min_toks_to_match; |
|
275 d_set_opt_mf d_set_fcn; |
|
276 d_get_opt_mf d_get_fcn; |
|
277 }; |
|
278 |
|
279 static DASSL_OPTIONS dassl_option_table [] = |
|
280 { |
|
281 { "absolute tolerance", |
|
282 { "absolute", "tolerance", 0, 0, }, |
|
283 { 1, 0, 0, 0, }, 1, |
3131
|
284 &DASSL_options::set_absolute_tolerance, |
|
285 &DASSL_options::absolute_tolerance, }, |
2928
|
286 |
|
287 { "initial step size", |
|
288 { "initial", "step", "size", 0, }, |
|
289 { 1, 0, 0, 0, }, 1, |
3131
|
290 &DASSL_options::set_initial_step_size, |
|
291 &DASSL_options::initial_step_size, }, |
2928
|
292 |
|
293 { "maximum step size", |
|
294 { "maximum", "step", "size", 0, }, |
|
295 { 2, 0, 0, 0, }, 1, |
3131
|
296 &DASSL_options::set_maximum_step_size, |
|
297 &DASSL_options::maximum_step_size, }, |
2928
|
298 |
|
299 { "relative tolerance", |
|
300 { "relative", "tolerance", 0, 0, }, |
|
301 { 1, 0, 0, 0, }, 1, |
3131
|
302 &DASSL_options::set_relative_tolerance, |
|
303 &DASSL_options::relative_tolerance, }, |
2928
|
304 |
|
305 { 0, |
|
306 { 0, 0, 0, 0, }, |
|
307 { 0, 0, 0, 0, }, 0, |
|
308 0, 0, }, |
|
309 }; |
|
310 |
|
311 static void |
3523
|
312 print_dassl_option_list (std::ostream& os) |
2928
|
313 { |
|
314 print_usage ("dassl_options", 1); |
|
315 |
|
316 os << "\n" |
|
317 << "Options for dassl include:\n\n" |
|
318 << " keyword value\n" |
|
319 << " ------- -----\n\n"; |
|
320 |
|
321 DASSL_OPTIONS *list = dassl_option_table; |
|
322 |
|
323 const char *keyword; |
|
324 while ((keyword = list->keyword) != 0) |
|
325 { |
3568
|
326 os << " " |
|
327 << std::setiosflags (std::ios::left) << std::setw (40) |
|
328 << keyword |
|
329 << std::resetiosflags (std::ios::left) |
|
330 << " "; |
2928
|
331 |
|
332 double val = (dassl_opts.*list->d_get_fcn) (); |
|
333 if (val < 0.0) |
|
334 os << "computed automatically"; |
|
335 else |
|
336 os << val; |
|
337 |
|
338 os << "\n"; |
|
339 list++; |
|
340 } |
|
341 |
|
342 os << "\n"; |
|
343 } |
|
344 |
|
345 static void |
3523
|
346 set_dassl_option (const std::string& keyword, double val) |
2928
|
347 { |
|
348 DASSL_OPTIONS *list = dassl_option_table; |
|
349 |
|
350 while (list->keyword != 0) |
|
351 { |
|
352 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
353 list->min_toks_to_match, MAX_TOKENS)) |
|
354 { |
|
355 (dassl_opts.*list->d_set_fcn) (val); |
|
356 |
|
357 return; |
|
358 } |
|
359 list++; |
|
360 } |
|
361 |
|
362 warning ("dassl_options: no match for `%s'", keyword.c_str ()); |
|
363 } |
|
364 |
|
365 static octave_value_list |
3523
|
366 show_dassl_option (const std::string& keyword) |
2928
|
367 { |
|
368 octave_value retval; |
|
369 |
|
370 DASSL_OPTIONS *list = dassl_option_table; |
|
371 |
|
372 while (list->keyword != 0) |
|
373 { |
|
374 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
375 list->min_toks_to_match, MAX_TOKENS)) |
|
376 { |
|
377 double val = (dassl_opts.*list->d_get_fcn) (); |
|
378 if (val < 0.0) |
|
379 retval = "computed automatically"; |
|
380 else |
|
381 retval = val; |
|
382 |
|
383 return retval; |
|
384 } |
|
385 list++; |
|
386 } |
|
387 |
|
388 warning ("dassl_options: no match for `%s'", keyword.c_str ()); |
|
389 |
|
390 return retval; |
|
391 } |
|
392 |
|
393 DEFUN_DLD (dassl_options, args, , |
3373
|
394 "-*- texinfo -*-\n\ |
|
395 @deftypefn {Loadable Function} {} dassl_options (@var{opt}, @var{val})\n\ |
|
396 When called with two arguments, this function allows you set options\n\ |
|
397 parameters for the function @code{lsode}. Given one argument,\n\ |
|
398 @code{dassl_options} returns the value of the corresponding option. If\n\ |
|
399 no arguments are supplied, the names of all the available options and\n\ |
|
400 their current values are displayed.\n\ |
|
401 @end deftypefn") |
2928
|
402 { |
|
403 octave_value_list retval; |
|
404 |
|
405 int nargin = args.length (); |
|
406 |
|
407 if (nargin == 0) |
|
408 { |
|
409 print_dassl_option_list (octave_stdout); |
|
410 return retval; |
|
411 } |
|
412 else if (nargin == 1 || nargin == 2) |
|
413 { |
3523
|
414 std::string keyword = args(0).string_value (); |
2928
|
415 |
|
416 if (! error_state) |
|
417 { |
|
418 if (nargin == 1) |
|
419 return show_dassl_option (keyword); |
|
420 else |
|
421 { |
|
422 double val = args(1).double_value (); |
|
423 |
|
424 if (! error_state) |
|
425 { |
|
426 set_dassl_option (keyword, val); |
|
427 return retval; |
|
428 } |
|
429 } |
|
430 } |
|
431 } |
|
432 |
|
433 print_usage ("dassl_options"); |
|
434 |
|
435 return retval; |
|
436 } |
|
437 |
|
438 /* |
|
439 ;;; Local Variables: *** |
|
440 ;;; mode: C++ *** |
|
441 ;;; End: *** |
|
442 */ |