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  typedef typename GridView::template Codim<codim>::template Partition<InteriorBorder_Partition>::Iterator EntityIterator;
164  for (EntityIterator it = _grid_view.template begin<codim,InteriorBorder_Partition>(),
165  end_it = _grid_view.template end<codim,InteriorBorder_Partition>();
166  it != end_it;
167  ++it)
168  {
169  index_type index = index_set.index(*it);
170  size_type gt_index = GlobalGeometryTypeIndex::index(it->type());
171 
172  bool border_entity = _border_entities[gt_index][index] = (it->partitionType() == BorderEntity);
173  if (!border_entity)
174  continue;
175 
176  id_type id = id_set.id(*it);
177 
178  _index_to_id[gt_index][index] = id;
179  _id_to_index[id] = EntityIndex(gt_index,index);
180  }
181  }
182  create_for_codim<codim-1>();
183  }
184 
185  template<int codim>
186  typename enable_if<
187  (codim > 0) && !Capabilities::hasEntity<Grid,codim>::v
188  >::type
189  create_for_codim()
190  {
191  if (_gfs.ordering().contains(codim))
192  DUNE_THROW(Dune::Exception,"Required codim " << codim << " not supported by grid!");
193  create_for_codim<codim-1>();
194  }
195 
196  template<int codim>
197  typename enable_if<
198  (codim == 0)
199  >::type
200  create_for_codim()
201  {}
202 
203  };
204 
205  } // namespace PDELab
206 } // namespace Dune
207 
208 #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
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