pub mod complex_f64;
pub mod lcg;
pub mod su3;
pub mod su3_extended;
pub mod shader_sources;
pub use shader_sources::{
lattice_preamble, lattice_preamble_df64, lattice_shader_names, lattice_shader_source,
};
pub mod absorbed_shaders;
pub mod cg;
pub mod dirac;
pub mod gpu_cg_resident;
pub mod gpu_cg_solver;
pub mod gpu_hmc_leapfrog;
pub mod gpu_hmc_trajectory;
pub mod gpu_hmc_types;
pub mod gpu_kinetic_energy;
pub mod gpu_lattice_init;
pub mod gpu_multi_shift_cg;
pub mod gpu_polyakov;
pub mod gpu_pseudofermion;
pub mod gpu_resident_observables;
pub mod gpu_wilson_action;
pub mod higgs_u1;
pub mod hmc_force_su3;
pub mod omelyan_integrator;
pub mod plaquette;
pub mod rop_force_accum;
pub mod tmu_tables;
pub mod constants;
pub mod cpu_complex;
#[cfg(test)]
pub mod cpu_dirac;
#[cfg(test)]
pub mod cpu_su3;
#[cfg(test)]
pub mod pseudofermion;
pub mod rhmc;
#[cfg(test)]
pub mod rhmc_hmc;
#[cfg(test)]
pub mod wilson;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub enum NeighborMode {
OnTheFly,
PrecomputedBuffer(Vec<u32>),
}
#[inline]
#[must_use]
pub fn action_density(avg_plaquette: f64) -> f64 {
6.0 * (1.0 - avg_plaquette)
}
impl NeighborMode {
#[must_use]
pub fn precompute_periodic_2d(l: usize) -> Self {
let n_sites = l * l;
let mut table = Vec::with_capacity(n_sites * 4);
let l_u = l as u32;
for y in 0..l {
for x in 0..l {
let idx = |x: u32, y: u32| -> u32 { y * l_u + x };
let x = x as u32;
let y = y as u32;
table.push(idx((x + 1) % l_u, y));
table.push(idx((x + l_u - 1) % l_u, y));
table.push(idx(x, (y + 1) % l_u));
table.push(idx(x, (y + l_u - 1) % l_u));
}
}
Self::PrecomputedBuffer(table)
}
#[must_use]
pub fn precompute_periodic_3d(l: usize) -> Self {
let n_sites = l * l * l;
let mut table = Vec::with_capacity(n_sites * 6);
let l_u = l as u32;
for z in 0..l {
for y in 0..l {
for x in 0..l {
let idx = |x: u32, y: u32, z: u32| -> u32 { z * l_u * l_u + y * l_u + x };
let x = x as u32;
let y = y as u32;
let z = z as u32;
table.push(idx((x + 1) % l_u, y, z));
table.push(idx((x + l_u - 1) % l_u, y, z));
table.push(idx(x, (y + 1) % l_u, z));
table.push(idx(x, (y + l_u - 1) % l_u, z));
table.push(idx(x, y, (z + 1) % l_u));
table.push(idx(x, y, (z + l_u - 1) % l_u));
}
}
}
Self::PrecomputedBuffer(table)
}
#[must_use]
pub fn precompute_periodic_4d(dims: [u32; 4]) -> Self {
let [nx, ny, nz, nt] = dims;
let n_sites = (nx * ny * nz * nt) as usize;
let mut table = Vec::with_capacity(n_sites * 8);
for t in 0..nt {
for z in 0..nz {
for y in 0..ny {
for x in 0..nx {
let idx = |x: u32, y: u32, z: u32, t: u32| -> u32 {
t * nz * ny * nx + z * ny * nx + y * nx + x
};
table.push(idx((x + 1) % nx, y, z, t));
table.push(idx((x + nx - 1) % nx, y, z, t));
table.push(idx(x, (y + 1) % ny, z, t));
table.push(idx(x, (y + ny - 1) % ny, z, t));
table.push(idx(x, y, (z + 1) % nz, t));
table.push(idx(x, y, (z + nz - 1) % nz, t));
table.push(idx(x, y, z, (t + 1) % nt));
table.push(idx(x, y, z, (t + nt - 1) % nt));
}
}
}
}
Self::PrecomputedBuffer(table)
}
#[must_use]
pub fn create_gpu_buffer(
&self,
device: &crate::device::WgpuDevice,
) -> Option<Arc<wgpu::Buffer>> {
let table = match self {
Self::OnTheFly => return None,
Self::PrecomputedBuffer(t) => t,
};
let buffer = device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Neighbor Table"),
contents: bytemuck::cast_slice(table),
usage: wgpu::BufferUsages::STORAGE,
});
Some(Arc::new(buffer))
}
}
#[cfg(test)]
mod action_density_tests {
use super::action_density;
#[test]
fn cold_start_zero() {
assert!((action_density(1.0)).abs() < 1e-15);
}
#[test]
fn strong_coupling() {
let ad = action_density(0.0);
assert!((ad - 6.0).abs() < 1e-15);
}
#[test]
fn typical_value() {
let ad = action_density(0.55);
assert!((ad - 2.7).abs() < 1e-14);
}
}
#[cfg(test)]
mod neighbor_tests {
use super::NeighborMode;
#[test]
fn precompute_2d_table_size() {
let mode = NeighborMode::precompute_periodic_2d(8);
let NeighborMode::PrecomputedBuffer(table) = &mode else {
panic!("expected PrecomputedBuffer");
};
assert_eq!(table.len(), 8 * 8 * 4, "8×8 lattice × 4 neighbors = 256");
}
#[test]
fn precompute_2d_periodic_boundary() {
let mode = NeighborMode::precompute_periodic_2d(4);
let NeighborMode::PrecomputedBuffer(table) = &mode else {
panic!("expected PrecomputedBuffer");
};
const NEIGHBORS_2D: usize = 4;
let site = 0;
let nbr_plus_x = table[site * NEIGHBORS_2D];
let nbr_minus_x = table[site * NEIGHBORS_2D + 1];
let nbr_plus_y = table[site * NEIGHBORS_2D + 2];
let nbr_minus_y = table[site * NEIGHBORS_2D + 3];
assert_eq!(nbr_plus_x, 1, "site (0,0) +x -> (1,0)");
assert_eq!(nbr_minus_x, 3, "site (0,0) -x wraps to (3,0)");
assert_eq!(nbr_plus_y, 4, "site (0,0) +y -> (0,1)");
assert_eq!(nbr_minus_y, 12, "site (0,0) -y wraps to (0,3)");
}
#[test]
fn precompute_3d_table_size() {
let mode = NeighborMode::precompute_periodic_3d(4);
let NeighborMode::PrecomputedBuffer(table) = &mode else {
panic!("expected PrecomputedBuffer");
};
assert_eq!(table.len(), 4 * 4 * 4 * 6, "4³ lattice × 6 neighbors = 384");
}
#[test]
fn precompute_3d_periodic_wrap() {
let mode = NeighborMode::precompute_periodic_3d(3);
let NeighborMode::PrecomputedBuffer(table) = &mode else {
panic!("expected PrecomputedBuffer");
};
const NEIGHBORS_3D: usize = 6;
let site = 0;
let nbr_minus_x = table[site * NEIGHBORS_3D + 1];
let nbr_minus_y = table[site * NEIGHBORS_3D + 3];
let nbr_minus_z = table[site * NEIGHBORS_3D + 5];
assert_eq!(nbr_minus_x, 2, "site (0,0,0) -x wraps to (2,0,0)");
assert_eq!(nbr_minus_y, 6, "site (0,0,0) -y wraps to (0,2,0)");
assert_eq!(nbr_minus_z, 18, "site (0,0,0) -z wraps to (0,0,2)");
}
#[test]
fn precompute_4d_table_size() {
let mode = NeighborMode::precompute_periodic_4d([4, 4, 4, 4]);
let NeighborMode::PrecomputedBuffer(table) = &mode else {
panic!("expected PrecomputedBuffer");
};
assert_eq!(table.len(), 2048);
}
#[test]
fn precompute_4d_periodic_boundary() {
let mode = NeighborMode::precompute_periodic_4d([4, 4, 4, 4]);
let NeighborMode::PrecomputedBuffer(table) = &mode else {
panic!("expected PrecomputedBuffer");
};
let idx = |x: u32, y: u32, z: u32, t: u32| -> u32 { t * 4 * 4 * 4 + z * 4 * 4 + y * 4 + x };
const NEIGHBORS_4D: usize = 8;
let site = 0;
let nbr_plus_x = table[site * NEIGHBORS_4D];
let nbr_minus_x = table[site * NEIGHBORS_4D + 1];
assert_eq!(nbr_plus_x, idx(1, 0, 0, 0));
assert_eq!(nbr_minus_x, idx(3, 0, 0, 0));
}
}