25#ifndef DUMUX_VERTEX_ENRICHMENT_HELPER_HH
26#define DUMUX_VERTEX_ENRICHMENT_HELPER_HH
30#include <unordered_map>
31#include <unordered_set>
33#include <dune/common/exceptions.hh>
34#include <dune/common/reservedvector.hh>
36#include <dune/grid/common/mcmgmapper.hh>
54template<
class Gr
idView,
class CodimOneGr
idView >
57 static constexpr int dim = GridView::dimension;
58 static constexpr int dimWorld = GridView::dimensionworld;
59 static_assert(dim == 2 || dim == 3,
"Grid dimension must be two or three");
60 static_assert(dimWorld == int(CodimOneGridView::dimensionworld),
"world dimension mismatch");
62 using Intersection =
typename GridView::Intersection;
63 using MCMGMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GridView>;
65 using Element =
typename GridView::template Codim<0>::Entity;
66 using GlobalPosition =
typename Element::Geometry::GlobalCoordinate;
68 using ElementPath = std::vector< GridIndexType >;
69 using NodalElementPaths = std::vector< std::vector<ElementPath> >;
88 template<
class IndexMap,
class CodimOneGr
idAdapter >
89 static std::size_t
enrich(IndexMap& indexMap,
90 const std::vector<bool>& vertexMarkers,
91 const GridView& gridView,
92 const MCMGMapper& vertexMapper,
93 const MCMGMapper& elementMapper,
94 const CodimOneGridView& codimOneGridView,
98 NodalElementPaths nodalPaths(gridView.size(dim));
99 for (
const auto& e : elements(gridView))
101 const auto eIdx = elementMapper.index(e);
103 std::vector<unsigned int> handledFacets;
104 const auto refElement = referenceElement(e);
105 for (
const auto& is : intersections(gridView, e))
112 if (std::find(handledFacets.begin(), handledFacets.end(), is.indexInInside()) != handledFacets.end())
116 const auto numCorners = is.geometry().corners();
117 std::vector<GridIndexType> faceVertexIndices(
numCorners);
119 faceVertexIndices[i] = vertexMapper.subIndex( e,
120 refElement.subEntity(is.indexInInside(), 1, i, dim),
126 handledFacets.push_back(is.indexInInside());
130 const auto vIdxGlobal = faceVertexIndices[i];
131 if (!vertexMarkers[vIdxGlobal])
136 for (
const auto& path : nodalPaths[vIdxGlobal])
137 if (std::find(path.begin(), path.end(), eIdx) != path.end())
138 { found =
true;
break; }
149 path.push_back(eIdx);
150 continuePathSearch_(path, gridView, elementMapper, vertexMapper, codimOneGridAdapter, e, refElement, is, vIdxGlobal);
151 nodalPaths[vIdxGlobal].emplace_back(std::move(path));
159 std::vector<std::size_t> bulkVertexIndexOffsets(gridView.size(dim), 0);
160 for (
const auto& v : vertices(gridView))
162 const auto vIdx = vertexMapper.index(v);
163 if (vertexMarkers[vIdx])
164 bulkVertexIndexOffsets[vIdx] = nodalPaths[vIdx].size()-1;
168 std::size_t sumOffset = 0;
169 std::size_t size = 0;
170 for (
auto& nodalOffset : bulkVertexIndexOffsets)
172 const auto os = nodalOffset;
173 nodalOffset = sumOffset;
175 size += (os == 0) ? 1 : os + 1;
179 for (
const auto& e : elements(gridView))
181 const auto& eg = e.geometry();
182 const auto eIdx = elementMapper.index(e);
183 for (
int i = 0; i < eg.corners(); ++i)
185 const auto origVIdx = vertexMapper.subIndex(e, i, dim);
188 if (!vertexMarkers[origVIdx])
189 indexMap[eIdx][i] += bulkVertexIndexOffsets[origVIdx];
195 const auto& paths = nodalPaths[ origVIdx ];
196 for (
int pathIdx = 0; pathIdx < paths.size(); ++pathIdx)
198 const auto& curPath = paths[pathIdx];
199 if ( std::find(curPath.begin(), curPath.end(), eIdx) != curPath.end() )
201 indexMap[eIdx][i] += bulkVertexIndexOffsets[origVIdx] + pathIdx;
208 DUNE_THROW(Dune::InvalidStateException,
"Element not found in any path");
218 template<
class ReferenceElement,
class CodimOneGr
idAdapter >
219 static void continuePathSearch_(ElementPath& path,
220 const GridView& gridView,
221 const MCMGMapper& elementMapper,
222 const MCMGMapper& vertexMapper,
224 const Element& element,
225 const ReferenceElement& refElement,
226 const Intersection& prevIntersection,
227 GridIndexType vIdxGlobal)
230 static constexpr int numIsToFind = dim == 3 ? 2 : 1;
233 unsigned int foundCounter = 0;
234 std::vector<unsigned int> handledFacets;
235 for (
const auto& is : intersections(gridView, element))
238 if (is.indexInInside() == prevIntersection.indexInInside())
242 if (std::count(handledFacets.begin(), handledFacets.end(), is.indexInInside()))
246 const auto numCorners = is.geometry().corners();
247 std::vector<GridIndexType> faceVertexIndices(
numCorners);
249 faceVertexIndices[i] = vertexMapper.subIndex( element,
250 refElement.subEntity(is.indexInInside(), 1, i, dim),
254 if (std::find(faceVertexIndices.begin(), faceVertexIndices.end(), vIdxGlobal) != faceVertexIndices.end())
257 handledFacets.push_back(is.indexInInside());
269 const auto outsideElement = is.outside();
270 const auto outsideElemIdx = elementMapper.index(outsideElement);
271 if (std::find(path.begin(), path.end(), outsideElemIdx) == path.end())
273 const auto idxInOutside = is.indexInOutside();
274 const auto outsideRefElement = referenceElement(outsideElement);
275 path.push_back(outsideElemIdx);
278 for (
const auto& outsideIs : intersections(gridView, outsideElement))
280 if (outsideIs.indexInInside() == idxInOutside)
283 if (foundCounter == numIsToFind)
284 return continuePathSearch_(path,
296 continuePathSearch_(path,
311 if (foundCounter != numIsToFind)
312 DUNE_THROW(Dune::InvalidStateException,
"Found " << foundCounter <<
" instead of " << numIsToFind <<
" intersections around vertex");
Defines the index types used for grid and local indices.
Define some often used mathematical functions.
std::size_t numCorners(Shape shape)
Returns the number of corners of a given geometry.
Definition: throatproperties.hh:215
typename GridView::IndexSet::IndexType GridIndex
Definition: indextraits.hh:39
Adapter that allows retrieving information on a d-dimensional grid for entities of a (d-1)-dimensiona...
Definition: codimonegridadapter.hh:52
bool composeFacetElement(const IndexStorage &bulkVertexIndices) const
Returns true if a given set of bulk vertex indices make up a facet grid element.
Definition: codimonegridadapter.hh:192
Specialization of the enrichment helper class for 2d grids. In this case, we look for two-dimensional...
Definition: enrichmenthelper.hh:56
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