24#ifndef DUMUX_IO_GRID_MANAGER_YASP_HH
25#define DUMUX_IO_GRID_MANAGER_YASP_HH
27#include <dune/common/math.hh>
29#include <dune/grid/yaspgrid.hh>
30#include <dune/grid/io/file/dgfparser/dgfyasp.hh>
32#ifndef DUMUX_IO_GRID_MANAGER_BASE_HH
57template<
class Coordinates,
int dim>
61 using ct =
typename Dune::YaspGrid<dim, Coordinates>::ctype;
62 using GlobalPosition = Dune::FieldVector<ct, dim>;
64 using Grid =
typename Dune::YaspGrid<dim, Coordinates>;
70 void init(
const std::string& modelParamGroup =
"")
75 ParentType::makeGridFromDgfFile(getParamFromGroup<std::string>(modelParamGroup,
"Grid.File"));
76 postProcessing_(modelParamGroup);
84 const auto upperRight = getParamFromGroup<GlobalPosition>(modelParamGroup,
"Grid.UpperRight");
87 std::array<int, dim> cells; cells.fill(1);
88 cells = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Cells", cells);
92 const std::bitset<dim> periodic;
95 const int overlap = getParamFromGroup<int>(modelParamGroup,
"Grid.Overlap", 1);
98 ParentType::gridPtr() = createGrid_(modelParamGroup, upperRight, cells, periodic, overlap, Coordinates{});
100 postProcessing_(modelParamGroup);
106 const auto prefix = modelParamGroup.empty() ? modelParamGroup : modelParamGroup +
".";
108 << prefix +
"Grid.UpperRight"
109 <<
", or a grid file in " << prefix +
"Grid.File");
118 std::unique_ptr<Grid> createGrid_(
const std::string& modelParamGroup,
119 const GlobalPosition& upperRight,
120 const std::array<int, dim>& cells,
121 const std::bitset<dim>& periodic,
123 Dune::EquidistantCoordinates<ct, dim>)
const
128 return std::make_unique<Grid>(upperRight, cells, periodic, overlap);
133 const auto partitioning = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Partitioning");
134 Dune::YaspFixedSizePartitioner<dim> lb(partitioning);
135 return std::make_unique<Grid>(upperRight, cells, periodic, overlap,
typename Grid::CollectiveCommunicationType(), &lb);
142 std::unique_ptr<Grid> createGrid_(
const std::string& modelParamGroup,
143 const GlobalPosition& upperRight,
144 const std::array<int, dim>& cells,
145 const std::bitset<dim>& periodic,
147 Dune::EquidistantOffsetCoordinates<ct, dim>)
const
149 const auto lowerLeft = getParamFromGroup<GlobalPosition>(modelParamGroup,
"Grid.LowerLeft", GlobalPosition(0.0));
154 return std::make_unique<Grid>(lowerLeft, upperRight, cells, periodic, overlap);
159 const auto partitioning = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Partitioning");
160 Dune::YaspFixedSizePartitioner<dim> lb(partitioning);
161 return std::make_unique<Grid>(lowerLeft, upperRight, cells, periodic, overlap,
typename Grid::CollectiveCommunicationType(), &lb);
168 void postProcessing_(
const std::string& modelParamGroup)
171 const bool keepPhysicalOverlap = getParamFromGroup<bool>(modelParamGroup,
"Grid.KeepPhysicalOverlap",
true);
172 ParentType::grid().refineOptions(keepPhysicalOverlap);
173 ParentType::maybeRefineGrid(modelParamGroup);
174 ParentType::loadBalance();
207template<
class ctype,
int dim>
209:
public GridManagerBase<Dune::YaspGrid<dim, Dune::TensorProductCoordinates<ctype, dim> > >
212 using Grid =
typename Dune::YaspGrid<dim, Dune::TensorProductCoordinates<ctype, dim> >;
218 void init(
const std::string& modelParamGroup =
"")
223 std::array<std::vector<ctype>, dim> positions;
224 for (
int i = 0; i < dim; ++i)
225 positions[i] =
getParamFromGroup<std::vector<ctype>>(modelParamGroup,
"Grid.Positions" + std::to_string(i));
228 std::array<std::vector<int>, dim> cells;
229 for (
int i = 0; i < dim; ++i)
231 cells[i].resize(positions[i].size()-1, 1.0);
232 cells[i] = getParamFromGroup<std::vector<int>>(modelParamGroup,
"Grid.Cells" + std::to_string(i), cells[i]);
236 std::array<std::vector<ctype>, dim> grading;
237 for (
int i = 0; i < dim; ++i)
239 grading[i].resize(positions[i].size()-1, 1.0);
240 grading[i] = getParamFromGroup<std::vector<ctype>>(modelParamGroup,
"Grid.Grading" + std::to_string(i), grading[i]);
244 init(positions, cells, grading, modelParamGroup);
250 void init(
const std::array<std::vector<ctype>, dim>& positions,
251 const std::array<std::vector<int>, dim>& cells,
252 const std::array<std::vector<ctype>, dim>& grading,
253 const std::string& modelParamGroup =
"")
258 const int overlap = getParamFromGroup<int>(modelParamGroup,
"Grid.Overlap", 1);
259 const bool verbose = getParamFromGroup<bool>(modelParamGroup,
"Grid.Verbosity",
false);
262 const std::bitset<dim> periodic;
265 for (
unsigned int dimIdx = 0; dimIdx < dim; ++dimIdx)
267 if (cells[dimIdx].size() + 1 != positions[dimIdx].size())
269 DUNE_THROW(Dune::RangeError,
"Make sure to specify correct \"Cells\" and \"Positions\" arrays");
271 if (grading[dimIdx].size() + 1 != positions[dimIdx].size())
273 DUNE_THROW(Dune::RangeError,
"Make sure to specify correct \"Grading\" and \"Positions\" arrays");
275 ctype temp = std::numeric_limits<ctype>::lowest();
276 for (
unsigned int posIdx = 0; posIdx < positions[dimIdx].size(); ++posIdx)
278 if (temp > positions[dimIdx][posIdx])
280 DUNE_THROW(Dune::RangeError,
"Make sure to specify a monotone increasing \"Positions\" array");
282 temp = positions[dimIdx][posIdx];
286 const auto globalPositions = computeGlobalPositions_(positions, cells, grading, verbose);
292 ParentType::gridPtr() = std::make_shared<Grid>(globalPositions, periodic, overlap);
297 const auto partitioning = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Partitioning");
298 Dune::YaspFixedSizePartitioner<dim> lb(partitioning);
299 ParentType::gridPtr() = std::make_shared<Grid>(globalPositions, periodic, overlap,
typename Grid::CollectiveCommunicationType(), &lb);
302 postProcessing_(modelParamGroup);
309 void postProcessing_(
const std::string& modelParamGroup)
312 const bool keepPhysicalOverlap = getParamFromGroup<bool>(modelParamGroup,
"Grid.KeepPhysicalOverlap",
true);
313 ParentType::grid().refineOptions(keepPhysicalOverlap);
314 ParentType::maybeRefineGrid(modelParamGroup);
315 ParentType::loadBalance();
319 std::array<std::vector<ctype>, dim>
320 computeGlobalPositions_(
const std::array<std::vector<ctype>, dim>& positions,
321 const std::array<std::vector<int>, dim>& cells,
322 const std::array<std::vector<ctype>, dim>& grading,
323 bool verbose =
false)
325 std::array<std::vector<ctype>, dim> globalPositions;
328 for (
int dimIdx = 0; dimIdx < dim; dimIdx++)
330 for (
int zoneIdx = 0; zoneIdx < cells[dimIdx].size(); ++zoneIdx)
332 ctype lower = positions[dimIdx][zoneIdx];
333 ctype upper = positions[dimIdx][zoneIdx+1];
334 int numCells = cells[dimIdx][zoneIdx];
335 ctype gradingFactor = grading[dimIdx][zoneIdx];
336 ctype length = upper - lower;
338 bool increasingCellSize =
false;
342 std::cout <<
"dim " << dimIdx
343 <<
" lower " << lower
344 <<
" upper " << upper
345 <<
" numCells " << numCells
346 <<
" grading " << gradingFactor;
349 if (gradingFactor > 1.0)
351 increasingCellSize =
true;
356 if (gradingFactor < 0.0)
359 gradingFactor = abs(gradingFactor);
360 if (gradingFactor < 1.0)
362 increasingCellSize =
true;
367 if (gradingFactor > 1.0 - 1e-7 && gradingFactor < 1.0 + 1e-7)
369 height = 1.0 / numCells;
372 std::cout <<
" -> h " << height * length << std::endl;
378 height = (1.0 - gradingFactor) / (1.0 - power(gradingFactor, numCells));
382 std::cout <<
" -> grading_eff " << gradingFactor
383 <<
" h_min " << height * power(gradingFactor, 0) * length
384 <<
" h_max " << height * power(gradingFactor, numCells-1) * length
389 std::vector<ctype> localPositions;
390 localPositions.push_back(0);
391 for (
int i = 0; i < numCells-1; i++)
394 if (!(gradingFactor < 1.0 + 1e-7 && gradingFactor > 1.0 - 1e-7))
396 if (increasingCellSize)
398 hI *= power(gradingFactor, i);
402 hI *= power(gradingFactor, numCells-i-1);
405 localPositions.push_back(localPositions[i] + hI);
408 for (
int i = 0; i < localPositions.size(); i++)
410 localPositions[i] *= length;
411 localPositions[i] += lower;
414 for (
unsigned int i = 0; i < localPositions.size(); ++i)
416 globalPositions[dimIdx].push_back(localPositions[i]);
419 globalPositions[dimIdx].push_back(positions[dimIdx].back());
422 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:374
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:390
Definition: common/pdesolver.hh:35
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:313
The grid manager base interface (public) and methods common to most grid manager specializations (pro...
Definition: gridmanager_base.hh:66
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:74
typename Dune::YaspGrid< dim, Coordinates > Grid
Definition: gridmanager_yasp.hh:64
void init(const std::string &modelParamGroup="")
Make the grid. This is implemented by specializations of this method.
Definition: gridmanager_yasp.hh:70
typename Dune::YaspGrid< dim, Dune::TensorProductCoordinates< ctype, dim > > Grid
Definition: gridmanager_yasp.hh:212
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:250
void init(const std::string &modelParamGroup="")
Make the grid. This is implemented by specializations of this method.
Definition: gridmanager_yasp.hh:218