dune-grid  2.2.0
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 
00012 #include <dune/geometry/type.hh>
00013 #include <dune/geometry/referenceelements.hh>
00014 
00015 #include <dune/grid/common/mcmgmapper.hh>
00016 
00022 namespace Dune
00023 {
00026 
00028   //
00029   //  Base VTKFunction
00030   //
00031 
00036   template< class GridView >
00037   class VTKFunction
00038   {
00039   public:
00040     typedef typename GridView::ctype ctype;
00041     enum { dim = GridView::dimension };
00042     typedef typename GridView::template Codim< 0 >::Entity Entity;
00043 
00046     virtual int ncomps () const = 0;
00047 
00049 
00056     virtual double evaluate (int comp, const Entity& e,
00057                              const Dune::FieldVector<ctype,dim>& xi) const = 0;
00058 
00060     virtual std::string name () const = 0;
00061 
00063     virtual ~VTKFunction () {}
00064   };
00065 
00067   //
00068   //  P0VTKFunction
00069   //
00070 
00072 
00086   template<typename GV, typename V>
00087   class P0VTKFunction
00088   : public VTKFunction< GV >
00089   {
00091     typedef VTKFunction< GV > Base;
00093     typedef MultipleCodimMultipleGeomTypeMapper<GV, MCMGElementLayout> Mapper;
00094 
00096     const V& v;
00098     std::string s;
00100     int ncomps_;
00103     int mycomp_;
00105     Mapper mapper;
00106 
00107   public:
00108     typedef typename Base::Entity Entity;
00109     typedef typename Base::ctype ctype;
00110     using Base::dim;
00111 
00113     virtual int ncomps () const
00114     {
00115       return 1;
00116     }
00117 
00119     virtual double evaluate (int comp, const Entity& e,
00120                              const Dune::FieldVector<ctype,dim>& xi) const
00121     {
00122       return v[mapper.map(e)*ncomps_+mycomp_];
00123     }
00124 
00126     virtual std::string name () const
00127     {
00128       return s;
00129     }
00130 
00132 
00148     P0VTKFunction(const GV &gv, const V &v_, const std::string &s_,
00149                   int ncomps=1, int mycomp=0 )
00150       : v( v_ ),
00151         s( s_ ),
00152         ncomps_(ncomps),
00153         mycomp_(mycomp),
00154         mapper( gv )
00155     {
00156       if (v.size()!=(unsigned int)(mapper.size()*ncomps_))
00157         DUNE_THROW(IOError, "P0VTKFunction: size mismatch");
00158     }
00159 
00161     virtual ~P0VTKFunction() {}
00162   };
00163 
00165   //
00166   //  P1VTKFunction
00167   //
00168 
00170 
00189   template<typename GV, typename V>
00190   class P1VTKFunction
00191   : public VTKFunction< GV >
00192   {
00194     typedef VTKFunction< GV > Base;
00196     typedef MultipleCodimMultipleGeomTypeMapper<GV, MCMGVertexLayout> Mapper;
00197 
00199     const V& v;
00201     std::string s;
00203     int ncomps_;
00206     int mycomp_;
00208     Mapper mapper;
00209 
00210   public:
00211     typedef typename Base::Entity Entity;
00212     typedef typename Base::ctype ctype;
00213     using Base::dim;
00214 
00216     virtual int ncomps () const
00217     {
00218       return 1;
00219     }
00220 
00222     virtual double evaluate (int comp, const Entity& e,
00223                              const Dune::FieldVector<ctype,dim>& xi) const
00224     {
00225       double min=1E100;
00226       int imin=-1;
00227       Dune::GeometryType gt = e.type();
00228       for (int i=0; i<e.template count<dim>(); ++i)
00229       {
00230         Dune::FieldVector<ctype,dim> local =
00231           Dune::GenericReferenceElements<ctype,dim>::general(gt)
00232           .position(i,dim);
00233         local -= xi;
00234         if (local.infinity_norm()<min)
00235         {
00236           min = local.infinity_norm();
00237           imin = i;
00238         }
00239       }
00240       return v[mapper.map(e,imin,dim)*ncomps_+mycomp_];
00241     }
00242 
00244     virtual std::string name () const
00245     {
00246       return s;
00247     }
00248 
00250 
00266     P1VTKFunction(const GV& gv, const V &v_, const std::string &s_,
00267                   int ncomps=1, int mycomp=0 )
00268       : v( v_ ),
00269         s( s_ ),
00270         ncomps_(ncomps),
00271         mycomp_(mycomp),
00272         mapper( gv )
00273     {
00274       if (v.size()!=(unsigned int)(mapper.size()*ncomps_))
00275         DUNE_THROW(IOError,"P1VTKFunction: size mismatch");
00276     }
00277 
00279     virtual ~P1VTKFunction() {}
00280   };
00281 
00283 
00284 } // namespace Dune
00285 
00286 #endif // DUNE_GRID_IO_FILE_VTK_FUNCTION_HH