libMesh
Loading...
Searching...
No Matches
location_maps.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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/elem.h"
22#include "libmesh/libmesh_logging.h"
23#include "libmesh/location_maps.h"
24#include "libmesh/mesh_base.h"
25#include "libmesh/mesh_tools.h"
26#include "libmesh/node.h"
27#include "libmesh/parallel.h"
28
29// C++ Includes
30#include <limits>
31#include <utility>
32
33namespace
34{
35using libMesh::Real;
36
37// 10 bits per coordinate, to work with 32+ bit machines
38const unsigned int chunkmax = 1024;
39const Real chunkfloat = 1024.0;
40}
41
42
43
44namespace libMesh
45{
46
47template <typename T>
49{
50 // This function must be run on all processors at once
51 // for non-serial meshes
52 if (!mesh.is_serial())
53 libmesh_parallel_only(mesh.comm());
54
55 LOG_SCOPE("init()", "LocationMap");
56
57 // Clear the old map
58 _map.clear();
59
60 // Cache a bounding box
62
63 this->fill(mesh);
64}
65
66
67
68template <typename T>
70{
71 this->_map.emplace(this->key(this->point_of(t)), &t);
72}
73
74
75
76template <>
78{
79 return node;
80}
81
82
83
84template <>
86{
87 return elem.vertex_average();
88}
89
90
91
92template <typename T>
94 const Real tol)
95{
96 LOG_SCOPE("find()", "LocationMap");
97
98 // Look for a likely key in the multimap
99 unsigned int pointkey = this->key(p);
100
101 // Look for the exact key first
102 for (const auto & pr : as_range(_map.equal_range(pointkey)))
103 if (p.absolute_fuzzy_equals(this->point_of(*(pr.second)), tol))
104 return pr.second;
105
106 // Look for neighboring bins' keys next
107 for (int xoffset = -1; xoffset != 2; ++xoffset)
108 {
109 for (int yoffset = -1; yoffset != 2; ++yoffset)
110 {
111 for (int zoffset = -1; zoffset != 2; ++zoffset)
112 {
113 auto key_pos = _map.equal_range(pointkey +
114 xoffset*chunkmax*chunkmax +
115 yoffset*chunkmax +
116 zoffset);
117 for (const auto & pr : as_range(key_pos))
118 if (p.absolute_fuzzy_equals(this->point_of(*(pr.second)), tol))
119 return pr.second;
120 }
121 }
122 }
123
124 return nullptr;
125}
126
127
128
129template <typename T>
130unsigned int LocationMap<T>::key(const Point & p)
131{
132 Real xscaled = 0., yscaled = 0., zscaled = 0.;
133
134 const Real deltax = _bbox.max()(0) - _bbox.min()(0);
135
136 if (std::abs(deltax) > TOLERANCE)
137 xscaled = (p(0) - _bbox.min()(0))/deltax;
138
139 // Only check y-coords if libmesh is compiled with LIBMESH_DIM>1
140#if LIBMESH_DIM > 1
141 const Real deltay = _bbox.max()(1) - _bbox.min()(1);
142
143 if (std::abs(deltay) > TOLERANCE)
144 yscaled = (p(1) - _bbox.min()(1))/deltay;
145#endif
146
147 // Only check z-coords if libmesh is compiled with LIBMESH_DIM>2
148#if LIBMESH_DIM > 2
149 const Real deltaz = _bbox.max()(2) - _bbox.min()(2);
150
151 if (std::abs(deltaz) > TOLERANCE)
152 zscaled = (p(2) - _bbox.min()(2))/deltaz;
153#endif
154
155 unsigned int n0 = static_cast<unsigned int> (chunkfloat * xscaled),
156 n1 = static_cast<unsigned int> (chunkfloat * yscaled),
157 n2 = static_cast<unsigned int> (chunkfloat * zscaled);
158
159 return chunkmax*chunkmax*n0 + chunkmax*n1 + n2;
160}
161
162
163
164template <>
166{
167 // Populate the nodes map
168 for (auto & node : mesh.node_ptr_range())
169 this->insert(*node);
170}
171
172
173
174template <>
176{
177 // Populate the elem map
178 for (auto & elem : mesh.active_element_ptr_range())
179 this->insert(*elem);
180}
181
182
183
184template class LIBMESH_EXPORT LocationMap<Elem>;
185template class LIBMESH_EXPORT LocationMap<Node>;
186
187} // namespace libMesh
This is the base class from which all geometric element types are derived.
Definition elem.h:96
Point vertex_average() const
Definition elem.C:609
Data structures that enable location-based lookups The key is a hash of the Point location.
Point point_of(const T &) const
T * find(const Point &, const Real tol=TOLERANCE)
void init(MeshBase &)
unsigned int key(const Point &)
void fill(MeshBase &)
This is the MeshBase class.
Definition mesh_base.h:81
virtual bool is_serial() const
Definition mesh_base.h:365
A Node is like a Point, but with more information.
Definition node.h:55
const Parallel::Communicator & comm() const
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition point.h:41
bool absolute_fuzzy_equals(const TypeVector< T > &rhs, Real tol=TOLERANCE) const
MeshBase & mesh
libMesh::BoundingBox create_nodal_bounding_box(const MeshBase &mesh)
Definition mesh_tools.C:655
The libMesh namespace provides an interface to certain functionality in the library.
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
Helper function that allows us to treat a homogenous pair as a range.
static constexpr Real TOLERANCE
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real