version 3.9
vertexmapper.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
7
13#ifndef DUMUX_ENRICHED_VERTEX_DOF_MAPPER_HH
14#define DUMUX_ENRICHED_VERTEX_DOF_MAPPER_HH
15
16#include <vector>
17
18#include <dune/common/exceptions.hh>
19#include <dune/common/timer.hh>
20#include <dune/grid/common/mcmgmapper.hh>
21
22#include "enrichmenthelper.hh"
23
24namespace Dumux {
25
34{
35
36public:
51 template< class GridView,
52 class VertexMapper,
53 class CodimOneGridView,
55 static void markVerticesForEnrichment(std::vector<bool>& vertexMarkers,
56 const GridView& gridView,
57 const VertexMapper& vertexMapper,
58 const CodimOneGridView& codimOneGridView,
59 const CodimOneGridAdapter& codimOneGridAdapter)
60 {
61 static constexpr int dim = GridView::dimension;
62 static_assert(CodimOneGridView::dimension == dim-1, "Grid dimension mismatch");
63
64 // reset the markers
65 vertexMarkers.assign(gridView.size(dim), false);
66
67 // first find all bulk grid vertices on the boundary
68 std::vector<bool> isOnBoundary(gridView.size(dim), false);
69 for (const auto& e : elements(gridView))
70 {
71 const auto refElem = referenceElement(e);
72 for (const auto& is : intersections(gridView, e))
73 if (is.boundary())
74 for (int i = 0; i < is.geometry().corners(); ++i)
75 isOnBoundary[ vertexMapper.subIndex( e,
76 refElem.subEntity(is.indexInInside(), 1, i, dim),
77 dim ) ] = true;
78 }
79
80 // mark all vertices on the lower-dimensional grid to be enriched (except immersed boundaries)
81 std::vector<typename GridView::IndexSet::IndexType> vertexIndicesStorage;
82 for (const auto& codimOneElement : elements(codimOneGridView))
83 {
84 // if a codimension one element has 2 or more embedments, we need to enrich
85 if (codimOneGridAdapter.numEmbedments(codimOneElement) >= 2)
86 {
87 for (int i = 0; i < codimOneElement.subEntities(dim-1); ++i)
88 vertexMarkers[ codimOneGridAdapter.bulkGridVertexIndex(codimOneElement.template subEntity<dim-1>(i)) ] = true;
89
90 // however, we have to exclude immersed boundaries
91 const auto refElem = referenceElement(codimOneElement);
92 for (const auto& intersection : intersections(codimOneGridView, codimOneElement))
93 {
94 // skip if intersection is not on boundary
95 if (!intersection.boundary())
96 continue;
97
98 // obtain all bulk grid indices of the lower-dimensional intersection corners
99 const auto numCorners = intersection.geometry().corners();
100 vertexIndicesStorage.resize(numCorners);
101 for (int i = 0; i < numCorners; ++i)
102 {
103 const auto vIdxLocal = refElem.subEntity(intersection.indexInInside(), 1, i, dim-1);
104 vertexIndicesStorage[i] = codimOneGridAdapter.bulkGridVertexIndex( codimOneElement.template subEntity<dim-1>(vIdxLocal) );
105 }
106
107 // if any of the vertices is on an immersed boundary, we must not enrich any of them
108 if (std::any_of(vertexIndicesStorage.begin(), vertexIndicesStorage.end(), [&isOnBoundary] (auto idx) { return !isOnBoundary[idx]; }))
109 std::for_each(vertexIndicesStorage.begin(), vertexIndicesStorage.end(), [&vertexMarkers] (auto idx) { vertexMarkers[idx] = false; });
110 }
111 }
112 }
113 }
114};
115
124template<class GV>
126{
127 static constexpr int dim = GV::dimension;
128 static_assert(dim > 1, "Vertex dof enrichment mapper currently only works for dim > 1!");
129
130 using GIType = typename GV::IndexSet::IndexType;
131 using Vertex = typename GV::template Codim<dim>::Entity;
132 using Element = typename GV::template Codim<0>::Entity;
133 using MCMGMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GV>;
134
135public:
137 using GridView = GV;
139 using GridIndexType = GIType;
140
142 EnrichedVertexDofMapper(const GV& gridView)
143 : gridView_(gridView)
144 , elementMapper_(gridView, Dune::mcmgElementLayout())
145 , vertexMapper_(gridView, Dune::mcmgVertexLayout())
146 {
147 initialize_();
148 }
149
151 EnrichedVertexDofMapper(const GV& gridView, Dune::MCMGLayout layout)
152 : EnrichedVertexDofMapper(gridView)
153 {
154 if ( !( static_cast<bool>(layout(Dune::GeometryTypes::vertex, dim)) ) )
155 DUNE_THROW(Dune::InvalidStateException, "Vertex mapper only makes sense for vertex layout!");
156 }
157
159 GridIndexType subIndex(const Element& e, unsigned int i, unsigned int codim) const
160 {
161 assert(codim == dim && "Only element corners can be mapped by this mapper");
162 return indexMap_[elementMapper_.index(e)][i];
163 }
164
166 GridIndexType vertexIndex(const Element& e, unsigned int i, unsigned int codim) const
167 {
168 assert(codim == dim && "Only element corners can be mapped by this mapper");
169 return vertexMapper_.subIndex(e, i, codim);
170 }
171
173 GridIndexType vertexIndex(const Vertex& v) const
174 {
175 assert(Vertex::Geometry::mydimension == 0 && "Only vertices can be mapped by this mapper");
176 return vertexMapper_.index(v);
177 }
178
183 template< class EntityType >
184 GridIndexType index(const EntityType& e) const
185 {
186 if (hasEnrichedVertices_)
187 DUNE_THROW(Dune::InvalidStateException, "Index map contains enriched vertex dofs. Direct mapping from vertex to index not possible.");
188
189 assert(EntityType::Geometry::mydimension == 0 && "Only vertices can be mapped by this mapper");
190 return vertexMapper_.index(e);
191 }
192
194 std::size_t size() const
195 { return size_; }
196
198 bool isEnriched(const Vertex& v)
199 { return isEnriched_[ vertexMapper_.index(v) ]; }
200
203 void update(const GV& gridView)
204 {
205 gridView_ = gridView;
206 initialize_();
207 }
208
209 void update(GV&& gridView)
210 {
211 gridView_ = std::move(gridView);
212 initialize_();
213 }
214
226 template<class CodimOneGridView, class CodimOneGridAdapter>
227 void enrich(const CodimOneGridView& codimOneGridView,
228 const CodimOneGridAdapter& codimOneGridAdapter,
229 bool verbose = false)
230 {
231 static const int codimOneDim = CodimOneGridView::dimension;
232 static_assert(codimOneDim == dim-1, "Grid dimension mismatch!");
233 static_assert(codimOneDim == 2 || codimOneDim == 1, "Inadmissible codimension one grid dimension");
234 static_assert(int(CodimOneGridView::dimensionworld) == int(GV::dimensionworld), "Grid world dimension mismatch");
235
236 // keep track of time
237 Dune::Timer watch;
238
239 // mark vertices for enrichment using the indicator
240 EnrichmentIndicator::markVerticesForEnrichment(isEnriched_, gridView_, vertexMapper_, codimOneGridView, codimOneGridAdapter);
241
242 // let the helper class do the enrichment of the index map
244 isEnriched_,
245 gridView_,
246 vertexMapper_,
247 elementMapper_,
248 codimOneGridView,
249 codimOneGridAdapter);
250
251 // check if new index map contains enriched dofs
252 hasEnrichedVertices_ = std::any_of(isEnriched_.begin(), isEnriched_.end(), [] (bool isEnriched) { return isEnriched; });
253
254 if (verbose)
255 std::cout << "Vertex dof enrichment took " << watch.elapsed() << " seconds." << std::endl;
256 }
257
258private:
259
261 void initialize_()
262 {
263 size_ = gridView_.size(dim);
264 hasEnrichedVertices_ = false;
265 indexMap_.resize(gridView_.size(0));
266 isEnriched_.resize(gridView_.size(dim), false);
267 for (const auto& e : elements(gridView_))
268 {
269 const auto numCorners = e.geometry().corners();
270 const auto eIdxGlobal = elementMapper_.index(e);
271 indexMap_[eIdxGlobal].resize(numCorners);
272 for (unsigned int i = 0; i < numCorners; ++i)
273 indexMap_[eIdxGlobal][i] = vertexMapper_.subIndex(e, i, dim);
274 }
275 }
276
277 // data members
278 std::size_t size_;
279 GV gridView_;
280 MCMGMapper elementMapper_;
281 MCMGMapper vertexMapper_;
282 bool hasEnrichedVertices_;
283 std::vector<bool> isEnriched_;
284 std::vector< std::vector<GIType> > indexMap_;
285};
286
287} // end namespace Dumux
288
289#endif
Adapter that allows retrieving information on a d-dimensional grid for entities of a (d-1)-dimensiona...
Definition: codimonegridadapter.hh:40
std::size_t numEmbedments(const FacetGridElement &e) const
Returns the number of d-dimensional elements in which the given (d-1)-dimensional element is embedded...
Definition: codimonegridadapter.hh:226
BulkIndexType bulkGridVertexIndex(const FacetGridVertex &v) const
Returns the index within the d-dimensional grid of a vertex of the (d-1)-dimensional grid.
Definition: codimonegridadapter.hh:133
A vertex mapper that allows for enrichment of nodes. Indication on where to enrich the nodes is done ...
Definition: vertexmapper.hh:126
void enrich(const CodimOneGridView &codimOneGridView, const CodimOneGridAdapter &codimOneGridAdapter, bool verbose=false)
Enriches the dof map subject to a (dim-1)-dimensional grid.
Definition: vertexmapper.hh:227
std::size_t size() const
returns the number of dofs managed by this mapper
Definition: vertexmapper.hh:194
GridIndexType vertexIndex(const Element &e, unsigned int i, unsigned int codim) const
map nodal subentity of codim 0 entity to the grid vertex index
Definition: vertexmapper.hh:166
GridIndexType index(const EntityType &e) const
Definition: vertexmapper.hh:184
void update(const GV &gridView)
Definition: vertexmapper.hh:203
EnrichedVertexDofMapper(const GV &gridView)
the constructor
Definition: vertexmapper.hh:142
GridIndexType vertexIndex(const Vertex &v) const
map nodal entity to the grid vertex index
Definition: vertexmapper.hh:173
GIType GridIndexType
export the grid index type
Definition: vertexmapper.hh:139
GridIndexType subIndex(const Element &e, unsigned int i, unsigned int codim) const
map nodal subentity of codim 0 entity to the grid dof
Definition: vertexmapper.hh:159
GV GridView
export the underlying grid view type
Definition: vertexmapper.hh:137
EnrichedVertexDofMapper(const GV &gridView, Dune::MCMGLayout layout)
constructor taking a layout as additional argument (for compatibility)
Definition: vertexmapper.hh:151
bool isEnriched(const Vertex &v)
returns true if a vertex dof had been enriched
Definition: vertexmapper.hh:198
void update(GV &&gridView)
Definition: vertexmapper.hh:209
An indicator class used to mark vertices for enrichment. This implementation marks all vertices of a ...
Definition: vertexmapper.hh:34
static void markVerticesForEnrichment(std::vector< bool > &vertexMarkers, const GridView &gridView, const VertexMapper &vertexMapper, const CodimOneGridView &codimOneGridView, const CodimOneGridAdapter &codimOneGridAdapter)
Function that marks vertices for enrichment. This implementation works on the basis of a facet-confor...
Definition: vertexmapper.hh:55
static std::size_t enrich(IndexMap &indexMap, const std::vector< bool > &vertexMarkers, const GridView &gridView, const MCMGMapper &vertexMapper, const MCMGMapper &elementMapper, const CodimOneGridView &codimOneGridView, const CodimOneGridAdapter &codimOneGridAdapter)
Enriches the dof map subject to a (dim-1)-dimensional grid.
Definition: enrichmenthelper.hh:77
std::size_t numCorners(Shape shape)
Returns the number of corners of a given geometry.
Definition: throatproperties.hh:220
Definition: adapt.hh:17
Definition: common/pdesolver.hh:24