12#ifndef DUMUX_IO_GRID_DATA_HH
13#define DUMUX_IO_GRID_DATA_HH
19#include <dune/common/exceptions.hh>
20#include <dune/common/parallel/communication.hh>
21#include <dune/common/parallel/mpihelper.hh>
22#include <dune/grid/common/gridfactory.hh>
23#include <dune/grid/common/capabilities.hh>
24#include <dune/grid/io/file/dgfparser/parser.hh>
25#include <dune/grid/io/file/dgfparser/gridptr.hh>
30#include <dune/grid/uggrid.hh>
33#include "gridinput_.hh"
42struct isUG :
public std::false_type {};
46struct isUG<
Dune::UGGrid<dim>> :
public std::true_type {};
58 using Intersection =
typename Grid::LeafIntersection;
59 using Element =
typename Grid::template Codim<0>::Entity;
60 using Vertex =
typename Grid::template Codim<Grid::dimension>::Entity;
61 using GridInput = Detail::GridData::GridInput<Grid>;
68 GridData(std::shared_ptr<Grid> grid, std::shared_ptr<Dune::GridFactory<Grid>> factory,
69 std::vector<int>&& elementMarkers, std::vector<int>&& boundaryMarkers, std::vector<int>&& faceMarkers = std::vector<int>{})
70 : gridPtr_(std::move(grid))
71 , gridInput_(std::make_shared<GridInput>(gridPtr_, std::move(factory)))
72 , elementMarkers_(elementMarkers)
73 , boundaryMarkers_(boundaryMarkers)
74 , faceMarkers_(faceMarkers)
80 : dgfGrid_(std::move(grid))
85 GridData(std::shared_ptr<Grid> grid, std::shared_ptr<Dune::GridFactory<Grid>> factory,
87 : gridPtr_(std::move(grid))
88 , gridInput_(std::make_shared<GridInput>(gridPtr_, std::move(factory)))
90 , pointData_(pointData)
95 template<
class ImageGr
id>
96 GridData(std::shared_ptr<Grid> grid, std::shared_ptr<ImageGrid> imageGrid,
98 : gridPtr_(std::move(grid))
99 , gridInput_(std::make_shared<GridInput>(gridPtr_, std::move(imageGrid)))
100 , cellData_(cellData)
101 , pointData_(pointData)
114 const std::vector<double>&
parameters(
const Vertex& vertex)
const
118 if (vertex.level() != 0)
119 DUNE_THROW(Dune::IOError,
"You can only obtain parameters for level 0 vertices!");
121 return dgfGrid_.parameters(vertex);
124 DUNE_THROW(Dune::InvalidStateException,
"The parameters method is only available if the grid was constructed with a DGF file.");
130 const std::vector<double>&
parameters(
const Element& element)
const
134 if (element.hasFather())
136 auto level0Element = element;
137 while(level0Element.hasFather())
138 level0Element = level0Element.father();
140 return dgfGrid_.parameters(level0Element);
144 return dgfGrid_.parameters(element);
148 DUNE_THROW(Dune::InvalidStateException,
"The parameters method is only available if the grid was constructed with a DGF file.");
154 template <
class Gr
idImp,
class IntersectionImp>
155 const Dune::DGFBoundaryParameter::type&
parameters(
const Dune::Intersection<GridImp, IntersectionImp>& intersection)
const
158 return dgfGrid_.parameters(intersection);
160 DUNE_THROW(Dune::InvalidStateException,
"The parameters method is only available if the grid was constructed with a DGF file.");
178 DUNE_THROW(Dune::InvalidStateException,
"Domain markers are only available for gmsh grids.");
179 if (boundarySegmentIndex >= boundaryMarkers_.size())
180 DUNE_THROW(Dune::RangeError,
"Boundary segment index "<< boundarySegmentIndex <<
" bigger than number of boundary segments in grid.\n"
181 "Make sure to call this function only for boundaries that were defined as physical entities in gmsh.");
182 return boundaryMarkers_[boundarySegmentIndex];
197 {
return gridInput_->wasInserted(intersection); }
207 DUNE_THROW(Dune::InvalidStateException,
"Domain markers are only available for gmsh grids.");
210 auto level0element = element;
211 while (level0element.hasFather())
212 level0element = level0element.father();
217 return elementMarkers_[gridPtr_->levelGridView(0).indexSet().index(level0element)];
219 return elementMarkers_[gridInput_->insertionIndex(level0element)];
226 template<bool ug = Detail::isUG<Grid>::value,
typename std::enable_if_t<!ug, int> = 0>
229 return GmshDataHandle(*gridPtr_, *gridInput_, elementMarkers_, boundaryMarkers_, faceMarkers_);
236 template<bool ug = Detail::isUG<Grid>::value,
typename std::enable_if_t<ug, int> = 0>
239 return GmshDataHandle(*gridPtr_, *gridInput_, elementMarkers_, boundaryMarkers_);
254 double getParameter(
const Element& element,
const std::string& fieldName)
const
255 {
return getArrayParameter<double, 1>(element, fieldName)[0]; }
264 template<
class T, std::
size_t size>
265 std::array<T, size>
getArrayParameter(
const Element& element,
const std::string& fieldName)
const
268 DUNE_THROW(Dune::InvalidStateException,
"This access function is only available for data from VTK files.");
270 if (cellData_.count(fieldName) == 0)
271 DUNE_THROW(Dune::IOError,
"No field with the name " << fieldName <<
" found in cell data.");
274 auto level0element = element;
275 while (level0element.hasFather())
276 level0element = level0element.father();
279 const auto index = [&]{
280 if (gridPtr_->comm().size() > 1)
281 return gridPtr_->levelGridView(0).indexSet().index(level0element);
282 else if (Detail::GridData::CartesianGrid<Grid>)
283 return gridPtr_->levelGridView(0).indexSet().index(level0element);
285 return gridInput_->insertionIndex(level0element);
288 std::array<T, size> param;
289 const auto& data = cellData_.at(fieldName);
291 if (
const std::size_t nc = data.size()/gridPtr_->levelGridView(0).size(0); size != nc)
292 DUNE_THROW(Dune::IOError,
"Array size " << size <<
" does not match number of components of field " << nc);
294 for (std::size_t i = 0; i < size; ++i)
295 param[i] =
static_cast<T
>(data[i + size*index]);
305 double getParameter(
const Vertex& vertex,
const std::string& fieldName)
const
306 {
return getArrayParameter<double, 1>(vertex, fieldName)[0]; }
316 template<
class T, std::
size_t size>
320 DUNE_THROW(Dune::InvalidStateException,
"This access function is only available for data from VTK files.");
322 if (vertex.level() != 0)
323 DUNE_THROW(Dune::IOError,
"You can only obtain parameters for level 0 vertices!");
325 if (pointData_.count(fieldName) == 0)
326 DUNE_THROW(Dune::IOError,
"No field with the name " << fieldName <<
" found in point data");
329 const auto index = [&]{
330 if (gridPtr_->comm().size() > 1)
331 return gridPtr_->levelGridView(0).indexSet().index(vertex);
332 else if (Detail::GridData::CartesianGrid<Grid>)
333 return gridPtr_->levelGridView(0).indexSet().index(vertex);
335 return gridInput_->insertionIndex(vertex);
338 std::array<T, size> param;
339 const auto& data = pointData_.at(fieldName);
341 if (
const std::size_t nc = data.size()/gridPtr_->levelGridView(0).size(Grid::dimension); size != nc)
342 DUNE_THROW(Dune::IOError,
"Array size " << size <<
" does not match number of components of field " << nc);
344 for (std::size_t i = 0; i < size; ++i)
345 param[i] =
static_cast<T
>(data[i + size*index]);
355 DUNE_THROW(Dune::InvalidStateException,
"This access function is only available for data from VTK files.");
357 return VtkDataHandle(*gridPtr_, *gridInput_, cellData_, pointData_);
366 DUNE_THROW(Dune::InvalidStateException,
"This access function is only available for data from VTK files.");
374 {
return dataSourceType_; }
378 std::shared_ptr<Grid> gridPtr_;
379 std::shared_ptr<GridInput> gridInput_;
385 std::vector<int> elementMarkers_;
386 std::vector<int> boundaryMarkers_;
387 std::vector<int> faceMarkers_;
395 Dune::GridPtr<Grid> dgfGrid_;
Class for grid data attached to dgf or gmsh grid files.
Definition: griddata.hh:57
GridData(std::shared_ptr< Grid > grid, std::shared_ptr< Dune::GridFactory< Grid > > factory, std::vector< int > &&elementMarkers, std::vector< int > &&boundaryMarkers, std::vector< int > &&faceMarkers=std::vector< int >{})
constructor for gmsh grid data
Definition: griddata.hh:68
GridData(std::shared_ptr< Grid > grid, std::shared_ptr< ImageGrid > imageGrid, VTKReader::Data &&cellData, VTKReader::Data &&pointData)
constructor for structured VTK grid data
Definition: griddata.hh:96
double getParameter(const Vertex &vertex, const std::string &fieldName) const
Call the parameters function of the DGF grid pointer if available for vertex data.
Definition: griddata.hh:305
int getBoundaryDomainMarker(const Intersection &intersection) const
Return the boundary domain marker (Gmsh physical entity number) of an intersection Only available whe...
Definition: griddata.hh:190
double getParameter(const Element &element, const std::string &fieldName) const
Get an element parameter.
Definition: griddata.hh:254
GridData(std::shared_ptr< Grid > grid, std::shared_ptr< Dune::GridFactory< Grid > > factory, VTKReader::Data &&cellData, VTKReader::Data &&pointData)
constructor for unstructured VTK grid data
Definition: griddata.hh:85
void communicateStructuredVtkData()
Communication of the data in parallel simulations for structured grid is done manually.
Definition: griddata.hh:363
const Dune::DGFBoundaryParameter::type & parameters(const Dune::Intersection< GridImp, IntersectionImp > &intersection) const
Call the parameters function of the DGF grid pointer if available.
Definition: griddata.hh:155
GridData(Dune::GridPtr< Grid > grid)
constructor for dgf grid data
Definition: griddata.hh:79
VtkDataHandle createVtkDataHandle()
Create a data handle for communication of the data in parallel simulations via the grid.
Definition: griddata.hh:352
const std::vector< double > & parameters(const Vertex &vertex) const
Call the parameters function of the DGF grid pointer if available for vertex data.
Definition: griddata.hh:114
std::array< T, size > getArrayParameter(const Vertex &vertex, const std::string &fieldName) const
Call the parameters function of the DGF grid pointer if available for vertex data.
Definition: griddata.hh:317
int getBoundaryDomainMarker(int boundarySegmentIndex) const
Return the boundary domain marker (Gmsh physical entity number) of an intersection Only available whe...
Definition: griddata.hh:175
GmshDataHandle createGmshDataHandle()
Create a data handle for communication of the data in parallel simulations.
Definition: griddata.hh:227
std::array< T, size > getArrayParameter(const Element &element, const std::string &fieldName) const
Get an element parameter that is an array.
Definition: griddata.hh:265
int getElementDomainMarker(const Element &element) const
Return the element domain marker (Gmsh physical entity number) of an element. Only available when usi...
Definition: griddata.hh:204
DataSourceType dataSourceType() const
Definition: griddata.hh:373
DataSourceType
Definition: griddata.hh:65
const std::vector< double > & parameters(const Element &element) const
Call the parameters function of the DGF grid pointer if available for element data.
Definition: griddata.hh:130
bool wasInserted(const Intersection &intersection) const
Returns true if an intersection was inserted during grid creation.
Definition: griddata.hh:196
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:350
A data handle for commucating grid data for gmsh grids.
void communicateStructuredVtkData(const Grid &grid, const GridInput &gridInput, ::Dumux::VTKReader::Data &cellData, ::Dumux::VTKReader::Data &pointData)
Definition: vtkgriddatahandle.hh:305
Definition: common/pdesolver.hh:24
Definition: griddata.hh:42
A data handle for commucating grid data for gmsh grids.
Definition: gmshgriddatahandle.hh:37
A data handle for communicating grid data for VTK grids.
Definition: vtkgriddatahandle.hh:44
A data handle for communicating grid data for VTK grids.
A vtk file reader using tinyxml2 as xml backend.