dune-pdelab  2.0.0
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 
9 #include <dune/common/typetraits.hh>
10 #include <dune/geometry/typeindex.hh>
11 #include <dune/grid/common/gridenums.hh>
12 #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 
51  {
52  return this->first;
53  }
54 
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<
71  index_type,
72  id_type
73  >
75 
76  typedef 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  const std::vector<GeometryType>& geometry_types = index_set.geomTypes(codim);
102  for (typename std::vector<GeometryType>::const_iterator it = geometry_types.begin(),
103  end_it = geometry_types.end();
104  it != end_it;
105  ++it)
106  {
107  _border_entities[GlobalGeometryTypeIndex::index(*it)].resize(index_set.size(*it));
108  _index_to_id[GlobalGeometryTypeIndex::index(*it)];
109  }
110  }
111  create_for_codim<Grid::dimension>();
112  }
113 
114  bool isBorderEntity(std::size_t gt_index, std::size_t entity_index) const
115  {
116  return _border_entities[gt_index][entity_index];
117  }
118 
119  id_type id(std::size_t gt_index,index_type entity_index) const
120  {
121  typename IndexToIdMap::value_type::const_iterator it = _index_to_id[gt_index].find(entity_index);
122  if (it == _index_to_id[gt_index].end())
123  {
124  DUNE_THROW(Dune::Exception,"invalid argument (entity not in map)");
125  }
126  return it->second;
127  }
128 
129  EntityIndex index(id_type entity_id) const
130  {
131  typename IdToIndexMap::const_iterator it = _id_to_index.find(entity_id);
132  if (it == _id_to_index.end())
133  {
134  DUNE_THROW(Dune::Exception,"invalid argument (entity not in map)");
135  }
136  return it->second;
137  }
138 
139  std::pair<bool,EntityIndex> findIndex(id_type entity_id) const
140  {
141  typename IdToIndexMap::const_iterator it = _id_to_index.find(entity_id);
142  if (it == _id_to_index.end())
143  return std::make_pair(false,EntityIndex());
144  else
145  return std::make_pair(true,it->second);
146  }
147 
148  private:
149 
150  const GFS& _gfs;
151  GridView _grid_view;
152  BorderEntitySet _border_entities;
153  IndexToIdMap _index_to_id;
154  IdToIndexMap _id_to_index;
155 
156  template<int codim>
157  typename enable_if<
158  (codim > 0) && Capabilities::hasEntity<Grid,codim>::v
159  >::type
160  create_for_codim()
161  {
162  const typename GridView::IndexSet& index_set = _grid_view.indexSet();
163  const typename Grid::GlobalIdSet& id_set = _grid_view.grid().globalIdSet();
164 
165  if (_gfs.ordering().contains(codim))
166  {
167  typedef typename GridView::template Codim<codim>::template Partition<InteriorBorder_Partition>::Iterator EntityIterator;
168  for (EntityIterator it = _grid_view.template begin<codim,InteriorBorder_Partition>(),
169  end_it = _grid_view.template end<codim,InteriorBorder_Partition>();
170  it != end_it;
171  ++it)
172  {
173  index_type index = index_set.index(*it);
174  size_type gt_index = GlobalGeometryTypeIndex::index(it->type());
175 
176  bool border_entity = _border_entities[gt_index][index] = (it->partitionType() == BorderEntity);
177  if (!border_entity)
178  continue;
179 
180  id_type id = id_set.id(*it);
181 
182  _index_to_id[gt_index][index] = id;
183  _id_to_index[id] = EntityIndex(gt_index,index);
184  }
185  }
186  create_for_codim<codim-1>();
187  }
188 
189  template<int codim>
190  typename enable_if<
191  (codim > 0) && !Capabilities::hasEntity<Grid,codim>::v
192  >::type
193  create_for_codim()
194  {
195  if (_gfs.ordering().contains(codim))
196  DUNE_THROW(Dune::Exception,"Required codim " << codim << " not supported by grid!");
197  create_for_codim<codim-1>();
198  }
199 
200  template<int codim>
201  typename enable_if<
202  (codim == 0)
203  >::type
204  create_for_codim()
205  {}
206 
207  };
208 
209  } // namespace PDELab
210 } // namespace Dune
211 
212 #endif // DUNE_PDELAB_COMMON_BORDERINDEXIDCACHE_HH
Definition: borderindexidcache.hh:37
std::size_t size_type
Definition: borderindexidcache.hh:32
Provide common name for std::unordered_map and std::unordered_multimap classes in Dune::PDELab namesp...
EntityIndex()
Definition: borderindexidcache.hh:43
std::vector< std::vector< bool > > BorderEntitySet
Definition: borderindexidcache.hh:67
bool isBorderEntity(std::size_t gt_index, std::size_t entity_index) const
Definition: borderindexidcache.hh:114
id_type id(std::size_t gt_index, index_type entity_index) const
Definition: borderindexidcache.hh:119
std::vector< unordered_map< index_type, id_type > > IndexToIdMap
Definition: borderindexidcache.hh:74
GFS::Traits::GridView GridView
Definition: borderindexidcache.hh:29
std::pair< bool, EntityIndex > findIndex(id_type entity_id) const
Definition: borderindexidcache.hh:139
size_type entityIndex() const
Definition: borderindexidcache.hh:55
unordered_map< id_type, EntityIndex > IdToIndexMap
Definition: borderindexidcache.hh:79
EntityIndex(size_type gt_index, size_type entity_index)
Definition: borderindexidcache.hh:46
GFS::Traits::GridView::Grid::GlobalIdSet::IdType id_type
Definition: borderindexidcache.hh:34
size_type geometryTypeIndex() const
Definition: borderindexidcache.hh:50
void update()
Definition: borderindexidcache.hh:88
std::size_t size_type
Definition: borderindexidcache.hh:41
Definition: borderindexidcache.hh:25
Definition: unordered_map.hh:46
GridView::Grid Grid
Definition: borderindexidcache.hh:30
GFS::Traits::GridView::IndexSet::IndexType index_type
Definition: borderindexidcache.hh:33
GFS GridFunctionSpace
Definition: borderindexidcache.hh:28
EntityIndex index(id_type entity_id) const
Definition: borderindexidcache.hh:129
BorderIndexIdCache(const GFS &gfs)
Definition: borderindexidcache.hh:81