Index

NAME

FieldFunc - scalar field defined as a C/C++ function or functor


INCLUDE

include "FieldFunc.h"

SYNTAX

 // explicit functions defining a scalarfield:
 typedef NUMT  (*fxtFieldFunc)(const Ptv(real)& x, real time);

 class FieldFunc : public Field
 {
   fxtFieldFunc    f_ptr;    // function pointer

 public:

   FieldFunc (const char* name = NULL) : Field(name)  { f_ptr = NULL; }
   FieldFunc (fxtFieldFunc f_ptr_)     { this->f_ptr = f_ptr_; }
   FieldFunc (const FieldFunc& f);
  ~FieldFunc ();

   void operator = (fxtFieldFunc funct);
   void operator = (const FieldFunc& f);

   virtual bool ok () const { return true; };

   // valuePt is usually used from a Field* or Field& or Handle(Field):
   virtual NUMT valuePt (const Ptv(real)& x, real t = DUMMY);

   // operator() is usually used from a FieldFunc pointer/ref/handle to
   // access functors (problem dependent classes derived from FieldFunc):
   virtual NUMT operator () (const Ptv(real)& x, real t) const;

   // use finite differences to approximate the derivatives:
   virtual Ptv(NUMT) derivativePt (const Ptv(real)& x, real t = DUMMY);

   virtual NUMT valueFEM (const FiniteElement& fe, real t = DUMMY);
   virtual void derivativeFEM (Ptv(NUMT)& d, const FiniteElement& fe,
                               real t = DUMMY);
   void minmax (NUMT& min, NUMT& max, GridWithPts& grid) const;
   virtual void minmax (NUMT& min, NUMT& max, GridWithPts* grid =NULL) const;

   CLASS_INFO

   VIRTUAL_CAST(FieldFunc)
 };



KEYWORDS

scalar field, explicit function



DESCRIPTION

The  class  implements  a  scalar  field  defined  in terms of an
explicit function.

There are two ways to implement a function field in  terms  of  a
FieldFunc object:


Create  a  standard C or C++ function of same type as the pointer
fxtFieldFunc which has a  typedef  definition  in  the  FieldFunc
header  file.   The  function  can be a static member function of
some class, but not an  ordinary  (non-static)  member  function.
There  is automatic type conversion from an fxtFieldFunc function
to a FieldFunc object.

Derive a new class from FieldFunc (use the constructor in  Field­
Func  with no arguments when initializing the base class). In the
derived class one implements the  virtual  function  valuePt,  or
both  valuePt  and  operator().  Both these functions perform the
same task, the only difference is that  operator()  is  a  const-
function. If operator() is missing, one inherits a default opera­
tor() which just calls valuePt.  This type of FieldFunc is  actu­
ally a so called functor. In the user's class one may have refer­
ences to other problem dependent classes such that data on  other
classes,  reflecting  e.g.  parameters  in  the  function, can be
accessed.


One of the purposes of FieldFunc is to support  an  interface  to
user  defined functions such that all scalar fields, whether they
are explicit functions or complicated finite  element  functions,
have  a  common  interface.   Another  purpose is to offer a base
class for problem dependent functors reflecting  fields.

The FieldFunc class enables computation of the derivative of  the
explicit function using finite differences.

Note  that there are some predefined FieldFunc functors: BellFunc
for Gaussian bell functions.  See the FieldFunc.h file.


CONSTRUCTORS AND INITIALIZATION

One constructor takes a pointer to  the  explicit  function.  The
other  has  no  arguments and is used when the function is repre­
sented as a functor.



MEMBER FUNCTIONS

valuePt - evaluates the function. This is  a  non-const  function
and it is also virtual in the Field base class.

operator()  -  same as valuePt, but the function is const. It has
no virtual counterpart in class Field.



EXAMPLES

Many classes, like FieldFE, have operator=(FieldFunc)  for  rapid
initialization  of a field in terms of an explicit function.  The
user can either put a C/C++-function on the right  hand  side  of
the  equality  sign  (automatic conversion to a FieldFunc will be
performed if the FieldFunc.h header is included) or one can  give
reference  to  a  subclass  of  FieldFunc  if  a  functor is used
instead.

An example of FieldFunc and functors is given in the report
 Details of Finite Element Programming in Diffpack.  Other appli­
cations are found in the reports
 Diffpack  programs  for a Simple PDE Solved by Finite Difference
Methods and  Solving a Simple PDE Using Diffpack: Increasing  the
Flexibility.


SEE ALSO

class Field


DEVELOPED BY

SINTEF Applied Mathematics, Oslo, Norway, and University of Oslo,
Dept. of Mathematics, Norway


AUTHOR

Hans Petter Langtangen, SINTEF/UiO