use crate::device::WgpuDevice;
use crate::device::capabilities::WORKGROUP_SIZE_COMPACT;
use crate::error::Result;
use std::sync::Arc;
pub type CellSortResult = (Vec<f64>, Vec<usize>, Vec<u32>, Vec<u32>);
pub struct YukawaCellListF64 {
device: Arc<WgpuDevice>,
pipeline: wgpu::ComputePipeline,
}
#[derive(Clone, Debug)]
pub struct CellListParams {
pub box_size: [f64; 3],
pub n_cells: [usize; 3],
pub cutoff: f64,
pub kappa: f64,
pub prefactor: f64,
pub epsilon: f64,
}
impl YukawaCellListF64 {
pub fn new(device: Arc<WgpuDevice>) -> Result<Self> {
let shader_source = include_str!("yukawa_celllist_f64.wgsl");
let shader_module =
device.compile_shader_f64(shader_source, Some("YukawaCellListF64 Shader"));
let pipeline = device
.device
.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: Some("YukawaCellListF64 Pipeline"),
layout: None,
module: &shader_module,
entry_point: Some("main"),
cache: None,
compilation_options: Default::default(),
});
Ok(Self { device, pipeline })
}
pub fn compute_forces(
&self,
positions: &[f64],
params: &CellListParams,
) -> Result<(Vec<f64>, Vec<f64>)> {
let n = positions.len() / 3;
if n == 0 {
return Ok((vec![], vec![]));
}
self.compute_gpu(positions, params)
}
fn compute_gpu(
&self,
positions: &[f64],
params: &CellListParams,
) -> Result<(Vec<f64>, Vec<f64>)> {
let n = positions.len() / 3;
let n_cells_total = params.n_cells[0] * params.n_cells[1] * params.n_cells[2];
let (sorted_pos, _indices, cell_start, cell_count) =
self.sort_particles_by_cell(positions, params)?;
let cell_size = [
params.box_size[0] / params.n_cells[0] as f64,
params.box_size[1] / params.n_cells[1] as f64,
params.box_size[2] / params.n_cells[2] as f64,
];
let gpu_params: Vec<f64> = vec![
n as f64,
params.kappa,
params.prefactor,
params.cutoff * params.cutoff,
params.box_size[0],
params.box_size[1],
params.box_size[2],
params.epsilon,
params.n_cells[0] as f64,
params.n_cells[1] as f64,
params.n_cells[2] as f64,
cell_size[0],
cell_size[1],
cell_size[2],
n_cells_total as f64,
0.0, ];
let pos_bytes: &[u8] = bytemuck::cast_slice(&sorted_pos);
let params_bytes: &[u8] = bytemuck::cast_slice(&gpu_params);
let cs_bytes: &[u8] = bytemuck::cast_slice(&cell_start);
let cc_bytes: &[u8] = bytemuck::cast_slice(&cell_count);
let pos_buffer = self
.device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("YCL Positions"),
contents: pos_bytes,
usage: wgpu::BufferUsages::STORAGE,
});
let forces_buffer = self.device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("YCL Forces"),
size: (n * 3 * 8) as u64,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
let pe_buffer = self.device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("YCL PE"),
size: (n * 8) as u64,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
let params_buffer =
self.device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("YCL Params"),
contents: params_bytes,
usage: wgpu::BufferUsages::STORAGE,
});
let cs_buffer = self
.device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("YCL CellStart"),
contents: cs_bytes,
usage: wgpu::BufferUsages::STORAGE,
});
let cc_buffer = self
.device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("YCL CellCount"),
contents: cc_bytes,
usage: wgpu::BufferUsages::STORAGE,
});
let bind_group_layout = self.pipeline.get_bind_group_layout(0);
let bind_group = self
.device
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("YCL Bind Group"),
layout: &bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: pos_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: forces_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: pe_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: params_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 4,
resource: cs_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 5,
resource: cc_buffer.as_entire_binding(),
},
],
});
let n_workgroups = (n as u32).div_ceil(WORKGROUP_SIZE_COMPACT);
let mut encoder = self
.device
.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
label: Some("YCL Encoder"),
});
{
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("YCL Pass"),
timestamp_writes: None,
});
pass.set_pipeline(&self.pipeline);
pass.set_bind_group(0, Some(&bind_group), &[]);
pass.dispatch_workgroups(n_workgroups, 1, 1);
}
self.device.submit_commands(Some(encoder.finish()));
let forces = self.read_f64_buffer(&forces_buffer, n * 3)?;
let energies = self.read_f64_buffer(&pe_buffer, n)?;
let (_, original_indices, _, _) = self.sort_particles_by_cell(positions, params)?;
let mut forces_unsorted = vec![0.0f64; n * 3];
let mut energies_unsorted = vec![0.0f64; n];
for (sorted_idx, &orig_idx) in original_indices.iter().enumerate() {
forces_unsorted[orig_idx * 3] = forces[sorted_idx * 3];
forces_unsorted[orig_idx * 3 + 1] = forces[sorted_idx * 3 + 1];
forces_unsorted[orig_idx * 3 + 2] = forces[sorted_idx * 3 + 2];
energies_unsorted[orig_idx] = energies[sorted_idx];
}
Ok((forces_unsorted, energies_unsorted))
}
fn read_f64_buffer(&self, buffer: &wgpu::Buffer, count: usize) -> Result<Vec<f64>> {
let staging = self.device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("YCL Staging"),
size: (count * 8) as u64,
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
mapped_at_creation: false,
});
let mut encoder = self
.device
.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
label: Some("YCL Copy"),
});
encoder.copy_buffer_to_buffer(buffer, 0, &staging, 0, (count * 8) as u64);
self.device.submit_commands(Some(encoder.finish()));
let results: Vec<f64> = self.device.map_staging_buffer(&staging, count)?;
Ok(results)
}
pub fn sort_particles_by_cell(
&self,
positions: &[f64],
params: &CellListParams,
) -> Result<CellSortResult> {
let n = positions.len() / 3;
let n_cells_total = params.n_cells[0] * params.n_cells[1] * params.n_cells[2];
let mut particle_cells: Vec<(usize, usize)> = (0..n)
.map(|i| {
let cell = self.get_cell_index(
positions[i * 3],
positions[i * 3 + 1],
positions[i * 3 + 2],
params,
);
(i, cell)
})
.collect();
particle_cells.sort_by_key(|&(_, cell)| cell);
let mut sorted_positions = vec![0.0f64; n * 3];
let particle_indices: Vec<usize> = particle_cells.iter().map(|&(i, _)| i).collect();
for (new_idx, &(old_idx, _)) in particle_cells.iter().enumerate() {
sorted_positions[new_idx * 3] = positions[old_idx * 3];
sorted_positions[new_idx * 3 + 1] = positions[old_idx * 3 + 1];
sorted_positions[new_idx * 3 + 2] = positions[old_idx * 3 + 2];
}
let mut cell_start = vec![0u32; n_cells_total];
let mut cell_count = vec![0u32; n_cells_total];
let mut current_cell = usize::MAX;
for (idx, &(_, cell)) in particle_cells.iter().enumerate() {
if cell != current_cell {
cell_start[cell] = idx as u32;
current_cell = cell;
}
cell_count[cell] += 1;
}
Ok((sorted_positions, particle_indices, cell_start, cell_count))
}
#[expect(
dead_code,
reason = "CPU reference implementation for GPU parity validation"
)]
fn build_cell_list(
&self,
positions: &[f64],
params: &CellListParams,
) -> Result<Vec<Vec<usize>>> {
let n = positions.len() / 3;
let n_cells_total = params.n_cells[0] * params.n_cells[1] * params.n_cells[2];
let mut cells: Vec<Vec<usize>> = vec![Vec::new(); n_cells_total];
for i in 0..n {
let cell = self.get_cell_index(
positions[i * 3],
positions[i * 3 + 1],
positions[i * 3 + 2],
params,
);
cells[cell].push(i);
}
Ok(cells)
}
fn get_cell_index(&self, x: f64, y: f64, z: f64, params: &CellListParams) -> usize {
let cell_size = [
params.box_size[0] / params.n_cells[0] as f64,
params.box_size[1] / params.n_cells[1] as f64,
params.box_size[2] / params.n_cells[2] as f64,
];
let cx = ((x / cell_size[0]).floor() as usize).min(params.n_cells[0] - 1);
let cy = ((y / cell_size[1]).floor() as usize).min(params.n_cells[1] - 1);
let cz = ((z / cell_size[2]).floor() as usize).min(params.n_cells[2] - 1);
cx + cy * params.n_cells[0] + cz * params.n_cells[0] * params.n_cells[1]
}
#[expect(
dead_code,
reason = "CPU reference implementation for GPU parity validation"
)]
fn compute_cpu(
&self,
positions: &[f64],
params: &CellListParams,
cells: &[Vec<usize>],
) -> (Vec<f64>, Vec<f64>) {
let n = positions.len() / 3;
let mut forces = vec![0.0f64; n * 3];
let mut energies = vec![0.0f64; n];
let cutoff_sq = params.cutoff * params.cutoff;
let eps_sq = params.epsilon * params.epsilon;
for (cell_idx, cell_particles) in cells.iter().enumerate() {
let neighbors = self.get_neighbor_cells(cell_idx, params);
for &i in cell_particles {
let xi = positions[i * 3];
let yi = positions[i * 3 + 1];
let zi = positions[i * 3 + 2];
for &neighbor_cell in &neighbors {
for &j in &cells[neighbor_cell] {
if i >= j {
continue; }
let xj = positions[j * 3];
let yj = positions[j * 3 + 1];
let zj = positions[j * 3 + 2];
let dx = self.pbc_delta(xj - xi, params.box_size[0]);
let dy = self.pbc_delta(yj - yi, params.box_size[1]);
let dz = self.pbc_delta(zj - zi, params.box_size[2]);
let r_sq = dz.mul_add(dz, dx.mul_add(dx, dy * dy)) + eps_sq;
if r_sq > cutoff_sq {
continue;
}
let r = r_sq.sqrt();
let exp_kr = (-params.kappa * r).exp();
let u = params.prefactor * exp_kr / r;
let f_over_r = params.prefactor * exp_kr * (params.kappa + 1.0 / r) / r_sq;
let fx = f_over_r * dx;
let fy = f_over_r * dy;
let fz = f_over_r * dz;
forces[i * 3] += fx;
forces[i * 3 + 1] += fy;
forces[i * 3 + 2] += fz;
forces[j * 3] -= fx;
forces[j * 3 + 1] -= fy;
forces[j * 3 + 2] -= fz;
energies[i] = 0.5f64.mul_add(u, energies[i]);
energies[j] = 0.5f64.mul_add(u, energies[j]);
}
}
}
}
(forces, energies)
}
fn pbc_delta(&self, delta: f64, box_size: f64) -> f64 {
box_size.mul_add(-(delta / box_size).round(), delta)
}
fn get_neighbor_cells(&self, cell_idx: usize, params: &CellListParams) -> Vec<usize> {
let nx = params.n_cells[0];
let ny = params.n_cells[1];
let nz = params.n_cells[2];
let cz = cell_idx / (nx * ny);
let cy = (cell_idx % (nx * ny)) / nx;
let cx = cell_idx % nx;
let mut neighbors = Vec::with_capacity(27);
for dz in [-1i32, 0, 1] {
for dy in [-1i32, 0, 1] {
for dx in [-1i32, 0, 1] {
let ncx = ((cx as i32 + dx + nx as i32) % nx as i32) as usize;
let ncy = ((cy as i32 + dy + ny as i32) % ny as i32) as usize;
let ncz = ((cz as i32 + dz + nz as i32) % nz as i32) as usize;
neighbors.push(ncx + ncy * nx + ncz * nx * ny);
}
}
}
neighbors
}
}
#[cfg(test)]
mod tests {
use super::*;
fn get_test_device() -> Option<Arc<crate::device::WgpuDevice>> {
crate::device::test_pool::get_test_device_if_f64_gpu_available_sync()
}
#[test]
fn test_cell_list_two_particles() {
let Some(device) = get_test_device() else {
return;
};
let op = YukawaCellListF64::new(device).unwrap();
let positions = vec![0.5, 0.5, 0.5, 1.5, 0.5, 0.5];
let params = CellListParams {
box_size: [10.0, 10.0, 10.0],
n_cells: [5, 5, 5],
cutoff: 5.0,
kappa: 1.0,
prefactor: 1.0,
epsilon: 1e-10,
};
let (forces, energies) = op.compute_forces(&positions, ¶ms).unwrap();
assert!(
(forces[0] + forces[3]).abs() < 1e-10,
"Forces should be equal and opposite"
);
assert!(
(energies[0] - energies[1]).abs() < 1e-10,
"Energies should be equal"
);
}
#[test]
fn test_cell_list_pbc() {
let Some(device) = get_test_device() else {
return;
};
let op = YukawaCellListF64::new(device).unwrap();
let positions = vec![0.5, 0.5, 0.5, 9.5, 0.5, 0.5];
let params = CellListParams {
box_size: [10.0, 10.0, 10.0],
n_cells: [5, 5, 5],
cutoff: 5.0,
kappa: 1.0,
prefactor: 1.0,
epsilon: 1e-10,
};
let (forces, energies) = op.compute_forces(&positions, ¶ms).unwrap();
assert!(energies[0].abs() > 1e-10, "Should have interaction via PBC");
assert!(
forces[0] > 0.0,
"Particle 0 should be pushed in +x (away from PBC image): {}",
forces[0]
);
}
}