dune-pdelab  2.5-dev
borderindexidcache.hh
Go to the documentation of this file.
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=8 sw=2 sts=2:
3 #ifndef DUNE_PDELAB_COMMON_BORDERINDEXIDCACHE_HH
4 #define DUNE_PDELAB_COMMON_BORDERINDEXIDCACHE_HH
5 
6 #include <vector>
7 #include <utility>
8 #include <unordered_map>
9 
10 #include <dune/common/typetraits.hh>
11 #include <dune/geometry/dimension.hh>
12 #include <dune/geometry/typeindex.hh>
13 #include <dune/grid/common/gridenums.hh>
14 #include <dune/grid/common/capabilities.hh>
15 #include <dune/grid/common/partitionset.hh>
16 
17 namespace Dune {
18  namespace PDELab {
19 
20 
24 
25 
26  template<typename GFS>
28  {
29 
30  typedef GFS GridFunctionSpace;
32  typedef typename GFS::Traits::GridView GridView;
33  typedef typename GridView::Grid Grid;
34 
35  typedef std::size_t size_type;
36  using index_type = typename EntitySet::Traits::Index;
37  typedef typename GFS::Traits::GridView::Grid::GlobalIdSet::IdType id_type;
38 
39 
40  struct EntityIndex
41  : public std::pair<std::size_t,std::size_t>
42  {
43 
44  typedef std::size_t size_type;
45 
47  {}
48 
49  EntityIndex(size_type gt_index, size_type entity_index)
50  : std::pair<size_type,size_type>(gt_index,entity_index)
51  {}
52 
53  size_type geometryTypeIndex() const
54  {
55  return this->first;
56  }
57 
58  size_type entityIndex() const
59  {
60  return this->second;
61  }
62 
63  };
64 
65 
66  typedef std::vector<
67  std::vector<
68  bool
69  >
71 
72  typedef std::vector<
73  std::unordered_map<
74  index_type,
75  id_type
76  >
78 
79  typedef std::unordered_map<
80  id_type,
83 
84  BorderIndexIdCache(const GFS& gfs)
85  : _gfs(gfs)
86  , _entity_set(gfs.entitySet())
87  {
88  update();
89  }
90 
91  void update()
92  {
93  _border_entities.resize(GlobalGeometryTypeIndex::size(Grid::dimension));
94  _index_to_id.resize(GlobalGeometryTypeIndex::size(Grid::dimension));
95 
96  auto& index_set = _entity_set.indexSet();
97 
98  // Skip codim 0 - cells can't ever be border entities
99  for (int codim = 1; codim <= Grid::dimension; ++codim)
100  {
101  if (!_gfs.ordering().contains(codim))
102  continue;
103 
104  for (auto gt : index_set.types(codim))
105  {
106  _border_entities[GlobalGeometryTypeIndex::index(gt)].resize(index_set.size(gt));
107  _index_to_id[GlobalGeometryTypeIndex::index(gt)];
108  }
109  }
110  create_for_codim<Grid::dimension>();
111  }
112 
113  bool isBorderEntity(std::size_t gt_index, std::size_t entity_index) const
114  {
115  return _border_entities[gt_index][entity_index];
116  }
117 
118  id_type id(std::size_t gt_index,index_type entity_index) const
119  {
120  typename IndexToIdMap::value_type::const_iterator it = _index_to_id[gt_index].find(entity_index);
121  if (it == _index_to_id[gt_index].end())
122  {
123  DUNE_THROW(Dune::Exception,"invalid argument (entity not in map)");
124  }
125  return it->second;
126  }
127 
128  EntityIndex index(id_type entity_id) const
129  {
130  typename IdToIndexMap::const_iterator it = _id_to_index.find(entity_id);
131  if (it == _id_to_index.end())
132  {
133  DUNE_THROW(Dune::Exception,"invalid argument (entity not in map)");
134  }
135  return it->second;
136  }
137 
138  std::pair<bool,EntityIndex> findIndex(id_type entity_id) const
139  {
140  typename IdToIndexMap::const_iterator it = _id_to_index.find(entity_id);
141  if (it == _id_to_index.end())
142  return std::make_pair(false,EntityIndex());
143  else
144  return std::make_pair(true,it->second);
145  }
146 
147  private:
148 
149  const GFS& _gfs;
150  EntitySet _entity_set;
151  BorderEntitySet _border_entities;
152  IndexToIdMap _index_to_id;
153  IdToIndexMap _id_to_index;
154 
155  template<int codim>
156  typename std::enable_if<
157  (codim > 0) && Capabilities::hasEntity<Grid,codim>::v
158  >::type
159  create_for_codim()
160  {
161  auto& index_set = _entity_set.indexSet();
162  auto& id_set = _entity_set.gridView().grid().globalIdSet();
163 
164  if (_gfs.ordering().contains(codim))
165  {
166  for (const auto& e : entities(_entity_set,Codim<codim>{},Partitions::interiorBorder))
167  {
168  index_type index = index_set.index(e);
169  size_type gt_index = GlobalGeometryTypeIndex::index(e.type());
170 
171  bool border_entity = _border_entities[gt_index][index] = (e.partitionType() == BorderEntity);
172  if (!border_entity)
173  continue;
174 
175  id_type id = id_set.id(e);
176 
177  _index_to_id[gt_index][index] = id;
178  _id_to_index[id] = EntityIndex(gt_index,index);
179  }
180  }
181  create_for_codim<codim-1>();
182  }
183 
184  template<int codim>
185  typename std::enable_if<
186  (codim > 0) && !Capabilities::hasEntity<Grid,codim>::v
187  >::type
188  create_for_codim()
189  {
190  if (_gfs.ordering().contains(codim))
191  DUNE_THROW(Dune::Exception,"Required codim " << codim << " not supported by grid!");
192  create_for_codim<codim-1>();
193  }
194 
195  template<int codim>
196  typename std::enable_if<
197  (codim == 0)
198  >::type
199  create_for_codim()
200  {}
201 
202  };
203 
204  } // namespace PDELab
205 } // namespace Dune
206 
207 #endif // DUNE_PDELAB_COMMON_BORDERINDEXIDCACHE_HH
id_type id(std::size_t gt_index, index_type entity_index) const
Definition: borderindexidcache.hh:118
std::size_t size_type
Definition: borderindexidcache.hh:35
STL namespace.
GridView::Grid Grid
Definition: borderindexidcache.hh:33
std::size_t size_type
Definition: borderindexidcache.hh:44
Definition: borderindexidcache.hh:27
GFS::Traits::GridView::Grid::GlobalIdSet::IdType id_type
Definition: borderindexidcache.hh:37
impl::EntitySet< G > EntitySet
the entity set of this function space.
Definition: gridfunctionspace.hh:120
typename GridFunctionSpace::Traits::EntitySet EntitySet
Definition: borderindexidcache.hh:31
EntityIndex(size_type gt_index, size_type entity_index)
Definition: borderindexidcache.hh:49
bool isBorderEntity(std::size_t gt_index, std::size_t entity_index) const
Definition: borderindexidcache.hh:113
EntityIndex()
Definition: borderindexidcache.hh:46
void update()
Definition: borderindexidcache.hh:91
size_type entityIndex() const
Definition: borderindexidcache.hh:58
std::pair< bool, EntityIndex > findIndex(id_type entity_id) const
Definition: borderindexidcache.hh:138
size_type geometryTypeIndex() const
Definition: borderindexidcache.hh:53
std::vector< std::unordered_map< index_type, id_type > > IndexToIdMap
Definition: borderindexidcache.hh:77
GFS GridFunctionSpace
Definition: borderindexidcache.hh:30
BorderIndexIdCache(const GFS &gfs)
Definition: borderindexidcache.hh:84
std::unordered_map< id_type, EntityIndex > IdToIndexMap
Definition: borderindexidcache.hh:82
EntityIndex index(id_type entity_id) const
Definition: borderindexidcache.hh:128
For backward compatibility – Do not use this!
Definition: adaptivity.hh:27
GFS::Traits::GridView GridView
Definition: borderindexidcache.hh:32
const Entity & e
Definition: localfunctionspace.hh:111
typename EntitySet::Traits::Index index_type
Definition: borderindexidcache.hh:36
Definition: borderindexidcache.hh:40
std::vector< std::vector< bool > > BorderEntitySet
Definition: borderindexidcache.hh:70