home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / src / f-lsode.cc < prev    next >
C/C++ Source or Header  |  1995-01-03  |  7KB  |  319 lines

  1. // f-lsode.cc                                           -*- C++ -*-
  2. /*
  3.  
  4. Copyright (C) 1993, 1994, 1995 John W. Eaton
  5.  
  6. This file is part of Octave.
  7.  
  8. Octave is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License as published by the
  10. Free Software Foundation; either version 2, or (at your option) any
  11. later version.
  12.  
  13. Octave is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with Octave; see the file COPYING.  If not, write to the Free
  20. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. */
  23.  
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27.  
  28. #include <strstream.h>
  29.  
  30. #include "ODE.h"
  31.  
  32. #include "tree-const.h"
  33. #include "variables.h"
  34. #include "gripes.h"
  35. #include "error.h"
  36. #include "utils.h"
  37. #include "pager.h"
  38. #include "help.h"
  39. #include "defun-dld.h"
  40.  
  41. // Global pointer for user defined function required by lsode.
  42. static tree_fvc *lsode_fcn;
  43.  
  44. static ODE_options lsode_opts;
  45.  
  46. ColumnVector
  47. lsode_user_function (const ColumnVector& x, double t)
  48. {
  49.   ColumnVector retval;
  50.  
  51.   int nstates = x.capacity ();
  52.  
  53.   Octave_object args;
  54.   args(1) = t;
  55.  
  56.   if (nstates > 1)
  57.     {
  58.       Matrix m (nstates, 1);
  59.       for (int i = 0; i < nstates; i++)
  60.     m (i, 0) = x.elem (i);
  61.       tree_constant state (m);
  62.       args(0) = state;
  63.     }
  64.   else
  65.     {
  66.       double d = x.elem (0);
  67.       tree_constant state (d);
  68.       args(0) = state;
  69.     }
  70.  
  71.   if (lsode_fcn)
  72.     {
  73.       Octave_object tmp = lsode_fcn->eval (0, 1, args);
  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. DEFUN_DLD_BUILTIN ("lsode", Flsode, Slsode, 4, 1,
  96.   "lsode (F, X0, T_OUT, T_CRIT)\n\
  97. \n\
  98. The first argument is the name of the function to call to\n\
  99. compute the vector of right hand sides.  It must have the form\n\
  100. \n\
  101.   xdot = f (x, t)\n\
  102. \n\
  103. where xdot and x are vectors and t is a scalar.\n")
  104. {
  105.   Octave_object retval;
  106.  
  107.   int nargin = args.length ();
  108.  
  109.   if (nargin < 3 || nargin > 4 || nargout > 1)
  110.     {
  111.       print_usage ("lsode");
  112.       return retval;
  113.     }
  114.  
  115.   lsode_fcn = is_valid_function (args(0), "lsode", 1);
  116.   if (! lsode_fcn || takes_correct_nargs (lsode_fcn, 2, "lsode", 1) != 1)
  117.     return retval;
  118.  
  119.   ColumnVector state = args(1).vector_value ();
  120.  
  121.   if (error_state)
  122.     {
  123.       error ("lsode: expecting state vector as second argument");
  124.       return retval;
  125.     }
  126.  
  127.   ColumnVector out_times = args(2).vector_value ();
  128.  
  129.   if (error_state)
  130.     {
  131.       error ("lsode: expecting output time vector as third argument");
  132.       return retval;
  133.     }
  134.  
  135.   ColumnVector crit_times;
  136.  
  137.   int crit_times_set = 0;
  138.   if (nargin > 3)
  139.     {
  140.       crit_times = args(3).vector_value ();
  141.  
  142.       if (error_state)
  143.     {
  144.       error ("lsode: expecting critical time vector as fourth argument");
  145.       return retval;
  146.     }
  147.  
  148.       crit_times_set = 1;
  149.     }
  150.  
  151.   double tzero = out_times.elem (0);
  152.   int nsteps = out_times.capacity ();
  153.  
  154.   ODEFunc func (lsode_user_function);
  155.   ODE ode (state, tzero, func);
  156.   ode.copy (lsode_opts);
  157.  
  158.   int nstates = state.capacity ();
  159.   Matrix output (nsteps, nstates + 1);
  160.  
  161.   if (crit_times_set)
  162.     output = ode.integrate (out_times, crit_times);
  163.   else
  164.     output = ode.integrate (out_times);
  165.  
  166.   retval.resize (1);
  167.   retval(0) = output;
  168.   return retval;
  169. }
  170.  
  171. typedef void (ODE_options::*d_set_opt_mf) (double);
  172. typedef double (ODE_options::*d_get_opt_mf) (void);
  173.  
  174. #define MAX_TOKENS 3
  175.  
  176. struct ODE_OPTIONS
  177. {
  178.   const char *keyword;
  179.   const char *kw_tok[MAX_TOKENS + 1];
  180.   int min_len[MAX_TOKENS + 1];
  181.   int min_toks_to_match;
  182.   d_set_opt_mf d_set_fcn;
  183.   d_get_opt_mf d_get_fcn;
  184. };
  185.  
  186. static ODE_OPTIONS lsode_option_table [] =
  187. {
  188.   { "absolute tolerance",
  189.     { "absolute", "tolerance", 0, 0, },
  190.     { 1, 0, 0, 0, }, 1,
  191.     ODE_options::set_absolute_tolerance,
  192.     ODE_options::absolute_tolerance, },
  193.  
  194.   { "initial step size",
  195.     { "initial", "step", "size", 0, },
  196.     { 1, 0, 0, 0, }, 1,
  197.     ODE_options::set_initial_step_size,
  198.     ODE_options::initial_step_size, },
  199.  
  200.   { "maximum step size",
  201.     { "maximum", "step", "size", 0, },
  202.     { 2, 0, 0, 0, }, 1,
  203.     ODE_options::set_maximum_step_size,
  204.     ODE_options::maximum_step_size, },
  205.  
  206.   { "minimum step size",
  207.     { "minimum", "step", "size", 0, },
  208.     { 2, 0, 0, 0, }, 1,
  209.     ODE_options::set_minimum_step_size,
  210.     ODE_options::minimum_step_size, },
  211.  
  212.   { "relative tolerance",
  213.     { "relative", "tolerance", 0, 0, },
  214.     { 1, 0, 0, 0, }, 1,
  215.     ODE_options::set_relative_tolerance,
  216.     ODE_options::relative_tolerance, },
  217.  
  218.   { 0,
  219.     { 0, 0, 0, 0, },
  220.     { 0, 0, 0, 0, }, 0,
  221.     0, 0, },
  222. };
  223.  
  224. static void
  225. print_lsode_option_list (void)
  226. {
  227.   ostrstream output_buf;
  228.  
  229.   print_usage ("lsode_options", 1);
  230.  
  231.   output_buf << "\n"
  232.          << "Options for lsode include:\n\n"
  233.          << "  keyword                                  value\n"
  234.          << "  -------                                  -----\n\n";
  235.  
  236.   ODE_OPTIONS *list = lsode_option_table;
  237.  
  238.   const char *keyword;
  239.   while ((keyword = list->keyword) != 0)
  240.     {
  241.       output_buf.form ("  %-40s ", keyword);
  242.  
  243.       double val = (lsode_opts.*list->d_get_fcn) ();
  244.       if (val < 0.0)
  245.     output_buf << "computed automatically";
  246.       else
  247.     output_buf << val;
  248.  
  249.       output_buf << "\n";
  250.       list++;
  251.     }
  252.  
  253.   output_buf << "\n" << ends;
  254.   maybe_page_output (output_buf);
  255. }
  256.  
  257. static void
  258. do_lsode_option (char *keyword, double val)
  259. {
  260.   ODE_OPTIONS *list = lsode_option_table;
  261.  
  262.   while (list->keyword != 0)
  263.     {
  264.       if (keyword_almost_match (list->kw_tok, list->min_len, keyword,
  265.                 list->min_toks_to_match, MAX_TOKENS))
  266.     {
  267.       (lsode_opts.*list->d_set_fcn) (val);
  268.  
  269.       return;
  270.     }
  271.       list++;
  272.     }
  273.  
  274.   warning ("lsode_options: no match for `%s'", keyword);
  275. }
  276.  
  277. DEFUN_DLD_BUILTIN ("lsode_options", Flsode_options, Slsode_options, -1, 1,
  278.   "lsode_options (KEYWORD, VALUE)\n\
  279. \n\
  280. Set or show options for lsode.  Keywords may be abbreviated\n\
  281. to the shortest match.")
  282. {
  283.   Octave_object retval;
  284.  
  285.   int nargin = args.length ();
  286.  
  287.   if (nargin == 0)
  288.     {
  289.       print_lsode_option_list ();
  290.       return retval;
  291.     }
  292.   else if (nargin == 2)
  293.     {
  294.       char *keyword = args(0).string_value ();
  295.  
  296.       if (! error_state)
  297.     {
  298.       double val = args(1).double_value ();
  299.  
  300.       if (! error_state)
  301.         {
  302.           do_lsode_option (keyword, val);
  303.           return retval;
  304.         }
  305.     }
  306.     }
  307.  
  308.   print_usage ("lsode_options");
  309.  
  310.   return retval;
  311. }
  312.  
  313. /*
  314. ;;; Local Variables: ***
  315. ;;; mode: C++ ***
  316. ;;; page-delimiter: "^/\\*" ***
  317. ;;; End: ***
  318. */
  319.