tree_node.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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 
164 
165 private:
170  const Elem * find_element_in_children (const Point & p,
171  const std::set<subdomain_id_type> * allowed_subdomains,
172  Real relative_tol) const;
173 
177  BoundingBox create_bounding_box (unsigned int c) const;
178 
182  const MeshBase & mesh;
183 
188 
193  std::vector<TreeNode<N> * > children;
194 
199 
203  std::vector<const Elem *> elements;
204 
208  std::vector<const Node *> nodes;
209 
214  const unsigned int tgt_bin_size;
215 
224 
229 };
230 
231 
232 
233 
234 
235 // ------------------------------------------------------------
236 // TreeNode class inline methods
237 template <unsigned int N>
238 inline
240  unsigned int tbs,
241  const TreeNode<N> * p) :
242  mesh (m),
243  parent (p),
244  tgt_bin_size (tbs),
245  target_bin_size_increase_level(10),
246  contains_ifems (false)
247 {
248  // libmesh_assert our children are empty, thus we are active.
249  libmesh_assert (children.empty());
250  libmesh_assert (this->active());
251 
252  // Reserve space for the nodes & elements
253  nodes.reserve (tgt_bin_size);
254  elements.reserve (tgt_bin_size);
255 }
256 
257 
258 
259 template <unsigned int N>
260 inline
262 {
263  // When we are destructed we must delete all of our
264  // children. They will this delete their children,
265  // All the way down the line...
266  for (std::size_t c=0; c<children.size(); c++)
267  delete children[c];
268 }
269 
270 
271 
272 template <unsigned int N>
273 inline
274 unsigned int TreeNode<N>::level () const
275 {
276  if (parent != nullptr)
277  return parent->level()+1;
278 
279  // if we have no parent, we are a level-0 box
280  return 0;
281 }
282 
283 
284 } // namespace libMesh
285 
286 
287 #endif // LIBMESH_TREE_NODE_H
unsigned int target_bin_size_increase_level
Definition: tree_node.h:223
BoundingBox create_bounding_box(unsigned int c) const
Definition: tree_node.C:229
A geometric point in (x,y,z) space associated with a DOF.
Definition: node.h:52
const TreeNode< N > * parent
Definition: tree_node.h:187
bool bounds_point(const Point &p, Real relative_tol=0) const
Definition: tree_node.C:201
unsigned int n_active_bins() const
Definition: tree_node.C:503
The base class for all geometric element types.
Definition: elem.h:100
MeshBase & mesh
std::vector< const Node * > nodes
Definition: tree_node.h:208
void transform_nodes_to_elements(std::vector< std::vector< const Elem *>> &nodes_to_elem)
Definition: tree_node.C:388
bool bounds_node(const Node *nd, Real relative_tol=0) const
Definition: tree_node.C:191
bool active() const
Definition: tree_node.h:82
static const Real TOLERANCE
std::vector< const Elem * > elements
Definition: tree_node.h:203
Base class for Mesh.
Definition: mesh_base.h:77
bool insert(const Node *nd)
Definition: tree_node.C:36
const Elem * find_element_in_children(const Point &p, const std::set< subdomain_id_type > *allowed_subdomains, Real relative_tol) const
Definition: tree_node.C:550
const unsigned int tgt_bin_size
Definition: tree_node.h:214
BoundingBox bounding_box
Definition: tree_node.h:198
Base class for different Tree types.
Definition: tree_node.h:53
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void print_nodes(std::ostream &out=libMesh::out) const
Definition: tree_node.C:346
const MeshBase & mesh
Definition: tree_node.h:182
unsigned int level() const
Definition: tree_node.h:274
bool is_root() const
Definition: tree_node.h:76
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:523
void set_bounding_box(const std::pair< Point, Point > &bbox)
Definition: tree_node.C:183
OStreamProxy out(std::cout)
void print_elements(std::ostream &out=libMesh::out) const
Definition: tree_node.C:367
A geometric point in (x,y,z) space.
Definition: point.h:38
std::vector< TreeNode< N > *> children
Definition: tree_node.h:193
TreeNode(const MeshBase &m, unsigned int tbs, const TreeNode< N > *p=nullptr)
Definition: tree_node.h:239
uint8_t dof_id_type
Definition: id_types.h:64