3.5-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
facet/cellcentered/mpfa/couplingmapper.hh
Go to the documentation of this file.
1/*****************************************************************************
2 * See the file COPYING for full copying permissions. *
3 * *
4 * This program is free software: you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation, either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
16 *****************************************************************************/
22#ifndef DUMUX_CCMPFA_FACETCOUPLING_MAPPER_HH
23#define DUMUX_CCMPFA_FACETCOUPLING_MAPPER_HH
24
25#include <dune/common/indices.hh>
26#include <dune/common/float_cmp.hh>
27
33
34namespace Dumux {
35
48template<class BulkFVG, class LowDimFVG, std::size_t bulkId, std::size_t lowDimId>
49class FacetCouplingMapper<BulkFVG, LowDimFVG, bulkId, lowDimId, DiscretizationMethods::CCMpfa>
50: public virtual FacetCouplingMapperBase<BulkFVG, LowDimFVG, bulkId, lowDimId>
51{
53
54 using BulkFVElementGeometry = typename BulkFVG::LocalView;
55 using BulkSubControlVolumeFace = typename BulkFVG::SubControlVolumeFace;
56
57 using BulkIndexType = typename IndexTraits<typename BulkFVG::GridView>::GridIndex;
58 using LowDimIndexType = typename IndexTraits<typename LowDimFVG::GridView>::GridIndex;
59
60 using LowDimElement = typename LowDimFVG::GridView::template Codim<0>::Entity;
61 using GlobalPosition = typename LowDimElement::Geometry::GlobalCoordinate;
62 static constexpr int lowDimDim = LowDimFVG::GridView::dimension;
63
64public:
66 using ParentType::bulkGridId;
67 using ParentType::facetGridId;
68
77 template< class Embeddings >
78 void update(const BulkFVG& bulkFvGridGeometry,
79 const LowDimFVG& lowDimFvGridGeometry,
80 std::shared_ptr<const Embeddings> embeddings)
81 {
82 // forward to update function with instantiated vertex adapter
84 update(bulkFvGridGeometry, lowDimFvGridGeometry, embeddings, GridAdapter(embeddings));
85 }
86
96 template< class Embeddings, class CodimOneGridAdapter >
97 void update(const BulkFVG& bulkFvGridGeometry,
98 const LowDimFVG& lowDimFvGridGeometry,
99 std::shared_ptr<const Embeddings> embeddings,
100 const CodimOneGridAdapter& codimOneGridAdapter)
101 {
102 // define the policy how to add map entries for given lowdim element and adjoined entity indices
103 auto addCouplingEntryPolicy = [&] (auto&& adjoinedEntityIndices,
104 const LowDimElement& lowDimElement,
105 const LowDimFVG& lowDimFvGridGeometry,
106 const BulkFVG& bulkFvGridGeometry)
107 {
108 const auto lowDimElemIdx = lowDimFvGridGeometry.elementMapper().index(lowDimElement);
109 auto& lowDimData = this->couplingMap_(facetGridId, bulkGridId)[lowDimElemIdx];
110
111 // determine corner indices (in bulk grid indices)
112 const auto& lowDimGeometry = lowDimElement.geometry();
113 const auto numElementCorners = lowDimElement.subEntities(lowDimDim);
114 std::vector<BulkIndexType> elemCornerIndices(numElementCorners);
115 for (int i = 0; i < numElementCorners; ++i)
116 elemCornerIndices[i] = codimOneGridAdapter.bulkGridVertexIndex(lowDimElement.template subEntity<lowDimDim>(i));
117
118 // Find the scvfs in the adjoined entities coinciding with the low dim element
119 auto fvGeometry = localView(bulkFvGridGeometry);
120 for (auto bulkElemIdx : adjoinedEntityIndices)
121 {
122 const auto bulkElement = bulkFvGridGeometry.element(bulkElemIdx);
123 fvGeometry.bind(bulkElement);
124
125 std::vector<BulkIndexType> embeddedScvfIndices;
126 for (const auto& scvf : scvfs(fvGeometry))
127 {
128 // for non-boundary faces, it suffices to check if one
129 // of the outside scv indices is in the set of embedments
130 if (!scvf.boundary())
131 {
132 if ( std::count(adjoinedEntityIndices.begin(), adjoinedEntityIndices.end(), scvf.outsideScvIdx()) )
133 embeddedScvfIndices.push_back(scvf.index());
134 }
135
136 // otherwise, do float comparison of element and scvf facet corner
137 else
138 {
139 const auto eps = lowDimGeometry.volume()*1e-8;
140 const auto diffVec = lowDimGeometry.center()-scvf.facetCorner();
141
142 if ( Dune::FloatCmp::eq<GlobalPosition, Dune::FloatCmp::CmpStyle::absolute>(diffVec, GlobalPosition(0.0), eps) )
143 embeddedScvfIndices.push_back(scvf.index());
144 }
145 }
146
147 // Error tracking. The boundary scvf detection might has to be improved for very fine grids!?
148 if ( embeddedScvfIndices.size() != numElementCorners )
149 DUNE_THROW(Dune::InvalidStateException, "Could not find all coupling scvfs in embedment");
150
151 // add each dof in the low dim element to coupling stencil of the bulk element and vice versa
152 auto& bulkData = this->couplingMap_(bulkGridId, facetGridId)[bulkElemIdx];
153 const auto lowDimElementDofs = LowDimFVG::discMethod == DiscretizationMethods::box
154 ? this->extractNodalDofs_(lowDimElement, lowDimFvGridGeometry)
155 : std::vector<LowDimIndexType>( {lowDimElemIdx} );
156
157 // add coupling info to all elements/scvfs in interaction volumes
158 for (auto dofIdx : lowDimElementDofs)
159 for (auto scvfIdx : embeddedScvfIndices)
160 this->addCouplingsFromIV_(bulkFvGridGeometry, fvGeometry.scvf(scvfIdx), fvGeometry, lowDimElemIdx, dofIdx);
161
162 // sort the scvfs according to the corners of the low dim element if box is used
163 // that allows identifying which scvf flux enters which low dim scv later
164 if (LowDimFVG::discMethod == DiscretizationMethods::box)
165 {
166 const auto copy = embeddedScvfIndices;
167
168 for (unsigned int i = 0; i < numElementCorners; ++i)
169 {
170 const auto& scvf = fvGeometry.scvf(copy[i]);
171 auto it = std::find(elemCornerIndices.begin(), elemCornerIndices.end(), scvf.vertexIndex());
172 assert(it != elemCornerIndices.end());
173 embeddedScvfIndices[ std::distance(elemCornerIndices.begin(), it) ] = copy[i];
174 }
175 }
176
177 // add info on which scvfs coincide with which low dim element
178 auto& elemToScvfMap = bulkData.elementToScvfMap[lowDimElemIdx];
179 elemToScvfMap.insert(elemToScvfMap.end(), embeddedScvfIndices.begin(), embeddedScvfIndices.end());
180
181 // add embedment
182 lowDimData.embedments.emplace_back( bulkElemIdx, std::move(embeddedScvfIndices) );
183 }
184 };
185
186 // let the parent do the update subject to the execution policy defined above
187 ParentType::update_(bulkFvGridGeometry, lowDimFvGridGeometry, embeddings, addCouplingEntryPolicy);
188
189 // lambda to make a container unique
190 auto makeUnique = [] (auto& c)
191 {
192 std::sort(c.begin(), c.end());
193 c.erase( std::unique(c.begin(), c.end()), c.end() );
194 };
195
196 // lambda to make bulk coupling map entry unique
197 auto makeBulkMapEntryUnique = [&makeUnique] (auto& data)
198 {
199 makeUnique(data.second.couplingStencil);
200 makeUnique(data.second.couplingElementStencil);
201 std::for_each(data.second.dofToCouplingScvfMap.begin(),
202 data.second.dofToCouplingScvfMap.end(),
203 [&makeUnique] (auto& pair) { makeUnique(pair.second); });
204 };
205
206 // make bulk map unique
207 auto& bulkCouplingData = this->couplingMap_(bulkGridId, facetGridId);
208 std::for_each(bulkCouplingData.begin(), bulkCouplingData.end(), makeBulkMapEntryUnique);
209
210 // coupling stencils might not be unique in lowdim domain
211 auto& lowDimCouplingData = this->couplingMap_(facetGridId, bulkGridId);
212 std::for_each(lowDimCouplingData.begin(),
213 lowDimCouplingData.end(),
214 [&makeUnique] (auto& pair) { makeUnique(pair.second.couplingStencil); });
215 }
216
217private:
218 void addCouplingsFromIV_(const BulkFVG& bulkFvGridGeometry,
219 const BulkSubControlVolumeFace& scvf,
220 const BulkFVElementGeometry& fvGeometry,
221 LowDimIndexType lowDimElemIdx,
222 LowDimIndexType lowDimDofIdx)
223 {
224 const auto& gridIvIndexSets = bulkFvGridGeometry.gridInteractionVolumeIndexSets();
225 if (bulkFvGridGeometry.vertexUsesSecondaryInteractionVolume(scvf.vertexIndex()))
226 addCouplingsFromIVIndexSet_(gridIvIndexSets.secondaryIndexSet(scvf), fvGeometry, lowDimElemIdx, lowDimDofIdx);
227 else
228 addCouplingsFromIVIndexSet_(gridIvIndexSets.primaryIndexSet(scvf), fvGeometry, lowDimElemIdx, lowDimDofIdx);
229 }
230
231 template< class IVIndexSet >
232 void addCouplingsFromIVIndexSet_(const IVIndexSet& indexSet,
233 const BulkFVElementGeometry& fvGeometry,
234 LowDimIndexType lowDimElemIdx,
235 LowDimIndexType lowDimDofIdx)
236 {
237 auto& lowDimData = this->couplingMap_(facetGridId, bulkGridId)[lowDimElemIdx];
238 for (auto bulkElemIdx : indexSet.gridScvIndices())
239 {
240 auto& bulkData = this->couplingMap_(bulkGridId, facetGridId)[bulkElemIdx];
241
242 lowDimData.couplingStencil.push_back( bulkElemIdx );
243 bulkData.couplingStencil.push_back( lowDimDofIdx );
244 bulkData.couplingElementStencil.push_back( lowDimElemIdx );
245 }
246
247 // insert scvf couplings stemming from this interaction volume
248 for (auto scvfIdx : indexSet.gridScvfIndices())
249 {
250 const auto& scvf = fvGeometry.scvf(scvfIdx);
251 auto& bulkData = this->couplingMap_(bulkGridId, facetGridId)[scvf.insideScvIdx()];
252 auto& couplingScvfs = bulkData.dofToCouplingScvfMap[lowDimDofIdx];
253 couplingScvfs.insert(couplingScvfs.end(), scvfIdx);
254 }
255 }
256};
257
258} // end namespace Dumux
259
260#endif
Defines the index types used for grid and local indices.
The available discretization methods in Dumux.
static ctype distance(const Dune::FieldVector< ctype, dimWorld > &a, const Dune::FieldVector< ctype, dimWorld > &b)
Compute the shortest distance between two points.
Definition: distance.hh:292
GridCache::LocalView localView(const GridCache &gridCache)
Free function to get the local view of a grid cache object.
Definition: localview.hh:38
Definition: adapt.hh:29
constexpr Box box
Definition: method.hh:139
typename GridView::IndexSet::IndexType GridIndex
Definition: indextraits.hh:39
void update(const BulkFVG &bulkFvGridGeometry, const LowDimFVG &lowDimFvGridGeometry, std::shared_ptr< const Embeddings > embeddings, const CodimOneGridAdapter &codimOneGridAdapter)
Update coupling maps. This is the standard interface required by any mapper implementation.
Definition: facet/cellcentered/mpfa/couplingmapper.hh:97
void update(const BulkFVG &bulkFvGridGeometry, const LowDimFVG &lowDimFvGridGeometry, std::shared_ptr< const Embeddings > embeddings)
Update coupling maps. This is the standard interface required by any mapper implementation.
Definition: facet/cellcentered/mpfa/couplingmapper.hh:78
Adapter that allows retrieving information on a d-dimensional grid for entities of a (d-1)-dimensiona...
Definition: codimonegridadapter.hh:52
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
Implementation for the coupling mapper that sets up and stores the coupling maps between two domains ...
Definition: facet/couplingmapper.hh:52
Base class for the coupling mapper that sets up and stores the coupling maps between two domains of d...
Definition: couplingmapperbase.hh:51