version 3.8
codimonegridadapter.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_FACETCOUPLING_CODIM_ONE_GRID_ADAPTER_HH
14#define DUMUX_FACETCOUPLING_CODIM_ONE_GRID_ADAPTER_HH
15
16#include <cassert>
17#include <vector>
18#include <memory>
19
20#include <dune/grid/common/mcmgmapper.hh>
21
23
24namespace Dumux {
25
38template<class Embeddings, int bulkGridId = 0, int facetGridId = 1>
40{
41 // Extract some types of the facet-conforming grid of codimension one
42 using FacetGridView = typename Embeddings::template GridView<facetGridId>;
43 using FacetGridVertex = typename FacetGridView::template Codim<FacetGridView::dimension>::Entity;
44 using FacetGridElement = typename FacetGridView::template Codim<0>::Entity;
45 using FacetGridIndexType = typename IndexTraits<FacetGridView>::GridIndex;
46
47 // Extract some types of the bulk grid
48 using BulkGridView = typename Embeddings::template GridView<bulkGridId>;
49 using BulkMapper = Dune::MultipleCodimMultipleGeomTypeMapper<BulkGridView>;
50 using BulkGridElement = typename BulkGridView::template Codim<0>::Entity;
51 using BulkGridIntersection = typename BulkGridView::Intersection;
52 using BulkGridVertex = typename BulkGridView::template Codim<BulkGridView::dimension>::Entity;
53 using BulkIndexType = typename IndexTraits<BulkGridView>::GridIndex;
54
55 // check if provided id combination makes sense
56 static_assert( int(FacetGridView::dimension) == int(BulkGridView::dimension) - 1,
57 "Grid dimension mismatch! Please check the provided domain ids!" );
58 static_assert( int(FacetGridView::dimensionworld) == int(BulkGridView::dimensionworld),
59 "Grid world dimension mismatch! All grids must have the same world dimension" );
60
61public:
62
64 CodimOneGridAdapter(std::shared_ptr<const Embeddings> embeddings)
65 : embeddingsPtr_(embeddings)
66 , bulkVertexMapper_(embeddings->template gridView<bulkGridId>(), Dune::mcmgVertexLayout())
67 {
68 // bulk insertion to grid index map
69 const auto& bulkGridView = embeddings->template gridView<bulkGridId>();
70 bulkInsertionToGridVIdx_.resize(bulkGridView.size(BulkGridView::dimension));
71 for (const auto& v : vertices(bulkGridView))
72 bulkInsertionToGridVIdx_[embeddings->template insertionIndex<bulkGridId>(v)] = bulkVertexMapper_.index(v);
73
74 // Determine map from the hierarchy's vertex idx to bulk insertion idx
75 // There is one unique set of vertex indices within the hierarchy.
76 // Obtain the hierarchy indices that make up the bulk grid. These
77 // are ordered corresponding to their insertion (thus loopIdx = insertionIdx)
78 hierarchyToBulkInsertionIdx_.resize(embeddingsPtr_->numVerticesInHierarchy());
79 bulkGridHasHierarchyVertex_.resize(embeddingsPtr_->numVerticesInHierarchy(), false);
80 const auto& bulkHierarchyIndices = embeddingsPtr_->gridHierarchyIndices(bulkGridId);
81 for (std::size_t insIdx = 0; insIdx < bulkHierarchyIndices.size(); ++insIdx)
82 {
83 hierarchyToBulkInsertionIdx_[ bulkHierarchyIndices[insIdx] ] = insIdx;
84 bulkGridHasHierarchyVertex_[ bulkHierarchyIndices[insIdx] ] = true;
85 }
86
87 // determine which bulk vertices lie on facet elements
88 bulkVertexIsOnFacetGrid_.resize(bulkGridView.size(BulkGridView::dimension), false);
89 const auto& facetGridView = embeddings->template gridView<facetGridId>();
90 for (const auto& v : vertices(facetGridView))
91 {
92 const auto insIdx = embeddings->template insertionIndex<facetGridId>(v);
93 const auto hierarchyInsIdx = embeddings->gridHierarchyIndices(facetGridId)[insIdx];
94
95 if (bulkGridHasHierarchyVertex_[hierarchyInsIdx])
96 bulkVertexIsOnFacetGrid_[ getBulkGridVertexIndex_(hierarchyInsIdx) ] = true;
97 }
98
99 // determine the bulk vertex indices that make up facet elements & connectivity
100 facetElementCorners_.resize(facetGridView.size(0));
101 facetElementsAtBulkVertex_.resize(bulkGridView.size(BulkGridView::dimension));
102
103 std::size_t facetElementCounter = 0;
104 for (const auto& element : elements(facetGridView))
105 {
106 if (isEmbedded(element))
107 {
108 // obtain the bulk vertex indices of the corners of this element
109 const auto numCorners = element.subEntities(FacetGridView::dimension);
110 std::vector<BulkIndexType> cornerIndices(numCorners);
111 for (int i = 0; i < numCorners; ++i)
112 cornerIndices[i] = bulkGridVertexIndex(element.template subEntity<FacetGridView::dimension>(i));
113
114 // update connectivity map facetVertex -> facetElements
115 for (auto bulkVIdx : cornerIndices)
116 facetElementsAtBulkVertex_[bulkVIdx].push_back(facetElementCounter);
117
118 // update facet elements (identified by corners - store them sorted!)
119 std::sort(cornerIndices.begin(), cornerIndices.end());
120 facetElementCorners_[facetElementCounter] = std::move(cornerIndices);
121 }
122
123 facetElementCounter++;
124 }
125 }
126
133 BulkIndexType bulkGridVertexIndex(const FacetGridVertex& v) const
134 {
135 const auto insIdx = embeddingsPtr_->template insertionIndex<facetGridId>(v);
136 const auto hierarchyInsIdx = embeddingsPtr_->gridHierarchyIndices(facetGridId)[insIdx];
137 return getBulkGridVertexIndex_(hierarchyInsIdx);
138 }
139
144 bool isOnFacetGrid(const BulkGridVertex& v) const
145 {
146 const auto bulkInsIdx = embeddingsPtr_->template insertionIndex<bulkGridId>(v);
147 const auto bulkVIdx = bulkInsertionToGridVIdx_[bulkInsIdx];
148 return bulkVertexIsOnFacetGrid_[bulkVIdx];
149 }
150
157 bool isOnFacetGrid(const BulkGridElement& element, const BulkGridIntersection& intersection) const
158 {
159 // Intersection lies on facet grid, if the corners of the intersection make up a facet element
160 const auto refElement = referenceElement(element);
161 const auto numCorners = intersection.geometry().corners();
162 const auto facetIdx = intersection.indexInInside();
163
164 std::vector<BulkIndexType> cornerIndices(numCorners);
165 for (int i = 0; i < numCorners; ++i)
166 cornerIndices[i] = bulkVertexMapper_.subIndex( element,
167 refElement.subEntity(facetIdx, 1, i, BulkGridView::dimension),
168 BulkGridView::dimension );
169
170 return composeFacetElement(cornerIndices);
171 }
172
179 template<class IndexStorage>
180 bool composeFacetElement(const IndexStorage& bulkVertexIndices) const
181 {
182 // set up a vector containing all element indices the vertices are connected to
183 std::vector<std::size_t> facetElemIndices;
184 for (auto bulkVIdx : bulkVertexIndices)
185 facetElemIndices.insert( facetElemIndices.end(),
186 facetElementsAtBulkVertex_[bulkVIdx].begin(),
187 facetElementsAtBulkVertex_[bulkVIdx].end() );
188
189 // if no facet elements are connected to the vertices this is not on facet grid
190 if (facetElemIndices.size() == 0)
191 return false;
192
193 // make the container unique
194 std::sort(facetElemIndices.begin(), facetElemIndices.end());
195 facetElemIndices.erase(std::unique(facetElemIndices.begin(), facetElemIndices.end()), facetElemIndices.end());
196
197 // check if given indices make up a facet element
198 auto cornerIndexCopy = bulkVertexIndices;
199 std::sort(cornerIndexCopy.begin(), cornerIndexCopy.end());
200 for (const auto& facetElemIdx : facetElemIndices)
201 {
202 const auto& facetElemCorners = facetElementCorners_[facetElemIdx];
203 if (facetElemCorners.size() != cornerIndexCopy.size())
204 continue;
205
206 if ( std::equal(cornerIndexCopy.begin(), cornerIndexCopy.end(),
207 facetElemCorners.begin(), facetElemCorners.end()) )
208 return true;
209 }
210
211 // no corresponding facet element found
212 return false;
213 }
214
219 bool isEmbedded(const FacetGridElement& e) const
220 { return numEmbedments(e) > 0; }
221
226 std::size_t numEmbedments(const FacetGridElement& e) const
227 { return embeddingsPtr_->template adjoinedEntityIndices<facetGridId>(e).size(); }
228
229private:
231 BulkIndexType getBulkGridVertexIndex_(BulkIndexType hierarchyInsertionIdx) const
232 {
233 assert(bulkGridHasHierarchyVertex_[hierarchyInsertionIdx]);
234 return bulkInsertionToGridVIdx_[ hierarchyToBulkInsertionIdx_[hierarchyInsertionIdx] ];
235 }
236
237 // shared pointer to the embedment data
238 std::shared_ptr<const Embeddings> embeddingsPtr_;
239
240 // vertex mapper of the bulk grid
241 BulkMapper bulkVertexMapper_;
242
243 // data stored on grid vertices
244 std::vector<bool> bulkVertexIsOnFacetGrid_;
245 std::vector<BulkIndexType> bulkInsertionToGridVIdx_;
246 std::vector<BulkIndexType> hierarchyToBulkInsertionIdx_;
247 std::vector<bool> bulkGridHasHierarchyVertex_;
248
249 // data stored for elements on the codim one grid
250 std::vector< std::vector<BulkIndexType> > facetElementsAtBulkVertex_;
251 std::vector< std::vector<BulkIndexType> > facetElementCorners_;
252};
253
254} // end namespace Dumux
255
256#endif
Adapter that allows retrieving information on a d-dimensional grid for entities of a (d-1)-dimensiona...
Definition: codimonegridadapter.hh:40
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:180
bool isOnFacetGrid(const BulkGridVertex &v) const
Returns true if the vertex of the d-dimensional grid with the given vertex index also exists on the (...
Definition: codimonegridadapter.hh:144
CodimOneGridAdapter(std::shared_ptr< const Embeddings > embeddings)
The constructor.
Definition: codimonegridadapter.hh:64
bool isEmbedded(const FacetGridElement &e) const
Returns true if a (d-1)-dimensional element is embedded in the d-dimensional domain.
Definition: codimonegridadapter.hh:219
bool isOnFacetGrid(const BulkGridElement &element, const BulkGridIntersection &intersection) const
Returns true if the given intersection coincides with a facet grid.
Definition: codimonegridadapter.hh:157
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
Defines the index types used for grid and local indices.
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
typename GridView::IndexSet::IndexType GridIndex
Definition: indextraits.hh:27