Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store and re-use computed neighbor vectors. #738

Merged
merged 48 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
3604b44
Make vec3, vec2, quat constexpr-qualified (also constexpr implies inl…
bdice Mar 26, 2021
9192f71
Add vectors to NeighborBond, NeighborList.
bdice Mar 26, 2021
e0db071
Store neighbor vectors in all neighbor bonds, use this data instead o…
bdice Mar 28, 2021
8edb406
Ignore E402 because flake8 doesn't like some changes made by isort; I…
bdice Mar 28, 2021
0d59ffc
Use const& as suggested by clang-tidy.
bdice Mar 28, 2021
4e80ea2
Fix docstring.
bdice Mar 29, 2021
1c07f7d
Use indexing for weights instead of making a new array.
bdice Mar 29, 2021
b2b2f58
Don't compute bond variable twice.
bdice Mar 29, 2021
37c54a9
Merge branch 'master' into feature/neighbor-vectors
bdice Mar 29, 2021
c9d67b1
Remove unused NeighborQuery parameter.
bdice Mar 29, 2021
593ed2a
Use true Voronoi vectors (not wrapped) for neighbor list, update test…
bdice Mar 30, 2021
f6be4bb
Bump error tolerances a bit. Typical errors in the tests seem around …
bdice Mar 30, 2021
3b93604
Use vector on Voronoi facet most aligned with normal, remove incorrec…
bdice Mar 30, 2021
f22e3f4
Add test for small unit cell (diamond) with non-zero bond vectors.
bdice Mar 30, 2021
559469d
Merge remote-tracking branch 'origin/master' into feature/neighbor-ve…
bdice Jul 14, 2021
e380c74
Merge remote-tracking branch 'origin/master' into feature/neighbor-ve…
bdice Jul 10, 2022
7c1a938
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 10, 2022
33ee9b9
merge branch next into feature/neighbor-vectors
tommy-waltmann Aug 25, 2022
e3ec3a9
bump tolerance on minkowski test
tommy-waltmann Sep 15, 2022
3772b4d
remove unused header
tommy-waltmann Sep 15, 2022
84d0a6c
fix precision issue
tommy-waltmann Sep 26, 2022
089ed25
fix binning issue
tommy-waltmann Sep 26, 2022
c95c73d
Merge branch 'next' into feature/neighbor-vectors
tommy-waltmann Oct 12, 2022
248cb94
update migration guide
tommy-waltmann Oct 12, 2022
066ba40
change neighborbond to a struct
tommy-waltmann Nov 4, 2022
e663a76
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 4, 2022
92e5959
update neighborlist method
tommy-waltmann Nov 4, 2022
a353cba
Merge branch 'feature/neighbor-vectors' of github.com:glotzerlab/freu…
tommy-waltmann Nov 7, 2022
7718cba
add data protections for neighborbond class
tommy-waltmann Nov 7, 2022
e40bebb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 7, 2022
8c4fb1a
update NeighborBond constructor usage
tommy-waltmann Nov 7, 2022
76824b9
linting
tommy-waltmann Nov 7, 2022
5665429
linting
tommy-waltmann Nov 7, 2022
c1f3024
localize usage of computing distances to neighborbond
tommy-waltmann Nov 7, 2022
7c5ba9e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 7, 2022
bf4ee41
Apply suggestions from code review
tommy-waltmann Nov 8, 2022
c8f9f52
revert precision changes
tommy-waltmann Nov 8, 2022
05d5c7c
Merge branch 'feature/neighbor-vectors' of github.com:glotzerlab/freu…
tommy-waltmann Nov 8, 2022
82b89cc
fix narrowing conversion
tommy-waltmann Nov 8, 2022
a9da67a
update precision
tommy-waltmann Nov 8, 2022
5198ce2
refactor repeated test check
tommy-waltmann Nov 9, 2022
4ea3e94
update docstring example
tommy-waltmann Nov 9, 2022
a695846
linting
tommy-waltmann Nov 9, 2022
d10307c
Merge branch 'next' into feature/neighbor-vectors
tommy-waltmann Nov 11, 2022
34f8df6
Apply suggestions from code review
tommy-waltmann Nov 28, 2022
9272fba
fix setter names
tommy-waltmann Nov 28, 2022
360e839
merge branch next into feature/neighbor-vectors
tommy-waltmann Nov 28, 2022
a7ef0df
update credits and changelog
tommy-waltmann Nov 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cpp/cluster/Cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ void Cluster::compute(const freud::locality::NeighborQuery* nq, const freud::loc
nq, nq->getPoints(), num_points, qargs, nlist,
[&dj](const freud::locality::NeighborBond& neighbor_bond) {
// Merge the two sets using the disjoint set
if (!dj.same(neighbor_bond.point_idx, neighbor_bond.query_point_idx))
if (!dj.same(neighbor_bond.getPointIdx(), neighbor_bond.getQueryPointIdx()))
{
dj.unite(neighbor_bond.point_idx, neighbor_bond.query_point_idx);
dj.unite(neighbor_bond.getPointIdx(), neighbor_bond.getQueryPointIdx());
}
});

Expand Down
4 changes: 2 additions & 2 deletions cpp/density/CorrelationFunction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ void CorrelationFunction<T>::accumulate(const freud::locality::NeighborQuery* ne
accumulateGeneral(
neighbor_query, query_points, n_query_points, nlist, qargs,
[&](const freud::locality::NeighborBond& neighbor_bond) {
size_t value_bin = m_histogram.bin({neighbor_bond.distance});
size_t value_bin = m_histogram.bin({neighbor_bond.getDistance()});
m_local_histograms.increment(value_bin);
m_local_correlation_function.increment(
value_bin,
product(values[neighbor_bond.point_idx], query_values[neighbor_bond.query_point_idx]));
product(values[neighbor_bond.getPointIdx()], query_values[neighbor_bond.getQueryPointIdx()]));
});
}

