24#ifndef DUMUX_CAKE_GRID_MANAGER_HH
25#define DUMUX_CAKE_GRID_MANAGER_HH
32#include <dune/common/dynvector.hh>
33#include <dune/common/float_cmp.hh>
34#include <dune/common/math.hh>
35#include <dune/grid/common/gridfactory.hh>
49 using Scalar =
typename Grid::ctype;
51 using GridFactory = Dune::GridFactory<Grid>;
52 using GridPointer = std::shared_ptr<Grid>;
54 enum { dim = Grid::dimension,
55 dimWorld = Grid::dimensionworld };
61 void init(
const std::string& modelParamGroup =
"")
63 static_assert(dim == 2 || dim == 3,
"The CakeGridManager is only implemented for 2D and 3D.");
65 const bool verbose = getParamFromGroup<bool>(modelParamGroup,
"Grid.Verbosity",
false);
67 std::array<std::vector<Scalar>, dim> polarCoordinates;
69 Dune::FieldVector<int, dim> indices(-1);
70 createVectors(polarCoordinates, indices, modelParamGroup, verbose);
96 static void createVectors(std::array<std::vector<Scalar>, dim> &polarCoordinates,
97 Dune::FieldVector<int, dim> &indices,
98 const std::string& modelParamGroup,
102 std::array<std::vector<Scalar>, dim> positions;
104 for (
int i = 0; i < dim; ++i)
106 const bool hasRadial =
hasParamInGroup(modelParamGroup,
"Grid.Radial" + std::to_string(i));
107 const bool hasAngular =
hasParamInGroup(modelParamGroup,
"Grid.Angular" + std::to_string(i));
108 const bool hasAxial = (dim == 3) &&
hasParamInGroup(modelParamGroup,
"Grid.Axial" + std::to_string(i));
109 if (
static_cast<int>(hasRadial) +
static_cast<int>(hasAngular) +
static_cast<int>(hasAxial) != 1)
110 DUNE_THROW(Dune::RangeError,
"Multiple or no position vectors (radial, angular, axial) specified in coord direction: " << i << std::endl);
114 positions[i] = getParamFromGroup<std::vector<Scalar>>(modelParamGroup,
"Grid.Radial" + std::to_string(i));
119 positions[i] = getParamFromGroup<std::vector<Scalar>>(modelParamGroup,
"Grid.Angular" + std::to_string(i));
124 positions[i] = getParamFromGroup<std::vector<Scalar>>(modelParamGroup,
"Grid.Axial" + std::to_string(i));
128 if (!std::is_sorted(positions[i].begin(), positions[i].end()))
129 DUNE_THROW(Dune::GridError,
"Make sure to specify a monotone increasing \"Positions\" array");
131 if(positions[i].size() < 2)
132 DUNE_THROW(Dune::GridError,
"Make sure to specify position arrays with at least two entries (min and max value).");
136 for (
int i = 0; i < dim; ++i)
138 DUNE_THROW(Dune::RangeError,
"Please specify Positions Angular and Radial and Axial correctly and unambiguously!" << std::endl);
141 std::array<std::vector<int>, dim> cells;
142 for (
int i = 0; i < dim; ++i)
144 cells[i].resize(positions[i].size()-1, 1.0);
145 cells[i] = getParamFromGroup<std::vector<int>>(modelParamGroup,
"Grid.Cells" + std::to_string(i), cells[i]);
146 if (cells[i].size() + 1 != positions[i].size())
147 DUNE_THROW(Dune::RangeError,
"Make sure to specify the correct length of \"Cells\" and \"Positions\" arrays");
151 std::array<std::vector<Scalar>, dim> grading;
152 for (
int i = 0; i < dim; ++i)
154 grading[i].resize(positions[i].size()-1, 1.0);
155 grading[i] = getParamFromGroup<std::vector<Scalar>>(modelParamGroup,
"Grid.Grading" + std::to_string(i), grading[i]);
156 if (grading[i].size() + 1 != positions[i].size())
157 DUNE_THROW(Dune::RangeError,
"Make sure to specify the correct length of \"Grading\" and \"Positions\" arrays");
161 std::array<std::vector<Scalar>, dim> globalPositions;
164 for (
int dimIdx = 0; dimIdx < dim; dimIdx++)
167 std::size_t numGlobalPositions = 1;
168 for (
int zoneIdx = 0; zoneIdx < cells[dimIdx].size(); ++zoneIdx)
169 numGlobalPositions += cells[dimIdx][zoneIdx];
171 globalPositions[dimIdx].resize(numGlobalPositions);
172 std::size_t posIdx = 0;
173 for (
int zoneIdx = 0; zoneIdx < cells[dimIdx].size(); ++zoneIdx)
175 const Scalar lower = positions[dimIdx][zoneIdx];
176 const Scalar upper = positions[dimIdx][zoneIdx+1];
177 const int numCells = cells[dimIdx][zoneIdx];
178 Scalar gradingFactor = grading[dimIdx][zoneIdx];
179 const Scalar length = upper - lower;
181 bool increasingCellSize =
false;
185 std::cout <<
"dim " << dimIdx
186 <<
" lower " << lower
187 <<
" upper " << upper
188 <<
" numCells " << numCells
189 <<
" grading " << gradingFactor;
192 if (gradingFactor > 1.0)
193 increasingCellSize =
true;
197 if (gradingFactor < 0.0)
200 gradingFactor = abs(gradingFactor);
202 if (gradingFactor < 1.0)
203 increasingCellSize =
true;
206 const bool useGrading = Dune::FloatCmp::eq(gradingFactor, 1.0) ? false :
true;
211 height = 1.0 / numCells;
213 std::cout <<
" -> h " << height * length << std::endl;
218 height = (1.0 - gradingFactor) / (1.0 - power(gradingFactor, numCells));
222 std::cout <<
" -> grading_eff " << gradingFactor
223 <<
" h_min " << height * power(gradingFactor, 0) * length
224 <<
" h_max " << height * power(gradingFactor, numCells-1) * length
230 Dune::DynamicVector<Scalar> localPositions(numCells, 0.0);
231 for (
int i = 1; i < numCells; i++)
236 if (increasingCellSize)
237 hI *= power(gradingFactor, i-1);
240 hI *= power(gradingFactor, numCells-i);
242 localPositions[i] = localPositions[i-1] + hI;
245 localPositions *= length;
246 localPositions += lower;
248 for (
int i = 0; i < numCells; ++i)
249 globalPositions[dimIdx][posIdx++] = localPositions[i];
252 globalPositions[dimIdx][posIdx] = positions[dimIdx].back();
255 polarCoordinates[0] = globalPositions[indices[0]];
256 polarCoordinates[1] = globalPositions[indices[1]];
258 polarCoordinates[2] = globalPositions[dim - indices[0] - indices[1]];
261 std::transform(polarCoordinates[1].begin(), polarCoordinates[1].end(),
262 polarCoordinates[1].begin(),
263 [](Scalar s){
return s*M_PI/180; });
274 std::unique_ptr<Grid>
createCakeGrid(std::array<std::vector<Scalar>, dim> &polarCoordinates,
275 Dune::FieldVector<int, dim> &indices,
276 const std::string& modelParamGroup,
277 bool verbose =
false)
279 GridFactory gridFactory;
282 if (gridFactory.comm().rank() != 0)
283 return std::unique_ptr<Grid>(gridFactory.createGrid());
285 const auto& dR = polarCoordinates[0];
286 const auto& dA = polarCoordinates[1];
292 const int dAMax = Dune::FloatCmp::eq(dA[dA.size()-1], 2*M_PI) ? dA.size() - 2 : dA.size() - 1;
293 constexpr auto type = Dune::GeometryTypes::cube(dim);
294 const bool hasHole = dR[0] >= 1.0e-8*dR.back();
297 if constexpr (dim == 3)
299 constexpr auto prismType = Dune::GeometryTypes::prism;
300 std::vector<Scalar> dZ = polarCoordinates[2];
301 for (
int j = 0; j <= dAMax; ++j)
303 for (
int l = 0; l <= dZ.size() - 1; ++l)
305 for (
int i = hasHole ? 0 : 1; i <= dR.size()- 1; ++i)
310 Dune::FieldVector <double, dim> v(0.0);
311 v[indices[2]] = dZ[l];
312 v[indices[0]] = cos(dA[j])*dR[i];
313 v[indices[1]] = sin(dA[j])*dR[i];
316 gridFactory.insertVertex(v);
325 for (
int l = 0; l <= dZ.size() - 1; ++l)
327 Dune::FieldVector <double, dim> v(0.0);
328 v[indices[2]] = dZ[l];
334 gridFactory.insertVertex(v);
339 std::cout <<
"Filled node vector" << std::endl;
344 unsigned int rSize = hasHole ? dR.size() : dR.size()-1;
345 unsigned int zSize = dZ.size();
346 for (
int j = 0; j < dA.size() - 1; ++j)
348 for (
int l = 0; l < dZ.size() - 1; ++l)
352 for (
int i = 0; i < dR.size() - 1; ++i)
356 std::vector<unsigned int> vid({rSize*zSize*(dAMax+1) + l, z, z+rSize*zSize,
357 rSize*zSize*(dAMax+1) + l+1, z+rSize, z+rSize*zSize+rSize});
362 gridFactory.insertElement(prismType, vid);
366 std::vector<unsigned int> vid({z, z+1,
367 z+rSize*zSize, z+rSize*zSize+1,
369 z+rSize*zSize+rSize, z+rSize*zSize+rSize+1});
374 gridFactory.insertElement(type, vid);
384 for (
int i = 0; i < dR.size() - 1; ++i)
388 std::vector<unsigned int> vid({rSize*zSize*(dAMax+1) + l, z, t,
389 rSize*zSize*(dAMax+1) + l+1, z+rSize, t+rSize});
394 gridFactory.insertElement(prismType, vid);
398 std::vector<unsigned int> vid({z, z+1,
401 t+rSize, t+rSize+1});
406 gridFactory.insertElement(type, vid);
415 std::cout <<
"assign nodes 360° ends..." << std::endl;
425 constexpr auto triangleType = Dune::GeometryTypes::simplex(dim);
426 for (
int j = 0; j <= dAMax; ++j)
428 for (
int i = hasHole ? 0 : 1; i <= dR.size()- 1; ++i)
431 Dune::FieldVector <double, dim> v(0.0);
433 v[indices[0]] = cos(dA[j])*dR[i];
434 v[indices[1]] = sin(dA[j])*dR[i];
437 gridFactory.insertVertex(v);
445 Dune::FieldVector <double, dim> v(0.0);
451 gridFactory.insertVertex(v);
454 std::cout <<
"Filled node vector" << std::endl;
459 unsigned int rSize = hasHole ? dR.size() : dR.size()-1;
460 for (
int j = 0; j < dA.size() - 1; ++j)
464 for (
int i = 0; i < dR.size() - 1; ++i)
468 std::vector<unsigned int> vid({rSize*(dAMax+1), z, z+rSize});
473 gridFactory.insertElement(triangleType, vid);
477 std::vector<unsigned int> vid({z, z+1, z+rSize, z+rSize+1});
482 gridFactory.insertElement(type, vid);
491 for (
int i = 0; i < dR.size() - 1; ++i)
495 std::vector<unsigned int> vid({rSize*(dAMax+1), z, t});
500 gridFactory.insertElement(triangleType, vid);
504 std::vector<unsigned int> vid({z, z+1, t, t+1});
509 gridFactory.insertElement(type, vid);
518 std::cout <<
"assign nodes 360 ends..." << std::endl;
524 return std::unique_ptr<Grid>(gridFactory.createGrid());
546 { std::cout <<
"Coordinates of: " << v << std::endl; }
550 std::cout <<
"element vertex indices: ";
551 for (
int k = 0; k < vid.size(); ++k)
552 std::cout << vid[k] <<
" ";
553 std::cout << std::endl;
565 GridPointer cakeGrid_;
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
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:177
Adaption of the non-isothermal two-phase two-component flow model to problems with CO2.
Definition: adapt.hh:29
Provides a grid manager with a method for creating creating vectors with polar Coordinates and one fo...
Definition: cakegridmanager.hh:48
void init(const std::string &modelParamGroup="")
Make the grid.
Definition: cakegridmanager.hh:61
void loadBalance()
Distributes the grid on all processes of a parallel computation.
Definition: cakegridmanager.hh:539
GridPointer & gridPtr()
Returns a reference to the shared pointer to the grid.
Definition: cakegridmanager.hh:559
static void printCoordinate(const Dune::FieldVector< double, dim > &v)
Definition: cakegridmanager.hh:545
static void printIndices(const std::vector< unsigned int > &vid)
Definition: cakegridmanager.hh:548
Grid & grid()
Returns a reference to the grid.
Definition: cakegridmanager.hh:530
static void createVectors(std::array< std::vector< Scalar >, dim > &polarCoordinates, Dune::FieldVector< int, dim > &indices, const std::string &modelParamGroup, bool verbose=false)
Create vectors containing polar coordinates of all points.
Definition: cakegridmanager.hh:96
std::unique_ptr< Grid > createCakeGrid(std::array< std::vector< Scalar >, dim > &polarCoordinates, Dune::FieldVector< int, dim > &indices, const std::string &modelParamGroup, bool verbose=false)
Creates Cartesian grid from polar coordinates.
Definition: cakegridmanager.hh:274