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