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#include <memory>
12
13namespace eonc {
14
16 : list_(other.list_),
17 owns_(other.owns_) {
18 other.list_ = VesinNeighborList{};
19 other.owns_ = false;
20}
21
23 if (this != &other) {
24 free_list();
25 list_ = other.list_;
26 owns_ = other.owns_;
27 other.list_ = VesinNeighborList{};
28 other.owns_ = false;
29 }
30 return *this;
31}
32
34
36 if (owns_) {
37 vesin_free(&list_);
38 owns_ = false;
39 }
40 list_ = VesinNeighborList{};
41}
42
43void VesinNeighbors::compute(const double *R, std::size_t n, const double *box,
44 const Options &opt) {
45 if (R == nullptr || (n > 0 && box == nullptr)) {
46 throw std::invalid_argument("VesinNeighbors::compute: null R or box");
47 }
48 if (opt.cutoff <= 0.0) {
49 free_list();
50 return;
51 }
52
53 // Pass the existing VesinNeighborList through so vesin can re-use pair /
54 // distance / vector buffers (upstream contract). Do not vesin_free first —
55 // that forces a full realloc every call and is the #386 ASV regression.
56 VesinOptions vopt{};
57 vopt.cutoff = opt.cutoff;
58 vopt.full = opt.full;
59 vopt.sorted = opt.sorted;
60 vopt.algorithm = opt.algorithm;
61 vopt.skin = opt.skin;
62 vopt.n_threads = opt.n_threads;
63 vopt.return_shifts = opt.return_shifts;
64 vopt.return_distances = opt.return_distances;
65 vopt.return_vectors = opt.return_vectors;
66
67 bool periodic[3] = {opt.periodic[0], opt.periodic[1], opt.periodic[2]};
68 double box33[3][3];
69 for (int a = 0; a < 3; ++a) {
70 for (int b = 0; b < 3; ++b) {
71 box33[a][b] = box[3 * a + b];
72 }
73 }
74
75 VesinDevice cpu{VesinCPU, 0};
76 const char *error_message = nullptr;
77 int status =
78 vesin_neighbors(reinterpret_cast<const double (*)[3]>(R), n, box33,
79 periodic, cpu, vopt, &list_, &error_message);
80 if (status != EXIT_SUCCESS) {
81 std::string err = "vesin_neighbors failed";
82 if (error_message != nullptr) {
83 err += ": ";
84 err += error_message;
85 }
86 free_list();
87 throw std::runtime_error(err);
88 }
89 owns_ = true;
90}
91
92VesinNeighborList *VesinNeighbors::release() {
93 if (!owns_) {
94 return nullptr;
95 }
96 auto heap = std::make_unique<VesinNeighborList>(list_);
97 list_ = VesinNeighborList{};
98 owns_ = false;
99 return heap.release();
100}
101
102} // 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