24#ifndef DUMUX_IO_STRUCTURED_LATTICE_GRID_CREATOR_HH
25#define DUMUX_IO_STRUCTURED_LATTICE_GRID_CREATOR_HH
34#include <dune/common/concept.hh>
35#include <dune/common/exceptions.hh>
36#include <dune/common/version.hh>
37#include <dune/grid/common/gridfactory.hh>
38#include <dune/grid/yaspgrid.hh>
39#include <dune/geometry/referenceelements.hh>
42#include <dune/foamgrid/foamgrid.hh>
43#include <dune/foamgrid/dgffoam.hh>
57template<
class GlobalPosition>
58struct LowDimElementSelector
61 auto require(F&& f) ->
decltype(
62 bool(f(std::declval<const GlobalPosition&>(), std::declval<const GlobalPosition&>()))
72class StructuredLatticeGridCreator
74 using GridType = Dune::FoamGrid<1, dimWorld>;
75 using Element =
typename GridType::template Codim<0>::Entity;
76 using GlobalPosition =
typename Element::Geometry::GlobalCoordinate;
77 using CoordScalar =
typename GridType::ctype;
79 using HostGridView =
typename HostGrid::LeafGridView;
80 using ReferenceElements =
typename Dune::ReferenceElements<CoordScalar, dimWorld>;
82 using VertexIndexPair = std::pair<unsigned int, unsigned int>;
86 VertexIndexPair localVertexIndices;
87 unsigned int directionNumber;
91 using Grid = GridType;
93 void init(
const std::string& paramGroup =
"")
95 auto useAllElements = [](
const GlobalPosition& a,
const GlobalPosition& b){
return true; };
96 init(useAllElements, paramGroup);
99 template<
class LowDimElementSelector,
100 typename std::enable_if_t<Dune::models<Concept::LowDimElementSelector<GlobalPosition>, LowDimElementSelector>(),
int> = 0>
101 void init(
const LowDimElementSelector& lowDimElementSelector,
102 const std::string& paramGroup =
"")
104 paramGroup_ = paramGroup;
105 removeThroatsOnBoundary_ = getParamFromGroup<std::vector<std::size_t>>(paramGroup,
"Grid.RemoveThroatsOnBoundary",
106 std::vector<std::size_t>());
108 setElementSelector_(lowDimElementSelector);
109 initRandomNumberGenerator_();
112 const auto hostGridInputData = getHostGridInputData_();
114 using HastGridManager = GridManager<HostGrid>;
115 HastGridManager hostGridManager;
116 hostGridManager.init(hostGridInputData.positions, hostGridInputData.cells, hostGridInputData.grading, paramGroup_);
117 hostGridView_ = std::make_unique<HostGridView>(hostGridManager.grid().leafGridView());
119 hostGridLowerLeft_ = hostGridInputData.lowerLeft;
120 hostGridUpperRight_ = hostGridInputData.upperRight;
122 convertHostGridToLowDimGrid_();
130 if (Dune::MPIHelper::getCommunication().size() > 1)
131 gridPtr_->loadBalance();
138 {
return *gridPtr(); }
143 std::shared_ptr<Grid>& gridPtr()
148 template<
class LowDimElementSelector>
149 void setElementSelector_(
const LowDimElementSelector& lowDimElementSelector)
151 if (!removeThroatsOnBoundary_.empty())
153 auto finalSelector = [&](
const GlobalPosition& a,
const GlobalPosition& b)
155 static const auto lambdaToRemoveThroatsOnBoundary = getLambdaToRemoveThroatsOnBoundary_();
157 return min(lambdaToRemoveThroatsOnBoundary(a, b), lowDimElementSelector(a, b));
159 considerLowDimElement_ = finalSelector;
162 considerLowDimElement_ = lowDimElementSelector;
165 void initRandomNumberGenerator_()
167 directionProbability_ = getDirectionProbability_();
171 const auto seed = getParamFromGroup<std::size_t>(paramGroup_,
"Grid.DeletionRandomNumberSeed");
172 generator_.seed(seed);
176 std::random_device rd;
177 generator_.seed(rd());
181 void convertHostGridToLowDimGrid_()
185 for (
const auto& element : elements(*hostGridView_))
187 const auto& geometry =
element.geometry();
188 const auto& refElement = ReferenceElements::general(geometry.type());
189 treatEdges_(element, refElement, geometry);
190 treatDiagonals_(element, refElement, geometry);
192 if (elementSet_.empty())
193 DUNE_THROW(Dune::GridError,
"Trying to create pore network with zero elements!");
196 Dune::GridFactory<Grid> factory;
197 for (
auto&& vertex : vertexSet_)
198 factory.insertVertex(vertex);
199 for (
auto&& element : elementSet_)
200#if DUNE_VERSION_NEWER(DUNE_COMMON,2,7)
201 factory.insertElement(Dune::GeometryTypes::cube(1), {
element.first,
element.second});
203 factory.insertElement(Dune::GeometryType(1), {
element.first,
element.second});
206 gridPtr_ = std::shared_ptr<Grid>(factory.createGrid());
209 void resizeContainers_()
211 vertexInserted_.resize(hostGridView_->size(dimWorld),
false);
212 hostVertexIdxToVertexIdx_.resize(hostGridView_->size(dimWorld));
213 edgeInserted_.resize(hostGridView_->size(dimWorld-1),
false);
214 faceInserted_.resize(hostGridView_->size(dimWorld-2),
false);
217 template<
class HostGr
idElement>
218 bool maybeInsertElementAndVertices_(
const HostGridElement& hostGridElement,
219 const typename HostGridElement::Geometry& hostGridElementGeometry,
220 std::size_t localVertexIdx0,
221 std::size_t localVertexIdx1,
222 double directionProbability)
224 if (neglectElement_(hostGridElementGeometry, localVertexIdx0, localVertexIdx1, directionProbability))
228 insertElementAndVertices_(hostGridElement, hostGridElementGeometry, localVertexIdx0, localVertexIdx1, directionProbability);
233 template<
class HostGr
idElement>
234 void insertElementAndVertices_(
const HostGridElement& hostGridElement,
235 const typename HostGridElement::Geometry& hostGridElementGeometry,
236 std::size_t localVertexIdx0,
237 std::size_t localVertexIdx1,
238 double directionProbability)
241 const auto vIdxGlobal0 = hostGridView_->indexSet().subIndex(hostGridElement, localVertexIdx0, dimWorld);
242 const auto vIdxGlobal1 = hostGridView_->indexSet().subIndex(hostGridElement, localVertexIdx1, dimWorld);
244 auto getGobalVertexIdx = [&](
auto globalIdx,
auto localIdx)
246 if (!vertexInserted_[globalIdx])
248 vertexInserted_[globalIdx] =
true;
249 hostVertexIdxToVertexIdx_[globalIdx] = vertexSet_.size();
250 vertexSet_.push_back(hostGridElementGeometry.corner(localIdx));
253 return hostVertexIdxToVertexIdx_[globalIdx];
256 const auto newVertexIdx0 = getGobalVertexIdx(vIdxGlobal0, localVertexIdx0);
257 const auto newVertexIdx1 = getGobalVertexIdx(vIdxGlobal1, localVertexIdx1);
258 elementSet_.emplace_back(newVertexIdx0, newVertexIdx1);
261 auto getDirectionProbability_()
const
263 std::array<double, numDirections_()> directionProbability;
264 directionProbability.fill(0.0);
267 if (getParamFromGroup<bool>(paramGroup_,
"Grid.RegularLattice",
false))
269 directionProbability.fill(1.0);
270 for (
int i = 0; i < dimWorld; ++i)
271 directionProbability[i] = 0.0;
273 return directionProbability;
281 directionProbability = getParamFromGroup<decltype(directionProbability)>(paramGroup_,
"Grid.DeletionProbability");
285 throwDirectionError_();
289 return directionProbability;
292 void throwDirectionError_()
const
295 Dune::FieldVector<std::string, numDirections_()> directions;
299 directions[0] =
"1: (1, 0)\n";
300 directions[1] =
"2: (0, 1)\n";
302 directions[2] =
"3: (1, 1)\n";
303 directions[3] =
"4: (1, -1)\n";
308 directions[0] =
" 1: (1, 0, 0)\n";
309 directions[1] =
"2: (0, 1, 0)\n";
310 directions[2] =
"3: (0, 0, 1)\n";
312 directions[3] =
"4: (1, 1, 0)\n";
313 directions[4] =
"5: (1, -1, 0)\n";
314 directions[5] =
"6: (1, 0, 1)\n";
315 directions[6] =
"7: (1, 0, -1)\n";
316 directions[7] =
"8: (0, 1, 1)\n";
317 directions[8] =
"9: (0, 1, -1)\n";
319 directions[9] =
"10: (1, 1, 1)\n";
320 directions[10] =
"11: (1, 1, -1)\n";
321 directions[11] =
"12: (-1, 1, 1)\n";
322 directions[12] =
"13: (-1, -1, 1)\n";
324 DUNE_THROW(ParameterException,
"You must specifiy probabilities for all directions (" << numDirections_() <<
") \n" << directions <<
"\nExample (3D):\n\n"
325 <<
"DeletionProbability = 0.5 0.5 0 0 0 0 0 0 0 0 0 0 0 \n\n"
326 <<
"deletes approximately 50% of all throats in x and y direction, while no deletion in any other direction takes place.\n" );
329 static constexpr std::size_t numDirections_()
330 {
return (dimWorld < 3) ? 4 : 13; }
332 template<
class Geometry>
333 bool neglectElement_(Geometry& hostGridElementGeometry,
334 std::size_t localVertexIdx0,
335 std::size_t localVertexIdx1,
336 double directionProbability)
338 if (randomNumer_() < directionProbability)
343 if (!considerLowDimElement_(hostGridElementGeometry.corner(localVertexIdx0), hostGridElementGeometry.corner(localVertexIdx1)))
350 {
return uniformdist_(generator_); }
352 auto getHostGridInputData_()
const
356 std::array<std::vector<int>, dimWorld> cells;
357 std::array<std::vector<CoordScalar>, dimWorld> positions;
358 std::array<std::vector<CoordScalar>, dimWorld> grading;
359 GlobalPosition lowerLeft;
360 GlobalPosition upperRight;
364 auto& cells = inputData.cells;
365 auto& positions = inputData.positions;
366 auto& grading = inputData.grading;
367 auto& lowerLeft = inputData.lowerLeft;
368 auto& upperRight = inputData.upperRight;
371 for (
int i = 0; i < dimWorld; ++i)
372 positions[i] =
getParamFromGroup<std::vector<CoordScalar>>(paramGroup_,
"Grid.Positions" + std::to_string(i), std::vector<CoordScalar>{});
373 if (std::none_of(positions.begin(), positions.end(), [&](
auto& v){ return v.empty(); }))
375 for (
int i = 0; i < dimWorld; ++i)
377 cells[i].resize(positions[i].size()-1, 1.0);
378 grading[i].resize(positions[i].size()-1, 1.0);
383 const auto lowerLeft = getParamFromGroup<GlobalPosition>(paramGroup_,
"Grid.LowerLeft", GlobalPosition(0.0));
384 const auto upperRight = getParamFromGroup<GlobalPosition>(paramGroup_,
"Grid.UpperRight");
385 const auto numPores = getParamFromGroup<std::vector<unsigned int>>(paramGroup_,
"Grid.NumPores");
386 if (numPores.size() != dimWorld)
387 DUNE_THROW(ParameterException,
"Grid.NumPores has to be a space-separated list of " << dimWorld <<
" integers!");
389 for (
int i = 0; i < dimWorld; ++i)
391 positions[i].push_back(lowerLeft[i]);
392 positions[i].push_back(upperRight[i]);
393 cells[i].push_back(numPores[i] - 1);
394 grading[i].resize(positions[i].size()-1, 1.0);
395 grading[i] = getParamFromGroup<std::vector<CoordScalar>>(paramGroup_,
"Grid.Grading" + std::to_string(i), grading[i]);
402 GlobalPosition result;
403 for (
int i = 0; i < dimWorld; ++i)
404 result[i] = positions[i][0];
411 GlobalPosition result;
412 for (
int i = 0; i < dimWorld; ++i)
413 result[i] = positions[i].back();
420 template<
class HostGr
idElement,
class ReferenceElement,
class Geometry>
421 void treatEdges_(
const HostGridElement& element,
const ReferenceElement& refElement,
const Geometry& geometry)
424 for (
unsigned int edgeIdx = 0; edgeIdx <
element.subEntities(dimWorld-1); ++edgeIdx)
426 const auto vIdxLocal0 = refElement.subEntity(edgeIdx, dimWorld-1, 0, dimWorld);
427 const auto vIdxLocal1 = refElement.subEntity(edgeIdx, dimWorld-1, 1, dimWorld);
428 const auto edgeIdxGlobal = hostGridView_->indexSet().subIndex(element, edgeIdx, dimWorld-1);
430 if (edgeInserted_[edgeIdxGlobal])
433 edgeInserted_[edgeIdxGlobal] =
true;
435 std::size_t directionNumber = 0;
448 else if(edgeIdx == 4 || edgeIdx == 5 || edgeIdx == 8 || edgeIdx == 9)
450 else if(edgeIdx == 6 || edgeIdx == 7 || edgeIdx == 10 || edgeIdx == 11)
453 maybeInsertElementAndVertices_(element, geometry, vIdxLocal0, vIdxLocal1, directionProbability_[directionNumber]);
457 template<
class HostGr
idElement,
class ReferenceElement,
class Geometry>
458 void treatDiagonals_(
const HostGridElement& element,
const ReferenceElement& refElement,
const Geometry& geometry)
460 if constexpr (dimWorld < 3)
461 treatDiagonalConnections2D_(element, geometry);
463 treatDiagonalConnections3D_(element, refElement, geometry);
466 template<
class HostGr
idElement,
class Geometry>
467 void treatDiagonalConnections2D_(
const HostGridElement& element,
468 const Geometry& geometry)
470 static constexpr std::array<Diagonal, 2> diagonals{ Diagonal{std::make_pair(0, 3), 2},
471 Diagonal{std::make_pair(1, 2), 3} };
473 treatIntersectingDiagonals_(element, geometry, diagonals);
476 template<
class HostGr
idElement,
class ReferenceElement,
class Geometry>
477 void treatDiagonalConnections3D_(
const HostGridElement& element,
478 const ReferenceElement& refElement,
479 const Geometry& geometry)
482 for (
auto faceIdx = 0; faceIdx <
element.subEntities(dimWorld-2); ++faceIdx)
484 const auto faceIdxGlobal = hostGridView_->indexSet().subIndex(element, faceIdx, dimWorld-2);
486 if (faceInserted_[faceIdxGlobal])
489 faceInserted_[faceIdxGlobal] =
true;
492 std::array<unsigned int, 4> vIdxLocal;
493 for (
int i = 0; i < 4; i++)
494 vIdxLocal[i] = refElement.subEntity(faceIdx, dimWorld-2, i, dimWorld);
496 const auto directionNumbers = [&]()
499 return std::make_pair<unsigned int, unsigned int>(8,7);
500 else if (faceIdx < 4)
501 return std::make_pair<unsigned int, unsigned int>(6,5);
503 return std::make_pair<unsigned int, unsigned int>(4,3);
506 const std::array<Diagonal, 2> diagonals{ Diagonal{std::make_pair(vIdxLocal[1], vIdxLocal[2]), directionNumbers.first},
507 Diagonal{std::make_pair(vIdxLocal[0], vIdxLocal[3]), directionNumbers.second} };
509 treatIntersectingDiagonals_(element, geometry, diagonals);
512 static constexpr std::array<Diagonal, 4> diagonals{ Diagonal{std::make_pair(0, 7), 9},
513 Diagonal{std::make_pair(3, 4), 10},
514 Diagonal{std::make_pair(1, 6), 11},
515 Diagonal{std::make_pair(2, 5), 12}, };
517 treatIntersectingDiagonals_(element, geometry, diagonals);
520 template<
class HostGr
idElement,
class Geometry,
class Diagonals>
521 void treatIntersectingDiagonals_(
const HostGridElement& element,
522 const Geometry& geometry,
523 const Diagonals& diagonals)
525 static const bool allowIntersectingDiagonals = getParamFromGroup<bool>(paramGroup_,
"Grid.AllowIntersectingDiagonals",
true);
527 auto treat = [&](
const Diagonal& diagonal)
529 return maybeInsertElementAndVertices_(element, geometry,
530 diagonal.localVertexIndices.first, diagonal.localVertexIndices.second,
531 directionProbability_[diagonal.directionNumber]);
534 if (allowIntersectingDiagonals)
537 for (
const auto& diagonal : diagonals)
542 auto order = createOrderedList_(diagonals.size());
543 std::shuffle(order.begin(), order.end(), generator_);
546 const auto& diagonal = diagonals[i];
547 const bool inserted = treat(diagonal);
554 std::vector<std::size_t> createOrderedList_(
const std::size_t size)
const
556 std::vector<std::size_t> result(size);
557 std::iota(result.begin(), result.end(), 0);
561 auto getLambdaToRemoveThroatsOnBoundary_()
const
563 auto negletElementsOnBoundary = [&](
const GlobalPosition& a,
const GlobalPosition& b)
565 const auto center = 0.5 * (a + b);
566 const auto eps = (a-b).two_norm() * 1e-5;
568 bool neglectElement =
false;
569 for (
auto i : removeThroatsOnBoundary_)
573 case 0: neglectElement = center[0] < hostGridLowerLeft_[0] + eps;
break;
574 case 1: neglectElement = center[0] > hostGridUpperRight_[0] - eps;
break;
575 case 2:
if constexpr (dimWorld > 1)
577 neglectElement = center[1] < hostGridLowerLeft_[1] + eps;
580 case 3:
if constexpr (dimWorld > 1)
582 neglectElement = center[1] > hostGridUpperRight_[1] - eps;
585 case 4:
if constexpr (dimWorld > 2)
587 neglectElement = center[2] < hostGridLowerLeft_[2] + eps;
590 case 5:
if constexpr (dimWorld > 2)
592 neglectElement = center[2] > hostGridUpperRight_[2] - eps;
604 return negletElementsOnBoundary;
607 std::string paramGroup_;
608 GlobalPosition hostGridLowerLeft_;
609 GlobalPosition hostGridUpperRight_;
610 std::vector<std::size_t> removeThroatsOnBoundary_;
611 std::unique_ptr<const HostGridView> hostGridView_;
612 std::function<bool(
const GlobalPosition&,
const GlobalPosition&)> considerLowDimElement_;
614 std::vector<GlobalPosition> vertexSet_;
615 std::vector<VertexIndexPair> elementSet_;
617 std::vector<bool> vertexInserted_;
618 std::vector<std::size_t> hostVertexIdxToVertexIdx_;
619 std::vector<std::size_t> edgeInserted_;
620 std::vector<std::size_t> faceInserted_;
622 mutable std::mt19937 generator_;
623 std::uniform_real_distribution<> uniformdist_{0, 1};
624 std::array<double, numDirections_()> directionProbability_;
626 std::shared_ptr<Grid> gridPtr_;
Some exceptions thrown in DuMux
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
Grid manager specialization for YaspGrid.
T getParamFromGroup(Args &&... args)
A free function to get a parameter from the parameter tree singleton with a model group.
Definition: parameters.hh:161
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
Definition: discretization/porenetwork/fvelementgeometry.hh:34
Exception thrown if a run-time parameter is not specified correctly.
Definition: exceptions.hh:60
Definition: consistentlyorientedgrid.hh:32