3.5-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
couplingmapperbase.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 3 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_FACETCOUPLING_MAPPER_BASE_HH
23#define DUMUX_FACETCOUPLING_MAPPER_BASE_HH
24
25#include <vector>
26#include <unordered_map>
27#include <algorithm>
28#include <memory>
29
30#include <dune/common/indices.hh>
31#include <dune/common/exceptions.hh>
33
34namespace Dumux {
35
46template< class BulkFVG,
47 class LowDimFVG,
48 std::size_t bulkId,
49 std::size_t lowDimId>
51{
52 using BulkGridView = typename BulkFVG::GridView;
53 using BulkIndexType = typename IndexTraits<BulkGridView>::GridIndex;
54
55 using LowDimGridView = typename LowDimFVG::GridView;
56 using LowDimElement = typename LowDimGridView::template Codim<0>::Entity;
57 using LowDimIndexType = typename IndexTraits<LowDimGridView>::GridIndex;
58
59 // make sure the grid geometry combination makes sense
60 static constexpr int bulkDim = BulkGridView::dimension;
61 static constexpr int lowDimDim = LowDimGridView::dimension;
62 static_assert(bulkDim == lowDimDim+1, "Lower-dimensional geometry is not of codim 1 w.r.t. bulk geometry!");
63
64 // discretization method of the bulk domain
65 static constexpr auto bulkDiscMethod = BulkFVG::discMethod;
66
67 // helper struct to check validity of the given domain id offset
68 template< class Embeddings >
69 class IsValidDomainId
70 {
71 using GCBulkGridView = typename Embeddings::template GridView<bulkId>;
72 using GCLowDimGridView = typename Embeddings::template GridView<lowDimId>;
73 static constexpr bool bulkMatch = std::is_same<GCBulkGridView, BulkGridView>::value;
74 static constexpr bool lowDimMatch = std::is_same<GCLowDimGridView, LowDimGridView>::value;
75 static_assert(bulkMatch, "The bulk domain id does not match the provided bulk grid geometry");
76 static_assert(lowDimMatch, "The lowDim domain id does not match the provided lowDim grid geometry");
77
78 public:
79 static constexpr bool value = bulkMatch && lowDimMatch;
80 };
81
86 struct BulkCouplingData
87 {
89 std::vector< LowDimIndexType > couplingStencil;
90
92 std::vector< LowDimIndexType > couplingElementStencil;
93
95 std::unordered_map< LowDimIndexType, std::vector<BulkIndexType> > dofToCouplingScvfMap;
96
98 std::unordered_map< LowDimIndexType, std::vector<BulkIndexType> > elementToScvfMap;
99 };
100
105 struct LowDimCouplingData
106 {
108 std::vector< BulkIndexType > couplingStencil;
109
111 using Embedment = std::pair< BulkIndexType, std::vector<BulkIndexType> >;
112 std::vector< Embedment > embedments;
113 };
114
116 using BulkCouplingMap = std::unordered_map<BulkIndexType, BulkCouplingData>;
117 using LowDimCouplingMap = std::unordered_map<LowDimIndexType, LowDimCouplingData>;
118
120 using LowDimStencil = std::vector<LowDimIndexType>;
121 using BulkStencil = std::vector<BulkIndexType>;
122
124 template<std::size_t id>
125 using GridIdType = Dune::index_constant<id>;
126
127public:
129 static constexpr auto bulkGridId = GridIdType<bulkId>();
130 static constexpr auto facetGridId = GridIdType<lowDimId>();
131
133 template<std::size_t id>
134 using Stencil = typename std::conditional<id == bulkId, BulkStencil, LowDimStencil>::type;
135
137 template<std::size_t i, std::size_t j>
138 using CouplingMap = typename std::conditional<i == bulkId, BulkCouplingMap, LowDimCouplingMap>::type;
139
141 template<int dim>
142 static constexpr GridIdType< (dim == bulkDim ? bulkId : lowDimId) > gridId()
143 { return GridIdType< (dim == bulkDim ? bulkId : lowDimId) >(); }
144
149 template< class Embeddings >
150 void update(const BulkFVG& bulkFvGridGeometry,
151 const LowDimFVG& lowDimFvGridGeometry,
152 std::shared_ptr<const Embeddings> embeddings)
153 { DUNE_THROW(Dune::NotImplemented, "Implementation does not provide an update() function."); }
154
156 const BulkCouplingMap& couplingMap(GridIdType<bulkId>, GridIdType<lowDimId>) const
157 { return bulkCouplingData_; }
158
160 const LowDimCouplingMap& couplingMap(GridIdType<lowDimId>, GridIdType<bulkId>) const
161 { return lowDimCouplingData_; }
162
163protected:
164
174 template< class Embeddings, typename AddCouplingEntryPolicy >
175 void update_(const BulkFVG& bulkFvGridGeometry,
176 const LowDimFVG& lowDimFvGridGeometry,
177 std::shared_ptr<const Embeddings> embeddings,
178 AddCouplingEntryPolicy&& addCouplingEntryPolicy)
179 {
180 // some static assertions on the grid creator
181 static_assert(IsValidDomainId<Embeddings>::value, "Grid type mismatch. Please review the provided domain id offset.");
182
183 // clear data
184 bulkCouplingData_.clear();
185 lowDimCouplingData_.clear();
186
187 // set up maps between element indices and insertion indices
188 const auto bulkInsertionToElemIdxMap = makeInsertionToGridIndexMap_(embeddings, bulkFvGridGeometry);
189
190 // set up coupling maps coming from the low dim domain
191 for (const auto& element : elements(lowDimFvGridGeometry.gridView()))
192 {
193 auto adjoinedEntities = embeddings->template adjoinedEntityIndices<lowDimId>(element);
194
195 // proceed only if element is embedded
196 if (adjoinedEntities.size() == 0)
197 continue;
198
199 // turn (insertion) indices into actual grid element indices ...
200 std::for_each(adjoinedEntities.begin(), adjoinedEntities.end(), [&] (auto& idx) { idx = bulkInsertionToElemIdxMap.at(idx); });
201
202 // ... and add them
203 addCouplingEntryPolicy(std::move(adjoinedEntities), element, lowDimFvGridGeometry, bulkFvGridGeometry);
204 }
205 }
206
208 template< class GridGeometry>
209 std::vector< typename IndexTraits<typename GridGeometry::GridView>::GridIndex >
210 extractNodalDofs_(const typename GridGeometry::GridView::template Codim<0>::Entity& element,
211 const GridGeometry& gridGeometry)
212 {
213 static constexpr int dim = GridGeometry::GridView::dimension;
214 using GridIndexType = typename IndexTraits<typename GridGeometry::GridView>::GridIndex;
215
216 const auto numCorners = element.subEntities(dim);
217 std::vector< GridIndexType > nodalDofs(numCorners);
218 for (unsigned int i = 0; i < numCorners; ++i)
219 nodalDofs[i] = gridGeometry.vertexMapper().subIndex(element, i, dim);
220
221 return nodalDofs;
222 }
223
225 BulkCouplingMap& couplingMap_(GridIdType<bulkId>, GridIdType<lowDimId>)
226 { return bulkCouplingData_; }
227
229 LowDimCouplingMap& couplingMap_(GridIdType<lowDimId>, GridIdType<bulkId>)
230 { return lowDimCouplingData_; }
231
232private:
233
235 template< class Embeddings, class GridGeometry>
236 std::unordered_map< typename IndexTraits<typename GridGeometry::GridView>::GridIndex, typename IndexTraits<typename GridGeometry::GridView>::GridIndex >
237 makeInsertionToGridIndexMap_(std::shared_ptr<const Embeddings> embeddings, const GridGeometry& gridGeometry) const
238 {
239 using GridIndexType = typename IndexTraits<typename GridGeometry::GridView>::GridIndex;
240
241 std::unordered_map< GridIndexType, GridIndexType > map;
242 for (const auto& e : elements(gridGeometry.gridView()))
243 map.insert( std::make_pair( embeddings->template insertionIndex<bulkId>(e), gridGeometry.elementMapper().index(e) ) );
244
245 return map;
246 }
247
248 BulkCouplingMap bulkCouplingData_;
249 LowDimCouplingMap lowDimCouplingData_;
250};
251
252} // end namespace Dumux
253
254#endif
Defines the index types used for grid and local indices.
Definition: adapt.hh:29
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
Base class for the coupling mapper that sets up and stores the coupling maps between two domains of d...
Definition: couplingmapperbase.hh:51
const BulkCouplingMap & couplingMap(GridIdType< bulkId >, GridIdType< lowDimId >) const
returns coupling data for bulk -> lowDim
Definition: couplingmapperbase.hh:156
static constexpr GridIdType<(dim==bulkDim ? bulkId :lowDimId) > gridId()
Allow retrievment of grid id for a given grid dimension.
Definition: couplingmapperbase.hh:142
static constexpr auto facetGridId
Definition: couplingmapperbase.hh:130
typename std::conditional< id==bulkId, BulkStencil, LowDimStencil >::type Stencil
Export the stencil type for the provided grid index.
Definition: couplingmapperbase.hh:134
std::vector< typename IndexTraits< typename GridGeometry::GridView >::GridIndex > extractNodalDofs_(const typename GridGeometry::GridView::template Codim< 0 >::Entity &element, const GridGeometry &gridGeometry)
Creates a container with the nodal dofs within an element.
Definition: couplingmapperbase.hh:210
void update(const BulkFVG &bulkFvGridGeometry, const LowDimFVG &lowDimFvGridGeometry, std::shared_ptr< const Embeddings > embeddings)
Update coupling maps. This is the standard interface and has to be overloaded by the implementation.
Definition: couplingmapperbase.hh:150
LowDimCouplingMap & couplingMap_(GridIdType< lowDimId >, GridIdType< bulkId >)
returns non-const coupling data for lowDim -> bulk
Definition: couplingmapperbase.hh:229
const LowDimCouplingMap & couplingMap(GridIdType< lowDimId >, GridIdType< bulkId >) const
returns coupling data for lowDim -> bulk
Definition: couplingmapperbase.hh:160
void update_(const BulkFVG &bulkFvGridGeometry, const LowDimFVG &lowDimFvGridGeometry, std::shared_ptr< const Embeddings > embeddings, AddCouplingEntryPolicy &&addCouplingEntryPolicy)
Update coupling maps.
Definition: couplingmapperbase.hh:175
static constexpr auto bulkGridId
Export grid ids.
Definition: couplingmapperbase.hh:129
typename std::conditional< i==bulkId, BulkCouplingMap, LowDimCouplingMap >::type CouplingMap
Export the coupling map type.
Definition: couplingmapperbase.hh:138
BulkCouplingMap & couplingMap_(GridIdType< bulkId >, GridIdType< lowDimId >)
returns non-const coupling data for bulk -> lowDim
Definition: couplingmapperbase.hh:225