15#ifndef SEAMS_NEIGHBOURS_H_
16#define SEAMS_NEIGHBOURS_H_
29#ifdef SEAMS_HAS_LINKCELL
30#include <linkcell.hpp>
32#ifdef SEAMS_HAS_MINIMAGE
76 const std::vector<double> &boxLow,
double H[3][3],
78 const double xspan = box.size() > 0 ? box[0] : 0.0;
79 const double yspan = box.size() > 1 ? box[1] : 0.0;
80 const double zspan = box.size() > 2 ? box[2] : 0.0;
81 const double xlo_b = boxLow.size() > 0 ? boxLow[0] : 0.0;
82 const double ylo_b = boxLow.size() > 1 ? boxLow[1] : 0.0;
83 const double zlo_b = boxLow.size() > 2 ? boxLow[2] : 0.0;
84 const double xy = box.size() >= 6 ? box[3] : 0.0;
85 const double xz = box.size() >= 6 ? box[4] : 0.0;
86 const double yz = box.size() >= 6 ? box[5] : 0.0;
87 const double xmin = std::min(std::min(0.0, xy), std::min(xz, xy + xz));
88 const double xmax = std::max(std::max(0.0, xy), std::max(xz, xy + xz));
89 const double ymin = std::min(0.0, yz);
90 const double ymax = std::max(0.0, yz);
91 H[0][0] = xspan - xmax + xmin;
95 H[1][1] = yspan - ymax + ymin;
100 origin[0] = xlo_b - xmin;
101 origin[1] = ylo_b - ymin;
107 const std::vector<double> &boxLow,
112 lengths[0] = H[0][0];
113 lengths[1] = H[1][1];
114 lengths[2] = H[2][2];
119 const std::vector<double> &boxLow) {
123 if (lengths[1] > lengths[axial]) {
126 if (lengths[2] > lengths[axial]) {
138inline void dumpToFrac(
const double H[3][3],
const double origin[3],
double x,
139 double y,
double z,
double s[3]) {
140 const double lx = H[0][0];
141 const double ly = H[1][1];
142 const double lz = H[2][2];
143 const double xy = H[1][0];
144 const double xz = H[2][0];
145 const double yz = H[2][1];
146 s[2] = (z - origin[2]) / lz;
147 s[1] = (y - origin[1] - yz * s[2]) / ly;
148 s[0] = (x - origin[0] - xy * s[1] - xz * s[2]) / lx;
153 const double s[3],
double r[3]) {
154 r[0] = origin[0] + H[0][0] * s[0] + H[1][0] * s[1] + H[2][0] * s[2];
155 r[1] = origin[1] + H[1][1] * s[1] + H[2][1] * s[2];
156 r[2] = origin[2] + H[2][2] * s[2];
162 const std::vector<double> &boxLow) {
166 const double vol = H[0][0] * H[1][1] * H[2][2];
167 return vol < 0.0 ? -vol : vol;
181 std::vector<double> &boxLow,
double xlo,
182 double ylo,
double zlo,
double lx,
double ly,
183 double lz,
double xy,
double xz,
double yz) {
184 if (!(lx > 0.0 && ly > 0.0 && lz > 0.0)) {
190 std::abs(xy) > 0.0 || std::abs(xz) > 0.0 || std::abs(yz) > 0.0;
193 boxLow = {xlo, ylo, zlo};
196 const double xmin = std::min(std::min(0.0, xy), std::min(xz, xy + xz));
197 const double xmax = std::max(std::max(0.0, xy), std::max(xz, xy + xz));
198 const double ymin = std::min(0.0, yz);
199 const double ymax = std::max(0.0, yz);
200 box = {lx + xmax - xmin, ly + ymax - ymin, lz, xy, xz, yz};
201 boxLow = {xlo + xmin, ylo + ymin, zlo};
211 std::vector<double> &boxLow,
double a,
212 double b,
double c,
double alphaDeg,
213 double betaDeg,
double gammaDeg) {
214 constexpr double kDeg = 3.14159265358979323846 / 180.0;
215 const double alpha = alphaDeg * kDeg;
216 const double beta = betaDeg * kDeg;
217 const double gamma = gammaDeg * kDeg;
218 const double cosA = std::cos(alpha);
219 const double cosB = std::cos(beta);
220 const double cosG = std::cos(gamma);
221 const double sinG = std::sin(gamma);
222 if (!(a > 0.0 && b > 0.0 && c > 0.0) || std::abs(sinG) < 1e-18) {
227 double xy = b * cosG;
228 const double ly = b * sinG;
229 double xz = c * cosB;
230 double yz = c * (cosA - cosB * cosG) / sinG;
233 const double tol = 1e-10 * std::max(a, std::max(b, c));
234 if (std::abs(xy) < tol) {
237 if (std::abs(xz) < tol) {
240 if (std::abs(yz) < tol) {
243 const double lz2 = c * c - xz * xz - yz * yz;
244 if (!(ly > 0.0) || !(lz2 > 0.0)) {
253#ifdef SEAMS_HAS_LINKCELL
255inline lc_cell lammpsBoxToLcCell(
const std::vector<double> &box,
256 const std::vector<double> &boxLow) {
257#ifdef SEAMS_HAS_MINIMAGE
258 const double xspan = box.size() > 0 ? box[0] : 0.0;
259 const double yspan = box.size() > 1 ? box[1] : 0.0;
260 const double zspan = box.size() > 2 ? box[2] : 0.0;
261 const double xy = box.size() >= 6 ? box[3] : 0.0;
262 const double xz = box.size() >= 6 ? box[4] : 0.0;
263 const double yz = box.size() >= 6 ? box[5] : 0.0;
264 const double xlo_b = boxLow.size() > 0 ? boxLow[0] : 0.0;
265 const double ylo_b = boxLow.size() > 1 ? boxLow[1] : 0.0;
266 const double zlo_b = boxLow.size() > 2 ? boxLow[2] : 0.0;
268 if (mi_cell_from_lammps_bounds(xspan, yspan, zspan, xy, xz, yz, xlo_b, ylo_b,
270 lc_cell c = lc_cell_ortho(raw.ax, raw.by, raw.cz);
286 double H[3][3], o[3];
288 lc_cell c = lc_cell_ortho(H[0][0], H[1][1], H[2][2]);
304inline linkcell::Cell lammpsBoxToLinkcell(
const std::vector<double> &box,
305 const std::vector<double> &boxLow) {
306 return linkcell::Cell(lammpsBoxToLcCell(box, boxLow));
313inline void residentFrameCell(
const double *box,
const double *boxLow,
314 int nBox, linkcell::Cell &cell,
315 const double *&frameLens) {
316 cell = linkcell::Cell::ortho(box[0], box[1], box[2]);
318 if (boxLow !=
nullptr || nBox >= 6) {
319 std::vector<double>
dump(
static_cast<std::size_t
>(std::max(nBox, 3)));
320 for (
int i = 0; i < nBox && i < static_cast<int>(
dump.size()); ++i) {
321 dump[
static_cast<std::size_t
>(i)] = box[i];
323 std::vector<double> lo;
324 if (boxLow !=
nullptr) {
325 lo = {boxLow[0], boxLow[1], boxLow[2]};
327 cell = lammpsBoxToLinkcell(dump, lo);
340 int typeI,
int typeJ);
346 int typeI,
int typeJ);
364 const std::vector<char> *source);
379 const std::vector<char> *source, std::vector<std::vector<int>> &rows,
380 bool byId =
true,
bool everyRow =
false);
384 const std::vector<int> &subset,
double rcutoff,
385 std::vector<std::vector<int>> &rows);
404 const std::vector<int> *subset,
double rcutoff, std::vector<int> &offsets,
405 std::vector<int> &cols,
bool half =
false,
406 std::vector<std::vector<int>> *directRows =
nullptr,
const int *indexToID =
nullptr,
407 bool *wroteRows =
nullptr,
bool branchy =
false);
413 int typeI, std::vector<std::vector<int>> &rows);
417 double cutoff, std::vector<std::vector<int>> &rows);
424 double candidateCutoff, std::vector<int> &offsets, std::vector<int> &cols);
433 const std::vector<int> *subset =
nullptr,
bool half =
false);
435 [[nodiscard]]
const std::vector<int> &
offsets()
const {
return offsets_; }
436 [[nodiscard]]
const std::vector<int> &
cols()
const {
return cols_; }
445 std::vector<double> x0_;
446 std::vector<double> y0_;
447 std::vector<double> z0_;
448 std::vector<double> box0_;
449 std::vector<double> boxLow0_;
450 std::vector<int> wideOff_;
451 std::vector<int> wideCols_;
452 std::vector<int> offsets_;
453 std::vector<int> cols_;
454 const std::vector<int> *subset_{
nullptr};
456 bool moved(
const molSys::PointCloud<molSys::Point<double>,
double> &yCloud)
const;
457 void filter(
const molSys::PointCloud<molSys::Point<double>,
double> &yCloud);
463 const std::vector<int> &indexToID,
const std::vector<int> &offsets,
464 const std::vector<int> &cols);
468 int nop,
const std::vector<int> &offsets,
const std::vector<int> &cols);
473 const std::vector<int> &indexToID,
const std::vector<int> &offsets,
474 const std::vector<int> &cols);
503 const std::vector<std::vector<int>> &nList);
527 double candidateCutoff,
int typeI,
bool mutual =
true);
533 double candidateCutoff,
int typeI,
bool mutual, std::vector<int> &offsets,
534 std::vector<int> &cols,
bool park =
true);
541 double candidateCutoff,
int typeI, std::vector<double> &xyz,
542 std::vector<int> &nom);
548 double candidateCutoff,
const std::vector<int> &types,
bool mutual =
true);
553std::pair<std::vector<std::vector<int>>, std::vector<std::vector<int>>>
556 double candidateCutoff,
int typeI);
558std::pair<std::vector<std::vector<int>>, std::vector<std::vector<int>>>
561 double candidateCutoff,
const std::vector<int> &types);
625 const std::vector<std::vector<int>> &
631 [[nodiscard]]
const std::vector<std::vector<int>> &
bonds()
const {
645 int changedAtoms_{0};
646 std::vector<double> x0_;
647 std::vector<double> y0_;
648 std::vector<double> z0_;
649 std::vector<double> box0_;
650 std::vector<std::pair<int, int>> candidates_;
651 std::vector<std::pair<int, int>> bonded_;
652 std::vector<std::vector<int>> nList_;
655 const molSys::PointCloud<molSys::Point<double>,
double> &yCloud)
const;
656 void rebuildCandidates(
657 const molSys::PointCloud<molSys::Point<double>,
double> &yCloud);
659 const molSys::PointCloud<molSys::Point<double>,
double> &yCloud);
const std::vector< int > & cols() const
const std::vector< int > & offsets() const
CutoffSkin(double cutoff, double skin)
bool update(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, const std::vector< int > *subset=nullptr, bool half=false)
SkinNeighborList(double cutoff, double skin, int typeI, BondGraph graph=BondGraph::KnnMutual, int k=4)
const std::vector< std::vector< int > > & update(const molSys::PointCloud< molSys::Point< double >, double > &yCloud)
int lastChangedAtoms() const
Atoms whose cutoff bond set changed on the last update.
const std::vector< std::vector< int > > & bonds() const
File for containing generic or common functions.
The main molecular system handler.
Functions for building neighbour lists.
std::vector< std::vector< int > > idRowsFromCsr(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, const std::vector< int > &indexToID, const std::vector< int > &offsets, const std::vector< int > &cols)
Atom-ID rows, self first, from a CSR of cloud indices.
std::pair< double, double > shellSeparation(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, int k, int typeI)
The shell-separation certificate for the exact reduction of the k-nearest graph to the cutoff graph: ...
bool directedKNearestCsr(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, int k, double candidateCutoff, std::vector< int > &offsets, std::vector< int > &cols)
Directed k nearest neighbours as a CSR of cloud indices, no self entry.
std::vector< std::vector< int > > kNearestNeighbourList(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, int k, double candidateCutoff, int typeI, bool mutual=true)
Bonded graph from the k nearest neighbours of each particle rather than a distance cutoff.
const char * bondGraphName(BondGraph graph)
bool blockWindowRows(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, double rcutoff, const std::vector< char > *source, std::vector< std::vector< int > > &rows, bool byId=true, bool everyRow=false)
void dumpBoundsToH(const std::vector< double > &box, const std::vector< double > &boxLow, double H[3][3], double origin[3])
LAMMPS dump bound spans to restricted triclinic H (rows a, b, c).
void dumpCellLengths(const std::vector< double > &box, const std::vector< double > &boxLow, double lengths[3])
Recovered restricted-triclinic lengths lx, ly, lz (H diagonal).
const std::vector< std::vector< int > > & reusedShellList(double rcutoff, const molSys::PointCloud< molSys::Point< double >, double > &yCloud, const std::vector< char > *source)
void kNearestPacked(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, int k, double candidateCutoff, int typeI, std::vector< double > &xyz, std::vector< int > &nom)
std::pair< std::vector< std::vector< int > >, std::vector< std::vector< int > > > kNearestNeighbourPair(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, int k, double candidateCutoff, int typeI)
Mutual and union k-nearest graphs from one candidate search.
std::vector< std::tuple< int, int, double > > nearestUnlike(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, int typeI, int typeJ)
Nearest unlike image of each typeI particle among typeJ particles.
void dumpFromFrac(const double H[3][3], const double origin[3], const double s[3], double r[3])
Fractional to cartesian coordinates via dump H.
std::vector< std::vector< int > > indexRowsFromCsr(int nop, const std::vector< int > &offsets, const std::vector< int > &cols)
Index rows, self first, from a CSR of cloud indices.
std::vector< std::vector< int > > halfNeighList(double rcutoff, const molSys::PointCloud< molSys::Point< double >, double > &yCloud, int typeI=1)
void preferSerialCutoff(bool on)
When set, neighListO stays on the one-thread builder even if the OpenMP team is wider.
std::vector< std::pair< int, int > > mutualNearestUnlike(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, int typeI, int typeJ)
Subset of nearestUnlike where j's nearest typeI is i (mutual).
int clearNeighbourList(std::vector< std::vector< int > > &nList)
Erases memory for a vector of vectors for the neighbour list.
bool setBoxFromLengthsAngles(std::vector< double > &box, std::vector< double > &boxLow, double a, double b, double c, double alphaDeg, double betaDeg, double gammaDeg)
Crystallographic lengths and angles in degrees, stored as a dump box.
void dumpToFrac(const double H[3][3], const double origin[3], double x, double y, double z, double s[3])
Cartesian to fractional coordinates via dump H.
std::vector< std::vector< int > > neighListLinkcell(double rcutoff, const molSys::PointCloud< molSys::Point< double >, double > &yCloud, int typeI)
int dumpAxialDim(const std::vector< double > &box, const std::vector< double > &boxLow)
Longest recovered length: 0 = x, 1 = y, 2 = z.
bool cellListRowsThreaded(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, const std::vector< int > &subset, double rcutoff, std::vector< std::vector< int > > &rows)
std::vector< std::vector< int > > neighList(double rcutoff, const molSys::PointCloud< molSys::Point< double >, double > &yCloud, int typeI, int typeJ)
All these functions use atom IDs and not indices.
std::vector< std::vector< int > > getNewNeighbourListByIndex(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, double cutoff)
bool refillIndexRows(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, double cutoff, std::vector< std::vector< int > > &rows)
Same rows as getNewNeighbourListByIndex, written into rows.
bool refillNeighListO(double rcutoff, const molSys::PointCloud< molSys::Point< double >, double > &yCloud, int typeI, std::vector< std::vector< int > > &rows)
void kNearestCsr(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, int k, double candidateCutoff, int typeI, bool mutual, std::vector< int > &offsets, std::vector< int > &cols, bool park=true)
std::vector< std::vector< int > > halfIdRowsFromCsr(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, const std::vector< int > &indexToID, const std::vector< int > &offsets, const std::vector< int > &cols)
Half list by atom ID. The lower cloud index owns the pair.
std::vector< std::vector< int > > neighbourListByIndex(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, const std::vector< std::vector< int > > &nList)
double dumpVolume(const std::vector< double > &box, const std::vector< double > &boxLow)
Triclinic dump-cell volume, the absolute value of det(H), from dumpBoundsToH.
BondGraph bondGraphFromName(const std::string &name)
bool setBoxFromLammpsEdges(std::vector< double > &box, std::vector< double > &boxLow, double xlo, double ylo, double zlo, double lx, double ly, double lz, double xy, double xz, double yz)
LAMMPS edge origin and edge lengths, plus tilt, as a dump box.
std::vector< std::vector< int > > neighListPair(double rcutoff, const molSys::PointCloud< molSys::Point< double >, double > &yCloud, int typeI, int typeJ)
bool orthoFullCsr(const molSys::PointCloud< molSys::Point< double >, double > &yCloud, const std::vector< int > *subset, double rcutoff, std::vector< int > &offsets, std::vector< int > &cols, bool half=false, std::vector< std::vector< int > > *directRows=nullptr, const int *indexToID=nullptr, bool *wroteRows=nullptr, bool branchy=false)
Full cutoff list as CSR of cloud indices.
BondGraph
Bond graph for TUM.
std::vector< std::vector< int > > neighListO(double rcutoff, const molSys::PointCloud< molSys::Point< double >, double > &yCloud, int typeI)
void dump(const Runtime &cfg, std::ostream &os)
This contains a collection of points; contains information for a particular frame.
This contains per-particle information.