Skip to content

_celllist

Pair enumeration by binning, in numpy.

This is the fallback under chmpy.calc.neighbors when vesin is not installed, and it is meant to be quick enough that installing vesin is an optimisation rather than a requirement.

The approach is the usual linked-cell list, written without a Python loop over atoms: atoms are binned on a grid at least as coarse as the cutoff, and the enumeration runs one vectorised pass per bin offset. Three things keep the element count down, and they are the difference between this and a numpy neighbour list that is ten times slower.

  • No replicated coordinate array. The obvious numpy approach tiles the positions over every periodic image and builds one tree over N * n_images points; for a small cell and a 6 A cutoff that is a 125-fold blow-up in memory before a single distance is computed. Here periodicity is a wrap of the bin index, and the lattice translation falls out of that wrap as an integer, so the coordinates are never copied.

  • Half the bin offsets. A pair found at offset o is the same pair found at -o from the other end, so only the lexicographically positive half of the offsets is visited (13 of 27, plus the zero offset restricted to j > i). Every candidate distance is therefore computed once rather than twice. A full list is the half list plus its mirror, which costs a copy rather than a second enumeration.

  • Shifts built only for survivors. The lattice translation of a pair is an integer 3-vector, and assembling it for every candidate costs more than the distances do. Instead the per-atom part of the translation is folded into a shifted query position, so a candidate costs one subtraction and one dot product, and the integer shifts are assembled afterwards for the ~15% of candidates that survive the cutoff.

The pairs come out in a different order from vesin's, and a half list may pick the opposite representative of a pair -- (j, i, -S) where vesin gives (i, j, S). Both describe the same set of interactions, which is all any consumer of a half list can rely on.

cell_list_pairs(positions, cell, pbc, cutoff, full=False, self_pairs=True)

Enumerate pairs within cutoff.

Parameters:

Name Type Description Default
positions

(N, 3) Cartesian coordinates, Angstroms

required
cell

(3, 3) lattice vectors as rows

required
pbc

(3,) which directions are periodic

required
cutoff

maximum pair distance

required
full

emit each pair from both ends

False
self_pairs

allow an atom to pair with its own periodic image

True

Returns:

Type Description
(i, j, shifts)

(P,) int64, (P,) int64, (P, 3) float64, where the pair

vector is positions[j] - positions[i] + shifts @ cell

Source code in chmpy/calc/_celllist.py
def cell_list_pairs(positions, cell, pbc, cutoff, full=False, self_pairs=True):
    """Enumerate pairs within `cutoff`.

    Args:
        positions: (N, 3) Cartesian coordinates, Angstroms
        cell: (3, 3) lattice vectors as rows
        pbc: (3,) which directions are periodic
        cutoff: maximum pair distance
        full: emit each pair from both ends
        self_pairs: allow an atom to pair with its own periodic image

    Returns:
        (i, j, shifts): (P,) int64, (P,) int64, (P, 3) float64, where the pair
        vector is ``positions[j] - positions[i] + shifts @ cell``
    """
    positions = np.ascontiguousarray(positions, dtype=np.float64)
    pbc = np.asarray(pbc, dtype=bool)

    if bool(np.all(pbc)):
        i, j, shifts = _periodic_pairs(
            positions, np.asarray(cell, dtype=np.float64), cutoff, self_pairs
        )
    elif not np.any(pbc):
        i, j, shifts = _free_pairs(positions, cutoff)
    else:
        raise NotImplementedError("mixed periodicity is not handled by the cell list")

    if full:
        i, j = np.concatenate([i, j]), np.concatenate([j, i])
        shifts = np.concatenate([shifts, -shifts])
    return i, j, shifts