dune-grid  2.3beta2
function.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 
4 #ifndef DUNE_GRID_IO_FILE_VTK_FUNCTION_HH
5 #define DUNE_GRID_IO_FILE_VTK_FUNCTION_HH
6 
7 #include <string>
8 
9 #include <dune/common/exceptions.hh>
10 #include <dune/common/fvector.hh>
11 
12 #include <dune/geometry/type.hh>
13 #include <dune/geometry/referenceelements.hh>
14 
16 
22 namespace Dune
23 {
26 
28  //
29  // Base VTKFunction
30  //
31 
36  template< class GridView >
38  {
39  public:
40  typedef typename GridView::ctype ctype;
42  typedef typename GridView::template Codim< 0 >::Entity Entity;
43 
46  virtual int ncomps () const = 0;
47 
49 
56  virtual double evaluate (int comp, const Entity& e,
57  const Dune::FieldVector<ctype,dim>& xi) const = 0;
58 
60  virtual std::string name () const = 0;
61 
63  virtual ~VTKFunction () {}
64  };
65 
67  //
68  // P0VTKFunction
69  //
70 
72 
86  template<typename GV, typename V>
88  : public VTKFunction< GV >
89  {
91  typedef VTKFunction< GV > Base;
94 
96  const V& v;
98  std::string s;
100  int ncomps_;
103  int mycomp_;
105  Mapper mapper;
106 
107  public:
108  typedef typename Base::Entity Entity;
109  typedef typename Base::ctype ctype;
110  using Base::dim;
111 
113  virtual int ncomps () const
114  {
115  return 1;
116  }
117 
119  virtual double evaluate (int comp, const Entity& e,
120  const Dune::FieldVector<ctype,dim>& xi) const
121  {
122  return v[mapper.map(e)*ncomps_+mycomp_];
123  }
124 
126  virtual std::string name () const
127  {
128  return s;
129  }
130 
132 
148  P0VTKFunction(const GV &gv, const V &v_, const std::string &s_,
149  int ncomps=1, int mycomp=0 )
150  : v( v_ ),
151  s( s_ ),
152  ncomps_(ncomps),
153  mycomp_(mycomp),
154  mapper( gv )
155  {
156  if (v.size()!=(unsigned int)(mapper.size()*ncomps_))
157  DUNE_THROW(IOError, "P0VTKFunction: size mismatch");
158  }
159 
161  virtual ~P0VTKFunction() {}
162  };
163 
165  //
166  // P1VTKFunction
167  //
168 
170 
189  template<typename GV, typename V>
191  : public VTKFunction< GV >
192  {
194  typedef VTKFunction< GV > Base;
197 
199  const V& v;
201  std::string s;
203  int ncomps_;
206  int mycomp_;
208  Mapper mapper;
209 
210  public:
211  typedef typename Base::Entity Entity;
212  typedef typename Base::ctype ctype;
213  using Base::dim;
214 
216  virtual int ncomps () const
217  {
218  return 1;
219  }
220 
222  virtual double evaluate (int comp, const Entity& e,
223  const Dune::FieldVector<ctype,dim>& xi) const
224  {
225  double min=1E100;
226  int imin=-1;
227  Dune::GeometryType gt = e.type();
228  for (int i=0; i<e.template count<dim>(); ++i)
229  {
230  Dune::FieldVector<ctype,dim> local =
231  Dune::ReferenceElements<ctype,dim>::general(gt)
232  .position(i,dim);
233  local -= xi;
234  if (local.infinity_norm()<min)
235  {
236  min = local.infinity_norm();
237  imin = i;
238  }
239  }
240  return v[mapper.map(e,imin,dim)*ncomps_+mycomp_];
241  }
242 
244  virtual std::string name () const
245  {
246  return s;
247  }
248 
250 
266  P1VTKFunction(const GV& gv, const V &v_, const std::string &s_,
267  int ncomps=1, int mycomp=0 )
268  : v( v_ ),
269  s( s_ ),
270  ncomps_(ncomps),
271  mycomp_(mycomp),
272  mapper( gv )
273  {
274  if (v.size()!=(unsigned int)(mapper.size()*ncomps_))
275  DUNE_THROW(IOError,"P1VTKFunction: size mismatch");
276  }
277 
279  virtual ~P1VTKFunction() {}
280  };
281 
283 
284 } // namespace Dune
285 
286 #endif // DUNE_GRID_IO_FILE_VTK_FUNCTION_HH