3.3.0
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
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 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
19
25#ifndef DUMUX_ENRICHED_VERTEX_DOF_MAPPER_HH
26#define DUMUX_ENRICHED_VERTEX_DOF_MAPPER_HH
27
28#include <vector>
29
30#include <dune/common/exceptions.hh>
31#include <dune/common/timer.hh>
32#include <dune/grid/common/mcmgmapper.hh>
33
34#include "enrichmenthelper.hh"
35
36namespace Dumux {
37
46{
47
48public:
63 template< class GridView,
64 class VertexMapper,
65 class CodimOneGridView,
67 static void markVerticesForEnrichment(std::vector<bool>& vertexMarkers,
68 const GridView& gridView,
69 const VertexMapper& vertexMapper,
70 const CodimOneGridView& codimOneGridView,
71 const CodimOneGridAdapter& codimOneGridAdapter)
72 {
73 static constexpr int dim = GridView::dimension;
74 static_assert(CodimOneGridView::dimension == dim-1, "Grid dimension mismatch");
75
76 // reset the markers
77 vertexMarkers.assign(gridView.size(dim), false);
78
79 // first find all bulk grid vertices on the boundary
80 std::vector<bool> isOnBoundary(gridView.size(dim), false);
81 for (const auto& e : elements(gridView))
82 {
83 const auto refElem = referenceElement(e);
84 for (const auto& is : intersections(gridView, e))
85 if (is.boundary())
86 for (int i = 0; i < is.geometry().corners(); ++i)
87 isOnBoundary[ vertexMapper.subIndex( e,
88 refElem.subEntity(is.indexInInside(), 1, i, dim),
89 dim ) ] = true;
90 }
91
92 // for now, set all markers to true for vertices on codim one grid
93 for (const auto& vertex : vertices(codimOneGridView))
94 vertexMarkers[codimOneGridAdapter.bulkGridVertexIndex(vertex)] = true;
95
96 // unmark where necessary
97 for (const auto& codimOneElement : elements(codimOneGridView))
98 {
99 // if a codimension one element has less than two embedments we do not need to enrich
100 if (codimOneGridAdapter.numEmbedments(codimOneElement) < 2)
101 for (int i = 0; i < codimOneElement.subEntities(dim-1); ++i)
102 vertexMarkers[ codimOneGridAdapter.bulkGridVertexIndex(codimOneElement.template subEntity<dim-1>(i)) ] = false;
103
104 // otherwise, we check for immersed boundaries where we also must not enrich
105 else
106 {
107 const auto refElem = referenceElement(codimOneElement);
108 for (const auto& intersection : intersections(codimOneGridView, codimOneElement))
109 {
110 // skip if intersection is not on boundary
111 if (!intersection.boundary())
112 continue;
113
114 // obtain all grid indices of the intersection corners
115 const auto numCorners = intersection.geometry().corners();
116 std::vector<typename GridView::IndexSet::IndexType> vertexIndices(numCorners);
117 for (int i = 0; i < numCorners; ++i)
118 {
119 const auto vIdxLocal = refElem.subEntity(intersection.indexInInside(), 1, i, dim-1);
120 vertexIndices[i] = codimOneGridAdapter.bulkGridVertexIndex( codimOneElement.template subEntity<dim-1>(vIdxLocal) );
121 }
122
123 // if any of the vertices is on an immersed boudnary, we must not enrich any of them
124 if (std::any_of(vertexIndices.begin(), vertexIndices.end(), [&isOnBoundary] (auto idx) { return !isOnBoundary[idx]; }))
125 std::for_each(vertexIndices.begin(), vertexIndices.end(), [&vertexMarkers] (auto idx) { vertexMarkers[idx] = false; });
126 }
127 }
128 }
129 }
130};
131
140template<class GV>
142{
143 static constexpr int dim = GV::dimension;
144 static_assert(dim > 1, "Vertex dof enrichment mapper currently only works for dim > 1!");
145
146 using GIType = typename GV::IndexSet::IndexType;
147 using Vertex = typename GV::template Codim<dim>::Entity;
148 using Element = typename GV::template Codim<0>::Entity;
149 using MCMGMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GV>;
150
151public:
153 using GridView = GV;
155 using GridIndexType = GIType;
156
158 EnrichedVertexDofMapper(const GV& gridView)
159 : gridView_(gridView)
160 , elementMapper_(gridView, Dune::mcmgElementLayout())
161 , vertexMapper_(gridView, Dune::mcmgVertexLayout())
162 {
163 initialize_();
164 }
165
167 EnrichedVertexDofMapper(const GV& gridView, Dune::MCMGLayout layout)
168 : EnrichedVertexDofMapper(gridView)
169 {
170 if ( !( static_cast<bool>(layout(Dune::GeometryTypes::vertex, dim)) ) )
171 DUNE_THROW(Dune::InvalidStateException, "Vertex mapper only makes sense for vertex layout!");
172 }
173
175 GridIndexType subIndex(const Element& e, unsigned int i, unsigned int codim) const
176 {
177 assert(codim == dim && "Only element corners can be mapped by this mapper");
178 return indexMap_[elementMapper_.index(e)][i];
179 }
180
182 GridIndexType vertexIndex(const Element& e, unsigned int i, unsigned int codim) const
183 {
184 assert(codim == dim && "Only element corners can be mapped by this mapper");
185 return vertexMapper_.subIndex(e, i, codim);
186 }
187
189 GridIndexType vertexIndex(const Vertex& v) const
190 {
191 assert(Vertex::Geometry::mydimension == 0 && "Only vertices can be mapped by this mapper");
192 return vertexMapper_.index(v);
193 }
194
199 template< class EntityType >
200 GridIndexType index(const EntityType& e) const
201 {
202 if (hasEnrichedVertices_)
203 DUNE_THROW(Dune::InvalidStateException, "Index map contains enriched vertex dofs. Direct mapping from vertex to index not possible.");
204
205 assert(EntityType::Geometry::mydimension == 0 && "Only vertices can be mapped by this mapper");
206 return vertexMapper_.index(e);
207 }
208
210 std::size_t size() const
211 { return size_; }
212
214 bool isEnriched(const Vertex& v)
215 { return isEnriched_[ vertexMapper_.index(v) ]; }
216
219 void update()
220 {
221 initialize_();
222 }
223
235 template<class CodimOneGridView, class CodimOneGridAdapter>
236 void enrich(const CodimOneGridView& codimOneGridView,
237 const CodimOneGridAdapter& codimOneGridAdapter,
238 bool verbose = false)
239 {
240 static const int codimOneDim = CodimOneGridView::dimension;
241 static_assert(codimOneDim == dim-1, "Grid dimension mismatch!");
242 static_assert(codimOneDim == 2 || codimOneDim == 1, "Inadmissible codimension one grid dimension");
243 static_assert(int(CodimOneGridView::dimensionworld) == int(GV::dimensionworld), "Grid world dimension mismatch");
244
245 // keep track of time
246 Dune::Timer watch;
247
248 // mark vertices for enrichment using the indicator
249 EnrichmentIndicator::markVerticesForEnrichment(isEnriched_, gridView_, vertexMapper_, codimOneGridView, codimOneGridAdapter);
250
251 // let the helper class do the enrichment of the index map
253 isEnriched_,
254 gridView_,
255 vertexMapper_,
256 elementMapper_,
257 codimOneGridView,
258 codimOneGridAdapter);
259
260 // check if new index map contains enriched dofs
261 hasEnrichedVertices_ = std::any_of(isEnriched_.begin(), isEnriched_.end(), [] (bool isEnriched) { return isEnriched; });
262
263 if (verbose)
264 std::cout << "Vertex dof enrichment took " << watch.elapsed() << " seconds." << std::endl;
265 }
266
267private:
268
270 void initialize_()
271 {
272 size_ = gridView_.size(dim);
273 hasEnrichedVertices_ = false;
274 indexMap_.resize(gridView_.size(0));
275 isEnriched_.resize(gridView_.size(dim), false);
276 for (const auto& e : elements(gridView_))
277 {
278 const auto numCorners = e.geometry().corners();
279 const auto eIdxGlobal = elementMapper_.index(e);
280 indexMap_[eIdxGlobal].resize(numCorners);
281 for (unsigned int i = 0; i < numCorners; ++i)
282 indexMap_[eIdxGlobal][i] = vertexMapper_.subIndex(e, i, dim);
283 }
284 }
285
286 // data members
287 std::size_t size_;
288 const GV gridView_;
289 MCMGMapper elementMapper_;
290 MCMGMapper vertexMapper_;
291 bool hasEnrichedVertices_;
292 std::vector<bool> isEnriched_;
293 std::vector< std::vector<GIType> > indexMap_;
294};
295
296} // end namespace Dumux
297
298#endif
Definition: adapt.hh:29
Definition: common/pdesolver.hh:35
Adapter that allows retrieving information on a d-dimensional grid for entities of a (d-1)-dimensiona...
Definition: codimonegridadapter.hh:52
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:238
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:145
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:89
An indicator class used to mark vertices for enrichment. This implementation marks all vertices of a ...
Definition: vertexmapper.hh:46
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:67
A vertex mapper that allows for enrichment of nodes. Indication on where to enrich the nodes is done ...
Definition: vertexmapper.hh:142
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:236
std::size_t size() const
returns the number of dofs managed by this mapper
Definition: vertexmapper.hh:210
void update()
Definition: vertexmapper.hh:219
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:182
GridIndexType index(const EntityType &e) const
Definition: vertexmapper.hh:200
EnrichedVertexDofMapper(const GV &gridView)
the constructor
Definition: vertexmapper.hh:158
GridIndexType vertexIndex(const Vertex &v) const
map nodal entity to the grid vertex index
Definition: vertexmapper.hh:189
GIType GridIndexType
export the grid index type
Definition: vertexmapper.hh:155
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:175
GV GridView
export the underlying grid view type
Definition: vertexmapper.hh:153
EnrichedVertexDofMapper(const GV &gridView, Dune::MCMGLayout layout)
constructor taking a layout as additional argument (for compatibility)
Definition: vertexmapper.hh:167
bool isEnriched(const Vertex &v)
returns true if a vertex dof had been enriched
Definition: vertexmapper.hh:214