3.2-git
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
36#include <dune/common/version.hh>
37#if DUNE_VERSION_LT(DUNE_COMMON,2,7)
38#include <dune/common/parallel/collectivecommunication.hh>
39#else
40#include <dune/common/parallel/communication.hh>
41#endif
42
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>
48
52
53#include "griddata.hh"
54
55namespace Dumux {
56
63template <class Grid>
64class GridManager;
65
71template <class GridType>
73{
74public:
75 using Grid = GridType;
77
81 void init(const std::string& modelParamGroup = "")
82 {
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.");
87 }
88
93 {
95 return *dgfGridPtr();
96 else
97 return *gridPtr();
98 }
99
104 {
105 if (Dune::MPIHelper::getCollectiveCommunication().size() > 1)
106 {
107 // if we may have dgf parameters use load balancing of the dgf pointer
109 {
110 dgfGridPtr().loadBalance();
111 // update the grid data
112 gridData_ = std::make_shared<GridData>(dgfGridPtr());
113 }
114
115 // if we have gmsh parameters we have to manually load balance the data
117 {
118 // element and face markers are communicated during load balance
119 auto dh = gridData_->createGmshDataHandle();
120 gridPtr()->loadBalance(dh.interface());
121 gridPtr()->communicate(dh.interface(), Dune::InteriorBorder_All_Interface, Dune::ForwardCommunication);
122 }
123 else
124 gridPtr()->loadBalance();
125 }
126 }
127
128 std::shared_ptr<GridData> getGridData() const
129 {
130 if (!gridData_)
131 DUNE_THROW(Dune::IOError, "No grid data available");
132
133 return gridData_;
134 }
135
136
137protected:
138
142 std::shared_ptr<Grid>& gridPtr()
143 {
145 return gridPtr_;
146 else
147 DUNE_THROW(Dune::InvalidStateException, "You are using DGF. To get the grid pointer use method dgfGridPtr()!");
148 }
149
153 Dune::GridPtr<Grid>& dgfGridPtr()
154 {
156 return dgfGridPtr_;
157 else
158 DUNE_THROW(Dune::InvalidStateException, "The DGF grid pointer is only available if the grid was constructed with a DGF file!");
159 }
160
164 std::string getFileExtension(const std::string& fileName) const
165 {
166 std::size_t i = fileName.rfind('.', fileName.length());
167 if (i != std::string::npos)
168 {
169 return(fileName.substr(i+1, fileName.length() - i));
170 }
171 else
172 {
173 DUNE_THROW(Dune::IOError, "Please provide and extension for your grid file ('"<< fileName << "')!");
174 }
175 return "";
176 }
177
184 void makeGridFromFile(const std::string& fileName,
185 const std::string& modelParamGroup)
186 {
187 // We found a file in the input file...does it have a supported extension?
188 const std::string extension = getFileExtension(fileName);
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);
191
192 // Dune Grid Format (DGF) files
193 if (extension == "dgf")
194 {
196 dgfGridPtr() = Dune::GridPtr<Grid>(fileName.c_str(), Dune::MPIHelper::getCommunicator());
197 gridData_ = std::make_shared<GridData>(dgfGridPtr_);
198 }
199
200 // Gmsh mesh format
201 else if (extension == "msh")
202 {
203 // get some optional parameters
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);
207
208 if (domainMarkers)
210
211 // as default read it on all processes in parallel
212 if(domainMarkers)
213 {
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));
219 }
220 else
221 {
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());
225 }
226 }
227
228 // VTK file formats for unstructured grids
229 else if (extension == "vtu" || extension == "vtp")
230 {
231 if (Dune::MPIHelper::getCollectiveCommunication().size() > 1)
232 DUNE_THROW(Dune::NotImplemented, "Reading grids in parallel from VTK file formats is currently not supported!");
233
234 VTKReader vtkReader(fileName);
235 VTKReader::Data cellData, pointData;
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));
240 }
241 }
242
246 void makeGridFromDgfFile(const std::string& fileName)
247 {
248 // We found a file in the input file...does it have a supported extension?
249 const std::string extension = getFileExtension(fileName);
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);
252
254 dgfGridPtr() = Dune::GridPtr<Grid>(fileName.c_str(), Dune::MPIHelper::getCommunicator());
255 gridData_ = std::make_shared<GridData>(dgfGridPtr_);
256 }
257
262
266 template <int dim, int dimworld>
268 const std::string& modelParamGroup)
269 {
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));
273
274 using CellArray = std::array<unsigned int, dim>;
275 CellArray cells; cells.fill(1);
276 cells = getParamFromGroup<CellArray>(modelParamGroup, "Grid.Cells", cells);
277
278 // make the grid
279 if (cellType == CellType::Cube)
280 {
281 gridPtr() = Dune::StructuredGridFactory<Grid>::createCubeGrid(lowerLeft, upperRight, cells);
282 }
283 else if (cellType == CellType::Simplex)
284 {
285 gridPtr() = Dune::StructuredGridFactory<Grid>::createSimplexGrid(lowerLeft, upperRight, cells);
286 }
287 else
288 {
289 DUNE_THROW(Dune::GridError, "Unknown cell type for making structured grid! Choose Cube or Simplex.");
290 }
291 }
292
296 void maybeRefineGrid(const std::string& modelParamGroup)
297 {
298 if (hasParamInGroup(modelParamGroup, "Grid.Refinement"))
299 grid().globalRefine(getParamFromGroup<int>(modelParamGroup, "Grid.Refinement"));
300 }
301
307
312
313 std::shared_ptr<Grid> gridPtr_;
314 Dune::GridPtr<Grid> dgfGridPtr_;
315
316 std::shared_ptr<GridData> gridData_;
317};
318
319template <class Grid>
320class GridManager : public GridManagerBase<Grid> {};
321
322} // end namespace Dumux
323
324#endif
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 &paramGroup, const std::string &param)
Check whether a key exists in the parameter tree with a model group prefix.
Definition: parameters.hh:391
Definition: adapt.hh:29
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