dune-grid  2.3beta2
mcmgmapper.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_MCMGMAPPER_HH
5 #define DUNE_MCMGMAPPER_HH
6 
7 #include <iostream>
8 #include <map>
9 
10 #include <dune/geometry/referenceelements.hh>
11 #include <dune/geometry/type.hh>
12 #include <dune/geometry/typeindex.hh>
13 
14 #include "mapper.hh"
15 
22 namespace Dune
23 {
30 
31  //
32  // Common Layout templates
33  //
34 
36 
43  template<int dimgrid> struct MCMGElementLayout {
46  bool contains (Dune::GeometryType gt) { return gt.dim()==dimgrid; }
47  };
48 
50 
57  template<int dim> struct MCMGVertexLayout {
60  bool contains (Dune::GeometryType gt) { return gt.dim()==0; }
61  };
62 
64  //
65  // MultipleCodimMultipleGeomTypeMapper
66  //
67 
101  template <typename GV, template<int> class Layout>
103  public Mapper<typename GV::Grid,MultipleCodimMultipleGeomTypeMapper<GV,Layout> >
104  {
105  public:
106 
107  // the following lines need to be skipped for intel compilers, because they
108  // lead to ambiguous calls to methods
109 #ifndef __INTEL_COMPILER
110 
111 
114 #endif
115 
124  MultipleCodimMultipleGeomTypeMapper (const GV& gridView_, const Layout<GV::dimension> layout)
125  : gridView(gridView_),
126  is(gridView.indexSet()),
127  offset(GlobalGeometryTypeIndex::size(GV::dimension)),
128  layout(layout)
129  {
130  update();
131  }
132 
138  : gridView(gridView_),
139  is(gridView.indexSet()),
140  offset(GlobalGeometryTypeIndex::size(GV::dimension))
141  {
142  update();
143  }
144 
152  template<class EntityType>
153  int map (const EntityType& e) const
154  {
155  const GeometryType gt = e.type();
156  return is.index(e) + offset[GlobalGeometryTypeIndex::index(gt)];
157  }
158 
166  int map (const typename GV::template Codim<0>::Entity& e, int i, unsigned int codim) const
167  {
168  GeometryType gt=ReferenceElements<double,GV::dimension>::general(e.type()).type(i,codim);
169  assert(GlobalGeometryTypeIndex::index(gt) < n);
170  return is.subIndex(e, i, codim) + offset[GlobalGeometryTypeIndex::index(gt)];
171  }
172 
181  int size () const
182  {
183  return n;
184  }
185 
192  template<class EntityType>
193  bool contains (const EntityType& e, int& result) const
194  {
195  if(!is.contains(e) || !layout.contains(e.type()))
196  {
197  result = 0;
198  return false;
199  }
200  result = map(e);
201  return true;
202  }
203 
212  bool contains (const typename GV::template Codim<0>::Entity& e, int i, int cc, int& result) const
213  {
214  result = this->map(e,i,cc);
215  return true;
216  }
217 
220  void update ()
221  {
222  n = 0;
223 
224  for (unsigned int codim = 0; codim <= GV::dimension; ++codim)
225  {
226  // walk over all geometry types in the codimension
227  typedef std::vector<GeometryType> GTV;
228  const GTV &gtv = is.geomTypes(codim);
229  for (typename GTV::const_iterator it = gtv.begin(); it != gtv.end(); ++it)
230  {
231  // if the geometry type is contained in the layout, increment offset
232  if (layout.contains(*it))
233  {
234  offset[GlobalGeometryTypeIndex::index(*it)] = n;
235  n += is.size(*it);
236  }
237  }
238  }
239  }
240 
241  private:
242  // number of data elements required
243  unsigned int n;
244  // GridView is needed to keep the IndexSet valid
245  const GV gridView;
246  const typename GV::IndexSet& is;
247  // provide an array for the offsets
248  std::vector<int> offset;
249  mutable Layout<GV::dimension> layout; // get layout object
250  };
251 
253  //
254  // Leaf and level mapper
255  //
256 
267  template <typename G, template<int> class Layout>
269  : public MultipleCodimMultipleGeomTypeMapper<typename G::LeafGridView,Layout>
270  {
271  typedef MultipleCodimMultipleGeomTypeMapper<typename G::LeafGridView,
272  Layout> Base;
273  public:
278  : Base(grid.leafView())
279  {}
280 
289  LeafMultipleCodimMultipleGeomTypeMapper (const G& grid, const Layout<G::dimension> layout)
290  : Base(grid.leafView(),layout)
291  {}
292 
293  };
294 
306  template <typename G, template<int> class Layout>
308  : public MultipleCodimMultipleGeomTypeMapper<typename G::LevelGridView,Layout> {
309  typedef MultipleCodimMultipleGeomTypeMapper<typename G::LevelGridView,
310  Layout> Base;
311  public:
316  LevelMultipleCodimMultipleGeomTypeMapper (const G& grid, int level)
317  : Base(grid.levelGridView(level))
318  {}
319 
329  LevelMultipleCodimMultipleGeomTypeMapper (const G& grid, int level, const Layout<G::dimension> layout)
330  : Base(grid.levelGridView(level),layout)
331  {}
332 
333  };
334 
336 }
337 #endif