Change default precision to float and use float4 for force and potential calculations

This commit is contained in:
Alex Selimov 2025-09-12 21:44:41 -04:00
parent dd83fc6330
commit 130b613a7c
Signed by: aselimov
GPG key ID: 3DDB9C3E023F1F31
9 changed files with 151 additions and 362 deletions

View file

@ -5,6 +5,7 @@
#include "vec3.h"
#include <cmath>
#include <cstdio>
#include <cuda_runtime.h>
#include <variant>
#ifdef __CUDACC__
@ -13,18 +14,6 @@
#define CUDA_CALLABLE
#endif
/**
* Result struct for the Pair Potential
*/
struct ForceAndEnergy {
real energy;
Vec3<real> force;
CUDA_CALLABLE inline static ForceAndEnergy zero() {
return {0.0, {0.0, 0.0, 0.0}};
};
};
/**
* Calculate the Lennard-Jones energy and force for the current particle
* pair described by displacement vector r
@ -40,7 +29,7 @@ struct LennardJones {
m_rcutoffsq = rcutoff * rcutoff;
};
CUDA_CALLABLE ForceAndEnergy calc_force_and_energy(Vec3<real> r) {
CUDA_CALLABLE float4 calc_force_and_energy(Vec3<real> r) {
real rmagsq = r.squared_norm2();
if (rmagsq < m_rcutoffsq && rmagsq > 0.0) {
real inv_rmag = 1 / sqrt(rmagsq);
@ -60,10 +49,10 @@ struct LennardJones {
(12.0 * sigma_r12 * inv_rmag - 6.0 * sigma_r6 * inv_rmag);
Vec3<real> force = r.scale(force_mag * inv_rmag);
return {energy, force};
return make_float4(force.x, force.y, force.z, energy);
} else {
return ForceAndEnergy::zero();
return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
}
};
};
@ -85,7 +74,7 @@ struct Morse {
m_rcutoffsq = rcutoff * rcutoff;
};
CUDA_CALLABLE ForceAndEnergy calc_force_and_energy(Vec3<real> r) {
CUDA_CALLABLE float4 calc_force_and_energy(Vec3<real> r) {
real rmagsq = r.squared_norm2();
if (rmagsq < m_rcutoffsq && rmagsq > 0.0) {
real rmag = sqrt(rmagsq);
@ -104,10 +93,10 @@ struct Morse {
// Direction: normalized vector
Vec3<real> force = r.scale(force_mag / rmag);
return {energy, force};
return make_float4(force.x, force.y, force.z, energy);
} else {
return ForceAndEnergy::zero();
return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
}
};
};