numerics
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 lattice.
3///
4/// PBCLattice2D builds up[i], dn[i], lt[i], rt[i] once from modulo arithmetic
5/// so the hot-path (Metropolis sweeps, BFS cluster detection) never calls %.
6///
7/// Flat layout: i = row * N + col, row and col in [0, N).
8#pragma once
9
10#include <vector>
11
12namespace num {
13
14/// 4-neighbor periodic-boundary index arrays for an N×N lattice.
16 int N; ///< Side length; total sites = N*N
17 std::vector<int> up, dn, lt, rt; ///< up/dn = row±1, lt/rt = col±1 (PBC)
18
19 explicit PBCLattice2D(int N)
20 : N(N), up(N*N), dn(N*N), lt(N*N), rt(N*N)
21 {
22 for (int row = 0; row < N; ++row)
23 for (int col = 0; col < N; ++col) {
24 const int i = row * N + col;
25 up[i] = ((row - 1 + N) % N) * N + col;
26 dn[i] = ((row + 1) % N) * N + col;
27 lt[i] = row * N + (col - 1 + N) % N;
28 rt[i] = row * N + (col + 1) % N;
29 }
30 }
31};
32
33} // namespace num
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
4-neighbor periodic-boundary index arrays for an N×N 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