24#ifndef DUMUX_IO_GRID_MANAGER_YASP_HH
25#define DUMUX_IO_GRID_MANAGER_YASP_HH
27#include <dune/grid/yaspgrid.hh>
28#include <dune/grid/io/file/dgfparser/dgfyasp.hh>
30#ifndef DUMUX_IO_GRID_MANAGER_BASE_HH
54template<
class Coordinates,
int dim>
58 using ct =
typename Dune::YaspGrid<dim, Coordinates>::ctype;
59 using GlobalPosition = Dune::FieldVector<ct, dim>;
61 using Grid =
typename Dune::YaspGrid<dim, Coordinates>;
67 void init(
const std::string& modelParamGroup =
"")
72 ParentType::makeGridFromDgfFile(getParamFromGroup<std::string>(modelParamGroup,
"Grid.File"));
73 postProcessing_(modelParamGroup);
81 const auto upperRight = getParamFromGroup<GlobalPosition>(modelParamGroup,
"Grid.UpperRight");
84 std::array<int, dim> cells; cells.fill(1);
85 cells = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Cells", cells);
89 const std::bitset<dim> periodic;
92 const int overlap = getParamFromGroup<int>(modelParamGroup,
"Grid.Overlap", 1);
95 ParentType::gridPtr() = createGrid_(modelParamGroup, upperRight, cells, periodic, overlap, Coordinates{});
97 postProcessing_(modelParamGroup);
103 const auto prefix = modelParamGroup ==
"" ? modelParamGroup : modelParamGroup +
".";
105 << prefix +
"Grid.UpperRight"
106 <<
", or a grid file in " << prefix +
"Grid.File");
115 std::unique_ptr<Grid> createGrid_(
const std::string& modelParamGroup,
116 const GlobalPosition& upperRight,
117 const std::array<int, dim>& cells,
118 const std::bitset<dim>& periodic,
120 Dune::EquidistantCoordinates<ct, dim>)
const
125 return std::make_unique<Grid>(upperRight, cells, periodic, overlap);
130 const auto partitioning = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Partitioning");
131 Dune::YaspFixedSizePartitioner<dim> lb(partitioning);
132 return std::make_unique<Grid>(upperRight, cells, periodic, overlap,
typename Grid::CollectiveCommunicationType(), &lb);
139 std::unique_ptr<Grid> createGrid_(
const std::string& modelParamGroup,
140 const GlobalPosition& upperRight,
141 const std::array<int, dim>& cells,
142 const std::bitset<dim>& periodic,
144 Dune::EquidistantOffsetCoordinates<ct, dim>)
const
146 const auto lowerLeft = getParamFromGroup<GlobalPosition>(modelParamGroup,
"Grid.LowerLeft", GlobalPosition(0.0));
151 return std::make_unique<Grid>(lowerLeft, upperRight, cells, periodic, overlap);
156 const auto partitioning = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Partitioning");
157 Dune::YaspFixedSizePartitioner<dim> lb(partitioning);
158 return std::make_unique<Grid>(lowerLeft, upperRight, cells, periodic, overlap,
typename Grid::CollectiveCommunicationType(), &lb);
165 void postProcessing_(
const std::string& modelParamGroup)
168 const bool keepPhysicalOverlap = getParamFromGroup<bool>(modelParamGroup,
"Grid.KeepPhysicalOverlap",
true);
169 ParentType::grid().refineOptions(keepPhysicalOverlap);
170 ParentType::maybeRefineGrid(modelParamGroup);
171 ParentType::loadBalance();
203template<
class ctype,
int dim>
205:
public GridManagerBase<Dune::YaspGrid<dim, Dune::TensorProductCoordinates<ctype, dim> > >
208 using Grid =
typename Dune::YaspGrid<dim, Dune::TensorProductCoordinates<ctype, dim> >;
214 void init(
const std::string& modelParamGroup =
"")
219 std::array<std::vector<ctype>, dim> positions;
220 for (
int i = 0; i < dim; ++i)
221 positions[i] =
getParamFromGroup<std::vector<ctype>>(modelParamGroup,
"Grid.Positions" + std::to_string(i));
224 std::array<std::vector<int>, dim> cells;
225 for (
int i = 0; i < dim; ++i)
227 cells[i].resize(positions[i].size()-1, 1.0);
228 cells[i] = getParamFromGroup<std::vector<int>>(modelParamGroup,
"Grid.Cells" + std::to_string(i), cells[i]);
232 std::array<std::vector<ctype>, dim> grading;
233 for (
int i = 0; i < dim; ++i)
235 grading[i].resize(positions[i].size()-1, 1.0);
236 grading[i] = getParamFromGroup<std::vector<ctype>>(modelParamGroup,
"Grid.Grading" + std::to_string(i), grading[i]);
240 init(positions, cells, grading, modelParamGroup);
246 void init(
const std::array<std::vector<ctype>, dim>& positions,
247 const std::array<std::vector<int>, dim>& cells,
248 const std::array<std::vector<ctype>, dim>& grading,
249 const std::string& modelParamGroup =
"")
254 const int overlap = getParamFromGroup<int>(modelParamGroup,
"Grid.Overlap", 1);
255 const bool verbose = getParamFromGroup<bool>(modelParamGroup,
"Grid.Verbosity",
false);
258 const std::bitset<dim> periodic;
261 for (
unsigned int dimIdx = 0; dimIdx < dim; ++dimIdx)
263 if (cells[dimIdx].size() + 1 != positions[dimIdx].size())
265 DUNE_THROW(Dune::RangeError,
"Make sure to specify correct \"Cells\" and \"Positions\" arrays");
267 if (grading[dimIdx].size() + 1 != positions[dimIdx].size())
269 DUNE_THROW(Dune::RangeError,
"Make sure to specify correct \"Grading\" and \"Positions\" arrays");
271 ctype temp = std::numeric_limits<ctype>::lowest();
272 for (
unsigned int posIdx = 0; posIdx < positions[dimIdx].size(); ++posIdx)
274 if (temp > positions[dimIdx][posIdx])
276 DUNE_THROW(Dune::RangeError,
"Make sure to specify a monotone increasing \"Positions\" array");
278 temp = positions[dimIdx][posIdx];
282 const auto globalPositions = computeGlobalPositions_(positions, cells, grading, verbose);
288 ParentType::gridPtr() = std::make_shared<Grid>(globalPositions, periodic, overlap);
293 const auto partitioning = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Partitioning");
294 Dune::YaspFixedSizePartitioner<dim> lb(partitioning);
295 ParentType::gridPtr() = std::make_shared<Grid>(globalPositions, periodic, overlap,
typename Grid::CollectiveCommunicationType(), &lb);
298 postProcessing_(modelParamGroup);
305 void postProcessing_(
const std::string& modelParamGroup)
308 const bool keepPhysicalOverlap = getParamFromGroup<bool>(modelParamGroup,
"Grid.KeepPhysicalOverlap",
true);
309 ParentType::grid().refineOptions(keepPhysicalOverlap);
310 ParentType::maybeRefineGrid(modelParamGroup);
311 ParentType::loadBalance();
315 std::array<std::vector<ctype>, dim>
316 computeGlobalPositions_(
const std::array<std::vector<ctype>, dim>& positions,
317 const std::array<std::vector<int>, dim>& cells,
318 const std::array<std::vector<ctype>, dim>& grading,
319 bool verbose =
false)
321 std::array<std::vector<ctype>, dim> globalPositions;
323 for (
int dimIdx = 0; dimIdx < dim; dimIdx++)
325 for (
int zoneIdx = 0; zoneIdx < cells[dimIdx].size(); ++zoneIdx)
327 ctype lower = positions[dimIdx][zoneIdx];
328 ctype upper = positions[dimIdx][zoneIdx+1];
329 int numCells = cells[dimIdx][zoneIdx];
330 ctype gradingFactor = grading[dimIdx][zoneIdx];
331 ctype length = upper - lower;
333 bool increasingCellSize =
false;
337 std::cout <<
"dim " << dimIdx
338 <<
" lower " << lower
339 <<
" upper " << upper
340 <<
" numCells " << numCells
341 <<
" grading " << gradingFactor;
344 if (gradingFactor > 1.0)
346 increasingCellSize =
true;
351 if (gradingFactor < 0.0)
354 gradingFactor = abs(gradingFactor);
355 if (gradingFactor < 1.0)
357 increasingCellSize =
true;
362 if (gradingFactor > 1.0 - 1e-7 && gradingFactor < 1.0 + 1e-7)
364 height = 1.0 / numCells;
367 std::cout <<
" -> h " << height * length << std::endl;
373 height = (1.0 - gradingFactor) / (1.0 - pow(gradingFactor, numCells));
377 std::cout <<
" -> grading_eff " << gradingFactor
378 <<
" h_min " << height * pow(gradingFactor, 0) * length
379 <<
" h_max " << height * pow(gradingFactor, numCells-1) * length
384 std::vector<ctype> localPositions;
385 localPositions.push_back(0);
386 for (
int i = 0; i < numCells-1; i++)
389 if (!(gradingFactor < 1.0 + 1e-7 && gradingFactor > 1.0 - 1e-7))
391 if (increasingCellSize)
393 hI *= pow(gradingFactor, i);
397 hI *= pow(gradingFactor, numCells-i-1);
400 localPositions.push_back(localPositions[i] + hI);
403 for (
int i = 0; i < localPositions.size(); i++)
405 localPositions[i] *= length;
406 localPositions[i] += lower;
409 for (
unsigned int i = 0; i < localPositions.size(); ++i)
411 globalPositions[dimIdx].push_back(localPositions[i]);
414 globalPositions[dimIdx].push_back(positions[dimIdx].back());
417 return globalPositions;
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:438
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:454
make the local view function available whenever we use the grid geometry
Definition: adapt.hh:29
Definition: common/properties/model.hh:34
Exception thrown if a run-time parameter is not specified correctly.
Definition: exceptions.hh:60
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
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
typename Dune::YaspGrid< dim, Coordinates > Grid
Definition: gridmanager_yasp.hh:61
void init(const std::string &modelParamGroup="")
Make the grid. This is implemented by specializations of this method.
Definition: gridmanager_yasp.hh:67
typename Dune::YaspGrid< dim, Dune::TensorProductCoordinates< ctype, dim > > Grid
Definition: gridmanager_yasp.hh:208
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:246
void init(const std::string &modelParamGroup="")
Make the grid. This is implemented by specializations of this method.
Definition: gridmanager_yasp.hh:214