libMesh
point_locator_base.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 // Local Includes
21 #include "libmesh/point_locator_base.h"
22 #include "libmesh/point_locator_tree.h"
23 #include "libmesh/elem.h"
24 #include "libmesh/enum_point_locator_type.h"
25 #include "libmesh/point_locator_nanoflann.h"
26 
27 namespace libMesh
28 {
29 
30 
31 
32 
33 //------------------------------------------------------------------
34 // PointLocatorBase methods
36  const PointLocatorBase * master) :
37  _verbose (false),
38  _master (master),
39  _mesh (mesh),
40  _initialized (false),
41  _use_close_to_point_tol (false),
42  _close_to_point_tol (TOLERANCE),
43  _use_contains_point_tol (false),
44  _contains_point_tol (TOLERANCE)
45 {
46  // If we have a non-nullptr master, inherit its close-to-point tolerances.
47  if (_master)
48  {
51  }
52 }
53 
54 
55 
57 
58 
59 
61 {
62  return this->_initialized;
63 }
64 
65 
66 
67 std::unique_ptr<PointLocatorBase> PointLocatorBase::build (PointLocatorType t,
68  const MeshBase & mesh,
69  const PointLocatorBase * master)
70 {
71  switch (t)
72  {
73  case TREE:
74  return std::make_unique<PointLocatorTree>(mesh, /*Trees::NODES,*/ master);
75 
76  case TREE_ELEMENTS:
77  return std::make_unique<PointLocatorTree>(mesh, Trees::ELEMENTS, master);
78 
80  return std::make_unique<PointLocatorTree>(mesh, Trees::LOCAL_ELEMENTS, master);
81 
82 #ifdef LIBMESH_HAVE_NANOFLANN
83  case NANOFLANN:
84  return std::make_unique<PointLocatorNanoflann>(mesh, master);
85 #endif
86 
87  default:
88  libmesh_error_msg("ERROR: Bad PointLocatorType = " << t);
89  }
90 }
91 
93 {
94  return _close_to_point_tol;
95 }
96 
97 
99 {
101  _close_to_point_tol = close_to_point_tol;
102 }
103 
105 {
106  _use_close_to_point_tol = false;
108 }
109 
111 {
113  _contains_point_tol = contains_point_tol;
114 }
115 
117 {
118  _use_contains_point_tol = false;
120 }
121 
123 {
124  return _contains_point_tol;
125 }
126 
128 {
129  return _mesh;
130 }
131 
132 
133 const Node *
135 locate_node(const Point & p,
136  const std::set<subdomain_id_type> * allowed_subdomains,
137  Real tol) const
138 {
139  std::set<const Elem *> candidate_elements;
140  this->operator()(p, candidate_elements, allowed_subdomains);
141 
142  for (const auto & elem : candidate_elements)
143  {
144  const int elem_n_nodes = elem->n_nodes();
145  const Real hmax = elem->hmax();
146  const Real dist_tol_sq = (tol * hmax) * (tol * hmax);
147 
148  for (int n=0; n != elem_n_nodes; ++n)
149  if ((elem->point(n) - p).norm_sq() < dist_tol_sq)
150  return elem->node_ptr(n);
151  }
152 
153  return nullptr;
154 }
155 
156 } // namespace libMesh
virtual ~PointLocatorBase()
Destructor.
A Node is like a Point, but with more information.
Definition: node.h:52
virtual void set_close_to_point_tol(Real close_to_point_tol)
Set a tolerance to use when determining if a point is contained within the mesh.
bool _use_close_to_point_tol
true if we will use a user-specified tolerance for locating the element in an exhaustive search...
static constexpr Real TOLERANCE
Real _close_to_point_tol
The tolerance to use.
virtual Real get_contains_point_tol() const
Get the tolerance for determining element containment in the point locator.
bool _initialized
true when properly initialized, false otherwise.
MeshBase & mesh
virtual void set_contains_point_tol(Real contains_point_tol)
Set a tolerance to use when checking if a point is within an element in the mesh. ...
The libMesh namespace provides an interface to certain functionality in the library.
Real get_close_to_point_tol() const
Get the close-to-point tolerance.
This is the MeshBase class.
Definition: mesh_base.h:75
const MeshBase & _mesh
constant reference to the mesh in which the point is looked for.
This is the base class for point locators.
virtual void unset_contains_point_tol()
Specify that we do not want to use a user-specified tolerance to determine if a point is inside an el...
virtual const Node * locate_node(const Point &p, const std::set< subdomain_id_type > *allowed_subdomains=nullptr, Real tol=TOLERANCE) const
PointLocatorBase(const MeshBase &mesh, const PointLocatorBase *master)
Constructor.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
auto norm_sq(const T &a) -> decltype(std::norm(a))
Definition: tensor_tools.h:104
virtual const Elem * operator()(const Point &p, const std::set< subdomain_id_type > *allowed_subdomains=nullptr) const =0
Locates the element in which the point with global coordinates p is located.
const PointLocatorBase * _master
Const pointer to our master, initialized to nullptr if none given.
PointLocatorType
defines an enum for the types of point locators (given a point with global coordinates, locate the corresponding element in space) available in libMesh.
virtual void unset_close_to_point_tol()
Specify that we do not want to use a user-specified tolerance to determine if a point is contained wi...
static std::unique_ptr< PointLocatorBase > build(PointLocatorType t, const MeshBase &mesh, const PointLocatorBase *master=nullptr)
Builds an PointLocator for the mesh mesh.
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39
bool _use_contains_point_tol
true if we will use a user-specified tolerance for locating the element.
const MeshBase & get_mesh() const
Get a const reference to this PointLocator&#39;s mesh.
Real _contains_point_tol
The tolerance to use when locating an element in the tree.