libMesh::LocationMap< T > Class Template Reference

std::map-like data structure using hashed Points for keys. More...

#include <location_maps.h>

Public Member Functions

void init (MeshBase &)
 
void clear ()
 
void insert (T &)
 
bool empty () const
 
T * find (const Point &, const Real tol=TOLERANCE)
 
Point point_of (const T &) const
 
template<>
Point point_of (const Node &node) const
 
template<>
Point point_of (const Elem &elem) const
 

Protected Member Functions

unsigned int key (const Point &)
 
void fill (MeshBase &)
 
template<>
void fill (MeshBase &mesh)
 
template<>
void fill (MeshBase &mesh)
 

Private Types

typedef std::unordered_multimap< unsigned int, T * > map_type
 

Private Attributes

map_type _map
 
std::vector< Real_lower_bound
 
std::vector< Real_upper_bound
 

Detailed Description

template<typename T>
class libMesh::LocationMap< T >

std::map-like data structure using hashed Points for keys.

Data structures that enable location-based lookups The key is a hash of the Point location. For efficiency we will use a hashed multimap if it is available, otherwise a regular multimap.

Author
Roy Stogner
Date
2008

Definition at line 53 of file location_maps.h.

Member Typedef Documentation

◆ map_type

template<typename T>
typedef std::unordered_multimap<unsigned int, T *> libMesh::LocationMap< T >::map_type
private

Definition at line 55 of file location_maps.h.

Member Function Documentation

◆ clear()

template<typename T>
void libMesh::LocationMap< T >::clear ( )
inline

Definition at line 59 of file location_maps.h.

References libMesh::LocationMap< T >::_map.

59 { _map.clear(); }

◆ empty()

template<typename T>
bool libMesh::LocationMap< T >::empty ( ) const
inline

Definition at line 63 of file location_maps.h.

References libMesh::LocationMap< T >::_map.

Referenced by libMesh::Parallel::sync_dofobject_data_by_xyz().

63 { return _map.empty(); }

◆ fill() [1/3]

template<typename T>
void libMesh::LocationMap< T >::fill ( MeshBase )
protected

◆ fill() [2/3]

template<>
void libMesh::LocationMap< Node >::fill ( MeshBase mesh)
protected

Definition at line 183 of file location_maps.C.

References mesh, and libMesh::MeshBase::node_ptr_range().

184 {
185  // Populate the nodes map
186  for (auto & node : mesh.node_ptr_range())
187  this->insert(*node);
188 }
MeshBase & mesh

◆ fill() [3/3]

template<>
void libMesh::LocationMap< Elem >::fill ( MeshBase mesh)
protected

Definition at line 193 of file location_maps.C.

References libMesh::MeshBase::active_element_ptr_range(), and mesh.

194 {
195  // Populate the elem map
196  for (auto & elem : mesh.active_element_ptr_range())
197  this->insert(*elem);
198 }
MeshBase & mesh

◆ find()

template<typename T >
T * libMesh::LocationMap< T >::find ( const Point p,
const Real  tol = TOLERANCE 
)

Definition at line 111 of file location_maps.C.

References libMesh::TypeVector< T >::absolute_fuzzy_equals(), and libMesh::as_range().

Referenced by libMesh::Parallel::sync_dofobject_data_by_xyz().

113 {
114  LOG_SCOPE("find()", "LocationMap");
115 
116  // Look for a likely key in the multimap
117  unsigned int pointkey = this->key(p);
118 
119  // Look for the exact key first
120  for (const auto & pr : as_range(_map.equal_range(pointkey)))
121  if (p.absolute_fuzzy_equals(this->point_of(*(pr.second)), tol))
122  return pr.second;
123 
124  // Look for neighboring bins' keys next
125  for (int xoffset = -1; xoffset != 2; ++xoffset)
126  {
127  for (int yoffset = -1; yoffset != 2; ++yoffset)
128  {
129  for (int zoffset = -1; zoffset != 2; ++zoffset)
130  {
131  auto key_pos = _map.equal_range(pointkey +
132  xoffset*chunkmax*chunkmax +
133  yoffset*chunkmax +
134  zoffset);
135  for (const auto & pr : as_range(key_pos))
136  if (p.absolute_fuzzy_equals(this->point_of(*(pr.second)), tol))
137  return pr.second;
138  }
139  }
140  }
141 
142  return nullptr;
143 }
SimpleRange< I > as_range(const std::pair< I, I > &p)
Definition: simple_range.h:57
unsigned int key(const Point &)

◆ init()

template<typename T >
void libMesh::LocationMap< T >::init ( MeshBase mesh)

Definition at line 46 of file location_maps.C.

