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>
35#include <dune/common/parallel/communication.hh>
36#include <dune/common/parallel/mpihelper.hh>
37#include <dune/grid/io/file/dgfparser/dgfparser.hh>
38#include <dune/grid/io/file/gmshreader.hh>
39#include <dune/grid/common/gridfactory.hh>
40#include <dune/grid/utility/structuredgridfactory.hh>
64template <
class Gr
idType>
74 void init(
const std::string& modelParamGroup =
"")
76 DUNE_THROW(Dune::NotImplemented,
77 "The header with the GridManager specialization for grid type " << Dune::className<Grid>()
78 <<
" is not included or no specialization has been implemented!"
79 <<
" In case of the latter, consider providing your own GridManager.");
98 if (Dune::MPIHelper::getCollectiveCommunication().size() > 1)
112 auto dh =
gridData_->createGmshDataHandle();
113 gridPtr()->loadBalance(dh.interface());
114 gridPtr()->communicate(dh.interface(), Dune::InteriorBorder_All_Interface, Dune::ForwardCommunication);
124 DUNE_THROW(Dune::IOError,
"No grid data available");
140 DUNE_THROW(Dune::InvalidStateException,
"You are using DGF. To get the grid pointer use method dgfGridPtr()!");
151 DUNE_THROW(Dune::InvalidStateException,
"The DGF grid pointer is only available if the grid was constructed with a DGF file!");
159 std::size_t i = fileName.rfind(
'.', fileName.length());
160 if (i != std::string::npos)
162 return(fileName.substr(i+1, fileName.length() - i));
166 DUNE_THROW(Dune::IOError,
"Please provide and extension for your grid file ('"<< fileName <<
"')!");
178 const std::string& modelParamGroup)
182 if (extension !=
"dgf" && extension !=
"msh" && extension !=
"vtu" && extension !=
"vtp")
183 DUNE_THROW(Dune::IOError,
"Grid type " << Dune::className<Grid>() <<
" doesn't support grid files with extension: *."<< extension);
186 if (extension ==
"dgf")
189 dgfGridPtr() = Dune::GridPtr<Grid>(fileName.c_str(), Dune::MPIHelper::getCommunicator());
194 else if (extension ==
"msh")
197 const bool verbose = getParamFromGroup<bool>(modelParamGroup,
"Grid.Verbosity",
false);
198 const bool boundarySegments = getParamFromGroup<bool>(modelParamGroup,
"Grid.BoundarySegments",
false);
199 const bool domainMarkers = getParamFromGroup<bool>(modelParamGroup,
"Grid.DomainMarkers",
false);
207 std::vector<int> boundaryMarkers, elementMarkers;
208 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
209 Dune::GmshReader<Grid>::read(*gridFactory, fileName, boundaryMarkers, elementMarkers, verbose, boundarySegments);
210 gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
211 gridData_ = std::make_shared<GridData>(
gridPtr_, std::move(gridFactory), std::move(elementMarkers), std::move(boundaryMarkers));
215 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
216 Dune::GmshReader<Grid>::read(*gridFactory, fileName, verbose, boundarySegments);
217 gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
222 else if (extension ==
"vtu" || extension ==
"vtp")
224 if (Dune::MPIHelper::getCollectiveCommunication().size() > 1)
225 DUNE_THROW(Dune::NotImplemented,
"Reading grids in parallel from VTK file formats is currently not supported!");
229 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
230 const bool verbose = getParamFromGroup<bool>(modelParamGroup,
"Grid.Verbosity",
false);
231 gridPtr() = vtkReader.
readGrid(*gridFactory, cellData, pointData, verbose);
232 gridData_ = std::make_shared<GridData>(
gridPtr_, std::move(gridFactory), std::move(cellData), std::move(pointData));
243 if(extension !=
"dgf")
244 DUNE_THROW(Dune::IOError,
"Grid type " << Dune::className<Grid>() <<
" only supports DGF (*.dgf) but the specified filename has extension: *."<< extension);
247 dgfGridPtr() = Dune::GridPtr<Grid>(fileName.c_str(), Dune::MPIHelper::getCommunicator());
259 template <
int dim,
int dimworld>
261 const std::string& modelParamGroup)
263 using GlobalPosition = Dune::FieldVector<typename Grid::ctype, dimworld>;
264 const auto upperRight = getParamFromGroup<GlobalPosition>(modelParamGroup,
"Grid.UpperRight");
265 const auto lowerLeft = getParamFromGroup<GlobalPosition>(modelParamGroup,
"Grid.LowerLeft", GlobalPosition(0.0));
267 using CellArray = std::array<unsigned int, dim>;
268 CellArray cells; cells.fill(1);
269 cells = getParamFromGroup<CellArray>(modelParamGroup,
"Grid.Cells", cells);
272 if (cellType == CellType::Cube)
274 gridPtr() = Dune::StructuredGridFactory<Grid>::createCubeGrid(lowerLeft, upperRight, cells);
276 else if (cellType == CellType::Simplex)
278 gridPtr() = Dune::StructuredGridFactory<Grid>::createSimplexGrid(lowerLeft, upperRight, cells);
282 DUNE_THROW(Dune::GridError,
"Unknown cell type for making structured grid! Choose Cube or Simplex.");
292 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:390
Class for grid data attached to dgf or gmsh grid files.
Definition: griddata.hh:66
The grid manager (this is the class used by the user) for all supported grid managers that constructs...
Definition: gridmanager_base.hh:313
The grid manager base interface (public) and methods common to most grid manager specializations (pro...
Definition: gridmanager_base.hh:66
std::shared_ptr< GridData > getGridData() const
Definition: gridmanager_base.hh:121
GridType Grid
Definition: gridmanager_base.hh:68
Dune::GridPtr< Grid > dgfGridPtr_
Definition: gridmanager_base.hh:307
void maybeRefineGrid(const std::string &modelParamGroup)
Refines a grid after construction if GridParameterGroup.Refinement is set in the input file.
Definition: gridmanager_base.hh:289
void makeGridFromFile(const std::string &fileName, const std::string &modelParamGroup)
Makes a grid from a file. We currently support.
Definition: gridmanager_base.hh:177
void makeStructuredGrid(CellType cellType, const std::string &modelParamGroup)
Makes a structured cube grid using the structured grid factory.
Definition: gridmanager_base.hh:260
Dune::GridPtr< Grid > & dgfGridPtr()
Returns a reference to the DGF grid pointer (Dune::GridPtr<Grid>).
Definition: gridmanager_base.hh:146
std::shared_ptr< Grid > gridPtr_
Definition: gridmanager_base.hh:306
Grid & grid()
Returns a reference to the grid.
Definition: gridmanager_base.hh:85
std::shared_ptr< GridData > gridData_
Definition: gridmanager_base.hh:309
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:239
bool enableGmshDomainMarkers_
A state variable if domain markers have been read from a Gmsh file.
Definition: gridmanager_base.hh:304
void loadBalance()
Call loadBalance() function of the grid.
Definition: gridmanager_base.hh:96
CellType
The cell types for structured grids.
Definition: gridmanager_base.hh:254
@ Simplex
Definition: gridmanager_base.hh:254
@ Cube
Definition: gridmanager_base.hh:254
std::shared_ptr< Grid > & gridPtr()
Returns a reference to the grid pointer (std::shared_ptr<Grid>)
Definition: gridmanager_base.hh:135
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:74
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:299
std::string getFileExtension(const std::string &fileName) const
Returns the filename extension of a given filename.
Definition: gridmanager_base.hh:157
A vtk file reader using tinyxml2 as xml backend.
Definition: vtkreader.hh:51
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:128
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:59