FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
discrete_evaluator.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 <kernel/assembly/discrete_evaluator.hpp>
10#include <kernel/global/remote_lambda.hpp>
11#include <kernel/lafem/dense_vector.hpp>
12#include <kernel/trafo/inverse_mapping.hpp>
13#include <kernel/util/dist.hpp>
14#include <kernel/util/math.hpp>
15#include <kernel/util/tiny_algebra.hpp>
16#include <type_traits>
17#include <utility>
18
19namespace FEAT::Global
20{
21 namespace Intern
22 {
24 template<typename PointRange_>
25 static constexpr int dim_from_point_range =
26 std::remove_const_t<std::remove_reference_t<decltype(*std::declval<PointRange_>().begin())>>::n;
27
39 template<typename PointRange_, typename VectorType_, typename Space_, bool gradient_>
41
42 // Specialization for non-gradient, non-blocked vector
43 template<typename PointRange_, typename DataType_, typename IndexType_, typename Space_>
44 struct DiscreteEvaluatorTypeHelper<PointRange_, LAFEM::DenseVector<DataType_, IndexType_>, Space_, false>
45 {
46 using DataType = DataType_;
47 using IndexType = IndexType_;
48 using PointType = std::remove_const_t<std::remove_reference_t<decltype(*std::declval<PointRange_>().begin())>>;
49 static constexpr int point_dim = PointType::n;
50 using SpaceType = Space_;
51 using MeshType = typename SpaceType::MeshType;
53 using EvalValueType = typename EvalType::ValueType;
54
56 };
57
58 // Specialization for non-gradient, blocked vector
59 template<typename PointRange_, typename DataType_, typename IndexType_, int dim_, typename Space_>
61 PointRange_,
62 LAFEM::DenseVectorBlocked<DataType_, IndexType_, dim_>,
63 Space_,
64 false>
65 {
66 using DataType = DataType_;
67 using IndexType = IndexType_;
68 using PointType = std::remove_const_t<std::remove_reference_t<decltype(*std::declval<PointRange_>().begin())>>;
69 static constexpr int point_dim = PointType::n;
70 using SpaceType = Space_;
71 using MeshType = typename SpaceType::MeshType;
73 using EvalValueType = typename EvalType::ValueType;
75
76 static_assert(point_dim == dim_);
77 };
78
79 // Specialization for gradient, non-blocked vector
80 template<typename PointRange_, typename DataType_, typename IndexType_, typename Space_>
81 struct DiscreteEvaluatorTypeHelper<PointRange_, LAFEM::DenseVector<DataType_, IndexType_>, Space_, true>
82 {
83 using DataType = DataType_;
84 using IndexType = IndexType_;
85 using PointType = std::remove_const_t<std::remove_reference_t<decltype(*std::declval<PointRange_>().begin())>>;
86 static constexpr int point_dim = PointType::n;
87 using SpaceType = Space_;
88 using MeshType = typename SpaceType::MeshType;
90 using EvalValueType = typename EvalType::ValueType;
92 };
93
94 // Specialization for gradient, blocked vector
95 template<typename PointRange_, typename DataType_, typename IndexType_, int dim_, typename Space_>
97 PointRange_,
98 LAFEM::DenseVectorBlocked<DataType_, IndexType_, dim_>,
99 Space_,
100 true>
101 {
102 using DataType = DataType_;
103 using IndexType = IndexType_;
104 using PointType = std::remove_const_t<std::remove_reference_t<decltype(*std::declval<PointRange_>().begin())>>;
105 static constexpr int point_dim = PointType::n;
106 using SpaceType = Space_;
107 using MeshType = typename SpaceType::MeshType;
109 using EvalValueType = typename EvalType::ValueType;
111
112 static_assert(point_dim == dim_);
113 };
114 } // namespace Intern
115
126 {
127 public:
154 template<typename PointRange_, typename DT_, typename IT_, typename Space_>
155 static std::vector<Assembly::ScalarDiscreteEvalData<DT_, Intern::dim_from_point_range<PointRange_>>>
156 eval_fe_function(const Dist::Comm& comm, const PointRange_& points, const LAFEM::DenseVector<DT_, IT_>& vector, const Space_& space)
157 {
158 return _remote_eval<false>(comm, points, vector, space);
159 }
160
187 template<typename PointRange_, typename DT_, typename IT_, typename Space_>
188 static std::vector<Assembly::VectorDiscreteEvalData<DT_, Intern::dim_from_point_range<PointRange_>>> eval_fe_function(
189 const Dist::Comm& comm,
190 const PointRange_& points,
191 const LAFEM::DenseVectorBlocked<DT_, IT_, Intern::dim_from_point_range<PointRange_>>& vector,
192 const Space_& space)
193 {
194 return _remote_eval<false>(comm, points, vector, space);
195 }
196
223 template<typename PointRange_, typename DT_, typename IT_, typename Space_>
224 static std::vector<Assembly::VectorDiscreteEvalData<DT_, Intern::dim_from_point_range<PointRange_>>>
225 eval_fe_gradient(const Dist::Comm& comm, const PointRange_& points, const LAFEM::DenseVector<DT_, IT_>& vector, const Space_& space)
226 {
227 return _remote_eval<true>(comm, points, vector, space);
228 }
229
256 template<typename PointRange_, typename DT_, typename IT_, typename Space_>
257 static std::vector<Assembly::MatrixDiscreteEvalData<
258 DT_,
259 Intern::dim_from_point_range<PointRange_>,
260 Intern::dim_from_point_range<PointRange_>>>
262 const Dist::Comm& comm,
263 const PointRange_& points,
264 const LAFEM::DenseVectorBlocked<DT_, IT_, Intern::dim_from_point_range<PointRange_>>& vector,
265 const Space_& space)
266 {
267 return _remote_eval<true>(comm, points, vector, space);
268 }
269
270 private:
297 template<bool gradient_, typename PointRange_, typename VectorType_, typename Space_>
298 static std::vector<typename Intern::DiscreteEvaluatorTypeHelper<PointRange_, VectorType_, Space_, gradient_>::EvalType>
299 _remote_eval(const Dist::Comm& comm, const PointRange_& points, const VectorType_& vector, const Space_& space)
300 {
301 // We need to know the types of the values produced by evaluating the finite-element function
302 // and the appropriate *DiscreteEvalData wrapper. The DiscreteEvaluatorTypeHelper
303 // provides the correct types based on the VectorType_ and gradient_ params.
305
306 using DataType = typename Helper::DataType;
307 using BoundingBoxType = typename Helper::BoundingBoxType;
308 using PointType = typename Helper::PointType;
309 using EvalType = typename Helper::EvalType;
310 using EvalValueType = typename Helper::EvalValueType;
311
312 const auto total_ranks = std::size_t(comm.size());
313 const auto rank = std::size_t(comm.rank());
314 const auto num_points = std::size_t(std::distance(points.begin(), points.end()));
315
316 // Exchange bounding boxes with other ranks
317 std::vector<BoundingBoxType> bbs(static_cast<std::size_t>(total_ranks));
318 bbs.at(rank) = get_local_bb<BoundingBoxType>(space.get_mesh());
319 comm.allgather(bbs.data(), 1, bbs.data(), 1);
320
321 // Build structure for exchanging points
322 std::vector<int> ranks;
323 ranks.reserve(num_points);
324 std::vector<Index> indices;
325 ranks.reserve(num_points);
326
327 Index point_idx(0);
328 for(auto it = points.begin(); it != points.end(); ++it)
329 {
330 const PointType& p = *it;
331 for(std::size_t i(0); i < total_ranks; i++)
332 {
333 const BoundingBoxType& bb = bbs[i];
334
335 if(is_in_bb(bb, p))
336 {
337 ranks.push_back(int(i));
338 indices.push_back(point_idx);
339 }
340 }
341 point_idx++;
342 }
343
344 // Cache inverse mapping
345 // The inverse mapping creates a list of bounding boxes for each cell.
346 // This way we create this list only once per rank, rather than once per point
348
349 // Make remote calls. Each ranks returns a vector of values for each point, in case a rank manages to unmap the point on multiple cells
350 ScalarRemoteLambda<EvalValueType(const PointType&)> rl(comm);
351 std::vector<EvalValueType> evals = rl.call(
352 ranks,
353 indices,
354 points,
355 [&vector, &space, &inv_mapping](const PointType& p)
356 {
357 auto unmapping = inv_mapping.unmap_point(p);
358 if constexpr(gradient_)
359 {
360 return Assembly::DiscreteEvaluator::eval_fe_gradient(unmapping, vector, space).mean_value();
361 }
362 else
363 {
364 return Assembly::DiscreteEvaluator::eval_fe_function(unmapping, vector, space).mean_value();
365 }
366 });
367
368 // Assemble final results from individual calls
369 std::vector<EvalType> result(std::size_t(std::distance(points.begin(), points.end())));
370
371 for(std::size_t i(0); i < evals.size(); i++)
372 {
373 // To which point did this remote lambda call belong?
374 const Index idx = indices[i];
375
376 // Add evaluation result to total results for that point
377 result[idx].values.push_back(evals[i]);
378 }
379
380 return result;
381 }
382
384 template<typename BoundingBoxType_, typename PointType_>
385 static bool is_in_bb(const BoundingBoxType_& bb, const PointType_& p)
386 {
387 using DataType = typename BoundingBoxType_::ValueType;
388 constexpr int dim = BoundingBoxType_::m;
389
390 constexpr DataType bb_eps(1E-2);
391 for(int i(0); i < dim; i++)
392 {
393 if(p[i] > (bb[1][i] + bb_eps) || p[i] < (bb[0][i] - bb_eps))
394 {
395 return false;
396 }
397 }
398 return true;
399 }
400
402 template<typename BoundingBoxType_, typename MeshType_>
403 static BoundingBoxType_ get_local_bb(const MeshType_& mesh)
404 {
405 const auto& vertex_set = mesh.get_vertex_set();
406
407 if(vertex_set.get_num_vertices() == 0)
408 {
409 return BoundingBoxType_{0};
410 }
411
412 // Determine local bounding box
413 BoundingBoxType_ local_bb;
414
415 local_bb[0] = vertex_set[0];
416 local_bb[1] = vertex_set[0];
417 for(Index vtx_idx(0); vtx_idx < vertex_set.get_num_vertices(); vtx_idx++)
418 {
419 const auto& v = vertex_set[vtx_idx];
420
421 for(int i(0); i < MeshType_::world_dim; i++)
422 {
423 Math::mini(local_bb[0][i], v[i]);
424 Math::maxi(local_bb[1][i], v[i]);
425 }
426 }
427
428 return local_bb;
429 }
430 }; // class InverseMapping
431} // namespace FEAT::Global
FEAT Kernel base header.
static VectorDiscreteEvalData< DT_, dim_ > eval_fe_gradient(const Tiny::Vector< DTP_, dim_, s_ > &point, const LAFEM::DenseVector< DT_, IT_ > &vector, const Space_ &space)
Evaluates the gradient of a scalar finite-element function in a given point.
static ScalarDiscreteEvalData< DT_, dim_ > eval_fe_function(const Tiny::Vector< DTP_, dim_, s_ > &point, const LAFEM::DenseVector< DT_, IT_ > &vector, const Space_ &space)
Evaluates a scalar finite-element function in a given point.
Discrete evaluation data for matrix-valued functions.
Discrete evaluation data for scalar functions.
Discrete evaluation data for vector-valued functions.
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
Global discrete evaluator.
static std::vector< Assembly::ScalarDiscreteEvalData< DT_, Intern::dim_from_point_range< PointRange_ > > > eval_fe_function(const Dist::Comm &comm, const PointRange_ &points, const LAFEM::DenseVector< DT_, IT_ > &vector, const Space_ &space)
Evaluates a scalar finite-element function in all given points.
static std::vector< typename Intern::DiscreteEvaluatorTypeHelper< PointRange_, VectorType_, Space_, gradient_ >::EvalType > _remote_eval(const Dist::Comm &comm, const PointRange_ &points, const VectorType_ &vector, const Space_ &space)
Common function for evaluating a scalar or vector-valued finite-element function on a distributed dom...
static BoundingBoxType_ get_local_bb(const MeshType_ &mesh)
Get bounding box of local part of domain.
static std::vector< Assembly::VectorDiscreteEvalData< DT_, Intern::dim_from_point_range< PointRange_ > > > eval_fe_function(const Dist::Comm &comm, const PointRange_ &points, const LAFEM::DenseVectorBlocked< DT_, IT_, Intern::dim_from_point_range< PointRange_ > > &vector, const Space_ &space)
Evaluates a vector-valued finite-element function in all given points.
static std::vector< Assembly::MatrixDiscreteEvalData< DT_, Intern::dim_from_point_range< PointRange_ >, Intern::dim_from_point_range< PointRange_ > > > eval_fe_gradient(const Dist::Comm &comm, const PointRange_ &points, const LAFEM::DenseVectorBlocked< DT_, IT_, Intern::dim_from_point_range< PointRange_ > > &vector, const Space_ &space)
Evaluates the gradient of a vector-valued finite-element function in all given points.
static std::vector< Assembly::VectorDiscreteEvalData< DT_, Intern::dim_from_point_range< PointRange_ > > > eval_fe_gradient(const Dist::Comm &comm, const PointRange_ &points, const LAFEM::DenseVector< DT_, IT_ > &vector, const Space_ &space)
Evaluates the gradient of a scalar finite-element function in all given points.
static bool is_in_bb(const BoundingBoxType_ &bb, const PointType_ &p)
Check if a point is in a bounding box.
MPI-abstraction for running scalar lambdas on other ranks.
Blocked Dense data vector class template.
Dense data vector class template.
Tiny Matrix class template.
Tiny Vector class template.
Inverse Trafo mapping class template.
InvMapDataType unmap_point(const ImagePointType &img_point, bool ignore_failures=false) const
Unmaps a given image point.
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.
Helper struct for determining types involved in a global evaluation of a finite-element function.