Loading...
Searching...
No Matches
VesinNeighbors.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5*/
6
8
9#include <cstdlib>
10#include <cstring>
11
12namespace eonc {
13
15 : list_(other.list_),
16 owns_(other.owns_) {
17 other.list_ = VesinNeighborList{};
18 other.owns_ = false;
19}
20
22 if (this != &other) {
23 free_list();
24 list_ = other.list_;
25 owns_ = other.owns_;
26 other.list_ = VesinNeighborList{};
27 other.owns_ = false;
28 }
29 return *this;
30}
31
33
35 if (owns_) {
36 vesin_free(&list_);
37 owns_ = false;
38 }
39 list_ = VesinNeighborList{};
40}
41
42void VesinNeighbors::compute(const double *R, std::size_t n, const double *box,
43 const Options &opt) {
44 if (R == nullptr || (n > 0 && box == nullptr)) {
45 throw std::invalid_argument("VesinNeighbors::compute: null R or box");
46 }
47 if (opt.cutoff <= 0.0) {
48 free_list();
49 return;
50 }
51
52 // Pass the existing VesinNeighborList through so vesin can re-use pair /
53 // distance / vector buffers (upstream contract). Do not vesin_free first —
54 // that forces a full realloc every call and is the #386 ASV regression.
55 VesinOptions vopt{};
56 vopt.cutoff = opt.cutoff;
57 vopt.full = opt.full;
58 vopt.sorted = opt.sorted;
59 vopt.algorithm = opt.algorithm;
60 vopt.skin = opt.skin;
61 vopt.n_threads = opt.n_threads;
62 vopt.return_shifts = opt.return_shifts;
63 vopt.return_distances = opt.return_distances;
64 vopt.return_vectors = opt.return_vectors;
65
66 bool periodic[3] = {opt.periodic[0], opt.periodic[1], opt.periodic[2]};
67 double box33[3][3];
68 for (int a = 0; a < 3; ++a) {
69 for (int b = 0; b < 3; ++b) {
70 box33[a][b] = box[3 * a + b];
71 }
72 }
73
74 VesinDevice cpu{VesinCPU, 0};
75 const char *error_message = nullptr;
76 int status =
77 vesin_neighbors(reinterpret_cast<const double (*)[3]>(R), n, box33,
78 periodic, cpu, vopt, &list_, &error_message);
79 if (status != EXIT_SUCCESS) {
80 std::string err = "vesin_neighbors failed";
81 if (error_message != nullptr) {
82 err += ": ";
83 err += error_message;
84 }
85 free_list();
86 throw std::runtime_error(err);
87 }
88 owns_ = true;
89}
90
91VesinNeighborList *VesinNeighbors::release() {
92 if (!owns_) {
93 return nullptr;
94 }
95 auto *heap = new VesinNeighborList(list_);
96 list_ = VesinNeighborList{};
97 owns_ = false;
98 return heap;
99}
100
101} // namespace eonc
VesinNeighborList list_
VesinNeighborList * release()
Steal ownership of the internal list (for Metatomic custom deleters).
VesinNeighbors & operator=(const VesinNeighbors &)=delete
void compute(const double *R, std::size_t n, const double *box, const Options &opt)
Build / rebuild the list.
VesinNeighbors()=default
RAII resource manager for the ARTn C library with global synchronization.
VesinAlgorithm algorithm
vesin pair-search algorithm (auto / brute-force MIC / cell list).
int32_t n_threads
Threads for the vesin build; 1 keeps small builds deterministic and spawn-free, 0 defers to OMP_NUM_T...
double skin
vesin 0.6 native Verlet skin: > 0 lets vesin cache the topology in this list until an atom moves more...
bool full
true: both i→j and j→i; false: half list
std::array< bool, 3 > periodic