Add pair potential and tests

This commit is contained in:
Alex Selimov 2025-04-16 14:01:40 -04:00
parent dfd6f43e9b
commit 6162b27a89
11 changed files with 330 additions and 14 deletions

View File

@ -1,16 +1,17 @@
project(${CMAKE_PROJECT_NAME}_lib CUDA CXX)
set(HEADER_FILES
./test.h
particle.hpp
simulation.hpp
box.hpp
pair_potentials.hpp
)
set(SOURCE_FILES
./test.cpp
pair_potentials.cpp
)
# The library contains header and source files.
add_library(${CMAKE_PROJECT_NAME}_lib
${SOURCE_FILES}
${HEADER_FILES}
${SOURCE_FILES}
)
target_include_directories(${CMAKE_PROJECT_NAME}_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

22
src/box.hpp Normal file
View File

@ -0,0 +1,22 @@
#ifndef BOX_H
#define BOX_H
/**
* Struct representing the simulation box.
* Currently the simulation box is always assumed to be perfectly rectangular.
* This code does not support shearing the box. This functionality may be added
* in later.
*/
template <typename T> struct Box {
T xlo;
T xhi;
T ylo;
T yhi;
T zlo;
T zhi;
bool x_is_periodic;
bool y_is_periodic;
bool z_is_periodic;
};
#endif

33
src/pair_potentials.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "potentials.hpp"
#include <cmath>
/**
* Calculate the Lennard-Jones energy and force for the current particle pair
* described by displacement vector r
*/
ForceAndEnergy LennardJones::calc_force_and_energy(Vec3<real> r) {
real rmagsq = r.squared_norm2();
if (rmagsq < this->m_rcutoffsq && rmagsq > 0.0) {
real inv_rmag = 1 / std::sqrt(rmagsq);
// Pre-Compute the terms (doing this saves on multiple devisions/pow
// function call)
real sigma_r = m_sigma / inv_rmag;
real sigma_r5 = sigma_r * sigma_r * sigma_r * sigma_r * sigma_r;
real sigma_r6 = sigma_r5 * sigma_r;
real sigma_r11 = sigma_r5 * sigma_r5 * sigma_r;
real sigma_r12 = sigma_r6 * sigma_r6;
// Get the energy
real energy = 4.0 * m_epsilon * (sigma_r12 - sigma_r6);
// Get the force vector
real force_mag = 4.0 * m_epsilon * (6.0 * sigma_r5 - 12.0 * sigma_r11);
Vec3<real> force = r.scale(force_mag * inv_rmag);
return {energy, force};
} else {
return ForceAndEnergy::zero();
}
};

41
src/pair_potentials.hpp Normal file
View File

@ -0,0 +1,41 @@
#ifndef POTENTIALS_H
#define POTENTIALS_H
#include "precision.hpp"
#include "vec3.h"
/**
* Result struct for the Pair Potential
*/
struct ForceAndEnergy {
real energy;
Vec3<real> force;
inline static ForceAndEnergy zero() { return {0.0, {0.0, 0.0, 0.0}}; };
};
/**
* Abstract implementation of a Pair Potential.
* Pair potentials are potentials which depend solely on the distance
* between two particles. These do not include multi-body potentials such as
* EAM
*
*/
struct PairPotential {
real m_rcutoffsq;
/**
* Calculate the force and energy for a specific atom pair based on a
* displacement vector r.
*/
virtual ForceAndEnergy calc_force_and_energy(Vec3<real> r) = 0;
};
struct LennardJones : PairPotential {
real m_epsilon;
real m_sigma;
ForceAndEnergy calc_force_and_energy(Vec3<real> r);
};
#endif

18
src/particle.hpp Normal file
View File

@ -0,0 +1,18 @@
#ifndef PARTICLE_H
#define PARTICLE_H
#include "vec3.h"
/**
* Class representing a single molecular dynamics particle.
* This class is only used on the host side of the code and is converted
* to the device arrays.
*/
template <typename T = float> struct Particle {
Vec3<T> pos;
Vec3<T> vel;
Vec3<T> force;
T mass;
};
#endif

15
src/precision.hpp Normal file
View File

@ -0,0 +1,15 @@
#ifndef PRECISION_H
#define PRECISION_H
#ifdef USE_FLOATS
/*
* If macro USE_FLOATS is set then the default type will be floating point
* precision. Otherwise we use double precision by default
*/
typedef float real;
#else
typedef double real;
#endif
#endif

17
src/simulation.hpp Normal file
View File

@ -0,0 +1,17 @@
#ifndef SIMULATION_H
#define SIMULATION_H
#include "box.hpp"
#include "particle.hpp"
#include <vector>
template <typename T> class Simulation {
// Simulation State variables
T timestep;
Box<T> box;
// Host Data
std::vector<Particle<T>> particles;
};
#endif

View File

@ -1,4 +0,0 @@
#include "test.h"
#include <iostream>
void test_hello() { std::cout << "Hello!"; }

View File

@ -1,2 +0,0 @@
#include <iostream>
void test_hello();

View File

@ -1,8 +1,9 @@
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
add_executable(Unit_Tests_run
test_example.cpp
test_potential.cpp
)
target_link_libraries(Unit_Tests_run gtest gtest_main)
target_link_libraries(Unit_Tests_run ${CMAKE_PROJECT_NAME}_lib)
add_test(Name Tests COMMAND Unit_Tests_run)

View File

@ -0,0 +1,174 @@
#include "potentials.hpp"
#include "precision.hpp"
#include "gtest/gtest.h"
#include <cmath>
class LennardJonesTest : public ::testing::Test {
protected:
void SetUp() override {
// Default parameters
sigma = 1.0;
epsilon = 1.0;
rCutoff = 2.5;
// Create default LennardJones object
lj = new LennardJones(sigma, epsilon, rCutoff);
}
void TearDown() override { delete lj; }
real sigma;
real epsilon;
real rCutoff;
LennardJones *lj;
// Helper function to compare Vec3 values with tolerance
void expectVec3Near(const Vec3<real> &expected, const Vec3<real> &actual,
real tolerance) {
EXPECT_NEAR(expected.x, actual.x, tolerance);
EXPECT_NEAR(expected.y, actual.y, tolerance);
EXPECT_NEAR(expected.z, actual.z, tolerance);
}
};
TEST_F(LennardJonesTest, ZeroDistance) {
// At zero distance, the calculation should return zero force and energy
Vec3<real> r(0.0, 0.0, 0.0);
auto result = lj->calc_force_and_energy(r);
EXPECT_EQ(0.0, result.energy);
expectVec3Near(Vec3<real>(0.0, 0.0, 0.0), result.force, 1e-10);
}
TEST_F(LennardJonesTest, BeyondCutoff) {
// Distance beyond cutoff should return zero force and energy
Vec3<real> r(3.0, 0.0, 0.0); // 3.0 > rCutoff (2.5)
auto result = lj->calc_force_and_energy(r);
EXPECT_EQ(0.0, result.energy);
expectVec3Near(Vec3<real>(0.0, 0.0, 0.0), result.force, 1e-10);
}
TEST_F(LennardJonesTest, AtMinimum) {
// The LJ potential has a minimum at r = 2^(1/6) * sigma
real min_dist = std::pow(2.0, 1.0 / 6.0) * sigma;
Vec3<real> r(min_dist, 0.0, 0.0);
auto result = lj->calc_force_and_energy(r);
// At minimum, force should be close to zero
EXPECT_NEAR(-epsilon, result.energy, 1e-10);
expectVec3Near(Vec3<real>(0.0, 0.0, 0.0), result.force, 1e-10);
}
TEST_F(LennardJonesTest, AtEquilibrium) {
// At r = sigma, the energy should be zero and force should be repulsive
Vec3<real> r(sigma, 0.0, 0.0);
auto result = lj->calc_force_and_energy(r);
EXPECT_NEAR(0.0, result.energy, 1e-10);
EXPECT_GT(result.force.x,
0.0); // Force should be repulsive (positive x-direction)
EXPECT_NEAR(0.0, result.force.y, 1e-10);
EXPECT_NEAR(0.0, result.force.z, 1e-10);
}
TEST_F(LennardJonesTest, RepulsiveRegion) {
// Test in the repulsive region (r < sigma)
Vec3<real> r(0.8 * sigma, 0.0, 0.0);
auto result = lj->calc_force_and_energy(r);
// Energy should be positive and force should be repulsive
EXPECT_GT(result.energy, 0.0);
EXPECT_GT(result.force.x, 0.0); // Force should be repulsive
}
TEST_F(LennardJonesTest, AttractiveRegion) {
// Test in the attractive region (sigma < r < r_min)
Vec3<real> r(1.5 * sigma, 0.0, 0.0);
auto result = lj->calc_force_and_energy(r);
// Energy should be negative and force should be attractive
EXPECT_LT(result.energy, 0.0);
EXPECT_LT(result.force.x,
0.0); // Force should be attractive (negative x-direction)
}
TEST_F(LennardJonesTest, ArbitraryDirection) {
// Test with a vector in an arbitrary direction
Vec3<real> r(1.0, 1.0, 1.0);
auto result = lj->calc_force_and_energy(r);
// The force should be in the same direction as r but opposite sign
// (attractive region)
real rmag = std::sqrt(r.squared_norm2());
// Calculate expected force direction (should be along -r)
Vec3<real> normalized_r = r.scale(1.0 / rmag);
real force_dot_r = result.force.x * normalized_r.x +
result.force.y * normalized_r.y +
result.force.z * normalized_r.z;
// In this case, we're at r = sqrt(3) * sigma which is in attractive region
EXPECT_LT(force_dot_r, 0.0); // Force should be attractive
// Force should be symmetric in all dimensions for this vector
EXPECT_NEAR(result.force.x, result.force.y, 1e-10);
EXPECT_NEAR(result.force.y, result.force.z, 1e-10);
}
TEST_F(LennardJonesTest, ParameterVariation) {
// Test with different parameter values
real new_sigma = 2.0;
real new_epsilon = 0.5;
real new_rCutoff = 5.0;
LennardJones lj2(new_sigma, new_epsilon, new_rCutoff);
Vec3<real> r(2.0, 0.0, 0.0);
auto result1 = lj->calc_force_and_energy(r);
auto result2 = lj2.calc_force_and_energy(r);
// Results should be different with different parameters
EXPECT_NE(result1.energy, result2.energy);
EXPECT_NE(result1.force.x, result2.force.x);
}
TEST_F(LennardJonesTest, ExactValueCheck) {
// Test with pre-calculated values for a specific case
LennardJones lj_exact(1.0, 1.0, 3.0);
Vec3<real> r(1.5, 0.0, 0.0);
auto result = lj_exact.calc_force_and_energy(r);
// Pre-calculated values (you may need to adjust these based on your specific
// implementation)
real expected_energy =
4.0 * (std::pow(1.0 / 1.5, 12) - std::pow(1.0 / 1.5, 6));
real expected_force =
24.0 * (std::pow(1.0 / 1.5, 6) - 2.0 * std::pow(1.0 / 1.5, 12)) / 1.5;
EXPECT_NEAR(expected_energy, result.energy, 1e-10);
EXPECT_NEAR(-expected_force, result.force.x,
1e-10); // Negative because force is attractive
EXPECT_NEAR(0.0, result.force.y, 1e-10);
EXPECT_NEAR(0.0, result.force.z, 1e-10);
}
TEST_F(LennardJonesTest, NearCutoff) {
// Test behavior just inside and just outside the cutoff
real inside_cutoff = rCutoff - 0.01;
real outside_cutoff = rCutoff + 0.01;
Vec3<real> r_inside(inside_cutoff, 0.0, 0.0);
Vec3<real> r_outside(outside_cutoff, 0.0, 0.0);
auto result_inside = lj->calc_force_and_energy(r_inside);
auto result_outside = lj->calc_force_and_energy(r_outside);
// Inside should have non-zero values
EXPECT_NE(0.0, result_inside.energy);
EXPECT_NE(0.0, result_inside.force.x);
// Outside should be zero
EXPECT_EQ(0.0, result_outside.energy);
expectVec3Near(Vec3<real>(0.0, 0.0, 0.0), result_outside.force, 1e-10);
}