Skip to content

Commit

Permalink
Support old Nanoflann API *and* new Nanoflann API
Browse files Browse the repository at this point in the history
This gets us around the chicken-and-egg problem of not being able to get
the Nanoflann update in libMesh to pass MOOSE tests.

Remember to build backwards compatibility into your library API updates,
kids!

We can remove the `namespace nanoflann` shims after the update makes it
into the libMesh submodule.

Refs #25904, but we can wait until the shims are gone before we close
that ticket.
  • Loading branch information
roystgnr committed Nov 3, 2023
1 parent 191b9de commit 445e4bf
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 14 deletions.
11 changes: 10 additions & 1 deletion framework/include/utils/KDTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
#include "libmesh/nanoflann.hpp"
#include "libmesh/utility.h"

// Make newer nanoflann API compatible with older nanoflann versions
#if NANOFLANN_VERSION < 0x150
namespace nanoflann
{
template <typename T, typename U>
using ResultItem = std::pair<T, U>;
}
#endif

class KDTree
{
public:
Expand All @@ -34,7 +43,7 @@ class KDTree

void radiusSearch(const Point & query_point,
Real radius,
std::vector<std::pair<std::size_t, Real>> & indices_dist);
std::vector<nanoflann::ResultItem<std::size_t, Real>> & indices_dist);

std::size_t numberCandidatePoints();

Expand Down
15 changes: 12 additions & 3 deletions framework/src/constraints/AutomaticMortarGeneration.C
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@
using namespace libMesh;
using MetaPhysicL::DualNumber;

// Make newer nanoflann API spelling compatible with older nanoflann
// versions
#if NANOFLANN_VERSION < 0x150
namespace nanoflann
{
typedef SearchParams SearchParameters;
}
#endif

