|
numerics 0.1.0
|
include/spatial/pbc_lattice.hpp provides num::PBCLattice2D, a small struct that precomputes the four periodic-boundary neighbor index arrays for an \(N \times N\) square lattice.
Any 2D simulation on a periodic lattice (Ising, lattice-gas, MD) needs to look up the four neighbors of each site without calling the modulo operator in the hot path. This pattern appeared verbatim in the Ising app as a private build_neighbor_arrays() method with four std::vector<int> members (up, dn, lt, rt).
PBCLattice2D extracts the construction and encapsulates the four arrays.
Construction is \(O(N^2)\) and done once; subsequent lookups are direct array reads.
Flat row-major layout: site \((row, col)\) has flat index \(i = row \cdot N + col\).
\[ \texttt{up}[i] = ((row - 1 + N) \bmod N) \cdot N + col \]
\[ \texttt{dn}[i] = ((row + 1) \bmod N) \cdot N + col \]
\[ \texttt{lt}[i] = row \cdot N + (col - 1 + N) \bmod N \]
\[ \texttt{rt}[i] = row \cdot N + (col + 1) \bmod N \]
Used by: Ising IsingLattice::sweep, IsingLattice::sweep_umbrella.