Compare commits

..

3 Commits

15 changed files with 376 additions and 34 deletions

5
.gitignore vendored
View File

@ -1,5 +1,7 @@
# Builds # Builds
build/ build/
Debug/
Testing/
# Google Tests # Google Tests
tests/lib/ tests/lib/
@ -7,3 +9,6 @@ tests/lib/
# Jet Brains # Jet Brains
.idea/ .idea/
cmake-build-debug/ cmake-build-debug/
# Cache dir
.cache

View File

@ -1,32 +1,47 @@
cmake_minimum_required(VERSION 3.9) cmake_minimum_required(VERSION 3.9)
project(MyProject LANGUAGES CUDA CXX) set(NAME "cudaCAC")
project(${NAME} LANGUAGES CUDA CXX)
enable_testing()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Default settings
add_compile_options(-Wall -Wextra -Wpedantic) add_compile_options(-Wall -Wextra -Wpedantic)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CUDA_ARCHITECTURES 61) set(CMAKE_CUDA_ARCHITECTURES 61)
set(CUDA_SEPARABLE_COMPILATION ON) set(CUDA_SEPARABLE_COMPILATION ON)
# Add Vec3 as a dependency
include(FetchContent)
FetchContent_Declare(Vec3
GIT_REPOSITORY https://www.alexselimov.com/git/aselimov/Vec3.git
)
FetchContent_GetProperties(Vec3)
if(NOT Vec3_POPULATED)
FetchContent_MakeAvailable(Vec3)
include_directories(${Vec3_SOURCE_DIR})
endif()
include_directories(src) include_directories(src)
include_directories(kernels) include_directories(kernels)
include_directories(/usr/local/cuda-12.8/include) include_directories(/usr/local/cuda-12.8/include)
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(kernels) add_subdirectory(kernels)
add_subdirectory(tests) add_subdirectory(tests)
add_executable(${CMAKE_PROJECT_NAME}_run main.cpp) add_executable(${NAME} main.cpp)
install(DIRECTORY src/ DESTINATION src/)
target_link_libraries( target_link_libraries(
${CMAKE_PROJECT_NAME}_run ${NAME}
PRIVATE PRIVATE
${CMAKE_PROJECT_NAME}_lib ${NAME}_lib
${CMAKE_PROJECT_NAME}_cuda_lib ${NAME}_cuda_lib
${CUDA_LIBRARIES} ${CUDA_LIBRARIES}
) )

View File

@ -1,4 +1,4 @@
project(${CMAKE_PROJECT_NAME}_cuda_lib CUDA CXX) project(${NAME}_cuda_lib CUDA CXX)
set(HEADER_FILES set(HEADER_FILES
hello_world.h hello_world.h
@ -8,11 +8,9 @@ set(SOURCE_FILES
) )
# The library contains header and source files. # The library contains header and source files.
add_library(${CMAKE_PROJECT_NAME}_cuda_lib STATIC add_library(${NAME}_cuda_lib STATIC
${SOURCE_FILES} ${SOURCE_FILES}
${HEADER_FILES} ${HEADER_FILES}
) )
if(CMAKE_COMPILER_IS_GNUCXX) target_compile_options(${CMAKE_PROJECT_NAME}_cuda_lib PRIVATE -Wno-gnu-line-marker -Wno-pedantic)
target_compile_options(${CMAKE_PROJECT_NAME}_cuda_lib PRIVATE -Wno-gnu-line-marker)
endif()

View File

@ -1,10 +1,10 @@
#include "hello_world.h" #include "particle.hpp"
#include "vec3.h"
#include <iostream> #include <iostream>
int main() { int main() {
std::cout << "Starting CUDA example..." << std::endl; // Using endl to flush Particle<float> test = {
check_cuda(); {0.0, 0.0, 0.0}, {0.0, 0.0, 0.0}, {0.0, 0.0, 0.0}, 10};
launch_hello_cuda(); std::cout << test.pos.x << " " << test.pos.y << " " << test.pos.z;
std::cout << "Ending CUDA example" << std::endl; // Using endl to flush
return 0; return 0;
} }

View File

@ -1,16 +1,17 @@
project(${CMAKE_PROJECT_NAME}_lib CUDA CXX) project(${NAME}_lib CUDA CXX)
set(HEADER_FILES set(HEADER_FILES
./test.h particle.hpp
simulation.hpp
box.hpp
pair_potentials.hpp
) )
set(SOURCE_FILES set(SOURCE_FILES
./test.cpp pair_potentials.cpp
) )
# The library contains header and source files. # The library contains header and source files.
add_library(${CMAKE_PROJECT_NAME}_lib add_library(${NAME}_lib
${HEADER_FILES}
${SOURCE_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 "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();
}
};

49
src/pair_potentials.hpp Normal file
View File

@ -0,0 +1,49 @@
#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

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}) include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
add_executable(Unit_Tests_run add_executable(${NAME}_tests
test_example.cpp test_potential.cpp
) )
target_link_libraries(Unit_Tests_run gtest gtest_main) target_link_libraries(${NAME}_tests gtest gtest_main)
target_link_libraries(Unit_Tests_run ${CMAKE_PROJECT_NAME}_lib) target_link_libraries(${NAME}_tests ${CMAKE_PROJECT_NAME}_lib)
add_test(NAME ${NAME}Tests COMMAND ${CMAKE_BINARY_DIR}/tests/unit_tests/${NAME}_tests)

View File

@ -0,0 +1,174 @@
#include "pair_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;
r_cutoff = 2.5;
// Create default LennardJones object
lj = new LennardJones(sigma, epsilon, r_cutoff);
}
void TearDown() override { delete lj; }
real sigma;
real epsilon;
real r_cutoff;
LennardJones *lj;
// Helper function to compare Vec3 values with 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);
}
};
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);
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 > r_cutoff (2.5)
auto result = lj->calc_force_and_energy(r);
EXPECT_EQ(0.0, result.energy);
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};
auto result = lj->calc_force_and_energy(r);
// At minimum, force should be close to zero
EXPECT_NEAR(-epsilon, result.energy, 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};
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 r_mag = std::sqrt(r.squared_norm2());
// Calculate expected force direction (should be along -r)
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;
// 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_r_cutoff = 5.0;
LennardJones lj2(new_sigma, new_epsilon, new_r_cutoff);
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 = 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};
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);
expect_vec3_near({0.0, 0.0, 0.0}, result_outside.force, 1e-10);
}