References libMesh::ParallelObject::comm(), libMesh::MeshBase::is_serial(), std::max(), libMesh::Parallel::Communicator::max(), mesh, std::min(), libMesh::Parallel::Communicator::min(), and libMesh::MeshBase::node_ptr_range().

47 {
48  // This function must be run on all processors at once
49  // for non-serial meshes
50  if (!mesh.is_serial())
51  libmesh_parallel_only(mesh.comm());
52 
53  LOG_SCOPE("init()", "LocationMap");
54 
55  // Clear the old map
56  _map.clear();
57 
58  // Cache a bounding box
59  _lower_bound.clear();
60  _lower_bound.resize(LIBMESH_DIM, std::numeric_limits<Real>::max());
61  _upper_bound.clear();
62  _upper_bound.resize(LIBMESH_DIM, -std::numeric_limits<Real>::max());
63 
64  for (auto & node : mesh.node_ptr_range())
65  for (unsigned int i=0; i != LIBMESH_DIM; ++i)
66  {
67  // Expand the bounding box if necessary
69  (*node)(i));
71  (*node)(i));
72  }
73 
74  // On a parallel mesh we might not yet have a full bounding box
75  if (!mesh.is_serial())
76  {
77  mesh.comm().min(_lower_bound);
78  mesh.comm().max(_upper_bound);
79  }
80 
81  this->fill(mesh);
82 }
MeshBase & mesh
std::vector< Real > _upper_bound
Definition: location_maps.h:78
long double max(long double a, double b)
void fill(MeshBase &)
std::vector< Real > _lower_bound
Definition: location_maps.h:77
long double min(long double a, double b)

◆ insert()

template<typename T >
void libMesh::LocationMap< T >::insert ( T &  t)

Definition at line 87 of file location_maps.C.

88 {
89  this->_map.insert(std::make_pair(this->key(this->point_of(t)), &t));
90 }
Point point_of(const T &) const
unsigned int key(const Point &)

◆ key()

template<typename T >
unsigned int libMesh::LocationMap< T >::key ( const Point p)
protected

Definition at line 148 of file location_maps.C.

References std::abs(), libMesh::Real, and libMesh::TOLERANCE.

149 {
150  Real xscaled = 0., yscaled = 0., zscaled = 0.;
151 
152  Real deltax = _upper_bound[0] - _lower_bound[0];
153 
154  if (std::abs(deltax) > TOLERANCE)
155  xscaled = (p(0) - _lower_bound[0])/deltax;
156 
157  // Only check y-coords if libmesh is compiled with LIBMESH_DIM>1
158 #if LIBMESH_DIM > 1
159  Real deltay = _upper_bound[1] - _lower_bound[1];
160 
161  if (std::abs(deltay) > TOLERANCE)
162  yscaled = (p(1) - _lower_bound[1])/deltay;
163 #endif
164 
165  // Only check z-coords if libmesh is compiled with LIBMESH_DIM>2
166 #if LIBMESH_DIM > 2
167  Real deltaz = _upper_bound[2] - _lower_bound[2];
168 
169  if (std::abs(deltaz) > TOLERANCE)
170  zscaled = (p(2) - _lower_bound[2])/deltaz;
171 #endif
172 
173  unsigned int n0 = static_cast<unsigned int> (chunkfloat * xscaled),
174  n1 = static_cast<unsigned int> (chunkfloat * yscaled),
175  n2 = static_cast<unsigned int> (chunkfloat * zscaled);
176 
177  return chunkmax*chunkmax*n0 + chunkmax*n1 + n2;
178 }
double abs(double a)
std::vector< Real > _upper_bound
Definition: location_maps.h:78
static const Real TOLERANCE
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::vector< Real > _lower_bound
Definition: location_maps.h:77

◆ point_of() [1/3]

template<typename T>
Point libMesh::LocationMap< T >::point_of ( const T &  ) const

◆ point_of() [2/3]

template<>
Point libMesh::LocationMap< Node >::point_of ( const Node node) const

Definition at line 95 of file location_maps.C.

96 {
97  return node;
98 }

◆ point_of() [3/3]

template<>
Point libMesh::LocationMap< Elem >::point_of ( const Elem elem) const

Definition at line 103 of file location_maps.C.

References libMesh::Elem::centroid().

104 {
105  return elem.centroid();
106 }

Member Data Documentation

◆ _lower_bound

template<typename T>
std::vector<Real> libMesh::LocationMap< T >::_lower_bound
private

Definition at line 77 of file location_maps.h.

◆ _map

template<typename T>
map_type libMesh::LocationMap< T >::_map
private

◆ _upper_bound

template<typename T>
std::vector<Real> libMesh::LocationMap< T >::_upper_bound
private

Definition at line 78 of file location_maps.h.


The documentation for this class was generated from the following files: