3.4
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
gridmanager_base.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 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
25#ifndef DUMUX_IO_GRID_MANAGER_BASE_HH
26#define DUMUX_IO_GRID_MANAGER_BASE_HH
27
28#include <array>
29#include <bitset>
30#include <memory>
31#include <sstream>
32
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>
41
45
46#include "griddata.hh"
47
48namespace Dumux {
49
56template <class Grid>
57class GridManager;
58
64template <class GridType>
66{
67public:
68 using Grid = GridType;
70
74 void init(const std::string& modelParamGroup = "")
75 {
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.");
80 }
81
86 {
88 return *dgfGridPtr();
89 else
90 return *gridPtr();
91 }
92
97 {
98 if (Dune::MPIHelper::getCollectiveCommunication().size() > 1)
99 {
100 // if we may have dgf parameters use load balancing of the dgf pointer
102 {
103 dgfGridPtr().loadBalance();
104 // update the grid data
105 gridData_ = std::make_shared<GridData>(dgfGridPtr());
106 }
107
108 // if we have gmsh parameters we have to manually load balance the data
110 {
111 // element and face markers are communicated during load balance
112 auto dh = gridData_->createGmshDataHandle();
113 gridPtr()->loadBalance(dh.interface());
114 gridPtr()->communicate(dh.interface(), Dune::InteriorBorder_All_Interface, Dune::ForwardCommunication);
115 }
116 else
117 gridPtr()->loadBalance();
118 }
119 }
120
125 std::shared_ptr<GridData> getGridData() const
126 {
127 if (!gridData_)
128 DUNE_THROW(Dune::IOError, "No grid data available");
129
130 return gridData_;
131 }
132
136 bool hasGridData() const
137 { return static_cast<bool>(gridData_); }
138
139protected:
140
144 std::shared_ptr<Grid>& gridPtr()
145 {
147 return gridPtr_;
148 else
149 DUNE_THROW(Dune::InvalidStateException, "You are using DGF. To get the grid pointer use method dgfGridPtr()!");
150 }
151
155 Dune::GridPtr<Grid>& dgfGridPtr()
156 {
158 return dgfGridPtr_;
159 else
160 DUNE_THROW(Dune::InvalidStateException, "The DGF grid pointer is only available if the grid was constructed with a DGF file!");
161 }
162
166 std::string getFileExtension(const std::string& fileName) const
167 {
168 std::size_t i = fileName.rfind('.', fileName.length());
169 if (i != std::string::npos)
170 {
171 return(fileName.substr(i+1, fileName.length() - i));
172 }
173 else
174 {
175 DUNE_THROW(Dune::IOError, "Please provide and extension for your grid file ('"<< fileName << "')!");
176 }
177 return "";
178 }
179
186 void makeGridFromFile(const std::string& fileName,
187 const std::string& modelParamGroup)
188 {
189 // We found a file in the input file...does it have a supported extension?
190 const std::string extension = getFileExtension(fileName);
191 if (extension != "dgf" && extension != "msh" && extension != "vtu" && extension != "vtp")
192 DUNE_THROW(Dune::IOError, "Grid type " << Dune::className<Grid>() << " doesn't support grid files with extension: *."<< extension);
193
194 // Dune Grid Format (DGF) files
195 if (extension == "dgf")
196 {
198 dgfGridPtr() = Dune::GridPtr<Grid>(fileName.c_str(), Dune::MPIHelper::getCommunicator());
199 gridData_ = std::make_shared<GridData>(dgfGridPtr_);
200 }
201
202 // Gmsh mesh format
203 else if (extension == "msh")
204 {
205 // get some optional parameters
206 const bool verbose = getParamFromGroup<bool>(modelParamGroup, "Grid.Verbosity", false);
207 const bool boundarySegments = getParamFromGroup<bool>(modelParamGroup, "Grid.BoundarySegments", false);
208 const bool domainMarkers = getParamFromGroup<bool>(modelParamGroup, "Grid.DomainMarkers", false);
209
210 if (domainMarkers)
212
213 // as default read it on all processes in parallel
214 if(domainMarkers)
215 {
216 std::vector<int> boundaryMarkers, elementMarkers;
217 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
218 Dune::GmshReader<Grid>::read(*gridFactory, fileName, boundaryMarkers, elementMarkers, verbose, boundarySegments);
219 gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
220 gridData_ = std::make_shared<GridData>(gridPtr_, std::move(gridFactory), std::move(elementMarkers), std::move(boundaryMarkers));
221 }
222 else
223 {
224 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
225 Dune::GmshReader<Grid>::read(*gridFactory, fileName, verbose, boundarySegments);
226 gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
227 }
228 }
229
230 // VTK file formats for unstructured grids
231 else if (extension == "vtu" || extension == "vtp")
232 {
233 if (Dune::MPIHelper::getCollectiveCommunication().size() > 1)
234 DUNE_THROW(Dune::NotImplemented, "Reading grids in parallel from VTK file formats is currently not supported!");
235
236 VTKReader vtkReader(fileName);
237 VTKReader::Data cellData, pointData;
238 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
239 const bool verbose = getParamFromGroup<bool>(modelParamGroup, "Grid.Verbosity", false);
240 gridPtr() = vtkReader.readGrid(*gridFactory, cellData, pointData, verbose);
241 gridData_ = std::make_shared<GridData>(gridPtr_, std::move(gridFactory), std::move(cellData), std::move(pointData));
242 }
243 }
244
248 void makeGridFromDgfFile(const std::string& fileName)
249 {
250 // We found a file in the input file...does it have a supported extension?
251 const std::string extension = getFileExtension(fileName);
252 if(extension != "dgf")
253 DUNE_THROW(Dune::IOError, "Grid type " << Dune::className<Grid>() << " only supports DGF (*.dgf) but the specified filename has extension: *."<< extension);
254
256 dgfGridPtr() = Dune::GridPtr<Grid>(fileName.c_str(), Dune::MPIHelper::getCommunicator());
257 gridData_ = std::make_shared<GridData>(dgfGridPtr_);
258 }
259
264
268 template <int dim, int dimworld>
270 const std::string& modelParamGroup)
271 {
272 using GlobalPosition = Dune::FieldVector<typename Grid::ctype, dimworld>;
273 const auto upperRight = getParamFromGroup<GlobalPosition>(modelParamGroup, "Grid.UpperRight");
274 const auto lowerLeft = getParamFromGroup<GlobalPosition>(modelParamGroup, "Grid.LowerLeft", GlobalPosition(0.0));
275
276 using CellArray = std::array<unsigned int, dim>;
277 CellArray cells; cells.fill(1);
278 cells = getParamFromGroup<CellArray>(modelParamGroup, "Grid.Cells", cells);
279
280 // make the grid
281 if (cellType == CellType::Cube)
282 {
283 gridPtr() = Dune::StructuredGridFactory<Grid>::createCubeGrid(lowerLeft, upperRight, cells);
284 }
285 else if (cellType == CellType::Simplex)
286 {
287 gridPtr() = Dune::StructuredGridFactory<Grid>::createSimplexGrid(lowerLeft, upperRight, cells);
288 }
289 else
290 {
291 DUNE_THROW(Dune::GridError, "Unknown cell type for making structured grid! Choose Cube or Simplex.");
292 }
293 }
294
298 void maybeRefineGrid(const std::string& modelParamGroup)
299 {
300 if (hasParamInGroup(modelParamGroup, "Grid.Refinement"))
301 grid().globalRefine(getParamFromGroup<int>(modelParamGroup, "Grid.Refinement"));
302 }
303
309
314
315 std::shared_ptr<Grid> gridPtr_;
316 Dune::GridPtr<Grid> dgfGridPtr_;
317
318 std::shared_ptr<GridData> gridData_;
319};
320
321template <class Grid>
322class GridManager : public GridManagerBase<Grid> {};
323
324} // end namespace Dumux
325
326#endif
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
The available discretization methods in Dumux.
A vtk file reader using tinyxml2 as xml backend.
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:374
Definition: adapt.hh:29
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:322
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
Get an owning pointer to grid data associated with the grid.
Definition: gridmanager_base.hh:125
GridType Grid
Definition: gridmanager_base.hh:68
Dune::GridPtr< Grid > dgfGridPtr_
Definition: gridmanager_base.hh:316
void maybeRefineGrid(const std::string &modelParamGroup)
Refines a grid after construction if GridParameterGroup.Refinement is set in the input file.
Definition: gridmanager_base.hh:298
void makeGridFromFile(const std::string &fileName, const std::string &modelParamGroup)
Makes a grid from a file. We currently support.
Definition: gridmanager_base.hh:186
void makeStructuredGrid(CellType cellType, const std::string &modelParamGroup)
Makes a structured cube grid using the structured grid factory.
Definition: gridmanager_base.hh:269
Dune::GridPtr< Grid > & dgfGridPtr()
Returns a reference to the DGF grid pointer (Dune::GridPtr<Grid>).
Definition: gridmanager_base.hh:155
bool hasGridData() const
Check whether there is data associated with the grid.
Definition: gridmanager_base.hh:136
std::shared_ptr< Grid > gridPtr_
Definition: gridmanager_base.hh:315
Grid & grid()
Returns a reference to the grid.
Definition: gridmanager_base.hh:85
std::shared_ptr< GridData > gridData_
Definition: gridmanager_base.hh:318
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:248
bool enableGmshDomainMarkers_
A state variable if domain markers have been read from a Gmsh file.
Definition: gridmanager_base.hh:313
void loadBalance()
Call loadBalance() function of the grid.
Definition: gridmanager_base.hh:96
CellType
The cell types for structured grids.
Definition: gridmanager_base.hh:263
@ Simplex
Definition: gridmanager_base.hh:263
@ Cube
Definition: gridmanager_base.hh:263
std::shared_ptr< Grid > & gridPtr()
Returns a reference to the grid pointer (std::shared_ptr<Grid>)
Definition: gridmanager_base.hh:144
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:308
std::string getFileExtension(const std::string &fileName) const
Returns the filename extension of a given filename.
Definition: gridmanager_base.hh:166
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
Class for grid data attached to dgf or gmsh grid files.