version 3.10-dev
multidomain/facet/gridmanager.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//
17#ifndef DUMUX_FACETCOUPLING_GRID_MANAGER_HH
18#define DUMUX_FACETCOUPLING_GRID_MANAGER_HH
19
20#include <cassert>
21#include <map>
22#include <tuple>
23#include <type_traits>
24#include <vector>
25
26#include <dune/common/exceptions.hh>
27#include <dune/common/hybridutilities.hh>
28#include <dune/grid/common/gridfactory.hh>
29
34
35#include "gmshreader.hh"
36
37namespace Dumux {
38namespace FCGridManagerChecks {
39
40 // The grid creator and grid data classes provided below
41 // require that the grids passed as template arguments are
42 // ordered in descending grid dimension and they must have
43 // the same world dimension. The following helper structs
44 // verify these conditions and are reused below.
45 // evaluates if dim is descending or dimworld is equal for two grids
46 template<bool checkDimWorld, typename G1, typename G2>
47 static constexpr bool evalCondition()
48 {
49 return checkDimWorld ? int(G1::dimensionworld) == int(G2::dimensionworld)
50 : int(G1::dimension) > int(G2::dimension);
51 }
52
53 // helper structs to evaluate conditions on the grid
54 template<bool checkDimWorld, typename... Gs> struct FulfillConditions;
55 template<bool checkDimWorld, typename G1, typename... Gs>
56 struct FulfillConditions<checkDimWorld, G1, Gs...>
57 {
58 using G2 = typename std::tuple_element_t<0, std::tuple<Gs...>>;
59 static constexpr bool value = evalCondition<checkDimWorld, G1, G2>() && FulfillConditions<checkDimWorld, Gs...>::value;
60 };
61 template<bool checkDimWorld, typename G1, typename G2>
62 struct FulfillConditions<checkDimWorld, G1, G2> { static constexpr bool value = evalCondition<checkDimWorld, G1, G2>(); };
63}
64
73template<typename... Grids>
75{
76 // make sure all grids have the same world dimension and are ordered in descending dimension
77 static_assert(FCGridManagerChecks::FulfillConditions<false, Grids...>::value, "All grids must have the same world dimension!");
78 static_assert(FCGridManagerChecks::FulfillConditions<true, Grids...>::value, "Grids must be ordered w.r.t the dimension in descending order!");
79
81 static constexpr std::size_t numGrids = sizeof...(Grids);
83 template<std::size_t id> using Grid = typename std::tuple_element_t<id, std::tuple<Grids...>>;
85 template<std::size_t id> using GridDataPtr = std::shared_ptr< GridData<Grid<id>> >;
87 template<std::size_t id> using Intersection = typename Grid<id>::LeafGridView::Intersection;
88
89public:
91 template<std::size_t id> using GridData = GridData<Grid<id>>;
92
94 template<std::size_t id>
95 void setGridData(GridData<id>&& gridData)
96 {
97 std::get<id>(gridDataPtrTuple_) = std::make_shared<GridData<id>>( std::move(gridData) );
98 }
99
101 template<std::size_t id>
102 std::shared_ptr<const GridData<id>> getSubDomainGridData() const
103 { return std::get<id>(gridDataPtrTuple_); }
104
106 template<std::size_t id>
107 int getElementDomainMarker(const typename Grid<id>::template Codim<0>::Entity& element) const
108 { return std::get<id>(gridDataPtrTuple_)->getElementDomainMarker(element); }
109
111 template<std::size_t id>
112 int getBoundaryDomainMarker(const typename Grid<id>::LeafGridView::Intersection& is) const
113 { return std::get<id>(gridDataPtrTuple_)->getBoundaryDomainMarker(is); }
114
116 template<std::size_t id>
117 int getBoundaryDomainMarker(int boundarySegmentIndex) const
118 { return std::get<id>(gridDataPtrTuple_)->getBoundaryDomainMarker(boundarySegmentIndex); }
119
121 template<std::size_t id>
122 bool wasInserted(const Intersection<id>& intersection) const
123 { return std::get<id>(gridDataPtrTuple_)->wasInserted(intersection); }
124
125private:
128 GridDataPtrTuple gridDataPtrTuple_;
129};
130
143template<typename... Grids>
145{
146 // make sure all grids have the same world dimension and are ordered in descending dimension
147 static_assert(FCGridManagerChecks::FulfillConditions<false, Grids...>::value, "All grids must have the same world dimension!");
148 static_assert(FCGridManagerChecks::FulfillConditions<true, Grids...>::value, "Grids must be ordered w.r.t the dimension in descending order!");
149
151 template<std::size_t id> using Grid = typename std::tuple_element_t<id, std::tuple<Grids...>>;
153 template<std::size_t id> using GridFactory = typename Dune::GridFactory<Grid<id>>;
154
156 using GIType = typename IndexTraits< typename Grid<0>::LeafGridView >::GridIndex;
158 using EmbedmentMap = std::unordered_map<GIType, std::vector<GIType>>;
159
160public:
162 template<std::size_t id> using GridView = typename Grid<id>::LeafGridView;
163
165 static constexpr std::size_t numGrids = sizeof...(Grids);
167 static constexpr int bulkGridId = 0;
169 static constexpr int bulkDim = Grid<bulkGridId>::dimension;
170
174 using GridIndexType = GIType;
175
177 template<std::size_t id>
178 const GridView<id>& gridView() const
179 { return *std::get<id>(gridViewPtrTuple_); }
180
182 template<std::size_t id, class Entity>
183 GridIndexType insertionIndex(const Entity& entity) const
184 { return std::get<id>(gridFactoryPtrTuple_)->insertionIndex(entity); }
185
187 template<std::size_t id>
188 typename std::unordered_map< GridIndexType, std::vector<GridIndexType> >::mapped_type
189 embeddedEntityIndices(const typename Grid<id>::template Codim<0>::Entity& element) const
190 {
191 const auto& map = embeddedEntityMaps_[id];
192 auto it = map.find( std::get<id>(gridFactoryPtrTuple_)->insertionIndex(element) );
193 if (it != map.end()) return it->second;
194 else return typename std::unordered_map< GridIndexType, std::vector<GridIndexType> >::mapped_type();
195 }
196
198 template<std::size_t id>
199 typename std::unordered_map< GridIndexType, std::vector<GridIndexType> >::mapped_type
200 adjoinedEntityIndices(const typename Grid<id>::template Codim<0>::Entity& element) const
201 {
202 const auto& map = adjoinedEntityMaps_[id];
203 auto it = map.find( std::get<id>(gridFactoryPtrTuple_)->insertionIndex(element) );
204 if (it != map.end()) return it->second;
205 else return typename std::unordered_map< GridIndexType, std::vector<GridIndexType> >::mapped_type();
206 }
207
209 const std::unordered_map< GridIndexType, std::vector<GridIndexType> >& embeddedEntityMap(std::size_t id) const
210 { assert(id < numGrids); return embeddedEntityMaps_[id]; }
211
213 std::unordered_map< GridIndexType, std::vector<GridIndexType> >& embeddedEntityMap(std::size_t id)
214 { assert(id < numGrids); return embeddedEntityMaps_[id]; }
215
217 const std::unordered_map< GridIndexType, std::vector<GridIndexType> >& adjoinedEntityMap(std::size_t id) const
218 { assert(id < numGrids); return adjoinedEntityMaps_[id]; }
219
221 std::unordered_map< GridIndexType, std::vector<GridIndexType> >& adjoinedEntityMap(std::size_t id)
222 { assert(id < numGrids); return adjoinedEntityMaps_[id]; }
223
225 const std::vector<GridIndexType>& gridHierarchyIndices(std::size_t id) const
226 { assert(id < numGrids); return gridVertexIndices_[id]; }
227
229 std::size_t numVerticesInHierarchy() const
230 { return numVerticesInHierarchy_; }
231
241 template<std::size_t id>
242 void setData( std::shared_ptr<Grid<id>> gridPtr,
243 std::shared_ptr<GridFactory<id>> gridFactoryPtr,
244 EmbedmentMap&& embeddedEntityMap,
245 EmbedmentMap&& adjoinedEntityMap,
246 std::vector<GridIndexType>&& gridVertexIndices,
247 std::size_t numVerticesInHierarchy )
248 {
249 std::get<id>(gridViewPtrTuple_) = std::make_shared<GridView<id>>(gridPtr->leafGridView());
250 std::get<id>(gridFactoryPtrTuple_) = gridFactoryPtr;
251 embeddedEntityMaps_[id] = std::move(embeddedEntityMap);
252 adjoinedEntityMaps_[id] = std::move(adjoinedEntityMap);
253 gridVertexIndices_[id] = std::move(gridVertexIndices);
254 numVerticesInHierarchy_ = numVerticesInHierarchy;
255 }
256
257private:
259 std::array<EmbedmentMap, numGrids> embeddedEntityMaps_;
260 std::array<EmbedmentMap, numGrids> adjoinedEntityMaps_;
261
263 std::size_t numVerticesInHierarchy_;
264 std::array<std::vector<GridIndexType>, numGrids> gridVertexIndices_;
265
267 using Indices = std::make_index_sequence<numGrids>;
268 template<std::size_t id> using GridViewPtr = std::shared_ptr<GridView<id>>;
270 GridPtrTuple gridViewPtrTuple_;
271
273 template<std::size_t id> using GridFactoryPtr = std::shared_ptr< Dune::GridFactory<Grid<id>> >;
274 using GridFactoryPtrTuple = typename makeFromIndexedType<std::tuple, GridFactoryPtr, Indices>::type;
275 GridFactoryPtrTuple gridFactoryPtrTuple_;
276};
277
287template<typename... Grids>
289{
290 // make sure all grids have the same world dimension and are ordered in descending dimension
291 static_assert(FCGridManagerChecks::FulfillConditions<false, Grids...>::value, "All grids must have the same world dimension!");
292 static_assert(FCGridManagerChecks::FulfillConditions<true, Grids...>::value, "Grids must be ordered w.r.t the dimension in descending order!");
293
294 // we use a wrapper class for the grid data containing the data on all grids
296public:
298 template<std::size_t id> using Grid = typename std::tuple_element_t<id, std::tuple<Grids...>>;
300 template<std::size_t id> using GridPtr = typename std::shared_ptr< Grid<id> >;
301
303 static constexpr std::size_t numGrids = sizeof...(Grids);
305 static constexpr int bulkGridId = 0;
306
311
313 template<std::size_t id>
314 const Grid<id>& grid() const
315 { return *std::get<id>(gridPtrTuple_); }
316
318 std::shared_ptr<const GridData> getGridData() const
319 {
320 if (!enableEntityMarkers_)
321 DUNE_THROW(Dune::IOError, "No grid data available");
322 return gridDataPtr_;
323 }
324
326 std::shared_ptr<const Embeddings> getEmbeddings() const
327 { return embeddingsPtr_; }
328
330 void init(const std::string& paramGroup = "")
331 {
332 // reset the grid & embedding data
333 gridDataPtr_ = std::make_shared<GridData>();
334 embeddingsPtr_ = std::make_shared<Embeddings>();
335
336 // get filename and determine grid file extension
337 const auto fileName = getParamFromGroup<std::string>(paramGroup, "Grid.File");
338 const auto ext = getFileExtension(fileName);
339
340 // get some parameters
341 const bool verbose = getParamFromGroup<bool>(paramGroup, "Grid.Verbosity", false);
342 const bool domainMarkers = getParamFromGroup<bool>(paramGroup, "Grid.DomainMarkers", false);
343 const bool boundarySegments = getParamFromGroup<bool>(paramGroup, "Grid.BoundarySegments", false);
344
345 // forward to the corresponding reader
346 if (ext == "msh")
347 {
348 const auto thresh = getParamFromGroup<std::size_t>(paramGroup, "Grid.GmshPhysicalEntityThreshold", 0);
350 gmshReader.read(fileName, (boundarySegments ? thresh : 0), verbose);
351 passDataFromReader(gmshReader, domainMarkers, boundarySegments);
352 }
353 else
354 DUNE_THROW(Dune::NotImplemented, "Reader for grid files of type ." + ext);
355
356 // find out if entity markers are active
357 enableEntityMarkers_ = domainMarkers || boundarySegments;
358 }
359
362 {
363 using namespace Dune::Hybrid;
364 forEach(integralRange(Dune::Hybrid::size(gridPtrTuple_)), [&](const auto id)
365 {
366 std::get<id>(this->gridPtrTuple_)->loadBalance();
367 });
368 }
369
370protected:
372 template<std::size_t id>
374 { return *std::get<id>(gridPtrTuple_); }
375
377 std::shared_ptr<Embeddings> getEmbeddings_()
378 { return embeddingsPtr_; }
379
380private:
382 static std::string getFileExtension(const std::string& fileName)
383 {
384 const auto pos = fileName.rfind('.', fileName.length());
385 if (pos != std::string::npos)
386 return(fileName.substr(pos+1, fileName.length() - pos));
387 else
388 DUNE_THROW(Dune::IOError, "Please provide an extension for your grid file ('"<< fileName << "')!");
389 }
390
392 template<typename MeshFileReader>
393 void passDataFromReader(MeshFileReader& reader, bool domainMarkers, bool boundarySegments)
394 {
395 const auto& vertices = reader.gridVertices();
396
397 using namespace Dune::Hybrid;
398 forEach(integralRange(Dune::Hybrid::size(gridPtrTuple_)), [&](const auto id)
399 {
400 using GridFactory = Dune::GridFactory<Grid<id>>;
401 auto factoryPtr = std::make_shared<GridFactory>();
402
403 // insert grid vertices
404 for (const auto idx : reader.vertexIndices(id))
405 factoryPtr->insertVertex(vertices[idx]);
406
407 // insert elements
408 for (const auto& e : reader.elementData(id))
409 factoryPtr->insertElement(e.gt, e.cornerIndices);
410
411 // insert boundary segments
412 if (boundarySegments)
413 for (const auto& segment : reader.boundarySegmentData(id))
414 factoryPtr->insertBoundarySegment(segment);
415
416 // make grid
417 auto gridPtr = std::shared_ptr<Grid<id>>(factoryPtr->createGrid());
418
419 // maybe create and set grid data object
420 if (domainMarkers || boundarySegments)
421 {
422 typename GridDataWrapper::template GridData<id> gridData( gridPtr,
423 factoryPtr,
424 std::move(reader.elementMarkerMap(id)),
425 std::move(reader.boundaryMarkerMap(id)) );
426 gridDataPtr_->template setGridData<id>( std::move(gridData) );
427 }
428
429 // copy the embeddings
430 embeddingsPtr_->template setData<id>( gridPtr,
431 factoryPtr,
432 std::move(reader.embeddedEntityMap(id)),
433 std::move(reader.adjoinedEntityMap(id)),
434 std::move(reader.vertexIndices(id)),
435 vertices.size() );
436
437 // set the grid pointer
438 std::get<id>(gridPtrTuple_) = gridPtr;
439 });
440 }
441
443 using Indices = std::make_index_sequence<numGrids>;
444 using GridPtrTuple = typename makeFromIndexedType<std::tuple, GridPtr, Indices>::type;
445 GridPtrTuple gridPtrTuple_;
446
448 bool enableEntityMarkers_;
449 std::shared_ptr<GridData> gridDataPtr_;
450
452 std::shared_ptr<Embeddings> embeddingsPtr_;
453};
454
455} // end namespace Dumux
456
457#endif
Contains the embeddings between grids with codimension one among the grid hierarchy....
Definition: multidomain/facet/gridmanager.hh:145
std::unordered_map< GridIndexType, std::vector< GridIndexType > > & embeddedEntityMap(std::size_t id)
Returns non-const reference to maps of the embedded entities.
Definition: multidomain/facet/gridmanager.hh:213
std::size_t numVerticesInHierarchy() const
Returns the number of vertices contained in the entire grid hierarch.
Definition: multidomain/facet/gridmanager.hh:229
const std::vector< GridIndexType > & gridHierarchyIndices(std::size_t id) const
Returns the hierachy's insertion indices that make up the grid for the given id.
Definition: multidomain/facet/gridmanager.hh:225
static constexpr int bulkDim
state the dimension of the highest-dimensional grid
Definition: multidomain/facet/gridmanager.hh:169
GridView< bulkGridId > BulkGridView
export the bulk grid type
Definition: multidomain/facet/gridmanager.hh:172
const std::unordered_map< GridIndexType, std::vector< GridIndexType > > & adjoinedEntityMap(std::size_t id) const
Returns const reference to the maps of the adjoined entities of dimension d+1.
Definition: multidomain/facet/gridmanager.hh:217
std::unordered_map< GridIndexType, std::vector< GridIndexType > >::mapped_type adjoinedEntityIndices(const typename Grid< id >::template Codim< 0 >::Entity &element) const
Returns the insertion indices of the entities in which the element is embedded.
Definition: multidomain/facet/gridmanager.hh:200
static constexpr int bulkGridId
export the grid id of the bulk grid (descending grid dim -> always zero!)
Definition: multidomain/facet/gridmanager.hh:167
GridIndexType insertionIndex(const Entity &entity) const
return the insertion index of an entity of the i-th grid
Definition: multidomain/facet/gridmanager.hh:183
std::unordered_map< GridIndexType, std::vector< GridIndexType > >::mapped_type embeddedEntityIndices(const typename Grid< id >::template Codim< 0 >::Entity &element) const
Returns the insertion indices of the entities embedded in given element.
Definition: multidomain/facet/gridmanager.hh:189
typename Grid< id >::LeafGridView GridView
export the i-th grid view type
Definition: multidomain/facet/gridmanager.hh:162
void setData(std::shared_ptr< Grid< id > > gridPtr, std::shared_ptr< GridFactory< id > > gridFactoryPtr, EmbedmentMap &&embeddedEntityMap, EmbedmentMap &&adjoinedEntityMap, std::vector< GridIndexType > &&gridVertexIndices, std::size_t numVerticesInHierarchy)
Sets the required data for a specific grid on the hierarchy.
Definition: multidomain/facet/gridmanager.hh:242
GIType GridIndexType
export the type used for indices
Definition: multidomain/facet/gridmanager.hh:174
const std::unordered_map< GridIndexType, std::vector< GridIndexType > > & embeddedEntityMap(std::size_t id) const
Returns const reference to maps of the embedded entities.
Definition: multidomain/facet/gridmanager.hh:209
static constexpr std::size_t numGrids
export the number of created grids
Definition: multidomain/facet/gridmanager.hh:165
const GridView< id > & gridView() const
return reference to the i-th grid view
Definition: multidomain/facet/gridmanager.hh:178
std::unordered_map< GridIndexType, std::vector< GridIndexType > > & adjoinedEntityMap(std::size_t id)
Returns non-const reference to the maps of the adjoined entities of dimension d+1.
Definition: multidomain/facet/gridmanager.hh:221
Reads gmsh files where (n-1)-dimensional grids are defined on the faces or edges of n-dimensional gri...
Definition: gmshreader.hh:50
void read(const std::string &fileName, bool verbose=false)
Definition: gmshreader.hh:73
Grid data object to store element and boundary segment markers for all grids of the hierarchy.
Definition: multidomain/facet/gridmanager.hh:75
int getBoundaryDomainMarker(int boundarySegmentIndex) const
Returns the boundary marker for a given boundary segment index.
Definition: multidomain/facet/gridmanager.hh:117
std::shared_ptr< const GridData< id > > getSubDomainGridData() const
return the grid data for a specific grid
Definition: multidomain/facet/gridmanager.hh:102
GridData< Grid< id > > GridData
export the i-th grid data type
Definition: multidomain/facet/gridmanager.hh:91
bool wasInserted(const Intersection< id > &intersection) const
Returns true if an intersection was inserted during grid creation.
Definition: multidomain/facet/gridmanager.hh:122
int getElementDomainMarker(const typename Grid< id >::template Codim< 0 >::Entity &element) const
Returns domain marker of an element.
Definition: multidomain/facet/gridmanager.hh:107
void setGridData(GridData< id > &&gridData)
set the grid data object for the i-th grid
Definition: multidomain/facet/gridmanager.hh:95
int getBoundaryDomainMarker(const typename Grid< id >::LeafGridView::Intersection &is) const
Returns the boundary marker of an intersection.
Definition: multidomain/facet/gridmanager.hh:112
Creates the grids in the context of hybrid-dimensional coupled models, where the (n-1)-dimensional do...
Definition: multidomain/facet/gridmanager.hh:289
typename std::tuple_element_t< id, std::tuple< Grids... > > Grid
export the i-th grid type
Definition: multidomain/facet/gridmanager.hh:298
typename std::shared_ptr< Grid< id > > GridPtr
export the i-th grid pointer type
Definition: multidomain/facet/gridmanager.hh:300
static constexpr std::size_t numGrids
export the number of created grids
Definition: multidomain/facet/gridmanager.hh:303
std::shared_ptr< const Embeddings > getEmbeddings() const
return a pointer to the object containing embeddings
Definition: multidomain/facet/gridmanager.hh:326
Grid< id > & grid_()
return non-const reference to i-th grid
Definition: multidomain/facet/gridmanager.hh:373
void loadBalance()
Distributes the grid on all processes of a parallel computation.
Definition: multidomain/facet/gridmanager.hh:361
std::shared_ptr< Embeddings > getEmbeddings_()
return non-const pointer to the object containing embeddings
Definition: multidomain/facet/gridmanager.hh:377
static constexpr int bulkGridId
export the grid id of the bulk grid (descending grid dim -> always zero!)
Definition: multidomain/facet/gridmanager.hh:305
const Grid< id > & grid() const
returns the i-th grid
Definition: multidomain/facet/gridmanager.hh:314
std::shared_ptr< const GridData > getGridData() const
return a pointer to the grid data object
Definition: multidomain/facet/gridmanager.hh:318
void init(const std::string &paramGroup="")
creates the grids from a file given in parameter tree
Definition: multidomain/facet/gridmanager.hh:330
Class for grid data attached to dgf or gmsh grid files.
Defines the index types used for grid and local indices.
static constexpr bool evalCondition()
Definition: multidomain/facet/gridmanager.hh:47
Definition: adapt.hh:17
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
typename std::tuple_element_t< 0, std::tuple< Gs... > > G2
Definition: multidomain/facet/gridmanager.hh:58
Definition: multidomain/facet/gridmanager.hh:54
Structure to define the index types used for grid and local indices.
Definition: indextraits.hh:26
Definition: utility.hh:28
Utilities for template meta programming.