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
55template<
class Coordinates,
int dim>
59 using ct =
typename Dune::YaspGrid<dim, Coordinates>::ctype;
60 using GlobalPosition = Dune::FieldVector<ct, dim>;
62 using Grid =
typename Dune::YaspGrid<dim, Coordinates>;
68 void init(
const std::string& modelParamGroup =
"")
73 ParentType::makeGridFromDgfFile(getParamFromGroup<std::string>(modelParamGroup,
"Grid.File"));
74 postProcessing_(modelParamGroup);
82 const auto upperRight = getParamFromGroup<GlobalPosition>(modelParamGroup,
"Grid.UpperRight");
85 std::array<int, dim> cells; cells.fill(1);
86 cells = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Cells", cells);
90 const std::bitset<dim> periodic;
93 const int overlap = getParamFromGroup<int>(modelParamGroup,
"Grid.Overlap", 1);
96 ParentType::gridPtr() = createGrid_(modelParamGroup, upperRight, cells, periodic, overlap, Coordinates{});
98 postProcessing_(modelParamGroup);
104 const auto prefix = modelParamGroup.empty() ? modelParamGroup : modelParamGroup +
".";
106 << prefix +
"Grid.UpperRight"
107 <<
", or a grid file in " << prefix +
"Grid.File");
116 std::unique_ptr<Grid> createGrid_(
const std::string& modelParamGroup,
117 const GlobalPosition& upperRight,
118 const std::array<int, dim>& cells,
119 const std::bitset<dim>& periodic,
121 Dune::EquidistantCoordinates<ct, dim>)
const
126 return std::make_unique<Grid>(upperRight, cells, periodic, overlap);
131 const auto partitioning = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Partitioning");
132 Dune::YaspFixedSizePartitioner<dim> lb(partitioning);
133 return std::make_unique<Grid>(upperRight, cells, periodic, overlap,
typename Grid::CollectiveCommunicationType(), &lb);
140 std::unique_ptr<Grid> createGrid_(
const std::string& modelParamGroup,
141 const GlobalPosition& upperRight,
142 const std::array<int, dim>& cells,
143 const std::bitset<dim>& periodic,
145 Dune::EquidistantOffsetCoordinates<ct, dim>)
const
147 const auto lowerLeft = getParamFromGroup<GlobalPosition>(modelParamGroup,
"Grid.LowerLeft", GlobalPosition(0.0));
152 return std::make_unique<Grid>(lowerLeft, upperRight, cells, periodic, overlap);
157 const auto partitioning = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Partitioning");
158 Dune::YaspFixedSizePartitioner<dim> lb(partitioning);
159 return std::make_unique<Grid>(lowerLeft, upperRight, cells, periodic, overlap,
typename Grid::CollectiveCommunicationType(), &lb);
166 void postProcessing_(
const std::string& modelParamGroup)
169 const bool keepPhysicalOverlap = getParamFromGroup<bool>(modelParamGroup,
"Grid.KeepPhysicalOverlap",
true);
170 ParentType::grid().refineOptions(keepPhysicalOverlap);
171 ParentType::maybeRefineGrid(modelParamGroup);
172 ParentType::loadBalance();
205template<
class ctype,
int dim>
207:
public GridManagerBase<Dune::YaspGrid<dim, Dune::TensorProductCoordinates<ctype, dim> > >
210 using Grid =
typename Dune::YaspGrid<dim, Dune::TensorProductCoordinates<ctype, dim> >;
216 void init(
const std::string& modelParamGroup =
"")
221 std::array<std::vector<ctype>, dim> positions;
222 for (
int i = 0; i < dim; ++i)
223 positions[i] =
getParamFromGroup<std::vector<ctype>>(modelParamGroup,
"Grid.Positions" + std::to_string(i));
226 std::array<std::vector<int>, dim> cells;
227 for (
int i = 0; i < dim; ++i)
229 cells[i].resize(positions[i].size()-1, 1.0);
230 cells[i] = getParamFromGroup<std::vector<int>>(modelParamGroup,
"Grid.Cells" + std::to_string(i), cells[i]);
234 std::array<std::vector<ctype>, dim> grading;
235 for (
int i = 0; i < dim; ++i)
237 grading[i].resize(positions[i].size()-1, 1.0);
238 grading[i] = getParamFromGroup<std::vector<ctype>>(modelParamGroup,
"Grid.Grading" + std::to_string(i), grading[i]);
242 init(positions, cells, grading, modelParamGroup);
248 void init(
const std::array<std::vector<ctype>, dim>& positions,
249 const std::array<std::vector<int>, dim>& cells,
250 const std::array<std::vector<ctype>, dim>& grading,
251 const std::string& modelParamGroup =
"")
256 const int overlap = getParamFromGroup<int>(modelParamGroup,
"Grid.Overlap", 1);
257 const bool verbose = getParamFromGroup<bool>(modelParamGroup,
"Grid.Verbosity",
false);
260 const std::bitset<dim> periodic;
263 for (
unsigned int dimIdx = 0; dimIdx < dim; ++dimIdx)
265 if (cells[dimIdx].size() + 1 != positions[dimIdx].size())
267 DUNE_THROW(Dune::RangeError,
"Make sure to specify correct \"Cells\" and \"Positions\" arrays");
269 if (grading[dimIdx].size() + 1 != positions[dimIdx].size())
271 DUNE_THROW(Dune::RangeError,
"Make sure to specify correct \"Grading\" and \"Positions\" arrays");
273 ctype temp = std::numeric_limits<ctype>::lowest();
274 for (
unsigned int posIdx = 0; posIdx < positions[dimIdx].size(); ++posIdx)
276 if (temp > positions[dimIdx][posIdx])
278 DUNE_THROW(Dune::RangeError,
"Make sure to specify a monotone increasing \"Positions\" array");
280 temp = positions[dimIdx][posIdx];
284 const auto globalPositions = computeGlobalPositions_(positions, cells, grading, verbose);
290 ParentType::gridPtr() = std::make_shared<Grid>(globalPositions, periodic, overlap);
295 const auto partitioning = getParamFromGroup<std::array<int, dim>>(modelParamGroup,
"Grid.Partitioning");
296 Dune::YaspFixedSizePartitioner<dim> lb(partitioning);
297 ParentType::gridPtr() = std::make_shared<Grid>(globalPositions, periodic, overlap,
typename Grid::CollectiveCommunicationType(), &lb);
300 postProcessing_(modelParamGroup);
307 void postProcessing_(
const std::string& modelParamGroup)
310 const bool keepPhysicalOverlap = getParamFromGroup<bool>(modelParamGroup,
"Grid.KeepPhysicalOverlap",
true);
311 ParentType::grid().refineOptions(keepPhysicalOverlap);
312 ParentType::maybeRefineGrid(modelParamGroup);
313 ParentType::loadBalance();
317 std::array<std::vector<ctype>, dim>
318 computeGlobalPositions_(
const std::array<std::vector<ctype>, dim>& positions,
319 const std::array<std::vector<int>, dim>& cells,
320 const std::array<std::vector<ctype>, dim>& grading,
321 bool verbose =
false)
323 std::array<std::vector<ctype>, dim> globalPositions;
325 for (
int dimIdx = 0; dimIdx < dim; dimIdx++)
327 for (
int zoneIdx = 0; zoneIdx < cells[dimIdx].size(); ++zoneIdx)
329 ctype lower = positions[dimIdx][zoneIdx];
330 ctype upper = positions[dimIdx][zoneIdx+1];
331 int numCells = cells[dimIdx][zoneIdx];
332 ctype gradingFactor = grading[dimIdx][zoneIdx];
333 ctype length = upper - lower;
335 bool increasingCellSize =
false;
339 std::cout <<
"dim " << dimIdx
340 <<
" lower " << lower
341 <<
" upper " << upper
342 <<
" numCells " << numCells
343 <<
" grading " << gradingFactor;
346 if (gradingFactor > 1.0)
348 increasingCellSize =
true;
353 if (gradingFactor < 0.0)
356 gradingFactor = abs(gradingFactor);
357 if (gradingFactor < 1.0)
359 increasingCellSize =
true;
364 if (gradingFactor > 1.0 - 1e-7 && gradingFactor < 1.0 + 1e-7)
366 height = 1.0 / numCells;
369 std::cout <<
" -> h " << height * length << std::endl;
375 height = (1.0 - gradingFactor) / (1.0 - pow(gradingFactor, numCells));
379 std::cout <<
" -> grading_eff " << gradingFactor
380 <<
" h_min " << height * pow(gradingFactor, 0) * length
381 <<
" h_max " << height * pow(gradingFactor, numCells-1) * length
386 std::vector<ctype> localPositions;
387 localPositions.push_back(0);
388 for (
int i = 0; i < numCells-1; i++)
391 if (!(gradingFactor < 1.0 + 1e-7 && gradingFactor > 1.0 - 1e-7))
393 if (increasingCellSize)
395 hI *= pow(gradingFactor, i);
399 hI *= pow(gradingFactor, numCells-i-1);
402 localPositions.push_back(localPositions[i] + hI);
405 for (
int i = 0; i < localPositions.size(); i++)
407 localPositions[i] *= length;
408 localPositions[i] += lower;
411 for (
unsigned int i = 0; i < localPositions.size(); ++i)
413 globalPositions[dimIdx].push_back(localPositions[i]);
416 globalPositions[dimIdx].push_back(positions[dimIdx].back());
419 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:375
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:391
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:320
The grid manager base interface (public) and methods common to most grid manager specializations (pro...
Definition: gridmanager_base.hh:73
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:81
typename Dune::YaspGrid< dim, Coordinates > Grid
Definition: gridmanager_yasp.hh:62
void init(const std::string &modelParamGroup="")
Make the grid. This is implemented by specializations of this method.
Definition: gridmanager_yasp.hh:68
typename Dune::YaspGrid< dim, Dune::TensorProductCoordinates< ctype, dim > > Grid
Definition: gridmanager_yasp.hh:210
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:248
void init(const std::string &modelParamGroup="")
Make the grid. This is implemented by specializations of this method.
Definition: gridmanager_yasp.hh:216