12#ifndef DUMUX_IO_GRID_MANAGER_YASP_HH
13#define DUMUX_IO_GRID_MANAGER_YASP_HH
15#include <dune/common/math.hh>
17#include <dune/grid/yaspgrid.hh>
18#include <dune/grid/io/file/dgfparser/dgfyasp.hh>
20#ifndef DUMUX_IO_GRID_MANAGER_BASE_HH
47template<
class Coordinates,
int dim>
52 using GlobalPosition = Dune::FieldVector<ct, dim>;
60 void init(
const std::string& modelParamGroup =
"")
65 const auto filename = getParamFromGroup<std::string>(modelParamGroup,
"Grid.File");
66 const std::string extension = ParentType::getFileExtension(filename);
68 if (extension !=
"dgf" && extension !=
"vti")
69 DUNE_THROW(Dune::IOError,
"Grid type " << Dune::className<Grid>() <<
" doesn't support grid files with extension: *."<< extension);
72 if (extension ==
"vti")
74#if DUMUX_HAVE_GRIDFORMAT
78 const bool verbose = getParamFromGroup<bool>(modelParamGroup,
"Grid.Verbosity",
false);
79 std::shared_ptr<Detail::VTKReader::ImageGrid<ct, dim>> gridInfo
80 = vtkReader.readStructuredGrid<ct, dim>(cellData, pointData, verbose);
81 const std::bitset<dim> periodic;
82 const int overlap = getParamFromGroup<int>(modelParamGroup,
"Grid.Overlap", 1);
83 const auto upperRight = gridInfo->upperRight();
84 const auto cells = gridInfo->cells();
85 const auto lowerLeft = gridInfo->lowerLeft();
87 if constexpr (std::is_same_v<Dune::EquidistantCoordinates<ct, dim>, Coordinates>)
88 init_(upperRight, cells, modelParamGroup, overlap, periodic);
90 init_(lowerLeft, upperRight, cells, modelParamGroup, overlap, periodic);
92 ParentType::gridData_ = std::make_shared<GridData<Grid>>(
93 ParentType::gridPtr(), gridInfo, std::move(cellData), std::move(pointData)
96 ParentType::enableVtkData_ =
true;
98 DUNE_THROW(Dune::IOError,
"Grid type " << Dune::className<Grid>() <<
" doesn't support grid files with extension: *."<< extension);
103 ParentType::makeGridFromDgfFile(
104 getParamFromGroup<std::string>(modelParamGroup,
"Grid.File")
108 postProcessing_(modelParamGroup);
116 const auto upperRight = getParamFromGroup<GlobalPosition>(modelParamGroup,
"Grid.UpperRight");
119 std::array<int, dim> cells; cells.fill(1);
120 cells = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Cells", cells);
124 const std::bitset<dim> periodic;
127 const int overlap = getParamFromGroup<int>(modelParamGroup,
"Grid.Overlap", 1);
129 if constexpr (std::is_same_v<Dune::EquidistantCoordinates<ct, dim>, Coordinates>)
130 init(upperRight, cells, modelParamGroup, overlap, periodic);
133 const auto lowerLeft = getParamFromGroup<GlobalPosition>(modelParamGroup,
"Grid.LowerLeft", GlobalPosition(0.0));
134 init(lowerLeft, upperRight, cells, modelParamGroup, overlap, periodic);
141 const auto prefix = modelParamGroup.empty() ? modelParamGroup : modelParamGroup +
".";
143 << prefix +
"Grid.UpperRight"
144 <<
", or a grid file in " << prefix +
"Grid.File");
153 void init(
const GlobalPosition& upperRight,
154 const std::array<int, dim>& cells,
155 const std::string& modelParamGroup =
"",
156 const int overlap = 1,
157 const std::bitset<dim> periodic = std::bitset<dim>{})
159 init_(upperRight, cells, modelParamGroup, overlap, periodic);
160 postProcessing_(modelParamGroup);
167 void init(
const GlobalPosition& lowerLeft,
168 const GlobalPosition& upperRight,
169 const std::array<int, dim>& cells,
170 const std::string& modelParamGroup =
"",
171 const int overlap = 1,
172 const std::bitset<dim> periodic = std::bitset<dim>{})
174 init_(lowerLeft, upperRight, cells, modelParamGroup, overlap, periodic);
175 postProcessing_(modelParamGroup);
183 void init_(
const GlobalPosition& upperRight,
184 const std::array<int, dim>& cells,
185 const std::string& modelParamGroup =
"",
186 const int overlap = 1,
187 const std::bitset<dim> periodic = std::bitset<dim>{})
189 static_assert(std::is_same_v<Dune::EquidistantCoordinates<ct, dim>, Coordinates>,
190 "Use init function taking lowerLeft as argument when working with EquidistantOffsetCoordinates");
195 ParentType::gridPtr() = std::make_unique<Grid>(upperRight, cells, periodic, overlap);
200 const auto partitioning = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Partitioning");
201 Dune::Yasp::FixedSizePartitioning<dim> lb(partitioning);
202 ParentType::gridPtr() = std::make_unique<Grid>(upperRight, cells, periodic, overlap,
typename Grid::Communication(), &lb);
210 void init_(
const GlobalPosition& lowerLeft,
211 const GlobalPosition& upperRight,
212 const std::array<int, dim>& cells,
213 const std::string& modelParamGroup =
"",
214 const int overlap = 1,
215 const std::bitset<dim> periodic = std::bitset<dim>{})
217 static_assert(std::is_same_v<Dune::EquidistantOffsetCoordinates<ct, dim>, Coordinates>,
218 "LowerLeft can only be specified with EquidistantOffsetCoordinates");
223 ParentType::gridPtr() = std::make_unique<Grid>(lowerLeft, upperRight, cells, periodic, overlap);
228 const auto partitioning = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Partitioning");
229 Dune::Yasp::FixedSizePartitioning<dim> lb(partitioning);
230 ParentType::gridPtr() = std::make_unique<Grid>(lowerLeft, upperRight, cells, periodic, overlap,
typename Grid::Communication(), &lb);
237 void postProcessing_(
const std::string& modelParamGroup)
240 const bool keepPhysicalOverlap = getParamFromGroup<bool>(modelParamGroup,
"Grid.KeepPhysicalOverlap",
true);
241 ParentType::grid().refineOptions(keepPhysicalOverlap);
242 ParentType::maybeRefineGrid(modelParamGroup);
243 ParentType::loadBalance();
276template<
class ctype,
int dim>
278:
public GridManagerBase<Dune::YaspGrid<dim, Dune::TensorProductCoordinates<ctype, dim> > >
287 void init(
const std::string& modelParamGroup =
"")
292 std::array<std::vector<ctype>, dim> positions;
293 for (
int i = 0; i < dim; ++i)
294 positions[i] =
getParamFromGroup<std::vector<ctype>>(modelParamGroup,
"Grid.Positions" + std::to_string(i));
297 std::array<std::vector<int>, dim> cells;
298 for (
int i = 0; i < dim; ++i)
300 cells[i].resize(positions[i].size()-1, 1.0);
301 cells[i] = getParamFromGroup<std::vector<int>>(modelParamGroup,
"Grid.Cells" + std::to_string(i), cells[i]);
305 std::array<std::vector<ctype>, dim> grading;
306 for (
int i = 0; i < dim; ++i)
308 grading[i].resize(positions[i].size()-1, 1.0);
309 grading[i] = getParamFromGroup<std::vector<ctype>>(modelParamGroup,
"Grid.Grading" + std::to_string(i), grading[i]);
313 init(positions, cells, grading, modelParamGroup);
319 void init(
const std::array<std::vector<ctype>, dim>& positions,
320 const std::array<std::vector<int>, dim>& cells,
321 const std::array<std::vector<ctype>, dim>& grading,
322 const std::string& modelParamGroup =
"")
325 const int overlap = getParamFromGroup<int>(modelParamGroup,
"Grid.Overlap", 1);
326 const bool verbose = getParamFromGroup<bool>(modelParamGroup,
"Grid.Verbosity",
false);
329 const std::bitset<dim> periodic;
332 for (
unsigned int dimIdx = 0; dimIdx < dim; ++dimIdx)
334 if (cells[dimIdx].size() + 1 != positions[dimIdx].size())
336 DUNE_THROW(Dune::RangeError,
"Make sure to specify correct \"Cells\" and \"Positions\" arrays");
338 if (grading[dimIdx].size() + 1 != positions[dimIdx].size())
340 DUNE_THROW(Dune::RangeError,
"Make sure to specify correct \"Grading\" and \"Positions\" arrays");
342 ctype temp = std::numeric_limits<ctype>::lowest();
343 for (
unsigned int posIdx = 0; posIdx < positions[dimIdx].size(); ++posIdx)
345 if (temp > positions[dimIdx][posIdx])
347 DUNE_THROW(Dune::RangeError,
"Make sure to specify a monotone increasing \"Positions\" array");
349 temp = positions[dimIdx][posIdx];
353 const auto globalPositions = computeGlobalPositions_(positions, cells, grading, verbose);
359 ParentType::gridPtr() = std::make_shared<Grid>(globalPositions, periodic, overlap);
364 const auto partitioning = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Partitioning");
365 Dune::Yasp::FixedSizePartitioning<dim> lb(partitioning);
366 ParentType::gridPtr() = std::make_shared<Grid>(globalPositions, periodic, overlap,
typename Grid::Communication(), &lb);
369 postProcessing_(modelParamGroup);
376 void postProcessing_(
const std::string& modelParamGroup)
379 const bool keepPhysicalOverlap = getParamFromGroup<bool>(modelParamGroup,
"Grid.KeepPhysicalOverlap",
true);
380 ParentType::grid().refineOptions(keepPhysicalOverlap);
381 ParentType::maybeRefineGrid(modelParamGroup);
382 ParentType::loadBalance();
386 std::array<std::vector<ctype>, dim>
387 computeGlobalPositions_(
const std::array<std::vector<ctype>, dim>& positions,
388 const std::array<std::vector<int>, dim>& cells,
389 const std::array<std::vector<ctype>, dim>& grading,
390 bool verbose =
false)
392 std::array<std::vector<ctype>, dim> globalPositions;
395 for (
int dimIdx = 0; dimIdx < dim; dimIdx++)
397 for (
int zoneIdx = 0; zoneIdx < cells[dimIdx].size(); ++zoneIdx)
399 ctype lower = positions[dimIdx][zoneIdx];
400 ctype upper = positions[dimIdx][zoneIdx+1];
401 int numCells = cells[dimIdx][zoneIdx];
402 ctype gradingFactor = grading[dimIdx][zoneIdx];
403 ctype length = upper - lower;
405 bool increasingCellSize =
false;
409 std::cout <<
"dim " << dimIdx
410 <<
" lower " << lower
411 <<
" upper " << upper
412 <<
" numCells " << numCells
413 <<
" grading " << gradingFactor;
416 if (gradingFactor > 1.0)
418 increasingCellSize =
true;
423 if (gradingFactor < 0.0)
426 gradingFactor = abs(gradingFactor);
427 if (gradingFactor < 1.0)
429 increasingCellSize =
true;
434 if (gradingFactor > 1.0 - 1e-7 && gradingFactor < 1.0 + 1e-7)
436 height = 1.0 / numCells;
439 std::cout <<
" -> h " << height * length << std::endl;
445 height = (1.0 - gradingFactor) / (1.0 - power(gradingFactor, numCells));
449 std::cout <<
" -> grading_eff " << gradingFactor
450 <<
" h_min " << height * power(gradingFactor, 0) * length
451 <<
" h_max " << height * power(gradingFactor, numCells-1) * length
456 std::vector<ctype> localPositions;
457 localPositions.push_back(0);
458 for (
int i = 0; i < numCells-1; i++)
461 if (!(gradingFactor < 1.0 + 1e-7 && gradingFactor > 1.0 - 1e-7))
463 if (increasingCellSize)
465 hI *= power(gradingFactor, i);
469 hI *= power(gradingFactor, numCells-i-1);
472 localPositions.push_back(localPositions[i] + hI);
475 for (
int i = 0; i < localPositions.size(); i++)
477 localPositions[i] *= length;
478 localPositions[i] += lower;
481 for (
unsigned int i = 0; i < localPositions.size(); ++i)
483 globalPositions[dimIdx].push_back(localPositions[i]);
486 globalPositions[dimIdx].push_back(positions[dimIdx].back());
489 return globalPositions;
493namespace Grid::Capabilities {
497template<
class Coordinates,
int dim>
typename Dune::YaspGrid< dim, Coordinates > Grid
Definition: gridmanager_yasp.hh:54
void init(const std::string &modelParamGroup="")
Make the grid. This is implemented by specializations of this method.
Definition: gridmanager_yasp.hh:60
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:153
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:167
typename Dune::YaspGrid< dim, Dune::TensorProductCoordinates< ctype, dim > > Grid
Definition: gridmanager_yasp.hh:281
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:319
void init(const std::string &modelParamGroup="")
Make the grid. This is implemented by specializations of this method.
Definition: gridmanager_yasp.hh:287
The grid manager base interface (public) and methods common to most grid manager specializations (pro...
Definition: gridmanager_base.hh:56
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:64
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:342
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
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: common/pdesolver.hh:24
static bool eval(const GV &gv)
Definition: gridmanager_yasp.hh:501
Definition: gridcapabilities.hh:27