view src/Function.cc @ 119:4b8b705e22a7

Added new output messagging and changed index convvenction for extracting a Function: now start from 1.
author gedeone-octave <marcovass89@hotmail.it>
date Sun, 01 Sep 2013 23:14:28 +0200
parents d03627091414
children 9486cbdc0a2e
line wrap: on
line source

#include <dolfin.h>
#include "functionspace.h"
#include "function.h"

DEFUN_DLD (Function, args, , "-*- texinfo -*-\n\
@deftypefn {Function File} {[@var{func}]} = \
Function (@var{name}, @var{Functional Space} or @var{Function},\
@var{Vector} or @var{index})\n\
These function can be used in two ways\n\
@itemize @bullet \n\
@item To create a function from a vector. In this case, the arguments are:\n\
@itemize @bullet \n\
@item @var{name} is a string representing the name of the function\n\
@item @var{Functional Space} is the fem-fenics functional space where\
the vector is defined\n\
@item @var{Vector} contains the value of the coefficients\
for the basis functions of @var{Functional Space}\n\
@end itemize\n\
@item To extract a scalar field from a vectorial one\n\
@itemize @bullet \n\
@item @var{name} is a string representing the name of the function\n\
@item @var{Function} is the vector valued Function\n\
@item @var{Index} contains the index of the scalar field to extract.\
Index starts from 1. \n\
@end itemize\n\
@end itemize \n\
The output @var{func} is an object which contain a representation of the\
function @var{Vector} which can be plotted or saved.\n\
@seealso{fem_plot, fem_save}\n\
@end deftypefn")
{

  int nargin = args.length ();
  octave_value retval;
  
  if (nargin < 3 || nargin > 3)
    print_usage ();
  else
    {
      if (! functionspace_type_loaded)
        {
          functionspace::register_type ();
          functionspace_type_loaded = true;
          mlock ();
        }

      if (! function_type_loaded)
        {
          function::register_type ();
          function_type_loaded = true;
          mlock ();
        }

      if (args(1).type_id () == functionspace::static_type_id ())
        {
          std::string str = args(0).string_value ();
          const functionspace & fspo =
            static_cast<const functionspace&> (args(1).get_rep ());
          Array <double> myu = args(2).array_value ();

          if (!error_state)
            {
              const boost::shared_ptr<const dolfin::FunctionSpace> & V = fspo.get_pfsp ();

              if (V->dim () != myu.length ())
                error ("The size of the functional space and of the vector must agree");
              else
                {
                  std::cout <<"creating function from vector..."<< std::endl;
                  dolfin::Vector du(myu.length ());
                  for (std::size_t i = 0; i < myu.length (); ++i)
                    du.setitem (i, myu(i));

                  boost::shared_ptr<dolfin::Vector> uu (new dolfin::Vector(du));
                  boost::shared_ptr <const dolfin::Function> u (new dolfin::Function(V, uu));

                  retval = new function (str, u);
               }
            }
        }
      else if (args(1).type_id () == function::static_type_id ())
        {
          std::string str = args(0).string_value ();
          const function & fspo =
            static_cast<const function&> (args(1).get_rep ());
          int idx = args(2).int_value ();

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

              if (f->value_rank () < 1)
                error ("Function: The function you provided isn't a vecotr field");
              else
                {
                  std::cout <<"Extracting function..."<< std::endl;
                  if (idx < 1 || idx > f->value_dimension (0))
                    error ("Function: index out of bounds");
                 else
                   {
                      boost::shared_ptr <const dolfin::Function> u (new dolfin::Function((*f)[idx - 1]));
                      retval = new function (str, u);
                   }
                }
            }
        }
    }
  return retval;
}