Change default precision to float and use float4 for force and potential calculations

This commit is contained in:
Alex Selimov 2025-09-12 21:44:41 -04:00
parent dd83fc6330
commit 130b613a7c
Signed by: aselimov
GPG key ID: 3DDB9C3E023F1F31
9 changed files with 151 additions and 362 deletions

View file

@ -55,33 +55,30 @@ protected:
}
// Helper function to run the force calculation kernel
std::pair<std::vector<real>, std::vector<real>>
run_force_calculation(int n_particles, const std::vector<real> &positions,
std::vector<float4>
run_force_calculation(int n_particles, const std::vector<float4> &positions,
const std::vector<real> &box_dimensions) {
std::vector<real> forces(3 * n_particles, 0.0);
std::vector<real> energies(n_particles, 0.0);
std::vector<float4> force_energies(n_particles,
make_float4(0.0, 0.0, 0.0, 0.0));
real *d_positions = allocateAndCopyToGPU(positions);
real *d_forces = allocateAndCopyToGPU(forces);
real *d_energies = allocateAndCopyToGPU(energies);
float4 *d_positions = allocateAndCopyToGPU(positions);
float4 *d_force_energies = allocateAndCopyToGPU(force_energies);
real *d_box_len = allocateAndCopyToGPU(box_dimensions);
std::vector<PairPotentials> potentials = {LennardJones(1.0, 1.0, 3.0)};
CAC::launch_force_kernels(d_positions, d_forces, d_energies, n_particles,
CAC::launch_force_kernels(d_positions, d_force_energies, n_particles,
d_box_len, potentials, GRID_SIZE, BLOCK_SIZE);
checkCudaError(cudaGetLastError(), "kernel launch");
checkCudaError(cudaDeviceSynchronize(), "kernel execution");
std::vector<real> result_forces =
copyFromGPUAndFree(d_forces, 3 * n_particles);
std::vector<real> result_energies =
copyFromGPUAndFree(d_energies, n_particles);
std::vector<float4> result_force_energies =
copyFromGPUAndFree(d_force_energies, n_particles);
checkCudaError(cudaFree(d_positions), "cudaFree positions");
checkCudaError(cudaFree(d_box_len), "cudaFree box_len");
return {result_forces, result_energies};
return result_force_energies;
}
};
@ -90,14 +87,14 @@ TEST_F(CudaForceKernelTest, BasicFunctionalityTest) {
const real tolerance = 1e-5;
// Set up test data - simple 2x2 grid of particles
std::vector<real> positions = {
0.0, 0.0, 0.0, // particle 0
0.5, 0.0, 0.0, // particle 1
std::vector<float4> positions = {
make_float4(0.0, 0.0, 0.0, 0.0), // particle 0
make_float4(0.5, 0.0, 0.0, 0.0), // particle 1
};
std::vector<real> box_dimensions = {10.0, 10.0, 10.0};
auto [result_forces, result_energies] =
auto result_force_energies =
run_force_calculation(n_particles, positions, box_dimensions);
// Verify results - forces should be non-zero and energies should be
@ -105,17 +102,14 @@ TEST_F(CudaForceKernelTest, BasicFunctionalityTest) {
bool has_nonzero_force = false;
bool has_nonzero_energy = false;
for (int i = 0; i < 3 * n_particles; i++) {
if (std::abs(result_forces[i]) > tolerance) {
has_nonzero_force = true;
break;
}
}
for (int i = 0; i < n_particles; i++) {
if (std::abs(result_energies[i]) > tolerance) {
if (std::abs(result_force_energies[i].x) > tolerance ||
std::abs(result_force_energies[i].y) > tolerance ||
std::abs(result_force_energies[i].z) > tolerance) {
has_nonzero_force = true;
}
if (std::abs(result_force_energies[i].w) > tolerance) {
has_nonzero_energy = true;
break;
}
}
@ -130,60 +124,61 @@ TEST_F(CudaForceKernelTest, PeriodicBoundaryConditionsTest) {
const real tolerance = 1e-5;
// Place particles near opposite edges of a small box
std::vector<real> positions = {
0.1, 0.0, 0.0, // particle 0 near left edge
4.9, 0.0, 0.0 // particle 1 near right edge
std::vector<float4> positions = {
make_float4(0.1, 0.0, 0.0, 0.0), // particle 0 near left edge
make_float4(4.9, 0.0, 0.0, 0.0) // particle 1 near right edge
};
std::vector<real> box_dimensions = {5.0, 5.0, 5.0}; // Small box to test PBC
auto [result_forces, result_energies] =
auto result_force_energies =
run_force_calculation(n_particles, positions, box_dimensions);
// With PBC, particles should interact as if they're close (distance ~0.2)
// rather than far apart (distance ~4.8)
EXPECT_GT(std::abs(result_forces[0]), tolerance)
EXPECT_GT(std::abs(result_force_energies[0].x), tolerance)
<< "Expected significant force due to PBC";
EXPECT_GT(std::abs(result_energies[0]), tolerance)
<< "Expected significant energy due to PBC";
}
TEST_F(CudaForceKernelTest, SingleParticleTest) {
const int n_particles = 1;
std::vector<real> positions = {0.0, 0.0, 0.0};
std::vector<float4> positions = {make_float4(0.0, 0.0, 0.0, 0.0)};
std::vector<real> box_dimensions = {10.0, 10.0, 10.0};
auto [result_forces, result_energies] =
auto result_force_energies =
run_force_calculation(n_particles, positions, box_dimensions);
// Single particle should have zero force and energy
EXPECT_NEAR(result_forces[0], 0.0, 1e-10);
EXPECT_NEAR(result_forces[1], 0.0, 1e-10);
EXPECT_NEAR(result_forces[2], 0.0, 1e-10);
EXPECT_NEAR(result_energies[0], 0.0, 1e-10);
EXPECT_NEAR(result_force_energies[0].x, 0.0, 1e-10);
EXPECT_NEAR(result_force_energies[0].y, 0.0, 1e-10);
EXPECT_NEAR(result_force_energies[0].z, 0.0, 1e-10);
EXPECT_NEAR(result_force_energies[0].w, 0.0, 1e-10);
}
TEST_F(CudaForceKernelTest, ForceSymmetryTest) {
const int n_particles = 2;
const real tolerance = 1e-5;
std::vector<real> positions = {
0.0, 0.0, 0.0, // particle 0
1.5, 0.0, 0.0 // particle 1
std::vector<float4> positions = {
make_float4(0.0, 0.0, 0.0, 0.0), // particle 0
make_float4(1.5, 0.0, 0.0, 0.0) // particle 1
};
std::vector<real> box_dimensions = {10.0, 10.0, 10.0};
auto [result_forces, result_energies] =
auto result_force_energies =
run_force_calculation(n_particles, positions, box_dimensions);
// Newton's third law: forces should be equal and opposite
EXPECT_NEAR(result_forces[0], -result_forces[3], tolerance)
EXPECT_NEAR(result_force_energies[0].x, -result_force_energies[1].x,
tolerance)
<< "Force x-components should be opposite";
EXPECT_NEAR(result_forces[1], -result_forces[4], tolerance)
EXPECT_NEAR(result_force_energies[0].y, -result_force_energies[1].y,
tolerance)
<< "Force y-components should be opposite";
EXPECT_NEAR(result_forces[2], -result_forces[5], tolerance)
EXPECT_NEAR(result_force_energies[0].z, -result_force_energies[1].z,
tolerance)
<< "Force z-components should be opposite";
// Energies should be equal for symmetric particles
EXPECT_NEAR(result_energies[0], result_energies[1], tolerance)
EXPECT_NEAR(result_force_energies[0].w, result_force_energies[1].w, tolerance)
<< "Energies should be equal";
}