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