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#include <kernel/trafo/mapping_base.hpp>
11
12#include <vector>
13
14namespace FEAT
15{
16 namespace Trafo
17 {
19 namespace Intern
20 {
21 template<typename Shape_>
22 struct InverseMappingHelper;
23 } // namespace Intern
25
35 public FEAT::Exception
36 {
37 public:
47 template<typename DT_, int n_, int s_>
49 Exception(String("Failed to unmap point ") + stringify(point) + " on cell " + stringify(cell))
50 {
51 }
52 }; // class InverseMappingError
53
71 template<typename DataType_, int shape_dim_, int world_dim_ = shape_dim_>
73 {
74 public:
79
82
84 std::vector<Index> cells;
85
87 std::vector<DomainPointType> dom_points;
88
91 {
92 }
93
95 InverseMappingData(const InverseMappingData& other) = default;
96
99 img_point(other.img_point),
100 cells(std::forward<std::vector<Index>>(other.cells)),
101 dom_points(std::forward<std::vector<DomainPointType>>(other.dom_points))
102 {
103 }
104
107 {
108 if(this != &other)
109 {
110 img_point = other.img_point;
111 cells = std::forward<std::vector<Index>>(other.cells);
112 dom_points = std::forward<std::vector<DomainPointType>>(other.dom_points);
113 }
114 return *this;
115 }
116
120 bool empty() const
121 {
122 return cells.empty();
123 }
124
126 std::size_t size() const
127 {
128 return cells.size();
129 }
130 }; // class InverseMappingData
131
156 template<typename Trafo_, typename DataType_>
158 {
159 public:
161 typedef Trafo_ TrafoType;
163 typedef DataType_ DataType;
165 typedef typename TrafoType::ShapeType ShapeType;
167 static constexpr int shape_dim = ShapeType::dimension;
169 static constexpr int world_dim = TrafoType::world_dim;
170
173
178
179 protected:
185 std::vector<BBox> _bboxes;
192
193 public:
211 explicit InverseMapping(const TrafoType& trafo,
212 DataType bbox_tol = DataType(1E-2),
213 DataType domain_tol = DataType(1E-4),
214 bool init_boxes = true
215 ) :
216 _trafo(trafo),
217 _domain_tol(domain_tol),
218 _newton_tol(Math::pow(Math::eps<DataType>(), DataType(0.9))),
220 {
221 if(init_boxes)
222 init_bounding_boxes(bbox_tol);
223 }
224
226 InverseMapping(const InverseMapping& other) = delete;
227
229 InverseMapping& operator=(const InverseMapping& other) = delete;
230
232 InverseMapping(InverseMapping&& other) = default;
233
236
238 virtual ~InverseMapping() = default;
239
241 const TrafoType& get_trafo() const
242 {
243 return _trafo;
244 }
245
257 {
258 // get the mesh
259 const auto& mesh = _trafo.get_mesh();
260 // get the vertex set
261 const auto& vtx_set = mesh.get_vertex_set();
262 // get the vertices-at-cell index set
263 const auto& vert_idx = mesh.template get_index_set<shape_dim, 0>();
264
265 // resize bounding boxes array
266 _bboxes.resize(std::size_t(mesh.get_num_elements()));
267
268 // loop over all cells
269 for(Index cell(0); cell < vert_idx.get_num_entities(); ++cell)
270 {
271 // get this cell's vertex indices
272 const auto& vidx = vert_idx[cell];
273
274 // get this cell's bounding box
275 BBox& bbox = _bboxes.at(std::size_t(cell));
276
277 // build cell bounding box
278 bbox[0] = bbox[1] = vtx_set[vidx[0]];
279 for(int i(1); i < vert_idx.get_num_indices(); ++i)
280 {
281 // get vertex and update bounding box
282 const auto& vtx = vtx_set[vidx[i]];
283 for(int j(0); j < world_dim; ++j)
284 {
285 Math::minimax(DataType(vtx[j]), bbox[0][j], bbox[1][j]);
286 }
287 }
288
289 // finally, incorporate tolerances
290 for(int j(0); j < world_dim; ++j)
291 {
292 // compute bbox dimension
293 DataType bb_extra = bbox_tol * (bbox[1][j] - bbox[0][j]);
294 bbox[0][j] -= bb_extra;
295 bbox[1][j] += bb_extra;
296 }
297 }
298 }
299
336 InvMapDataType unmap_point(const ImagePointType& img_point, bool ignore_failures = false) const
337 {
338 // store image point
339 InvMapDataType inv_data;
340 inv_data.img_point = img_point;
341
342 // find candidate cells
343 std::vector<Index> cells;
344 if(!this->find_candidate_cells(cells, img_point))
345 return inv_data;
346
347 // loop over all candidate cells
348 for(Index cell : cells)
349 {
350 // try to unmap the point by newton
352 if(!this->unmap_point_by_newton(dom_point, img_point, cell))
353 {
354 // point unmapping failed!
355 if(ignore_failures)
356 continue;
357
358 // throw an inverse mapping error
359 throw InverseMappingError(dom_point, cell);
360 }
361
362 // check if the domain point is on the reference element
363 if(!this->test_domain_point(dom_point))
364 {
365 // nope ==> skip point
366 continue;
367 }
368
369 // okay
370 inv_data.cells.push_back(cell);
371 inv_data.dom_points.push_back(dom_point);
372 }
373
374 // found at least one cell?
375 return inv_data;
376 }
377
419 InvMapDataType unmap_point(const ImagePointType& img_point, const std::vector<Index>& cells, bool ignore_failures = false) const
420 {
421 // store image point
422 InvMapDataType inv_data;
423 inv_data.img_point = img_point;
424
425 if(cells.empty())
426 return inv_data;
427
428 // loop over all candidate cells
429 for(Index cell : cells)
430 {
431 // try to unmap the point by newton
433 if(!this->unmap_point_by_newton(dom_point, img_point, cell))
434 {
435 // point unmapping failed!
436 if(ignore_failures)
437 continue;
438
439 // throw an inverse mapping error
440 throw InverseMappingError(dom_point, cell);
441 }
442
443 // check if the domain point is on the reference element
444 if(!this->test_domain_point(dom_point))
445 {
446 // nope ==> skip point
447 continue;
448 }
449
450 // okay
451 inv_data.cells.push_back(cell);
452 inv_data.dom_points.push_back(dom_point);
453 }
454
455 // found at least one cell?
456 return inv_data;
457 }
458
482 bool find_candidate_cells(std::vector<Index>& cells, const ImagePointType& img_point) const
483 {
484 XASSERTM(_bboxes.size() == _trafo.get_mesh().get_num_elements(), "Boundingbox are not initilialized correctly");
485 // loop over all cells/bounding boxes
486 for(std::size_t cell(0); cell < _bboxes.size(); ++cell)
487 {
488 // point inside bounding box?
489 if(is_in_bounding_box(cell, img_point))
490 {
491 cells.push_back(Index(cell));
492 }
493 }
494
495 return !cells.empty();
496 }
497
498 bool is_in_bounding_box(Index cell, const ImagePointType& img_point) const
499 {
500 const BBox& bbox = _bboxes.at(cell);
501
502 // check whether the point is in the bounding box
503 for(int j(0); j < shape_dim; ++j)
504 {
505 if((img_point[j] < bbox[0][j]) || (img_point[j] > bbox[1][j]))
506 {
507 return false;
508 }
509 }
510
511 return true;
512 }
513
531 {
532 // define an evaluator
533 typedef typename TrafoType::template Evaluator<ShapeType, DataType>::Type TrafoEvaluator;
534 TrafoEvaluator trafo_eval(_trafo);
535
536 // for Newton iteration we need image points and inverse jacobians
537 static constexpr TrafoTags trafo_tags = TrafoTags::img_point|TrafoTags::jac_inv;
538
539 // define the trafo eval data
540 typename TrafoEvaluator::template ConfigTraits<trafo_tags>::EvalDataType trafo_data;
541
542 // initialize output domain point to cell centre
543 for(int i(0); i < shape_dim; ++i)
545
546 // compute squared tolerance
547 const DataType tol_sqr = Math::sqr(_newton_tol);
548
549 // no convergence yet
550 bool converged = false;
551
552 // prepare trafo evaluator
553 trafo_eval.prepare(cell);
554
555 // newton iteration
556 for(Index iter(0); iter < _newton_max_iter; ++iter)
557 {
558 // evaluate trafo in current point
559 trafo_eval(trafo_data, dom_point);
560
561 // compute image point distance vector
562 ImagePointType def = trafo_data.img_point - img_point;
563
564 // check defect for tolerance
565 if(def.norm_euclid_sqr() < tol_sqr)
566 {
567 converged = true;
568 break;
569 }
570
571 // update reference point by newton
572 dom_point -= trafo_data.jac_inv * def;
573 }
574
575 // finish trafo evaluator
576 trafo_eval.finish();
577
578 // Newton converged?
579 return converged;
580 }
581
592 {
593 return Intern::InverseMappingHelper<ShapeType>::is_on_ref(dom_point, _domain_tol);
594 }
595 }; // class InverseMapping
596
606 template<typename InverseMappingType_, typename CFMappingType_>
608 {
609 public:
611 using InverseMappingType = InverseMappingType_;
613 using TrafoType = typename InverseMappingType::TrafoType;
615 using DataType = typename InverseMappingType::DataType;
617 using ShapeType = typename InverseMappingType::ShapeType;
619 static constexpr int shape_dim = ShapeType::dimension;
621 static constexpr int world_dim = TrafoType::world_dim;
622
624 using InvMapDataType = typename InverseMappingType::InvMapDataType;
626 using ImagePointType = typename InvMapDataType::ImagePointType;
628 using DomainPointType = typename InvMapDataType::DomainPointType;
630 using CFMappingType = CFMappingType_;
631
632 protected:
634 std::deque<std::reference_wrapper<const InverseMappingType>> _mappings;
636 std::deque<CFMappingType_> _coarse_fine_mappings;
637
638 public:
641
644
647
650
653
655 virtual ~HierarchicalInverseMapping() = default;
656
668 void push_level(const InverseMappingType& imapping)
669 {
670 XASSERT(_mappings.empty());
671
672 _mappings.push_back(imapping);
673 }
674
687 void push_level(const InverseMappingType& imapping, const CFMappingType_& cf_mapping)
688 {
689 XASSERT(!_mappings.empty());
690 XASSERT(_mappings.back().get().get_trafo().get_mesh().get_num_elements() < imapping.get_trafo().get_mesh().get_num_elements());
691 XASSERT(cf_mapping.get_num_nodes_domain() == _mappings.back().get().get_trafo().get_mesh().get_num_elements());
692 XASSERT(cf_mapping.get_num_nodes_image() == imapping.get_trafo().get_mesh().get_num_elements());
693
694 _mappings.push_back(imapping);
695 _coarse_fine_mappings.push_back(cf_mapping);
696 }
697
699 const TrafoType& get_trafo() const
700 {
701 return _mappings.back().get().get_trafo();
702 }
703
734 InvMapDataType unmap_point(const ImagePointType& img_point, bool ignore_failures = false) const
735 {
736 const Index num_layers = _mappings.size();
737
738 XASSERT(num_layers > 0);
739 XASSERT(_mappings.size() == _coarse_fine_mappings.size() + 1);
740
741 // Double buffered cells and domain points for transferring between layers
742 InvMapDataType current;
743 InvMapDataType next;
744
745 current.img_point = img_point;
746 next.img_point = img_point;
747
748 // Regular inverse mapping on coarsest mesh
749 current = _mappings.front().get().unmap_point(img_point, ignore_failures);
750
751 if(num_layers == 1)
752 {
753 // There is no hierarchy to check. We are done
754 return current;
755 }
756
757 // First layer is coarse layer. Skip it
758 for(Index level(1); level < num_layers; level++)
759 {
760 const CFMappingType& coarse_to_fine = _coarse_fine_mappings[level - 1];
761 const InverseMappingType& fine_inv_mapping = _mappings[level];
762 for(Index cell : current.cells)
763 {
764 const auto fbegin = coarse_to_fine.image_begin(cell);
765 const auto fend = coarse_to_fine.image_end(cell);
766
767 for(auto it = fbegin; it != fend; ++it)
768 {
769 DomainPointType domain_point{};
770 if(try_unmap(fine_inv_mapping, domain_point, img_point, *it, ignore_failures))
771 {
772 next.cells.push_back(*it);
773 next.dom_points.push_back(domain_point);
774 }
775 }
776 }
777
778 // Switch mappings for next layer
779 std::swap(current, next);
780
781 // Empty new fine mappings
782 next.cells.clear();
783 next.dom_points.clear();
784 }
785
786 // current contains result of last iteration
787 return current;
788 }
789 private:
804 static bool try_unmap(
805 const InverseMappingType& mapping,
806 DomainPointType& domain_point,
808 const Index cell,
809 const bool ignore_failures = false)
810 {
811 if(!mapping.is_in_bounding_box(cell, img_point))
812 {
813 return false;
814 }
815
816 if(!mapping.unmap_point_by_newton(domain_point, img_point, cell))
817 {
818 if(!ignore_failures)
819 {
820 throw InverseMappingError(domain_point, cell);
821 }
822 else
823 {
824 return false;
825 }
826 }
827
828 return mapping.test_domain_point(domain_point);
829 }
830 }; // class HierarchicalInverseMapping
831
833 namespace Intern
834 {
835 template<int dim_>
836 struct InverseMappingHelper<Shape::Simplex<dim_>>
837 {
838 // checks whether p is on the reference cell
839 template<typename PT_, typename DT_>
840 static bool is_on_ref(const PT_& p, const DT_ tol)
841 {
842 // reference simplex: all coords are in range [0,1]
843 // and the sum of all coords is <= 1
844 DT_ s = DT_(0);
845 for(int i(0); i < dim_; ++i)
846 {
847 // coord outside range [-tol, 1+tol] ?
848 if((p[i] < -tol) || (p[i] > DT_(1)+tol))
849 return false;
850 s += p[i];
851 }
852 // coord sum <= 1+tol ?
853 return (s <= DT_(1) + tol);
854 }
855 };
856
857 template<int dim_>
858 struct InverseMappingHelper<Shape::Hypercube<dim_>>
859 {
860 // checks whether p is on the reference cell
861 template<typename PT_, typename DT_>
862 static bool is_on_ref(const PT_& p, const DT_ tol)
863 {
864 // reference hypercube: all coordinates are in range [-1,+1]
865 for(int i(0); i < dim_; ++i)
866 {
867 // coord outside range [-1-tol, +1+tol] ?
868 if((p[i] < -DT_(1)-tol) || (p[i] > DT_(1)+tol))
869 return false;
870 }
871 return true;
872 }
873 };
874 } // namespace Intern
876 } // namespace Trafo
877} // namespace FEAT
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Base exception class.
Definition: exception.hpp:27
String class implementation.
Definition: string.hpp:47
Tiny Matrix class template.
Tiny Vector class template.
CUDA_HOST_DEVICE DataType norm_euclid_sqr() const
Computes the squared euclid norm of this vector.
Accelerated inverse mapping for multigrid hierarchies.
std::deque< CFMappingType_ > _coarse_fine_mappings
deque of mapping between levels
static constexpr int world_dim
the world dimension
typename InvMapDataType::ImagePointType ImagePointType
the image point type
virtual ~HierarchicalInverseMapping()=default
virtual destructor
static constexpr int shape_dim
the shape dimension
HierarchicalInverseMapping(HierarchicalInverseMapping &&other)=default
Move constructor.
void push_level(const InverseMappingType &imapping)
Push the coarse level into the hierarchy.
InverseMappingType_ InverseMappingType
the type of the inverse mapping on each level
HierarchicalInverseMapping & operator=(HierarchicalInverseMapping &&other)=default
Move-assignment operator.
static bool try_unmap(const InverseMappingType &mapping, DomainPointType &domain_point, const ImagePointType &img_point, const Index cell, const bool ignore_failures=false)
Try to unmap a point.
typename InverseMappingType::DataType DataType
the datatype to be used
HierarchicalInverseMapping()=default
Constructor.
HierarchicalInverseMapping & operator=(const HierarchicalInverseMapping &other)=delete
Copy-assignment operator.
typename InverseMappingType::TrafoType TrafoType
the underlying trafo type
void push_level(const InverseMappingType &imapping, const CFMappingType_ &cf_mapping)
Push a fine level into the hierarchy.
std::deque< std::reference_wrapper< const InverseMappingType > > _mappings
deque of inverse mapping for each level
const TrafoType & get_trafo() const
Return a reference to the underlying trafo on the finest level.
typename InverseMappingType::ShapeType ShapeType
the shape type
InvMapDataType unmap_point(const ImagePointType &img_point, bool ignore_failures=false) const
Unmaps a given image point.
typename InverseMappingType::InvMapDataType InvMapDataType
the inverse mapping data type
CFMappingType_ CFMappingType
the type of mapping between mesh levels
HierarchicalInverseMapping(const HierarchicalInverseMapping &other)=delete
Copy constructor.
typename InvMapDataType::DomainPointType DomainPointType
the domain point type
Data structure for InverseMapping evaluations.
Tiny::Vector< DataType_, shape_dim_ > DomainPointType
the domain point type
InverseMappingData & operator=(InverseMappingData &&other)
move-assign operator
ImagePointType img_point
the image point that was unmapped
std::vector< Index > cells
the indices of the cells that intersect with the image point
InverseMappingData()
default constructor
InverseMappingData(const InverseMappingData &other)=default
use default copy constructor
std::vector< DomainPointType > dom_points
the domain points on each cell that map onto the image point
Tiny::Vector< DataType_, world_dim_ > ImagePointType
the image point type
InverseMappingData(InverseMappingData &&other)
move constructor
bool empty() const
Checks whether no cells were found.
Inverse Trafo Mapping Error class.
InverseMappingError(const Tiny::Vector< DT_, n_, s_ > &point, Index cell)
Constructor.
Inverse Trafo mapping class template.
std::vector< BBox > _bboxes
the array of cell bounding boxes
bool find_candidate_cells(std::vector< Index > &cells, const ImagePointType &img_point) const
Determines a set of candidate cells by a bounding-box test.
Index _newton_max_iter
maximum number of newton iterations for unmapping
void init_bounding_boxes(DataType bbox_tol)
Initializes the cell bounding boxes array.
Tiny::Matrix< DataType_, 2, world_dim > BBox
our bounding box type
static constexpr int shape_dim
the shape dimension
Trafo_ TrafoType
the underlying trafo type
bool unmap_point_by_newton(DomainPointType &dom_point, const ImagePointType &img_point, const Index cell) const
Unmaps a given point on a particular cell by Newton iteration.
DataType_ DataType
the datatype to be used
bool test_domain_point(const DomainPointType &dom_point) const
Tests whether a given domain point is on the reference element.
InverseMapping & operator=(InverseMapping &&other)=default
Move-assignment operator.
InverseMappingData< DataType, shape_dim, world_dim > InvMapDataType
the inverse mapping data type
InvMapDataType::DomainPointType DomainPointType
the domain point type
InverseMapping & operator=(const InverseMapping &other)=delete
Copy-assignment operator.
InverseMapping(const InverseMapping &other)=delete
Copy constructor.
InvMapDataType unmap_point(const ImagePointType &img_point, const std::vector< Index > &cells, bool ignore_failures=false) const
Unmaps a given image point.
InvMapDataType::ImagePointType ImagePointType
the image point type
InvMapDataType unmap_point(const ImagePointType &img_point, bool ignore_failures=false) const
Unmaps a given image point.
const TrafoType & get_trafo() const
Return a reference to the underlying trafo.
InverseMapping(const TrafoType &trafo, DataType bbox_tol=DataType(1E-2), DataType domain_tol=DataType(1E-4), bool init_boxes=true)
Constructor.
static constexpr int world_dim
the world dimension
InverseMapping(InverseMapping &&other)=default
Move constructor.
DataType _domain_tol
tolerance for unmapped domain coordinates
virtual ~InverseMapping()=default
virtual destructor
DataType _newton_tol
absolute tolerance for newton iteration
const TrafoType & _trafo
the trafo mapping
TrafoType::ShapeType ShapeType
the shape type
void minimax(T_ x, T_ &a, T_ &b)
Updates the minimum and maximum.
Definition: math.hpp:195
T_ sqr(T_ x)
Returns the square of a value.
Definition: math.hpp:95
FEAT namespace.
Definition: adjactor.hpp:12
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:993
std::uint64_t Index
Index data type.
TrafoTags
Trafo configuration tags enum.
Definition: eval_tags.hpp:22
@ img_point
specifies whether the trafo should supply image point coordinates
@ dom_point
specifies whether the trafo should supply domain point coordinates
@ jac_inv
specifies whether the trafo should supply inverse jacobian matrices
Reference cell traits structure.
Definition: shape.hpp:230