dune-pdelab  2.5-dev
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  for (auto gt : _index_set.types(0))
42  {
43  _gt_offsets[LocalGeometryTypeIndex::index(gt) + 1] = _index_set.size(gt);
44  }
45 
46  // convert to offsets
47  std::partial_sum(_gt_offsets.begin(),_gt_offsets.end(),_gt_offsets.begin());
48  }
49 
50  size_type map(const Element& e) const
51  {
52  return _gt_offsets[LocalGeometryTypeIndex::index(e.type())] + _index_set.index(e);
53  }
54 
55  ElementMapperBase(const GV& gv)
56  : _gt_offsets(LocalGeometryTypeIndex::size(dim) + 1)
57  , _index_set(gv.indexSet())
58  {
59  update();
60  }
61 
62  private:
63 
64  std::vector<size_type> _gt_offsets;
65  const IndexSet& _index_set;
66 
67  };
68 
69  // implementation for grids with a single codim 0 geometry type
70  template<typename GV>
71  class ElementMapperBase<GV,true>
72  {
73 
74  protected:
75 
76  typedef typename GV::template Codim<0>::Entity Element;
77  typedef typename GV::IndexSet IndexSet;
78  typedef std::size_t size_type;
79 
80  void update()
81  {}
82 
83  size_type map(const Element& e) const
84  {
85  return _index_set.index(e);
86  }
87 
88  ElementMapperBase(const GV& gv)
89  : _index_set(gv.indexSet())
90  {}
91 
92  private:
93 
94  const IndexSet& _index_set;
95 
96  };
97 
98 #endif // DOXYGEN
99 
100 
102 
112  template<typename GV>
114  : public ElementMapperBase<GV,
115  Dune::Capabilities::hasSingleGeometryType<
116  typename GV::Grid
117  >::v
118  >
119  {
120 
121  typedef ElementMapperBase<
122  GV,
123  Dune::Capabilities::hasSingleGeometryType<
124  typename GV::Grid
125  >::v
126  > BaseT;
127 
128  public:
129 
131  typedef typename BaseT::size_type size_type;
132 
134  typedef typename BaseT::Element Element;
135 
137 
140  ElementMapper(const GV& gv)
141  : BaseT(gv)
142  {}
143 
145 
153  size_type map(const Element& e) const
154  {
155  return BaseT::map(e);
156  }
157 
158  };
159 
160  } // namespace PDELab
161 
162 } // namespace Dune
163 
164 #endif // DUNE_PDELAB_COMMON_ELEMENTMAPPER_HH
Dune::PDELab::ElementMapper::Element
BaseT::Element Element
The type of the codim 0 entities of the GridView.
Definition: elementmapper.hh:134
dim
static const int dim
Definition: adaptivity.hh:84
index
std::size_t index
Definition: interpolate.hh:118
Dune
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
Dune::PDELab::ElementMapper::map
size_type map(const Element &e) const
Return the index of the given element.
Definition: elementmapper.hh:153
Dune::PDELab::ElementMapper::ElementMapper
ElementMapper(const GV &gv)
Construct a CellIndexProvider for the given GridView.
Definition: elementmapper.hh:140
Dune::PDELab::ElementMapper
Class providing a consecutive index for codim 0 entities of a GridView.
Definition: elementmapper.hh:113
e
const Entity & e
Definition: localfunctionspace.hh:120
Dune::PDELab::ElementMapper::size_type
BaseT::size_type size_type
The type of the returned index.
Definition: elementmapper.hh:131