3.5-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
walldistance.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 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
24#ifndef DUMUX_DISCRETIZATION_WALL_DISTANCE_HH
25#define DUMUX_DISCRETIZATION_WALL_DISTANCE_HH
26
27#include <vector>
28
29#if HAVE_TBB
30#include <tbb/tbb.h>
31#endif
32
33#include <dune/common/parallel/mpihelper.hh>
34#include <dune/common/shared_ptr.hh>
35#include <dune/common/reservedvector.hh>
36#include <dune/grid/common/partitionset.hh>
37
38#include <dumux/common/tag.hh>
41
42namespace Dumux {
43
50template<class GridGeometry, template<class> class DistanceField = AABBDistanceField>
52{
53 using GridView = typename GridGeometry::GridView;
54 using GridIndexType = typename IndexTraits<GridView>::GridIndex;
55 using SubControlVolumeFace = typename GridGeometry::SubControlVolumeFace;
56 using FVElementGeometry = typename GridGeometry::LocalView;
57 using Element = typename GridGeometry::GridView::template Codim<0>::Entity;
58 using Scalar = typename GridView::Grid::ctype;
59 using GlobalPosition = typename SubControlVolumeFace::GlobalPosition;
60
61 static constexpr int dim = GridView::dimension;
62 static constexpr int dimWorld = GridView::dimensionworld;
63 static_assert(dim == dimWorld, "Wall distances cannot be computed for embedded surface or network domains.");
64
65 using CornerStorage = Dune::ReservedVector<GlobalPosition, (1<<(GridView::dimension-1))>;
66
67 // We use a simplified geometry type here which allows much easier MPI communication
68 // for parallel runs compared to the Dune geometry types (due to fixed-size storage).
69 // This type only implements a minimal part of the Geometry interface.
70 struct SimpleGeometry
71 {
72 SimpleGeometry() = default;
73 SimpleGeometry(CornerStorage&& corners)
74 : corners_(std::move(corners))
75 , center_(0.0)
76 {
77 for (int i = 0; i < corners_.size(); ++i)
78 center_ += corners_[i];
79 center_ /= corners_.size();
80 }
81
82 using GlobalCoordinate = GlobalPosition;
83 using ctype = typename GlobalCoordinate::value_type;
84 static constexpr int coorddimension = GridView::dimensionworld;
85 static constexpr int mydimension = GridView::dimension-1;
86
87 std::size_t corners() const
88 { return corners_.size(); }
89
90 const auto& corner(int i) const
91 { return corners_[i]; }
92
93 const auto& center() const
94 { return center_; }
95
96 private:
97 CornerStorage corners_;
98 GlobalCoordinate center_;
99 };
100
101 struct WallDataImpl
102 {
103 GridIndexType eIdx;
104 GridIndexType scvfIdx;
105 GlobalPosition scvfOuterNormal;
106 int rank; // for parallel runs
107 };
108
109 struct ElementCenters {};
110 struct VertexCenters {};
111
112public:
115 using WallData = WallDataImpl;
116
123 template<class LocationTag, class ScvfSelectionFunctor>
124 WallDistance(std::shared_ptr<const GridGeometry> gridGeometry, LocationTag tag, const ScvfSelectionFunctor& select)
125 : gridGeometry_(gridGeometry)
126 {
127 initializeWallDistance_(select, tag);
128 }
129
136 template<class LocationTag>
137 WallDistance(std::shared_ptr<const GridGeometry> gridGeometry, LocationTag tag)
138 : WallDistance(gridGeometry, tag, [](const FVElementGeometry& fvGeometry, const SubControlVolumeFace& scvf) { return true; }) {}
139
141 template<class LocationTag, class ScvfSelectionFunctor>
142 WallDistance(const GridGeometry& gridGeometry, LocationTag tag, const ScvfSelectionFunctor& select)
143 : WallDistance(Dune::stackobject_to_shared_ptr(gridGeometry), tag, select) {}
144
146 template<class LocationTag>
147 WallDistance(const GridGeometry& gridGeometry, LocationTag tag)
148 : WallDistance(Dune::stackobject_to_shared_ptr(gridGeometry), tag) {}
149
155 const std::vector<Scalar>& wallDistance() const
156 { return distance_; }
157
163 const std::vector<WallData>& wallData() const
164 { return wallData_; }
165
166private:
173 template<class ConsiderFaceFunction, class LocationTag>
174 void initializeWallDistance_(const ConsiderFaceFunction& considerFace, LocationTag loc)
175 {
176 const std::size_t numSamplingPoints = (loc == atElementCenters)
177 ? gridGeometry_->gridView().size(0)
178 : gridGeometry_->gridView().size(dim);
179 // Reset the containers.
180 wallData_.resize(numSamplingPoints);
181 distance_.resize(numSamplingPoints, std::numeric_limits<Scalar>::max());
182
183 std::vector<SimpleGeometry> wallGeometries;
184 wallGeometries.reserve(gridGeometry_->numBoundaryScvf());
185
186 std::vector<WallData> tempWallData;
187 tempWallData.reserve(gridGeometry_->numBoundaryScvf());
188
189 // Loop over all elements: find all wall scvfs.
190 auto fvGeometry = localView(*gridGeometry_);
191 for (const auto& element : elements(gridGeometry_->gridView(), Dune::Partitions::interior))
192 {
193 fvGeometry.bindElement(element);
194 if (!fvGeometry.hasBoundaryScvf())
195 continue;
196
197 const auto eIdx = gridGeometry_->elementMapper().index(element);
198
199 for (const auto& scvf : scvfs(fvGeometry))
200 {
201 if (scvf.boundary() && considerFace(fvGeometry, scvf))
202 {
203 const auto& geo = scvf.geometry();
204 CornerStorage corners;
205 for (int i = 0; i < geo.corners(); ++i)
206 corners.push_back(geo.corner(i));
207
208 wallGeometries.emplace_back(std::move(corners));
209 tempWallData.push_back(WallData{
210 eIdx, scvf.index(), scvf.unitOuterNormal(), gridGeometry_->gridView().comm().rank()
211 });
212 }
213 }
214 }
215
216#if HAVE_MPI
217 // Handle parallel runs. We need to prepare a global vector of wall geometries,
218 // containing the wall geometries of each process in order to get a correct distance field.
219 const bool isParallel = gridGeometry_->gridView().comm().size() > 1;
220 std::vector<SimpleGeometry> globalWallGeometries;
221 std::vector<WallData> globalTempWallData;
222 const auto distanceField = [&]
223 {
224 if (isParallel)
225 {
226 const auto& communication = gridGeometry_->gridView().comm();
227 const int totalNumberOfBoundaryGeometries = communication.sum(wallGeometries.size());
228 globalWallGeometries.resize(totalNumberOfBoundaryGeometries);
229 globalTempWallData.resize(totalNumberOfBoundaryGeometries);
230
231 // prepare a displacement vector
232 std::vector<int> numGeosPerProcLocal{static_cast<int>(wallGeometries.size())};
233 std::vector<int> numGeosPerProcGlobal(communication.size());
234 communication.allgather(numGeosPerProcLocal.data(), 1, numGeosPerProcGlobal.data());
235
236 std::vector<int> disp(communication.size(), 0);
237 disp[1] = numGeosPerProcGlobal[0];
238 for (int i = 2; i < numGeosPerProcGlobal.size(); ++i)
239 disp[i] = disp[i-1] + numGeosPerProcGlobal[i-1];
240
241 // concatenate the wall geometries and temp scvf data of each process into a global vector
242 communication.allgatherv(
243 wallGeometries.data(),
244 wallGeometries.size(),
245 globalWallGeometries.data(),
246 numGeosPerProcGlobal.data(),
247 disp.data()
248 );
249
250 communication.allgatherv(
251 tempWallData.data(),
252 tempWallData.size(),
253 globalTempWallData.data(),
254 numGeosPerProcGlobal.data(),
255 disp.data()
256 );
257
258 // pass the global vector of wall geometries to the distance field
259 return DistanceField<SimpleGeometry>(globalWallGeometries);
260 }
261 else
262 return DistanceField<SimpleGeometry>(wallGeometries);
263 }();
264#else
265 const DistanceField<SimpleGeometry> distanceField(wallGeometries);
266#endif
267
268 // compute sampling points
269 std::vector<GlobalPosition> points(numSamplingPoints);
270 if (loc == atElementCenters)
271 for (const auto& element : elements(gridGeometry_->gridView()))
272 points[gridGeometry_->elementMapper().index(element)] = element.geometry().center();
273 else
274 for (const auto& vertex : vertices(gridGeometry_->gridView()))
275 points[gridGeometry_->vertexMapper().index(vertex)] = vertex.geometry().corner(0);
276
277 // get the actual distances (this is the most expensive part)
278 if (loc == atElementCenters)
279 {
280 const auto kernel = [&](std::size_t eIdx){
281 const auto [d, idx] = distanceField.distanceAndIndex(points[eIdx]);
282 distance_[eIdx] = d;
283#if HAVE_MPI
284 wallData_[eIdx] = isParallel ? globalTempWallData[idx] : tempWallData[idx];
285#else
286 wallData_[eIdx] = tempWallData[idx];
287#endif
288 };
289
290 runKernel_(numSamplingPoints, kernel);
291 }
292 else
293 {
294 const auto kernel = [&](std::size_t vIdx){
295 const auto [d, idx] = distanceField.distanceAndIndex(points[vIdx]);
296 distance_[vIdx] = d;
297#if HAVE_MPI
298 wallData_[vIdx] = isParallel ? globalTempWallData[idx] : tempWallData[idx];
299#else
300 wallData_[vIdx] = tempWallData[idx];
301#endif
302 };
303
304 runKernel_(numSamplingPoints, kernel);
305 }
306 }
307
308 template<class Kernel>
309 void runKernel_(std::size_t size, const Kernel& kernel)
310 {
311#if HAVE_TBB
312 // parallelize with tbb if we have enough work (enough evaluation points)
313 if (size > 10000)
314 tbb::parallel_for(std::size_t(0), size, [&](std::size_t i){ kernel(i); });
315 else
316 for (std::size_t i = 0; i < size; ++i) kernel(i);
317
318#else
319 for (std::size_t i = 0; i < size; ++i) kernel(i);
320#endif
321 }
322
323 std::vector<Scalar> distance_;
324 std::vector<WallData> wallData_;
325 std::shared_ptr<const GridGeometry> gridGeometry_;
326};
327
328} // end namespace Dumux
329
330#endif
Defines the index types used for grid and local indices.
Helper class to create (named and comparable) tagged types.
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
Definition: adapt.hh:29
Definition: common/pdesolver.hh:36
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:52
static constexpr auto atVertexCenters
Definition: walldistance.hh:114
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:155
WallDistance(std::shared_ptr< const GridGeometry > gridGeometry, LocationTag tag)
Constructs a new wall distance object.
Definition: walldistance.hh:137
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:142
WallDataImpl WallData
Definition: walldistance.hh:115
static constexpr auto atElementCenters
Definition: walldistance.hh:113
WallDistance(std::shared_ptr< const GridGeometry > gridGeometry, LocationTag tag, const ScvfSelectionFunctor &select)
Constructs a new wall distance object.
Definition: walldistance.hh:124
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:163
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:147