Expand Down
4 changes: 2 additions & 2 deletions cpp/density/LocalDensity.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void LocalDensity::compute(const freud::locality::NeighborQuery* neighbor_query,
for (freud::locality::NeighborBond nb = ppiter->next(); !ppiter->end(); nb = ppiter->next())
{
// count particles that are fully in the r_max sphere
if (nb.distance < (m_r_max - m_diameter / float(2.0)))
if (nb.getDistance() < (m_r_max - m_diameter / float(2.0)))
{
num_neighbors += float(1.0);
}
Expand All @@ -54,7 +54,7 @@ void LocalDensity::compute(const freud::locality::NeighborQuery* neighbor_query,
// lots of them. It smooths out the neighbor count distributions and avoids noisy spikes
// that obscure data
num_neighbors
+= float(1.0) + (m_r_max - (nb.distance + m_diameter / float(2.0))) / m_diameter;
+= float(1.0) + (m_r_max - (nb.getDistance() + m_diameter / float(2.0))) / m_diameter;
}
m_num_neighbors_array[i] = num_neighbors;
if (m_box.is2D())
Expand Down
2 changes: 1 addition & 1 deletion cpp/density/RDF.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void RDF::accumulate(const freud::locality::NeighborQuery* neighbor_query, const
{
accumulateGeneral(neighbor_query, query_points, n_query_points, nlist, qargs,
[&](const freud::locality::NeighborBond& neighbor_bond) {
m_local_histograms(neighbor_bond.distance);
m_local_histograms(neighbor_bond.getDistance());
});
}

Expand Down
6 changes: 3 additions & 3 deletions cpp/environment/BondOrder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ void BondOrder::accumulate(const locality::NeighborQuery* neighbor_query, quat<f
{
accumulateGeneral(neighbor_query, query_points, n_query_points, nlist, qargs,
[&](const freud::locality::NeighborBond& neighbor_bond) {
const quat<float>& ref_q(orientations[neighbor_bond.point_idx]);
vec3<float> v(bondVector(neighbor_bond, neighbor_query, query_points));
const quat<float>& q = query_orientations[neighbor_bond.query_point_idx];
const quat<float>& ref_q(orientations[neighbor_bond.getPointIdx()]);
vec3<float> v(neighbor_bond.getVector());
const quat<float>& q(query_orientations[neighbor_bond.getQueryPointIdx()]);
if (m_mode == obcd)
{
// give bond directions of neighboring particles rotated by the matrix
Expand Down
5 changes: 3 additions & 2 deletions cpp/environment/LocalBondProjection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ void LocalBondProjection::compute(const locality::NeighborQuery* nq, const quat<
const size_t j(m_nlist.getNeighbors()(bond, 1));

// compute bond vector between the two particles
vec3<float> local_bond(bondVector(locality::NeighborBond(i, j), nq, query_points));
vec3<float> local_bond(m_nlist.getVectors()[bond]);

// rotate bond vector into the local frame of particle p
local_bond = rotate(conj(orientations[j]), local_bond);
// store the length of this local bond
float local_bond_len = std::sqrt(dot(local_bond, local_bond));
float local_bond_len = m_nlist.getDistances()[bond];

for (unsigned int k = 0; k < n_proj; k++)
{
Expand Down
10 changes: 3 additions & 7 deletions cpp/environment/LocalDescriptors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ void LocalDescriptors::compute(const locality::NeighborQuery* nq, const vec3<flo
&& m_nlist.getNeighbors()(bond_copy, 0) == i && neighbor_count < max_num_neighbors;
++bond_copy, ++neighbor_count)
{
const size_t j(m_nlist.getNeighbors()(bond_copy, 1));
const vec3<float> r_ij(bondVector(locality::NeighborBond(i, j), nq, query_points));
const vec3<float> r_ij(m_nlist.getVectors()[bond_copy]);
const float r_sq(dot(r_ij, r_ij));

for (size_t ii(0); ii < 3; ++ii)
Expand Down Expand Up @@ -105,14 +104,11 @@ void LocalDescriptors::compute(const locality::NeighborQuery* nq, const vec3<flo
++bond, ++neighbor_count)
{
const unsigned int sphCount(bond * getSphWidth());
const size_t j(m_nlist.getNeighbors()(bond, 1));
const vec3<float> r_ij(bondVector(locality::NeighborBond(i, j), nq, query_points));
const float r_sq(dot(r_ij, r_ij));
const vec3<float> r_ij(m_nlist.getVectors()[bond]);
const float magR(m_nlist.getDistances()[bond]);
const vec3<float> bond_ij(dot(rotation_0, r_ij), dot(rotation_1, r_ij),
dot(rotation_2, r_ij));

const float magR(std::sqrt(r_sq));

// Wrap theta into [0, 2*pi]
float theta(std::atan2(bond_ij.y, bond_ij.x));
theta = util::modulusPositive(theta, constants::TWO_PI);
Expand Down
12 changes: 5 additions & 7 deletions cpp/environment/MatchEnv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include "MatchEnv.h"

#include "NeighborBond.h"
#include "NeighborComputeFunctional.h"

namespace freud { namespace environment {
Expand Down Expand Up @@ -488,8 +487,7 @@ MatchEnv::~MatchEnv() = default;

EnvironmentCluster::~EnvironmentCluster() = default;

Environment MatchEnv::buildEnv(const freud::locality::NeighborQuery* nq,
const freud::locality::NeighborList* nlist, size_t num_bonds, size_t& bond,
Environment MatchEnv::buildEnv(const freud::locality::NeighborList* nlist, size_t num_bonds, size_t& bond,
unsigned int i, unsigned int env_ind)
{
Environment ei = Environment();
Expand All @@ -502,7 +500,7 @@ Environment MatchEnv::buildEnv(const freud::locality::NeighborQuery* nq,
const size_t j(nlist->getNeighbors()(bond, 1));
if (i != j)
{
vec3<float> delta(bondVector(locality::NeighborBond(i, j), nq, nq->getPoints()));
tommy-waltmann marked this conversation as resolved.
Show resolved Hide resolved
vec3<float> delta(nlist->getVectors()[bond]);
ei.addVec(delta);
}
}
Expand Down Expand Up @@ -540,7 +538,7 @@ void EnvironmentCluster::compute(const freud::locality::NeighborQuery* nq,
// if you don't do this, things will get screwy.
for (unsigned int i = 0; i < Np; i++)
{
Environment ei = buildEnv(nq, &env_nlist, env_num_bonds, env_bond, i, i);
Environment ei = buildEnv(&env_nlist, env_num_bonds, env_bond, i, i);
dj.s.push_back(ei);
dj.m_max_num_neigh = std::max(dj.m_max_num_neigh, ei.num_vecs);
;
Expand Down Expand Up @@ -726,7 +724,7 @@ void EnvironmentMotifMatch::compute(const freud::locality::NeighborQuery* nq,
for (unsigned int i = 0; i < Np; i++)
{
unsigned int dummy = i + 1;
Environment ei = buildEnv(nq, &nlist, num_bonds, bond, i, dummy);
Environment ei = buildEnv(&nlist, num_bonds, bond, i, dummy);
dj.s.push_back(ei);

// if the environment matches e0, merge it into the e0 environment set
Expand Down Expand Up @@ -803,7 +801,7 @@ void EnvironmentRMSDMinimizer::compute(const freud::locality::NeighborQuery* nq,
for (unsigned int i = 0; i < Np; i++)
{
unsigned int dummy = i + 1;
Environment ei = buildEnv(nq, &nlist, num_bonds, bond, i, dummy);
Environment ei = buildEnv(&nlist, num_bonds, bond, i, dummy);
dj.s.push_back(ei);

// if the environment matches e0, merge it into the e0 environment set
Expand Down
3 changes: 1 addition & 2 deletions cpp/environment/MatchEnv.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ class MatchEnv

//! Construct and return a local environment surrounding the particle indexed by i. Set the environment
//! index to env_ind.
static Environment buildEnv(const freud::locality::NeighborQuery* nq,
const freud::locality::NeighborList* nlist, size_t num_bonds, size_t& bond,
static Environment buildEnv(const freud::locality::NeighborList* nlist, size_t num_bonds, size_t& bond,
unsigned int i, unsigned int env_ind);

//! Returns the entire Np by m_num_neighbors by 3 matrix of all environments for all particles
Expand Down
34 changes: 18 additions & 16 deletions cpp/locality/AABBQuery.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ NeighborBond AABBQueryBallIterator::next()
// Check ii exclusion before including the pair.
if (r_sq < r_max_sq && r_sq >= r_min_sq)
{
return NeighborBond(m_query_point_idx, j, std::sqrt(r_sq));
return NeighborBond(m_query_point_idx, j, std::sqrt(r_sq), 1, r_ij);
}
}
}
Expand Down Expand Up @@ -230,7 +230,7 @@ NeighborBond AABBQueryIterator::next()
// r_min filtering because we're querying beyond the normally safe
// bounds, so we have to do it in this class.
m_current_neighbors.clear();
m_all_distances.clear();
m_all_bonds_minimum_distance.clear();
m_query_points_below_r_min.clear();
std::shared_ptr<NeighborQueryPerPointIterator> ball_it = std::make_shared<AABBQueryBallIterator>(
static_cast<const AABBQuery*>(m_neighbor_query), m_query_point, m_query_point_idx,
Expand All @@ -243,26 +243,28 @@ NeighborBond AABBQueryIterator::next()
continue;
}

if (!m_exclude_ii || m_query_point_idx != nb.point_idx)
if (!m_exclude_ii || m_query_point_idx != nb.getPointIdx())
{
nb.query_point_idx = m_query_point_idx;
nb.setQueryPointIdx(m_query_point_idx);
// If we've expanded our search radius beyond safe
// distance, use the map instead of the vector.
if (m_search_extended)
{
if ((m_all_distances.count(nb.point_idx) == 0)
|| m_all_distances[nb.point_idx] > nb.distance)
const unsigned int nb_point_idx = nb.getPointIdx();
const float nb_distance = nb.getDistance();
if ((m_all_bonds_minimum_distance.count(nb_point_idx) == 0)
|| m_all_bonds_minimum_distance[nb_point_idx].getDistance() > nb_distance)
{
m_all_distances[nb.point_idx] = nb.distance;
if (nb.distance < m_r_min)
m_all_bonds_minimum_distance[nb_point_idx] = nb;
if (nb_distance < m_r_min)
{
m_query_points_below_r_min.insert(nb.point_idx);
m_query_points_below_r_min.insert(nb_point_idx);
}
}
}
else
{
if (nb.distance >= m_r_min)
if (nb.getDistance() >= m_r_min)
{
m_current_neighbors.emplace_back(nb);
}
Expand All @@ -281,18 +283,18 @@ NeighborBond AABBQueryIterator::next()
}

if ((m_r_cur >= m_r_max) || (m_r_cur >= max_plane_distance)
|| ((m_all_distances.size() - m_query_points_below_r_min.size()) >= m_num_neighbors))
|| ((m_all_bonds_minimum_distance.size() - m_query_points_below_r_min.size())
>= m_num_neighbors))
{
// Once this condition is reached, either we found enough
// neighbors beyond the normal min_plane_distance
// condition or we conclude that there are not enough
// neighbors left in the system.
for (const auto& bond_distance : m_all_distances)
for (const auto& minimum_distance_bond : m_all_bonds_minimum_distance)
{
if (bond_distance.second >= m_r_min)
if (minimum_distance_bond.second.getDistance() >= m_r_min)
{
m_current_neighbors.emplace_back(m_query_point_idx, bond_distance.first,
bond_distance.second);
m_current_neighbors.emplace_back(minimum_distance_bond.second);
}
}
std::sort(m_current_neighbors.begin(), m_current_neighbors.end());
Expand Down Expand Up @@ -320,7 +322,7 @@ NeighborBond AABBQueryIterator::next()
while ((m_count < m_num_neighbors) && (m_count < m_current_neighbors.size()))
{
m_count++;
if (m_current_neighbors[m_count - 1].distance > m_r_max)
if (m_current_neighbors[m_count - 1].getDistance() > m_r_max)
{
m_finished = true;
return ITERATOR_TERMINATOR;
Expand Down
7 changes: 4 additions & 3 deletions cpp/locality/AABBQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class AABBQueryIterator : public AABBIterator
float r_min, float scale, bool exclude_ii)
: AABBIterator(neighbor_query, query_point, query_point_idx, r_max, r_min, exclude_ii), m_count(0),
m_num_neighbors(num_neighbors), m_search_extended(false), m_r_cur(r_guess), m_scale(scale),
m_all_distances(), m_query_points_below_r_min()
m_all_bonds_minimum_distance(), m_query_points_below_r_min()
{
updateImageVectors(0);
}
Expand All @@ -167,8 +167,9 @@ class AABBQueryIterator : public AABBIterator
float
m_r_cur; //!< Current search ball cutoff distance in use for the current particle (expands as needed).
float m_scale; //!< The amount to scale m_r by when the current ball is too small.
std::map<unsigned int, float> m_all_distances; //!< Hash map of minimum distances found for a given point,
//!< used when searching beyond maximum safe AABB distance.
std::map<unsigned int, NeighborBond>
m_all_bonds_minimum_distance; //!< Hash map of minimum distances found for a given point,
//!< used when searching beyond maximum safe AABB distance.
std::unordered_set<unsigned int> m_query_points_below_r_min; //!< The set of query_points that were too
//!< close based on the r_min threshold.
};
Expand Down
8 changes: 4 additions & 4 deletions cpp/locality/LinkCell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ NeighborBond LinkCellQueryBallIterator::next()

if (r_sq < r_max_sq && r_sq >= r_min_sq)
{
return NeighborBond(m_query_point_idx, j, std::sqrt(r_sq));
return NeighborBond(m_query_point_idx, j, std::sqrt(r_sq), 1, r_ij);
}
}

Expand Down Expand Up @@ -604,7 +604,7 @@ NeighborBond LinkCellQueryIterator::next()
const float r_sq(dot(r_ij, r_ij));
if (r_sq < r_max_sq && r_sq >= r_min_sq)
{
m_current_neighbors.emplace_back(m_query_point_idx, j, std::sqrt(r_sq));
m_current_neighbors.emplace_back(m_query_point_idx, j, std::sqrt(r_sq), 1, r_ij);
}
}
}
Expand Down Expand Up @@ -639,7 +639,7 @@ NeighborBond LinkCellQueryIterator::next()
// closest possible neighbor in the new shell.
std::sort(m_current_neighbors.begin(), m_current_neighbors.end());
if ((m_current_neighbors.size() >= m_num_neighbors)
&& (m_current_neighbors[m_num_neighbors - 1].distance
&& (m_current_neighbors[m_num_neighbors - 1].getDistance()
< static_cast<float>(m_neigh_cell_iter.getRange() - 1) * m_linkcell->getCellWidth()))
{
break;
Expand All @@ -650,7 +650,7 @@ NeighborBond LinkCellQueryIterator::next()
while ((m_count < m_num_neighbors) && (m_count < m_current_neighbors.size()))
{
m_count++;
if (m_current_neighbors[m_count - 1].distance > m_r_max)
if (m_current_neighbors[m_count - 1].getDistance() > m_r_max)
{
m_finished = true;
return ITERATOR_TERMINATOR;
Expand Down
Loading