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 "LSODE.h" |
|
32 #include "lo-mappers.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 lsode. |
2968
|
45 static octave_function *lsode_fcn; |
2928
|
46 |
|
47 // Global pointer for optional user defined jacobian function used by lsode. |
2968
|
48 static octave_function *lsode_jac; |
2928
|
49 |
|
50 static LSODE_options lsode_opts; |
|
51 |
3243
|
52 // Is this a recursive call? |
|
53 static int call_depth = 0; |
|
54 |
2928
|
55 ColumnVector |
|
56 lsode_user_function (const ColumnVector& x, double t) |
|
57 { |
|
58 ColumnVector retval; |
|
59 |
|
60 int nstates = x.capacity (); |
|
61 |
|
62 octave_value_list args; |
|
63 args(1) = t; |
|
64 |
|
65 Matrix m (nstates, 1); |
|
66 for (int i = 0; i < nstates; i++) |
|
67 m (i, 0) = x (i); |
|
68 octave_value state (m); |
|
69 args(0) = state; |
|
70 |
|
71 if (lsode_fcn) |
|
72 { |
2968
|
73 octave_value_list tmp = lsode_fcn->do_index_op (1, args); |
2928
|
74 |
|
75 if (error_state) |
|
76 { |
|
77 gripe_user_supplied_eval ("lsode"); |
|
78 return retval; |
|
79 } |
|
80 |
|
81 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
82 { |
|
83 retval = tmp(0).vector_value (); |
|
84 |
|
85 if (error_state || retval.length () == 0) |
|
86 gripe_user_supplied_eval ("lsode"); |
|
87 } |
|
88 else |
|
89 gripe_user_supplied_eval ("lsode"); |
|
90 } |
|
91 |
|
92 return retval; |
|
93 } |
|
94 |
|
95 Matrix |
|
96 lsode_user_jacobian (const ColumnVector& x, double t) |
|
97 { |
|
98 Matrix retval; |
|
99 |
|
100 int nstates = x.capacity (); |
|
101 |
|
102 octave_value_list args; |
|
103 args(1) = t; |
|
104 |
|
105 Matrix m (nstates, 1); |
|
106 for (int i = 0; i < nstates; i++) |
|
107 m (i, 0) = x (i); |
|
108 octave_value state (m); |
|
109 args(0) = state; |
|
110 |
|
111 if (lsode_jac) |
|
112 { |
2968
|
113 octave_value_list tmp = lsode_jac->do_index_op (1, args); |
2928
|
114 |
|
115 if (error_state) |
|
116 { |
|
117 gripe_user_supplied_eval ("lsode"); |
|
118 return retval; |
|
119 } |
|
120 |
|
121 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
122 { |
|
123 retval = tmp(0).matrix_value (); |
|
124 |
|
125 if (error_state || retval.length () == 0) |
|
126 gripe_user_supplied_eval ("lsode"); |
|
127 } |
|
128 else |
|
129 gripe_user_supplied_eval ("lsode"); |
|
130 } |
|
131 |
|
132 return retval; |
|
133 } |
|
134 |
3323
|
135 #define LSODE_ABORT() \ |
|
136 do \ |
|
137 { \ |
|
138 unwind_protect::run_frame ("Flsode"); \ |
|
139 return retval; \ |
|
140 } \ |
|
141 while (0) |
|
142 |
|
143 #define LSODE_ABORT1(msg) \ |
|
144 do \ |
|
145 { \ |
|
146 ::error ("lsode: " ## msg); \ |
|
147 LSODE_ABORT (); \ |
|
148 } \ |
|
149 while (0) |
|
150 |
|
151 #define LSODE_ABORT2(fmt, arg) \ |
|
152 do \ |
|
153 { \ |
|
154 ::error ("lsode: " ## fmt, arg); \ |
|
155 LSODE_ABORT (); \ |
|
156 } \ |
|
157 while (0) |
|
158 |
2928
|
159 DEFUN_DLD (lsode, args, nargout, |
|
160 "lsode (F, X0, T_OUT, T_CRIT)\n\ |
|
161 \n\ |
|
162 The first argument is the name of the function to call to\n\ |
|
163 compute the vector of right hand sides. It must have the form\n\ |
|
164 \n\ |
|
165 xdot = f (x, t)\n\ |
|
166 \n\ |
|
167 where xdot and x are vectors and t is a scalar.\n") |
|
168 { |
|
169 octave_value_list retval; |
|
170 |
3243
|
171 unwind_protect::begin_frame ("Flsode"); |
2928
|
172 |
3243
|
173 unwind_protect_int (call_depth); |
|
174 call_depth++; |
|
175 |
|
176 if (call_depth > 1) |
3323
|
177 LSODE_ABORT1 ("invalid recursive call"); |
2928
|
178 |
3243
|
179 int nargin = args.length (); |
2928
|
180 |
3243
|
181 if (nargin > 2 && nargin < 5 && nargout < 2) |
2928
|
182 { |
3243
|
183 octave_value f_arg = args(0); |
2928
|
184 |
3243
|
185 switch (f_arg.rows ()) |
|
186 { |
|
187 case 1: |
|
188 lsode_fcn = extract_function |
|
189 (args(0), "lsode", "__lsode_fcn__", |
|
190 "function xdot = __lsode_fcn__ (x, t) xdot = ", |
|
191 "; endfunction"); |
|
192 break; |
2928
|
193 |
3243
|
194 case 2: |
2928
|
195 { |
3243
|
196 string_vector tmp = args(0).all_strings (); |
2928
|
197 |
3243
|
198 if (! error_state) |
2928
|
199 { |
3243
|
200 lsode_fcn = extract_function |
|
201 (tmp(0), "lsode", "__lsode_fcn__", |
|
202 "function xdot = __lsode_fcn__ (x, t) xdot = ", |
2928
|
203 "; endfunction"); |
|
204 |
3243
|
205 if (lsode_fcn) |
|
206 { |
|
207 lsode_jac = extract_function |
|
208 (tmp(1), "lsode", "__lsode_jac__", |
|
209 "function jac = __lsode_jac__ (x, t) jac = ", |
|
210 "; endfunction"); |
|
211 |
|
212 if (! lsode_jac) |
|
213 lsode_fcn = 0; |
|
214 } |
2928
|
215 } |
|
216 } |
3243
|
217 break; |
2928
|
218 |
3243
|
219 default: |
3323
|
220 LSODE_ABORT1 |
|
221 ("first arg should be a string or 2-element string array"); |
3243
|
222 } |
2928
|
223 |
3243
|
224 if (error_state || ! lsode_fcn) |
3323
|
225 LSODE_ABORT (); |
2928
|
226 |
3243
|
227 ColumnVector state = args(1).vector_value (); |
2928
|
228 |
|
229 if (error_state) |
3323
|
230 LSODE_ABORT1 ("expecting state vector as second argument"); |
3243
|
231 |
|
232 ColumnVector out_times = args(2).vector_value (); |
|
233 |
|
234 if (error_state) |
3323
|
235 LSODE_ABORT1 ("expecting output time vector as third argument"); |
2928
|
236 |
3243
|
237 ColumnVector crit_times; |
2928
|
238 |
3243
|
239 int crit_times_set = 0; |
|
240 if (nargin > 3) |
|
241 { |
|
242 crit_times = args(3).vector_value (); |
2928
|
243 |
3243
|
244 if (error_state) |
3323
|
245 LSODE_ABORT1 ("expecting critical time vector as fourth argument"); |
2928
|
246 |
3243
|
247 crit_times_set = 1; |
|
248 } |
2928
|
249 |
3243
|
250 double tzero = out_times (0); |
|
251 int nsteps = out_times.capacity (); |
|
252 |
|
253 ODEFunc func (lsode_user_function); |
|
254 if (lsode_jac) |
|
255 func.set_jacobian_function (lsode_user_jacobian); |
2928
|
256 |
3243
|
257 LSODE ode (state, tzero, func); |
|
258 |
|
259 ode.copy (lsode_opts); |
|
260 |
|
261 int nstates = state.capacity (); |
|
262 Matrix output (nsteps, nstates + 1); |
2928
|
263 |
3243
|
264 if (crit_times_set) |
|
265 output = ode.integrate (out_times, crit_times); |
|
266 else |
|
267 output = ode.integrate (out_times); |
|
268 |
|
269 if (! error_state) |
|
270 { |
|
271 retval.resize (1); |
|
272 retval(0) = output; |
|
273 } |
|
274 } |
2928
|
275 else |
3243
|
276 print_usage ("lsode"); |
2928
|
277 |
3243
|
278 unwind_protect::run_frame ("Flsode"); |
2928
|
279 |
|
280 return retval; |
|
281 } |
|
282 |
|
283 typedef void (LSODE_options::*d_set_opt_mf) (double); |
|
284 typedef void (LSODE_options::*i_set_opt_mf) (int); |
|
285 typedef double (LSODE_options::*d_get_opt_mf) (void); |
|
286 typedef int (LSODE_options::*i_get_opt_mf) (void); |
|
287 |
|
288 #define MAX_TOKENS 3 |
|
289 |
|
290 struct LSODE_OPTIONS |
|
291 { |
|
292 const char *keyword; |
|
293 const char *kw_tok[MAX_TOKENS + 1]; |
|
294 int min_len[MAX_TOKENS + 1]; |
|
295 int min_toks_to_match; |
|
296 d_set_opt_mf d_set_fcn; |
|
297 i_set_opt_mf i_set_fcn; |
|
298 d_get_opt_mf d_get_fcn; |
|
299 i_get_opt_mf i_get_fcn; |
|
300 }; |
|
301 |
|
302 static LSODE_OPTIONS lsode_option_table [] = |
|
303 { |
|
304 { "absolute tolerance", |
|
305 { "absolute", "tolerance", 0, 0, }, |
|
306 { 1, 0, 0, 0, }, 1, |
3131
|
307 &LSODE_options::set_absolute_tolerance, 0, |
|
308 &LSODE_options::absolute_tolerance, 0, }, |
2928
|
309 |
|
310 { "initial step size", |
|
311 { "initial", "step", "size", 0, }, |
|
312 { 1, 0, 0, 0, }, 1, |
3131
|
313 &LSODE_options::set_initial_step_size, 0, |
|
314 &LSODE_options::initial_step_size, 0, }, |
2928
|
315 |
|
316 { "maximum step size", |
|
317 { "maximum", "step", "size", 0, }, |
|
318 { 2, 0, 0, 0, }, 1, |
3131
|
319 &LSODE_options::set_maximum_step_size, 0, |
|
320 &LSODE_options::maximum_step_size, 0, }, |
2928
|
321 |
|
322 { "minimum step size", |
|
323 { "minimum", "step", "size", 0, }, |
|
324 { 2, 0, 0, 0, }, 1, |
3131
|
325 &LSODE_options::set_minimum_step_size, 0, |
|
326 &LSODE_options::minimum_step_size, 0, }, |
2928
|
327 |
|
328 { "relative tolerance", |
|
329 { "relative", "tolerance", 0, 0, }, |
|
330 { 1, 0, 0, 0, }, 1, |
3131
|
331 &LSODE_options::set_relative_tolerance, 0, |
|
332 &LSODE_options::relative_tolerance, 0, }, |
2928
|
333 |
|
334 { "step limit", |
|
335 { "step", "limit", 0, 0, }, |
|
336 { 1, 0, 0, 0, }, 1, |
3131
|
337 0, &LSODE_options::set_step_limit, |
|
338 0, &LSODE_options::step_limit, }, |
2928
|
339 |
|
340 { 0, |
|
341 { 0, 0, 0, 0, }, |
|
342 { 0, 0, 0, 0, }, 0, |
|
343 0, 0, 0, 0, }, |
|
344 }; |
|
345 |
|
346 static void |
|
347 print_lsode_option_list (ostream& os) |
|
348 { |
|
349 print_usage ("lsode_options", 1); |
|
350 |
|
351 os << "\n" |
|
352 << "Options for lsode include:\n\n" |
|
353 << " keyword value\n" |
|
354 << " ------- -----\n\n"; |
|
355 |
|
356 LSODE_OPTIONS *list = lsode_option_table; |
|
357 |
|
358 const char *keyword; |
|
359 while ((keyword = list->keyword) != 0) |
|
360 { |
|
361 os.form (" %-40s ", keyword); |
|
362 if (list->d_get_fcn) |
|
363 { |
|
364 double val = (lsode_opts.*list->d_get_fcn) (); |
|
365 if (val < 0.0) |
|
366 os << "computed automatically"; |
|
367 else |
|
368 os << val; |
|
369 } |
|
370 else |
|
371 { |
|
372 int val = (lsode_opts.*list->i_get_fcn) (); |
|
373 if (val < 0) |
|
374 os << "infinite"; |
|
375 else |
|
376 os << val; |
|
377 } |
|
378 os << "\n"; |
|
379 list++; |
|
380 } |
|
381 |
|
382 os << "\n"; |
|
383 } |
|
384 |
|
385 static void |
|
386 set_lsode_option (const string& keyword, double val) |
|
387 { |
|
388 LSODE_OPTIONS *list = lsode_option_table; |
|
389 |
|
390 while (list->keyword != 0) |
|
391 { |
|
392 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
393 list->min_toks_to_match, MAX_TOKENS)) |
|
394 { |
|
395 if (list->d_set_fcn) |
|
396 (lsode_opts.*list->d_set_fcn) (val); |
|
397 else |
|
398 { |
|
399 if (xisnan (val)) |
|
400 { |
|
401 error ("lsode_options: %s: expecting integer, found NaN", |
|
402 keyword.c_str ()); |
|
403 } |
|
404 else |
|
405 (lsode_opts.*list->i_set_fcn) (NINT (val)); |
|
406 } |
|
407 return; |
|
408 } |
|
409 list++; |
|
410 } |
|
411 |
|
412 warning ("lsode_options: no match for `%s'", keyword.c_str ()); |
|
413 } |
|
414 |
|
415 static octave_value_list |
|
416 show_lsode_option (const string& keyword) |
|
417 { |
|
418 octave_value retval; |
|
419 |
|
420 LSODE_OPTIONS *list = lsode_option_table; |
|
421 |
|
422 while (list->keyword != 0) |
|
423 { |
|
424 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
425 list->min_toks_to_match, MAX_TOKENS)) |
|
426 { |
|
427 if (list->d_get_fcn) |
|
428 { |
|
429 double val = (lsode_opts.*list->d_get_fcn) (); |
|
430 if (val < 0.0) |
|
431 retval = "computed automatically"; |
|
432 else |
|
433 retval = val; |
|
434 } |
|
435 else |
|
436 { |
|
437 int val = (lsode_opts.*list->i_get_fcn) (); |
|
438 if (val < 0) |
|
439 retval = "infinite"; |
|
440 else |
|
441 retval = static_cast<double> (val); |
|
442 } |
|
443 |
|
444 return retval; |
|
445 } |
|
446 list++; |
|
447 } |
|
448 |
|
449 warning ("lsode_options: no match for `%s'", keyword.c_str ()); |
|
450 |
|
451 return retval; |
|
452 } |
|
453 |
|
454 DEFUN_DLD (lsode_options, args, , |
|
455 "lsode_options (KEYWORD, VALUE)\n\ |
|
456 \n\ |
|
457 Set or show options for lsode. Keywords may be abbreviated\n\ |
|
458 to the shortest match.") |
|
459 { |
|
460 octave_value_list retval; |
|
461 |
|
462 int nargin = args.length (); |
|
463 |
|
464 if (nargin == 0) |
|
465 { |
|
466 print_lsode_option_list (octave_stdout); |
|
467 return retval; |
|
468 } |
|
469 else if (nargin == 1 || nargin == 2) |
|
470 { |
|
471 string keyword = args(0).string_value (); |
|
472 |
|
473 if (! error_state) |
|
474 { |
|
475 if (nargin == 1) |
|
476 return show_lsode_option (keyword); |
|
477 else |
|
478 { |
|
479 double val = args(1).double_value (); |
|
480 |
|
481 if (! error_state) |
|
482 { |
|
483 set_lsode_option (keyword, val); |
|
484 return retval; |
|
485 } |
|
486 } |
|
487 } |
|
488 } |
|
489 |
|
490 print_usage ("lsode_options"); |
|
491 |
|
492 return retval; |
|
493 } |
|
494 |
|
495 /* |
|
496 ;;; Local Variables: *** |
|
497 ;;; mode: C++ *** |
|
498 ;;; End: *** |
|
499 */ |