3.1-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#include <dune/common/parallel/collectivecommunication.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
55template <class Grid>
56class GridManager;
57
63template <class GridType>
65{
66public:
67 using Grid = GridType;
69
73 void init(const std::string& modelParamGroup = "")
74 {
75 DUNE_THROW(Dune::NotImplemented,
76 "The header with the GridManager specialization for grid type " << Dune::className<Grid>()
77 << " is not included or no specialization has been implemented!"
78 << " In case of the latter, consider providing your own GridManager.");
79 }
80
85 {
87 return *dgfGridPtr();
88 else
89 return *gridPtr();
90 }
91
96 {
97 if (Dune::MPIHelper::getCollectiveCommunication().size() > 1)
98 {
99 // if we may have dgf parameters use load balancing of the dgf pointer
101 {
102 dgfGridPtr().loadBalance();
103 // update the grid data
104 gridData_ = std::make_shared<GridData>(dgfGridPtr());
105 }
106
107 // if we have gmsh parameters we have to manually load balance the data
109 {
110 // element and face markers are communicated during load balance
111 auto dh = gridData_->createGmshDataHandle();
112 gridPtr()->loadBalance(dh.interface());
113 gridPtr()->communicate(dh.interface(), Dune::InteriorBorder_All_Interface, Dune::ForwardCommunication);
114 }
115 else
116 gridPtr()->loadBalance();
117 }
118 }
119
120 std::shared_ptr<GridData> getGridData() const
121 {
122 if (!gridData_)
123 DUNE_THROW(Dune::IOError, "No grid data available");
124
125 return gridData_;
126 }
127
128
129protected:
130
134 std::shared_ptr<Grid>& gridPtr()
135 {
137 return gridPtr_;
138 else
139 DUNE_THROW(Dune::InvalidStateException, "You are using DGF. To get the grid pointer use method dgfGridPtr()!");
140 }
141
145 Dune::GridPtr<Grid>& dgfGridPtr()
146 {
148 return dgfGridPtr_;
149 else
150 DUNE_THROW(Dune::InvalidStateException, "The DGF grid pointer is only available if the grid was constructed with a DGF file!");
151 }
152
156 std::string getFileExtension(const std::string& fileName) const
157 {
158 std::size_t i = fileName.rfind('.', fileName.length());
159 if (i != std::string::npos)
160 {
161 return(fileName.substr(i+1, fileName.length() - i));
162 }
163 else
164 {
165 DUNE_THROW(Dune::IOError, "Please provide and extension for your grid file ('"<< fileName << "')!");
166 }
167 return "";
168 }
169
176 void makeGridFromFile(const std::string& fileName,
177 const std::string& modelParamGroup)
178 {
179 // We found a file in the input file...does it have a supported extension?
180 const std::string extension = getFileExtension(fileName);
181 if (extension != "dgf" && extension != "msh" && extension != "vtu" && extension != "vtp")
182 DUNE_THROW(Dune::IOError, "Grid type " << Dune::className<Grid>() << " doesn't support grid files with extension: *."<< extension);
183
184 // Dune Grid Format (DGF) files
185 if (extension == "dgf")
186 {
188 dgfGridPtr() = Dune::GridPtr<Grid>(fileName.c_str(), Dune::MPIHelper::getCommunicator());
189 gridData_ = std::make_shared<GridData>(dgfGridPtr_);
190 }
191
192 // Gmsh mesh format
193 else if (extension == "msh")
194 {
195 // get some optional parameters
196 const bool verbose = getParamFromGroup<bool>(modelParamGroup, "Grid.Verbosity", false);
197 const bool boundarySegments = getParamFromGroup<bool>(modelParamGroup, "Grid.BoundarySegments", false);
198 const bool domainMarkers = getParamFromGroup<bool>(modelParamGroup, "Grid.DomainMarkers", false);
199
200 if (domainMarkers)
202
203 // as default read it on all processes in parallel
204 if(domainMarkers)
205 {
206 std::vector<int> boundaryMarkers, elementMarkers;
207 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
208 Dune::GmshReader<Grid>::read(*gridFactory, fileName, boundaryMarkers, elementMarkers, verbose, boundarySegments);
209 gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
210 gridData_ = std::make_shared<GridData>(gridPtr_, std::move(gridFactory), std::move(elementMarkers), std::move(boundaryMarkers));
211 }
212 else
213 {
214 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
215 Dune::GmshReader<Grid>::read(*gridFactory, fileName, verbose, boundarySegments);
216 gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
217 }
218 }
219
220 // VTK file formats for unstructured grids
221 else if (extension == "vtu" || extension == "vtp")
222 {
223 if (Dune::MPIHelper::getCollectiveCommunication().size() > 1)
224 DUNE_THROW(Dune::NotImplemented, "Reading grids in parallel from VTK file formats is currently not supported!");
225
226 VTKReader vtkReader(fileName);
227 VTKReader::Data cellData, pointData;
228 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
229 const bool verbose = getParamFromGroup<bool>(modelParamGroup, "Grid.Verbosity", false);
230 gridPtr() = vtkReader.readGrid(*gridFactory, cellData, pointData, verbose);
231 gridData_ = std::make_shared<GridData>(gridPtr_, std::move(gridFactory), std::move(cellData), std::move(pointData));
232 }
233 }
234
238 void makeGridFromDgfFile(const std::string& fileName)
239 {
240 // We found a file in the input file...does it have a supported extension?
241 const std::string extension = getFileExtension(fileName);
242 if(extension != "dgf")
243 DUNE_THROW(Dune::IOError, "Grid type " << Dune::className<Grid>() << " only supports DGF (*.dgf) but the specified filename has extension: *."<< extension);
244
246 dgfGridPtr() = Dune::GridPtr<Grid>(fileName.c_str(), Dune::MPIHelper::getCommunicator());
247 gridData_ = std::make_shared<GridData>(dgfGridPtr_);
248 }
249
254
258 template <int dim, int dimworld>
260 const std::string& modelParamGroup)
261 {
262 using GlobalPosition = Dune::FieldVector<typename Grid::ctype, dimworld>;
263 const auto upperRight = getParamFromGroup<GlobalPosition>(modelParamGroup, "Grid.UpperRight");
264 const auto lowerLeft = getParamFromGroup<GlobalPosition>(modelParamGroup, "Grid.LowerLeft", GlobalPosition(0.0));
265
266 using CellArray = std::array<unsigned int, dim>;
267 CellArray cells; cells.fill(1);
268 cells = getParamFromGroup<CellArray>(modelParamGroup, "Grid.Cells", cells);
269
270 // make the grid
271 if (cellType == CellType::Cube)
272 {
273 gridPtr() = Dune::StructuredGridFactory<Grid>::createCubeGrid(lowerLeft, upperRight, cells);
274 }
275 else if (cellType == CellType::Simplex)
276 {
277 gridPtr() = Dune::StructuredGridFactory<Grid>::createSimplexGrid(lowerLeft, upperRight, cells);
278 }
279 else
280 {
281 DUNE_THROW(Dune::GridError, "Unknown cell type for making structured grid! Choose Cube or Simplex.");
282 }
283 }
284
288 void maybeRefineGrid(const std::string& modelParamGroup)
289 {
290 if (hasParamInGroup(modelParamGroup, "Grid.Refinement"))
291 grid().globalRefine(getParamFromGroup<int>(modelParamGroup, "Grid.Refinement"));
292 }
293
299
304
305 std::shared_ptr<Grid> gridPtr_;
306 Dune::GridPtr<Grid> dgfGridPtr_;
307
308 std::shared_ptr<GridData> gridData_;
309};
310
311template <class Grid>
312class GridManager : public GridManagerBase<Grid> {};
313
314} // end namespace Dumux
315
316#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:454
make the local view function available whenever we use the grid geometry
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:312
The grid manager base interface (public) and methods common to most grid manager specializations (pro...
Definition: gridmanager_base.hh:65
std::shared_ptr< GridData > getGridData() const
Definition: gridmanager_base.hh:120
GridType Grid
Definition: gridmanager_base.hh:67
Dune::GridPtr< Grid > dgfGridPtr_
Definition: gridmanager_base.hh:306
void maybeRefineGrid(const std::string &modelParamGroup)
Refines a grid after construction if GridParameterGroup.Refinement is set in the input file.
Definition: gridmanager_base.hh:288
void makeGridFromFile(const std::string &fileName, const std::string &modelParamGroup)
Makes a grid from a file. We currently support.
Definition: gridmanager_base.hh:176
void makeStructuredGrid(CellType cellType, const std::string &modelParamGroup)
Makes a structured cube grid using the structured grid factory.
Definition: gridmanager_base.hh:259
Dune::GridPtr< Grid > & dgfGridPtr()
Returns a reference to the DGF grid pointer (Dune::GridPtr<Grid>).
Definition: gridmanager_base.hh:145
std::shared_ptr< Grid > gridPtr_
Definition: gridmanager_base.hh:305
Grid & grid()
Returns a reference to the grid.
Definition: gridmanager_base.hh:84
std::shared_ptr< GridData > gridData_
Definition: gridmanager_base.hh:308
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:238
bool enableGmshDomainMarkers_
A state variable if domain markers have been read from a Gmsh file.
Definition: gridmanager_base.hh:303
void loadBalance()
Call loadBalance() function of the grid.
Definition: gridmanager_base.hh:95
CellType
The cell types for structured grids.
Definition: gridmanager_base.hh:253
@ Simplex
Definition: gridmanager_base.hh:253
@ Cube
Definition: gridmanager_base.hh:253
std::shared_ptr< Grid > & gridPtr()
Returns a reference to the grid pointer (std::shared_ptr<Grid>)
Definition: gridmanager_base.hh:134
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:73
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:298
std::string getFileExtension(const std::string &fileName) const
Returns the filename extension of a given filename.
Definition: gridmanager_base.hh:156
A vtk file reader using tinyxml2 as xml backend.
Definition: vtkreader.hh:48
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:102
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:56