Loading...
Searching...
No Matches
CutoffNeighbors.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#include "CutoffNeighbors.h"
6
7#include <algorithm>
8#include <cmath>
9#include <linkcell.hpp>
10#include <stdexcept>
11#include <string>
12#include <tuple>
13
14namespace gpr::geometry {
15
16std::vector<CutoffPair> cutoffNeighbors(const double* positions,
17 std::size_t count,
18 const std::array<double, 9>& cell,
19 const std::array<bool, 3>& periodic,
20 double cutoff)
21{
22 if (!(cutoff > 0.0) || !std::isfinite(cutoff))
23 throw std::invalid_argument(
24 "neighbor cutoff must be finite and positive");
25 if (count == 0) return {};
26 if (!positions)
27 throw std::invalid_argument("neighbor positions are required");
28
29 const bool open = !periodic[0] && !periodic[1] && !periodic[2];
30 const linkcell::Cell box = linkcell::Cell::from_vectors(
31 {cell[0], cell[1], cell[2]}, {cell[3], cell[4], cell[5]},
32 {cell[6], cell[7], cell[8]});
33 // half keeps one side of (i, j, S) and (j, i, -S). An open cell has
34 // no lattice, so pairs_open is that half list on the occupied bins.
35 // A mask stays on pairs_within_pbc. A non-positive hint is the
36 // library default. A cutoff much smaller than the box asks for a
37 // mesh the library refuses; doubling the edge keeps the same pairs
38 // and stops once one bin covers the longest axis.
39 const auto query = [&](double hint) {
40 return open
41 ? linkcell::pairs_open(positions, count, cutoff, hint)
42 : linkcell::pairs_within_pbc(positions, count, box, periodic,
43 cutoff, nullptr, hint, true);
44 };
45 double hint = 0.0;
46 std::vector<linkcell::ShiftedPair> neighbors;
47 for (;;) {
48 try {
49 neighbors = query(hint);
50 break;
51 } catch (const linkcell::Error& error) {
52 const std::string message = error.what();
53 if (message.find("too fine") == std::string::npos) throw;
54 const double edge = hint > 0.0 ? hint : cutoff;
55 double longest = edge;
56 if (open) {
57 for (int axis = 0; axis < 3; ++axis) {
58 double lo = positions[axis], hi = lo;
59 for (std::size_t i = 1; i < count; ++i) {
60 const double value = positions[3 * i + axis];
61 lo = std::min(lo, value);
62 hi = std::max(hi, value);
63 }
64 longest = std::max(longest, hi - lo);
65 }
66 } else {
67 const double lengths[3] = {
68 std::hypot(cell[0], cell[1], cell[2]),
69 std::hypot(cell[3], cell[4], cell[5]),
70 std::hypot(cell[6], cell[7], cell[8])};
71 for (const double length: lengths)
72 if (length > 0.0) longest = std::max(longest, length);
73 }
74 const double next = std::min(edge * 2.0, std::max(edge, longest));
75 if (!(next > edge) || !std::isfinite(next)) throw;
76 hint = next;
77 }
78 }
79 std::vector<CutoffPair> pairs;
80 pairs.reserve(neighbors.size());
81 for (const auto& pair: neighbors)
82 pairs.push_back({static_cast<std::size_t>(pair.i),
83 static_cast<std::size_t>(pair.j), pair.shift});
84 std::sort(pairs.begin(), pairs.end(), [](const auto& a, const auto& b) {
85 return std::tie(a.first, a.second, a.shift) <
86 std::tie(b.first, b.second, b.shift);
87 });
88 return pairs;
89}
90
91} // namespace gpr::geometry
std::vector< CutoffPair > cutoffNeighbors(const double *positions, std::size_t count, const std::array< double, 9 > &cell, const std::array< bool, 3 > &periodic, double cutoff)