class MortarNodalGeometryOutput : public Output
{
public:
Expand Down Expand Up @@ -1109,7 +1118,7 @@ AutomaticMortarGeneration::buildMortarSegmentMesh3d()
std::vector<Real> out_dist_sqr(num_results);
nanoflann::KNNResultSet<Real> result_set(num_results);
result_set.init(&ret_index[0], &out_dist_sqr[0]);
kd_tree.findNeighbors(result_set, &query_pt[0], nanoflann::SearchParams(10));
kd_tree.findNeighbors(result_set, &query_pt[0], nanoflann::SearchParameters(10));

// Initialize list of processed primary elements, we don't want to revisit processed elements
std::set<const Elem *, CompareDofObjectsByID> processed_primary_elems;
Expand Down Expand Up @@ -1918,7 +1927,7 @@ AutomaticMortarGeneration::projectSecondaryNodesSinglePair(
std::vector<Real> out_dist_sqr(num_results);
nanoflann::KNNResultSet<Real> result_set(num_results);
result_set.init(&ret_index[0], &out_dist_sqr[0]);
kd_tree.findNeighbors(result_set, &query_pt[0], nanoflann::SearchParams(10));
kd_tree.findNeighbors(result_set, &query_pt[0], nanoflann::SearchParameters(10));

// If this flag gets set in the loop below, we can break out of the outer r-loop as well.
bool projection_succeeded = false;
Expand Down Expand Up @@ -2217,7 +2226,7 @@ AutomaticMortarGeneration::projectPrimaryNodesSinglePair(
std::vector<Real> out_dist_sqr(num_results);
nanoflann::KNNResultSet<Real> result_set(num_results);
result_set.init(&ret_index[0], &out_dist_sqr[0]);
kd_tree.findNeighbors(result_set, &query_pt[0], nanoflann::SearchParams(10));
kd_tree.findNeighbors(result_set, &query_pt[0], nanoflann::SearchParameters(10));

// If this flag gets set in the loop below, we can break out of the outer r-loop as well.
bool projection_succeeded = false;
Expand Down
15 changes: 13 additions & 2 deletions framework/src/mesh/MooseMesh.C
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@
static const int GRAIN_SIZE =
1; // the grain_size does not have much influence on our execution speed

// Make newer nanoflann API compatible with older nanoflann versions
#if NANOFLANN_VERSION < 0x150
namespace nanoflann
{
typedef SearchParams SearchParameters;

template <typename T, typename U>
using ResultItem = std::pair<T, U>;
}
#endif

InputParameters
MooseMesh::validParams()
{
Expand Down Expand Up @@ -1586,8 +1597,8 @@ MooseMesh::buildPeriodicNodeMap(std::multimap<dof_id_type, dof_id_type> & period
kd_tree->buildIndex();

// data structures for kd-tree search
nanoflann::SearchParams search_params;
std::vector<std::pair<std::size_t, Real>> ret_matches;
nanoflann::SearchParameters search_params;
std::vector<nanoflann::ResultItem<std::size_t, Real>> ret_matches;

// iterate over periodic nodes (boundary ids are in contiguous blocks)
PeriodicBoundaryBase * periodic = nullptr;
Expand Down
15 changes: 13 additions & 2 deletions framework/src/userobjects/RadialAverage.C
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
#include <iterator>
#include <algorithm>

// Make newer nanoflann API compatible with older nanoflann versions
#if NANOFLANN_VERSION < 0x150
namespace nanoflann
{
typedef SearchParams SearchParameters;

template <typename T, typename U>
using ResultItem = std::pair<T, U>;
}
#endif

registerMooseObject("MooseApp", RadialAverage);

// specialization for PointListAdaptor<RadialAverage::QPData>
Expand Down Expand Up @@ -250,8 +261,8 @@ RadialAverage::updateCommunicationLists()
mooseAssert(kd_tree != nullptr, "KDTree was not properly initialized.");
kd_tree->buildIndex();

std::vector<std::pair<std::size_t, Real>> ret_matches;
nanoflann::SearchParams search_params;
std::vector<nanoflann::ResultItem<std::size_t, Real>> ret_matches;
nanoflann::SearchParameters search_params;

// iterate over all boundary nodes and collect all boundary-near data points
_boundary_data_indices.clear();
Expand Down
16 changes: 14 additions & 2 deletions framework/src/userobjects/ThreadedRadialAverageLoop.C
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
#include "ThreadedRadialAverageLoop.h"
#include "Function.h"

// Make newer nanoflann API spelling compatible with older nanoflann
// versions
#if NANOFLANN_VERSION < 0x150
namespace nanoflann
{
typedef SearchParams SearchParameters;

template <typename T, typename U>
using ResultItem = std::pair<T, U>;
}
#endif

ThreadedRadialAverageLoop::ThreadedRadialAverageLoop(RadialAverage & green) : _radavg(green) {}

// Splitting Constructor
Expand All @@ -29,8 +41,8 @@ ThreadedRadialAverageLoop::operator()(const QPDataRange & qpdata_range)
const auto & weights_type = _radavg._weights_type;

// tree search data structures
std::vector<std::pair<std::size_t, Real>> ret_matches;
nanoflann::SearchParams search_params;
std::vector<nanoflann::ResultItem<std::size_t, Real>> ret_matches;
nanoflann::SearchParameters search_params;

// result map entry
const auto end_it = _radavg._average.end();
Expand Down
15 changes: 13 additions & 2 deletions framework/src/utils/KDTree.C
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
#include "libmesh/nanoflann.hpp"
#include "libmesh/point.h"

// Make newer nanoflann API compatible with older nanoflann versions
#if NANOFLANN_VERSION < 0x150
namespace nanoflann
{
typedef SearchParams SearchParameters;

template <typename T, typename U>
using ResultItem = std::pair<T, U>;
}
#endif

KDTree::KDTree(std::vector<Point> & master_points, unsigned int max_leaf_size)
: _point_list_adaptor(master_points.begin(), master_points.end()),
_kd_tree(std::make_unique<KdTreeT>(
Expand Down Expand Up @@ -53,9 +64,9 @@ KDTree::neighborSearch(const Point & query_point,
void
KDTree::radiusSearch(const Point & query_point,
Real radius,
std::vector<std::pair<std::size_t, Real>> & indices_dist)
std::vector<nanoflann::ResultItem<std::size_t, Real>> & indices_dist)
{
nanoflann::SearchParams sp;
nanoflann::SearchParameters sp;
_kd_tree->radiusSearch(&query_point(0), radius * radius, indices_dist, sp);
}

Expand Down
15 changes: 13 additions & 2 deletions modules/contact/src/actions/ContactAction.C
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@
#include "libmesh/petsc_nonlinear_solver.h"
#include "libmesh/string_to_enum.h"

// Make newer nanoflann API compatible with older nanoflann versions
#if NANOFLANN_VERSION < 0x150
namespace nanoflann
{
typedef SearchParams SearchParameters;

template <typename T, typename U>
using ResultItem = std::pair<T, U>;
}
#endif

using NodeBoundaryIDInfo = std::pair<const Node *, BoundaryID>;

// Counter for naming mortar auxiliary kernels
Expand Down Expand Up @@ -1291,8 +1302,8 @@ ContactAction::createSidesetsFromNodeProximity()
kd_tree->buildIndex();

// data structures for kd-tree search
nanoflann::SearchParams search_params;
std::vector<std::pair<std::size_t, Real>> ret_matches;
nanoflann::SearchParameters search_params;
std::vector<nanoflann::ResultItem<std::size_t, Real>> ret_matches;

const auto radius_for_search = getParam<Real>("automatic_pairing_distance");

Expand Down

0 comments on commit 445e4bf

Please sign in to comment.