2025-08-27 22:07:47 -04:00
|
|
|
#include <cmath>
|
|
|
|
#include <cuda_runtime.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
// Include your header files
|
|
|
|
#include "forces.cuh"
|
2025-09-12 22:47:21 -04:00
|
|
|
#include "kernel_config.cuh"
|
2025-09-10 22:47:54 -04:00
|
|
|
#include "potentials/pair_potentials.cuh"
|
2025-08-27 22:07:47 -04:00
|
|
|
#include "precision.hpp"
|
|
|
|
|
2025-09-10 06:10:36 -04:00
|
|
|
class CudaForceKernelTest : public ::testing::Test {
|
2025-08-27 22:07:47 -04:00
|
|
|
protected:
|
|
|
|
void SetUp() override {
|
|
|
|
// Set up CUDA device
|
|
|
|
cudaError_t err = cudaSetDevice(0);
|
|
|
|
ASSERT_EQ(err, cudaSuccess) << "Failed to set CUDA device";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() override {
|
|
|
|
// Clean up any remaining GPU memory
|
|
|
|
cudaDeviceReset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to check CUDA errors
|
|
|
|
void checkCudaError(cudaError_t err, const std::string &operation) {
|
|
|
|
ASSERT_EQ(err, cudaSuccess)
|
|
|
|
<< "CUDA error in " << operation << ": " << cudaGetErrorString(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to allocate and copy data to GPU
|
|
|
|
template <typename T>
|
|
|
|
T *allocateAndCopyToGPU(const std::vector<T> &host_data) {
|
|
|
|
T *device_ptr;
|
|
|
|
size_t size = host_data.size() * sizeof(T);
|
|
|
|
checkCudaError(cudaMalloc(&device_ptr, size), "cudaMalloc");
|
|
|
|
checkCudaError(
|
|
|
|
cudaMemcpy(device_ptr, host_data.data(), size, cudaMemcpyHostToDevice),
|
|
|
|
"cudaMemcpy H2D");
|
|
|
|
return device_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to copy data from GPU and free GPU memory
|
|
|
|
template <typename T>
|
|
|
|
std::vector<T> copyFromGPUAndFree(T *device_ptr, size_t count) {
|
|
|
|
std::vector<T> host_data(count);
|
|
|
|
size_t size = count * sizeof(T);
|
|
|
|
checkCudaError(
|
|
|
|
cudaMemcpy(host_data.data(), device_ptr, size, cudaMemcpyDeviceToHost),
|
|
|
|
"cudaMemcpy D2H");
|
|
|
|
checkCudaError(cudaFree(device_ptr), "cudaFree");
|
|
|
|
return host_data;
|
|
|
|
}
|
2025-09-10 06:10:36 -04:00
|
|
|
|
|
|
|
// Helper function to run the force calculation kernel
|
2025-09-12 21:44:41 -04:00
|
|
|
std::vector<float4>
|
|
|
|
run_force_calculation(int n_particles, const std::vector<float4> &positions,
|
2025-09-10 06:10:36 -04:00
|
|
|
const std::vector<real> &box_dimensions) {
|
2025-09-12 21:44:41 -04:00
|
|
|
std::vector<float4> force_energies(n_particles,
|
|
|
|
make_float4(0.0, 0.0, 0.0, 0.0));
|
2025-09-10 06:10:36 -04:00
|
|
|
|
2025-09-12 22:47:21 -04:00
|
|
|
KernelConfig kernel_config = get_launch_config(n_particles);
|
2025-09-12 21:44:41 -04:00
|
|
|
float4 *d_positions = allocateAndCopyToGPU(positions);
|
|
|
|
float4 *d_force_energies = allocateAndCopyToGPU(force_energies);
|
2025-09-10 06:10:36 -04:00
|
|
|
real *d_box_len = allocateAndCopyToGPU(box_dimensions);
|
|
|
|
|
2025-09-10 22:47:54 -04:00
|
|
|
std::vector<PairPotentials> potentials = {LennardJones(1.0, 1.0, 3.0)};
|
2025-09-12 21:44:41 -04:00
|
|
|
CAC::launch_force_kernels(d_positions, d_force_energies, n_particles,
|
2025-09-12 22:47:21 -04:00
|
|
|
d_box_len, potentials, kernel_config.blocks,
|
|
|
|
kernel_config.threads);
|
2025-09-10 06:10:36 -04:00
|
|
|
|
|
|
|
checkCudaError(cudaGetLastError(), "kernel launch");
|
|
|
|
checkCudaError(cudaDeviceSynchronize(), "kernel execution");
|
|
|
|
|
2025-09-12 21:44:41 -04:00
|
|
|
std::vector<float4> result_force_energies =
|
|
|
|
copyFromGPUAndFree(d_force_energies, n_particles);
|
2025-09-10 06:10:36 -04:00
|
|
|
|
|
|
|
checkCudaError(cudaFree(d_positions), "cudaFree positions");
|
|
|
|
checkCudaError(cudaFree(d_box_len), "cudaFree box_len");
|
|
|
|
|
2025-09-12 21:44:41 -04:00
|
|
|
return result_force_energies;
|
2025-09-10 06:10:36 -04:00
|
|
|
}
|
2025-08-27 22:07:47 -04:00
|
|
|
};
|
|
|
|
|
2025-09-10 06:10:36 -04:00
|
|
|
TEST_F(CudaForceKernelTest, BasicFunctionalityTest) {
|
|
|
|
const int n_particles = 2;
|
2025-08-27 22:07:47 -04:00
|
|
|
const real tolerance = 1e-5;
|
|
|
|
|
|
|
|
// Set up test data - simple 2x2 grid of particles
|
2025-09-12 21:44:41 -04:00
|
|
|
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
|
2025-08-27 22:07:47 -04:00
|
|
|
};
|
|
|
|
|
2025-09-10 06:10:36 -04:00
|
|
|
std::vector<real> box_dimensions = {10.0, 10.0, 10.0};
|
2025-08-27 22:07:47 -04:00
|
|
|
|
2025-09-12 21:44:41 -04:00
|
|
|
auto result_force_energies =
|
2025-09-10 06:10:36 -04:00
|
|
|
run_force_calculation(n_particles, positions, box_dimensions);
|
2025-08-27 22:07:47 -04:00
|
|
|
|
|
|
|
// Verify results - forces should be non-zero and energies should be
|
|
|
|
// calculated
|
|
|
|
bool has_nonzero_force = false;
|
|
|
|
bool has_nonzero_energy = false;
|
|
|
|
|
2025-09-12 21:44:41 -04:00
|
|
|
for (int i = 0; i < n_particles; i++) {
|
|
|
|
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) {
|
2025-08-27 22:07:47 -04:00
|
|
|
has_nonzero_force = true;
|
|
|
|
}
|
2025-09-12 21:44:41 -04:00
|
|
|
if (std::abs(result_force_energies[i].w) > tolerance) {
|
2025-08-27 22:07:47 -04:00
|
|
|
has_nonzero_energy = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-09-10 06:10:36 -04:00
|
|
|
EXPECT_TRUE(has_nonzero_force)
|
2025-08-27 22:07:47 -04:00
|
|
|
<< "Expected non-zero forces between particles";
|
2025-09-10 06:10:36 -04:00
|
|
|
EXPECT_TRUE(has_nonzero_energy)
|
|
|
|
<< "Expected non-zero energies for particles ";
|
2025-08-27 22:07:47 -04:00
|
|
|
}
|
2025-09-12 06:16:37 -04:00
|
|
|
|
|
|
|
TEST_F(CudaForceKernelTest, PeriodicBoundaryConditionsTest) {
|
|
|
|
const int n_particles = 2;
|
|
|
|
const real tolerance = 1e-5;
|
|
|
|
|
|
|
|
// Place particles near opposite edges of a small box
|
2025-09-12 21:44:41 -04:00
|
|
|
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
|
2025-09-12 06:16:37 -04:00
|
|
|
};
|
|
|
|
std::vector<real> box_dimensions = {5.0, 5.0, 5.0}; // Small box to test PBC
|
|
|
|
|
2025-09-12 21:44:41 -04:00
|
|
|
auto result_force_energies =
|
2025-09-12 06:16:37 -04:00
|
|
|
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)
|
2025-09-12 21:44:41 -04:00
|
|
|
EXPECT_GT(std::abs(result_force_energies[0].x), tolerance)
|
2025-09-12 06:16:37 -04:00
|
|
|
<< "Expected significant force due to PBC";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CudaForceKernelTest, SingleParticleTest) {
|
|
|
|
const int n_particles = 1;
|
|
|
|
|
2025-09-12 21:44:41 -04:00
|
|
|
std::vector<float4> positions = {make_float4(0.0, 0.0, 0.0, 0.0)};
|
2025-09-12 06:16:37 -04:00
|
|
|
std::vector<real> box_dimensions = {10.0, 10.0, 10.0};
|
|
|
|
|
2025-09-12 21:44:41 -04:00
|
|
|
auto result_force_energies =
|
2025-09-12 06:16:37 -04:00
|
|
|
run_force_calculation(n_particles, positions, box_dimensions);
|
|
|
|
// Single particle should have zero force and energy
|
2025-09-12 21:44:41 -04:00
|
|
|
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);
|
2025-09-12 06:16:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CudaForceKernelTest, ForceSymmetryTest) {
|
|
|
|
const int n_particles = 2;
|
|
|
|
const real tolerance = 1e-5;
|
|
|
|
|
2025-09-12 21:44:41 -04:00
|
|
|
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
|
2025-09-12 06:16:37 -04:00
|
|
|
};
|
|
|
|
std::vector<real> box_dimensions = {10.0, 10.0, 10.0};
|
|
|
|
|
2025-09-12 21:44:41 -04:00
|
|
|
auto result_force_energies =
|
2025-09-12 06:16:37 -04:00
|
|
|
run_force_calculation(n_particles, positions, box_dimensions);
|
|
|
|
|
|
|
|
// Newton's third law: forces should be equal and opposite
|
2025-09-12 21:44:41 -04:00
|
|
|
EXPECT_NEAR(result_force_energies[0].x, -result_force_energies[1].x,
|
|
|
|
tolerance)
|
2025-09-12 06:16:37 -04:00
|
|
|
<< "Force x-components should be opposite";
|
2025-09-12 21:44:41 -04:00
|
|
|
EXPECT_NEAR(result_force_energies[0].y, -result_force_energies[1].y,
|
|
|
|
tolerance)
|
2025-09-12 06:16:37 -04:00
|
|
|
<< "Force y-components should be opposite";
|
2025-09-12 21:44:41 -04:00
|
|
|
EXPECT_NEAR(result_force_energies[0].z, -result_force_energies[1].z,
|
|
|
|
tolerance)
|
2025-09-12 06:16:37 -04:00
|
|
|
<< "Force z-components should be opposite";
|
|
|
|
|
|
|
|
// Energies should be equal for symmetric particles
|
2025-09-12 21:44:41 -04:00
|
|
|
EXPECT_NEAR(result_force_energies[0].w, result_force_energies[1].w, tolerance)
|
2025-09-12 06:16:37 -04:00
|
|
|
<< "Energies should be equal";
|
|
|
|
}
|