18 const std::array<double, 9>& cell,
19 const std::array<bool, 3>& periodic,
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 {};
27 throw std::invalid_argument(
"neighbor positions are required");
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]});
39 const auto query = [&](
double hint) {
41 ? linkcell::pairs_open(positions, count, cutoff, hint)
42 : linkcell::pairs_within_pbc(positions, count, box, periodic,
43 cutoff,
nullptr, hint,
true);
46 std::vector<linkcell::ShiftedPair> neighbors;
49 neighbors = query(hint);
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;
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);
64 longest = std::max(longest, hi - lo);
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);
74 const double next = std::min(edge * 2.0, std::max(edge, longest));
75 if (!(next > edge) || !std::isfinite(next))
throw;
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);