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
add_library(${CMAKE_PROJECT_NAME}_lib
${HEADER_FILES}
${SOURCE_FILES}
${HEADER_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();