Skip to content

Commit

Permalink
Appeased the clang-tidy gods.
Browse files Browse the repository at this point in the history
  • Loading branch information
tfteague committed Sep 30, 2024
1 parent c9d7fd6 commit 2d36aea
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 39 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ include_directories(
add_subdirectory(freud)

# enable compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON FORCE)
if(NOT WIN32)
file(CREATE_LINK "${CMAKE_BINARY_DIR}/compile_commands.json"
"${CMAKE_SOURCE_DIR}/compile_commands.json" SYMBOLIC)
Expand Down
16 changes: 11 additions & 5 deletions freud/cluster/Cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@
// This file is from the freud project, released under the BSD 3-Clause License.

#include <algorithm>
#include <cstddef>
#include <numeric>
#include <memory>
#include <vector>

#include "Cluster.h"
#include "ManagedArray.h"
#include "NeighborBond.h"
#include "NeighborComputeFunctional.h"
#include "NeighborList.h"
#include "NeighborQuery.h"
#include "dset/dset.h"

//! Finds clusters using a network of neighbors.
namespace freud { namespace cluster {

void Cluster::compute(const std::shared_ptr<locality::NeighborQuery> nq,
const std::shared_ptr<locality::NeighborList> nlist, const locality::QueryArgs& qargs,
void Cluster::compute(const std::shared_ptr<locality::NeighborQuery>& nq,
const std::shared_ptr<locality::NeighborList>& nlist, const locality::QueryArgs& qargs,
const unsigned int* keys)
{
const unsigned int num_points = nq->getNPoints();
Expand Down Expand Up @@ -43,7 +49,7 @@ void Cluster::compute(const std::shared_ptr<locality::NeighborQuery> nq,
m_num_clusters = 0;
for (size_t i = 0; i < num_points; i++)
{
size_t s = dj.find(i);
size_t const s = dj.find(i);

// Label this cluster if we haven't seen it yet.
if (cluster_label[s] == num_points)
Expand Down Expand Up @@ -79,8 +85,8 @@ void Cluster::compute(const std::shared_ptr<locality::NeighborQuery> nq,
*/
for (size_t i = 0; i < num_points; i++)
{
size_t s = dj.find(i);
size_t cluster_idx = cluster_reindex[cluster_label[s]];
size_t const s = dj.find(i);
size_t const cluster_idx = cluster_reindex[cluster_label[s]];
(*m_cluster_idx)[i] = cluster_idx;
unsigned int key = i;
if (keys != nullptr)
Expand Down
5 changes: 3 additions & 2 deletions freud/cluster/Cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
#ifndef CLUSTER_H
#define CLUSTER_H

#include <memory>
#include <cstddef>
#include <vector>

#include "ManagedArray.h"
#include "NeighborList.h"
#include "NeighborQuery.h"
#include "VectorMath.h"

/*! \file Cluster.h
\brief Routines for clustering points.
Expand Down Expand Up @@ -44,7 +45,7 @@ class Cluster
Cluster() = default;

//! Compute the point clusters.
void compute(std::shared_ptr<locality::NeighborQuery> nq, std::shared_ptr<locality::NeighborList> nlist,
void compute(const std::shared_ptr<locality::NeighborQuery>& nq, const std::shared_ptr<locality::NeighborList>& nlist,
const locality::QueryArgs& qargs, const unsigned int* keys = nullptr);
//! Get the total number of clusters.
unsigned int getNumClusters() const
Expand Down
27 changes: 16 additions & 11 deletions freud/cluster/ClusterProperties.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// Copyright (c) 2010-2024 The Regents of the University of Michigan
// This file is from the freud project, released under the BSD 3-Clause License.

#include <memory>
#include <algorithm>
#include <cstddef>
#include <vector>

#include "ClusterProperties.h"
#include "NeighborComputeFunctional.h"
#include "ManagedArray.h"
#include "NeighborQuery.h"
#include "VectorMath.h"

/*! \file ClusterProperties.cc
\brief Routines for computing properties of point clusters.
Expand All @@ -21,7 +26,7 @@ namespace freud { namespace cluster {
getClusterInertiaMoments().
*/

void ClusterProperties::compute(std::shared_ptr<locality::NeighborQuery> nq, const unsigned int* cluster_idx,
void ClusterProperties::compute(const std::shared_ptr<locality::NeighborQuery>& nq, const unsigned int* cluster_idx,
const float* masses)
{
// determine the number of clusters
Expand Down Expand Up @@ -51,7 +56,7 @@ void ClusterProperties::compute(std::shared_ptr<locality::NeighborQuery> nq, con
const unsigned int c = cluster_idx[i];
cluster_points[c].push_back((*nq)[i]);
(*m_cluster_sizes)[c]++;
float mass = (masses != nullptr) ? masses[i] : float(1.0);
float const mass = (masses != nullptr) ? masses[i] : float(1.0);
(*m_cluster_masses)[c] += mass;
}

Expand All @@ -76,25 +81,25 @@ void ClusterProperties::compute(std::shared_ptr<locality::NeighborQuery> nq, con
// up the moment of inertia tensor. This has to be done in a loop over the points.
for (unsigned int i = 0; i < nq->getNPoints(); i++)
{
float mass = (masses != nullptr) ? masses[i] : float(1.0);
unsigned int c = cluster_idx[i];
vec3<float> pos = (*nq)[i];
vec3<float> mass_delta = nq->getBox().wrap(pos - (*m_cluster_centers_of_mass)[c]);
vec3<float> delta = nq->getBox().wrap(pos - (*m_cluster_centers)[c]);
float const mass = (masses != nullptr) ? masses[i] : float(1.0);
unsigned int const c = cluster_idx[i];
vec3<float> const pos = (*nq)[i];
vec3<float> const mass_delta = nq->getBox().wrap(pos - (*m_cluster_centers_of_mass)[c]);
vec3<float> const delta = nq->getBox().wrap(pos - (*m_cluster_centers)[c]);

// get the start pointer for our 3x3 matrix
(*m_cluster_moments_of_inertia)(c, 0, 0)
+= (std::pow(mass_delta.y, 2) + std::pow(mass_delta.z, 2)) * mass;
+= (mass_delta.y * mass_delta.y + mass_delta.z * mass_delta.z) * mass;
(*m_cluster_moments_of_inertia)(c, 0, 1) -= mass_delta.x * mass_delta.y * mass;
(*m_cluster_moments_of_inertia)(c, 0, 2) -= mass_delta.x * mass_delta.z * mass;
(*m_cluster_moments_of_inertia)(c, 1, 0) -= mass_delta.y * mass_delta.x * mass;
(*m_cluster_moments_of_inertia)(c, 1, 1)
+= (std::pow(mass_delta.x, 2) + std::pow(mass_delta.z, 2)) * mass;
+= (mass_delta.x * mass_delta.x + mass_delta.z * mass_delta.z) * mass;
(*m_cluster_moments_of_inertia)(c, 1, 2) -= mass_delta.y * mass_delta.z * mass;
(*m_cluster_moments_of_inertia)(c, 2, 0) -= mass_delta.z * mass_delta.x * mass;
(*m_cluster_moments_of_inertia)(c, 2, 1) -= mass_delta.z * mass_delta.y * mass;
(*m_cluster_moments_of_inertia)(c, 2, 2)
+= (std::pow(mass_delta.x, 2) + std::pow(mass_delta.y, 2)) * mass;
+= (mass_delta.x * mass_delta.x + mass_delta.y * mass_delta.y) * mass;

// get the start pointer for our 3x3 matrix
(*m_cluster_gyrations)(c, 0, 0) += delta.x * delta.x;
Expand Down
4 changes: 3 additions & 1 deletion freud/cluster/ClusterProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#ifndef CLUSTER_PROPERTIES_H
#define CLUSTER_PROPERTIES_H

#include <memory>
#include "VectorMath.h"
#include "ManagedArray.h"
#include "NeighborQuery.h"

Expand Down Expand Up @@ -36,7 +38,7 @@ class ClusterProperties
ClusterProperties() = default;

//! Compute properties of the point clusters
void compute(const std::shared_ptr<locality::NeighborQuery> nq, const unsigned int* cluster_idx,
void compute(const std::shared_ptr<locality::NeighborQuery>& nq, const unsigned int* cluster_idx,
const float* masses = nullptr);

//! Get a reference to the last computed cluster centers
Expand Down
18 changes: 10 additions & 8 deletions freud/cluster/export-Cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,36 @@
#include <nanobind/nanobind.h>
#include <nanobind/ndarray.h>
#include <nanobind/stl/shared_ptr.h> // NOLINT(misc-include-cleaner): used implicitly
#include <nanobind/stl/vector.h>
#include <nanobind/stl/vector.h> // NOLINT(misc-include-cleaner): used implicitly

#include <utility>

#include "Cluster.h"
#include "NeighborQuery.h"
#include "NeighborList.h"

namespace nb = nanobind;

namespace freud { namespace cluster {
template<typename T, typename shape>
using nb_array = nanobind::ndarray<T, shape, nanobind::device::cpu, nanobind::c_contig>;

namespace wrap {
void compute(std::shared_ptr<Cluster> self, std::shared_ptr<locality::NeighborQuery> nq,
std::shared_ptr<locality::NeighborList> nlist, const locality::QueryArgs& qargs,
nb_array<const unsigned int, nanobind::shape<-1>> keys)
namespace {
void compute(const std::shared_ptr<Cluster>& self,const std::shared_ptr<locality::NeighborQuery>& nq,
std::shared_ptr<locality::NeighborList>& nlist, const locality::QueryArgs& qargs,
const nb_array<const unsigned int, nanobind::shape<-1>>& keys)
{
const auto* keys_data = reinterpret_cast<const unsigned int*>(keys.data());
self->compute(nq, nlist, qargs, keys_data);
}
}; // end namespace wrap
}; // end anonymous namespace

namespace detail {
// NOLINTNEXTLINE(misc-use-internal-linkage)
void export_Cluster(nb::module_& module)
{
nanobind::class_<Cluster>(module, "Cluster")
.def(nb::init<>())
.def("compute", &wrap::compute, nanobind::arg("nq"), nanobind::arg("nlist").none(),
.def("compute", &compute, nanobind::arg("nq"), nanobind::arg("nlist").none(),
nanobind::arg("qargs"), nanobind::arg("keys").none())
.def("getNumClusters", &Cluster::getNumClusters)
.def("getClusterIdx", &Cluster::getClusterIdx)
Expand Down
17 changes: 9 additions & 8 deletions freud/cluster/export-ClusterProperties.cc
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
// Copyright (c) 2010-2024 The Regents of the University of Michigan
// This file is from the freud project, released under the BSD 3-Clause License.

#include <cstdint>
#include <memory>
#include <nanobind/nanobind.h>
#include <nanobind/ndarray.h>
#include <nanobind/stl/shared_ptr.h> // NOLINT(misc-include-cleaner): used implicitly
#include <nanobind/stl/vector.h>

#include <utility>

#include "ClusterProperties.h"
#include "NeighborQuery.h"

namespace nb = nanobind;

namespace freud { namespace cluster {
template<typename T, typename shape>
using nb_array = nanobind::ndarray<T, shape, nanobind::device::cpu, nanobind::c_contig>;

namespace wrap {
void compute(std::shared_ptr<ClusterProperties> self, std::shared_ptr<locality::NeighborQuery> nq,
nb_array<const uint32_t, nanobind::shape<-1>> cluster_idx,
nb_array<const float, nanobind::shape<-1>> masses)
namespace {
void compute(const std::shared_ptr<ClusterProperties>& self, const std::shared_ptr<locality::NeighborQuery>& nq,
const nb_array<const uint32_t, nanobind::shape<-1>>& cluster_idx,
const nb_array<const float, nanobind::shape<-1>>& masses)
{
const auto* masses_data = reinterpret_cast<const float*>(masses.data());
const auto* cluster_idx_data = reinterpret_cast<const uint32_t*>(cluster_idx.data());
self->compute(nq, cluster_idx_data, masses_data);
}
}; // end namespace wrap
}; // end anonymous namespace

namespace detail {
// NOLINTNEXTLINE(misc-use-internal-linkage)
void export_ClusterProperties(nb::module_& module)
{
nanobind::class_<ClusterProperties>(module, "ClusterProperties")
.def(nb::init<>())
.def("compute", &wrap::compute, nanobind::arg("nq"), nanobind::arg("cluster_idx"),
.def("compute", &compute, nanobind::arg("nq"), nanobind::arg("cluster_idx"),
nanobind::arg("masses_data").none())
.def("getClusterCenters", &ClusterProperties::getClusterCenters)
.def("getClusterCentersOfMass", &ClusterProperties::getClusterCentersOfMass)
Expand Down
7 changes: 4 additions & 3 deletions freud/cluster/module-Cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
#include <nanobind/nanobind.h>
#include <nanobind/nb_defs.h>

#include "NeighborQuery.h"

namespace nb = nanobind;

namespace freud::cluster::detail {
// NOLINTBEGIN(misc-use-internal-linkage)
void export_Cluster(nb::module_& module);
void export_ClusterProperties(nb::module_& module);
// NOLINTEND(misc-use-internal-linkage)
} // namespace freud::cluster::detail

using namespace freud::cluster::detail;
using namespace freud::cluster::detail;

NB_MODULE(_cluster, module)
NB_MODULE(_cluster, module) // NOLINT(misc-use-anonymous-namespace): caused by nanobind
{
export_Cluster(module);
export_ClusterProperties(module);
Expand Down

0 comments on commit 2d36aea

Please sign in to comment.