dune-grid  2.1.1
function.hh
Go to the documentation of this file.
00001 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002 // vi: set et ts=8 sw=2 sts=2:
00003 
00004 #ifndef DUNE_GRID_IO_FILE_VTK_FUNCTION_HH
00005 #define DUNE_GRID_IO_FILE_VTK_FUNCTION_HH
00006 
00007 #include <string>
00008 
00009 #include <dune/common/exceptions.hh>
00010 #include <dune/common/fvector.hh>
00011 #include <dune/common/geometrytype.hh>
00012 
00013 #include <dune/grid/common/genericreferenceelements.hh>
00014 #include <dune/grid/common/mcmgmapper.hh>
00015 
00021 namespace Dune
00022 {
00025 
00027   //
00028   //  Base VTKFunction
00029   //
00030 
00035   template <class Grid>
00036   class VTKFunction
00037   {
00038   public:
00039     typedef typename Grid::ctype ctype;
00040     enum { dim = Grid::dimension };
00041     typedef typename Grid::template Codim< 0 >::Entity Entity;
00042 
00045     virtual int ncomps () const = 0;
00046 
00048 
00055     virtual double evaluate (int comp, const Entity& e,
00056                              const Dune::FieldVector<ctype,dim>& xi) const = 0;
00057 
00059     virtual std::string name () const = 0;
00060 
00062     virtual ~VTKFunction () {}
00063   };
00064 
00066   //
00067   //  P0VTKFunction
00068   //
00069 
00071 
00085   template<typename GV, typename V>
00086   class P0VTKFunction
00087     : public VTKFunction<typename GV::Grid>
00088   {
00090     typedef VTKFunction<typename GV::Grid> Base;
00092     typedef MultipleCodimMultipleGeomTypeMapper<GV, MCMGElementLayout> Mapper;
00093 
00095     const V& v;
00097     std::string s;
00099     int ncomps_;
00102     int mycomp_;
00104     Mapper mapper;
00105 
00106   public:
00107     typedef typename Base::Entity Entity;
00108     typedef typename Base::ctype ctype;
00109     using Base::dim;
00110 
00112     virtual int ncomps () const
00113     {
00114       return 1;
00115     }
00116 
00118     virtual double evaluate (int comp, const Entity& e,
00119                              const Dune::FieldVector<ctype,dim>& xi) const
00120     {
00121       return v[mapper.map(e)*ncomps_+mycomp_];
00122     }
00123 
00125     virtual std::string name () const
00126     {
00127       return s;
00128     }
00129 
00131 
00147     P0VTKFunction(const GV &gv, const V &v_, const std::string &s_,
00148                   int ncomps=1, int mycomp=0 )
00149       : v( v_ ),
00150         s( s_ ),
00151         ncomps_(ncomps),
00152         mycomp_(mycomp),
00153         mapper( gv )
00154     {
00155       if (v.size()!=(unsigned int)(mapper.size()*ncomps_))
00156         DUNE_THROW(IOError, "P0VTKFunction: size mismatch");
00157     }
00158 
00160     virtual ~P0VTKFunction() {}
00161   };
00162 
00164   //
00165   //  P1VTKFunction
00166   //
00167 
00169 
00188   template<typename GV, typename V>
00189   class P1VTKFunction
00190     : public VTKFunction<typename GV::Grid>
00191   {
00193     typedef VTKFunction<typename GV::Grid> Base;
00195     typedef MultipleCodimMultipleGeomTypeMapper<GV, MCMGVertexLayout> Mapper;
00196 
00198     const V& v;
00200     std::string s;
00202     int ncomps_;
00205     int mycomp_;
00207     Mapper mapper;
00208 
00209   public:
00210     typedef typename Base::Entity Entity;
00211     typedef typename Base::ctype ctype;
00212     using Base::dim;
00213 
00215     virtual int ncomps () const
00216     {
00217       return 1;
00218     }
00219 
00221     virtual double evaluate (int comp, const Entity& e,
00222                              const Dune::FieldVector<ctype,dim>& xi) const
00223     {
00224       double min=1E100;
00225       int imin=-1;
00226       Dune::GeometryType gt = e.type();
00227       for (int i=0; i<e.template count<dim>(); ++i)
00228       {
00229         Dune::FieldVector<ctype,dim> local =
00230           Dune::GenericReferenceElements<ctype,dim>::general(gt)
00231           .position(i,dim);
00232         local -= xi;
00233         if (local.infinity_norm()<min)
00234         {
00235           min = local.infinity_norm();
00236           imin = i;
00237         }
00238       }
00239       return v[mapper.map(e,imin,dim)*ncomps_+mycomp_];
00240     }
00241 
00243     virtual std::string name () const
00244     {
00245       return s;
00246     }
00247 
00249 
00265     P1VTKFunction(const GV& gv, const V &v_, const std::string &s_,
00266                   int ncomps=1, int mycomp=0 )
00267       : v( v_ ),
00268         s( s_ ),
00269         ncomps_(ncomps),
00270         mycomp_(mycomp),
00271         mapper( gv )
00272     {
00273       if (v.size()!=(unsigned int)(mapper.size()*ncomps_))
00274         DUNE_THROW(IOError,"P1VTKFunction: size mismatch");
00275     }
00276 
00278     virtual ~P1VTKFunction() {}
00279   };
00280 
00282 
00283 } // namespace Dune
00284 
00285 #endif // DUNE_GRID_IO_FILE_VTK_FUNCTION_HH