24#ifndef DUMUX_IO_PORENETWORKGRID_DATA_HH
25#define DUMUX_IO_PORENETWORKGRID_DATA_HH
31#include <unordered_map>
35#include <dune/common/exceptions.hh>
36#include <dune/grid/common/gridfactory.hh>
37#include <dune/grid/common/mcmgmapper.hh>
38#include <dune/grid/io/file/dgfparser/gridptr.hh>
39#include <dune/grid/io/file/dgfparser/parser.hh>
40#include <dune/grid/utility/persistentcontainer.hh>
41#include <dune/geometry/axisalignedcubegeometry.hh>
57 static constexpr int dim = Grid::dimension;
58 static constexpr int dimWorld = Grid::dimensionworld;
59 using Intersection =
typename Grid::LeafIntersection;
60 using Element =
typename Grid::template Codim<0>::Entity;
61 using Vertex =
typename Grid::template Codim<dim>::Entity;
62 using GridView =
typename Grid::LeafGridView;
63 using GlobalPosition =
typename Element::Geometry::GlobalCoordinate;
65 using StringVector = std::vector<std::string>;
67 using Scalar =
typename Grid::ctype;
68 using PersistentParameterContainer = Dune::PersistentContainer<Grid, std::vector<typename Grid::ctype>>;
80 setParameterIndices_();
89 numSubregions_ = getParamFromGroup<std::size_t>(paramGroup_,
"Grid.NumSubregions", 0);
90 setParameterIndices_();
91 parametersForGeneratedGrid_ = std::make_unique<ParametersForGeneratedGrid>(gridView_(),
paramGroup);
98 const std::vector<double>&
parameters(
const Vertex& vertex)
const
100 if (isDgfData_ && !useCopiedDgfData_)
101 return dgfGrid_.parameters(vertex);
104 assert(!(*vertexParameters_)[vertex].empty() &&
"No parameters available.");
105 return (*vertexParameters_)[vertex];
112 const std::vector<double>&
parameters(
const Element& element)
const
114 if (isDgfData_ && !useCopiedDgfData_)
116 if (element.hasFather())
118 auto level0Element = element;
119 while(level0Element.hasFather())
120 level0Element = level0Element.father();
122 return dgfGrid_.parameters(level0Element);
126 return dgfGrid_.parameters(element);
131 assert(!(*elementParameters_)[element].empty() &&
"No parameters available.");
132 return (*elementParameters_)[element];
139 template <
class Gr
idImp,
class IntersectionImp>
140 const Dune::DGFBoundaryParameter::type&
parameters(
const Dune::Intersection<GridImp, IntersectionImp>& intersection)
const
143 return dgfGrid_.parameters(intersection);
145 DUNE_THROW(Dune::InvalidStateException,
"The parameters method is only available if the grid was constructed with a DGF file.");
153 std::vector<SmallLocalIndex> coordinationNumbers(gridView_().size(dim), 0);
155 for (
const auto &element : elements(gridView_()))
157 for (SmallLocalIndex vIdxLocal = 0; vIdxLocal < 2; ++vIdxLocal)
159 const auto vIdxGlobal = gridView_().indexSet().subIndex(element, vIdxLocal, dim);
160 coordinationNumbers[vIdxGlobal] += 1;
164 if (std::any_of(coordinationNumbers.begin(), coordinationNumbers.end(), [](
auto i){ return i == 0; }))
165 DUNE_THROW(Dune::InvalidStateException,
"One of the pores is not connected to another pore. SanitizeGrid will not help in this case. Check your grid file");
167 return coordinationNumbers;
176 DUNE_THROW(Dune::InvalidStateException,
"Assigning parameter not possible for dgf gids");
178 const auto numVertexParams = vertexParameterNames_.size();
179 const auto numElementParams = elementParameterNames_.size();
180 vertexParameters_ = makeParamContainer_(*factoryGrid_, numVertexParams, 1);
181 elementParameters_ = makeParamContainer_(*factoryGrid_, numElementParams, 0);
183 auto setParamHelper = [&](
const auto& entity,
const std::string& param,
const Scalar value)
185 setParameter_(entity, param, value);
188 auto getParamHelper = [&](
const auto& entity,
const std::string& param)
193 parametersForGeneratedGrid_->assignParameters(setParamHelper, getParamHelper, numSubregions_);
199 vertexParameters_->resize();
200 elementParameters_->resize();
201 vertexParameters_->shrinkToFit();
202 elementParameters_->shrinkToFit();
208 DUNE_THROW(Dune::InvalidStateException,
"copying dgf data only works when a dgf grid is actually used");
210 useCopiedDgfData_ =
true;
211 const auto someVertex = *(vertices(gridView_()).begin());
212 const auto someElement = *(elements(gridView_()).begin());
213 const auto numVertexParams = dgfGrid_.parameters(someVertex).size();
214 const auto numElementParams = dgfGrid_.parameters(someElement).size();
215 vertexParameters_ = makeParamContainer_(*dgfGrid_, numVertexParams, 1);
216 elementParameters_ = makeParamContainer_(*dgfGrid_, numElementParams, 0);
218 for (
const auto& element : elements(gridView_()))
220 for (
int i = 0; i < numElementParams; ++i)
221 (*elementParameters_)[element][i] = dgfGrid_.parameters(element)[i];
224 for (
const auto& vertex : vertices(gridView_()))
226 for (
int i = 0; i < numVertexParams; ++i)
227 (*vertexParameters_)[vertex][i] = dgfGrid_.parameters(vertex)[i];
238 if (
static_cast<bool>(parameterIndex_.count(paramName)))
239 return parameterIndex_.at(paramName);
242 std::stringstream list;
243 list <<
"List of parameter indices:\n";
244 for (
const auto& entry : parameterIndex_)
245 list << entry.first <<
" " << entry.second <<
"\n";
248 if (paramName.find(
"Throat") != std::string::npos)
249 msg =
"Make sure to include it in the vector of parameter names as a comment in the DGF file: Element Parameters = ... " + paramName +
" ...";
250 else if (paramName.find(
"Pore") != std::string::npos)
251 msg =
"Make sure to include it in the vector of parameter names as a comment in the DGF file: Vertex Parameters = ... " + paramName +
" ...";
261 {
return paramGroup_; }
268 return std::any_of(elementParameterNames_.begin(), elementParameterNames_.end(), [¶m](
const auto& i ){ return (i == param); });
276 return std::any_of(vertexParameterNames_.begin(), vertexParameterNames_.end(), [¶m](
const auto& i ){ return (i == param); });
282 Scalar
getParameter(
const Element& element,
const std::string& param)
const
288 Scalar
getParameter(
const Vertex& vertex,
const std::string& param)
const
296 {
return parametersForGeneratedGrid_->boundaryFaceMarkerAtPos(pos); }
302 {
return vertexParameterNames_; }
308 {
return elementParameterNames_; }
312 void setParameter_(
const Element& element,
const std::string& param,
const Scalar value)
313 { (*elementParameters_)[element][
parameterIndex(param)] = value; }
315 void setParameter_(
const Vertex& vertex,
const std::string& param,
const Scalar value)
318 void setParameterIndices_()
322 vertexParameterNames_ = StringVector{
"PoreInscribedRadius",
"PoreVolume",
"PoreLabel"};
323 elementParameterNames_ = StringVector{
"ThroatInscribedRadius",
"ThroatLength",
"ThroatLabel"};
324 if (numSubregions_ > 0)
326 vertexParameterNames_.push_back(
"PoreRegionId");
327 elementParameterNames_.push_back(
"ThroatRegionId");
333 vertexParameterNames_ = dgfFileParameterNames_(
"Vertex");
334 if (vertexParameterNames_.empty())
335 DUNE_THROW(Dune::InvalidStateException,
"No vertex parameter names specified in dgf file. Set '% Vertex parameters: Param1 Param2 ...'");
338 elementParameterNames_ = dgfFileParameterNames_(
"Element");
339 if (elementParameterNames_.empty())
340 DUNE_THROW(Dune::InvalidStateException,
"No element parameter names specified in dgf file. Set '% Element parameters: Param1 Param2 ...'");
343 if (
const auto& someElement = *(elements(gridView_()).begin()); elementParameterNames_.size() != dgfGrid_.nofParameters(someElement))
344 DUNE_THROW(Dune::InvalidStateException,
"Number of user-specified element parameters (" << elementParameterNames_.size()
345 <<
") does not match number of element parameters in dgf file (" << dgfGrid_.nofParameters(someElement) <<
")");
347 if (
const auto& someVertex = *(vertices(gridView_()).begin()); vertexParameterNames_.size() != dgfGrid_.nofParameters(someVertex))
348 DUNE_THROW(Dune::InvalidStateException,
"Number of user-specified vertex parameters (" << vertexParameterNames_.size()
349 <<
") does not match number of vertex parameters in dgf file (" << dgfGrid_.nofParameters(someVertex) <<
")");
352 for (
int i = 0; i < vertexParameterNames_.size(); ++i)
354 std::cout << vertexParameterNames_[i] <<
" is vertex parameter " << i << std::endl;
355 parameterIndex_[vertexParameterNames_[i]] = i;
358 for (
int i = 0; i < elementParameterNames_.size(); ++i)
360 std::cout << elementParameterNames_[i] <<
" is element parameter " << i << std::endl;
361 parameterIndex_[elementParameterNames_[i]] = i;
372 auto makeParamContainer_(
const Grid& grid,
int numParams,
int codim)
const
374 auto parameters = std::make_unique<PersistentParameterContainer>(grid, codim);
375 (*parameters).resize();
381 StringVector dgfFileParameterNames_(
const std::string& entity)
const
383 StringVector paramNames;
385 std::ifstream gridFile(getParamFromGroup<std::string>(paramGroup_,
"Grid.File"));
386 for (std::string
line; std::getline(gridFile,
line); )
392 if (
line.find(entity +
" parameters:", 0) != std::string::npos)
394 std::istringstream iss(
line.substr(
line.find(
":")+1, std::string::npos));
395 for (std::string item; std::getline(iss, item,
' '); )
397 paramNames.push_back(item);
407 const GridView gridView_()
const
408 {
return isDgfData_ ? dgfGrid_->leafGridView() : factoryGrid_->leafGridView(); }
411 Dune::GridPtr<Grid> dgfGrid_;
413 std::shared_ptr<Grid> factoryGrid_;
415 bool isDgfData_ =
false;
416 bool useCopiedDgfData_ =
false;
417 std::string paramGroup_;
419 std::unique_ptr<ParametersForGeneratedGrid> parametersForGeneratedGrid_;
421 std::vector<std::string> vertexParameterNames_;
422 std::vector<std::string> elementParameterNames_;
424 std::unique_ptr<PersistentParameterContainer> vertexParameters_;
425 std::unique_ptr<PersistentParameterContainer> elementParameters_;
427 std::size_t numSubregions_;
429 std::unordered_map<std::string, int> parameterIndex_;
Defines the index types used for grid and local indices.
Helper class to assign parameters to a generated grid.
This file contains functions related to calculate pore-throat properties.
Definition: discretization/porenetwork/fvelementgeometry.hh:34
constexpr Line line
Definition: couplingmanager1d3d_line.hh:43
Exception thrown if a run-time parameter is not specified correctly.
Definition: exceptions.hh:60
std::uint_least8_t SmallLocalIndex
Definition: indextraits.hh:41
Class for grid data attached to dgf or gmsh grid files.
Definition: porenetwork/griddata.hh:56
bool gridHasElementParameter(const std::string ¶m) const
Return if a given element parameter is provided by the grid.
Definition: porenetwork/griddata.hh:266
const std::vector< std::string > & vertexParameterNames() const
Returns the names of the vertex parameters.
Definition: porenetwork/griddata.hh:301
void resizeParameterContainers()
Definition: porenetwork/griddata.hh:196
GridData(Dune::GridPtr< Grid > grid, const std::string ¶mGroup)
constructor for dgf grid data
Definition: porenetwork/griddata.hh:74
const std::vector< double > & parameters(const Element &element) const
Call the parameters function of the DGF grid pointer if available for element data.
Definition: porenetwork/griddata.hh:112
const Dune::DGFBoundaryParameter::type & parameters(const Dune::Intersection< GridImp, IntersectionImp > &intersection) const
Call the parameters function of the DGF grid pointer if available.
Definition: porenetwork/griddata.hh:140
std::vector< SmallLocalIndex > getCoordinationNumbers() const
Returns the coordination numbers for all pore bodies.
Definition: porenetwork/griddata.hh:151
Scalar getParameter(const Element &element, const std::string ¶m) const
Returns the value of an element parameter.
Definition: porenetwork/griddata.hh:282
const std::vector< std::string > & elementParameterNames() const
Returns the names of the element parameters.
Definition: porenetwork/griddata.hh:307
void copyDgfData()
Definition: porenetwork/griddata.hh:205
int parameterIndex(const std::string ¶mName) const
Return the index for a given parameter name.
Definition: porenetwork/griddata.hh:234
int poreLabelAtPosForGenericGrid(const GlobalPosition &pos) const
Returns the pore label at a given position for a generic grid. This is needed by the grid creator in ...
Definition: porenetwork/griddata.hh:295
const std::string & paramGroup() const
Return the parameter group.
Definition: porenetwork/griddata.hh:260
GridData(std::shared_ptr< Grid > grid, const std::string ¶mGroup)
constructor for non-dgf grid data
Definition: porenetwork/griddata.hh:84
Scalar getParameter(const Vertex &vertex, const std::string ¶m) const
Returns the value of an vertex parameter.
Definition: porenetwork/griddata.hh:288
void assignParameters()
Assign parameters for generically created grids.
Definition: porenetwork/griddata.hh:173
const std::vector< double > & parameters(const Vertex &vertex) const
Call the parameters function of the DGF grid pointer if available for vertex data.
Definition: porenetwork/griddata.hh:98
bool gridHasVertexParameter(const std::string ¶m) const
Return if a given vertex parameter is provided by the grid.
Definition: porenetwork/griddata.hh:274
Helper class to assign parameters to a generated grid.
Definition: parametersforgeneratedgrid.hh:53