13#ifndef DUMUX_IO_GRID_MANAGER_YASP_HH
14#define DUMUX_IO_GRID_MANAGER_YASP_HH
16#include <dune/common/math.hh>
18#include <dune/grid/yaspgrid.hh>
19#include <dune/grid/io/file/dgfparser/dgfyasp.hh>
21#ifndef DUMUX_IO_GRID_MANAGER_BASE_HH
49template<
class Coordinates,
int dim>
54 using GlobalPosition = Dune::FieldVector<ct, dim>;
62 void init(
const std::string& modelParamGroup =
"")
70 if (extension !=
"dgf" && extension !=
"vti")
71 DUNE_THROW(Dune::IOError,
"Grid type " << Dune::className<Grid>() <<
" doesn't support grid files with extension: *."<< extension);
74 if (extension ==
"vti")
76#if DUMUX_HAVE_GRIDFORMAT
81 std::shared_ptr<Detail::VTKReader::ImageGrid<ct, dim>> gridInfo
82 = vtkReader.readStructuredGrid<ct, dim>(cellData, pointData, verbose);
83 const std::bitset<dim> periodic;
85 const auto upperRight = gridInfo->upperRight();
86 const auto cells = gridInfo->cells();
87 const auto lowerLeft = gridInfo->lowerLeft();
89 if constexpr (std::is_same_v<Dune::EquidistantCoordinates<ct, dim>, Coordinates>)
90 init_(upperRight, cells, modelParamGroup, overlap, periodic);
92 init_(lowerLeft, upperRight, cells, modelParamGroup, overlap, periodic);
100 DUNE_THROW(Dune::IOError,
"Grid type " << Dune::className<Grid>() <<
" doesn't support grid files with extension: *."<< extension);
110 postProcessing_(modelParamGroup);
121 std::array<int, dim> cells; cells.fill(1);
126 const std::bitset<dim> periodic;
131 if constexpr (std::is_same_v<Dune::EquidistantCoordinates<ct, dim>, Coordinates>)
132 init(upperRight, cells, modelParamGroup, overlap, periodic);
136 init(lowerLeft, upperRight, cells, modelParamGroup, overlap, periodic);
143 const auto prefix = modelParamGroup.empty() ? modelParamGroup : modelParamGroup +
".";
145 << prefix +
"Grid.UpperRight"
146 <<
", or a grid file in " << prefix +
"Grid.File");
155 void init(
const GlobalPosition& upperRight,
156 const std::array<int, dim>& cells,
157 const std::string& modelParamGroup =
"",
158 const int overlap = 1,
159 const std::bitset<dim> periodic = std::bitset<dim>{})
161 init_(upperRight, cells, modelParamGroup, overlap, periodic);
162 postProcessing_(modelParamGroup);
169 void init(
const GlobalPosition& lowerLeft,
170 const GlobalPosition& upperRight,
171 const std::array<int, dim>& cells,
172 const std::string& modelParamGroup =
"",
173 const int overlap = 1,
174 const std::bitset<dim> periodic = std::bitset<dim>{})
176 init_(lowerLeft, upperRight, cells, modelParamGroup, overlap, periodic);
177 postProcessing_(modelParamGroup);
185 void init_(
const GlobalPosition& upperRight,
186 const std::array<int, dim>& cells,
187 const std::string& modelParamGroup =
"",
188 const int overlap = 1,
189 const std::bitset<dim> periodic = std::bitset<dim>{})
191 static_assert(std::is_same_v<Dune::EquidistantCoordinates<ct, dim>, Coordinates>,
192 "Use init function taking lowerLeft as argument when working with EquidistantOffsetCoordinates");
197 ParentType::gridPtr() = std::make_unique<Grid>(upperRight, cells, periodic, overlap);
203 Dune::Yasp::FixedSizePartitioning<dim> lb(partitioning);
204 ParentType::gridPtr() = std::make_unique<Grid>(upperRight, cells, periodic, overlap,
typename Grid::Communication(), &lb);
212 void init_(
const GlobalPosition& lowerLeft,
213 const GlobalPosition& upperRight,
214 const std::array<int, dim>& cells,
215 const std::string& modelParamGroup =
"",
216 const int overlap = 1,
217 const std::bitset<dim> periodic = std::bitset<dim>{})
219 static_assert(std::is_same_v<Dune::EquidistantOffsetCoordinates<ct, dim>, Coordinates>,
220 "LowerLeft can only be specified with EquidistantOffsetCoordinates");
225 ParentType::gridPtr() = std::make_unique<Grid>(lowerLeft, upperRight, cells, periodic, overlap);
231 Dune::Yasp::FixedSizePartitioning<dim> lb(partitioning);
232 ParentType::gridPtr() = std::make_unique<Grid>(lowerLeft, upperRight, cells, periodic, overlap,
typename Grid::Communication(), &lb);
239 void postProcessing_(
const std::string& modelParamGroup)
243 ParentType::grid().refineOptions(keepPhysicalOverlap);
244 ParentType::maybeRefineGrid(modelParamGroup);
245 ParentType::loadBalance();
278template<
class ctype,
int dim>
280:
public GridManagerBase<Dune::YaspGrid<dim, Dune::TensorProductCoordinates<ctype, dim> > >
289 void init(
const std::string& modelParamGroup =
"")
294 std::array<std::vector<ctype>, dim> positions;
295 for (
int i = 0; i < dim; ++i)
296 positions[i] =
getParamFromGroup<std::vector<ctype>>(modelParamGroup,
"Grid.Positions" + std::to_string(i));
299 std::array<std::vector<int>, dim> cells;
300 for (
int i = 0; i < dim; ++i)
302 cells[i].resize(positions[i].size()-1, 1.0);
307 std::array<std::vector<ctype>, dim> grading;
308 for (
int i = 0; i < dim; ++i)
310 grading[i].resize(positions[i].size()-1, 1.0);
315 init(positions, cells, grading, modelParamGroup);
321 void init(
const std::array<std::vector<ctype>, dim>& positions,
322 const std::array<std::vector<int>, dim>& cells,
323 const std::array<std::vector<ctype>, dim>& grading,
324 const std::string& modelParamGroup =
"")
331 const std::bitset<dim> periodic;
334 for (
unsigned int dimIdx = 0; dimIdx < dim; ++dimIdx)
336 if (cells[dimIdx].size() + 1 != positions[dimIdx].size())
338 DUNE_THROW(Dune::RangeError,
"Make sure to specify correct \"Cells\" and \"Positions\" arrays");
340 if (grading[dimIdx].size() + 1 != positions[dimIdx].size())
342 DUNE_THROW(Dune::RangeError,
"Make sure to specify correct \"Grading\" and \"Positions\" arrays");
344 ctype temp = std::numeric_limits<ctype>::lowest();
345 for (
unsigned int posIdx = 0; posIdx < positions[dimIdx].size(); ++posIdx)
347 if (temp > positions[dimIdx][posIdx])
349 DUNE_THROW(Dune::RangeError,
"Make sure to specify a monotone increasing \"Positions\" array");
351 temp = positions[dimIdx][posIdx];
355 const auto globalPositions = computeGlobalPositions_(positions, cells, grading, verbose);
367 Dune::Yasp::FixedSizePartitioning<dim> lb(partitioning);
368 ParentType::gridPtr() = std::make_shared<Grid>(globalPositions, periodic, overlap,
typename Grid::Communication(), &lb);
371 postProcessing_(modelParamGroup);
378 void postProcessing_(
const std::string& modelParamGroup)
382 ParentType::grid().refineOptions(keepPhysicalOverlap);
383 ParentType::maybeRefineGrid(modelParamGroup);
384 ParentType::loadBalance();
388 std::array<std::vector<ctype>, dim>
389 computeGlobalPositions_(
const std::array<std::vector<ctype>, dim>& positions,
390 const std::array<std::vector<int>, dim>& cells,
391 const std::array<std::vector<ctype>, dim>& grading,
392 bool verbose =
false)
394 std::array<std::vector<ctype>, dim> globalPositions;
397 for (
int dimIdx = 0; dimIdx < dim; dimIdx++)
399 for (
int zoneIdx = 0; zoneIdx < cells[dimIdx].size(); ++zoneIdx)
401 ctype lower = positions[dimIdx][zoneIdx];
402 ctype upper = positions[dimIdx][zoneIdx+1];
403 int numCells = cells[dimIdx][zoneIdx];
404 ctype gradingFactor = grading[dimIdx][zoneIdx];
405 ctype length = upper - lower;
407 bool increasingCellSize =
false;
411 std::cout <<
"dim " << dimIdx
412 <<
" lower " << lower
413 <<
" upper " << upper
414 <<
" numCells " << numCells
415 <<
" grading " << gradingFactor;
418 if (gradingFactor > 1.0)
420 increasingCellSize =
true;
425 if (gradingFactor < 0.0)
428 gradingFactor = abs(gradingFactor);
429 if (gradingFactor < 1.0)
431 increasingCellSize =
true;
436 if (gradingFactor > 1.0 - 1e-7 && gradingFactor < 1.0 + 1e-7)
438 height = 1.0 / numCells;
441 std::cout <<
" -> h " << height * length << std::endl;
447 height = (1.0 - gradingFactor) / (1.0 - power(gradingFactor, numCells));
451 std::cout <<
" -> grading_eff " << gradingFactor
452 <<
" h_min " << height * power(gradingFactor, 0) * length
453 <<
" h_max " << height * power(gradingFactor, numCells-1) * length
458 std::vector<ctype> localPositions;
459 localPositions.push_back(0);
460 for (
int i = 0; i < numCells-1; i++)
463 if (!(gradingFactor < 1.0 + 1e-7 && gradingFactor > 1.0 - 1e-7))
465 if (increasingCellSize)
467 hI *= power(gradingFactor, i);
471 hI *= power(gradingFactor, numCells-i-1);
474 localPositions.push_back(localPositions[i] + hI);
477 for (
int i = 0; i < localPositions.size(); i++)
479 localPositions[i] *= length;
480 localPositions[i] += lower;
483 for (
unsigned int i = 0; i < localPositions.size(); ++i)
485 globalPositions[dimIdx].push_back(localPositions[i]);
488 globalPositions[dimIdx].push_back(positions[dimIdx].back());
491 return globalPositions;
499template<
class Coordinates,
int dim>
typename Dune::YaspGrid< dim, Coordinates > Grid
Definition gridmanager_yasp.hh:56
void init(const std::string &modelParamGroup="")
Make the grid. This is implemented by specializations of this method.
Definition gridmanager_yasp.hh:62
void init(const GlobalPosition &upperRight, const std::array< int, dim > &cells, const std::string &modelParamGroup="", const int overlap=1, const std::bitset< dim > periodic=std::bitset< dim >{})
Make the grid using input data not read from the input file.
Definition gridmanager_yasp.hh:155
GridManagerBase< Grid > ParentType
Definition gridmanager_yasp.hh:57
void init(const GlobalPosition &lowerLeft, const GlobalPosition &upperRight, const std::array< int, dim > &cells, const std::string &modelParamGroup="", const int overlap=1, const std::bitset< dim > periodic=std::bitset< dim >{})
Make the grid using input data not read from the input file.
Definition gridmanager_yasp.hh:169
GridManagerBase< Grid > ParentType
Definition gridmanager_yasp.hh:284
typename Dune::YaspGrid< dim, Dune::TensorProductCoordinates< ctype, dim > > Grid
Definition gridmanager_yasp.hh:283
void init(const std::array< std::vector< ctype >, dim > &positions, const std::array< std::vector< int >, dim > &cells, const std::array< std::vector< ctype >, dim > &grading, const std::string &modelParamGroup="")
Make the grid using input data not read from the input file.
Definition gridmanager_yasp.hh:321
void init(const std::string &modelParamGroup="")
Make the grid. This is implemented by specializations of this method.
Definition gridmanager_yasp.hh:289
The grid manager base interface (public) and methods common to most grid manager specializations (pro...
Definition gridmanager_base.hh:56
bool enableVtkData_
Definition gridmanager_base.hh:335
std::shared_ptr< GridData > gridData_
Definition gridmanager_base.hh:340
void makeGridFromDgfFile(const std::string &fileName)
Definition gridmanager_base.hh:265
std::shared_ptr< Grid > & gridPtr()
Definition gridmanager_base.hh:163
void init(const std::string &modelParamGroup="")
Definition gridmanager_base.hh:64
std::string getFileExtension(const std::string &fileName) const
Definition gridmanager_base.hh:185
The grid manager (this is the class used by the user) for all supported grid managers that constructs...
Definition gridmanager_base.hh:344
Exception thrown if a run-time parameter is not specified correctly.
Definition exceptions.hh:48
A vtk file reader using tinyxml2 as xml backend.
Definition vtkreader.hh:360
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:368
Definition consistentlyorientedgrid.hh:20
dune-grid capabilities compatibility layer
Provides a grid manager for all supported grid managers with input file interfaces....
T getParamFromGroup(Args &&... args)
A free function to get a parameter from the parameter tree singleton with a model group.
Definition parameters.hh:149
bool hasParamInGroup(const std::string ¶mGroup, const std::string ¶m)
Check whether a key exists in the parameter tree with a model group prefix.
Definition parameters.hh:165
Definition gridcapabilities.hh:17
Definition common/pdesolver.hh:24
static bool eval(const GV &gv)
Definition gridmanager_yasp.hh:503
Definition gridcapabilities.hh:27