Commit a711ce2a authored by Peter Eastman's avatar Peter Eastman
Browse files

Refactored ForceImpl, lots of KernelImpl subclasses, and other related classes...

Refactored ForceImpl, lots of KernelImpl subclasses, and other related classes to avoid redundant calculations when requesting a State with both forces and energies
parent 767ea1bd
......@@ -65,18 +65,14 @@ public:
*/
virtual void initialize(const System& system, const GBSAOBCSoftcoreForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the GBSAOBCSoftcoreForce
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -104,18 +100,14 @@ public:
*/
virtual void initialize(const System& system, const NonbondedSoftcoreForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the NonbondedSoftcoreForce
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -137,18 +129,14 @@ public:
*/
virtual void initialize(const System& system, const GBVISoftcoreForce& force, const std::vector<double>& scaledRadii) = 0;
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the GBVISoftcoreForce
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
} // namespace OpenMM
......
......@@ -53,8 +53,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters() {
return std::map<std::string, double>(); // This force field doesn't define any parameters.
}
......
......@@ -62,8 +62,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters() {
return std::map<std::string, double>(); // This force field doesn't define any parameters.
}
......
......@@ -56,8 +56,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters() {
return std::map<std::string, double>(); // This force field doesn't define any parameters.
}
......
......@@ -48,12 +48,8 @@ void GBSAOBCSoftcoreForceImpl::initialize(ContextImpl& context) {
dynamic_cast<CalcGBSAOBCSoftcoreForceKernel&>(kernel.getImpl()).initialize(context.getSystem(), owner);
}
void GBSAOBCSoftcoreForceImpl::calcForces(ContextImpl& context ) {
dynamic_cast<CalcGBSAOBCSoftcoreForceKernel&>(kernel.getImpl()).executeForces(context);
}
double GBSAOBCSoftcoreForceImpl::calcEnergy(ContextImpl& context) {
return dynamic_cast<CalcGBSAOBCSoftcoreForceKernel&>(kernel.getImpl()).executeEnergy(context);
double GBSAOBCSoftcoreForceImpl::calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy) {
return dynamic_cast<CalcGBSAOBCSoftcoreForceKernel&>(kernel.getImpl()).execute(context, includeForces, includeEnergy);
}
std::vector<std::string> GBSAOBCSoftcoreForceImpl::getKernelNames() {
......
......@@ -261,12 +261,8 @@ void GBVISoftcoreForceImpl::findScaledRadii( int numberOfParticles, const std::v
}
void GBVISoftcoreForceImpl::calcForces(ContextImpl& context) {
dynamic_cast<CalcGBVISoftcoreForceKernel&>(kernel.getImpl()).executeForces(context);
}
double GBVISoftcoreForceImpl::calcEnergy(ContextImpl& context) {
return dynamic_cast<CalcGBVISoftcoreForceKernel&>(kernel.getImpl()).executeEnergy(context);
double GBVISoftcoreForceImpl::calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy) {
return dynamic_cast<CalcGBVISoftcoreForceKernel&>(kernel.getImpl()).execute(context, includeForces, includeEnergy);
}
std::vector<std::string> GBVISoftcoreForceImpl::getKernelNames() {
......
......@@ -87,12 +87,8 @@ void NonbondedSoftcoreForceImpl::initialize(ContextImpl& context) {
dynamic_cast<CalcNonbondedSoftcoreForceKernel&>(kernel.getImpl()).initialize(context.getSystem(), owner);
}
void NonbondedSoftcoreForceImpl::calcForces(ContextImpl& context ) {
dynamic_cast<CalcNonbondedSoftcoreForceKernel&>(kernel.getImpl()).executeForces(context);
}
double NonbondedSoftcoreForceImpl::calcEnergy(ContextImpl& context) {
return dynamic_cast<CalcNonbondedSoftcoreForceKernel&>(kernel.getImpl()).executeEnergy(context);
double NonbondedSoftcoreForceImpl::calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy) {
return dynamic_cast<CalcNonbondedSoftcoreForceKernel&>(kernel.getImpl()).execute(context, includeForces, includeEnergy);
}
std::vector<std::string> NonbondedSoftcoreForceImpl::getKernelNames() {
......
......@@ -377,7 +377,7 @@ void CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::initialize(const System& sy
}
}
void CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::executeForces(ContextImpl& context) {
double CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
// ---------------------------------------------------------------------------------------
......@@ -428,10 +428,6 @@ void CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::executeForces(ContextImpl&
//kPrintForces( gpu, "Post kCalculateLocalSoftcoreForces ", call );
//kPrintForces(gpu, "Post kCalculateLocalSoftcoreForces", call );
//kReduceForces(gpu);
}
double CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::executeEnergy(ContextImpl& context) {
executeForces(context);
return 0.0;
}
......@@ -519,7 +515,7 @@ void CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel::initialize(const System& syst
radius, scale, charge, nonPolarScalingFactors );
}
void CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel::executeForces(ContextImpl& context) {
double CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
// ---------------------------------------------------------------------------------------
......@@ -576,11 +572,7 @@ void CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel::executeForces(ContextImpl& co
// second loop of Obc GBSA forces
kCalculateObcGbsaSoftcoreForces2(gpu);
}
double CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel::executeEnergy(ContextImpl& context) {
executeForces( context );
return 0.0;
return 0.0;
}
CudaFreeEnergyCalcGBVISoftcoreForceKernel::~CudaFreeEnergyCalcGBVISoftcoreForceKernel() {
......@@ -661,7 +653,7 @@ void CudaFreeEnergyCalcGBVISoftcoreForceKernel::initialize(const System& system,
}
void CudaFreeEnergyCalcGBVISoftcoreForceKernel::executeForces(ContextImpl& context) {
double CudaFreeEnergyCalcGBVISoftcoreForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
// ---------------------------------------------------------------------------------------
......@@ -747,10 +739,6 @@ void CudaFreeEnergyCalcGBVISoftcoreForceKernel::executeForces(ContextImpl& conte
kCalculateGBVISoftcoreForces2(gpu);
//kPrintForces( gpu, "Post GBVISoftcoreForces2", call );
}
double CudaFreeEnergyCalcGBVISoftcoreForceKernel::executeEnergy(ContextImpl& context) {
executeForces( context );
return 0.0;
}
......
......@@ -73,18 +73,14 @@ public:
*/
void initialize(const System& system, const NonbondedSoftcoreForce& force);
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
void executeForces(ContextImpl& context);
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the NonbondedForce
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
double executeEnergy(ContextImpl& context);
double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
/**
* Get flag signalling whether GBSA/OBC force is included
*
......@@ -177,18 +173,14 @@ public:
*/
void initialize(const System& system, const GBSAOBCSoftcoreForce& force);
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
void executeForces(ContextImpl& context);
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the GBSAOBCForce
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
double executeEnergy(ContextImpl& context);
double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
private:
CudaPlatform::PlatformData& data;
FILE* log;
......@@ -223,18 +215,14 @@ public:
*/
void initialize(const System& system, const GBVISoftcoreForce& force, const std::vector<double> & scaledRadii);
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
void executeForces(ContextImpl& context);
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the GBVIForce
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
double executeEnergy(ContextImpl& context);
double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
/**
* Apply quintic scaling for Born radii
......
......@@ -226,47 +226,11 @@ void ReferenceFreeEnergyCalcNonbondedSoftcoreForceKernel::initialize(const Syste
rfDielectric = (RealOpenMM)force.getReactionFieldDielectric();
}
void ReferenceFreeEnergyCalcNonbondedSoftcoreForceKernel::executeForces(ContextImpl& context) {
double ReferenceFreeEnergyCalcNonbondedSoftcoreForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
RealOpenMM** posData = extractPositions(context);
RealOpenMM** forceData = extractForces(context);
ReferenceFreeEnergyLJCoulombSoftcoreIxn clj;
//clj.setSoftCoreLJLambda( softCoreLJLambda );
bool periodic = (nonbondedMethod == CutoffPeriodic);
bool ewald = (nonbondedMethod == Ewald);
bool pme = (nonbondedMethod == PME);
if (nonbondedMethod != NoCutoff) {
computeNeighborListVoxelHash(*neighborList, numParticles, posData, exclusions, (periodic || ewald || pme) ? periodicBoxSize : NULL, nonbondedCutoff, 0.0);
clj.setUseCutoff(nonbondedCutoff, *neighborList, rfDielectric);
}
if (periodic||ewald||pme)
clj.setPeriodic(periodicBoxSize);
if (ewald)
clj.setUseEwald(ewaldAlpha, kmax[0], kmax[1], kmax[2]);
if (pme)
clj.setUsePME(ewaldAlpha);
clj.calculatePairIxn(numParticles, posData, particleParamArray, exclusionArray, 0, forceData, 0, 0);
ReferenceBondForce refBondForce;
ReferenceFreeEnergyLJCoulomb14Softcore nonbonded14;
if (nonbondedMethod == CutoffNonPeriodic || nonbondedMethod == CutoffPeriodic)
nonbonded14.setUseCutoff(nonbondedCutoff, rfDielectric);
refBondForce.calculateForce(num14, bonded14IndexArray, posData, bonded14ParamArray, forceData, 0, nonbonded14);
}
double ReferenceFreeEnergyCalcNonbondedSoftcoreForceKernel::executeEnergy(ContextImpl& context) {
RealOpenMM** posData = extractPositions(context);
RealOpenMM** forceData = allocateRealArray(numParticles, 3);
RealOpenMM energy = 0;
ReferenceFreeEnergyLJCoulombSoftcoreIxn clj;
// clj.setSoftCoreLJLambda( softCoreLJLambda );
......@@ -290,7 +254,6 @@ double ReferenceFreeEnergyCalcNonbondedSoftcoreForceKernel::executeEnergy(Contex
nonbonded14.setUseCutoff(nonbondedCutoff, rfDielectric);
refBondForce.calculateForce(num14, bonded14IndexArray, posData, bonded14ParamArray, forceData, &energy, nonbonded14);
disposeRealArray(forceData, numParticles);
return energy;
}
......@@ -361,13 +324,7 @@ void ReferenceFreeEnergyCalcGBSAOBCSoftcoreForceKernel::initialize(const System&
obc->setIncludeAceApproximation(true);
}
void ReferenceFreeEnergyCalcGBSAOBCSoftcoreForceKernel::executeForces(ContextImpl& context) {
RealOpenMM** posData = extractPositions(context);
RealOpenMM** forceData = extractForces(context);
obc->computeImplicitSolventForces(posData, &charges[0], forceData, 1);
}
double ReferenceFreeEnergyCalcGBSAOBCSoftcoreForceKernel::executeEnergy(ContextImpl& context) {
double ReferenceFreeEnergyCalcGBSAOBCSoftcoreForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
RealOpenMM** posData = extractPositions(context);
RealOpenMM** forceData = extractForces(context);
obc->computeImplicitSolventForces(posData, &charges[0], forceData, 1);
......@@ -433,23 +390,18 @@ void ReferenceFreeEnergyCalcGBVISoftcoreForceKernel::initialize(const System& sy
gbviSoftcore = new CpuGBVISoftcore(gBVIParameters);
}
void ReferenceFreeEnergyCalcGBVISoftcoreForceKernel::executeForces(ContextImpl& context) {
RealOpenMM** posData = extractPositions(context);
RealOpenMM** forceData = extractForces(context);
RealOpenMM* bornRadii = new RealOpenMM[context.getSystem().getNumParticles()];
gbviSoftcore->computeBornRadii(posData, bornRadii, NULL );
gbviSoftcore->computeBornForces(bornRadii, posData, &charges[0], forceData);
delete[] bornRadii;
}
double ReferenceFreeEnergyCalcGBVISoftcoreForceKernel::executeEnergy(ContextImpl& context) {
double ReferenceFreeEnergyCalcGBVISoftcoreForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
RealOpenMM** posData = extractPositions(context);
RealOpenMM* bornRadii = new RealOpenMM[context.getSystem().getNumParticles()];
gbviSoftcore->computeBornRadii(posData, bornRadii, NULL );
RealOpenMM energy = gbviSoftcore->computeBornEnergy(bornRadii ,posData, &charges[0]);
gbviSoftcore->computeBornRadii(posData, bornRadii, NULL );
if (includeForces) {
RealOpenMM** forceData = extractForces(context);
gbviSoftcore->computeBornForces(bornRadii, posData, &charges[0], forceData);
}
RealOpenMM energy = 0.0;
if (includeEnergy)
energy = gbviSoftcore->computeBornEnergy(bornRadii ,posData, &charges[0]);
delete[] bornRadii;
return static_cast<double>(energy);
}
......@@ -57,18 +57,14 @@ public:
*/
void initialize(const System& system, const NonbondedSoftcoreForce& force);
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
void executeForces(ContextImpl& context);
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the NonbondedSoftcoreForce
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
double executeEnergy(ContextImpl& context);
double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
private:
int numParticles, num14;
int **exclusionArray, **bonded14IndexArray;
......@@ -96,18 +92,14 @@ public:
*/
void initialize(const System& system, const GBSAOBCSoftcoreForce& force);
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
void executeForces(ContextImpl& context);
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the GBSAOBCSoftcoreForce
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
double executeEnergy(ContextImpl& context);
double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
private:
CpuObcSoftcore* obc;
std::vector<RealOpenMM> charges;
......@@ -130,18 +122,14 @@ public:
*/
void initialize(const System& system, const GBVISoftcoreForce& force, const std::vector<double> & scaledRadii);
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
void executeForces(ContextImpl& context);
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the GBVISoftcoreForce
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
double executeEnergy(ContextImpl& context);
double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
private:
CpuGBVISoftcore* gbviSoftcore;
std::vector<RealOpenMM> charges;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment