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