namespace nneigh#
Overview#
Functions for building neighbour lists. More…
namespace nneigh {
// enums
enum BondGraph;
// classes
class CutoffSkin;
class SkinNeighborList;
// global functions
void dumpBoundsToH(const std::vector<double>& box, const std::vector<double>& boxLow, double H[3][3], double origin[3]);
void dumpCellLengths(const std::vector<double>& box, const std::vector<double>& boxLow, double lengths[3]);
int dumpAxialDim(const std::vector<double>& box, const std::vector<double>& boxLow);
int dumpAxialDim(const molSys::PointCloud<molSys::Point<double>, double>& yCloud);
void dumpToFrac(const double H[3][3], const double origin[3], double x, double y, double z, double s[3]);
void dumpFromFrac(const double H[3][3], const double origin[3], const double s[3], double r[3]);
double dumpVolume(const std::vector<double>& box, const std::vector<double>& boxLow);
double dumpVolume(const molSys::PointCloud<molSys::Point<double>, double>& yCloud);
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);
bool setBoxFromLengthsAngles(std::vector<double>& box, std::vector<double>& boxLow, double a, double b, double c, double alphaDeg, double betaDeg, double gammaDeg);
lc_cell lammpsBoxToLcCell(const std::vector<double>& box, const std::vector<double>& boxLow);
linkcell::Cell lammpsBoxToLinkcell(const std::vector<double>& box, const std::vector<double>& boxLow);
void residentFrameCell(const double* box, const double* boxLow, int nBox, linkcell::Cell& cell, const double*& frameLens);
void linkcellCutoffCsr(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, double rcutoff, bool half, const std::array<bool, 3>& periodic, const std::vector<int>& mask, int threads, linkcell::PairCsr& out);
std::vector<std::vector<int>> neighList(double rcutoff, const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int typeI, int typeJ);
std::vector<std::vector<int>> neighListPair(double rcutoff, const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int typeI, int typeJ);
const std::vector<std::vector<int>>& reusedShellList(double rcutoff, const molSys::PointCloud<molSys::Point<double>, double>& yCloud, const std::vector<char>* source);
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);
bool cellListRowsThreaded(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, const std::vector<int>& subset, double rcutoff, std::vector<std::vector<int>>& rows);
void preferSerialCutoff(bool on);
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);
bool refillNeighListO(double rcutoff, const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int typeI, std::vector<std::vector<int>>& rows);
bool refillIndexRows(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, double cutoff, std::vector<std::vector<int>>& rows);
bool directedKNearestCsr(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int k, double candidateCutoff, std::vector<int>& offsets, std::vector<int>& cols);
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);
std::vector<std::vector<int>> neighListO(double rcutoff, const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int typeI);
std::vector<std::vector<int>> neighListLinkcell(double rcutoff, const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int typeI);
std::vector<std::vector<int>> halfNeighList(double rcutoff, const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int typeI = 1);
std::vector<std::vector<int>> neighbourListByIndex(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, const std::vector<std::vector<int>>& nList);
std::vector<std::vector<int>> getNewNeighbourListByIndex(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, double cutoff);
std::vector<std::vector<int>> kNearestNeighbourList(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int k, double candidateCutoff, int typeI, bool mutual = true);
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);
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::vector<std::vector<int>> kNearestNeighbourList(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int k, double candidateCutoff, const std::vector<int>& types, bool mutual = true);
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);
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, const std::vector<int>& types);
std::pair<double, double> shellSeparation(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int k, int typeI);
std::vector<std::tuple<int, int, double>> nearestUnlike(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int typeI, int typeJ);
std::vector<std::pair<int, int>> mutualNearestUnlike(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int typeI, int typeJ);
int clearNeighbourList(std::vector<std::vector<int>>& nList);
BondGraph bondGraphFromName(const std::string& name);
const char* bondGraphName(BondGraph graph);
} // namespace nneigh
Detailed Documentation#
Functions for building neighbour lists.
This namespace contains functions that build neighbour lists (linkcell cutoff rows or k-nearest searches), saving either the atom IDs or atom indices (according to a PointCloud) in a row-ordered vector of vectors. Whether the atom IDs or atom indices (i.e. the indices of the elements in the vector pts inside the PointCloud) are saved, the neighbour lists are constructed such that the first element is the ‘central atom’, whose neighbours are being saved on that particular line. The central atom is followed by the atom IDs or indices of the nearest neighbours.
In a ‘full’ neighbour list, if 1 is a neighbour of 2 then 1 is saved in the neighbour list of 2 AND 2 is also saved in the neighbour list of 1.
In a ‘half’ neighbour list, if 1 is a neighbour of 2, then 2 is saved in the neighbour list of 1 but not vice versa.
Changelog#
Amrita Goswami [amrita16thaug646@gmail.com]; date modified: Nov 14, 2019
Rohit Goswami [rog32@hi.is]; date modified: Mar 20, 2021
Global Functions#
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).
box[0..2] are xhi_bound - xlo_bound etc. Optional box[3..5] are tilt xy, xz, yz. boxLow is the bound lo. Inverse of xlo_bound = xlo + min(0, xy, xz, xy+xz).
void dumpCellLengths(const std::vector<double>& box, const std::vector<double>& boxLow, double lengths[3])
Recovered restricted-triclinic lengths lx, ly, lz (H diagonal).
int dumpAxialDim(const std::vector<double>& box, const std::vector<double>& boxLow)
Longest recovered length: 0 = x, 1 = y, 2 = z.
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.
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.
double dumpVolume(const std::vector<double>& box, const std::vector<double>& boxLow)
Triclinic dump-cell volume, the absolute value of det(H), from dumpBoundsToH.
Bound spans Lx*Ly*Lz are not the cell volume when tilt is present.
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.
lx, ly, lz are xhi - xlo and not the bound spans. A zero tilt stays three spans so the orthorhombic pair loop is unchanged. Returns false when an edge is not positive.
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.
alpha is between b and c, beta between a and c, gamma between a and b. The basis is the reduced triclinic frame: a along x, b in the xy plane. Returns false when the cell is not a positive volume.
lc_cell lammpsBoxToLcCell(const std::vector<double>& box, const std::vector<double>& boxLow)
Map a LAMMPS dump box onto lc_cell.
void residentFrameCell(const double* box, const double* boxLow, int nBox, linkcell::Cell& cell, const double*& frameLens)
Cell and optional per-frame ortho lengths for analyzeResident.
boxLow set or nBox >= 6: one dump box via lammpsBoxToLinkcell, frameLens is null. Otherwise Cell::ortho of box[0..2] and frameLens is box.
void linkcellCutoffCsr(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, double rcutoff, bool half, const std::array<bool, 3>& periodic, const std::vector<int>& mask, int threads, linkcell::PairCsr& out)
Cutoff rows of the cloud from linkcell’s threaded CSR search, in cloud indices.
Row i lists every j != i whose nearest image is strictly within rcutoff, once and ascending; half keeps j > i. An open axis of periodic takes no image, and a cloud with every axis open needs no box. A nonzero entry of mask keeps that atom; a masked atom has an empty row and is in no row. An empty mask keeps every atom. The search runs on threads Rayon workers, and the rows are the same at every width. out keeps its capacity between calls. Throws std::invalid_argument for a cutoff that is not finite and positive or a periodic axis without a box, and linkcell::Error for coordinates that are not finite.
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.
Full neighbour list for pairs of type I and type J, from linkcell’s cutoff rows. The neighbour list does not differentiate between the types of atoms
std::vector<std::vector<int>> neighListPair(double rcutoff, const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int typeI, int typeJ)
I-J neighbour list (I==J is like-type and reuses neighListO). Unlike-type pairs come from the same linkcell rows as neighListO.
const std::vector<std::vector<int>>& reusedShellList(double rcutoff, const molSys::PointCloud<molSys::Point<double>, double>& yCloud, const std::vector<char>* source)
Cutoff rows by atom ID, self first, for every atom or only the atoms marked in source; an unmarked atom’s row is its own ID. Partners are in ascending atom ID. The row storage stays allocated on the calling thread and is refilled in place, so overlapping calls do not share it. The returned reference is valid on this thread until the next call on this thread. Same partners as neighListO for the rows that are filled.
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)
Neighbour rows on a cell grid that covers the owned block plus its halo. Anchors are folded onto the first owned atom. Every atom, ghosts included, is shifted by a box length so it sits against that block. An axis whose empty gap is wider than the cutoff does not wrap, and the fused cutoff list runs on that window rather than on the periodic box. Returns false when the atoms fill the box or the box is not orthorhombic. Rows are atom IDs when byId is set, and cloud indices otherwise. Self is first, then partners in ascending order, matching neighListO. A null source marks every atom as an anchor; otherwise only marked atoms receive partners. With everyRow the source still places the window, and every atom in it receives partners.
bool cellListRowsThreaded(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, const std::vector<int>& subset, double rcutoff, std::vector<std::vector<int>>& rows)
Threaded cell-list neighbour rows. rows[k] holds the indices within rcutoff of subset[k] under the minimum image convention, ascending and without the atom itself. Atoms are binned in fractional coordinates of the recovered triclinic cell H; every axis needs at least three cells of perpendicular width rcutoff, so that the nearest image of every neighbour sits in one of the 27 surrounding cells. Returns false, with rows untouched, when the cell is too small for that. Each row is built by one thread.
void preferSerialCutoff(bool on)
When set, the cutoff lists search on one thread even if the OpenMP team is wider.
The team is left in place for the caller.
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.
Orthorhombic, or a restricted triclinic cell stored as bound spans plus xy, xz, yz.
Orthorhombic boxes (box.size() == 3) and restricted triclinic dump boxes (box.size() >= 6) both use the parallel cell sort. A null subset is every atom. cols[offsets[i] .. offsets[i + 1]] is sorted and has no self entry. A neighbour is kept when the squared minimum image is strictly below the cutoff squared. half keeps the pair only on the lower cloud index. The pair set does not depend on the thread count. Scratch buffers stay allocated and are refilled on the next call. Returns false when the box is not usable.
bool refillNeighListO(double rcutoff, const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int typeI, std::vector<std::vector<int>>& rows)
Same rows as neighListO, written into rows. A row keeps the capacity of the previous call, so a repeated frame does not allocate per atom.
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 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.
candidateCutoff is the cell-size hint used by the k-nearest search. Rows are nearest first. Returns false when the cloud has no box.
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::vector<std::vector<int>> neighListO(double rcutoff, const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int typeI)
Full neighbour list for one type, from linkcell’s cutoff rows. An atom of another type keeps a row of its own ID. Empty when the cloud has no box. You can only use this for neighbour lists with one type
std::vector<std::vector<int>> neighListLinkcell(double rcutoff, const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int typeI)
neighListO’s rows with the team narrowed to four threads below 4096 atoms. Empty when the cloud has no box or no atom of typeI. The OpenMP team is left running at that width.
std::vector<std::vector<int>> halfNeighList(double rcutoff, const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int typeI = 1)
Half neighbour list for one type, from linkcell’s half rows: the lower cloud index owns the pair. You can only use this for neighbour lists with one type
std::vector<std::vector<int>> neighbourListByIndex(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, const std::vector<std::vector<int>>& nList)
The following function outputs a neighbour list using indices and NOT atom IDs
Converts the neighbour list build with atom IDs into a neighbour list of atom indices, according to the pointCloud
std::vector<std::vector<int>> getNewNeighbourListByIndex(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, double cutoff)
Gets a neighbour list by index, according to a pointCloud given as the input. Assume no slices or other skullduggery
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.
The default intersection symmetrization (mutual = true) bonds i and j only when each lists the other among its k nearest; the union alternative bonds on either nomination. In a crystal the first shell is mutual and the two coincide; on disordered packings the mutual graph is sparser, which starves accidental ring structure measured on the dense null, mutual scores zero false crystal where union reaches 2.5%. Nominations are a periodic linked-cell k-nearest search via linkcell (Allen and Tildesley): KD-trees have no minimum-image convention. candidateCutoff is only a cell-size hint: an empty hinted shell deliberately falls back to the whole periodic cell so a k-nearest query still returns the nearest available atoms. On an undistorted tetrahedral lattice with k = 4 this graph equals the first-shell cutoff graph. Rows are by atom ID with the leading self entry, like neighListO.
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)
Same neighbours as kNearestNeighbourList, as a CSR of cloud indices with no self entry. Column order matches that list after the atom-id rows are mapped back to indices.
void kNearestPacked(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int k, double candidateCutoff, int typeI, std::vector<double>& xyz, std::vector<int>& nom)
Packed nominations from the same search as kNearestNeighbourList. xyz is n by 3 and nom is n by k cloud indices, nearest first, with unused slots set to -1. The rows are not symmetrized.
std::vector<std::vector<int>> kNearestNeighbourList(const molSys::PointCloud<molSys::Point<double>, double>& yCloud, int k, double candidateCutoff, const std::vector<int>& types, bool mutual = true)
k-nearest graph on a type set (water oxygens).
Substrate, ions and other species never appear as neighbours. An empty set is no atoms.
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.
Seeded affiliation needs both; building them separately repeats the cell list.
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: returns {max_i d_k(i), min_i d_{k+1}(i)} over particles of the type.
When max_i d_k(i) <= rcutoff <= min_i d_{k+1}(i), the two graphs coincide edge for edge and every downstream graph predicate is identical. Brute force, intended for validation. A particle of the type with fewer than k others of that type returns {infinity, 0}: no cutoff gives it k neighbours.
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.
Returns cloud-index pairs (i, j) and the MIC distance (sqrt of gen::periodicDistSq). On an ion cloud, typeI = 1 (cation vertex) and typeJ = 2 (anion vertex). This is the nearest unlike neighbour, not a coordination number.
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).
A contact pair is this mutual edge, not first-shell membership.
int clearNeighbourList(std::vector<std::vector<int>>& nList)
Erases memory for a vector of vectors for the neighbour list.