Update CMakeFiles, add initial pair potential implementation and tests
This commit is contained in:
parent
6162b27a89
commit
f15eb0cf51
9 changed files with 67 additions and 55 deletions
|
@ -1,4 +1,4 @@
|
|||
#include "potentials.hpp"
|
||||
#include "pair_potentials.hpp"
|
||||
#include "precision.hpp"
|
||||
#include "gtest/gtest.h"
|
||||
#include <cmath>
|
||||
|
@ -9,22 +9,22 @@ protected:
|
|||
// Default parameters
|
||||
sigma = 1.0;
|
||||
epsilon = 1.0;
|
||||
rCutoff = 2.5;
|
||||
r_cutoff = 2.5;
|
||||
|
||||
// Create default LennardJones object
|
||||
lj = new LennardJones(sigma, epsilon, rCutoff);
|
||||
lj = new LennardJones(sigma, epsilon, r_cutoff);
|
||||
}
|
||||
|
||||
void TearDown() override { delete lj; }
|
||||
|
||||
real sigma;
|
||||
real epsilon;
|
||||
real rCutoff;
|
||||
real r_cutoff;
|
||||
LennardJones *lj;
|
||||
|
||||
// Helper function to compare Vec3 values with tolerance
|
||||
void expectVec3Near(const Vec3<real> &expected, const Vec3<real> &actual,
|
||||
real tolerance) {
|
||||
void expect_vec3_near(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);
|
||||
|
@ -33,36 +33,36 @@ protected:
|
|||
|
||||
TEST_F(LennardJonesTest, ZeroDistance) {
|
||||
// At zero distance, the calculation should return zero force and energy
|
||||
Vec3<real> r(0.0, 0.0, 0.0);
|
||||
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);
|
||||
expect_vec3_near({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)
|
||||
Vec3<real> r = {3.0, 0.0, 0.0}; // 3.0 > r_cutoff (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);
|
||||
expect_vec3_near({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);
|
||||
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);
|
||||
expect_vec3_near({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);
|
||||
Vec3<real> r = {sigma, 0.0, 0.0};
|
||||
auto result = lj->calc_force_and_energy(r);
|
||||
|
||||
EXPECT_NEAR(0.0, result.energy, 1e-10);
|
||||
|
@ -74,7 +74,7 @@ TEST_F(LennardJonesTest, AtEquilibrium) {
|
|||
|
||||
TEST_F(LennardJonesTest, RepulsiveRegion) {
|
||||
// Test in the repulsive region (r < sigma)
|
||||
Vec3<real> r(0.8 * sigma, 0.0, 0.0);
|
||||
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
|
||||
|
@ -84,7 +84,7 @@ TEST_F(LennardJonesTest, RepulsiveRegion) {
|
|||
|
||||
TEST_F(LennardJonesTest, AttractiveRegion) {
|
||||
// Test in the attractive region (sigma < r < r_min)
|
||||
Vec3<real> r(1.5 * sigma, 0.0, 0.0);
|
||||
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
|
||||
|
@ -95,15 +95,15 @@ TEST_F(LennardJonesTest, AttractiveRegion) {
|
|||
|
||||
TEST_F(LennardJonesTest, ArbitraryDirection) {
|
||||
// Test with a vector in an arbitrary direction
|
||||
Vec3<real> r(1.0, 1.0, 1.0);
|
||||
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());
|
||||
real r_mag = std::sqrt(r.squared_norm2());
|
||||
|
||||
// Calculate expected force direction (should be along -r)
|
||||
Vec3<real> normalized_r = r.scale(1.0 / rmag);
|
||||
Vec3<real> normalized_r = r.scale(1.0 / r_mag);
|
||||
real force_dot_r = result.force.x * normalized_r.x +
|
||||
result.force.y * normalized_r.y +
|
||||
result.force.z * normalized_r.z;
|
||||
|
@ -120,11 +120,11 @@ TEST_F(LennardJonesTest, ParameterVariation) {
|
|||
// Test with different parameter values
|
||||
real new_sigma = 2.0;
|
||||
real new_epsilon = 0.5;
|
||||
real new_rCutoff = 5.0;
|
||||
real new_r_cutoff = 5.0;
|
||||
|
||||
LennardJones lj2(new_sigma, new_epsilon, new_rCutoff);
|
||||
LennardJones lj2(new_sigma, new_epsilon, new_r_cutoff);
|
||||
|
||||
Vec3<real> r(2.0, 0.0, 0.0);
|
||||
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);
|
||||
|
||||
|
@ -136,7 +136,7 @@ TEST_F(LennardJonesTest, ParameterVariation) {
|
|||
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);
|
||||
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
|
||||
|
@ -155,11 +155,11 @@ TEST_F(LennardJonesTest, ExactValueCheck) {
|
|||
|
||||
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;
|
||||
real inside_cutoff = r_cutoff - 0.01;
|
||||
real outside_cutoff = r_cutoff + 0.01;
|
||||
|
||||
Vec3<real> r_inside(inside_cutoff, 0.0, 0.0);
|
||||
Vec3<real> r_outside(outside_cutoff, 0.0, 0.0);
|
||||
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);
|
||||
|
@ -170,5 +170,5 @@ TEST_F(LennardJonesTest, NearCutoff) {
|
|||
|
||||
// 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);
|
||||
expect_vec3_near({0.0, 0.0, 0.0}, result_outside.force, 1e-10);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue