functions.hh

Go to the documentation of this file.
00001 
00002 // $Id: functions.hh 520 2008-10-07 22:06:21Z sander $
00003 
00004 #ifndef DUNE_FUNCTIONS_HH
00005 #define DUNE_FUNCTIONS_HH
00006 
00007 #include<iostream>
00008 #include<dune/common/fvector.hh>
00009 #include<dune/common/exceptions.hh>
00010 #include<dune/grid/common/grid.hh>
00011 #include<dune/grid/utility/hierarchicsearch.hh>
00012 #include<dune/grid/io/file/vtk/vtkwriter.hh>
00013 
00014 
00040 namespace Dune
00041 {
00047 
00048   const int InfinitelyDifferentiable=32767;
00049 
00051 
00059   template<class DT, class RT, int n, int m>
00060   class FunctionBase
00061   {
00062   public:
00064         typedef DT DomainFieldType;
00065 
00067         typedef RT RangeFieldType;
00068 
00070         enum { DimDomain = n, DimRange = m};
00071         
00073 
00079         virtual RT eval (int comp, const Dune::FieldVector<DT,n>& x) const = 0;
00080 
00082 
00086         virtual void evalall (const Dune::FieldVector<DT,n>& x, Dune::FieldVector<RT,m>& y) const = 0;
00087 
00088         virtual ~FunctionBase () {}
00089   };
00090 
00091 
00093   template<class DT, class RT, int n, int m>
00094   class FunctionDefault : virtual public FunctionBase<DT,RT,n,m>
00095   {
00096   public:
00098 
00102         virtual void evalall (const Dune::FieldVector<DT,n>& x, Dune::FieldVector<RT,m>& y) const 
00103         {
00104           for (int i=0; i<m; ++i)
00105                 y[i] = eval(i,x);
00106         }
00107   };
00108 
00109 
00110 
00111 
00112 
00114 
00123   template<class DT, class RT, int n, int m>
00124   class DifferentiableFunction : virtual public FunctionBase<DT,RT,n,m>
00125   {
00126   public:
00128 
00134         virtual RT derivative (int comp, const Dune::FieldVector<int,n>& d, const Dune::FieldVector<DT,n>& x) const = 0;
00135 
00137 
00140         virtual int order () const = 0;
00141   };
00142 
00143 
00144 
00146   template<class DT, class RT, int n, int m>
00147   class DifferentiableFunctionDefault : virtual public DifferentiableFunction<DT,RT,n,m>
00148   {
00149   public:
00151 
00158         RT derivative (int comp, const Dune::FieldVector<int,n>& d, const Dune::FieldVector<DT,n>& x) const
00159         {
00160           for (int i=0; i<n; ++i)
00161                 if (d[i]>0)
00162                   {
00163                         DT eps = 1E-5; // should be type dependent
00164                         if (sizeof(DT)>4) eps = 1E-10;
00165                         DT delta = eps*std::abs(x[i])+eps;
00166                         Dune::FieldVector<int,n> dd(d); dd[i] = dd[i]-1;
00167                         Dune::FieldVector<DT,n> xx(x); xx[i] += delta;
00168                         RT up = derivative(comp,dd,xx);
00169                         xx[i] = x[i]-delta;
00170                         RT down = derivative(comp,dd,xx);
00171                         return (up-down)/(2*delta);
00172                   }
00173           // d contains all zeros, evaluate function
00174           return eval(comp,x);
00175         }
00176   };
00177 
00178 
00179 
00181 
00189   template<class G, class RT, int m>
00190   class GridFunction : virtual public FunctionBase<typename G::ctype,RT,G::dimension,m>
00191   {
00193         typedef typename G::ctype DT;
00194 
00196         enum {n=G::dimension};
00197 
00199         typedef typename G::Traits::template Codim<0>::Entity Entity;
00200 
00201   public:
00203 
00209         virtual RT evallocal (int comp, const Entity& e, const Dune::FieldVector<DT,n>& xi) const = 0;
00210 
00212 
00217         virtual void evalalllocal (const Entity& e, const Dune::FieldVector<DT,G::dimension>& xi, 
00218                                                            Dune::FieldVector<RT,m>& y) const = 0;
00219   };
00220 
00221 
00222 
00224   template<class G, class RT, int m>
00225   class GridFunctionDefault : virtual public GridFunction<G,RT,m>
00226   {
00228         typedef typename G::ctype DT;
00229 
00231         enum {n=G::dimension};
00232 
00234         typedef typename G::Traits::template Codim<0>::Entity Entity;
00235 
00236   public:
00238 
00244         RT evallocal (int comp, const Entity& e, const Dune::FieldVector<DT,n>& xi) const
00245         {
00246           return eval(comp,e.geometry().global(xi));
00247         }
00248 
00249 
00251 
00256         void evalalllocal (const Entity& e, const Dune::FieldVector<DT,G::dimension>& xi, 
00257                                            Dune::FieldVector<RT,m>& y) const
00258         {
00259           for (int i=0; i<m; ++i)
00260           y[i] = evallocal(i,e,xi);
00261         }
00262 
00263   };
00264 
00265 
00267   template<class GV, class RT, int m>
00268   class GridFunctionGlobalEvalDefault : virtual public GridFunction<typename GV::Grid,RT,m>
00269   {
00271         typedef typename GV::Grid G;
00272 
00274         typedef typename G::ctype DT;
00275 
00277         enum {n=G::dimension};
00278 
00280         typedef typename G::Traits::template Codim<0>::Entity Entity;
00281 
00283         typedef typename G::Traits::template Codim<0>::EntityPointer EntityPointer;
00284 
00286       typedef typename GV::IndexSet IS;
00287   public:
00288 
00289       GridFunctionGlobalEvalDefault(const GV& _gv) : hsearch(_gv.grid(),_gv.indexSet()) {};
00290     
00300         virtual RT eval (int comp, const Dune::FieldVector<DT,n>& xi) const
00301         {
00302       EntityPointer e = hsearch.findEntity(xi);
00303       return evallocal(comp,*e,e->geometry().local(xi));
00304         }
00305 
00306   protected:
00307     HierarchicSearch<G,IS> hsearch;
00308   };
00309 
00311 
00318   template<class G, class RT, int m>
00319   class DifferentiableGridFunction : virtual public GridFunction<G,RT,m>, 
00320                                                                          virtual public DifferentiableFunction<typename G::ctype,RT,G::dimension,m>
00321   {
00323         typedef typename G::ctype DT;
00324 
00326         enum {n=G::dimension};
00327 
00329         typedef typename G::Traits::template Codim<0>::Entity Entity;
00330 
00331   public:
00333 
00341         virtual RT derivativelocal (int comp, const Dune::FieldVector<int,n>& d, 
00342                                                                 const Entity& e, const Dune::FieldVector<DT,n>& xi) const = 0;
00343   };
00344   
00346 
00353   template<class G, class RT, int m>
00354   class DifferentiableGridFunctionDefault : virtual public DifferentiableGridFunction<G,RT,m>
00355   {
00357         typedef typename G::ctype DT;
00358 
00360         enum {n=G::dimension};
00361 
00363         typedef typename G::Traits::template Codim<0>::Entity Entity;
00364 
00365   public:
00367 
00374         RT derivativelocal (int comp, const Dune::FieldVector<int,n>& d, 
00375                                                 const Entity& e, const Dune::FieldVector<DT,n>& xi) const
00376         { 
00377           return derivative(comp,d,e.geometry().global(xi));
00378         }
00379   };
00380   
00382   template<class GV, class RT, int m>
00383   class GridFunctionGlobalDifferentiationDefault :
00384     virtual public DifferentiableGridFunction<typename GV::Grid,RT,m>,
00385     virtual public GridFunctionGlobalEvalDefault<GV,RT,m>
00386   {
00388         typedef typename GV::Grid G;
00389 
00391         typedef typename G::ctype DT;
00392 
00394         enum {n=G::dimension};
00395 
00397         typedef typename G::Traits::template Codim<0>::Entity Entity;
00398 
00400         typedef typename G::Traits::template Codim<0>::EntityPointer EntityPointer;
00401 
00402   public:
00403 
00404     GridFunctionGlobalDifferentiationDefault(const GV & _gv) :
00405       GridFunctionGlobalEvalDefault<GV,RT,m>(_gv) {};
00406     
00410     virtual RT derivative (int comp, const Dune::FieldVector<int, n> &d,
00411                            const Dune::FieldVector<DT, n> &x) const
00412         {
00413       EntityPointer e = this->hsearch.findEntity(x);
00414       return derivativelocal(comp,d,*e,e->geometry().local(x));
00415         }
00416   };
00417 
00419 
00429   template<class DT, class RT, int n, int m>
00430   class C0Function : virtual public FunctionBase<DT,RT,n,m>
00431   {
00432   };
00433 
00435 
00444   template<class G, class RT, int m>
00445   class C0GridFunction : virtual public GridFunction<G,RT,m>,
00446                                                  virtual public C0Function<typename G::ctype,RT,G::dimension,m>
00447   {
00448   };
00449   
00450 
00452 
00462   template<class DT, class RT, int n, int m>
00463   class C1Function : virtual public DifferentiableFunction<DT,RT,n,m>,
00464                                          virtual public C0Function<DT,RT,n,m>
00465   {
00466   public:
00468 
00470         virtual int order () const {return 1;}
00471   };
00472 
00473 
00474 
00476 
00487   template<class DT, class RT, int n, int m>
00488   class L2Function : virtual public FunctionBase<DT,RT,n,m>
00489   {
00490   };
00491 
00492 
00493 
00495 
00504   template<class DT, class RT, int n, int m>
00505   class H1Function : virtual public DifferentiableFunction<DT,RT,n,m>,
00506                                          virtual public L2Function<DT,RT,n,m>
00507   {
00508   public:
00510 
00512         virtual int order () const {return 1;}
00513   };
00514 
00515 
00517 
00526   template<class DT, class RT, int n, int m>
00527   class HdivFunction : virtual public DifferentiableFunction<DT,RT,n,m>,
00528                                          virtual public L2Function<DT,RT,n,m>
00529   {
00530   public:
00532 
00534         virtual int order () const {return 1;}
00535   };
00536 
00537 
00538 
00540 
00549   template<class G, class RT, int m>
00550   class ElementwiseCInfinityFunction : virtual public DifferentiableGridFunction<G,RT,m>
00551   {
00552   public:
00554 
00556         virtual int order () const {return InfinitelyDifferentiable;}
00557   };
00558   
00559 
00560   // Wrapper for VTK output of grid functions
00561   template<class GV, class RT, int m>
00562   class VTKGridFunctionWrapper : public VTKWriter<GV>::VTKFunction  
00563   {
00564       enum {n=GV::dimension};
00565       typedef typename GV::Grid::ctype DT;
00566         typedef typename GV::template Codim<0>::Entity Entity;
00567 
00568   public:
00570         virtual int ncomps () const
00571         {
00572           return m;
00573         }
00574 
00576 
00582         virtual double evaluate (int comp, const Entity& e, const Dune::FieldVector<DT,n>& xi) const
00583         {
00584           return func.evallocal(comp,e,xi);
00585         }
00586           
00587         // get name
00588         virtual std::string name () const
00589         {
00590           return myname;
00591         }
00592 
00593         // constructor remembers reference to a grid function
00594       VTKGridFunctionWrapper (const GridFunction<typename GV::Grid,RT,m>& f, std::string s) : func(f), myname(s)
00595         {}
00596 
00597         virtual ~VTKGridFunctionWrapper() {}
00598       
00599   private:
00600       const GridFunction<typename GV::Grid,RT,m>& func;
00601         std::string myname;
00602   };
00603 
00604   
00605 
00608 }
00609 #endif

Generated on 6 Jan 2009 with Doxygen (ver 1.5.1) [logfile].