3.4
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
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 * 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_FACETCOUPLING_CODIM_ONE_GRID_ADAPTER_HH
26#define DUMUX_FACETCOUPLING_CODIM_ONE_GRID_ADAPTER_HH
27
28#include <cassert>
29#include <vector>
30#include <memory>
31
32#include <dune/grid/common/mcmgmapper.hh>
33
35
36namespace Dumux {
37
50template<class Embeddings, int bulkGridId = 0, int facetGridId = 1>
52{
53 // Extract some types of the facet-conforming grid of codimension one
54 using FacetGridView = typename Embeddings::template GridView<facetGridId>;
55 using FacetGridVertex = typename FacetGridView::template Codim<FacetGridView::dimension>::Entity;
56 using FacetGridElement = typename FacetGridView::template Codim<0>::Entity;
57 using FacetGridIndexType = typename IndexTraits<FacetGridView>::GridIndex;
58
59 // Extract some types of the bulk grid
60 using BulkGridView = typename Embeddings::template GridView<bulkGridId>;
61 using BulkMapper = Dune::MultipleCodimMultipleGeomTypeMapper<BulkGridView>;
62 using BulkGridElement = typename BulkGridView::template Codim<0>::Entity;
63 using BulkGridIntersection = typename BulkGridView::Intersection;
64 using BulkGridVertex = typename BulkGridView::template Codim<BulkGridView::dimension>::Entity;
65 using BulkIndexType = typename IndexTraits<BulkGridView>::GridIndex;
66
67 // check if provided id combination makes sense
68 static_assert( int(FacetGridView::dimension) == int(BulkGridView::dimension) - 1,
69 "Grid dimension mismatch! Please check the provided domain ids!" );
70 static_assert( int(FacetGridView::dimensionworld) == int(BulkGridView::dimensionworld),
71 "Grid world dimension mismatch! All grids must have the same world dimension" );
72
73public:
74
76 CodimOneGridAdapter(std::shared_ptr<const Embeddings> embeddings)
77 : embeddingsPtr_(embeddings)
78 , bulkVertexMapper_(embeddings->template gridView<bulkGridId>(), Dune::mcmgVertexLayout())
79 {
80 // bulk insertion to grid index map
81 const auto& bulkGridView = embeddings->template gridView<bulkGridId>();
82 bulkInsertionToGridVIdx_.resize(bulkGridView.size(BulkGridView::dimension));
83 for (const auto& v : vertices(bulkGridView))
84 bulkInsertionToGridVIdx_[embeddings->template insertionIndex<bulkGridId>(v)] = bulkVertexMapper_.index(v);
85
86 // Determine map from the hierarchy's vertex idx to bulk insertion idx
87 // There is one unique set of vertex indices within the hierarchy.
88 // Obtain the hierarchy indices that make up the bulk grid. These
89 // are ordered corresponding to their insertion (thus loopIdx = insertionIdx)
90 hierarchyToBulkInsertionIdx_.resize(embeddingsPtr_->numVerticesInHierarchy());
91 bulkGridHasHierarchyVertex_.resize(embeddingsPtr_->numVerticesInHierarchy(), false);
92 const auto& bulkHierarchyIndices = embeddingsPtr_->gridHierarchyIndices(bulkGridId);
93 for (std::size_t insIdx = 0; insIdx < bulkHierarchyIndices.size(); ++insIdx)
94 {
95 hierarchyToBulkInsertionIdx_[ bulkHierarchyIndices[insIdx] ] = insIdx;
96 bulkGridHasHierarchyVertex_[ bulkHierarchyIndices[insIdx] ] = true;
97 }
98
99 // determine which bulk vertices lie on facet elements
100 bulkVertexIsOnFacetGrid_.resize(bulkGridView.size(BulkGridView::dimension), false);
101 const auto& facetGridView = embeddings->template gridView<facetGridId>();
102 for (const auto& v : vertices(facetGridView))
103 {
104 const auto insIdx = embeddings->template insertionIndex<facetGridId>(v);
105 const auto hierarchyInsIdx = embeddings->gridHierarchyIndices(facetGridId)[insIdx];
106
107 if (bulkGridHasHierarchyVertex_[hierarchyInsIdx])
108 bulkVertexIsOnFacetGrid_[ getBulkGridVertexIndex_(hierarchyInsIdx) ] = true;
109 }
110
111 // determine the bulk vertex indices that make up facet elements & connectivity
112 facetElementCorners_.resize(facetGridView.size(0));
113 facetElementsAtBulkVertex_.resize(bulkGridView.size(BulkGridView::dimension));
114
115 std::size_t facetElementCounter = 0;
116 for (const auto& element : elements(facetGridView))
117 {
118 if (isEmbedded(element))
119 {
120 // obtain the bulk vertex indices of the corners of this element
121 const auto numCorners = element.subEntities(FacetGridView::dimension);
122 std::vector<BulkIndexType> cornerIndices(numCorners);
123 for (int i = 0; i < numCorners; ++i)
124 cornerIndices[i] = bulkGridVertexIndex(element.template subEntity<FacetGridView::dimension>(i));
125
126 // update connectivity map facetVertex -> facetElements
127 for (auto bulkVIdx : cornerIndices)
128 facetElementsAtBulkVertex_[bulkVIdx].push_back(facetElementCounter);
129
130 // update facet elements (identified by corners - store them sorted!)
131 std::sort(cornerIndices.begin(), cornerIndices.end());
132 facetElementCorners_[facetElementCounter] = std::move(cornerIndices);
133 }
134
135 facetElementCounter++;
136 }
137 }
138
145 BulkIndexType bulkGridVertexIndex(const FacetGridVertex& v) const
146 {
147 const auto insIdx = embeddingsPtr_->template insertionIndex<facetGridId>(v);
148 const auto hierarchyInsIdx = embeddingsPtr_->gridHierarchyIndices(facetGridId)[insIdx];
149 return getBulkGridVertexIndex_(hierarchyInsIdx);
150 }
151
156 bool isOnFacetGrid(const BulkGridVertex& v) const
157 {
158 const auto bulkInsIdx = embeddingsPtr_->template insertionIndex<bulkGridId>(v);
159 const auto bulkVIdx = bulkInsertionToGridVIdx_[bulkInsIdx];
160 return bulkVertexIsOnFacetGrid_[bulkVIdx];
161 }
162
169 bool isOnFacetGrid(const BulkGridElement& element, const BulkGridIntersection& intersection) const
170 {
171 // Intersection lies on facet grid, if the corners of the intersection make up a facet element
172 const auto refElement = referenceElement(element);
173 const auto numCorners = intersection.geometry().corners();
174 const auto facetIdx = intersection.indexInInside();
175
176 std::vector<BulkIndexType> cornerIndices(numCorners);
177 for (int i = 0; i < numCorners; ++i)
178 cornerIndices[i] = bulkVertexMapper_.subIndex( element,
179 refElement.subEntity(facetIdx, 1, i, BulkGridView::dimension),
180 BulkGridView::dimension );
181
182 return composeFacetElement(cornerIndices);
183 }
184
191 template<class IndexStorage>
192 bool composeFacetElement(const IndexStorage& bulkVertexIndices) const
193 {
194 // set up a vector containing all element indices the vertices are connected to
195 std::vector<std::size_t> facetElemIndices;
196 for (auto bulkVIdx : bulkVertexIndices)
197 facetElemIndices.insert( facetElemIndices.end(),
198 facetElementsAtBulkVertex_[bulkVIdx].begin(),
199 facetElementsAtBulkVertex_[bulkVIdx].end() );
200
201 // if no facet elements are connected to the vertices this is not on facet grid
202 if (facetElemIndices.size() == 0)
203 return false;
204
205 // make the container unique
206 std::sort(facetElemIndices.begin(), facetElemIndices.end());
207 facetElemIndices.erase(std::unique(facetElemIndices.begin(), facetElemIndices.end()), facetElemIndices.end());
208
209 // check if given indices make up a facet element
210 auto cornerIndexCopy = bulkVertexIndices;
211 std::sort(cornerIndexCopy.begin(), cornerIndexCopy.end());
212 for (const auto& facetElemIdx : facetElemIndices)
213 {
214 const auto& facetElemCorners = facetElementCorners_[facetElemIdx];
215 if (facetElemCorners.size() != cornerIndexCopy.size())
216 continue;
217
218 if ( std::equal(cornerIndexCopy.begin(), cornerIndexCopy.end(),
219 facetElemCorners.begin(), facetElemCorners.end()) )
220 return true;
221 }
222
223 // no corresponding facet element found
224 return false;
225 }
226
231 bool isEmbedded(const FacetGridElement& e) const
232 { return numEmbedments(e) > 0; }
233
238 std::size_t numEmbedments(const FacetGridElement& e) const
239 { return embeddingsPtr_->template adjoinedEntityIndices<facetGridId>(e).size(); }
240
241private:
243 BulkIndexType getBulkGridVertexIndex_(BulkIndexType hierarchyInsertionIdx) const
244 {
245 assert(bulkGridHasHierarchyVertex_[hierarchyInsertionIdx]);
246 return bulkInsertionToGridVIdx_[ hierarchyToBulkInsertionIdx_[hierarchyInsertionIdx] ];
247 }
248
249 // shared pointer to the embedment data
250 std::shared_ptr<const Embeddings> embeddingsPtr_;
251
252 // vertex mapper of the bulk grid
253 BulkMapper bulkVertexMapper_;
254
255 // data stored on grid vertices
256 std::vector<bool> bulkVertexIsOnFacetGrid_;
257 std::vector<BulkIndexType> bulkInsertionToGridVIdx_;
258 std::vector<BulkIndexType> hierarchyToBulkInsertionIdx_;
259 std::vector<bool> bulkGridHasHierarchyVertex_;
260
261 // data stored for elements on the codim one grid
262 std::vector< std::vector<BulkIndexType> > facetElementsAtBulkVertex_;
263 std::vector< std::vector<BulkIndexType> > facetElementCorners_;
264};
265
266} // end namespace Dumux
267
268#endif
Defines the index types used for grid and local indices.
Definition: adapt.hh:29
Definition: common/pdesolver.hh:36
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
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:156
CodimOneGridAdapter(std::shared_ptr< const Embeddings > embeddings)
The constructor.
Definition: codimonegridadapter.hh:76
bool isEmbedded(const FacetGridElement &e) const
Returns true if a (d-1)-dimensional element is embedded in the d-dimensional domain.
Definition: codimonegridadapter.hh:231
bool isOnFacetGrid(const BulkGridElement &element, const BulkGridIntersection &intersection) const
Returns true if the given intersection coincides with a facet grid.
Definition: codimonegridadapter.hh:169
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