numerics 0.1.0
Loading...
Searching...
No Matches
Boltzmann Acceptance Table

include/stochastic/boltzmann_table.hpp provides two small utilities in num::markov for computing Metropolis acceptance probabilities.


Motivation

Every Metropolis sweep must decide whether to accept a proposed spin flip. The acceptance probability is \(\min(1,\, e^{-\beta \Delta E})\).

In a typical Ising sweep over \(N^2 = 90\,000\) sites this is evaluated millions of times per second. Calling std::exp at runtime is avoidable because \(\Delta E\) is discrete: for the 2D Ising model,

\[ \Delta E = 2J s \cdot \sum_{\text{nbrs}} s_j - 2F s, \qquad s,\,s_j \in \{-1,+1\},\; \sum_{\text{nbrs}} \in \{-4,-2,0,2,4\} \]

so only 10 distinct values of \(\Delta E\) occur. Pre-computing a \(2 \times 5\) lookup table eliminates exp from the hot path entirely.


API

namespace num::markov {
// Single evaluation: min(1, exp(-beta DeltaE))
double boltzmann_accept(double dE, double beta) noexcept;
// Precompute a table for a discrete DeltaE set
std::vector<double> make_boltzmann_table(const std::vector<double>& dEs, double beta);
}
std::vector< double > make_boltzmann_table(const std::vector< double > &dEs, double beta)
double boltzmann_accept(double dE, double beta) noexcept
Metropolis acceptance probability min(1, exp(-beta*dE)).
real beta(real a, real b)
B(a, b) – beta function.
Definition math.hpp:248

boltzmann_accept replaces the inline ternary (dE <= 0.0) ? 1.0 : std::exp(-beta*dE) that previously appeared directly in rebuild_boltz().


Usage

Direct replacement in Ising

// Before
boltz[si][ni] = (dE <= 0.0) ? 1.0 : std::exp(-beta * dE);
// After
boltz[si][ni] = num::markov::boltzmann_accept(dE, beta);

Building a flat lookup table

// For any solver with discrete energy differences:
std::vector<double> dEs = {-4.0, -2.0, 0.0, 2.0, 4.0}; // scaled by 2J
auto table = num::markov::make_boltzmann_table(dEs, beta);
// table[2] = boltzmann_accept(0.0, beta) = 1.0 (always accept)
// table[3] = boltzmann_accept(2.0, beta) = exp(-2*beta)

Used by: Ising IsingLattice::rebuild_boltz.