dune-pdelab 2.10-git
Loading...
Searching...
No Matches
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
14#include <dune/grid/common/capabilities.hh>
16
17namespace 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
36 using index_type = typename EntitySet::Traits::Index;
37 typedef typename GFS::Traits::GridView::Grid::GlobalIdSet::IdType id_type;
38
39
41 : public std::pair<std::size_t,std::size_t>
42 {
43
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
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<
68 bool
69 >
71
72 typedef std::vector<
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 // clean data structures
99 // Skip codim 0 - cells can't ever be border entities
100 for (int codim = 1; codim <= Grid::dimension; ++codim)
101 {
102 if (!_gfs.ordering().contains(codim))
103 continue;
104
105 for (auto gt : index_set.types(codim))
106 {
107 _border_entities[GlobalGeometryTypeIndex::index(gt)].resize(index_set.size(gt));
108 _index_to_id[GlobalGeometryTypeIndex::index(gt)].clear();
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
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 EntitySet _entity_set;
152 BorderEntitySet _border_entities;
153 IndexToIdMap _index_to_id;
154 IdToIndexMap _id_to_index;
155
156 template<int codim>
157 typename std::enable_if<
159 >::type
160 create_for_codim()
161 {
162 auto& index_set = _entity_set.indexSet();
163 auto& id_set = _entity_set.gridView().grid().globalIdSet();
164
165 if (_gfs.ordering().contains(codim))
166 {
167 for (const auto& e : entities(_entity_set,Codim<codim>{},Partitions::interiorBorder))
168 {
169 index_type index = index_set.index(e);
170 size_type gt_index = GlobalGeometryTypeIndex::index(e.type());
171
172 bool border_entity = _border_entities[gt_index][index] = (e.partitionType() == BorderEntity);
173 if (!border_entity)
174 continue;
175
176 id_type id = id_set.id(e);
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 std::enable_if<
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 std::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
int id()
iterator end()
#define DUNE_THROW(E,...)
std::size_t index
Definition interpolate.hh:97
impl::EntitySet< G > EntitySet
the entity set of this function space.
Definition gridfunctionspace.hh:131
GFS::Traits::GridView::Grid::GlobalIdSet::IdType id_type
Definition borderindexidcache.hh:37
typename EntitySet::Traits::Index index_type
Definition borderindexidcache.hh:36
EntityIndex index(id_type entity_id) const
Definition borderindexidcache.hh:129
std::unordered_map< id_type, EntityIndex > IdToIndexMap
Definition borderindexidcache.hh:82
typename GridFunctionSpace::Traits::EntitySet EntitySet
Definition borderindexidcache.hh:31
bool isBorderEntity(std::size_t gt_index, std::size_t entity_index) const
Definition borderindexidcache.hh:114
std::vector< std::unordered_map< index_type, id_type > > IndexToIdMap
Definition borderindexidcache.hh:77
GFS GridFunctionSpace
Definition borderindexidcache.hh:30
size_type geometryTypeIndex() const
Definition borderindexidcache.hh:53
std::size_t size_type
Definition borderindexidcache.hh:35
std::vector< std::vector< bool > > BorderEntitySet
Definition borderindexidcache.hh:70
BorderIndexIdCache(const GFS &gfs)
Definition borderindexidcache.hh:84
size_type entityIndex() const
Definition borderindexidcache.hh:58
EntityIndex()
Definition borderindexidcache.hh:46
void update()
Definition borderindexidcache.hh:91
std::pair< bool, EntityIndex > findIndex(id_type entity_id) const
Definition borderindexidcache.hh:139
EntityIndex(size_type gt_index, size_type entity_index)
Definition borderindexidcache.hh:49
id_type id(std::size_t gt_index, index_type entity_index) const
Definition borderindexidcache.hh:119
GFS::Traits::GridView GridView
Definition borderindexidcache.hh:32
GridView::Grid Grid
Definition borderindexidcache.hh:33
std::size_t size_type
Definition borderindexidcache.hh:44
STL namespace.
For backward compatibility – Do not use this!
BorderEntity
constexpr InteriorBorder interiorBorder
static constexpr std::size_t index(const GeometryType &gt)
static constexpr std::size_t size(std::size_t maxdim)
Definition borderindexidcache.hh:28
Definition borderindexidcache.hh:42
T clear(T... args)
T end(T... args)
T find(T... args)
T make_pair(T... args)
T resize(T... args)