numerics 0.1.0
Loading...
Searching...
No Matches
pbc_lattice.hpp
Go to the documentation of this file.
1/// @file spatial/pbc_lattice.hpp
2/// @brief Precomputed periodic-boundary neighbor arrays for a 2D square
3/// lattice.
4///
5/// PBCLattice2D builds up[i], dn[i], lt[i], rt[i] once from modulo arithmetic
6/// so the hot-path (Metropolis sweeps, BFS cluster detection) never calls %.
7///
8/// Flat layout: i = row * N + col, row and col in [0, N).
9#pragma once
10
11#include <vector>
12
13namespace num {
14
15/// 4-neighbor periodic-boundary index arrays for an NxN lattice.
17 int N; ///< Side length; total sites = N*N
18 std::vector<int> up, dn, lt,
19 rt; ///< up/dn = row +/-1, lt/rt = col +/-1 (PBC)
20
21 explicit PBCLattice2D(int N)
22 : N(N)
23 , up(N * N)
24 , dn(N * N)
25 , lt(N * N)
26 , rt(N * N) {
27 for (int row = 0; row < N; ++row)
28 for (int col = 0; col < N; ++col) {
29 const int i = row * N + col;
30 up[i] = ((row - 1 + N) % N) * N + col;
31 dn[i] = ((row + 1) % N) * N + col;
32 lt[i] = row * N + (col - 1 + N) % N;
33 rt[i] = row * N + (col + 1) % N;
34 }
35 }
36};
37
38} // namespace num
4-neighbor periodic-boundary index arrays for an NxN lattice.
std::vector< int > up
std::vector< int > dn
std::vector< int > rt
up/dn = row +/-1, lt/rt = col +/-1 (PBC)
int N
Side length; total sites = N*N.
std::vector< int > lt