dune-pdelab  2.0.0
elementmapper.hh
Go to the documentation of this file.
1 #ifndef DUNE_PDELAB_COMMON_ELEMENTMAPPER_HH
2 #define DUNE_PDELAB_COMMON_ELEMENTMAPPER_HH
3 
4 #include <vector>
5 #include <algorithm>
6 #include <numeric>
7 
8 #include <dune/geometry/type.hh>
9 #include <dune/geometry/typeindex.hh>
10 #include <dune/grid/common/capabilities.hh>
11 
12 namespace Dune {
13 
14  namespace PDELab {
15 
16 #ifndef DOXYGEN
17 
18  // implementation for mixed grids
19  template<typename GV, bool has_single_cell_type>
20  class ElementMapperBase
21  {
22 
23  protected:
24 
25  typedef typename GV::template Codim<0>::Entity Element;
26  typedef std::size_t size_type;
27 
28  private:
29 
30  static const size_type dim = GV::dimension;
31  typedef typename GV::IndexSet IndexSet;
32 
33  protected:
34 
35  void update()
36  {
37  // clear old values
38  std::fill(_gt_offsets.begin(),_gt_offsets.end(),0);
39 
40  // extract per-GeometryType sizes in codim 0
41  typedef std::vector<GeometryType> GTVector;
42  const GTVector& geometry_types = _index_set.geomTypes(0);
43  for (typename GTVector::const_iterator it = geometry_types.begin(), end = geometry_types.end();
44  it != end;
45  ++it)
46  {
47  _gt_offsets[LocalGeometryTypeIndex::index(*it) + 1] = _index_set.size(*it);
48  }
49 
50  // convert to offsets
51  std::partial_sum(_gt_offsets.begin(),_gt_offsets.end(),_gt_offsets.begin());
52  }
53 
54  size_type map(const Element& e) const
55  {
56  return _gt_offsets[LocalGeometryTypeIndex::index(e.type())] + _index_set.index(e);
57  }
58 
59  ElementMapperBase(const GV& gv)
60  : _gt_offsets(LocalGeometryTypeIndex::size(dim) + 1)
61  , _index_set(gv.indexSet())
62  {
63  update();
64  }
65 
66  private:
67 
68  std::vector<size_type> _gt_offsets;
69  const IndexSet& _index_set;
70 
71  };
72 
73  // implementation for grids with a single codim 0 geometry type
74  template<typename GV>
75  class ElementMapperBase<GV,true>
76  {
77 
78  protected:
79 
80  typedef typename GV::template Codim<0>::Entity Element;
81  typedef typename GV::IndexSet IndexSet;
82  typedef std::size_t size_type;
83 
84  void update()
85  {}
86 
87  size_type map(const Element& e) const
88  {
89  return _index_set.index(e);
90  }
91 
92  ElementMapperBase(const GV& gv)
93  : _index_set(gv.indexSet())
94  {}
95 
96  private:
97 
98  const IndexSet& _index_set;
99 
100  };
101 
102 #endif // DOXYGEN
103 
104 
106 
116  template<typename GV>
118  : public ElementMapperBase<GV,
119  Dune::Capabilities::hasSingleGeometryType<
120  typename GV::Grid
121  >::v
122  >
123  {
124 
125  typedef ElementMapperBase<
126  GV,
127  Dune::Capabilities::hasSingleGeometryType<
128  typename GV::Grid
129  >::v
130  > BaseT;
131 
132  public:
133 
135  typedef typename BaseT::size_type size_type;
136 
138  typedef typename BaseT::Element Element;
139 
141 
144  ElementMapper(const GV& gv)
145  : BaseT(gv)
146  {}
147 
149 
157  size_type map(const Element& e) const
158  {
159  return BaseT::map(e);
160  }
161 
162  };
163 
164  } // namespace PDELab
165 
166 } // namespace Dune
167 
168 #endif // DUNE_PDELAB_COMMON_ELEMENTMAPPER_HH
BaseT::Element Element
The type of the codim 0 entities of the GridView.
Definition: elementmapper.hh:138
ElementMapper(const GV &gv)
Construct a CellIndexProvider for the given GridView.
Definition: elementmapper.hh:144
static const int dim
Definition: adaptivity.hh:82
BaseT::size_type size_type
The type of the returned index.
Definition: elementmapper.hh:135
Class providing a consecutive index for codim 0 entities of a GridView.
Definition: elementmapper.hh:117
size_type map(const Element &e) const
Return the index of the given element.
Definition: elementmapper.hh:157
const E & e
Definition: interpolate.hh:172