https://mooseframework.inl.gov
Loading...
Searching...
No Matches
NanoflannMeshAdaptor.h
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#pragma once
11
12// For MooseIndex
13#include "MooseTypes.h"
14
15#include "libmesh/libmesh_config.h"
16#include "libmesh/libmesh_common.h"
17#include "libmesh/mesh_base.h"
18#include "libmesh/point.h"
19#include "libmesh/elem.h"
20#ifdef LIBMESH_HAVE_NANOFLANN
21#include "libmesh/nanoflann.hpp"
22#else
23SORRY THIS APPLICATION REQUIRES NANOFLANN
24#endif
25
31template <unsigned int Dim>
33{
34private:
35 // Constant reference to the Mesh we are adapting for use in Nanoflann
36 const MeshBase & _mesh;
37
38public:
39 NanoflannMeshAdaptor(const MeshBase & mesh) : _mesh(mesh) {}
40
44 typedef Real coord_t;
45
49 inline size_t kdtree_get_point_count() const { return _mesh.n_nodes(); }
50
55 inline coord_t kdtree_distance(const coord_t * p1, const size_t idx_p2, size_t size) const
56 {
57 libmesh_assert_equal_to(size, Dim);
58
59 // Construct a libmesh Point object from the input coord_t. This
60 // assumes LIBMESH_DIM==3.
61 Point point1(p1[0], size > 1 ? p1[1] : 0., size > 2 ? p1[2] : 0.);
62
63 // Get the referred-to point from the Mesh
64 const Point & point2 = _mesh.point(idx_p2);
65
66 // Compute Euclidean distance, squared
67 return (point1 - point2).norm_sq();
68 }
69
73 inline coord_t kdtree_get_pt(const size_t idx, int dim) const
74 {
75 libmesh_assert_less(dim, (int)Dim);
76 libmesh_assert_less(idx, _mesh.n_nodes());
77 libmesh_assert_less(dim, 3);
78
79 return _mesh.point(idx)(dim);
80 }
81
86 template <class BBOX>
87 bool kdtree_get_bbox(BBOX & /* bb */) const
88 {
89 return false;
90 }
91};
92
93// Useful typedefs for working with NanoflannMeshAdaptors.
94
95// Declare a type templated on NanoflannMeshAdaptor
96typedef nanoflann::L2_Simple_Adaptor<Real, NanoflannMeshAdaptor<3>> adatper_t;
97
98// Declare a KDTree type based on NanoflannMeshAdaptor
99typedef nanoflann::KDTreeSingleIndexAdaptor<adatper_t, NanoflannMeshAdaptor<3>, 3> kd_tree_t;
100
108template <unsigned int Dim>
110{
111private:
112 // Constant reference to the Mesh we are adapting for use in Nanoflann
113 const MeshBase & _mesh;
114
115 // This could be generalized to a std::set of subodmain ids.
116 subdomain_id_type _sid;
117
118 // Indices of points that are attached to elements in the requested subdomain.
119 std::set<dof_id_type> _legal_point_indices;
120
121public:
122 NanoflannMeshSubdomainAdaptor(const MeshBase & mesh, subdomain_id_type s) : _mesh(mesh), _sid(s)
123 {
124 // Loop over the elements of the Mesh, for those in the requested
125 // subdomain, add its node ids to the _legal_point_indices set.
126 for (const auto & elem : _mesh.active_element_ptr_range())
127 if (elem->subdomain_id() == _sid)
128 for (MooseIndex(elem->n_vertices()) n = 0; n < elem->n_vertices(); ++n)
129 _legal_point_indices.insert(elem->node_id(n));
130 }
131
135 typedef Real coord_t;
136
140 inline size_t kdtree_get_point_count() const { return _mesh.n_nodes(); }
141
146 inline coord_t kdtree_distance(const coord_t * p1, const size_t idx_p2, size_t size) const
147 {
148 libmesh_assert_equal_to(size, Dim);
149
150 // If this is not a valid point, then return a "large" distance.
151 if (!_legal_point_indices.count(static_cast<dof_id_type>(idx_p2)))
152 return std::numeric_limits<coord_t>::max();
153
154 // Construct a libmesh Point object from the input coord_t. This
155 // assumes LIBMESH_DIM==3.
156 Point point1(p1[0], size > 1 ? p1[1] : 0., size > 2 ? p1[2] : 0.);
157
158 // Get the referred-to point from the Mesh
159 const Point & point2 = _mesh.point(idx_p2);
160
161 // Compute Euclidean distance, squared
162 return (point1 - point2).norm_sq();
163 }
164
168 inline coord_t kdtree_get_pt(const size_t idx, int dim) const
169 {
170 libmesh_assert_less(dim, (int)Dim);
171 libmesh_assert_less(idx, _mesh.n_nodes());
172 libmesh_assert_less(dim, 3);
173
174 // If this is not a valid point, then return a "large" distance.
175 if (!_legal_point_indices.count(static_cast<dof_id_type>(idx)))
176 return std::numeric_limits<coord_t>::max();
177
178 return _mesh.point(idx)(dim);
179 }
180
185 template <class BBOX>
186 bool kdtree_get_bbox(BBOX & /* bb */) const
187 {
188 return false;
189 }
190};
191
192// Useful typedefs for working with NanoflannMeshAdaptors.
193
194// Declare a type templated on NanoflannMeshAdaptor
195typedef nanoflann::L2_Simple_Adaptor<Real, NanoflannMeshSubdomainAdaptor<3>> subdomain_adatper_t;
196
197// Declare a KDTree type based on NanoflannMeshAdaptor
198typedef nanoflann::
199 KDTreeSingleIndexAdaptor<subdomain_adatper_t, NanoflannMeshSubdomainAdaptor<3>, 3>
nanoflann::KDTreeSingleIndexAdaptor< adatper_t, NanoflannMeshAdaptor< 3 >, 3 > kd_tree_t
nanoflann::KDTreeSingleIndexAdaptor< subdomain_adatper_t, NanoflannMeshSubdomainAdaptor< 3 >, 3 > subdomain_kd_tree_t
nanoflann::L2_Simple_Adaptor< Real, NanoflannMeshAdaptor< 3 > > adatper_t
nanoflann::L2_Simple_Adaptor< Real, NanoflannMeshSubdomainAdaptor< 3 > > subdomain_adatper_t
unsigned int dim
This allows us to adapt the MeshBase class for use with nanoflann.
size_t kdtree_get_point_count() const
Must return the number of data points.
NanoflannMeshAdaptor(const MeshBase &mesh)
coord_t kdtree_get_pt(const size_t idx, int dim) const
Returns the dim'th component of the idx'th point in the class.
coord_t kdtree_distance(const coord_t *p1, const size_t idx_p2, size_t size) const
Returns the distance between the vector "p1[0:size-1]" and the data point with index "idx_p2" stored ...
Real coord_t
libMesh Point coordinate type
bool kdtree_get_bbox(BBOX &) const
Optional bounding-box computation: return false to default to a standard bbox computation loop.
Special adaptor that works with subdomains of the Mesh.
NanoflannMeshSubdomainAdaptor(const MeshBase &mesh, subdomain_id_type s)
Real coord_t
libMesh Point coordinate type
coord_t kdtree_distance(const coord_t *p1, const size_t idx_p2, size_t size) const
Returns the distance between the vector "p1[0:size-1]" and the data point with index "idx_p2" stored ...
bool kdtree_get_bbox(BBOX &) const
Optional bounding-box computation: return false to default to a standard bbox computation loop.
size_t kdtree_get_point_count() const
Must return the number of data points.
std::set< dof_id_type > _legal_point_indices
coord_t kdtree_get_pt(const size_t idx, int dim) const
Returns the dim'th component of the idx'th point in the class.
MeshBase & mesh