DUNE PDELab (git)

borderindexidcache.hh
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
11#include <dune/geometry/dimension.hh>
13#include <dune/grid/common/gridenums.hh>
15#include <dune/grid/common/partitionset.hh>
16
17namespace Dune {
18 namespace PDELab {
19
20
24
25
26 template<typename GFS>
27 struct BorderIndexIdCache
28 {
29
30 typedef GFS GridFunctionSpace;
31 using EntitySet = typename GridFunctionSpace::Traits::EntitySet;
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
46 EntityIndex()
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 >
70 > BorderEntitySet;
71
72 typedef std::vector<
73 std::unordered_map<
74 index_type,
75 id_type
76 >
77 > IndexToIdMap;
78
79 typedef std::unordered_map<
80 id_type,
81 EntityIndex
82 > IdToIndexMap;
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));
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::hasEntityIterator<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::hasEntityIterator<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
Base class for Dune-Exceptions.
Definition: exceptions.hh:96
static constexpr std::size_t index(const GeometryType &gt)
Compute the index for the given geometry type over all dimensions.
Definition: typeindex.hh:138
static constexpr std::size_t size(std::size_t maxdim)
Compute total number of geometry types up to and including the given dimension.
Definition: typeindex.hh:125
static constexpr int dimension
The dimension of the grid.
Definition: grid.hh:387
A set of traits classes to store static information about grid implementation.
Traits for type conversions and type information.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:158
Traits::Grid Grid
type of the grid
Definition: gridview.hh:83
@ BorderEntity
on boundary between interior and overlap
Definition: gridenums.hh:32
impl::EntitySet< G > EntitySet
the entity set of this function space.
Definition: gridfunctionspace.hh:133
constexpr InteriorBorder interiorBorder
PartitionSet for the interior and border partitions.
Definition: partitionset.hh:286
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
Helper classes to provide indices for geometrytypes for use in a vector.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 23, 23:29, 2024)