version 3.11-dev
gridmanager_yasp.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightText: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_IO_GRID_MANAGER_YASP_HH
13#define DUMUX_IO_GRID_MANAGER_YASP_HH
14
15#include <dune/common/math.hh>
16
17#include <dune/grid/yaspgrid.hh>
18#include <dune/grid/io/file/dgfparser/dgfyasp.hh>
19
20#ifndef DUMUX_IO_GRID_MANAGER_BASE_HH
22#endif
23
25
26namespace Dumux {
27
47template<class Coordinates, int dim>
48class GridManager<Dune::YaspGrid<dim, Coordinates>>
49: public GridManagerBase<Dune::YaspGrid<dim, Coordinates>>
50{
52 using GlobalPosition = Dune::FieldVector<ct, dim>;
53public:
56
60 void init(const std::string& modelParamGroup = "")
61 {
62 // First try to create it from a DGF file in GridParameterGroup.File
63 if (hasParamInGroup(modelParamGroup, "Grid.File"))
64 {
65 const auto filename = getParamFromGroup<std::string>(modelParamGroup, "Grid.File");
66 const std::string extension = ParentType::getFileExtension(filename);
67
68 if (extension != "dgf" && extension != "vti")
69 DUNE_THROW(Dune::IOError, "Grid type " << Dune::className<Grid>() << " doesn't support grid files with extension: *."<< extension);
70
71 // VTK file formats for image grids
72 if (extension == "vti")
73 {
74#if DUMUX_HAVE_GRIDFORMAT
75 VTKReader vtkReader(filename);
76 VTKReader::Data cellData, pointData;
77
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();
86
87 if constexpr (std::is_same_v<Dune::EquidistantCoordinates<ct, dim>, Coordinates>)
88 init_(upperRight, cells, modelParamGroup, overlap, periodic);
89 else
90 init_(lowerLeft, upperRight, cells, modelParamGroup, overlap, periodic);
91
92 ParentType::gridData_ = std::make_shared<GridData<Grid>>(
93 ParentType::gridPtr(), gridInfo, std::move(cellData), std::move(pointData)
94 );
95
96 ParentType::enableVtkData_ = true;
97#else
98 DUNE_THROW(Dune::IOError, "Grid type " << Dune::className<Grid>() << " doesn't support grid files with extension: *."<< extension);
99#endif
100 }
101 else
102 {
103 ParentType::makeGridFromDgfFile(
104 getParamFromGroup<std::string>(modelParamGroup, "Grid.File")
105 );
106 }
107
108 postProcessing_(modelParamGroup);
109 return;
110 }
111
112 // Then look for the necessary keys to construct from the input file
113 else if (hasParamInGroup(modelParamGroup, "Grid.UpperRight"))
114 {
115 // get the upper right corner coordinates
116 const auto upperRight = getParamFromGroup<GlobalPosition>(modelParamGroup, "Grid.UpperRight");
117
118 // number of cells in each direction
119 std::array<int, dim> cells; cells.fill(1);
120 cells = getParamFromGroup<std::array<int, dim>>(modelParamGroup, "Grid.Cells", cells);
121
122 // \todo TODO periodic boundaries with yasp (the periodicity concept of yasp grid is currently not supported, use dune-spgrid)
123 // const auto periodic = getParamFromGroup<std::bitset<dim>>(modelParamGroup, "Grid.Periodic", std::bitset<dim>{});
124 const std::bitset<dim> periodic;
125
126 // get the overlap
127 const int overlap = getParamFromGroup<int>(modelParamGroup, "Grid.Overlap", 1);
128
129 if constexpr (std::is_same_v<Dune::EquidistantCoordinates<ct, dim>, Coordinates>)
130 init(upperRight, cells, modelParamGroup, overlap, periodic);
131 else
132 {
133 const auto lowerLeft = getParamFromGroup<GlobalPosition>(modelParamGroup, "Grid.LowerLeft", GlobalPosition(0.0));
134 init(lowerLeft, upperRight, cells, modelParamGroup, overlap, periodic);
135 }
136 }
137
138 // Didn't find a way to construct the grid
139 else
140 {
141 const auto prefix = modelParamGroup.empty() ? modelParamGroup : modelParamGroup + ".";
142 DUNE_THROW(ParameterException, "Please supply one of the parameters "
143 << prefix + "Grid.UpperRight"
144 << ", or a grid file in " << prefix + "Grid.File");
145
146 }
147 }
148
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>{})
158 {
159 init_(upperRight, cells, modelParamGroup, overlap, periodic);
160 postProcessing_(modelParamGroup);
161 }
162
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>{})
173 {
174 init_(lowerLeft, upperRight, cells, modelParamGroup, overlap, periodic);
175 postProcessing_(modelParamGroup);
176 }
177
178private:
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>{})
188 {
189 static_assert(std::is_same_v<Dune::EquidistantCoordinates<ct, dim>, Coordinates>,
190 "Use init function taking lowerLeft as argument when working with EquidistantOffsetCoordinates");
191
192 if (!hasParamInGroup(modelParamGroup, "Grid.Partitioning"))
193 {
194 // construct using default load balancing
195 ParentType::gridPtr() = std::make_unique<Grid>(upperRight, cells, periodic, overlap);
196 }
197 else
198 {
199 // construct using user defined partitioning
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);
203 }
204 }
205
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>{})
216 {
217 static_assert(std::is_same_v<Dune::EquidistantOffsetCoordinates<ct, dim>, Coordinates>,
218 "LowerLeft can only be specified with EquidistantOffsetCoordinates");
219
220 if (!hasParamInGroup(modelParamGroup, "Grid.Partitioning"))
221 {
222 // construct using default load balancing
223 ParentType::gridPtr() = std::make_unique<Grid>(lowerLeft, upperRight, cells, periodic, overlap);
224 }
225 else
226 {
227 // construct using user defined partitioning
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);
231 }
232 }
233
237 void postProcessing_(const std::string& modelParamGroup)
238 {
239 // Check if should refine the grid
240 const bool keepPhysicalOverlap = getParamFromGroup<bool>(modelParamGroup, "Grid.KeepPhysicalOverlap", true);
241 ParentType::grid().refineOptions(keepPhysicalOverlap);
242 ParentType::maybeRefineGrid(modelParamGroup);
243 ParentType::loadBalance();
244 }
245};
246
276template<class ctype, int dim>
277class GridManager<Dune::YaspGrid<dim, Dune::TensorProductCoordinates<ctype, dim> >>
278: public GridManagerBase<Dune::YaspGrid<dim, Dune::TensorProductCoordinates<ctype, dim> > >
279{
280public:
283
287 void init(const std::string& modelParamGroup = "")
288 {
289 // Only construction from the input file is possible
290 // Look for the necessary keys to construct from the input file
291 // The positions
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));
295
296 // the number of cells (has a default)
297 std::array<std::vector<int>, dim> cells;
298 for (int i = 0; i < dim; ++i)
299 {
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]);
302 }
303
304 // grading factor (has a default)
305 std::array<std::vector<ctype>, dim> grading;
306 for (int i = 0; i < dim; ++i)
307 {
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]);
310 }
311
312 // call the generic function
313 init(positions, cells, grading, modelParamGroup);
314 }
315
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 = "")
323 {
324 // Additional parameters (they have a default)
325 const int overlap = getParamFromGroup<int>(modelParamGroup, "Grid.Overlap", 1);
326 const bool verbose = getParamFromGroup<bool>(modelParamGroup, "Grid.Verbosity", false);
327 // \todo TODO periodic boundaries with yasp (the periodicity concept of yasp grid is currently not supported, use dune-spgrid)
328 // const auto periodic = getParamFromGroup<std::bitset<dim>>(modelParamGroup, "Grid.Periodic", std::bitset<dim>{});
329 const std::bitset<dim> periodic;
330
331 // Some sanity checks
332 for (unsigned int dimIdx = 0; dimIdx < dim; ++dimIdx)
333 {
334 if (cells[dimIdx].size() + 1 != positions[dimIdx].size())
335 {
336 DUNE_THROW(Dune::RangeError, "Make sure to specify correct \"Cells\" and \"Positions\" arrays");
337 }
338 if (grading[dimIdx].size() + 1 != positions[dimIdx].size())
339 {
340 DUNE_THROW(Dune::RangeError, "Make sure to specify correct \"Grading\" and \"Positions\" arrays");
341 }
342 ctype temp = std::numeric_limits<ctype>::lowest();
343 for (unsigned int posIdx = 0; posIdx < positions[dimIdx].size(); ++posIdx)
344 {
345 if (temp > positions[dimIdx][posIdx])
346 {
347 DUNE_THROW(Dune::RangeError, "Make sure to specify a monotone increasing \"Positions\" array");
348 }
349 temp = positions[dimIdx][posIdx];
350 }
351 }
352
353 const auto globalPositions = computeGlobalPositions_(positions, cells, grading, verbose);
354
355 // make the grid
356 if (!hasParamInGroup(modelParamGroup, "Grid.Partitioning"))
357 {
358 // construct using default load balancing
359 ParentType::gridPtr() = std::make_shared<Grid>(globalPositions, periodic, overlap);
360 }
361 else
362 {
363 // construct using user defined partitioning
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);
367 }
368
369 postProcessing_(modelParamGroup);
370 }
371
372private:
376 void postProcessing_(const std::string& modelParamGroup)
377 {
378 // Check if should refine the grid
379 const bool keepPhysicalOverlap = getParamFromGroup<bool>(modelParamGroup, "Grid.KeepPhysicalOverlap", true);
380 ParentType::grid().refineOptions(keepPhysicalOverlap);
381 ParentType::maybeRefineGrid(modelParamGroup);
382 ParentType::loadBalance();
383 }
384
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)
391 {
392 std::array<std::vector<ctype>, dim> globalPositions;
393 using std::pow;
394 using Dune::power;
395 for (int dimIdx = 0; dimIdx < dim; dimIdx++)
396 {
397 for (int zoneIdx = 0; zoneIdx < cells[dimIdx].size(); ++zoneIdx)
398 {
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;
404 ctype height = 1.0;
405 bool increasingCellSize = false;
406
407 if (verbose)
408 {
409 std::cout << "dim " << dimIdx
410 << " lower " << lower
411 << " upper " << upper
412 << " numCells " << numCells
413 << " grading " << gradingFactor;
414 }
415
416 if (gradingFactor > 1.0)
417 {
418 increasingCellSize = true;
419 }
420
421 // take absolute values and reverse cell size increment to achieve
422 // reverse behavior for negative values
423 if (gradingFactor < 0.0)
424 {
425 using std::abs;
426 gradingFactor = abs(gradingFactor);
427 if (gradingFactor < 1.0)
428 {
429 increasingCellSize = true;
430 }
431 }
432
433 // if the grading factor is exactly 1.0 do equal spacing
434 if (gradingFactor > 1.0 - 1e-7 && gradingFactor < 1.0 + 1e-7)
435 {
436 height = 1.0 / numCells;
437 if (verbose)
438 {
439 std::cout << " -> h " << height * length << std::endl;
440 }
441 }
442 // if grading factor is not 1.0, do power law spacing
443 else
444 {
445 height = (1.0 - gradingFactor) / (1.0 - power(gradingFactor, numCells));
446
447 if (verbose)
448 {
449 std::cout << " -> grading_eff " << gradingFactor
450 << " h_min " << height * power(gradingFactor, 0) * length
451 << " h_max " << height * power(gradingFactor, numCells-1) * length
452 << std::endl;
453 }
454 }
455
456 std::vector<ctype> localPositions;
457 localPositions.push_back(0);
458 for (int i = 0; i < numCells-1; i++)
459 {
460 ctype hI = height;
461 if (!(gradingFactor < 1.0 + 1e-7 && gradingFactor > 1.0 - 1e-7))
462 {
463 if (increasingCellSize)
464 {
465 hI *= power(gradingFactor, i);
466 }
467 else
468 {
469 hI *= power(gradingFactor, numCells-i-1);
470 }
471 }
472 localPositions.push_back(localPositions[i] + hI);
473 }
474
475 for (int i = 0; i < localPositions.size(); i++)
476 {
477 localPositions[i] *= length;
478 localPositions[i] += lower;
479 }
480
481 for (unsigned int i = 0; i < localPositions.size(); ++i)
482 {
483 globalPositions[dimIdx].push_back(localPositions[i]);
484 }
485 }
486 globalPositions[dimIdx].push_back(positions[dimIdx].back());
487 }
488
489 return globalPositions;
490 }
491};
492
493namespace Grid::Capabilities {
494
495// To the best of our knowledge YaspGrid is view thread-safe
496// This specialization can be removed after we depend on Dune release 2.9 and this is guaranteed by YaspGrid itself
497template<class Coordinates, int dim>
498struct MultithreadingSupported<Dune::YaspGrid<dim, Coordinates>>
499{
500 template<class GV>
501 static bool eval(const GV& gv) // default is independent of the grid view
502 { return true; }
503};
504
505} // end namespace Grid::Capabilities
506
507} // end namespace Dumux
508
509#endif
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 &paramGroup, const std::string &param)
Check whether a key exists in the parameter tree with a model group prefix.
Definition: parameters.hh:165
Definition: adapt.hh:17
Definition: common/pdesolver.hh:24
static bool eval(const GV &gv)
Definition: gridmanager_yasp.hh:501
Definition: gridcapabilities.hh:27