25#ifndef DUMUX_IO_GRID_MANAGER_BASE_HH
26#define DUMUX_IO_GRID_MANAGER_BASE_HH
33#include <dune/common/exceptions.hh>
34#include <dune/common/classname.hh>
36#include <dune/common/version.hh>
37#if DUNE_VERSION_LT(DUNE_COMMON,2,7)
38#include <dune/common/parallel/collectivecommunication.hh>
40#include <dune/common/parallel/communication.hh>
43#include <dune/common/parallel/mpihelper.hh>
44#include <dune/grid/io/file/dgfparser/dgfparser.hh>
45#include <dune/grid/io/file/gmshreader.hh>
46#include <dune/grid/common/gridfactory.hh>
47#include <dune/grid/utility/structuredgridfactory.hh>
71template <
class Gr
idType>
81 void init(
const std::string& modelParamGroup =
"")
83 DUNE_THROW(Dune::NotImplemented,
84 "The header with the GridManager specialization for grid type " << Dune::className<Grid>()
85 <<
" is not included or no specialization has been implemented!"
86 <<
" In case of the latter, consider providing your own GridManager.");
105 if (Dune::MPIHelper::getCollectiveCommunication().size() > 1)
119 auto dh =
gridData_->createGmshDataHandle();
120 gridPtr()->loadBalance(dh.interface());
121 gridPtr()->communicate(dh.interface(), Dune::InteriorBorder_All_Interface, Dune::ForwardCommunication);
131 DUNE_THROW(Dune::IOError,
"No grid data available");
147 DUNE_THROW(Dune::InvalidStateException,
"You are using DGF. To get the grid pointer use method dgfGridPtr()!");
158 DUNE_THROW(Dune::InvalidStateException,
"The DGF grid pointer is only available if the grid was constructed with a DGF file!");
166 std::size_t i = fileName.rfind(
'.', fileName.length());
167 if (i != std::string::npos)
169 return(fileName.substr(i+1, fileName.length() - i));
173 DUNE_THROW(Dune::IOError,
"Please provide and extension for your grid file ('"<< fileName <<
"')!");
185 const std::string& modelParamGroup)
189 if (extension !=
"dgf" && extension !=
"msh" && extension !=
"vtu" && extension !=
"vtp")
190 DUNE_THROW(Dune::IOError,
"Grid type " << Dune::className<Grid>() <<
" doesn't support grid files with extension: *."<< extension);
193 if (extension ==
"dgf")
196 dgfGridPtr() = Dune::GridPtr<Grid>(fileName.c_str(), Dune::MPIHelper::getCommunicator());
201 else if (extension ==
"msh")
204 const bool verbose = getParamFromGroup<bool>(modelParamGroup,
"Grid.Verbosity",
false);
205 const bool boundarySegments = getParamFromGroup<bool>(modelParamGroup,
"Grid.BoundarySegments",
false);
206 const bool domainMarkers = getParamFromGroup<bool>(modelParamGroup,
"Grid.DomainMarkers",
false);
214 std::vector<int> boundaryMarkers, elementMarkers;
215 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
216 Dune::GmshReader<Grid>::read(*gridFactory, fileName, boundaryMarkers, elementMarkers, verbose, boundarySegments);
217 gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
218 gridData_ = std::make_shared<GridData>(
gridPtr_, std::move(gridFactory), std::move(elementMarkers), std::move(boundaryMarkers));
222 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
223 Dune::GmshReader<Grid>::read(*gridFactory, fileName, verbose, boundarySegments);
224 gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
229 else if (extension ==
"vtu" || extension ==
"vtp")
231 if (Dune::MPIHelper::getCollectiveCommunication().size() > 1)
232 DUNE_THROW(Dune::NotImplemented,
"Reading grids in parallel from VTK file formats is currently not supported!");
236 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
237 const bool verbose = getParamFromGroup<bool>(modelParamGroup,
"Grid.Verbosity",
false);
238 gridPtr() = vtkReader.
readGrid(*gridFactory, cellData, pointData, verbose);
239 gridData_ = std::make_shared<GridData>(
gridPtr_, std::move(gridFactory), std::move(cellData), std::move(pointData));
250 if(extension !=
"dgf")
251 DUNE_THROW(Dune::IOError,
"Grid type " << Dune::className<Grid>() <<
" only supports DGF (*.dgf) but the specified filename has extension: *."<< extension);
254 dgfGridPtr() = Dune::GridPtr<Grid>(fileName.c_str(), Dune::MPIHelper::getCommunicator());
266 template <
int dim,
int dimworld>
268 const std::string& modelParamGroup)
270 using GlobalPosition = Dune::FieldVector<typename Grid::ctype, dimworld>;
271 const auto upperRight = getParamFromGroup<GlobalPosition>(modelParamGroup,
"Grid.UpperRight");
272 const auto lowerLeft = getParamFromGroup<GlobalPosition>(modelParamGroup,
"Grid.LowerLeft", GlobalPosition(0.0));
274 using CellArray = std::array<unsigned int, dim>;
275 CellArray cells; cells.fill(1);
276 cells = getParamFromGroup<CellArray>(modelParamGroup,
"Grid.Cells", cells);
279 if (cellType == CellType::Cube)
281 gridPtr() = Dune::StructuredGridFactory<Grid>::createCubeGrid(lowerLeft, upperRight, cells);
283 else if (cellType == CellType::Simplex)
285 gridPtr() = Dune::StructuredGridFactory<Grid>::createSimplexGrid(lowerLeft, upperRight, cells);
289 DUNE_THROW(Dune::GridError,
"Unknown cell type for making structured grid! Choose Cube or Simplex.");
299 grid().globalRefine(getParamFromGroup<int>(modelParamGroup,
"Grid.Refinement"));
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
The available discretization methods in Dumux.
Class for grid data attached to dgf or gmsh grid files.
A vtk file reader using tinyxml2 as xml backend.
bool hasParamInGroup(const std::string ¶mGroup, const std::string ¶m)
Check whether a key exists in the parameter tree with a model group prefix.
Definition: parameters.hh:391
Class for grid data attached to dgf or gmsh grid files.
Definition: griddata.hh:73
The grid manager (this is the class used by the user) for all supported grid managers that constructs...
Definition: gridmanager_base.hh:320
The grid manager base interface (public) and methods common to most grid manager specializations (pro...
Definition: gridmanager_base.hh:73
std::shared_ptr< GridData > getGridData() const
Definition: gridmanager_base.hh:128
GridType Grid
Definition: gridmanager_base.hh:75
Dune::GridPtr< Grid > dgfGridPtr_
Definition: gridmanager_base.hh:314
void maybeRefineGrid(const std::string &modelParamGroup)
Refines a grid after construction if GridParameterGroup.Refinement is set in the input file.
Definition: gridmanager_base.hh:296
void makeGridFromFile(const std::string &fileName, const std::string &modelParamGroup)
Makes a grid from a file. We currently support.
Definition: gridmanager_base.hh:184
void makeStructuredGrid(CellType cellType, const std::string &modelParamGroup)
Makes a structured cube grid using the structured grid factory.
Definition: gridmanager_base.hh:267
Dune::GridPtr< Grid > & dgfGridPtr()
Returns a reference to the DGF grid pointer (Dune::GridPtr<Grid>).
Definition: gridmanager_base.hh:153
std::shared_ptr< Grid > gridPtr_
Definition: gridmanager_base.hh:313
Grid & grid()
Returns a reference to the grid.
Definition: gridmanager_base.hh:92
std::shared_ptr< GridData > gridData_
Definition: gridmanager_base.hh:316
void makeGridFromDgfFile(const std::string &fileName)
Makes a grid from a DGF file. This is used by grid managers that only support DGF.
Definition: gridmanager_base.hh:246
bool enableGmshDomainMarkers_
A state variable if domain markers have been read from a Gmsh file.
Definition: gridmanager_base.hh:311
void loadBalance()
Call loadBalance() function of the grid.
Definition: gridmanager_base.hh:103
CellType
The cell types for structured grids.
Definition: gridmanager_base.hh:261
@ Simplex
Definition: gridmanager_base.hh:261
@ Cube
Definition: gridmanager_base.hh:261
std::shared_ptr< Grid > & gridPtr()
Returns a reference to the grid pointer (std::shared_ptr<Grid>)
Definition: gridmanager_base.hh:142
void init(const std::string &modelParamGroup="")
Make the grid. Implement this method in the specialization of this class for a grid type.
Definition: gridmanager_base.hh:81
bool enableDgfGridPointer_
A state variable if the DGF Dune::GridPtr has been enabled. It is always enabled if a DGF grid file w...
Definition: gridmanager_base.hh:306
std::string getFileExtension(const std::string &fileName) const
Returns the filename extension of a given filename.
Definition: gridmanager_base.hh:164
A vtk file reader using tinyxml2 as xml backend.
Definition: vtkreader.hh:49
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:103
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:57