24#ifndef DUMUX_DISCRETIZATION_WALL_DISTANCE_HH
25#define DUMUX_DISCRETIZATION_WALL_DISTANCE_HH
29#include <dune/common/parallel/mpihelper.hh>
30#include <dune/common/shared_ptr.hh>
31#include <dune/common/reservedvector.hh>
32#include <dune/grid/common/partitionset.hh>
47template<
class Gr
idGeometry,
template<
class>
class DistanceField = AABBDistanceField>
50 using GridView =
typename GridGeometry::GridView;
52 using SubControlVolumeFace =
typename GridGeometry::SubControlVolumeFace;
53 using FVElementGeometry =
typename GridGeometry::LocalView;
54 using Element =
typename GridGeometry::GridView::template Codim<0>::Entity;
55 using Scalar =
typename GridView::Grid::ctype;
56 using GlobalPosition =
typename SubControlVolumeFace::GlobalPosition;
58 static constexpr int dim = GridView::dimension;
59 static constexpr int dimWorld = GridView::dimensionworld;
60 static_assert(dim == dimWorld,
"Wall distances cannot be computed for embedded surface or network domains.");
62 using CornerStorage = Dune::ReservedVector<GlobalPosition, (1<<(GridView::dimension-1))>;
69 SimpleGeometry() =
default;
70 SimpleGeometry(CornerStorage&& corners)
71 : corners_(std::move(corners))
74 for (
int i = 0; i < corners_.size(); ++i)
75 center_ += corners_[i];
76 center_ /= corners_.size();
79 using GlobalCoordinate = GlobalPosition;
80 using ctype =
typename GlobalCoordinate::value_type;
81 static constexpr int coorddimension = GridView::dimensionworld;
82 static constexpr int mydimension = GridView::dimension-1;
84 std::size_t corners()
const
85 {
return corners_.size(); }
87 const auto& corner(
int i)
const
88 {
return corners_[i]; }
94 CornerStorage corners_;
95 GlobalCoordinate center_;
101 GridIndexType scvfIdx;
102 GlobalPosition scvfOuterNormal;
106 struct ElementCenters {};
107 struct VertexCenters {};
120 template<
class LocationTag,
class ScvfSelectionFunctor>
121 WallDistance(std::shared_ptr<const GridGeometry> gridGeometry, LocationTag tag,
const ScvfSelectionFunctor& select)
122 : gridGeometry_(gridGeometry)
124 initializeWallDistance_(select, tag);
133 template<
class LocationTag>
134 WallDistance(std::shared_ptr<const GridGeometry> gridGeometry, LocationTag tag)
135 :
WallDistance(gridGeometry, tag, [](const FVElementGeometry& fvGeometry, const SubControlVolumeFace& scvf) {
return true; }) {}
138 template<
class LocationTag,
class ScvfSelectionFunctor>
139 WallDistance(
const GridGeometry& gridGeometry, LocationTag tag,
const ScvfSelectionFunctor& select)
140 :
WallDistance(
Dune::stackobject_to_shared_ptr(gridGeometry), tag, select) {}
143 template<
class LocationTag>
153 {
return distance_; }
161 {
return wallData_; }
170 template<
class Cons
iderFaceFunction,
class LocationTag>
171 void initializeWallDistance_(
const ConsiderFaceFunction& considerFace, LocationTag loc)
174 ? gridGeometry_->gridView().size(0)
175 : gridGeometry_->gridView().size(dim);
177 wallData_.resize(numSamplingPoints);
178 distance_.resize(numSamplingPoints, std::numeric_limits<Scalar>::max());
180 std::vector<SimpleGeometry> wallGeometries;
181 wallGeometries.reserve(gridGeometry_->numBoundaryScvf());
183 std::vector<WallData> tempWallData;
184 tempWallData.reserve(gridGeometry_->numBoundaryScvf());
187 auto fvGeometry =
localView(*gridGeometry_);
188 for (
const auto& element : elements(gridGeometry_->gridView(), Dune::Partitions::interior))
190 fvGeometry.bindElement(element);
191 if (!fvGeometry.hasBoundaryScvf())
194 const auto eIdx = gridGeometry_->elementMapper().index(element);
196 for (
const auto& scvf : scvfs(fvGeometry))
198 if (scvf.boundary() && considerFace(fvGeometry, scvf))
200 const auto& geo = fvGeometry.geometry(scvf);
201 CornerStorage corners;
202 for (
int i = 0; i < geo.corners(); ++i)
203 corners.push_back(geo.corner(i));
205 wallGeometries.emplace_back(std::move(corners));
207 eIdx, scvf.index(), scvf.unitOuterNormal(), gridGeometry_->gridView().comm().rank()
216 const bool isParallel = gridGeometry_->gridView().comm().size() > 1;
217 std::vector<SimpleGeometry> globalWallGeometries;
218 std::vector<WallData> globalTempWallData;
219 const auto distanceField = [&]
223 const auto& communication = gridGeometry_->gridView().comm();
224 const int totalNumberOfBoundaryGeometries = communication.sum(wallGeometries.size());
225 globalWallGeometries.resize(totalNumberOfBoundaryGeometries);
226 globalTempWallData.resize(totalNumberOfBoundaryGeometries);
229 std::vector<int> numGeosPerProcLocal{
static_cast<int>(wallGeometries.size())};
230 std::vector<int> numGeosPerProcGlobal(communication.size());
231 communication.allgather(numGeosPerProcLocal.data(), 1, numGeosPerProcGlobal.data());
233 std::vector<int> disp(communication.size(), 0);
234 disp[1] = numGeosPerProcGlobal[0];
235 for (
int i = 2; i < numGeosPerProcGlobal.size(); ++i)
236 disp[i] = disp[i-1] + numGeosPerProcGlobal[i-1];
239 communication.allgatherv(
240 wallGeometries.data(),
241 wallGeometries.size(),
242 globalWallGeometries.data(),
243 numGeosPerProcGlobal.data(),
247 communication.allgatherv(
250 globalTempWallData.data(),
251 numGeosPerProcGlobal.data(),
256 return DistanceField<SimpleGeometry>(globalWallGeometries);
259 return DistanceField<SimpleGeometry>(wallGeometries);
262 const DistanceField<SimpleGeometry> distanceField(wallGeometries);
266 std::vector<GlobalPosition> points(numSamplingPoints);
268 for (
const auto& element : elements(gridGeometry_->gridView()))
269 points[gridGeometry_->elementMapper().index(element)] =
element.geometry().center();
271 for (
const auto& vertex : vertices(gridGeometry_->gridView()))
272 points[gridGeometry_->vertexMapper().index(vertex)] =
vertex.geometry().corner(0);
277 const auto kernel = [&](std::size_t eIdx){
278 const auto [d, idx] = distanceField.distanceAndIndex(points[eIdx]);
281 wallData_[eIdx] = isParallel ? globalTempWallData[idx] : tempWallData[idx];
283 wallData_[eIdx] = tempWallData[idx];
287 runKernel_(numSamplingPoints,
kernel);
291 const auto kernel = [&](std::size_t vIdx){
292 const auto [d, idx] = distanceField.distanceAndIndex(points[vIdx]);
295 wallData_[vIdx] = isParallel ? globalTempWallData[idx] : tempWallData[idx];
297 wallData_[vIdx] = tempWallData[idx];
301 runKernel_(numSamplingPoints,
kernel);
305 template<
class Kernel>
306 void runKernel_(std::size_t size,
const Kernel&
kernel)
312 for (std::size_t i = 0; i < size; ++i)
kernel(i);
315 std::vector<Scalar> distance_;
316 std::vector<WallData> wallData_;
317 std::shared_ptr<const GridGeometry> gridGeometry_;
Helper class to create (named and comparable) tagged types.
Defines the index types used for grid and local indices.
Parallel for loop (multithreading)
Corners::value_type center(const Corners &corners)
The center of a given list of corners.
Definition: center.hh:36
AABBDistanceField< Geometry > DistanceField
Class to calculate the closest distance from a point to a given set of geometries describing the doma...
Definition: distancefield.hh:129
GridCache::LocalView localView(const GridCache &gridCache)
Free function to get the local view of a grid cache object.
Definition: localview.hh:38
void parallelFor(const std::size_t count, const FunctorType &functor)
A parallel for loop (multithreading)
Definition: parallel_for.hh:172
Adaption of the non-isothermal two-phase two-component flow model to problems with CO2.
Definition: adapt.hh:29
Definition: deprecated.hh:149
constexpr Kernel kernel
Definition: couplingmanager1d3d_kernel.hh:50
typename GridView::IndexSet::IndexType GridIndex
Definition: indextraits.hh:39
Helper class to create (named and comparable) tagged types Tags any given type. The tagged type is eq...
Definition: tag.hh:42
Class to calculate the wall distance at every element or vertex of a grid.
Definition: walldistance.hh:49
static constexpr auto atVertexCenters
Definition: walldistance.hh:111
const std::vector< Scalar > & wallDistance() const
Returns a vector storing the distance from each DOF location to the nearest wall. For the atElementCe...
Definition: walldistance.hh:152
WallDistance(std::shared_ptr< const GridGeometry > gridGeometry, LocationTag tag)
Constructs a new wall distance object.
Definition: walldistance.hh:134
WallDistance(const GridGeometry &gridGeometry, LocationTag tag, const ScvfSelectionFunctor &select)
caller has to make sure the lifetime of grid geometry exceeds the lifetime of wall distance
Definition: walldistance.hh:139
WallDataImpl WallData
Definition: walldistance.hh:112
static constexpr auto atElementCenters
Definition: walldistance.hh:110
WallDistance(std::shared_ptr< const GridGeometry > gridGeometry, LocationTag tag, const ScvfSelectionFunctor &select)
Constructs a new wall distance object.
Definition: walldistance.hh:121
const std::vector< WallData > & wallData() const
Returns a vector storing additional information about the nearest scvf on the wall (element index and...
Definition: walldistance.hh:160
WallDistance(const GridGeometry &gridGeometry, LocationTag tag)
caller has to make sure the lifetime of grid geometry exceeds the lifetime of wall distance
Definition: walldistance.hh:144