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

Continuing converting AMOEBA to the new CUDA platform:...

Continuing converting AMOEBA to the new CUDA platform: AmoebaOutOfPlaneBendForce, AmoebaStretchBendForce, AmoebaTorsionTorsionForce
parent f5980599
...@@ -88,15 +88,15 @@ KernelImpl* AmoebaCudaKernelFactory::createKernelImpl(std::string name, const Pl ...@@ -88,15 +88,15 @@ KernelImpl* AmoebaCudaKernelFactory::createKernelImpl(std::string name, const Pl
if (name == CalcAmoebaPiTorsionForceKernel::Name()) if (name == CalcAmoebaPiTorsionForceKernel::Name())
return new CudaCalcAmoebaPiTorsionForceKernel(name, platform, cu, context.getSystem()); return new CudaCalcAmoebaPiTorsionForceKernel(name, platform, cu, context.getSystem());
// if (name == CalcAmoebaStretchBendForceKernel::Name()) if (name == CalcAmoebaStretchBendForceKernel::Name())
// return new CudaCalcAmoebaStretchBendForceKernel(name, platform, cu, context.getSystem()); return new CudaCalcAmoebaStretchBendForceKernel(name, platform, cu, context.getSystem());
//
// if (name == CalcAmoebaOutOfPlaneBendForceKernel::Name()) if (name == CalcAmoebaOutOfPlaneBendForceKernel::Name())
// return new CudaCalcAmoebaOutOfPlaneBendForceKernel(name, platform, cu, context.getSystem()); return new CudaCalcAmoebaOutOfPlaneBendForceKernel(name, platform, cu, context.getSystem());
//
// if (name == CalcAmoebaTorsionTorsionForceKernel::Name()) if (name == CalcAmoebaTorsionTorsionForceKernel::Name())
// return new CudaCalcAmoebaTorsionTorsionForceKernel(name, platform, cu, context.getSystem()); return new CudaCalcAmoebaTorsionTorsionForceKernel(name, platform, cu, context.getSystem());
//
// if (name == CalcAmoebaMultipoleForceKernel::Name()) // if (name == CalcAmoebaMultipoleForceKernel::Name())
// return new CudaCalcAmoebaMultipoleForceKernel(name, platform, cu, context.getSystem()); // return new CudaCalcAmoebaMultipoleForceKernel(name, platform, cu, context.getSystem());
// //
......
...@@ -74,7 +74,7 @@ private: ...@@ -74,7 +74,7 @@ private:
}; };
CudaCalcAmoebaHarmonicBondForceKernel::CudaCalcAmoebaHarmonicBondForceKernel(std::string name, const Platform& platform, CudaContext& cu, System& system) : CudaCalcAmoebaHarmonicBondForceKernel::CudaCalcAmoebaHarmonicBondForceKernel(std::string name, const Platform& platform, CudaContext& cu, System& system) :
CalcAmoebaHarmonicBondForceKernel(name, platform), cu(cu), system(system) { CalcAmoebaHarmonicBondForceKernel(name, platform), cu(cu), system(system), params(NULL) {
} }
CudaCalcAmoebaHarmonicBondForceKernel::~CudaCalcAmoebaHarmonicBondForceKernel() { CudaCalcAmoebaHarmonicBondForceKernel::~CudaCalcAmoebaHarmonicBondForceKernel() {
...@@ -218,7 +218,7 @@ private: ...@@ -218,7 +218,7 @@ private:
}; };
CudaCalcAmoebaHarmonicAngleForceKernel::CudaCalcAmoebaHarmonicAngleForceKernel(std::string name, const Platform& platform, CudaContext& cu, System& system) : CudaCalcAmoebaHarmonicAngleForceKernel::CudaCalcAmoebaHarmonicAngleForceKernel(std::string name, const Platform& platform, CudaContext& cu, System& system) :
CalcAmoebaHarmonicAngleForceKernel(name, platform), cu(cu), system(system) { CalcAmoebaHarmonicAngleForceKernel(name, platform), cu(cu), system(system), params(NULL) {
} }
CudaCalcAmoebaHarmonicAngleForceKernel::~CudaCalcAmoebaHarmonicAngleForceKernel() { CudaCalcAmoebaHarmonicAngleForceKernel::~CudaCalcAmoebaHarmonicAngleForceKernel() {
...@@ -242,7 +242,6 @@ void CudaCalcAmoebaHarmonicAngleForceKernel::initialize(const System& system, co ...@@ -242,7 +242,6 @@ void CudaCalcAmoebaHarmonicAngleForceKernel::initialize(const System& system, co
double angle, k; double angle, k;
force.getAngleParameters(startIndex+i, atoms[i][0], atoms[i][1], atoms[i][2], angle, k); force.getAngleParameters(startIndex+i, atoms[i][0], atoms[i][1], atoms[i][2], angle, k);
paramVector[i] = make_float2((float) angle, (float) k); paramVector[i] = make_float2((float) angle, (float) k);
} }
params->upload(paramVector); params->upload(paramVector);
map<string, string> replacements; map<string, string> replacements;
...@@ -318,7 +317,6 @@ void CudaCalcAmoebaHarmonicInPlaneAngleForceKernel::initialize(const System& sys ...@@ -318,7 +317,6 @@ void CudaCalcAmoebaHarmonicInPlaneAngleForceKernel::initialize(const System& sys
double angle, k; double angle, k;
force.getAngleParameters(startIndex+i, atoms[i][0], atoms[i][1], atoms[i][2], atoms[i][3], angle, k); force.getAngleParameters(startIndex+i, atoms[i][0], atoms[i][1], atoms[i][2], atoms[i][3], angle, k);
paramVector[i] = make_float2((float) angle, (float) k); paramVector[i] = make_float2((float) angle, (float) k);
} }
params->upload(paramVector); params->upload(paramVector);
map<string, string> replacements; map<string, string> replacements;
...@@ -466,7 +464,7 @@ private: ...@@ -466,7 +464,7 @@ private:
}; };
CudaCalcAmoebaPiTorsionForceKernel::CudaCalcAmoebaPiTorsionForceKernel(std::string name, const Platform& platform, CudaContext& cu, System& system) : CudaCalcAmoebaPiTorsionForceKernel::CudaCalcAmoebaPiTorsionForceKernel(std::string name, const Platform& platform, CudaContext& cu, System& system) :
CalcAmoebaPiTorsionForceKernel(name, platform), cu(cu), system(system) { CalcAmoebaPiTorsionForceKernel(name, platform), cu(cu), system(system), params(NULL) {
} }
CudaCalcAmoebaPiTorsionForceKernel::~CudaCalcAmoebaPiTorsionForceKernel() { CudaCalcAmoebaPiTorsionForceKernel::~CudaCalcAmoebaPiTorsionForceKernel() {
...@@ -490,7 +488,6 @@ void CudaCalcAmoebaPiTorsionForceKernel::initialize(const System& system, const ...@@ -490,7 +488,6 @@ void CudaCalcAmoebaPiTorsionForceKernel::initialize(const System& system, const
double k; double k;
force.getPiTorsionParameters(startIndex+i, atoms[i][0], atoms[i][1], atoms[i][2], atoms[i][3], atoms[i][4], atoms[i][5], k); force.getPiTorsionParameters(startIndex+i, atoms[i][0], atoms[i][1], atoms[i][2], atoms[i][3], atoms[i][4], atoms[i][5], k);
paramVector[i] = (float) k; paramVector[i] = (float) k;
} }
params->upload(paramVector); params->upload(paramVector);
map<string, string> replacements; map<string, string> replacements;
...@@ -503,272 +500,252 @@ double CudaCalcAmoebaPiTorsionForceKernel::execute(ContextImpl& context, bool in ...@@ -503,272 +500,252 @@ double CudaCalcAmoebaPiTorsionForceKernel::execute(ContextImpl& context, bool in
return 0.0; return 0.0;
} }
///* -------------------------------------------------------------------------- * /* -------------------------------------------------------------------------- *
// * AmoebaStretchBend * * AmoebaStretchBend *
// * -------------------------------------------------------------------------- */ * -------------------------------------------------------------------------- */
//
//class CudaCalcAmoebaStretchBendForceKernel::ForceInfo : public CudaForceInfo { class CudaCalcAmoebaStretchBendForceKernel::ForceInfo : public CudaForceInfo {
//public: public:
// ForceInfo(const AmoebaStretchBendForce& force) : force(force) { ForceInfo(const AmoebaStretchBendForce& force) : force(force) {
// } }
// int getNumParticleGroups() { int getNumParticleGroups() {
// return force.getNumStretchBends(); return force.getNumStretchBends();
// } }
// void getParticlesInGroup(int index, std::vector<int>& particles) { void getParticlesInGroup(int index, std::vector<int>& particles) {
// int particle1, particle2, particle3; int particle1, particle2, particle3;
// double lengthAB, lengthCB, angle, k; double lengthAB, lengthCB, angle, k;
// force.getStretchBendParameters(index, particle1, particle2, particle3, lengthAB, lengthCB, angle, k); force.getStretchBendParameters(index, particle1, particle2, particle3, lengthAB, lengthCB, angle, k);
// particles.resize(3); particles.resize(3);
// particles[0] = particle1; particles[0] = particle1;
// particles[1] = particle2; particles[1] = particle2;
// particles[2] = particle3; particles[2] = particle3;
// } }
// bool areGroupsIdentical(int group1, int group2) { bool areGroupsIdentical(int group1, int group2) {
// int particle1, particle2, particle3; int particle1, particle2, particle3;
// double lengthAB1, lengthAB2, lengthCB1, lengthCB2, angle1, angle2, k1, k2; double lengthAB1, lengthAB2, lengthCB1, lengthCB2, angle1, angle2, k1, k2;
// force.getStretchBendParameters(group1, particle1, particle2, particle3, lengthAB1, lengthCB1, angle1, k1); force.getStretchBendParameters(group1, particle1, particle2, particle3, lengthAB1, lengthCB1, angle1, k1);
// force.getStretchBendParameters(group2, particle1, particle2, particle3, lengthAB2, lengthCB2, angle2, k2); force.getStretchBendParameters(group2, particle1, particle2, particle3, lengthAB2, lengthCB2, angle2, k2);
// return (lengthAB1 == lengthAB2 && lengthCB1 == lengthCB2 && angle1 == angle2 && k1 == k2); return (lengthAB1 == lengthAB2 && lengthCB1 == lengthCB2 && angle1 == angle2 && k1 == k2);
// } }
//private: private:
// const AmoebaStretchBendForce& force; const AmoebaStretchBendForce& force;
//}; };
//
//CudaCalcAmoebaStretchBendForceKernel::CudaCalcAmoebaStretchBendForceKernel(std::string name, const Platform& platform, CudaContext& cu, System& system) : CudaCalcAmoebaStretchBendForceKernel::CudaCalcAmoebaStretchBendForceKernel(std::string name, const Platform& platform, CudaContext& cu, System& system) :
// CalcAmoebaStretchBendForceKernel(name, platform), cu(cu), system(system) { CalcAmoebaStretchBendForceKernel(name, platform), cu(cu), system(system), params(NULL) {
// data.incrementKernelCount(); }
//}
// CudaCalcAmoebaStretchBendForceKernel::~CudaCalcAmoebaStretchBendForceKernel() {
//CudaCalcAmoebaStretchBendForceKernel::~CudaCalcAmoebaStretchBendForceKernel() { cu.setAsCurrent();
// data.decrementKernelCount(); if (params != NULL)
//} delete params;
// }
//void CudaCalcAmoebaStretchBendForceKernel::initialize(const System& system, const AmoebaStretchBendForce& force) {
// void CudaCalcAmoebaStretchBendForceKernel::initialize(const System& system, const AmoebaStretchBendForce& force) {
// data.setAmoebaLocalForcesKernel( this ); cu.setAsCurrent();
// numStretchBends = force.getNumStretchBends(); int numContexts = cu.getPlatformData().contexts.size();
// int startIndex = cu.getContextIndex()*force.getNumStretchBends()/numContexts;
// std::vector<int> particle1(numStretchBends); int endIndex = (cu.getContextIndex()+1)*force.getNumStretchBends()/numContexts;
// std::vector<int> particle2(numStretchBends); numStretchBends = endIndex-startIndex;
// std::vector<int> particle3(numStretchBends); if (numStretchBends == 0)
// std::vector<float> lengthABParameters(numStretchBends); return;
// std::vector<float> lengthCBParameters(numStretchBends); vector<vector<int> > atoms(numStretchBends, vector<int>(3));
// std::vector<float> angleParameters(numStretchBends); params = CudaArray::create<float4>(cu, numStretchBends, "stretchBendParams");
// std::vector<float> kParameters(numStretchBends); vector<float4> paramVector(numStretchBends);
// for (int i = 0; i < numStretchBends; i++) {
// for (int i = 0; i < numStretchBends; i++) { double lengthAB, lengthCB, angle, k;
// force.getStretchBendParameters(startIndex+i, atoms[i][0], atoms[i][1], atoms[i][2], lengthAB, lengthCB, angle, k);
// double lengthAB, lengthCB, angle, k; paramVector[i] = make_float4((float) lengthAB, (float) lengthCB, (float) angle, (float) k);
// }
// force.getStretchBendParameters(i, particle1[i], particle2[i], particle3[i], lengthAB, lengthCB, angle, k); params->upload(paramVector);
// lengthABParameters[i] = static_cast<float>(lengthAB); map<string, string> replacements;
// lengthCBParameters[i] = static_cast<float>(lengthCB); replacements["PARAMS"] = cu.getBondedUtilities().addArgument(params->getDevicePointer(), "float4");
// angleParameters[i] = static_cast<float>(angle); replacements["RAD_TO_DEG"] = cu.doubleToString(180/M_PI);
// kParameters[i] = static_cast<float>(k); cu.getBondedUtilities().addInteraction(atoms, cu.replaceStrings(CudaAmoebaKernelSources::amoebaStretchBendForce, replacements), force.getForceGroup());
// } cu.addForce(new ForceInfo(force));
// gpuSetAmoebaStretchBendParameters(data.getAmoebaGpu(), particle1, particle2, particle3, lengthABParameters, lengthCBParameters, angleParameters, kParameters); }
// data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force));
//} double CudaCalcAmoebaStretchBendForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
// }
//double CudaCalcAmoebaStretchBendForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
// if( data.getAmoebaLocalForcesKernel() == this ){ /* -------------------------------------------------------------------------- *
// computeAmoebaLocalForces( data ); * AmoebaOutOfPlaneBend *
// } * -------------------------------------------------------------------------- */
// return 0.0;
//} class CudaCalcAmoebaOutOfPlaneBendForceKernel::ForceInfo : public CudaForceInfo {
// public:
///* -------------------------------------------------------------------------- * ForceInfo(const AmoebaOutOfPlaneBendForce& force) : force(force) {
// * AmoebaOutOfPlaneBend * }
// * -------------------------------------------------------------------------- */ int getNumParticleGroups() {
// return force.getNumOutOfPlaneBends();
//class CudaCalcAmoebaOutOfPlaneBendForceKernel::ForceInfo : public CudaForceInfo { }
//public: void getParticlesInGroup(int index, std::vector<int>& particles) {
// ForceInfo(const AmoebaOutOfPlaneBendForce& force) : force(force) { int particle1, particle2, particle3, particle4;
// } double k;
// int getNumParticleGroups() { force.getOutOfPlaneBendParameters(index, particle1, particle2, particle3, particle4, k);
// return force.getNumOutOfPlaneBends(); particles.resize(4);
// } particles[0] = particle1;
// void getParticlesInGroup(int index, std::vector<int>& particles) { particles[1] = particle2;
// int particle1, particle2, particle3, particle4; particles[2] = particle3;
// double k; particles[3] = particle4;
// force.getOutOfPlaneBendParameters(index, particle1, particle2, particle3, particle4, k); }
// particles.resize(4); bool areGroupsIdentical(int group1, int group2) {
// particles[0] = particle1; int particle1, particle2, particle3, particle4;
// particles[1] = particle2; double k1, k2;
// particles[2] = particle3; force.getOutOfPlaneBendParameters(group1, particle1, particle2, particle3, particle4, k1);
// particles[3] = particle4; force.getOutOfPlaneBendParameters(group2, particle1, particle2, particle3, particle4, k2);
// } return (k1 == k2);
// bool areGroupsIdentical(int group1, int group2) { }
// int particle1, particle2, particle3, particle4; private:
// double k1, k2; const AmoebaOutOfPlaneBendForce& force;
// force.getOutOfPlaneBendParameters(group1, particle1, particle2, particle3, particle4, k1); };
// force.getOutOfPlaneBendParameters(group2, particle1, particle2, particle3, particle4, k2);
// return (k1 == k2); CudaCalcAmoebaOutOfPlaneBendForceKernel::CudaCalcAmoebaOutOfPlaneBendForceKernel(std::string name, const Platform& platform, CudaContext& cu, System& system) :
// } CalcAmoebaOutOfPlaneBendForceKernel(name, platform), cu(cu), system(system), params(NULL) {
//private: }
// const AmoebaOutOfPlaneBendForce& force;
//}; CudaCalcAmoebaOutOfPlaneBendForceKernel::~CudaCalcAmoebaOutOfPlaneBendForceKernel() {
// cu.setAsCurrent();
//CudaCalcAmoebaOutOfPlaneBendForceKernel::CudaCalcAmoebaOutOfPlaneBendForceKernel(std::string name, const Platform& platform, CudaContext& cu, System& system) : if (params != NULL)
// CalcAmoebaOutOfPlaneBendForceKernel(name, platform), cu(cu), system(system) { delete params;
// data.incrementKernelCount(); }
//}
// void CudaCalcAmoebaOutOfPlaneBendForceKernel::initialize(const System& system, const AmoebaOutOfPlaneBendForce& force) {
//CudaCalcAmoebaOutOfPlaneBendForceKernel::~CudaCalcAmoebaOutOfPlaneBendForceKernel() { cu.setAsCurrent();
// data.decrementKernelCount(); int numContexts = cu.getPlatformData().contexts.size();
//} int startIndex = cu.getContextIndex()*force.getNumOutOfPlaneBends()/numContexts;
// int endIndex = (cu.getContextIndex()+1)*force.getNumOutOfPlaneBends()/numContexts;
//void CudaCalcAmoebaOutOfPlaneBendForceKernel::initialize(const System& system, const AmoebaOutOfPlaneBendForce& force) { numOutOfPlaneBends = endIndex-startIndex;
// if (numOutOfPlaneBends == 0)
// data.setAmoebaLocalForcesKernel( this ); return;
// numOutOfPlaneBends = force.getNumOutOfPlaneBends(); vector<vector<int> > atoms(numOutOfPlaneBends, vector<int>(4));
// params = CudaArray::create<float>(cu, numOutOfPlaneBends, "outOfPlaneParams");
// std::vector<int> particle1(numOutOfPlaneBends); vector<float> paramVector(numOutOfPlaneBends);
// std::vector<int> particle2(numOutOfPlaneBends); for (int i = 0; i < numOutOfPlaneBends; i++) {
// std::vector<int> particle3(numOutOfPlaneBends); double k;
// std::vector<int> particle4(numOutOfPlaneBends); force.getOutOfPlaneBendParameters(startIndex+i, atoms[i][0], atoms[i][1], atoms[i][2], atoms[i][3], k);
// std::vector<float> kParameters(numOutOfPlaneBends); paramVector[i] = (float) k;
// }
// for (int i = 0; i < numOutOfPlaneBends; i++) { params->upload(paramVector);
// map<string, string> replacements;
// double k; replacements["PARAMS"] = cu.getBondedUtilities().addArgument(params->getDevicePointer(), "float");
// replacements["CUBIC_K"] = cu.doubleToString(force.getAmoebaGlobalOutOfPlaneBendCubic());
// force.getOutOfPlaneBendParameters(i, particle1[i], particle2[i], particle3[i], particle4[i], k); replacements["QUARTIC_K"] = cu.doubleToString(force.getAmoebaGlobalOutOfPlaneBendQuartic());
// kParameters[i] = static_cast<float>(k); replacements["PENTIC_K"] = cu.doubleToString(force.getAmoebaGlobalOutOfPlaneBendPentic());
// } replacements["SEXTIC_K"] = cu.doubleToString(force.getAmoebaGlobalOutOfPlaneBendSextic());
// gpuSetAmoebaOutOfPlaneBendParameters(data.getAmoebaGpu(), particle1, particle2, particle3, particle4, kParameters, replacements["RAD_TO_DEG"] = cu.doubleToString(180/M_PI);
// static_cast<float>( force.getAmoebaGlobalOutOfPlaneBendCubic()), cu.getBondedUtilities().addInteraction(atoms, cu.replaceStrings(CudaAmoebaKernelSources::amoebaOutOfPlaneBendForce, replacements), force.getForceGroup());
// static_cast<float>( force.getAmoebaGlobalOutOfPlaneBendQuartic()), cu.addForce(new ForceInfo(force));
// static_cast<float>( force.getAmoebaGlobalOutOfPlaneBendPentic()), }
// static_cast<float>( force.getAmoebaGlobalOutOfPlaneBendSextic() ) );
// data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force)); double CudaCalcAmoebaOutOfPlaneBendForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
//} return 0.0;
// }
//double CudaCalcAmoebaOutOfPlaneBendForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
// if( data.getAmoebaLocalForcesKernel() == this ){ /* -------------------------------------------------------------------------- *
// computeAmoebaLocalForces( data ); * AmoebaTorsionTorsion *
// } * -------------------------------------------------------------------------- */
// return 0.0;
//} class CudaCalcAmoebaTorsionTorsionForceKernel::ForceInfo : public CudaForceInfo {
// public:
///* -------------------------------------------------------------------------- * ForceInfo(const AmoebaTorsionTorsionForce& force) : force(force) {
// * AmoebaTorsionTorsion * }
// * -------------------------------------------------------------------------- */ int getNumParticleGroups() {
// return force.getNumTorsionTorsions();
//class CudaCalcAmoebaTorsionTorsionForceKernel::ForceInfo : public CudaForceInfo { }
//public: void getParticlesInGroup(int index, std::vector<int>& particles) {
// ForceInfo(const AmoebaTorsionTorsionForce& force) : force(force) { int particle1, particle2, particle3, particle4, particle5, chiralCheckAtomIndex, gridIndex;
// } force.getTorsionTorsionParameters(index, particle1, particle2, particle3, particle4, particle5, chiralCheckAtomIndex, gridIndex);
// int getNumParticleGroups() { particles.resize(5);
// return force.getNumTorsionTorsions(); particles[0] = particle1;
// } particles[1] = particle2;
// void getParticlesInGroup(int index, std::vector<int>& particles) { particles[2] = particle3;
// int particle1, particle2, particle3, particle4, particle5, chiralCheckAtomIndex, gridIndex; particles[3] = particle4;
// force.getTorsionTorsionParameters(index, particle1, particle2, particle3, particle4, particle5, chiralCheckAtomIndex, gridIndex); particles[4] = particle5;
// particles.resize(5); }
// particles[0] = particle1; bool areGroupsIdentical(int group1, int group2) {
// particles[1] = particle2; int particle1, particle2, particle3, particle4, particle5;
// particles[2] = particle3; int chiral1, chiral2, grid1, grid2;
// particles[3] = particle4; force.getTorsionTorsionParameters(group1, particle1, particle2, particle3, particle4, particle5, chiral1, grid1);
// particles[4] = particle5; force.getTorsionTorsionParameters(group2, particle1, particle2, particle3, particle4, particle5, chiral2, grid2);
// } return (grid1 == grid2);
// bool areGroupsIdentical(int group1, int group2) { }
// int particle1, particle2, particle3, particle4, particle5; private:
// int chiral1, chiral2, grid1, grid2; const AmoebaTorsionTorsionForce& force;
// force.getTorsionTorsionParameters(group1, particle1, particle2, particle3, particle4, particle5, chiral1, grid1); };
// force.getTorsionTorsionParameters(group2, particle1, particle2, particle3, particle4, particle5, chiral2, grid2);
// return (grid1 == grid2); CudaCalcAmoebaTorsionTorsionForceKernel::CudaCalcAmoebaTorsionTorsionForceKernel(std::string name, const Platform& platform, CudaContext& cu, System& system) :
// } CalcAmoebaTorsionTorsionForceKernel(name, platform), cu(cu), system(system), gridValues(NULL), gridParams(NULL), torsionParams(NULL) {
//private: }
// const AmoebaTorsionTorsionForce& force;
//}; CudaCalcAmoebaTorsionTorsionForceKernel::~CudaCalcAmoebaTorsionTorsionForceKernel() {
// cu.setAsCurrent();
//CudaCalcAmoebaTorsionTorsionForceKernel::CudaCalcAmoebaTorsionTorsionForceKernel(std::string name, const Platform& platform, CudaContext& cu, System& system) : if (gridValues != NULL)
// CalcAmoebaTorsionTorsionForceKernel(name, platform), cu(cu), system(system) { delete gridValues;
// data.incrementKernelCount(); if (gridParams != NULL)
//} delete gridParams;
// if (torsionParams != NULL)
//CudaCalcAmoebaTorsionTorsionForceKernel::~CudaCalcAmoebaTorsionTorsionForceKernel() { delete torsionParams;
// data.decrementKernelCount(); }
//}
// void CudaCalcAmoebaTorsionTorsionForceKernel::initialize(const System& system, const AmoebaTorsionTorsionForce& force) {
//void CudaCalcAmoebaTorsionTorsionForceKernel::initialize(const System& system, const AmoebaTorsionTorsionForce& force) { cu.setAsCurrent();
// int numContexts = cu.getPlatformData().contexts.size();
// data.setAmoebaLocalForcesKernel( this ); int startIndex = cu.getContextIndex()*force.getNumTorsionTorsions()/numContexts;
// numTorsionTorsions = force.getNumTorsionTorsions(); int endIndex = (cu.getContextIndex()+1)*force.getNumTorsionTorsions()/numContexts;
// numTorsionTorsions = endIndex-startIndex;
// // torsion-torsion parameters if (numTorsionTorsions == 0)
// return;
// std::vector<int> particle1(numTorsionTorsions);
// std::vector<int> particle2(numTorsionTorsions); // Record torsion parameters.
// std::vector<int> particle3(numTorsionTorsions);
// std::vector<int> particle4(numTorsionTorsions); vector<vector<int> > atoms(numTorsionTorsions, vector<int>(5));
// std::vector<int> particle5(numTorsionTorsions); vector<int2> torsionParamsVec(numTorsionTorsions);
// std::vector<int> chiralCheckAtomIndex(numTorsionTorsions); torsionParams = CudaArray::create<int2>(cu, numTorsionTorsions, "torsionTorsionParams");
// std::vector<int> gridIndices(numTorsionTorsions); for (int i = 0; i < numTorsionTorsions; i++)
// force.getTorsionTorsionParameters(startIndex+i, atoms[i][0], atoms[i][1], atoms[i][2], atoms[i][3], atoms[i][4], torsionParamsVec[i].x, torsionParamsVec[i].y);
// for (int i = 0; i < numTorsionTorsions; i++) { torsionParams->upload(torsionParamsVec);
// force.getTorsionTorsionParameters(i, particle1[i], particle2[i], particle3[i],
// particle4[i], particle5[i], // Record the grids.
// chiralCheckAtomIndex[i], gridIndices[i]);
// } vector<float4> gridValuesVec;
// gpuSetAmoebaTorsionTorsionParameters(data.getAmoebaGpu(), particle1, particle2, particle3, particle4, particle5, chiralCheckAtomIndex, gridIndices ); vector<float4> gridParamsVec;
// for (int i = 0; i < force.getNumTorsionTorsionGrids(); i++) {
// // torsion-torsion grids const TorsionTorsionGrid& initialGrid = force.getTorsionTorsionGrid(i);
//
// numTorsionTorsionGrids = force.getNumTorsionTorsionGrids(); // check if grid needs to be reordered: x-angle should be 'slow' index
// std::vector<TorsionTorsionGridFloat> floatGrids;
// bool reordered = false;
// floatGrids.resize(numTorsionTorsionGrids); TorsionTorsionGrid reorderedGrid;
// for (int gridIndex = 0; gridIndex < numTorsionTorsionGrids; gridIndex++) { if (initialGrid[0][0][0] != initialGrid[0][1][0]) {
// AmoebaTorsionTorsionForceImpl::reorderGrid(initialGrid, reorderedGrid);
// const TorsionTorsionGrid& grid = force.getTorsionTorsionGrid( gridIndex ); reordered = true;
// floatGrids[gridIndex].resize( grid.size() ); }
// const TorsionTorsionGrid& grid = (reordered ? reorderedGrid : initialGrid);
// // check if grid needs to be reordered: x-angle should be 'slow' index float range = grid[0][grid[0].size()-1][1] - grid[0][0][1];
// gridParamsVec.push_back(make_float4(gridValuesVec.size(), grid[0][0][0], range/(grid.size()-1), grid.size()));
// TorsionTorsionGrid reorderedGrid; for (int j = 0; j < grid.size(); j++)
// int reorder = 0; for (int k = 0; k < grid[j].size(); k++)
// if( grid[0][0][0] != grid[0][1][0] ){ gridValuesVec.push_back(make_float4((float) grid[j][k][2], (float) grid[j][k][3], (float) grid[j][k][4], (float) grid[j][k][5]));
// AmoebaTorsionTorsionForceImpl::reorderGrid( grid, reorderedGrid ); }
// reorder = 1; gridValues = CudaArray::create<float4>(cu, gridValuesVec.size(), "torsionTorsionGridValues");
// if( data.getLog() ){ gridParams = CudaArray::create<float4>(cu, gridParamsVec.size(), "torsionTorsionGridParams");
// (void) fprintf( data.getLog(), "CudaCalcAmoebaTorsionTorsionForceKernel Reordered torsion-torsion grid %4d [%u %u] %12.3f %12.3f [%u %u] %12.3f %12.3f.\n", gridValues->upload(gridValuesVec);
// gridIndex, static_cast<unsigned int>(grid.size()), static_cast<unsigned int>(grid[0].size()), grid[0][0][0], grid[0][1][0], gridParams->upload(gridParamsVec);
// static_cast<unsigned int>(reorderedGrid.size() ), static_cast<unsigned int>(reorderedGrid[0].size() ), reorderedGrid[0][0][0], reorderedGrid[0][1][0] ); map<string, string> replacements;
// } replacements["GRID_VALUES"] = cu.getBondedUtilities().addArgument(gridValues->getDevicePointer(), "float4");
// } replacements["GRID_PARAMS"] = cu.getBondedUtilities().addArgument(gridParams->getDevicePointer(), "float4");
// for (unsigned int ii = 0; ii < grid.size(); ii++) { replacements["TORSION_PARAMS"] = cu.getBondedUtilities().addArgument(torsionParams->getDevicePointer(), "int2");
// replacements["RAD_TO_DEG"] = cu.doubleToString(180/M_PI);
// floatGrids[gridIndex][ii].resize( grid[ii].size() ); cu.getBondedUtilities().addInteraction(atoms, cu.replaceStrings(CudaAmoebaKernelSources::amoebaTorsionTorsionForce, replacements), force.getForceGroup());
// for (unsigned int jj = 0; jj < grid[ii].size(); jj++) { cu.getBondedUtilities().addPrefixCode(CudaAmoebaKernelSources::bicubic);
// cu.addForce(new ForceInfo(force));
// floatGrids[gridIndex][ii][jj].resize( grid[ii][jj].size() ); }
// if( reorder ){
// double CudaCalcAmoebaTorsionTorsionForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
// for( unsigned int kk = 0; kk < grid[ii][jj].size(); kk++) { return 0.0;
// floatGrids[gridIndex][ii][jj][kk] = static_cast<float>(reorderedGrid[ii][jj][kk]); }
// }
//
// } else {
// for( unsigned int kk = 0; kk < grid[ii][jj].size(); kk++) {
// floatGrids[gridIndex][ii][jj][kk] = static_cast<float>(grid[ii][jj][kk]);
// }
// }
// }
// }
// }
// gpuSetAmoebaTorsionTorsionGrids(data.getAmoebaGpu(), floatGrids );
// data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force));
//}
//
//double CudaCalcAmoebaTorsionTorsionForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
// if( data.getAmoebaLocalForcesKernel() == this ){
// computeAmoebaLocalForces( data );
// }
// return 0.0;
//}
//
///* -------------------------------------------------------------------------- * ///* -------------------------------------------------------------------------- *
// * AmoebaMultipole * // * AmoebaMultipole *
// * -------------------------------------------------------------------------- */ // * -------------------------------------------------------------------------- */
......
...@@ -253,6 +253,7 @@ private: ...@@ -253,6 +253,7 @@ private:
int numStretchBends; int numStretchBends;
CudaContext& cu; CudaContext& cu;
System& system; System& system;
CudaArray* params;
}; };
/** /**
...@@ -283,6 +284,7 @@ private: ...@@ -283,6 +284,7 @@ private:
int numOutOfPlaneBends; int numOutOfPlaneBends;
CudaContext& cu; CudaContext& cu;
System& system; System& system;
CudaArray* params;
}; };
/** /**
...@@ -314,6 +316,9 @@ private: ...@@ -314,6 +316,9 @@ private:
int numTorsionTorsionGrids; int numTorsionTorsionGrids;
CudaContext& cu; CudaContext& cu;
System& system; System& system;
CudaArray* gridValues;
CudaArray* gridParams;
CudaArray* torsionParams;
}; };
/** /**
......
// compute the value of the bond angle
real xab = pos1.x - pos2.x;
real yab = pos1.y - pos2.y;
real zab = pos1.z - pos2.z;
real xcb = pos3.x - pos2.x;
real ycb = pos3.y - pos2.y;
real zcb = pos3.z - pos2.z;
// compute the out-of-plane bending angle
real xdb = pos4.x - pos2.x;
real ydb = pos4.y - pos2.y;
real zdb = pos4.z - pos2.z;
real xad = pos1.x - pos4.x;
real yad = pos1.y - pos4.y;
real zad = pos1.z - pos4.z;
real xcd = pos3.x - pos4.x;
real ycd = pos3.y - pos4.y;
real zcd = pos3.z - pos4.z;
real rdb2 = xdb*xdb + ydb*ydb + zdb*zdb;
real rad2 = xad*xad + yad*yad + zad*zad;
real rcd2 = xcd*xcd + ycd*ycd + zcd*zcd;
real ee = xab*(ycb*zdb-zcb*ydb) + yab*(zcb*xdb-xcb*zdb) + zab*(xcb*ydb-ycb*xdb);
real dot = xad*xcd + yad*ycd + zad*zcd;
real cc = rad2*rcd2 - dot*dot;
real bkk2 = (cc != 0 ? (ee*ee)/(cc) : (real) 0);
bkk2 = rdb2 - bkk2;
real adXcd_0 = yad*zcd - zad*ycd;
real adXcd_1 = zad*xcd - xad*zcd;
real adXcd_2 = xad*ycd - yad*xcd;
real adXcd_nrm2 = adXcd_0*adXcd_0 + adXcd_1*adXcd_1 + adXcd_2*adXcd_2;
real adXcd_dot_db = xdb*adXcd_0 + ydb*adXcd_1 + zdb*adXcd_2;
adXcd_dot_db /= SQRT(rdb2*adXcd_nrm2);
real angle = abs(ASIN(adXcd_dot_db));
// find the out-of-plane energy and master chain rule terms
real dt = RAD_TO_DEG*angle;
real dt2 = dt * dt;
real dt3 = dt2 * dt;
real dt4 = dt2 * dt2;
float k = (rdb2 != 0 && cc != 0) ? PARAMS[index] : 0.0f;
energy += k*dt2*(1.0f + CUBIC_K*dt + QUARTIC_K*dt2 + PENTIC_K*dt3 + SEXTIC_K*dt4);
real deddt = k*dt*RAD_TO_DEG*(2.0f + 3.0f*CUBIC_K*dt + 4.0f*QUARTIC_K*dt2 + 5.0f*PENTIC_K*dt3 + 6.0f*SEXTIC_K*dt4);
real eeSign = (ee >= 0 ? 1 : -1);
real dedcos = -deddt*eeSign/SQRT(cc*bkk2);
// chain rule terms for first derivative components
real term = ee / cc;
real dccdxia = (xad*rcd2-xcd*dot) * term;
real dccdyia = (yad*rcd2-ycd*dot) * term;
real dccdzia = (zad*rcd2-zcd*dot) * term;
real dccdxic = (xcd*rad2-xad*dot) * term;
real dccdyic = (ycd*rad2-yad*dot) * term;
real dccdzic = (zcd*rad2-zad*dot) * term;
real dccdxid = -dccdxia - dccdxic;
real dccdyid = -dccdyia - dccdyic;
real dccdzid = -dccdzia - dccdzic;
term = ee / rdb2;
real deedxia = ydb*zcb - zdb*ycb;
real deedyia = zdb*xcb - xdb*zcb;
real deedzia = xdb*ycb - ydb*xcb;
real deedxic = yab*zdb - zab*ydb;
real deedyic = zab*xdb - xab*zdb;
real deedzic = xab*ydb - yab*xdb;
real deedxid = ycb*zab - zcb*yab + xdb*term;
real deedyid = zcb*xab - xcb*zab + ydb*term;
real deedzid = xcb*yab - ycb*xab + zdb*term;
// compute first derivative components for this angle
real3 force1 = make_real3(-dedcos*(dccdxia+deedxia), -dedcos*(dccdyia+deedyia), -dedcos*(dccdzia+deedzia));
real3 force3 = make_real3(-dedcos*(dccdxic+deedxic), -dedcos*(dccdyic+deedyic), -dedcos*(dccdzic+deedzic));
real3 force4 = make_real3(-dedcos*(dccdxid+deedxid), -dedcos*(dccdyid+deedyid), -dedcos*(dccdzid+deedzid));
real3 force2 = make_real3(-force1.x-force3.x-force4.x, -force1.y-force3.y-force4.y, -force1.z-force3.z-force4.z);
// compute the value of the bond angle
real xab = pos1.x - pos2.x;
real yab = pos1.y - pos2.y;
real zab = pos1.z - pos2.z;
real xcb = pos3.x - pos2.x;
real ycb = pos3.y - pos2.y;
real zcb = pos3.z - pos2.z;
real rab = SQRT(xab*xab + yab*yab + zab*zab);
real rcb = SQRT(xcb*xcb + ycb*ycb + zcb*zcb);
real xp = ycb*zab - zcb*yab;
real yp = zcb*xab - xcb*zab;
real zp = xcb*yab - ycb*xab;
real rp = SQRT(xp*xp + yp*yp + zp*zp);
real dot = xab*xcb + yab*ycb + zab*zcb;
real cosine = rab*rcb > 0 ? (dot / (rab*rcb)) : (real) 1;
cosine = (cosine > 1 ? (real) 1 : cosine);
cosine = (cosine < -1 ? -(real) 1 : cosine);
real angle = ACOS(cosine);
// find chain rule terms for the bond angle deviation
float4 parameters = PARAMS[index];
real dt = RAD_TO_DEG*(angle - parameters.z);
real terma = rab*rp != 0 ? (-RAD_TO_DEG/(rab*rab*rp)) : (real) 0;
real termc = rcb*rp != 0 ? (RAD_TO_DEG/(rcb*rcb*rp)) : (real) 0;
real ddtdxia = terma * (yab*zp-zab*yp);
real ddtdyia = terma * (zab*xp-xab*zp);
real ddtdzia = terma * (xab*yp-yab*xp);
real ddtdxic = termc * (ycb*zp-zcb*yp);
real ddtdyic = termc * (zcb*xp-xcb*zp);
real ddtdzic = termc * (xcb*yp-ycb*xp);
// find chain rule terms for the bond length deviations
real dr = (parameters.x > 0 ? (rab - parameters.x) : (real) 0);
terma = (parameters.x > 0 ? RECIP(rab) : (real) 0);
dr += (parameters.y > 0 ? (rcb - parameters.y) : (real) 0);
termc = (parameters.y > 0 ? RECIP(rcb) : (real) 0);
real ddrdxia = terma * xab;
real ddrdyia = terma * yab;
real ddrdzia = terma * zab;
real ddrdxic = termc * xcb;
real ddrdyic = termc * ycb;
real ddrdzic = termc * zcb;
// get the energy and master chain rule terms for derivatives
real term = ((rp != 0) ? parameters.w : (real) 0);
energy += term*dt*dr;
real3 force1 = make_real3(-term*(dt*ddrdxia+ddtdxia*dr), -term*(dt*ddrdyia+ddtdyia*dr), -term*(dt*ddrdzia+ddtdzia*dr));
real3 force3 = make_real3(-term*(dt*ddrdxic+ddtdxic*dr), -term*(dt*ddrdyic+ddtdyic*dr), -term*(dt*ddrdzic+ddtdzic*dr));
real3 force2 = make_real3(-force1.x-force3.x, -force1.y-force3.y, -force1.z-force3.z);
int2 torsionParams = TORSION_PARAMS[index];
real xba = pos2.x - pos1.x;
real yba = pos2.y - pos1.y;
real zba = pos2.z - pos1.z;
real xcb = pos3.x - pos2.x;
real ycb = pos3.y - pos2.y;
real zcb = pos3.z - pos2.z;
real xdc = pos4.x - pos3.x;
real ydc = pos4.y - pos3.y;
real zdc = pos4.z - pos3.z;
real xed = pos5.x - pos4.x;
real yed = pos5.y - pos4.y;
real zed = pos5.z - pos4.z;
real xt = yba*zcb - ycb*zba;
real yt = zba*xcb - zcb*xba;
real zt = xba*ycb - xcb*yba;
real xu = ycb*zdc - ydc*zcb;
real yu = zcb*xdc - zdc*xcb;
real zu = xcb*ydc - xdc*ycb;
real rt2 = xt*xt + yt*yt + zt*zt;
real ru2 = xu*xu + yu*yu + zu*zu;
real rtru = SQRT(rt2 * ru2);
real xv = ydc*zed - yed*zdc;
real yv = zdc*xed - zed*xdc;
real zv = xdc*yed - xed*ydc;
real rv2 = xv*xv + yv*yv + zv*zv;
real rurv = SQRT(ru2 * rv2);
real rcb = SQRT(xcb*xcb + ycb*ycb + zcb*zcb);
real cosine1 = (rtru != 0 ? (xt*xu+yt*yu+zt*zu)/rtru : (real) 0);
cosine1 = (cosine1 > 1 ? (real) 1 : cosine1);
cosine1 = (cosine1 < -1 ? (real) -1 : cosine1);
real angle1 = RAD_TO_DEG * ACOS(cosine1);
real sign = xba*xu + yba*yu + zba*zu;
angle1 = (sign < 0 ? -angle1 : angle1);
real value1 = angle1;
real rdc = SQRT(xdc*xdc + ydc*ydc + zdc*zdc);
real cosine2 = (xu*xv + yu*yv + zu*zv) / rurv;
cosine2 = (cosine2 > 1 ? (real) 1 : cosine2);
cosine2 = (cosine2 < -1 ? (real) -1 : cosine2);
real angle2 = RAD_TO_DEG * ACOS(cosine2);
sign = xcb*xv + ycb*yv + zcb*zv;
angle2 = (sign < 0 ? -angle2 : angle2);
real value2 = angle2;
// check for inverted chirality at the central atom
// if atom2.y < 0, then no chiral check required
// sign is set to 1.0 in this case
// use atom5 for the atom index to avoid warp divergence
int chiralAtomIndex = (torsionParams.x > -1 ? torsionParams.x : atom5);
real4 pos6 = posq[chiralAtomIndex];
real xac = pos6.x - pos3.x;
real yac = pos6.y - pos3.y;
real zac = pos6.z - pos3.z;
real xbc = pos2.x - pos3.x;
real ybc = pos2.y - pos3.y;
real zbc = pos2.z - pos3.z;
// xdc, ydc, zdc appear above
real xdc1 = pos4.x - pos3.x;
real ydc1 = pos4.y - pos3.y;
real zdc1 = pos4.z - pos3.z;
real c1 = ybc*zdc1 - zbc*ydc1;
real c2 = ydc1*zac - zdc1*yac;
real c3 = yac*zbc - zac*ybc;
real vol = xac*c1 + xbc*c2 + xdc1*c3;
sign = (vol > 0 ? (real) 1 : (real) -1);
sign = (torsionParams.x < 0 ? (real) 1 : sign);
value1 *= sign;
value2 *= sign;
// use bicubic interpolation to compute spline values
// compute indices into grid based on angles
float4 gridParams = GRID_PARAMS[torsionParams.y];
int index1 = (int) ((value1 - gridParams.y)/gridParams.z + 1.0e-05f);
real fIndex = (real) index1;
real x1l = gridParams.z*fIndex + gridParams.y;
real x1u = x1l + gridParams.z;
int index2 = (int) ((value2 - gridParams.y)/gridParams.z + 1.0e-05f);
fIndex = (real) index2;
real x2l = gridParams.z*fIndex + gridParams.y;
real x2u = x2l + gridParams.z;
int posIndex1 = index2 + index1*(int) gridParams.w;
posIndex1 += (int) gridParams.x;
int posIndex2 = index2 + (index1+1)*(int) gridParams.w;
posIndex2 += (int) gridParams.x;
// load grid points surrounding angle
real4 y;
real4 y1;
real4 y2;
real4 y12;
int localIndex = posIndex1;
y.x = GRID_VALUES[localIndex].x;
y1.x = GRID_VALUES[localIndex].y;
y2.x = GRID_VALUES[localIndex].z;
y12.x = GRID_VALUES[localIndex].w;
localIndex = posIndex2;
y.y = GRID_VALUES[localIndex].x;
y1.y = GRID_VALUES[localIndex].y;
y2.y = GRID_VALUES[localIndex].z;
y12.y = GRID_VALUES[localIndex].w;
localIndex = posIndex2 + 1;
y.z = GRID_VALUES[localIndex].x;
y1.z = GRID_VALUES[localIndex].y;
y2.z = GRID_VALUES[localIndex].z;
y12.z = GRID_VALUES[localIndex].w;
localIndex = posIndex1 + 1;
y.w = GRID_VALUES[localIndex].x;
y1.w = GRID_VALUES[localIndex].y;
y2.w = GRID_VALUES[localIndex].z;
y12.w = GRID_VALUES[localIndex].w;
// perform interpolation
real e;
real dedang1;
real dedang2;
bicubic(y, y1, y2, y12, value1, x1l, x1u, value2, x2l, x2u, &e, &dedang1, &dedang2);
energy += e;
dedang1 *= sign * RAD_TO_DEG;
dedang2 *= sign * RAD_TO_DEG;
// chain rule terms for first angle derivative components
real xca = pos3.x - pos1.x;
real yca = pos3.y - pos1.y;
real zca = pos3.z - pos1.z;
real xdb = pos4.x - pos2.x;
real ydb = pos4.y - pos2.y;
real zdb = pos4.z - pos2.z;
real dedxt = dedang1 * (yt*zcb - ycb*zt) / (rt2*rcb);
real dedyt = dedang1 * (zt*xcb - zcb*xt) / (rt2*rcb);
real dedzt = dedang1 * (xt*ycb - xcb*yt) / (rt2*rcb);
real dedxu = -dedang1 * (yu*zcb - ycb*zu) / (ru2*rcb);
real dedyu = -dedang1 * (zu*xcb - zcb*xu) / (ru2*rcb);
real dedzu = -dedang1 * (xu*ycb - xcb*yu) / (ru2*rcb);
// compute first derivative components for first angle
real dedxia = zcb*dedyt - ycb*dedzt;
real dedyia = xcb*dedzt - zcb*dedxt;
real dedzia = ycb*dedxt - xcb*dedyt;
real dedxib = yca*dedzt - zca*dedyt + zdc*dedyu - ydc*dedzu;
real dedyib = zca*dedxt - xca*dedzt + xdc*dedzu - zdc*dedxu;
real dedzib = xca*dedyt - yca*dedxt + ydc*dedxu - xdc*dedyu;
real dedxic = zba*dedyt - yba*dedzt + ydb*dedzu - zdb*dedyu;
real dedyic = xba*dedzt - zba*dedxt + zdb*dedxu - xdb*dedzu;
real dedzic = yba*dedxt - xba*dedyt + xdb*dedyu - ydb*dedxu;
real dedxid = zcb*dedyu - ycb*dedzu;
real dedyid = xcb*dedzu - zcb*dedxu;
real dedzid = ycb*dedxu - xcb*dedyu;
// chain rule terms for second angle derivative components
real xec = pos5.x - pos3.x;
real yec = pos5.y - pos3.y;
real zec = pos5.z - pos3.z;
real dedxu2 = dedang2 * (yu*zdc - ydc*zu) / (ru2*rdc);
real dedyu2 = dedang2 * (zu*xdc - zdc*xu) / (ru2*rdc);
real dedzu2 = dedang2 * (xu*ydc - xdc*yu) / (ru2*rdc);
real dedxv2 = -dedang2 * (yv*zdc - ydc*zv) / (rv2*rdc);
real dedyv2 = -dedang2 * (zv*xdc - zdc*xv) / (rv2*rdc);
real dedzv2 = -dedang2 * (xv*ydc - xdc*yv) / (rv2*rdc);
// compute first derivative components for second angle
real dedxib2 = zdc*dedyu2 - ydc*dedzu2;
real dedyib2 = xdc*dedzu2 - zdc*dedxu2;
real dedzib2 = ydc*dedxu2 - xdc*dedyu2;
real dedxic2 = ydb*dedzu2 - zdb*dedyu2 + zed*dedyv2 - yed*dedzv2;
real dedyic2 = zdb*dedxu2 - xdb*dedzu2 + xed*dedzv2 - zed*dedxv2;
real dedzic2 = xdb*dedyu2 - ydb*dedxu2 + yed*dedxv2 - xed*dedyv2;
real dedxid2 = zcb*dedyu2 - ycb*dedzu2 + yec*dedzv2 - zec*dedyv2;
real dedyid2 = xcb*dedzu2 - zcb*dedxu2 + zec*dedxv2 - xec*dedzv2;
real dedzid2 = ycb*dedxu2 - xcb*dedyu2 + xec*dedyv2 - yec*dedxv2;
real dedxie2 = zdc*dedyv2 - ydc*dedzv2;
real dedyie2 = xdc*dedzv2 - zdc*dedxv2;
real dedzie2 = ydc*dedxv2 - xdc*dedyv2;
real3 force1 = make_real3(-dedxia, -dedyia, -dedzia);
real3 force2 = make_real3(-dedxib-dedxib2, -dedyib-dedyib2, -dedzib-dedzib2);
real3 force3 = make_real3(-dedxic-dedxic2, -dedyic-dedyic2, -dedzic-dedzic2);
real3 force4 = make_real3(-dedxid-dedxid2, -dedyid-dedyid2, -dedzid-dedzid2);
real3 force5 = make_real3(-dedxie2, -dedyie2, -dedzie2);
\ No newline at end of file
__device__ void bicubic(real4 y, real4 y1i, real4 y2i, real4 y12i, real x1, real x1l, real x1u,
real x2, real x2l, real x2u, real* energyOut, real* dang1Out, real* dang2Out) {
real c[4][4];
real d1 = x1u - x1l;
real d2 = x2u - x2l;
real d12 = d1*d2;
real4 y1 = d1*y1i;
real4 y2 = d2*y2i;
real4 y12 = d12*y12i;
c[0][0] = y.x;
c[0][1] = y2.x;
c[0][2] = 3.0f*(y.w - y.x) - (2.0f*y2.x + y2.w);
c[0][3] = 2.0f*(y.x - y.w) + y2.x + y2.w;
c[1][0] = y1.x;
c[1][1] = y12.x;
c[1][2] = 3.0f*(y1.w - y1.x) - (2.0f*y12.x + y12.w);
c[1][3] = 2.0f*(y1.x - y1.w) + y12.x + y12.w;
c[2][0] = 3.0f*(y.y - y.x) - (2.0f*y1.x + y1.y);
c[2][1] = 3.0f*(y2.y - y2.x) - (2.0f*y12.x + y12.y);
c[2][2] = 9.0f*(y.x - y.y + y.z - y.w) + 6.0f* y1.x + 3.0f* y1.y - 3.0f* y1.z - 6.0f* y1.w +
6.0f* y2.x - 6.0f* y2.y - 3.0f* y2.z + 3.0f* y2.w +
4.0f*y12.x + 2.0f*y12.y + y12.z + 2.0f*y12.w;
c[2][3] = 6.0f*(y.y - y.x + y.w - y.z) + -4.0f* y1.x - 2.0f* y1.y + 2.0f* y1.z + 4.0f* y1.w +
-3.0f* y2.x + 3.0f* y2.y + 3.0f* y2.z - 3.0f* y2.w
-2.0f*y12.x - y12.y - y12.z - 2.0f*y12.w;
c[3][0] = 2.0f*(y.x - y.y) + y1.x + y1.y;
c[3][1] = 2.0f*(y2.x - y2.y) + y12.x + y12.y;
c[3][2] = 6.0f*( y.y - y.x + y.w - y.z) +
3.0f*(y1.z + y1.w - y1.x - y1.y) +
2.0f*( 2.0f*(y2.y - y2.x) + y2.z - y2.w) +
-2.0f*(y12.x + y12.y) - y12.z - y12.w;
c[3][3] = 4.0f*( y.x - y.y + y.z - y.w) +
2.0f*( y1.x + y1.y - y1.z - y1.w) +
2.0f*( y2.x - y2.y - y2.z + y2.w) +
y12.x + y12.y + y12.z + y12.w;
real t = (x1-x1l) / (x1u-x1l);
real u = (x2-x2l) / (x2u-x2l);
real energy = ((c[3][3]*u + c[3][2])*u + c[3][1])*u + c[3][0];
energy = t*energy + ((c[2][3]*u + c[2][2])*u + c[2][1])*u + c[2][0];
energy = t*energy + ((c[1][3]*u + c[1][2])*u + c[1][1])*u + c[1][0];
energy = t*energy + ((c[0][3]*u + c[0][2])*u + c[0][1])*u + c[0][0];
real dang1 = (3.0f*c[3][3]*t + 2.0f*c[2][3])*t + c[1][3];
dang1 = u*dang1 + (3.0f*c[3][2]*t + 2.0f*c[2][2])*t + c[1][2];
dang1 = u*dang1 + (3.0f*c[3][1]*t + 2.0f*c[2][1])*t + c[1][1];
dang1 = u*dang1 + (3.0f*c[3][0]*t + 2.0f*c[2][0])*t + c[1][0];
real dang2 = (3.0f*c[3][3]*u + 2.0f*c[3][2])*u + c[3][1];
dang2 = t*dang2 + (3.0f*c[2][3]*u + 2.0f*c[2][2])*u + c[2][1];
dang2 = t*dang2 + (3.0f*c[1][3]*u + 2.0f*c[1][2])*u + c[1][1];
dang2 = t*dang2 + (3.0f*c[0][3]*u + 2.0f*c[0][2])*u + c[0][1];
dang1 = dang1 / (x1u-x1l);
dang2 = dang2 / (x2u-x2l);
*energyOut = energy;
*dang1Out = dang1;
*dang2Out = dang2;
}
\ No newline at end of file
/* -------------------------------------------------------------------------- *
* OpenMMAmoeba *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation toolkit originating from *
* Simbios, the NIH National Center for Physics-Based Simulation of *
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2008 Stanford University and the Authors. *
* Authors: Mark Friedrichs *
* Contributors: *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
* USE OR OTHER DEALINGS IN THE SOFTWARE. *
* -------------------------------------------------------------------------- */
/**
* This tests the CUDA implementation of CudaAmoebaOutOfPlaneBendForce.
*/
#include "openmm/internal/AssertionUtilities.h"
#include "AmoebaTinkerParameterFile.h"
#include "openmm/Context.h"
#include "OpenMMAmoeba.h"
#include "openmm/System.h"
#include "openmm/LangevinIntegrator.h"
#include <iostream>
#include <vector>
using namespace OpenMM;
const double TOL = 1e-3;
#define PI_M 3.141592653589
#define RADIAN 57.29577951308
/* ---------------------------------------------------------------------------------------
Compute cross product of two 3-vectors and place in 3rd vector
vectorZ = vectorX x vectorY
@param vectorX x-vector
@param vectorY y-vector
@param vectorZ z-vector
@return vector is vectorZ
--------------------------------------------------------------------------------------- */
static void crossProductVector3( double* vectorX, double* vectorY, double* vectorZ ){
vectorZ[0] = vectorX[1]*vectorY[2] - vectorX[2]*vectorY[1];
vectorZ[1] = vectorX[2]*vectorY[0] - vectorX[0]*vectorY[2];
vectorZ[2] = vectorX[0]*vectorY[1] - vectorX[1]*vectorY[0];
return;
}
static double dotVector3( double* vectorX, double* vectorY ){
return vectorX[0]*vectorY[0] + vectorX[1]*vectorY[1] + vectorX[2]*vectorY[2];
}
static void computeAmoebaOutOfPlaneBendForce(int bondIndex, std::vector<Vec3>& positions, AmoebaOutOfPlaneBendForce& amoebaOutOfPlaneBendForce,
std::vector<Vec3>& forces, double* energy, FILE* log ) {
double kAngleCubic = amoebaOutOfPlaneBendForce.getAmoebaGlobalOutOfPlaneBendCubic();
double kAngleQuartic = amoebaOutOfPlaneBendForce.getAmoebaGlobalOutOfPlaneBendQuartic();
double kAnglePentic = amoebaOutOfPlaneBendForce.getAmoebaGlobalOutOfPlaneBendPentic();
double kAngleSextic = amoebaOutOfPlaneBendForce.getAmoebaGlobalOutOfPlaneBendSextic();
int particle1, particle2, particle3, particle4;
double kAngleQuadratic;
amoebaOutOfPlaneBendForce.getOutOfPlaneBendParameters(bondIndex, particle1, particle2, particle3, particle4, kAngleQuadratic );
#ifdef AMOEBA_DEBUG
if( log ){
(void) fprintf( log, "computeAmoebaOutOfPlaneBendForce: bond %d [%d %d %d %d] k=[%10.3e %10.3e %10.3e %10.3e %10.3e]\n",
bondIndex, particle1, particle2, particle3, particle4, kAngleQuadratic, kAngleCubic, kAngleQuartic, kAnglePentic, kAngleSextic );
(void) fflush( log );
}
#endif
enum { A, B, C, D, LastAtomIndex };
enum { AB, CB, DB, AD, CD, LastDeltaIndex };
// ---------------------------------------------------------------------------------------
// get deltaR between various combinations of the 4 atoms
// and various intermediate terms
double deltaR[LastDeltaIndex][6];
for( int ii = 0; ii < 3; ii++ ){
deltaR[AB][ii] = positions[particle1][ii] - positions[particle2][ii];
deltaR[CB][ii] = positions[particle3][ii] - positions[particle2][ii];
deltaR[DB][ii] = positions[particle4][ii] - positions[particle2][ii];
deltaR[AD][ii] = positions[particle1][ii] - positions[particle4][ii];
deltaR[CD][ii] = positions[particle3][ii] - positions[particle4][ii];
}
double rDB2 = dotVector3( deltaR[DB], deltaR[DB] );
double rAD2 = dotVector3( deltaR[AD], deltaR[AD] );
double rCD2 = dotVector3( deltaR[CD], deltaR[CD] );
double tempVector[3];
crossProductVector3( deltaR[CB], deltaR[DB], tempVector );
double eE = dotVector3( deltaR[AB], tempVector );
double dot = dotVector3( deltaR[AD], deltaR[CD] );
double cc = rAD2*rCD2 - dot*dot;
if( rDB2 <= 0.0 || cc == 0.0 ){
return;
}
double bkk2 = rDB2 - eE*eE/cc;
double cosine = sqrt(bkk2/rDB2);
double angle;
if( cosine >= 1.0 ){
angle = 0.0;
} else if( cosine <= -1.0 ){
angle = PI_M;
} else {
angle = RADIAN*acos(cosine);
}
#ifdef AMOEBA_DEBUG
if( log ){
(void) fprintf( log, "computeAmoebaOutOfPlaneBendForce: bkk2=%14.7e rDB2=%14.7e cos=%14.7e dt=%14.7e]\n",
bkk2, rDB2, cosine, angle );
(void) fflush( log );
}
#endif
// chain rule
double dt = angle;
double dt2 = dt*dt;
double dt3 = dt2*dt;
double dt4 = dt2*dt2;
double dEdDt = 2.0 + 3.0*kAngleCubic*dt + 4.0*kAngleQuartic*dt2 + 5.0*kAnglePentic *dt3 + 6.0*kAngleSextic *dt4;
dEdDt *= kAngleQuadratic*dt*RADIAN;
double dEdCos;
dEdCos = dEdDt/sqrt(cc*bkk2);
if( eE > 0.0 ){
dEdCos *= -1.0;
}
double term = eE/cc;
double dccd[LastAtomIndex][3];
for( int ii = 0; ii < 3; ii++ ){
dccd[A][ii] = (deltaR[AD][ii]*rCD2 - deltaR[CD][ii]*dot)*term;
dccd[C][ii] = (deltaR[CD][ii]*rAD2 - deltaR[AD][ii]*dot)*term;
dccd[D][ii] = -1.0*(dccd[A][ii] + dccd[C][ii]);
}
double deed[LastAtomIndex][3];
crossProductVector3( deltaR[DB], deltaR[CB], deed[A] );
crossProductVector3( deltaR[AB], deltaR[DB], deed[C] );
crossProductVector3( deltaR[CB], deltaR[AB], deed[D] );
term = eE/rDB2;
deed[D][0] += deltaR[DB][0]*term;
deed[D][1] += deltaR[DB][1]*term;
deed[D][2] += deltaR[DB][2]*term;
// ---------------------------------------------------------------------------------------
// forces
// calculate forces for atoms a, c, d
// the force for b is then -( a+ c + d)
double subForce[LastAtomIndex][3];
for( int jj = 0; jj < LastAtomIndex; jj++ ){
// A, C, D
for( int ii = 0; ii < 3; ii++ ){
subForce[jj][ii] = dEdCos*( dccd[jj][ii] + deed[jj][ii] );
}
if( jj == 0 )jj++; // skip B
// now compute B
if( jj == 3 ){
for( int ii = 0; ii < 3; ii++ ){
subForce[1][ii] = -1.0*(subForce[0][ii] + subForce[2][ii] + subForce[3][ii]);
}
}
}
// accumulate forces and energy
forces[particle1][0] -= subForce[0][0];
forces[particle1][1] -= subForce[0][1];
forces[particle1][2] -= subForce[0][2];
forces[particle2][0] -= subForce[1][0];
forces[particle2][1] -= subForce[1][1];
forces[particle2][2] -= subForce[1][2];
forces[particle3][0] -= subForce[2][0];
forces[particle3][1] -= subForce[2][1];
forces[particle3][2] -= subForce[2][2];
forces[particle4][0] -= subForce[3][0];
forces[particle4][1] -= subForce[3][1];
forces[particle4][2] -= subForce[3][2];
// ---------------------------------------------------------------------------------------
// calculate energy if 'energy' is set
double energyTerm = 1.0 + kAngleCubic *dt +
kAngleQuartic*dt2 +
kAnglePentic *dt3 +
kAngleSextic *dt4;
energyTerm *= kAngleQuadratic*dt2;
*energy += energyTerm;
return;
}
static void computeAmoebaOutOfPlaneBendForces( Context& context, AmoebaOutOfPlaneBendForce& amoebaOutOfPlaneBendForce,
std::vector<Vec3>& expectedForces, double* expectedEnergy, FILE* log ) {
// get positions and zero forces
State state = context.getState(State::Positions);
std::vector<Vec3> positions = state.getPositions();
expectedForces.resize( positions.size() );
for( unsigned int ii = 0; ii < expectedForces.size(); ii++ ){
expectedForces[ii][0] = expectedForces[ii][1] = expectedForces[ii][2] = 0.0;
}
// calculates forces/energy
*expectedEnergy = 0.0;
for( int ii = 0; ii < amoebaOutOfPlaneBendForce.getNumOutOfPlaneBends(); ii++ ){
computeAmoebaOutOfPlaneBendForce(ii, positions, amoebaOutOfPlaneBendForce, expectedForces, expectedEnergy, log );
}
#ifdef AMOEBA_DEBUG
if( log ){
(void) fprintf( log, "computeAmoebaOutOfPlaneBendForces: expected energy=%14.7e\n", *expectedEnergy );
for( unsigned int ii = 0; ii < positions.size(); ii++ ){
(void) fprintf( log, "%6u [%14.7e %14.7e %14.7e]\n", ii, expectedForces[ii][0], expectedForces[ii][1], expectedForces[ii][2] );
}
(void) fflush( log );
}
#endif
return;
}
void compareWithExpectedForceAndEnergy( Context& context, AmoebaOutOfPlaneBendForce& amoebaOutOfPlaneBendForce,
double tolerance, const std::string& idString, FILE* log) {
std::vector<Vec3> expectedForces;
double expectedEnergy;
computeAmoebaOutOfPlaneBendForces( context, amoebaOutOfPlaneBendForce, expectedForces, &expectedEnergy, log );
State state = context.getState(State::Forces | State::Energy);
const std::vector<Vec3> forces = state.getForces();
#ifdef AMOEBA_DEBUG
if( log ){
(void) fprintf( log, "computeAmoebaOutOfPlaneBendForces: expected energy=%14.7e %14.7e\n", expectedEnergy, state.getPotentialEnergy() );
for( unsigned int ii = 0; ii < forces.size(); ii++ ){
(void) fprintf( log, "%6u [%14.7e %14.7e %14.7e] [%14.7e %14.7e %14.7e]\n", ii,
expectedForces[ii][0], expectedForces[ii][1], expectedForces[ii][2], forces[ii][0], forces[ii][1], forces[ii][2] );
}
(void) fflush( log );
}
#endif
for( unsigned int ii = 0; ii < forces.size(); ii++ ){
ASSERT_EQUAL_VEC( expectedForces[ii], forces[ii], tolerance );
}
ASSERT_EQUAL_TOL( expectedEnergy, state.getPotentialEnergy(), tolerance );
}
void testOneOutOfPlaneBend( FILE* log ) {
System system;
int numberOfParticles = 4;
for( int ii = 0; ii < numberOfParticles; ii++ ){
system.addParticle(1.0);
}
LangevinIntegrator integrator(0.0, 0.1, 0.01);
AmoebaOutOfPlaneBendForce* amoebaOutOfPlaneBendForce = new AmoebaOutOfPlaneBendForce();
amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendCubic( -0.1400000E-01 );
amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendQuartic( 0.5600000E-04 );
amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendPentic( -0.7000000E-06 );
amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendSextic( 0.2200000E-07 );
double kOutOfPlaneBend = 0.328682196E-01;
amoebaOutOfPlaneBendForce->addOutOfPlaneBend(0, 1, 2, 3, kOutOfPlaneBend );
system.addForce(amoebaOutOfPlaneBendForce);
Context context(system, integrator, Platform::getPlatformByName( "CUDA"));
std::vector<Vec3> positions(numberOfParticles);
positions[0] = Vec3( 0.262660000E+02, 0.254130000E+02, 0.284200000E+01 );
positions[1] = Vec3( 0.269130000E+02, 0.266390000E+02, 0.353100000E+01 );
positions[2] = Vec3( 0.278860000E+02, 0.264630000E+02, 0.426300000E+01 );
positions[3] = Vec3( 0.245568230E+02, 0.250215290E+02, 0.796852800E+01 );
context.setPositions(positions);
compareWithExpectedForceAndEnergy( context, *amoebaOutOfPlaneBendForce, TOL, "testOneOutOfPlaneBend", log );
}
void testOneOutOfPlaneBend2( FILE* log, int setId ) {
System system;
int numberOfParticles = 4;
for( int ii = 0; ii < numberOfParticles; ii++ ){
system.addParticle(1.0);
}
LangevinIntegrator integrator(0.0, 0.1, 0.01);
AmoebaOutOfPlaneBendForce* amoebaOutOfPlaneBendForce = new AmoebaOutOfPlaneBendForce();
amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendCubic( -0.1400000E-01 );
amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendQuartic( 0.5600000E-04 );
amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendPentic( -0.7000000E-06 );
amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendSextic( 0.2200000E-07 );
/*
285 441 442 443 444 0.328682196E-01
286 441 442 444 443 0.164493407E-01
287 443 442 444 441 0.636650407E-02
288 442 444 447 448 0.392956472E-02
289 442 444 448 447 0.392956472E-02
290 447 444 448 442 0.214755281E-01
441 0.893800000E+01 0.439800000E+01 0.343100000E+01
442 0.779100000E+01 0.614600000E+01 0.390100000E+01
443 0.915400000E+01 0.683900000E+01 0.389400000E+01
444 0.101770000E+02 0.619000000E+01 0.379900000E+01
445 0.921000000E+01 0.813800000E+01 0.398600000E+01
446 0.708500000E+01 0.672900000E+01 0.332700000E+01
447 0.744300000E+01 0.605200000E+01 0.491900000E+01
448 0.100820000E+02 0.859300000E+01 0.398200000E+01
449 0.838000000E+01 0.866100000E+01 0.406000000E+01
*/
std::map<int,Vec3> coordinates;
coordinates[440] = Vec3( 0.893800000E+01, 0.439800000E+01, 0.343100000E+01 );
coordinates[441] = Vec3( 0.779100000E+01, 0.614600000E+01, 0.390100000E+01 );
coordinates[442] = Vec3( 0.915400000E+01, 0.683900000E+01, 0.389400000E+01 );
coordinates[443] = Vec3( 0.101770000E+02, 0.619000000E+01, 0.379900000E+01 );
coordinates[444] = Vec3( 0.921000000E+01, 0.813800000E+01, 0.398600000E+01 );
coordinates[445] = Vec3( 0.708500000E+01, 0.672900000E+01, 0.332700000E+01 );
coordinates[446] = Vec3( 0.744300000E+01, 0.605200000E+01, 0.491900000E+01 );
coordinates[447] = Vec3( 0.100820000E+02, 0.859300000E+01, 0.398200000E+01 );
coordinates[448] = Vec3( 0.838000000E+01, 0.866100000E+01, 0.406000000E+01 );
double kOutOfPlaneBend = 0.328682196E-01;
std::vector<int> particleIndices;
if( setId == 1 ){
particleIndices.push_back( 441 );
particleIndices.push_back( 442 );
particleIndices.push_back( 443 );
particleIndices.push_back( 444 );
kOutOfPlaneBend = 0.328682196E-01;
} else if( setId == 2 ){
particleIndices.push_back( 441 );
particleIndices.push_back( 442 );
particleIndices.push_back( 444 );
particleIndices.push_back( 443 );
kOutOfPlaneBend = 0.164493407E-01;
} else if( setId == 3 ){
particleIndices.push_back( 443 );
particleIndices.push_back( 442 );
particleIndices.push_back( 444 );
particleIndices.push_back( 441 );
kOutOfPlaneBend = 0.636650407E-02;
} else if( setId == 4 ){
particleIndices.push_back( 442 );
particleIndices.push_back( 444 );
particleIndices.push_back( 447 );
particleIndices.push_back( 448 );
kOutOfPlaneBend = 0.392956472E-02;
} else if( setId == 5 ){
particleIndices.push_back( 442 );
particleIndices.push_back( 444 );
particleIndices.push_back( 448 );
particleIndices.push_back( 447 );
kOutOfPlaneBend = 0.392956472E-02;
} else if( setId == 6 ){
particleIndices.push_back( 447 );
particleIndices.push_back( 444 );
particleIndices.push_back( 448 );
particleIndices.push_back( 442 );
kOutOfPlaneBend = 0.214755281E-01;
} else {
#ifdef AMOEBA_DEBUG
if( log ){
(void) fprintf( log, "Set id %d not recognized.\n", setId );
}
#endif
std::stringstream buffer;
buffer << "Set id " << setId << " not recognized.";
throw OpenMMException( buffer.str() );
}
#ifdef AMOEBA_DEBUG
if( log ){
(void) fprintf( log, "Set id %d.\n", setId );
}
#endif
amoebaOutOfPlaneBendForce->addOutOfPlaneBend(0, 1, 2, 3, kOutOfPlaneBend );
system.addForce(amoebaOutOfPlaneBendForce);
Context context(system, integrator, Platform::getPlatformByName( "CUDA"));
std::vector<Vec3> positions(numberOfParticles);
for( unsigned int ii = 0; ii < numberOfParticles; ii++ ){
if( coordinates.find( particleIndices[ii] ) == coordinates.end() ){
#ifdef AMOEBA_DEBUG
if( log ){
(void) fprintf( log, "Coordinates %d not loaded.", particleIndices[ii] );
}
#endif
std::stringstream buffer;
buffer << "Coordinates " << particleIndices[ii] << " not loaded.";
throw OpenMMException( buffer.str() );
}
positions[ii] = coordinates[particleIndices[ii]];
}
context.setPositions(positions);
compareWithExpectedForceAndEnergy( context, *amoebaOutOfPlaneBendForce, TOL, "testOneOutOfPlaneBend", log );
static int iter = 0;
static std::map<int,Vec3> totalForces;
static double totalEnergy;
if( iter == 0 ){
totalForces[441] = Vec3( 0.0, 0.0, 0.0 );
totalForces[442] = Vec3( 0.0, 0.0, 0.0 );
totalForces[443] = Vec3( 0.0, 0.0, 0.0 );
totalForces[444] = Vec3( 0.0, 0.0, 0.0 );
totalForces[445] = Vec3( 0.0, 0.0, 0.0 );
totalForces[446] = Vec3( 0.0, 0.0, 0.0 );
totalForces[447] = Vec3( 0.0, 0.0, 0.0 );
totalForces[448] = Vec3( 0.0, 0.0, 0.0 );
totalForces[449] = Vec3( 0.0, 0.0, 0.0 );
totalEnergy = 0.0;
}
iter++;
std::vector<Vec3> forces;
forces.resize( numberOfParticles );
double energy;
computeAmoebaOutOfPlaneBendForce( 0, positions, *amoebaOutOfPlaneBendForce, forces, &energy, log );
totalEnergy += energy;
for( unsigned int ii = 0; ii < numberOfParticles; ii++ ){
for( unsigned int jj = 0; jj < 3; jj++ ){
totalForces[particleIndices[ii]][jj] += forces[ii][jj];
}
}
if( iter == 6 ){
#ifdef AMOEBA_DEBUG
if( log ){
(void) fprintf( log, "computeAmoebaOutOfPlaneBendForces: energy=%14.7e\n", totalEnergy);
for( std::map<int,Vec3>::iterator ii = totalForces.begin(); ii != totalForces.end(); ii++ ){
int particleIndex = ii->first;
Vec3 forces = ii->second;
(void) fprintf( log, "%6d [%14.7e %14.7e %14.7e] \n", particleIndex,
forces[0], forces[1], forces[2] );
}
(void) fflush( log );
}
#endif
}
}
int main( int numberOfArguments, char* argv[] ) {
try {
std::cout << "TestCudaAmoebaOutOfPlaneBendForce running test..." << std::endl;
registerAmoebaCudaKernelFactories();
FILE* log = NULL;
testOneOutOfPlaneBend( log );
} catch(const std::exception& e) {
std::cout << "exception: " << e.what() << std::endl;
std::cout << "FAIL - ERROR. Test failed." << std::endl;
return 1;
}
std::cout << "Done" << std::endl;
return 0;
}
/* -------------------------------------------------------------------------- *
* OpenMMAmoeba *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation toolkit originating from *
* Simbios, the NIH National Center for Physics-Based Simulation of *
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2008 Stanford University and the Authors. *
* Authors: Mark Friedrichs *
* Contributors: *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
* USE OR OTHER DEALINGS IN THE SOFTWARE. *
* -------------------------------------------------------------------------- */
/**
* This tests the CUDA implementation of CudaAmoebaStretchBendForce.
*/
#include "openmm/internal/AssertionUtilities.h"
#include "AmoebaTinkerParameterFile.h"
#include "openmm/Context.h"
#include "OpenMMAmoeba.h"
#include "openmm/System.h"
#include "openmm/LangevinIntegrator.h"
#include <iostream>
#include <vector>
using namespace OpenMM;
const double TOL = 1e-5;
#define PI_M 3.141592653589
#define RADIAN 57.29577951308
/* ---------------------------------------------------------------------------------------
Compute cross product of two 3-vectors and place in 3rd vector
vectorZ = vectorX x vectorY
@param vectorX x-vector
@param vectorY y-vector
@param vectorZ z-vector
@return vector is vectorZ
--------------------------------------------------------------------------------------- */
static void crossProductVector3( double* vectorX, double* vectorY, double* vectorZ ){
vectorZ[0] = vectorX[1]*vectorY[2] - vectorX[2]*vectorY[1];
vectorZ[1] = vectorX[2]*vectorY[0] - vectorX[0]*vectorY[2];
vectorZ[2] = vectorX[0]*vectorY[1] - vectorX[1]*vectorY[0];
return;
}
static double dotVector3( double* vectorX, double* vectorY ){
return vectorX[0]*vectorY[0] + vectorX[1]*vectorY[1] + vectorX[2]*vectorY[2];
}
static void computeAmoebaStretchBendForce(int bondIndex, std::vector<Vec3>& positions, AmoebaStretchBendForce& amoebaStretchBendForce,
std::vector<Vec3>& forces, double* energy, FILE* log ) {
int particle1, particle2, particle3;
double abBondLength, cbBondLength, angleStretchBend, kStretchBend;
amoebaStretchBendForce.getStretchBendParameters(bondIndex, particle1, particle2, particle3, abBondLength, cbBondLength, angleStretchBend, kStretchBend);
angleStretchBend *= RADIAN;
#ifdef AMOEBA_DEBUG
if( log ){
(void) fprintf( log, "computeAmoebaStretchBendForce: bond %d [%d %d %d] ab=%10.3e cb=%10.3e angle=%10.3e k=%10.3e\n",
bondIndex, particle1, particle2, particle3, abBondLength, cbBondLength, angleStretchBend, kStretchBend );
(void) fflush( log );
}
#endif
enum { A, B, C, LastAtomIndex };
enum { AB, CB, CBxAB, ABxP, CBxP, LastDeltaIndex };
// ---------------------------------------------------------------------------------------
// get deltaR between various combinations of the 3 atoms
// and various intermediate terms
double deltaR[LastDeltaIndex][3];
double rAB2 = 0.0;
double rCB2 = 0.0;
for( int ii = 0; ii < 3; ii++ ){
deltaR[AB][ii] = positions[particle1][ii] - positions[particle2][ii];
rAB2 += deltaR[AB][ii]*deltaR[AB][ii];
deltaR[CB][ii] = positions[particle3][ii] - positions[particle2][ii];
rCB2 += deltaR[CB][ii]*deltaR[CB][ii];
}
double rAB = sqrt( rAB2 );
double rCB = sqrt( rCB2 );
crossProductVector3( deltaR[CB], deltaR[AB], deltaR[CBxAB] );
double rP = dotVector3( deltaR[CBxAB], deltaR[CBxAB] );
rP = sqrt( rP );
if( rP <= 0.0 ){
return;
}
double dot = dotVector3( deltaR[CB], deltaR[AB] );
double cosine = dot/(rAB*rCB);
double angle;
if( cosine >= 1.0 ){
angle = 0.0;
} else if( cosine <= -1.0 ){
angle = PI_M;
} else {
angle = RADIAN*acos(cosine);
}
double termA = -RADIAN/(rAB2*rP);
double termC = RADIAN/(rCB2*rP);
// P = CBxAB
crossProductVector3( deltaR[AB], deltaR[CBxAB], deltaR[ABxP] );
crossProductVector3( deltaR[CB], deltaR[CBxAB], deltaR[CBxP] );
for( int ii = 0; ii < 3; ii++ ){
deltaR[ABxP][ii] *= termA;
deltaR[CBxP][ii] *= termC;
}
double dr = rAB - abBondLength + rCB - cbBondLength;
termA = 1.0/rAB;
termC = 1.0/rCB;
double term = kStretchBend;
// ---------------------------------------------------------------------------------------
// forces
// calculate forces for atoms a, b, c
// the force for b is then -( a + c)
double subForce[LastAtomIndex][3];
double dt = angle - angleStretchBend;
for( int jj = 0; jj < 3; jj++ ){
subForce[A][jj] = term*(dt*termA*deltaR[AB][jj] + dr*deltaR[ABxP][jj] );
subForce[C][jj] = term*(dt*termC*deltaR[CB][jj] + dr*deltaR[CBxP][jj] );
subForce[B][jj] = -( subForce[A][jj] + subForce[C][jj] );
}
// ---------------------------------------------------------------------------------------
// accumulate forces and energy
forces[particle1][0] -= subForce[0][0];
forces[particle1][1] -= subForce[0][1];
forces[particle1][2] -= subForce[0][2];
forces[particle2][0] -= subForce[1][0];
forces[particle2][1] -= subForce[1][1];
forces[particle2][2] -= subForce[1][2];
forces[particle3][0] -= subForce[2][0];
forces[particle3][1] -= subForce[2][1];
forces[particle3][2] -= subForce[2][2];
*energy += term*dt*dr;
#ifdef AMOEBA_DEBUG
if( log ){
(void) fprintf( log, "computeAmoebaStretchBendForce: angle=%10.3e dt=%10.3e dr=%10.3e\n", angle, dt, dr );
(void) fflush( log );
}
#endif
return;
}
static void computeAmoebaStretchBendForces( Context& context, AmoebaStretchBendForce& amoebaStretchBendForce,
std::vector<Vec3>& expectedForces, double* expectedEnergy, FILE* log ) {
// get positions and zero forces
State state = context.getState(State::Positions);
std::vector<Vec3> positions = state.getPositions();
expectedForces.resize( positions.size() );
for( unsigned int ii = 0; ii < expectedForces.size(); ii++ ){
expectedForces[ii][0] = expectedForces[ii][1] = expectedForces[ii][2] = 0.0;
}
// calculates forces/energy
*expectedEnergy = 0.0;
for( int ii = 0; ii < amoebaStretchBendForce.getNumStretchBends(); ii++ ){
computeAmoebaStretchBendForce(ii, positions, amoebaStretchBendForce, expectedForces, expectedEnergy, log );
}
#ifdef AMOEBA_DEBUG
if( log ){
(void) fprintf( log, "computeAmoebaStretchBendForces: expected energy=%14.7e\n", *expectedEnergy );
for( unsigned int ii = 0; ii < positions.size(); ii++ ){
(void) fprintf( log, "%6u [%14.7e %14.7e %14.7e]\n", ii, expectedForces[ii][0], expectedForces[ii][1], expectedForces[ii][2] );
}
(void) fflush( log );
}
#endif
return;
}
void compareWithExpectedForceAndEnergy( Context& context, AmoebaStretchBendForce& amoebaStretchBendForce,
double tolerance, const std::string& idString, FILE* log) {
std::vector<Vec3> expectedForces;
double expectedEnergy;
computeAmoebaStretchBendForces( context, amoebaStretchBendForce, expectedForces, &expectedEnergy, log );
State state = context.getState(State::Forces | State::Energy);
const std::vector<Vec3> forces = state.getForces();
#ifdef AMOEBA_DEBUG
if( log ){
(void) fprintf( log, "computeAmoebaStretchBendForces: expected energy=%14.7e %14.7e\n", expectedEnergy, state.getPotentialEnergy() );
for( unsigned int ii = 0; ii < forces.size(); ii++ ){
(void) fprintf( log, "%6u [%14.7e %14.7e %14.7e] [%14.7e %14.7e %14.7e]\n", ii,
expectedForces[ii][0], expectedForces[ii][1], expectedForces[ii][2], forces[ii][0], forces[ii][1], forces[ii][2] );
}
(void) fflush( log );
}
#endif
for( unsigned int ii = 0; ii < forces.size(); ii++ ){
ASSERT_EQUAL_VEC( expectedForces[ii], forces[ii], tolerance );
}
ASSERT_EQUAL_TOL( expectedEnergy, state.getPotentialEnergy(), tolerance );
}
void testOneStretchBend( FILE* log ) {
System system;
int numberOfParticles = 3;
for( int ii = 0; ii < numberOfParticles; ii++ ){
system.addParticle(1.0);
}
LangevinIntegrator integrator(0.0, 0.1, 0.01);
AmoebaStretchBendForce* amoebaStretchBendForce = new AmoebaStretchBendForce();
double abLength = 0.144800000E+01;
double cbLength = 0.101500000E+01;
double angleStretchBend = 0.108500000E+03*DegreesToRadians;
//double kStretchBend = 0.750491578E-01;
double kStretchBend = 1.0;
amoebaStretchBendForce->addStretchBend(0, 1, 2, abLength, cbLength, angleStretchBend, kStretchBend );
system.addForce(amoebaStretchBendForce);
Context context(system, integrator, Platform::getPlatformByName( "CUDA"));
std::vector<Vec3> positions(numberOfParticles);
positions[0] = Vec3( 0.262660000E+02, 0.254130000E+02, 0.284200000E+01 );
positions[1] = Vec3( 0.273400000E+02, 0.244300000E+02, 0.261400000E+01 );
positions[2] = Vec3( 0.269573220E+02, 0.236108860E+02, 0.216376800E+01 );
context.setPositions(positions);
compareWithExpectedForceAndEnergy( context, *amoebaStretchBendForce, TOL, "testOneStretchBend", log );
}
int main( int numberOfArguments, char* argv[] ) {
try {
std::cout << "TestCudaAmoebaStretchBendForce running test..." << std::endl;
registerAmoebaCudaKernelFactories();
FILE* log = NULL;
testOneStretchBend( log );
} catch(const std::exception& e) {
std::cout << "exception: " << e.what() << std::endl;
std::cout << "FAIL - ERROR. Test failed." << std::endl;
return 1;
}
std::cout << "Done" << std::endl;
return 0;
}
This source diff could not be displayed because it is too large. You can view the blob instead.
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