Add basic LJ potential*

- Add PairPotential Abstract class
- Add Lennard-Jones potential that should work with both CUDA and C++
  code
- Add tests on HOST side for LJ potential
This commit is contained in:
Alex Selimov 2025-04-17 16:07:26 -04:00
parent f15eb0cf51
commit 5155ec21aa
11 changed files with 114 additions and 154 deletions

View file

@ -4,14 +4,13 @@ set(HEADER_FILES
particle.hpp
simulation.hpp
box.hpp
pair_potentials.hpp
)
set(SOURCE_FILES
pair_potentials.cpp
)
# The library contains header and source files.
add_library(${NAME}_lib
add_library(${NAME}_lib INTERFACE
${HEADER_FILES}
${SOURCE_FILES}
)

View file

@ -1,33 +0,0 @@
#include "pair_potentials.hpp"
#include <cmath>
PairPotential::~PairPotential() {};
/**
* 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_r6 = sigma_r * sigma_r * sigma_r * sigma_r * sigma_r * 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 *
(12.0 * sigma_r12 * inv_rmag - 6.0 * sigma_r6 * inv_rmag);
Vec3<real> force = r.scale(force_mag * inv_rmag);
return {energy, force};
} else {
return ForceAndEnergy::zero();
}
};

View file

@ -1,49 +0,0 @@
#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;
PairPotential(real rcutoff) : m_rcutoffsq(rcutoff * rcutoff) {};
virtual ~PairPotential() = 0;
/**
* 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;
LennardJones(real sigma, real epsilon, real rcutoff)
: PairPotential(rcutoff), m_epsilon(epsilon), m_sigma(sigma) {};
ForceAndEnergy calc_force_and_energy(Vec3<real> r);
~LennardJones() {};
};
#endif