FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
inverse_mapping.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8#include <functional>
9
10#include <kernel/adjacency/adjactor.hpp>
11#include <kernel/adjacency/base.hpp>
12#include <kernel/adjacency/graph.hpp>
14#include <kernel/global/remote_lambda.hpp>
15#include <kernel/trafo/inverse_mapping.hpp>
16#include <kernel/trafo/standard/mapping.hpp>
17#include <kernel/util/dist.hpp>
18#include <kernel/util/math.hpp>
19#include <kernel/util/string.hpp>
20#include <kernel/util/tiny_algebra.hpp>
21
22namespace FEAT::Global
23{
42 template<typename DataType_, int shape_dim_, int world_dim_ = shape_dim_>
44 {
45 public:
50
53
55 std::vector<int> ranks;
56
58 std::vector<Index> cells;
59
61 std::vector<DomainPointType> dom_points;
62
65
68
71
74 img_point(other.img_point),
75 ranks(std::move(other.ranks)),
76 cells(std::move(other.cells)),
77 dom_points(std::move(other.dom_points))
78 {
79 }
80
83 {
84 if(this != &other)
85 {
86 img_point = other.img_point;
87 ranks = std::move(other.ranks);
88 cells = std::move(other.cells);
89 dom_points = std::move(other.dom_points);
90 }
91 return *this;
92 }
93
97 bool empty() const
98 {
99 return cells.empty();
100 }
101
103 std::size_t size() const
104 {
105 return cells.size();
106 }
107 }; // class GlobalInverseMappingData
108
121 template<typename InverseMappingType_>
123 {
124 public:
126 using InverseMappingType = InverseMappingType_;
128 using TrafoType = typename InverseMappingType::TrafoType;
130 using DataType = typename InverseMappingType::DataType;
132 using ShapeType = typename InverseMappingType::ShapeType;
134 static constexpr int shape_dim = ShapeType::dimension;
136 static constexpr int world_dim = TrafoType::world_dim;
145
146 private:
150 std::reference_wrapper<const InverseMappingType> _mapping;
153
156 {
157 Index cell;
158 DomainPointType dom_point;
159
162 {
163 return Dist::Typemap()
164 .add_entry<Index>(offsetof(UnmappingResult, cell))
165 .add_entry<DomainPointType>(offsetof(UnmappingResult, dom_point));
166 }
167 };
168
169 public:
170
172 InverseMapping(const Dist::Comm& comm, const InverseMappingType& m) : _comm(comm.comm_dup()), _mapping(m), _local_bb(get_local_bb())
173 {
174 }
175
185 template<typename PointRange_>
186 std::vector<InvMapDataType> unmap_points(const PointRange_& points)
187 {
188 const int total_ranks = _comm.size();
189
190 // Exchange bounding boxes with other ranks
191 std::vector<BoundingBoxType> bbs(static_cast<std::size_t>(total_ranks));
192 bbs.at(std::size_t(_comm.rank())) = _local_bb;
193 _comm.allgather(bbs.data(), 1, bbs.data(), 1);
194
195 // Build structure for exchanging points
196 std::vector<int> ranks;
197 std::vector<Index> indices;
198
199 Index point_idx(0);
200 for(auto it = points.begin(); it != points.end(); ++it)
201 {
202 const ImagePointType& p = *it;
203 for(int i(0); i < total_ranks; i++)
204 {
205 const BoundingBoxType& bb = bbs[std::size_t(i)];
206
207 if(is_in_bb(bb, p))
208 {
209 ranks.push_back(i);
210 indices.push_back(point_idx);
211 }
212 }
213 point_idx++;
214 }
215
217 std::vector<std::vector<UnmappingResult>> result_pairs = rl.call(ranks, indices, points, [this](const ImagePointType& p)
218 {
219 // NOTE(mmuegge): ignore_failure is always set to true, because the RemoteLambda call is a collective operations
220 // and one process throwing an exception will cause a deadlock.
221 auto local_result = _mapping.get().unmap_point(p, true);
222
223 // Convert local result to result pairs for mpi sending
224 std::vector<UnmappingResult> result;
225 result.reserve(local_result.size());
226 for(Index i(0); i < local_result.size(); i++)
227 {
228 result.push_back({local_result.cells[i], local_result.dom_points[i]});
229 }
230
231 return result;
232 });
233
234
235 // Assemble final result from ResultPairs
236 std::vector<InvMapDataType> result(std::size_t(std::distance(points.begin(), points.end())));
237 for(std::size_t i(0); i < ranks.size(); i++)
238 {
239 const int rank = ranks[i];
240 const Index idx = indices[i];
241 auto iter = points.begin();
242 std::advance(iter, idx);
243 const ImagePointType& image_point = *iter;
244
245 std::vector<UnmappingResult>& pairs = result_pairs[i];
246
247 result[idx].img_point = image_point;
248 for(const UnmappingResult& pair : pairs)
249 {
250 result[idx].cells.push_back(pair.cell);
251 result[idx].dom_points.push_back(pair.dom_point);
252 result[idx].ranks.push_back(rank);
253 }
254 }
255
256 return result;
257 }
258
259 private:
261 static bool is_in_bb(const BoundingBoxType& bb, const ImagePointType& p)
262 {
263 constexpr DataType bb_eps(1E-2);
264 for(int i(0); i < world_dim; i++)
265 {
266 if(p[i] > (bb[1][i] + bb_eps) || p[i] < (bb[0][i] - bb_eps))
267 {
268 return false;
269 }
270 }
271 return true;
272 }
273
276 {
277 const auto& vertex_set = _mapping.get().get_trafo().get_mesh().get_vertex_set();
278
279 if(vertex_set.get_num_vertices() == 0)
280 {
281 return BoundingBoxType{0};
282 }
283
284 // Determine local bounding box
285 BoundingBoxType local_bb;
286
287 local_bb[0] = vertex_set[0];
288 local_bb[1] = vertex_set[0];
289 for(Index vtx_idx(0); vtx_idx < vertex_set.get_num_vertices(); vtx_idx++)
290 {
291 const auto& v = vertex_set[vtx_idx];
292
293 for(int i(0); i < world_dim; i++)
294 {
295 Math::mini(local_bb[0][i], v[i]);
296 Math::maxi(local_bb[1][i], v[i]);
297 }
298 }
299
300 return local_bb;
301 }
302 }; // class InverseMapping
303} // namespace FEAT::Global
FEAT Kernel base header.
Communicator class.
Definition: dist.hpp:1657
int size() const
Returns the size of this communicator.
Definition: dist.hpp:1814
int rank() const
Returns the rank of this process in this communicator.
Definition: dist.hpp:1802
void allgather(const void *sendbuf, std::size_t sendcount, const Datatype &sendtype, void *recvbuf, std::size_t recvcount, const Datatype &recvtype) const
Blocking gather-to-all.
Definition: dist.cpp:592
MPI typemap description.
Definition: dist.hpp:228
Typemap & add_entry(std::size_t displacement, int blocklength=1)
Add a member to this typemap.
Definition: dist.hpp:259
Data structure for InverseMapping evaluations.
GlobalInverseMappingData(const GlobalInverseMappingData &other)=default
use default copy constructor
GlobalInverseMappingData & operator=(GlobalInverseMappingData &&other)
move-assign operator
GlobalInverseMappingData & operator=(const GlobalInverseMappingData &other)=default
default copy assignment operator
bool empty() const
Checks whether no cells were found.
std::vector< int > ranks
the ranks of the cells that intersect with the image point
Tiny::Vector< DataType_, world_dim_ > ImagePointType
the image point type
std::vector< Index > cells
the indices of the cells that intersect with the image point
GlobalInverseMappingData(GlobalInverseMappingData &&other) noexcept
move constructor
std::vector< DomainPointType > dom_points
the domain points on each cell that map onto the image point
Tiny::Vector< DataType_, shape_dim_ > DomainPointType
the domain point type
ImagePointType img_point
the image point that was unmapped
GlobalInverseMappingData()=default
default constructor
Global inverse mapping.
BoundingBoxType get_local_bb()
Get bounding box of local part of domain.
typename InvMapDataType::ImagePointType ImagePointType
Point type for points on the mesh.
static constexpr int shape_dim
Shape dimension.
BoundingBoxType _local_bb
Bounding box of local patch.
typename InverseMappingType::DataType DataType
Datatype.
InverseMappingType_ InverseMappingType
Local inverse mapping type.
std::reference_wrapper< const InverseMappingType > _mapping
Reference to local inverse mapping.
typename InvMapDataType::DomainPointType DomainPointType
Point type for points on the reference element.
static bool is_in_bb(const BoundingBoxType &bb, const ImagePointType &p)
Check if a point is in a bounding box.
typename InverseMappingType::TrafoType TrafoType
Trafo type.
Dist::Comm _comm
Communicator that domain is distributed across.
static constexpr int world_dim
World dimension.
InverseMapping(const Dist::Comm &comm, const InverseMappingType &m)
Constructor.
typename InverseMappingType::ShapeType ShapeType
Shape type.
std::vector< InvMapDataType > unmap_points(const PointRange_ &points)
Unmap a range of points.
MPI-abstraction for running vector-valued lambdas on other ranks.
Global linear algebra namespace.
void mini(T_ &xmin, T_ x)
Updates the minimum of a set of values.
Definition: math.hpp:154
void maxi(T_ &xmax, T_ x)
Updates the maximum of a set of values.
Definition: math.hpp:172
std::uint64_t Index
Index data type.
@ dom_point
specifies whether the trafo should supply domain point coordinates
Pair for communicating cells and associated domain points between ranks.
static Dist::Typemap typemap()
Make type MPI-compatible.