libMesh
tree_node.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 #ifndef LIBMESH_TREE_NODE_H
21 #define LIBMESH_TREE_NODE_H
22 
23 // Local includes
24 #include "libmesh/libmesh_common.h"
25 #include "libmesh/bounding_box.h"
26 #include "libmesh/point.h"
27 
28 // C++ includes
29 #include <cstddef>
30 #include <set>
31 #include <unordered_map>
32 #include <vector>
33 
34 namespace libMesh
35 {
36 
37 // Forward Declarations
38 class MeshBase;
39 class Node;
40 class Elem;
41 
52 template <unsigned int N>
53 class TreeNode
54 {
55 public:
61  TreeNode (const MeshBase & m,
62  unsigned int tbs,
63  const TreeNode<N> * p = nullptr);
64 
70  ~TreeNode ();
71 
76  bool is_root() const { return (parent == nullptr); }
77 
82  bool active() const { return children.empty(); }
83 
89  bool insert (const Node * nd);
90 
96  bool insert (const Elem * nd);
97 
102  void refine ();
103 
107  void set_bounding_box (const std::pair<Point, Point> & bbox);
108 
113  bool bounds_node (const Node * nd,
114  Real relative_tol = 0) const;
115 
120  bool bounds_point (const Point & p,
121  Real relative_tol = 0) const;
122 
126  unsigned int level () const;
127 
132  void print_nodes(std::ostream & out=libMesh::out) const;
133 
138  void print_elements(std::ostream & out=libMesh::out) const;
139 
143  void transform_nodes_to_elements (std::vector<std::vector<const Elem *>> & nodes_to_elem);
144 
148  void transform_nodes_to_elements (std::unordered_map<dof_id_type, std::vector<const Elem *>> & nodes_to_elem);
149 
154  unsigned int n_active_bins() const;
155 
160  const Elem * find_element (const Point & p,
161  const std::set<subdomain_id_type> * allowed_subdomains = nullptr,
162  Real relative_tol = TOLERANCE) const;
163 
170  void find_elements (const Point & p,
171  std::set<const Elem *> & candidate_elements,
172  const std::set<subdomain_id_type> * allowed_subdomains = nullptr,
173  Real relative_tol = TOLERANCE) const;
174 
175 private:
180  const Elem * find_element_in_children (const Point & p,
181  const std::set<subdomain_id_type> * allowed_subdomains,
182  Real relative_tol) const;
183 
188  void find_elements_in_children (const Point & p,
189  std::set<const Elem *> & candidate_elements,
190  const std::set<subdomain_id_type> * allowed_subdomains,
191  Real relative_tol) const;
192 
196  BoundingBox create_bounding_box (unsigned int c) const;
197 
201  const MeshBase & mesh;
202 
207 
212  std::vector<TreeNode<N> * > children;
213 
218 
222  std::vector<const Elem *> elements;
223 
227  std::vector<const Node *> nodes;
228 
233  const unsigned int tgt_bin_size;
234 
243 
248 };
249 
250 
251 
252 
253 
254 // ------------------------------------------------------------
255 // TreeNode class inline methods
256 template <unsigned int N>
257 inline
259  unsigned int tbs,
260  const TreeNode<N> * p) :
261  mesh (m),
262  parent (p),
263  tgt_bin_size (tbs),
264  target_bin_size_increase_level(10),
265  contains_ifems (false)
266 {
267  // libmesh_assert our children are empty, thus we are active.
268  libmesh_assert (children.empty());
269  libmesh_assert (this->active());
270 
271  // Reserve space for the nodes & elements
272  nodes.reserve (tgt_bin_size);
273  elements.reserve (tgt_bin_size);
274 }
275 
276 
277 
278 template <unsigned int N>
279 inline
281 {
282  // When we are destructed we must delete all of our
283  // children. They will thus delete their children,
284  // All the way down the line...
285  for (auto c : children)
286  delete c;
287 }
288 
289 
290 
291 template <unsigned int N>
292 inline
293 unsigned int TreeNode<N>::level () const
294 {
295  if (parent != nullptr)
296  return parent->level()+1;
297 
298  // if we have no parent, we are a level-0 box
299  return 0;
300 }
301 
302 
303 } // namespace libMesh
304 
305 
306 #endif // LIBMESH_TREE_NODE_H
libMesh::TreeNode::mesh
const MeshBase & mesh
Reference to the mesh.
Definition: tree_node.h:201
libMesh::dof_id_type
uint8_t dof_id_type
Definition: id_types.h:67
libMesh::TreeNode::elements
std::vector< const Elem * > elements
Pointers to the elements in this tree node.
Definition: tree_node.h:222
libMesh::TreeNode::nodes
std::vector< const Node * > nodes
The node numbers contained in this portion of the tree.
Definition: tree_node.h:227
libMesh::TreeNode::print_elements
void print_elements(std::ostream &out=libMesh::out) const
Prints the contents of the elements set if we are active.
Definition: tree_node.C:361
libMesh::TreeNode::find_elements
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
Fills candidate_elements with any elements containing the specified point p, optionally restricted to...
Definition: tree_node.C:536
libMesh::TreeNode::~TreeNode
~TreeNode()
Destructor.
Definition: tree_node.h:280
libMesh::TreeNode::create_bounding_box
BoundingBox create_bounding_box(unsigned int c) const
Constructs the bounding box for child c.
Definition: tree_node.C:225
libMesh::TreeNode::active
bool active() const
Definition: tree_node.h:82
libMesh::BoundingBox
Defines a Cartesian bounding box by the two corner extremum.
Definition: bounding_box.h:40
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::TreeNode::tgt_bin_size
const unsigned int tgt_bin_size
The maximum number of things we should store before refining ourself.
Definition: tree_node.h:233
libMesh::TOLERANCE
static const Real TOLERANCE
Definition: libmesh_common.h:128
libMesh::TreeNode::find_elements_in_children
void find_elements_in_children(const Point &p, std::set< const Elem * > &candidate_elements, const std::set< subdomain_id_type > *allowed_subdomains, Real relative_tol) const
Look for points in our children, optionally restricted to a set of allowed subdomains.
Definition: tree_node.C:616
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
libMesh::TreeNode
This class defines a node on a tree.
Definition: tree_node.h:53
libMesh::TreeNode::contains_ifems
bool contains_ifems
Does this node contain any infinite elements.
Definition: tree_node.h:247
libMesh::TreeNode::refine
void refine()
Refine the tree node into N children if it contains more than tol nodes.
Definition: tree_node.C:134
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::TreeNode::children
std::vector< TreeNode< N > * > children
Pointers to our children.
Definition: tree_node.h:212
libMesh::MeshBase
This is the MeshBase class.
Definition: mesh_base.h:78
libMesh::TreeNode::transform_nodes_to_elements
void transform_nodes_to_elements(std::vector< std::vector< const Elem * >> &nodes_to_elem)
Transforms node numbers to element pointers.
Definition: tree_node.C:380
libMesh::TreeNode::bounding_box
BoundingBox bounding_box
The Cartesian bounding box for the node.
Definition: tree_node.h:217
libMesh::TreeNode::insert
bool insert(const Node *nd)
Tries to insert Node nd into the TreeNode.
Definition: tree_node.C:36
libMesh::Point
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38
libMesh::Node
A Node is like a Point, but with more information.
Definition: node.h:52
libMesh::TreeNode::set_bounding_box
void set_bounding_box(const std::pair< Point, Point > &bbox)
Sets the bounding box;.
Definition: tree_node.C:179
libMesh::TreeNode::print_nodes
void print_nodes(std::ostream &out=libMesh::out) const
Prints the contents of the node_numbers vector if we are active.
Definition: tree_node.C:342
libMesh::TreeNode::target_bin_size_increase_level
unsigned int target_bin_size_increase_level
This specifies the refinement level beyond which we will scale up the target bin size in child TreeNo...
Definition: tree_node.h:242
libMesh::TreeNode::find_element_in_children
const Elem * find_element_in_children(const Point &p, const std::set< subdomain_id_type > *allowed_subdomains, Real relative_tol) const
Look for point p in our children, optionally restricted to a set of allowed subdomains.
Definition: tree_node.C:560
libMesh::TreeNode::bounds_point
bool bounds_point(const Point &p, Real relative_tol=0) const
Definition: tree_node.C:197
libMesh::TreeNode::level
unsigned int level() const
Definition: tree_node.h:293
libMesh::TreeNode::bounds_node
bool bounds_node(const Node *nd, Real relative_tol=0) const
Definition: tree_node.C:187
libMesh::TreeNode::find_element
const Elem * find_element(const Point &p, const std::set< subdomain_id_type > *allowed_subdomains=nullptr, Real relative_tol=TOLERANCE) const
Definition: tree_node.C:509
libMesh::Elem
This is the base class from which all geometric element types are derived.
Definition: elem.h:100
libMesh::TreeNode::n_active_bins
unsigned int n_active_bins() const
Definition: tree_node.C:489
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::TreeNode::TreeNode
TreeNode(const MeshBase &m, unsigned int tbs, const TreeNode< N > *p=nullptr)
Constructor.
Definition: tree_node.h:258
libMesh::out
OStreamProxy out
libMesh::TreeNode::parent
const TreeNode< N > * parent
Pointer to this node's parent.
Definition: tree_node.h:206
libMesh::TreeNode::is_root
bool is_root() const
Definition: tree_node.h:76