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-dassl.cc < prev    next >
C/C++ Source or Header  |  1995-01-03  |  7KB  |  338 lines

  1. // f-dassl.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 "DAE.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 dassl.
  42. static tree_fvc *dassl_fcn;
  43.  
  44. static ODE_options dassl_opts;
  45.  
  46. ColumnVector
  47. dassl_user_function (const ColumnVector& x, const ColumnVector& xdot, double t)
  48. {
  49.   ColumnVector retval;
  50.  
  51.   int nstates = x.capacity ();
  52.  
  53.   assert (nstates == xdot.capacity ());
  54.  
  55.   Octave_object args;
  56.   args(2) = t;
  57.  
  58.   if (nstates > 1)
  59.     {
  60.       Matrix m1 (nstates, 1);
  61.       Matrix m2 (nstates, 1);
  62.       for (int i = 0; i < nstates; i++)
  63.     {
  64.       m1 (i, 0) = x.elem (i);
  65.       m2 (i, 0) = xdot.elem (i);
  66.     }
  67.       tree_constant state (m1);
  68.       tree_constant deriv (m2);
  69.       args(1) = deriv;
  70.       args(0) = state;
  71.     }
  72.   else
  73.     {
  74.       double d1 = x.elem (0);
  75.       double d2 = xdot.elem (0);
  76.       tree_constant state (d1);
  77.       tree_constant deriv (d2);
  78.       args(1) = deriv;
  79.       args(0) = state;
  80.     }
  81.  
  82.   if (dassl_fcn)
  83.     {
  84.       Octave_object tmp = dassl_fcn->eval (0, 1, args);
  85.  
  86.       if (error_state)
  87.     {
  88.       gripe_user_supplied_eval ("dassl");
  89.       return retval;
  90.     }
  91.  
  92.       if (tmp.length () > 0 && tmp(0).is_defined ())
  93.     {
  94.       retval = tmp(0).vector_value ();
  95.  
  96.       if (error_state || retval.length () == 0)
  97.         gripe_user_supplied_eval ("dassl");
  98.     }
  99.       else
  100.     gripe_user_supplied_eval ("dassl");
  101.     }
  102.  
  103.   return retval;
  104. }
  105.  
  106. DEFUN_DLD_BUILTIN ("dassl", Fdassl, Sdassl, 5, 2,
  107.   "dassl (\"function_name\", x_0, xdot_0, t_out)\n\
  108. dassl (F, X_0, XDOT_0, T_OUT, T_CRIT)\n\
  109. \n\
  110. The first argument is the name of the function to call to\n\
  111. compute the vector of residuals.  It must have the form\n\
  112. \n\
  113.   res = f (x, xdot, t)\n\
  114. \n\
  115. where x, xdot, and res are vectors, and t is a scalar.")
  116. {
  117.   Octave_object retval;
  118.  
  119.   int nargin = args.length ();
  120.  
  121.   if (nargin < 4 || nargin > 5)
  122.     {
  123.       print_usage ("dassl");
  124.       return retval;
  125.     }
  126.  
  127.   dassl_fcn = is_valid_function (args(0), "dassl", 1);
  128.   if (! dassl_fcn || takes_correct_nargs (dassl_fcn, 3, "dassl", 1) != 1)
  129.     return retval;
  130.  
  131.   ColumnVector state = args(1).vector_value ();
  132.  
  133.   if (error_state)
  134.     {
  135.       error ("dassl: expecting state vector as second argument");
  136.       return retval;
  137.     }
  138.  
  139.   ColumnVector deriv = args(2).vector_value ();
  140.  
  141.   if (error_state)
  142.     {
  143.       error ("dassl: expecting derivative vector as third argument");
  144.       return retval;
  145.     }
  146.  
  147.   ColumnVector out_times = args(3).vector_value ();
  148.  
  149.   if (error_state)
  150.     {
  151.       error ("dassl: expecting output time vector as fourth argument");
  152.       return retval;
  153.     }
  154.  
  155.   ColumnVector crit_times;
  156.   int crit_times_set = 0;
  157.   if (nargin > 4)
  158.     {
  159.       crit_times = args(4).vector_value ();
  160.  
  161.       if (error_state)
  162.     {
  163.       error ("dassl: expecting critical time vector as fifth argument");
  164.       return retval;
  165.     }
  166.  
  167.       crit_times_set = 1;
  168.     }
  169.  
  170.   if (state.capacity () != deriv.capacity ())
  171.     {
  172.       error ("dassl: x and xdot must have the same size");
  173.       return retval;
  174.     }
  175.  
  176.   double tzero = out_times.elem (0);
  177.  
  178.   DAEFunc func (dassl_user_function);
  179.   DAE dae (state, deriv, tzero, func);
  180.   dae.copy (dassl_opts);
  181.  
  182.   Matrix output;
  183.   Matrix deriv_output;
  184.  
  185.   if (crit_times_set)
  186.     output = dae.integrate (out_times, deriv_output, crit_times);
  187.   else
  188.     output = dae.integrate (out_times, deriv_output);
  189.  
  190.   retval.resize (2);
  191.   retval(0) = output;
  192.   retval(1) = deriv_output;
  193.   return retval;
  194. }
  195.  
  196. typedef void (ODE_options::*d_set_opt_mf) (double);
  197. typedef double (ODE_options::*d_get_opt_mf) (void);
  198.  
  199. #define MAX_TOKENS 3
  200.  
  201. struct DAE_OPTIONS
  202. {
  203.   const char *keyword;
  204.   const char *kw_tok[MAX_TOKENS + 1];
  205.   int min_len[MAX_TOKENS + 1];
  206.   int min_toks_to_match;
  207.   d_set_opt_mf d_set_fcn;
  208.   d_get_opt_mf d_get_fcn;
  209. };
  210.  
  211. static DAE_OPTIONS dassl_option_table [] =
  212. {
  213.   { "absolute tolerance",
  214.     { "absolute", "tolerance", 0, 0, },
  215.     { 1, 0, 0, 0, }, 1,
  216.     ODE_options::set_absolute_tolerance,
  217.     ODE_options::absolute_tolerance, },
  218.  
  219.   { "initial step size",
  220.     { "initial", "step", "size", 0, },
  221.     { 1, 0, 0, 0, }, 1,
  222.     ODE_options::set_initial_step_size,
  223.     ODE_options::initial_step_size, },
  224.  
  225.   { "maximum step size",
  226.     { "maximum", "step", "size", 0, },
  227.     { 2, 0, 0, 0, }, 1,
  228.     ODE_options::set_maximum_step_size,
  229.     ODE_options::maximum_step_size, },
  230.  
  231.   { "relative tolerance",
  232.     { "relative", "tolerance", 0, 0, },
  233.     { 1, 0, 0, 0, }, 1,
  234.     ODE_options::set_relative_tolerance,
  235.     ODE_options::relative_tolerance, },
  236.  
  237.   { 0,
  238.     { 0, 0, 0, 0, },
  239.     { 0, 0, 0, 0, }, 0,
  240.     0, 0, },
  241. };
  242.  
  243. static void
  244. print_dassl_option_list (void)
  245. {
  246.   ostrstream output_buf;
  247.  
  248.   print_usage ("dassl_options", 1);
  249.  
  250.   output_buf << "\n"
  251.          << "Options for dassl include:\n\n"
  252.          << "  keyword                                  value\n"
  253.          << "  -------                                  -----\n\n";
  254.  
  255.   DAE_OPTIONS *list = dassl_option_table;
  256.  
  257.   const char *keyword;
  258.   while ((keyword = list->keyword) != 0)
  259.     {
  260.       output_buf.form ("  %-40s ", keyword);
  261.  
  262.       double val = (dassl_opts.*list->d_get_fcn) ();
  263.       if (val < 0.0)
  264.     output_buf << "computed automatically";
  265.       else
  266.     output_buf << val;
  267.  
  268.       output_buf << "\n";
  269.       list++;
  270.     }
  271.  
  272.   output_buf << "\n" << ends;
  273.   maybe_page_output (output_buf);
  274. }
  275.  
  276. static void
  277. do_dassl_option (char *keyword, double val)
  278. {
  279.   DAE_OPTIONS *list = dassl_option_table;
  280.  
  281.   while (list->keyword != 0)
  282.     {
  283.       if (keyword_almost_match (list->kw_tok, list->min_len, keyword,
  284.                 list->min_toks_to_match, MAX_TOKENS))
  285.     {
  286.       (dassl_opts.*list->d_set_fcn) (val);
  287.  
  288.       return;
  289.     }
  290.       list++;
  291.     }
  292.  
  293.   warning ("dassl_options: no match for `%s'", keyword);
  294. }
  295.  
  296. DEFUN_DLD_BUILTIN ("dassl_options", Fdassl_options, Sdassl_options, -1, 1,
  297.   "dassl_options (KEYWORD, VALUE)\n\
  298. \n\
  299. Set or show options for dassl.  Keywords may be abbreviated\n\
  300. to the shortest match.")
  301. {
  302.   Octave_object retval;
  303.  
  304.   int nargin = args.length ();
  305.  
  306.   if (nargin == 0)
  307.     {
  308.       print_dassl_option_list ();
  309.       return retval;
  310.     }
  311.   else if (nargin == 2)
  312.     {
  313.       char *keyword = args(0).string_value ();
  314.  
  315.       if (! error_state)
  316.     {
  317.       double val = args(1).double_value ();
  318.  
  319.       if (! error_state)
  320.         {
  321.           do_dassl_option (keyword, val);
  322.           return retval;
  323.         }
  324.     }
  325.     }
  326.  
  327.   print_usage ("dassl_options");
  328.  
  329.   return retval;
  330. }
  331.  
  332. /*
  333. ;;; Local Variables: ***
  334. ;;; mode: C++ ***
  335. ;;; page-delimiter: "^/\\*" ***
  336. ;;; End: ***
  337. */
  338.