Fix force tests

This commit is contained in:
Alex Selimov 2025-09-12 06:16:37 -04:00
parent ac44ceaab1
commit fe6d88306f
Signed by: aselimov
GPG key ID: 3DDB9C3E023F1F31
2 changed files with 62 additions and 69 deletions

View file

@ -92,7 +92,7 @@ TEST_F(CudaForceKernelTest, BasicFunctionalityTest) {
// Set up test data - simple 2x2 grid of particles
std::vector<real> positions = {
0.0, 0.0, 0.0, // particle 0
1.0, 0.0, 0.0, // particle 1
0.5, 0.0, 0.0, // particle 1
};
std::vector<real> box_dimensions = {10.0, 10.0, 10.0};
@ -124,67 +124,66 @@ TEST_F(CudaForceKernelTest, BasicFunctionalityTest) {
EXPECT_TRUE(has_nonzero_energy)
<< "Expected non-zero energies for particles ";
}
//
// TEST_F(CudaKernelTest, PeriodicBoundaryConditionsTest) {
// const int n_particles = 2;
// 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<real> box_dimensions = {5.0, 5.0, 5.0}; // Small box to test
// PBC
//
// auto [result_forces, result_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)
// << "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<real> box_dimensions = {10.0, 10.0, 10.0};
//
// auto [result_forces, result_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);
// }
TEST_F(CudaForceKernelTest, PeriodicBoundaryConditionsTest) {
const int n_particles = 2;
const real tolerance = 1e-5;
// TEST_F(CudaKernelTest, 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<real> box_dimensions = {10.0, 10.0, 10.0};
//
// auto [result_forces, result_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)
// << "Force x-components should be opposite";
// EXPECT_NEAR(result_forces[1], -result_forces[4], tolerance)
// << "Force y-components should be opposite";
// EXPECT_NEAR(result_forces[2], -result_forces[5], tolerance)
// << "Force z-components should be opposite";
//
// // Energies should be equal for symmetric particles
// EXPECT_NEAR(result_energies[0], result_energies[1], tolerance)
// << "Energies should be equal";
// }
// 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<real> box_dimensions = {5.0, 5.0, 5.0}; // Small box to test PBC
auto [result_forces, result_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)
<< "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<real> box_dimensions = {10.0, 10.0, 10.0};
auto [result_forces, result_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);
}
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<real> box_dimensions = {10.0, 10.0, 10.0};
auto [result_forces, result_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)
<< "Force x-components should be opposite";
EXPECT_NEAR(result_forces[1], -result_forces[4], tolerance)
<< "Force y-components should be opposite";
EXPECT_NEAR(result_forces[2], -result_forces[5], tolerance)
<< "Force z-components should be opposite";
// Energies should be equal for symmetric particles
EXPECT_NEAR(result_energies[0], result_energies[1], tolerance)
<< "Energies should be equal";
}