use super::*;
use std::sync::Arc;
fn get_test_device() -> Option<Arc<crate::device::WgpuDevice>> {
crate::device::test_pool::get_test_device_if_f64_gpu_available_sync()
}
#[test]
fn test_coulomb_f64_two_particles() {
let Some(device) = get_test_device() else {
return;
};
let op = CoulombForceF64::new(device).unwrap();
let positions = vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0]; let charges = vec![1.0, -1.0];
let forces = op
.compute_forces(&positions, &charges, Some(1.0), None, Some(1e-10))
.unwrap();
assert!(forces[0] > 0.0, "F_x on particle 0 should be positive");
assert!(forces[1].abs() < 1e-10, "F_y on particle 0 should be ~0");
assert!(forces[2].abs() < 1e-10, "F_z on particle 0 should be ~0");
assert!(forces[3] < 0.0, "F_x on particle 1 should be negative");
assert!(
(forces[0] + forces[3]).abs() < 1e-10,
"Forces should sum to zero"
);
}
#[test]
fn test_coulomb_f64_repulsion() {
let Some(device) = get_test_device() else {
return;
};
let op = CoulombForceF64::new(device).unwrap();
let positions = vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0];
let charges = vec![1.0, 1.0];
let forces = op
.compute_forces(&positions, &charges, Some(1.0), None, Some(1e-10))
.unwrap();
assert!(
forces[0] < 0.0,
"F_x on particle 0 should be negative (repulsion)"
);
assert!(
forces[3] > 0.0,
"F_x on particle 1 should be positive (repulsion)"
);
}
#[test]
fn test_coulomb_f64_distance_scaling() {
let Some(device) = get_test_device() else {
return;
};
let op = CoulombForceF64::new(device).unwrap();
let positions1 = vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0];
let charges = vec![1.0, 1.0];
let forces1 = op
.compute_forces(&positions1, &charges, Some(1.0), None, Some(1e-10))
.unwrap();
let positions2 = vec![0.0, 0.0, 0.0, 2.0, 0.0, 0.0];
let forces2 = op
.compute_forces(&positions2, &charges, Some(1.0), None, Some(1e-10))
.unwrap();
let ratio = forces1[0].abs() / forces2[0].abs();
assert!(
(ratio - 4.0).abs() < 0.01,
"Force should scale as 1/r^2, ratio = {ratio}"
);
}
#[test]
fn test_coulomb_f64_with_energy_gpu() {
let Some(device) = get_test_device() else {
return;
};
let op = CoulombForceF64::new(device).unwrap();
let n = 40;
let mut positions = vec![0.0; n * 3];
let mut charges = vec![0.0; n];
for i in 0..n {
positions[i * 3] = i as f64; charges[i] = if i % 2 == 0 { 1.0 } else { -1.0 };
}
let (forces, energies) = op
.compute_forces_and_energy(&positions, &charges, Some(1.0), None, Some(1e-10))
.unwrap();
assert_eq!(forces.len(), n * 3, "Forces should have 3N elements");
assert_eq!(energies.len(), n, "Energies should have N elements");
let total_energy: f64 = energies.iter().sum();
assert!(
total_energy < 0.0,
"Total energy should be negative for alternating charges"
);
let mid = n / 2;
let fx_mid = forces[mid * 3].abs();
let fx_first = forces[0].abs();
assert!(
fx_mid < fx_first,
"Interior force {fx_mid} should be less than boundary force {fx_first}"
);
}