libMesh
tree.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 // C++ includes
21 
22 // Local includes
23 #include "libmesh/tree.h"
24 #include "libmesh/mesh_base.h"
25 #include "libmesh/mesh_tools.h"
26 
27 namespace libMesh
28 {
29 
30 
31 
32 // ------------------------------------------------------------
33 // Tree class method
34 
35 // constructor
36 template <unsigned int N>
38  unsigned int target_bin_size,
39  const Trees::BuildType bt) :
40  TreeBase(m),
41  root(m,target_bin_size),
42  build_type(bt)
43 {
44  // Set the root node bounding box equal to the bounding
45  // box for the entire domain.
46  root.set_bounding_box (MeshTools::create_bounding_box(mesh));
47 
48  if (build_type == Trees::NODES)
49  {
50  // Add all the nodes to the root node. It will
51  // automagically build the tree for us.
52  for (const auto & node : mesh.node_ptr_range())
53  {
54 #ifndef NDEBUG
55  bool node_was_inserted =
56 #endif
57  root.insert (node);
58  libmesh_assert(node_was_inserted);
59  }
60 
61  // Now the tree contains the nodes.
62  // However, we want element pointers, so here we
63  // convert between the two.
64  std::unordered_map<dof_id_type, std::vector<const Elem *>> nodes_to_elem;
65 
67  root.transform_nodes_to_elements (nodes_to_elem);
68  }
69 
70  else if (build_type == Trees::ELEMENTS)
71  {
72  // Add all active elements to the root node. It will
73  // automatically build the tree for us.
74  for (const auto & elem : mesh.active_element_ptr_range())
75  {
76 #ifndef NDEBUG
77  bool elem_was_inserted =
78 #endif
79  root.insert (elem);
80  libmesh_assert(elem_was_inserted);
81  }
82  }
83 
85  {
86  // Add all active, local elements to the root node. It will
87  // automatically build the tree for us.
88  for (const auto & elem : mesh.active_local_element_ptr_range())
89  {
90 #ifndef NDEBUG
91  bool elem_was_inserted =
92 #endif
93  root.insert (elem);
94  libmesh_assert(elem_was_inserted);
95  }
96  }
97 
98  else
99  libmesh_error_msg("Unknown build_type = " << build_type);
100 }
101 
102 
103 
104 template <unsigned int N>
105 void Tree<N>::print_nodes(std::ostream & my_out) const
106 {
107  my_out << "Printing nodes...\n";
108  root.print_nodes(my_out);
109 }
110 
111 
112 
113 template <unsigned int N>
114 void Tree<N>::print_elements(std::ostream & my_out) const
115 {
116  my_out << "Printing elements...\n";
117  root.print_elements(my_out);
118 }
119 
120 
121 
122 template <unsigned int N>
123 const Elem *
125  const std::set<subdomain_id_type> * allowed_subdomains,
126  Real relative_tol) const
127 {
128  return root.find_element(p, allowed_subdomains, relative_tol);
129 }
130 
131 
132 
133 template <unsigned int N>
134 void
136  std::set<const Elem *> & candidate_elements,
137  const std::set<subdomain_id_type> * allowed_subdomains,
138  Real relative_tol) const
139 {
140  return root.find_elements(p, candidate_elements, allowed_subdomains, relative_tol);
141 }
142 
143 
144 
145 template <unsigned int N>
146 const Elem *
148  const std::set<subdomain_id_type> * allowed_subdomains,
149  Real relative_tol) const
150 {
151  return this->find_element(p, allowed_subdomains, relative_tol);
152 }
153 
154 
155 // ------------------------------------------------------------
156 // Explicit Instantiations
157 template class LIBMESH_EXPORT Tree<2>;
158 template class LIBMESH_EXPORT Tree<4>;
159 template class LIBMESH_EXPORT Tree<8>;
160 
161 } // namespace libMesh
TreeNode< N > root
The tree root.
Definition: tree.h:114
virtual void print_nodes(std::ostream &my_out=libMesh::out) const override
Prints the nodes.
Definition: tree.C:105
BuildType
enum defining how to build the tree.
Definition: tree_base.h:58
const Elem * operator()(const Point &p, const std::set< subdomain_id_type > *allowed_subdomains=nullptr, Real relative_tol=TOLERANCE) const
Definition: tree.C:147
virtual const Elem * find_element(const Point &p, const std::set< subdomain_id_type > *allowed_subdomains=nullptr, Real relative_tol=TOLERANCE) const override
Definition: tree.C:124
This class defines a tree that may be used for fast point location in space.
Definition: tree.h:44
libMesh::BoundingBox create_bounding_box(const MeshBase &mesh)
Definition: mesh_tools.C:559
This is the base class from which all geometric element types are derived.
Definition: elem.h:94
void build_nodes_to_elem_map(const MeshBase &mesh, std::vector< std::vector< dof_id_type >> &nodes_to_elem_map)
After calling this function the input vector nodes_to_elem_map will contain the node to element conne...
Definition: mesh_tools.C:449
The libMesh namespace provides an interface to certain functionality in the library.
This is the MeshBase class.
Definition: mesh_base.h:75
virtual void find_elements(const Point &p, std::set< const Elem *> &candidate_elements, const std::set< subdomain_id_type > *allowed_subdomains=nullptr, Real relative_tol=TOLERANCE) const override
Adds to candidate_elements any elements containing the specified point p, optionally restricted to a ...
Definition: tree.C:135
const MeshBase & mesh
Constant reference to a mesh.
Definition: tree_base.h:124
virtual void print_elements(std::ostream &my_out=libMesh::out) const override
Prints the nodes.
Definition: tree.C:114
libmesh_assert(ctx)
Tree(const MeshBase &m, unsigned int target_bin_size, Trees::BuildType bt=Trees::NODES)
Constructor.
Definition: tree.C:37
This is the base class for trees, it allows pointer usage of trees.
Definition: tree_base.h:68
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39
const Trees::BuildType build_type
How the tree is built.
Definition: tree.h:119