version 3.11-dev
Loading...
Searching...
No Matches
gridmanager_alu.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-FileCopyrightText: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
13#ifndef DUMUX_IO_GRID_MANAGER_ALU_HH
14#define DUMUX_IO_GRID_MANAGER_ALU_HH
15
16// ALUGrid specific includes
17#if HAVE_DUNE_ALUGRID
18#include <dune/alugrid/grid.hh>
19#include <dune/alugrid/dgf.hh>
20#endif
21
22#ifndef DUMUX_IO_GRID_MANAGER_BASE_HH
24#endif
25
28
29namespace Dumux {
30
31#if HAVE_DUNE_ALUGRID
32
52template<int dim, int dimworld, Dune::ALUGridElementType elType, Dune::ALUGridRefinementType refinementType>
53class GridManager<Dune::ALUGrid<dim, dimworld, elType, refinementType>>
54: public GridManagerBase<Dune::ALUGrid<dim, dimworld, elType, refinementType>>
55{
56public:
57 using Grid = Dune::ALUGrid<dim, dimworld, elType, refinementType>;
59
63 void init(const std::string& modelParamGroup = "", bool adaptiveRestart = false)
64 {
65 // restarting an adaptive grid using Dune's BackupRestoreFacility
66 // TODO: the part after first || is backward compatibility with old sequential models remove once sequential adaptive restart is replaced
67 if (adaptiveRestart || hasParam("Restart") || hasParam("TimeManager.Restart"))
68 {
69 auto restartTime = getParamFromGroup<double>(modelParamGroup, "TimeLoop.Restart", 0.0);
70 // TODO: backward compatibility with old sequential models remove once sequential adaptive restart is replaced
71 if (hasParam("Restart") || hasParam("TimeManager.Restart"))
72 {
73 restartTime = getParamFromGroup<double>("TimeManager", "Restart");
74 std::cerr << "Warning: You are using a deprecated restart mechanism. The usage will change in the future.\n";
75 }
76
77 const int rank = Dune::MPIHelper::getCommunication().rank();
78 const std::string name = getParamFromGroup<std::string>(modelParamGroup, "Problem.Name");
79 std::ostringstream oss;
80 oss << name << "_time=" << restartTime << "_rank=" << rank << ".grs";
81 std::cout << "Restoring an ALUGrid from " << oss.str() << std::endl;
82 ParentType::gridPtr() = std::shared_ptr<Grid>(Dune::BackupRestoreFacility<Grid>::restore(oss.str()));
84 return;
85 }
86
87 // First, try to create it from a file in GridParameterGroup.File
88 else if (hasParamInGroup(modelParamGroup, "Grid.File"))
89 {
90 makeGridFromFile(getParamFromGroup<std::string>(modelParamGroup, "Grid.File"), modelParamGroup);
91 ParentType::maybeRefineGrid(modelParamGroup);
93 return;
94 }
95
96 // Then look for the necessary keys to construct from the input file
97 else if (hasParamInGroup(modelParamGroup, "Grid.UpperRight"))
98 {
99 if (elType == Dune::cube)
101 else if (elType == Dune::simplex)
103 else
104 DUNE_THROW(Dune::IOError, "ALUGrid only supports Dune::cube or Dune::simplex as cell type!");
105
106 ParentType::maybeRefineGrid(modelParamGroup);
108 }
109
110 // Didn't find a way to construct the grid
111 else
112 {
113 const auto prefix = modelParamGroup.empty() ? modelParamGroup : modelParamGroup + ".";
114 DUNE_THROW(ParameterException, "Please supply one of the parameters "
115 << prefix + "Grid.UpperRight"
116 << ", or a grid file in " << prefix + "Grid.File");
117
118 }
119 }
120
124 void makeGridFromFile(const std::string& fileName,
125 const std::string& modelParamGroup)
126 {
127 // We found a file in the input file...does it have a supported extension?
128 const std::string extension = ParentType::getFileExtension(fileName);
129 if (extension != "dgf" && extension != "msh" && extension != "vtu" && extension != "vti")
130 DUNE_THROW(Dune::IOError, "Grid type " << Dune::className<Grid>() << " doesn't support grid files with extension: *."<< extension);
131
132 // Dune Grid Format (DGF) files
133 if (extension == "dgf")
134 {
136 ParentType::dgfGridPtr() = Dune::GridPtr<Grid>(fileName.c_str(), Dune::MPIHelper::getCommunicator());
137 ParentType::gridData_ = std::make_shared<typename ParentType::GridData>(ParentType::dgfGridPtr());
138 }
139
140 // Gmsh mesh format
141 else if (extension == "msh")
142 {
143 // get some optional parameters
144 const bool verbose = getParamFromGroup<bool>(modelParamGroup, "Grid.Verbosity", false);
145 const bool boundarySegments = getParamFromGroup<bool>(modelParamGroup, "Grid.BoundarySegments", false);
146 const bool domainMarkers = getParamFromGroup<bool>(modelParamGroup, "Grid.DomainMarkers", false);
147
148 if (domainMarkers)
149 {
151 std::vector<int> boundaryMarkersInsertionIndex, boundaryMarkers, faceMarkers, elementMarkers;
152 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
153 Dune::GmshReader<Grid>::read(*gridFactory, fileName, boundaryMarkersInsertionIndex, elementMarkers, verbose, boundarySegments);
154 ParentType::gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
155
156 // reorder boundary markers according to boundarySegmentIndex
157 boundaryMarkers.resize(ParentType::gridPtr()->numBoundarySegments(), 0);
158 faceMarkers.resize(ParentType::gridPtr()->leafGridView().size(1), 0);
159 const auto& indexSet = ParentType::gridPtr()->leafGridView().indexSet();
160 for (const auto& element : elements(ParentType::gridPtr()->leafGridView()))
161 {
162 for (const auto& intersection : intersections(ParentType::gridPtr()->leafGridView(), element))
163 {
164 if (intersection.boundary() && gridFactory->wasInserted(intersection))
165 {
166 auto marker = boundaryMarkersInsertionIndex[gridFactory->insertionIndex(intersection)];
167 boundaryMarkers[intersection.boundarySegmentIndex()] = marker;
168 faceMarkers[indexSet.index(element.template subEntity<1>(intersection.indexInInside()))] = marker;
169 }
170 }
171 }
172
173 ParentType::gridData_ = std::make_shared<typename ParentType::GridData>(ParentType::gridPtr(), std::move(gridFactory),
174 std::move(elementMarkers), std::move(boundaryMarkers), std::move(faceMarkers));
175 }
176 else
177 {
178 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
179 Dune::GmshReader<Grid>::read(*gridFactory, fileName, verbose, boundarySegments);
180 ParentType::gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
181 }
182 }
183
184 // VTK file formats for unstructured grids
185 // (can be constructed from both unstructured or structured grid data file formats)
186 else if (extension == "vtu" || extension == "vti")
187 {
188 VTKReader vtkReader(fileName);
189 VTKReader::Data cellData, pointData;
190 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
191 const bool verbose = getParamFromGroup<bool>(modelParamGroup, "Grid.Verbosity", false);
192 ParentType::gridPtr() = vtkReader.readGrid(*gridFactory, cellData, pointData, verbose);
193 ParentType::gridData_ = std::make_shared<typename ParentType::GridData>(ParentType::gridPtr(), std::move(gridFactory), std::move(cellData), std::move(pointData));
195 }
196 }
197
201 template <int dimension, int dimensionworld, std::enable_if_t<dimension != dimensionworld, int> = 0>
203 const std::string& modelParamGroup)
204 {
205 DUNE_THROW(Dune::IOError, "ALUGrid currently only supports the creation of structured grids with dimension == dimensionworld. Consider reading in a grid file instead.");
206 }
207
211 template <int dimension, int dimensionworld, std::enable_if_t<dimension == dimensionworld, int> = 0>
213 const std::string& modelParamGroup)
214 {
215 // make a structured grid
216 if (elType == Dune::cube)
218 else if (elType == Dune::simplex)
220 else
221 DUNE_THROW(Dune::IOError, "ALUGrid only supports Dune::cube or Dune::simplex as cell type!");
222 }
223};
224
230template<int dim, int dimworld, Dune::ALUGridElementType elType, Dune::ALUGridRefinementType refinementType>
231class BoundaryFlag<Dune::ALUGrid<dim, dimworld, elType, refinementType>>
232{
233public:
234 BoundaryFlag() : flag_(invalidFlag_) {}
235
236 template<class Intersection>
237 BoundaryFlag(const Intersection& i) : flag_(invalidFlag_)
238 {
239 if (i.boundary())
240 flag_ = i.impl().boundaryId();
241 }
242
243 using value_type = int;
244
245 value_type get() const { return flag_; }
246
247 operator bool() const { return flag_ != invalidFlag_; }
248
249private:
250 static constexpr value_type invalidFlag_ = -1;
251 value_type flag_;
252};
253
254namespace Grid::Capabilities {
255
256// To the best of our knowledge ALUGrid is view thread-safe
257// This specialization can be removed after we depend on Dune release 2.9 in which this is guaranteed by ALUGrid itself
258template<int dim, int dimworld, Dune::ALUGridElementType elType, Dune::ALUGridRefinementType refinementType>
259struct MultithreadingSupported<Dune::ALUGrid<dim, dimworld, elType, refinementType>>
260{
261 template<class GV>
262 static bool eval(const GV&) // default is independent of the grid view
263 { return true; }
264};
265
266} // end namespace Grid::Capabilities
267
268#endif // HAVE_DUNE_ALUGRID
269
270} // end namespace Dumux
271
272#endif
Boundary flag to store e.g. in sub control volume faces.
BoundaryFlag(const Intersection &i)
Definition gridmanager_alu.hh:237
value_type get() const
Definition gridmanager_alu.hh:245
std::size_t value_type
Definition boundaryflag.hh:28
void init(const std::string &modelParamGroup="", bool adaptiveRestart=false)
Make the grid. This is implemented by specializations of this method.
Definition gridmanager_alu.hh:63
GridManagerBase< Grid > ParentType
Definition gridmanager_alu.hh:58
void makeStructuredGrid(typename ParentType::CellType cellType, const std::string &modelParamGroup)
Makes a structured cube grid using the structured grid factory.
Definition gridmanager_alu.hh:202
Dune::ALUGrid< dim, dimworld, elType, refinementType > Grid
Definition gridmanager_alu.hh:57
void makeGridFromFile(const std::string &fileName, const std::string &modelParamGroup)
Makes a grid from a file. We currently support *.dgf (Dune Grid Format) and *.msh (Gmsh mesh format).
Definition gridmanager_alu.hh:124
The grid manager base interface (public) and methods common to most grid manager specializations (pro...
Definition gridmanager_base.hh:56
void maybeRefineGrid(const std::string &modelParamGroup)
Definition gridmanager_base.hh:315
bool enableVtkData_
Definition gridmanager_base.hh:335
void makeGridFromFile(const std::string &fileName, const std::string &modelParamGroup)
Definition gridmanager_base.hh:205
void makeStructuredGrid(CellType cellType, const std::string &modelParamGroup)
Definition gridmanager_base.hh:286
Dune::GridPtr< Grid > & dgfGridPtr()
Definition gridmanager_base.hh:174
std::shared_ptr< GridData > gridData_
Definition gridmanager_base.hh:340
bool enableGmshDomainMarkers_
Definition gridmanager_base.hh:330
void loadBalance()
Definition gridmanager_base.hh:98
CellType
Definition gridmanager_base.hh:280
@ Simplex
Definition gridmanager_base.hh:280
@ Cube
Definition gridmanager_base.hh:280
std::shared_ptr< Grid > & gridPtr()
Definition gridmanager_base.hh:163
bool enableDgfGridPointer_
Definition gridmanager_base.hh:325
std::string getFileExtension(const std::string &fileName) const
Definition gridmanager_base.hh:185
The grid manager (this is the class used by the user) for all supported grid managers that constructs...
Definition gridmanager_base.hh:344
Exception thrown if a run-time parameter is not specified correctly.
Definition exceptions.hh:48
A vtk file reader using tinyxml2 as xml backend.
Definition vtkreader.hh:360
std::unordered_map< std::string, std::vector< double > > Data
the cell / point data type for point data read from a grid file
Definition vtkreader.hh:368
std::unique_ptr< Grid > readGrid(bool verbose=false) const
Read a grid from a vtk/vtu/vtp file, ignoring cell and point data.
Definition vtkreader.hh:441
dune-grid capabilities compatibility layer
Provides a grid manager for all supported grid managers with input file interfaces....
T getParamFromGroup(Args &&... args)
A free function to get a parameter from the parameter tree singleton with a model group.
Definition parameters.hh:149
bool hasParamInGroup(const std::string &paramGroup, const std::string &param)
Check whether a key exists in the parameter tree with a model group prefix.
Definition parameters.hh:165
bool hasParam(const std::string &param)
Check whether a key exists in the parameter tree.
Definition parameters.hh:157
Definition adapt.hh:17
Definition common/pdesolver.hh:24