view src/feval.cc @ 233:b07ec30369ab

Add checks to feval * src/feval.cc: check the spatial dimensions of the coordinates and that they belong to the domain
author Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
date Sat, 21 Jun 2014 15:28:07 +0200
parents a28b50969020
children a51c09492e30
line wrap: on
line source

/*
 Copyright (C) 2013 Marco Vassallo <gedeone-octave@users.sourceforge.net>

 This program is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3 of the License, or (at your option) any later
 version.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 details.

 You should have received a copy of the GNU General Public License along with
 this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "function.h"
#include <stdexcept>
#include <sstream>

DEFUN_DLD (feval, args, , "-*- texinfo -*-\n\
@deftypefn {Function File} {[@var{value}]} = \
feval (@var{function_name}, @var{Coordinate})\n\
Evaluate a function at a specific point of the domain and return the value. \n\
The input parameters are the function and the point where it has to\
be evaluated.\n\
The point can be either a vector or a matrix. If it is a matrix, each column\
represents a different point at which we evaluates the function.\n\
@seealso{Function}\n\
@end deftypefn")
{

  int nargin = args.length ();
  octave_value retval=0;
  
  if (nargin < 2 || nargin > 2)
    print_usage ();
  else
    {
      if (! function_type_loaded)
        {
          function::register_type ();
          function_type_loaded = true;
          mlock ();
        }

      if (args(0).type_id () == function::static_type_id ())
        {
          const function & fspo =
            static_cast<const function&> (args(0).get_rep ());
          Matrix point= args(1).matrix_value ();

          if (!error_state)
            {
              const boost::shared_ptr<const dolfin::Function> 
                & f = fspo.get_pfun ();

              if (point.rows () == 1)
                point = point.transpose ();

              if (point.rows () != f->geometric_dimension ())
                {
                  error ("feval: wrong coordinates dimension");
                }
              else
                {
                  dim_vector dims;
                  dims.resize (2);
                  dims(0) = f->value_dimension (0);
                  dims(1) = point.cols ();
                  Matrix res (dims);

                  dim_vector dims_tmp;
                  dims_tmp.resize (2);
                  dims_tmp(0) = f->value_dimension (0);
                  dims_tmp(1) = 1;
                  Array<double> res_tmp (dims_tmp);

                  for (uint i = 0; i < point.cols (); ++i)
                    {
                      Array<double> point_tmp = point.column (i);
                      dolfin::Array<double> 
                        x (point_tmp.length (), point_tmp.fortran_vec ());
                      dolfin::Array<double> 
                        values (res_tmp.length (), res_tmp.fortran_vec ());
                      try
                        {
                          f->eval (values, x);
                        }
                      catch (std::runtime_error & err)
                        {
                          std::string msg = "feval: cannot evaluate a function";
                          msg += " outside of its domain";
                          error (msg.c_str ());
                        }
                      for (uint j = 0; j < res_tmp.length (); ++j)
                        res(i, j) = res_tmp (j);
                    }
                  retval = octave_value (res);
                }
            }
        }
    }
  return retval;
}