Add force calculation kernel and fix incorrect ctest configuration for Cuda tests

This commit is contained in:
Alex Selimov 2025-08-27 22:07:47 -04:00
parent cad74747bf
commit dc74e4e5c0
Signed by: aselimov
GPG key ID: 3DDB9C3E023F1F31
6 changed files with 348 additions and 6 deletions

View file

@ -2,12 +2,14 @@ project(${NAME}_cuda_lib CUDA CXX)
set(HEADER_FILES
pair_potentials.cuh
forces.cuh
)
set(SOURCE_FILES
forces.cu
)
# The library contains header and source files.
add_library(${NAME}_cuda_lib INTERFACE
add_library(${NAME}_cuda_lib STATIC
${SOURCE_FILES}
${HEADER_FILES}
)

36
kernels/forces.cu Normal file
View file

@ -0,0 +1,36 @@
#include "forces.cuh"
__global__ void CAC::calc_forces_and_energies(real *xs, real *forces,
real *energies, int n_particles,
real *box_len,
PairPotential &potential) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n_particles) {
real xi = xs[3 * i];
real yi = xs[3 * i + 1];
real zi = xs[3 * i + 2];
for (int j = 0; j < n_particles; j++) {
if (i != j) {
real xj = xs[3 * j];
real yj = xs[3 * j + 1];
real zj = xs[3 * j + 2];
real dx = xi - xj;
real dy = yi - yj;
real dz = zi - zj;
// Apply periodic boundary conditions
dx -= box_len[0] * round(dx / box_len[0]);
dy -= box_len[1] * round(dy / box_len[1]);
dz -= box_len[2] * round(dz / box_len[2]);
ForceAndEnergy sol = potential.calc_force_and_energy({dx, dy, dz});
forces[3 * i] += sol.force.x;
forces[3 * i + 1] += sol.force.y;
forces[3 * i + 2] += sol.force.z;
energies[i] = sol.energy;
}
}
}
}

19
kernels/forces.cuh Normal file
View file

@ -0,0 +1,19 @@
#ifndef FORCES_CUH
#define FORCES_CUH
#include "pair_potentials.cuh"
#include "precision.hpp"
namespace CAC {
/**
* Calculate forces and energies using CUDA for acceleration
* This code currently only accepts a single PairPotential object and does an
* n^2 force calculation. Future improvements will:
* - Allow for neighbor listing
* - Allow for overlaid force calculations
*/
__global__ void calc_forces_and_energies(real *xs, real *forces, real *energies,
int n_particles, real *box_bd,
PairPotential &potential);
} // namespace CAC
#endif

View file

@ -1,5 +1,5 @@
#ifndef POTENTIALS_H
#define POTENTIALS_H
#ifndef POTENTIALS_CUH
#define POTENTIALS_CUH
#include "precision.hpp"
#include "vec3.h"
@ -84,8 +84,8 @@ struct LennardJones : PairPotential {
}
};
CUDA_CALLABLE ~LennardJones(){};
CUDA_CALLABLE inline ~LennardJones(){};
};
PairPotential::~PairPotential() {};
inline PairPotential::~PairPotential() {};
#endif