Unverified Commit 1e42b8be authored by Peter Eastman's avatar Peter Eastman Committed by GitHub
Browse files

Replaced several AMOEBA bonded forces with custom forces (#3046)

* Replaced several AMOEBA bonded forces with custom forces

* Deleted obsolete AMOEBA forces

* Replaced AmoebaPiTorsionForce with custom force
parent 014797f4
/* -------------------------------------------------------------------------- *
* 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-2016 Stanford University and the Authors. *
* Authors: *
* 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. *
* -------------------------------------------------------------------------- */
#include "openmm/Force.h"
#include "openmm/OpenMMException.h"
#include "openmm/AmoebaOutOfPlaneBendForce.h"
#include "openmm/internal/AmoebaOutOfPlaneBendForceImpl.h"
using namespace OpenMM;
AmoebaOutOfPlaneBendForce::AmoebaOutOfPlaneBendForce() : usePeriodic(false) {
_globalCubicK = -0.1400000E-01;
_globalQuarticK = 0.5600000E-04;
_globalPenticK = -0.7000000E-06;
_globalSexticK = 0.2200000E-07;
}
double AmoebaOutOfPlaneBendForce::getAmoebaGlobalOutOfPlaneBendCubic() const {
return _globalCubicK;
}
void AmoebaOutOfPlaneBendForce::setAmoebaGlobalOutOfPlaneBendCubic(double cubicK) {
_globalCubicK = cubicK;
}
double AmoebaOutOfPlaneBendForce::getAmoebaGlobalOutOfPlaneBendQuartic() const {
return _globalQuarticK;
}
void AmoebaOutOfPlaneBendForce::setAmoebaGlobalOutOfPlaneBendQuartic(double quarticK) {
_globalQuarticK = quarticK;
}
double AmoebaOutOfPlaneBendForce::getAmoebaGlobalOutOfPlaneBendPentic() const {
return _globalPenticK;
}
void AmoebaOutOfPlaneBendForce::setAmoebaGlobalOutOfPlaneBendPentic(double penticK) {
_globalPenticK = penticK;
}
double AmoebaOutOfPlaneBendForce::getAmoebaGlobalOutOfPlaneBendSextic() const {
return _globalSexticK;
}
void AmoebaOutOfPlaneBendForce::setAmoebaGlobalOutOfPlaneBendSextic(double sexticK) {
_globalSexticK = sexticK;
}
int AmoebaOutOfPlaneBendForce::addOutOfPlaneBend(int particle1, int particle2, int particle3, int particle4, double k) {
outOfPlaneBends.push_back(OutOfPlaneBendInfo(particle1, particle2, particle3, particle4, k));
return outOfPlaneBends.size()-1;
}
void AmoebaOutOfPlaneBendForce::getOutOfPlaneBendParameters(int index, int& particle1, int& particle2, int& particle3, int& particle4,
double& k) const {
particle1 = outOfPlaneBends[index].particle1;
particle2 = outOfPlaneBends[index].particle2;
particle3 = outOfPlaneBends[index].particle3;
particle4 = outOfPlaneBends[index].particle4;
k = outOfPlaneBends[index].k;
}
void AmoebaOutOfPlaneBendForce::setOutOfPlaneBendParameters(int index, int particle1, int particle2, int particle3, int particle4,
double k) {
outOfPlaneBends[index].particle1 = particle1;
outOfPlaneBends[index].particle2 = particle2;
outOfPlaneBends[index].particle3 = particle3;
outOfPlaneBends[index].particle4 = particle4;
outOfPlaneBends[index].k = k;
}
ForceImpl* AmoebaOutOfPlaneBendForce::createImpl() const {
return new AmoebaOutOfPlaneBendForceImpl(*this);
}
void AmoebaOutOfPlaneBendForce::updateParametersInContext(Context& context) {
dynamic_cast<AmoebaOutOfPlaneBendForceImpl&>(getImplInContext(context)).updateParametersInContext(getContextImpl(context));
}
void AmoebaOutOfPlaneBendForce::setUsesPeriodicBoundaryConditions(bool periodic) {
usePeriodic = periodic;
}
bool AmoebaOutOfPlaneBendForce::usesPeriodicBoundaryConditions() const {
return usePeriodic;
}
/* -------------------------------------------------------------------------- *
* 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: *
* 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. *
* -------------------------------------------------------------------------- */
#include "openmm/internal/ContextImpl.h"
#include "openmm/internal/AmoebaOutOfPlaneBendForceImpl.h"
#include "openmm/amoebaKernels.h"
using namespace OpenMM;
using std::pair;
using std::vector;
using std::set;
AmoebaOutOfPlaneBendForceImpl::AmoebaOutOfPlaneBendForceImpl(const AmoebaOutOfPlaneBendForce& owner) : owner(owner) {
}
AmoebaOutOfPlaneBendForceImpl::~AmoebaOutOfPlaneBendForceImpl() {
}
void AmoebaOutOfPlaneBendForceImpl::initialize(ContextImpl& context) {
kernel = context.getPlatform().createKernel(CalcAmoebaOutOfPlaneBendForceKernel::Name(), context);
kernel.getAs<CalcAmoebaOutOfPlaneBendForceKernel>().initialize(context.getSystem(), owner);
}
double AmoebaOutOfPlaneBendForceImpl::calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy, int groups) {
if ((groups&(1<<owner.getForceGroup())) != 0)
return kernel.getAs<CalcAmoebaOutOfPlaneBendForceKernel>().execute(context, includeForces, includeEnergy);
return 0.0;
}
std::vector<std::string> AmoebaOutOfPlaneBendForceImpl::getKernelNames() {
std::vector<std::string> names;
names.push_back(CalcAmoebaOutOfPlaneBendForceKernel::Name());
return names;
}
void AmoebaOutOfPlaneBendForceImpl::updateParametersInContext(ContextImpl& context) {
kernel.getAs<CalcAmoebaOutOfPlaneBendForceKernel>().copyParametersToContext(context, owner);
context.systemChanged();
}
/* -------------------------------------------------------------------------- *
* 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-2016 Stanford University and the Authors. *
* Authors: *
* 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. *
* -------------------------------------------------------------------------- */
#include "openmm/Force.h"
#include "openmm/OpenMMException.h"
#include "openmm/AmoebaPiTorsionForce.h"
#include "openmm/internal/AmoebaPiTorsionForceImpl.h"
using namespace OpenMM;
AmoebaPiTorsionForce::AmoebaPiTorsionForce() : usePeriodic(false) {
}
int AmoebaPiTorsionForce::addPiTorsion(int particle1, int particle2, int particle3, int particle4, int particle5, int particle6, double k) {
piTorsions.push_back(PiTorsionInfo(particle1, particle2, particle3, particle4, particle5, particle6, k));
return piTorsions.size()-1;
}
void AmoebaPiTorsionForce::getPiTorsionParameters(int index, int& particle1, int& particle2, int& particle3, int& particle4, int& particle5, int& particle6, double& k) const {
particle1 = piTorsions[index].particle1;
particle2 = piTorsions[index].particle2;
particle3 = piTorsions[index].particle3;
particle4 = piTorsions[index].particle4;
particle5 = piTorsions[index].particle5;
particle6 = piTorsions[index].particle6;
k = piTorsions[index].k;
}
void AmoebaPiTorsionForce::setPiTorsionParameters(int index, int particle1, int particle2, int particle3, int particle4, int particle5, int particle6, double k) {
piTorsions[index].particle1 = particle1;
piTorsions[index].particle2 = particle2;
piTorsions[index].particle3 = particle3;
piTorsions[index].particle4 = particle4;
piTorsions[index].particle5 = particle5;
piTorsions[index].particle6 = particle6;
piTorsions[index].k = k;
}
ForceImpl* AmoebaPiTorsionForce::createImpl() const {
return new AmoebaPiTorsionForceImpl(*this);
}
void AmoebaPiTorsionForce::updateParametersInContext(Context& context) {
dynamic_cast<AmoebaPiTorsionForceImpl&>(getImplInContext(context)).updateParametersInContext(getContextImpl(context));
}
void AmoebaPiTorsionForce::setUsesPeriodicBoundaryConditions(bool periodic) {
usePeriodic = periodic;
}
bool AmoebaPiTorsionForce::usesPeriodicBoundaryConditions() const {
return usePeriodic;
}
/* -------------------------------------------------------------------------- *
* 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: *
* 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. *
* -------------------------------------------------------------------------- */
#include "openmm/internal/ContextImpl.h"
#include "openmm/internal/AmoebaPiTorsionForceImpl.h"
#include "openmm/amoebaKernels.h"
using namespace OpenMM;
using std::pair;
using std::vector;
using std::set;
AmoebaPiTorsionForceImpl::AmoebaPiTorsionForceImpl(const AmoebaPiTorsionForce& owner) : owner(owner) {
}
AmoebaPiTorsionForceImpl::~AmoebaPiTorsionForceImpl() {
}
void AmoebaPiTorsionForceImpl::initialize(ContextImpl& context) {
kernel = context.getPlatform().createKernel(CalcAmoebaPiTorsionForceKernel::Name(), context);
kernel.getAs<CalcAmoebaPiTorsionForceKernel>().initialize(context.getSystem(), owner);
}
double AmoebaPiTorsionForceImpl::calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy, int groups) {
if ((groups&(1<<owner.getForceGroup())) != 0)
return kernel.getAs<CalcAmoebaPiTorsionForceKernel>().execute(context, includeForces, includeEnergy);
return 0.0;
}
std::vector<std::string> AmoebaPiTorsionForceImpl::getKernelNames() {
std::vector<std::string> names;
names.push_back(CalcAmoebaPiTorsionForceKernel::Name());
return names;
}
void AmoebaPiTorsionForceImpl::updateParametersInContext(ContextImpl& context) {
kernel.getAs<CalcAmoebaPiTorsionForceKernel>().copyParametersToContext(context, owner);
context.systemChanged();
}
/* -------------------------------------------------------------------------- *
* 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-2016 Stanford University and the Authors. *
* Authors: *
* 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. *
* -------------------------------------------------------------------------- */
#include "openmm/Force.h"
#include "openmm/OpenMMException.h"
#include "openmm/AmoebaStretchBendForce.h"
#include "openmm/internal/AmoebaStretchBendForceImpl.h"
using namespace OpenMM;
AmoebaStretchBendForce::AmoebaStretchBendForce() : usePeriodic(false) {
}
int AmoebaStretchBendForce::addStretchBend(int particle1, int particle2, int particle3,
double lengthAB, double lengthCB, double angle, double k1, double k2) {
stretchBends.push_back(StretchBendInfo(particle1, particle2, particle3, lengthAB, lengthCB, angle, k1, k2));
return stretchBends.size()-1;
}
void AmoebaStretchBendForce::getStretchBendParameters(int index, int& particle1, int& particle2, int& particle3,
double& lengthAB, double& lengthCB, double& angle, double& k1, double& k2) const {
particle1 = stretchBends[index].particle1;
particle2 = stretchBends[index].particle2;
particle3 = stretchBends[index].particle3;
lengthAB = stretchBends[index].lengthAB;
lengthCB = stretchBends[index].lengthCB;
angle = stretchBends[index].angle;
k1 = stretchBends[index].k1;
k2 = stretchBends[index].k2;
}
void AmoebaStretchBendForce::setStretchBendParameters(int index, int particle1, int particle2, int particle3,
double lengthAB, double lengthCB, double angle, double k1, double k2) {
stretchBends[index].particle1 = particle1;
stretchBends[index].particle2 = particle2;
stretchBends[index].particle3 = particle3;
stretchBends[index].lengthAB = lengthAB;
stretchBends[index].lengthCB = lengthCB;
stretchBends[index].angle = angle;
stretchBends[index].k1 = k1;
stretchBends[index].k2 = k2;
}
ForceImpl* AmoebaStretchBendForce::createImpl() const {
return new AmoebaStretchBendForceImpl(*this);
}
void AmoebaStretchBendForce::updateParametersInContext(Context& context) {
dynamic_cast<AmoebaStretchBendForceImpl&>(getImplInContext(context)).updateParametersInContext(getContextImpl(context));
}
void AmoebaStretchBendForce::setUsesPeriodicBoundaryConditions(bool periodic) {
usePeriodic = periodic;
}
bool AmoebaStretchBendForce::usesPeriodicBoundaryConditions() const {
return usePeriodic;
}
/* -------------------------------------------------------------------------- *
* 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: *
* 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. *
* -------------------------------------------------------------------------- */
#include "openmm/internal/ContextImpl.h"
#include "openmm/internal/AmoebaStretchBendForceImpl.h"
#include "openmm/amoebaKernels.h"
using namespace OpenMM;
using std::pair;
using std::vector;
using std::set;
AmoebaStretchBendForceImpl::AmoebaStretchBendForceImpl(const AmoebaStretchBendForce& owner) : owner(owner) {
}
AmoebaStretchBendForceImpl::~AmoebaStretchBendForceImpl() {
}
void AmoebaStretchBendForceImpl::initialize(ContextImpl& context) {
kernel = context.getPlatform().createKernel(CalcAmoebaStretchBendForceKernel::Name(), context);
kernel.getAs<CalcAmoebaStretchBendForceKernel>().initialize(context.getSystem(), owner);
}
double AmoebaStretchBendForceImpl::calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy, int groups) {
if ((groups&(1<<owner.getForceGroup())) != 0)
return kernel.getAs<CalcAmoebaStretchBendForceKernel>().execute(context, includeForces, includeEnergy);
return 0.0;
}
std::vector<std::string> AmoebaStretchBendForceImpl::getKernelNames() {
std::vector<std::string> names;
names.push_back(CalcAmoebaStretchBendForceKernel::Name());
return names;
}
void AmoebaStretchBendForceImpl::updateParametersInContext(ContextImpl& context) {
kernel.getAs<CalcAmoebaStretchBendForceKernel>().copyParametersToContext(context, owner);
context.systemChanged();
}
......@@ -48,12 +48,6 @@ extern "C" OPENMM_EXPORT void registerKernelFactories() {
try {
Platform& platform = Platform::getPlatformByName("CUDA");
AmoebaCudaKernelFactory* factory = new AmoebaCudaKernelFactory();
platform.registerKernelFactory(CalcAmoebaBondForceKernel::Name(), factory);
platform.registerKernelFactory(CalcAmoebaAngleForceKernel::Name(), factory);
platform.registerKernelFactory(CalcAmoebaInPlaneAngleForceKernel::Name(), factory);
platform.registerKernelFactory(CalcAmoebaPiTorsionForceKernel::Name(), factory);
platform.registerKernelFactory(CalcAmoebaStretchBendForceKernel::Name(), factory);
platform.registerKernelFactory(CalcAmoebaOutOfPlaneBendForceKernel::Name(), factory);
platform.registerKernelFactory(CalcAmoebaTorsionTorsionForceKernel::Name(), factory);
platform.registerKernelFactory(CalcAmoebaMultipoleForceKernel::Name(), factory);
platform.registerKernelFactory(CalcAmoebaGeneralizedKirkwoodForceKernel::Name(), factory);
......@@ -80,24 +74,6 @@ KernelImpl* AmoebaCudaKernelFactory::createKernelImpl(std::string name, const Pl
CudaPlatform::PlatformData& data = *static_cast<CudaPlatform::PlatformData*>(context.getPlatformData());
CudaContext& cu = *data.contexts[0];
if (name == CalcAmoebaBondForceKernel::Name())
return new CudaCalcAmoebaBondForceKernel(name, platform, cu, context.getSystem());
if (name == CalcAmoebaAngleForceKernel::Name())
return new CudaCalcAmoebaAngleForceKernel(name, platform, cu, context.getSystem());
if (name == CalcAmoebaInPlaneAngleForceKernel::Name())
return new CudaCalcAmoebaInPlaneAngleForceKernel(name, platform, cu, context.getSystem());
if (name == CalcAmoebaPiTorsionForceKernel::Name())
return new CudaCalcAmoebaPiTorsionForceKernel(name, platform, cu, context.getSystem());
if (name == CalcAmoebaStretchBendForceKernel::Name())
return new CudaCalcAmoebaStretchBendForceKernel(name, platform, cu, context.getSystem());
if (name == CalcAmoebaOutOfPlaneBendForceKernel::Name())
return new CudaCalcAmoebaOutOfPlaneBendForceKernel(name, platform, cu, context.getSystem());
if (name == CalcAmoebaTorsionTorsionForceKernel::Name())
return new CudaCalcAmoebaTorsionTorsionForceKernel(name, platform, cu, context.getSystem());
......
......@@ -40,232 +40,6 @@ namespace OpenMM {
class CudaCalcAmoebaGeneralizedKirkwoodForceKernel;
/**
* This kernel is invoked by AmoebaBondForce to calculate the forces acting on the system and the energy of the system.
*/
class CudaCalcAmoebaBondForceKernel : public CalcAmoebaBondForceKernel {
public:
CudaCalcAmoebaBondForceKernel(const std::string& name,
const Platform& platform,
CudaContext& cu,
const System& system);
/**
* Initialize the kernel.
*
* @param system the System this kernel will be applied to
* @param force the AmoebaBondForce this kernel will be used for
*/
void initialize(const System& system, const AmoebaBondForce& force);
/**
* 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 execute(ContextImpl& context, bool includeForces, bool includeEnergy);
/**
* Copy changed parameters over to a context.
*
* @param context the context to copy parameters to
* @param force the AmoebaBondForce to copy the parameters from
*/
void copyParametersToContext(ContextImpl& context, const AmoebaBondForce& force);
private:
class ForceInfo;
int numBonds;
CudaContext& cu;
const System& system;
CudaArray params;
};
/**
* This kernel is invoked by AmoebaAngleForce to calculate the forces acting on the system and the energy of the system.
*/
class CudaCalcAmoebaAngleForceKernel : public CalcAmoebaAngleForceKernel {
public:
CudaCalcAmoebaAngleForceKernel(const std::string& name, const Platform& platform, CudaContext& cu, const System& system);
/**
* Initialize the kernel.
*
* @param system the System this kernel will be applied to
* @param force the AmoebaAngleForce this kernel will be used for
*/
void initialize(const System& system, const AmoebaAngleForce& force);
/**
* 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 execute(ContextImpl& context, bool includeForces, bool includeEnergy);
/**
* Copy changed parameters over to a context.
*
* @param context the context to copy parameters to
* @param force the AmoebaAngleForce to copy the parameters from
*/
void copyParametersToContext(ContextImpl& context, const AmoebaAngleForce& force);
private:
class ForceInfo;
int numAngles;
CudaContext& cu;
const System& system;
CudaArray params;
};
/**
* This kernel is invoked by AmoebaInPlaneAngleForce to calculate the forces acting on the system and the energy of the system.
*/
class CudaCalcAmoebaInPlaneAngleForceKernel : public CalcAmoebaInPlaneAngleForceKernel {
public:
CudaCalcAmoebaInPlaneAngleForceKernel(const std::string& name, const Platform& platform, CudaContext& cu, const System& system);
/**
* Initialize the kernel.
*
* @param system the System this kernel will be applied to
* @param force the AmoebaInPlaneAngleForce this kernel will be used for
*/
void initialize(const System& system, const AmoebaInPlaneAngleForce& force);
/**
* 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 execute(ContextImpl& context, bool includeForces, bool includeEnergy);
/**
* Copy changed parameters over to a context.
*
* @param context the context to copy parameters to
* @param force the AmoebaInPlaneAngleForce to copy the parameters from
*/
void copyParametersToContext(ContextImpl& context, const AmoebaInPlaneAngleForce& force);
private:
class ForceInfo;
int numAngles;
CudaContext& cu;
const System& system;
CudaArray params;
};
/**
* This kernel is invoked by AmoebaPiTorsionForce to calculate the forces acting on the system and the energy of the system.
*/
class CudaCalcAmoebaPiTorsionForceKernel : public CalcAmoebaPiTorsionForceKernel {
public:
CudaCalcAmoebaPiTorsionForceKernel(const std::string& name, const Platform& platform, CudaContext& cu, const System& system);
/**
* Initialize the kernel.
*
* @param system the System this kernel will be applied to
* @param force the AmoebaPiTorsionForce this kernel will be used for
*/
void initialize(const System& system, const AmoebaPiTorsionForce& force);
/**
* 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 execute(ContextImpl& context, bool includeForces, bool includeEnergy);
/**
* Copy changed parameters over to a context.
*
* @param context the context to copy parameters to
* @param force the AmoebaPiTorsionForce to copy the parameters from
*/
void copyParametersToContext(ContextImpl& context, const AmoebaPiTorsionForce& force);
private:
class ForceInfo;
int numPiTorsions;
CudaContext& cu;
const System& system;
CudaArray params;
};
/**
* This kernel is invoked by AmoebaStretchBendForce to calculate the forces acting on the system and the energy of the system.
*/
class CudaCalcAmoebaStretchBendForceKernel : public CalcAmoebaStretchBendForceKernel {
public:
CudaCalcAmoebaStretchBendForceKernel(const std::string& name, const Platform& platform, CudaContext& cu, const System& system);
/**
* Initialize the kernel.
*
* @param system the System this kernel will be applied to
* @param force the AmoebaStretchBendForce this kernel will be used for
*/
void initialize(const System& system, const AmoebaStretchBendForce& force);
/**
* 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 execute(ContextImpl& context, bool includeForces, bool includeEnergy);
/**
* Copy changed parameters over to a context.
*
* @param context the context to copy parameters to
* @param force the AmoebaStretchBendForce to copy the parameters from
*/
void copyParametersToContext(ContextImpl& context, const AmoebaStretchBendForce& force);
private:
class ForceInfo;
int numStretchBends;
CudaContext& cu;
const System& system;
CudaArray params1; // Equilibrium values
CudaArray params2; // force constants
};
/**
* This kernel is invoked by AmoebaOutOfPlaneBendForce to calculate the forces acting on the system and the energy of the system.
*/
class CudaCalcAmoebaOutOfPlaneBendForceKernel : public CalcAmoebaOutOfPlaneBendForceKernel {
public:
CudaCalcAmoebaOutOfPlaneBendForceKernel(const std::string& name, const Platform& platform, CudaContext& cu, const System& system);
/**
* Initialize the kernel.
*
* @param system the System this kernel will be applied to
* @param force the AmoebaOutOfPlaneBendForce this kernel will be used for
*/
void initialize(const System& system, const AmoebaOutOfPlaneBendForce& force);
/**
* 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 execute(ContextImpl& context, bool includeForces, bool includeEnergy);
/**
* Copy changed parameters over to a context.
*
* @param context the context to copy parameters to
* @param force the AmoebaOutOfPlaneBendForce to copy the parameters from
*/
void copyParametersToContext(ContextImpl& context, const AmoebaOutOfPlaneBendForce& force);
private:
class ForceInfo;
int numOutOfPlaneBends;
CudaContext& cu;
const System& system;
CudaArray params;
};
/**
* This kernel is invoked by AmoebaTorsionTorsionForce to calculate the forces acting on the system and the energy of the system.
*/
......
float2 angleParams = PARAMS[index];
real deltaIdeal = theta*RAD_TO_DEG-angleParams.x;
real deltaIdeal2 = deltaIdeal*deltaIdeal;
real deltaIdeal3 = deltaIdeal*deltaIdeal2;
real deltaIdeal4 = deltaIdeal2*deltaIdeal2;
energy += angleParams.y*deltaIdeal2*(1.0f + CUBIC_K*deltaIdeal + QUARTIC_K*deltaIdeal2 + PENTIC_K*deltaIdeal3 + SEXTIC_K*deltaIdeal4);
real dEdAngle = angleParams.y*deltaIdeal*(2.0f + 3.0f*CUBIC_K*deltaIdeal + 4.0f*QUARTIC_K*deltaIdeal2 + 5.0f*PENTIC_K*deltaIdeal3 + 6.0f*SEXTIC_K*deltaIdeal4);
dEdAngle *= RAD_TO_DEG;
\ No newline at end of file
float2 bondParams = PARAMS[index];
real deltaIdeal = r-bondParams.x;
real deltaIdeal2 = deltaIdeal*deltaIdeal;
energy += bondParams.y*deltaIdeal2*(1.0f + CUBIC_K*deltaIdeal + QUARTIC_K*deltaIdeal2);
real dEdR = 2.0f*bondParams.y*deltaIdeal*(1.0f + 1.5f*CUBIC_K*deltaIdeal + 2.0f*QUARTIC_K*deltaIdeal2);
float2 angleParams = PARAMS[index];
real3 ad = make_real3(pos1.x-pos4.x, pos1.y-pos4.y, pos1.z-pos4.z);
real3 bd = make_real3(pos2.x-pos4.x, pos2.y-pos4.y, pos2.z-pos4.z);
real3 cd = make_real3(pos3.x-pos4.x, pos3.y-pos4.y, pos3.z-pos4.z);
#if APPLY_PERIODIC
APPLY_PERIODIC_TO_DELTA(ad)
APPLY_PERIODIC_TO_DELTA(bd)
APPLY_PERIODIC_TO_DELTA(cd)
#endif
real xt = ad.y*cd.z - ad.z*cd.y;
real yt = ad.z*cd.x - ad.x*cd.z;
real zt = ad.x*cd.y - ad.y*cd.x;
real rt2 = xt*xt + yt*yt + zt*zt;
real delta = -(xt*bd.x + yt*bd.y + zt*bd.z) / rt2;
real xip = pos2.x + xt*delta;
real yip = pos2.y + yt*delta;
real zip = pos2.z + zt*delta;
real3 ap = make_real3(pos1.x-xip, pos1.y-yip, pos1.z-zip);
real3 cp = make_real3(pos3.x-xip, pos3.y-yip, pos3.z-zip);
#if APPLY_PERIODIC
APPLY_PERIODIC_TO_DELTA(ap)
APPLY_PERIODIC_TO_DELTA(cp)
#endif
real rap2 = ap.x*ap.x + ap.y*ap.y + ap.z*ap.z;
real rcp2 = cp.x*cp.x + cp.y*cp.y + cp.z*cp.z;
real xm = cp.y*ap.z - cp.z*ap.y;
real ym = cp.z*ap.x - cp.x*ap.z;
real zm = cp.x*ap.y - cp.y*ap.x;
real rm = max(SQRT(xm*xm + ym*ym + zm*zm), (real) 1e-6f);
real dotp = ap.x*cp.x + ap.y*cp.y + ap.z*cp.z;
real product = SQRT(rap2*rcp2);
real cosine = (product > 0 ? (dotp/product) : 0);
cosine = max(min(cosine, (real) 1), (real) -1);
real angle;
if (cosine > 0.99f || cosine < -0.99f) {
real3 cross_prod = cross(ap, cp);
angle = ASIN(SQRT(dot(cross_prod, cross_prod)/(rap2*rcp2)))*RAD_TO_DEG;
if (cosine < 0.0f)
angle = 180-angle;
}
else
angle = ACOS(cosine)*RAD_TO_DEG;
// if product == 0, set force/energy to 0
real deltaIdeal = (product > 0 ? (angle - angleParams.x) : 0);
real deltaIdeal2 = deltaIdeal*deltaIdeal;
real deltaIdeal3 = deltaIdeal*deltaIdeal2;
real deltaIdeal4 = deltaIdeal2*deltaIdeal2;
energy += angleParams.y*deltaIdeal2*(1.0f + CUBIC_K*deltaIdeal + QUARTIC_K*deltaIdeal2 + PENTIC_K*deltaIdeal3 + SEXTIC_K*deltaIdeal4);
real dEdAngle = angleParams.y*deltaIdeal*(2.0f + 3.0f*CUBIC_K*deltaIdeal + 4.0f*QUARTIC_K*deltaIdeal2 + 5.0f*PENTIC_K*deltaIdeal3 + 6.0f*SEXTIC_K*deltaIdeal4);
dEdAngle *= RAD_TO_DEG;
real terma = -dEdAngle/(rap2*rm);
real termc = dEdAngle/(rcp2*rm);
real dedxia = terma * (ap.y*zm-ap.z*ym);
real dedyia = terma * (ap.z*xm-ap.x*zm);
real dedzia = terma * (ap.x*ym-ap.y*xm);
real dedxic = termc * (cp.y*zm-cp.z*ym);
real dedyic = termc * (cp.z*xm-cp.x*zm);
real dedzic = termc * (cp.x*ym-cp.y*xm);
real dedxip = -dedxia - dedxic;
real dedyip = -dedyia - dedyic;
real dedzip = -dedzia - dedzic;
real delta2 = 2.0f*delta;
real ptrt2 = (dedxip*xt + dedyip*yt + dedzip*zt) / rt2;
real term = (cd.z*bd.y-cd.y*bd.z) + delta2*(yt*cd.z-zt*cd.y);
real dpdxia = delta*(cd.y*dedzip-cd.z*dedyip) + term*ptrt2;
term = (cd.x*bd.z-cd.z*bd.x) + delta2*(zt*cd.x-xt*cd.z);
real dpdyia = delta*(cd.z*dedxip-cd.x*dedzip) + term*ptrt2;
term = (cd.y*bd.x-cd.x*bd.y) + delta2*(xt*cd.y-yt*cd.x);
real dpdzia = delta*(cd.x*dedyip-cd.y*dedxip) + term*ptrt2;
term = (ad.y*bd.z-ad.z*bd.y) + delta2*(zt*ad.y-yt*ad.z);
real dpdxic = delta*(ad.z*dedyip-ad.y*dedzip) + term*ptrt2;
term = (ad.z*bd.x-ad.x*bd.z) + delta2*(xt*ad.z-zt*ad.x);
real dpdyic = delta*(ad.x*dedzip-ad.z*dedxip) + term*ptrt2;
term = (ad.x*bd.y-ad.y*bd.x) + delta2*(yt*ad.x-xt*ad.y);
real dpdzic = delta*(ad.y*dedxip-ad.x*dedyip) + term*ptrt2;
dedxia = dedxia + dpdxia;
dedyia = dedyia + dpdyia;
dedzia = dedzia + dpdzia;
real dedxib = dedxip;
real dedyib = dedyip;
real dedzib = dedzip;
dedxic = dedxic + dpdxic;
dedyic = dedyic + dpdyic;
dedzic = dedzic + dpdzic;
real dedxid = -dedxia - dedxib - dedxic;
real dedyid = -dedyia - dedyib - dedyic;
real dedzid = -dedzia - dedzib - dedzic;
real3 force1 = make_real3(-dedxia, -dedyia, -dedzia);
real3 force2 = make_real3(-dedxib, -dedyib, -dedzib);
real3 force3 = make_real3(-dedxic, -dedyic, -dedzic);
real3 force4 = make_real3(-dedxid, -dedyid, -dedzid);
// compute the value of the bond angle
real3 ab = make_real3(pos1.x-pos2.x, pos1.y-pos2.y, pos1.z-pos2.z);
real3 cb = make_real3(pos3.x-pos2.x, pos3.y-pos2.y, pos3.z-pos2.z);
real3 db = make_real3(pos4.x-pos2.x, pos4.y-pos2.y, pos4.z-pos2.z);
real3 ad = make_real3(pos1.x-pos4.x, pos1.y-pos4.y, pos1.z-pos4.z);
real3 cd = make_real3(pos3.x-pos4.x, pos3.y-pos4.y, pos3.z-pos4.z);
#if APPLY_PERIODIC
APPLY_PERIODIC_TO_DELTA(ab)
APPLY_PERIODIC_TO_DELTA(cb)
APPLY_PERIODIC_TO_DELTA(db)
APPLY_PERIODIC_TO_DELTA(ad)
APPLY_PERIODIC_TO_DELTA(cd)
#endif
real rdb2 = db.x*db.x + db.y*db.y + db.z*db.z;
real rad2 = ad.x*ad.x + ad.y*ad.y + ad.z*ad.z;
real rcd2 = cd.x*cd.x + cd.y*cd.y + cd.z*cd.z;
real ee = ab.x*(cb.y*db.z-cb.z*db.y) + ab.y*(cb.z*db.x-cb.x*db.z) + ab.z*(cb.x*db.y-cb.y*db.x);
real dot = ad.x*cd.x + ad.y*cd.y + ad.z*cd.z;
real cc = rad2*rcd2 - dot*dot;
real bkk2 = (cc != 0 ? (ee*ee)/(cc) : (real) 0);
bkk2 = rdb2 - bkk2;
real adXcd_0 = ad.y*cd.z - ad.z*cd.y;
real adXcd_1 = ad.z*cd.x - ad.x*cd.z;
real adXcd_2 = ad.x*cd.y - ad.y*cd.x;
real adXcd_nrm2 = adXcd_0*adXcd_0 + adXcd_1*adXcd_1 + adXcd_2*adXcd_2;
real adXcd_dot_db = db.x*adXcd_0 + db.y*adXcd_1 + db.z*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 = (ad.x*rcd2-cd.x*dot) * term;
real dccdyia = (ad.y*rcd2-cd.y*dot) * term;
real dccdzia = (ad.z*rcd2-cd.z*dot) * term;
real dccdxic = (cd.x*rad2-ad.x*dot) * term;
real dccdyic = (cd.y*rad2-ad.y*dot) * term;
real dccdzic = (cd.z*rad2-ad.z*dot) * term;
real dccdxid = -dccdxia - dccdxic;
real dccdyid = -dccdyia - dccdyic;
real dccdzid = -dccdzia - dccdzic;
term = ee / rdb2;
real deedxia = db.y*cb.z - db.z*cb.y;
real deedyia = db.z*cb.x - db.x*cb.z;
real deedzia = db.x*cb.y - db.y*cb.x;
real deedxic = ab.y*db.z - ab.z*db.y;
real deedyic = ab.z*db.x - ab.x*db.z;
real deedzic = ab.x*db.y - ab.y*db.x;
real deedxid = cb.y*ab.z - cb.z*ab.y + db.x*term;
real deedyid = cb.z*ab.x - cb.x*ab.z + db.y*term;
real deedzid = cb.x*ab.y - cb.y*ab.x + db.z*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 pi-orbital torsion angle
real3 ad = make_real3(pos1.x-pos4.x, pos1.y-pos4.y, pos1.z-pos4.z);
real3 bd = make_real3(pos2.x-pos4.x, pos2.y-pos4.y, pos2.z-pos4.z);
real3 ec = make_real3(pos5.x-pos3.x, pos5.y-pos3.y, pos5.z-pos3.z);
real3 gc = make_real3(pos6.x-pos3.x, pos6.y-pos3.y, pos6.z-pos3.z);
#if APPLY_PERIODIC
APPLY_PERIODIC_TO_DELTA(ad)
APPLY_PERIODIC_TO_DELTA(bd)
APPLY_PERIODIC_TO_DELTA(ec)
APPLY_PERIODIC_TO_DELTA(gc)
#endif
real xip = ad.y*bd.z - bd.y*ad.z + pos3.x;
real yip = ad.z*bd.x - bd.z*ad.x + pos3.y;
real zip = ad.x*bd.y - bd.x*ad.y + pos3.z;
real xiq = ec.y*gc.z - gc.y*ec.z + pos4.x;
real yiq = ec.z*gc.x - gc.z*ec.x + pos4.y;
real ziq = ec.x*gc.y - gc.x*ec.y + pos4.z;
real xcp = pos3.x - xip;
real ycp = pos3.y - yip;
real zcp = pos3.z - zip;
real xdc = pos4.x - pos3.x;
real ydc = pos4.y - pos3.y;
real zdc = pos4.z - pos3.z;
real xqd = xiq - pos4.x;
real yqd = yiq - pos4.y;
real zqd = ziq - pos4.z;
real xt = ycp*zdc - ydc*zcp;
real yt = zcp*xdc - zdc*xcp;
real zt = xcp*ydc - xdc*ycp;
real xu = ydc*zqd - yqd*zdc;
real yu = zdc*xqd - zqd*xdc;
real zu = xdc*yqd - xqd*ydc;
real xtu = yt*zu - yu*zt;
real ytu = zt*xu - zu*xt;
real ztu = xt*yu - xu*yt;
real rt2 = xt*xt + yt*yt + zt*zt;
real ru2 = xu*xu + yu*yu + zu*zu;
real rtru = sqrtf(rt2 * ru2);
real rdc = sqrtf(xdc*xdc + ydc*ydc + zdc*zdc);
real cosine = rtru > 0.0f ? (xt*xu + yt*yu + zt*zu) / rtru : 0.0f;
real sine = (rtru*rdc) > 0.0f ? (xdc*xtu + ydc*ytu + zdc*ztu) / (rdc*rtru) : 0.0f;
// zero energy/force if rtru == 0
float v2 = PARAMS[index];
v2 = (rtru > 0 ? v2 : 0.0f);
// compute the multiple angle trigonometry and the phase terms
real cosine2 = cosine*cosine - sine*sine;
real sine2 = 2.0f * cosine * sine;
real phi2 = 1.0f - cosine2;
real dphi2 = 2.0f * sine2;
// calculate pi-orbital torsion energy and master chain rule term
energy += v2 * phi2;
real dedphi = v2 * dphi2;
// chain rule terms for first derivative components
real xdp = pos4.x - xip;
real ydp = pos4.y - yip;
real zdp = pos4.z - zip;
real xqc = xiq - pos3.x;
real yqc = yiq - pos3.y;
real zqc = ziq - pos3.z;
real dedxt = dedphi * (yt*zdc - ydc*zt) / (rt2*rdc);
real dedyt = dedphi * (zt*xdc - zdc*xt) / (rt2*rdc);
real dedzt = dedphi * (xt*ydc - xdc*yt) / (rt2*rdc);
real dedxu = -dedphi * (yu*zdc - ydc*zu) / (ru2*rdc);
real dedyu = -dedphi * (zu*xdc - zdc*xu) / (ru2*rdc);
real dedzu = -dedphi * (xu*ydc - xdc*yu) / (ru2*rdc);
// compute first derivative components for pi-orbital angle
real dedxip = zdc*dedyt - ydc*dedzt;
real dedyip = xdc*dedzt - zdc*dedxt;
real dedzip = ydc*dedxt - xdc*dedyt;
real dedxic = ydp*dedzt - zdp*dedyt + zqd*dedyu - yqd*dedzu;
real dedyic = zdp*dedxt - xdp*dedzt + xqd*dedzu - zqd*dedxu;
real dedzic = xdp*dedyt - ydp*dedxt + yqd*dedxu - xqd*dedyu;
real dedxid = zcp*dedyt - ycp*dedzt + yqc*dedzu - zqc*dedyu;
real dedyid = xcp*dedzt - zcp*dedxt + zqc*dedxu - xqc*dedzu;
real dedzid = ycp*dedxt - xcp*dedyt + xqc*dedyu - yqc*dedxu;
real dedxiq = zdc*dedyu - ydc*dedzu;
real dedyiq = xdc*dedzu - zdc*dedxu;
real dedziq = ydc*dedxu - xdc*dedyu;
// compute first derivative components for individual atoms
real dedxia = bd.y*dedzip - bd.z*dedyip;
real dedyia = bd.z*dedxip - bd.x*dedzip;
real dedzia = bd.x*dedyip - bd.y*dedxip;
real dedxib = ad.z*dedyip - ad.y*dedzip;
real dedyib = ad.x*dedzip - ad.z*dedxip;
real dedzib = ad.y*dedxip - ad.x*dedyip;
real dedxie = gc.y*dedziq - gc.z*dedyiq;
real dedyie = gc.z*dedxiq - gc.x*dedziq;
real dedzie = gc.x*dedyiq - gc.y*dedxiq;
real dedxig = ec.z*dedyiq - ec.y*dedziq;
real dedyig = ec.x*dedziq - ec.z*dedxiq;
real dedzig = ec.y*dedxiq - ec.x*dedyiq;
dedxic = dedxic + dedxip - dedxie - dedxig;
dedyic = dedyic + dedyip - dedyie - dedyig;
dedzic = dedzic + dedzip - dedzie - dedzig;
dedxid = dedxid + dedxiq - dedxia - dedxib;
dedyid = dedyid + dedyiq - dedyia - dedyib;
dedzid = dedzid + dedziq - dedzia - dedzib;
real3 force1 = make_real3(-dedxia, -dedyia, -dedzia);
real3 force2 = make_real3(-dedxib, -dedyib, -dedzib);
real3 force3 = make_real3(-dedxic, -dedyic, -dedzic);
real3 force4 = make_real3(-dedxid, -dedyid, -dedzid);
real3 force5 = make_real3(-dedxie, -dedyie, -dedzie);
real3 force6 = make_real3(-dedxig, -dedyig, -dedzig);
\ No newline at end of file
// compute the value of the bond angle
real3 ab = make_real3(pos1.x-pos2.x, pos1.y-pos2.y, pos1.z-pos2.z);
real3 cb = make_real3(pos3.x-pos2.x, pos3.y-pos2.y, pos3.z-pos2.z);
#if APPLY_PERIODIC
APPLY_PERIODIC_TO_DELTA(ab)
APPLY_PERIODIC_TO_DELTA(cb)
#endif
real rab = SQRT(ab.x*ab.x + ab.y*ab.y + ab.z*ab.z);
real rcb = SQRT(cb.x*cb.x + cb.y*cb.y + cb.z*cb.z);
real xp = cb.y*ab.z - cb.z*ab.y;
real yp = cb.z*ab.x - cb.x*ab.z;
real zp = cb.x*ab.y - cb.y*ab.x;
real rp = SQRT(xp*xp + yp*yp + zp*zp);
real dotp = ab.x*cb.x + ab.y*cb.y + ab.z*cb.z;
real cosine = rab*rcb > 0 ? (dotp / (rab*rcb)) : (real) 1;
cosine = (cosine > 1 ? (real) 1 : cosine);
cosine = (cosine < -1 ? -(real) 1 : cosine);
real angle;
if (cosine > 0.99f || cosine < -0.99f) {
// Highly unlikely a stretch-bend angle will be near 0 or 180, but just in case...
real3 cross_prod = cross(make_real3(ab.x, ab.y, ab.z), make_real3(cb.x, cb.y, cb.z));
angle = ASIN(SQRT(dot(cross_prod, cross_prod))/(rab*rcb))*RAD_TO_DEG;
if (cosine < 0.0f)
angle = 180-angle;
}
else
angle = ACOS(cosine)*RAD_TO_DEG;
// find chain rule terms for the bond angle deviation
float3 parameters = PARAMS[index];
float2 force_constants = FORCE_CONSTANTS[index];
real dt = angle - RAD_TO_DEG*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 * (ab.y*zp-ab.z*yp);
real ddtdyia = terma * (ab.z*xp-ab.x*zp);
real ddtdzia = terma * (ab.x*yp-ab.y*xp);
real ddtdxic = termc * (cb.y*zp-cb.z*yp);
real ddtdyic = termc * (cb.z*xp-cb.x*zp);
real ddtdzic = termc * (cb.x*yp-cb.y*xp);
// find chain rule terms for the bond length deviations
real dr1 = (parameters.x > 0 ? (rab - parameters.x) : (real) 0);
terma = (parameters.x > 0 ? RECIP(rab) : (real) 0);
real dr2 = (parameters.y > 0 ? (rcb - parameters.y) : (real) 0);
termc = (parameters.y > 0 ? RECIP(rcb) : (real) 0);
real frc1 = ((rp != 0) ? force_constants.x : (real) 0);
real frc2 = ((rp != 0) ? force_constants.y : (real) 0);
real drkk = dr1*frc1 + dr2*frc2;
real ddrdxia = terma * ab.x;
real ddrdyia = terma * ab.y;
real ddrdzia = terma * ab.z;
real ddrdxic = termc * cb.x;
real ddrdyic = termc * cb.y;
real ddrdzic = termc * cb.z;
// get the energy and master chain rule terms for derivatives
energy += dt*drkk;
real3 force1 = make_real3(-frc1*dt*ddrdxia-ddtdxia*drkk, -frc1*dt*ddrdyia-ddtdyia*drkk, -frc1*dt*ddrdzia-ddtdzia*drkk);
real3 force3 = make_real3(-frc2*dt*ddrdxic-ddtdxic*drkk, -frc2*dt*ddrdyic-ddtdyic*drkk, -frc2*dt*ddrdzic-ddtdzic*drkk);
real3 force2 = make_real3(-force1.x-force3.x, -force1.y-force3.y, -force1.z-force3.z);
/* -------------------------------------------------------------------------- *
* 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-2016 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 CudaAmoebaAngleForce.
*/
#ifdef WIN32
#define _USE_MATH_DEFINES // Needed to get M_PI
#endif
#include "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.h"
#include "openmm/CustomAngleForce.h"
#include "OpenMMAmoeba.h"
#include "openmm/System.h"
#include "openmm/LangevinIntegrator.h"
#include "SimTKOpenMMRealType.h"
#include <iostream>
#include <vector>
using namespace OpenMM;
extern "C" void registerAmoebaCudaKernelFactories();
const double TOL = 1e-5;
/* ---------------------------------------------------------------------------------------
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 void getPrefactorsGivenAngleCosine(double cosine, double idealAngle, double quadraticK, double cubicK,
double quarticK, double penticK, double sexticK,
double* dEdR, double* energyTerm) {
double angle;
if (cosine >= 1.0) {
angle = 0.0f;
} else if (cosine <= -1.0) {
angle = RADIAN*PI_M;
} else {
angle = RADIAN*acos(cosine);
}
double deltaIdeal = angle - idealAngle;
double deltaIdeal2 = deltaIdeal*deltaIdeal;
double deltaIdeal3 = deltaIdeal*deltaIdeal2;
double deltaIdeal4 = deltaIdeal2*deltaIdeal2;
// deltaIdeal = r - r_0
*dEdR = (2.0 +
3.0*cubicK* deltaIdeal +
4.0*quarticK*deltaIdeal2 +
5.0*penticK* deltaIdeal3 +
6.0*sexticK* deltaIdeal4 );
*dEdR *= RADIAN*quadraticK*deltaIdeal;
*energyTerm = 1.0f + cubicK* deltaIdeal +
quarticK*deltaIdeal2 +
penticK* deltaIdeal3 +
sexticK* deltaIdeal4;
*energyTerm *= quadraticK*deltaIdeal2;
return;
}
static void computeAmoebaAngleForce(int bondIndex, std::vector<Vec3>& positions, AmoebaAngleForce& amoebaAngleForce,
std::vector<Vec3>& forces, double* energy) {
int particle1, particle2, particle3;
double idealAngle;
double quadraticK;
amoebaAngleForce.getAngleParameters(bondIndex, particle1, particle2, particle3, idealAngle, quadraticK);
double cubicK = amoebaAngleForce.getAmoebaGlobalAngleCubic();
double quarticK = amoebaAngleForce.getAmoebaGlobalAngleQuartic();
double penticK = amoebaAngleForce.getAmoebaGlobalAnglePentic();
double sexticK = amoebaAngleForce.getAmoebaGlobalAngleSextic();
double deltaR[2][3];
double r2_0 = 0.0;
double r2_1 = 0.0;
for (int ii = 0; ii < 3; ii++) {
deltaR[0][ii] = positions[particle1][ii] - positions[particle2][ii];
r2_0 += deltaR[0][ii]*deltaR[0][ii];
deltaR[1][ii] = positions[particle3][ii] - positions[particle2][ii];
r2_1 += deltaR[1][ii]*deltaR[1][ii];
}
double pVector[3];
crossProductVector3(deltaR[0], deltaR[1], pVector);
double rp = sqrt(pVector[0]*pVector[0] + pVector[1]*pVector[1] + pVector[2]*pVector[2]);
if (rp < 1.0e-06) {
rp = 1.0e-06;
}
double dot = deltaR[0][0]*deltaR[1][0] + deltaR[0][1]*deltaR[1][1] + deltaR[0][2]*deltaR[1][2];
double cosine = dot/sqrt(r2_0*r2_1);
double dEdR;
double energyTerm;
getPrefactorsGivenAngleCosine(cosine, idealAngle, quadraticK, cubicK,
quarticK, penticK, sexticK, &dEdR, &energyTerm);
double termA = -dEdR/(r2_0*rp);
double termC = dEdR/(r2_1*rp);
double deltaCrossP[3][3];
crossProductVector3(deltaR[0], pVector, deltaCrossP[0]);
crossProductVector3(deltaR[1], pVector, deltaCrossP[2]);
for (int ii = 0; ii < 3; ii++) {
deltaCrossP[0][ii] *= termA;
deltaCrossP[2][ii] *= termC;
deltaCrossP[1][ii] = -1.0*(deltaCrossP[0][ii] + deltaCrossP[2][ii]);
}
forces[particle1][0] += deltaCrossP[0][0];
forces[particle1][1] += deltaCrossP[0][1];
forces[particle1][2] += deltaCrossP[0][2];
forces[particle2][0] += deltaCrossP[1][0];
forces[particle2][1] += deltaCrossP[1][1];
forces[particle2][2] += deltaCrossP[1][2];
forces[particle3][0] += deltaCrossP[2][0];
forces[particle3][1] += deltaCrossP[2][1];
forces[particle3][2] += deltaCrossP[2][2];
*energy += energyTerm;
}
static void computeAmoebaAngleForces(Context& context, AmoebaAngleForce& amoebaAngleForce,
std::vector<Vec3>& expectedForces, double* expectedEnergy) {
// 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 < amoebaAngleForce.getNumAngles(); ii++) {
computeAmoebaAngleForce(ii, positions, amoebaAngleForce, expectedForces, expectedEnergy);
}
return;
}
void compareWithExpectedForceAndEnergy(Context& context, AmoebaAngleForce& amoebaAngleForce,
double tolerance, const std::string& idString) {
std::vector<Vec3> expectedForces;
double expectedEnergy;
computeAmoebaAngleForces(context, amoebaAngleForce, expectedForces, &expectedEnergy);
State state = context.getState(State::Forces | State::Energy);
const std::vector<Vec3> forces = state.getForces();
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 testOneAngle() {
System system;
int numberOfParticles = 3;
for (int ii = 0; ii < numberOfParticles; ii++) {
system.addParticle(1.0);
}
LangevinIntegrator integrator(0.0, 0.1, 0.01);
AmoebaAngleForce* amoebaAngleForce = new AmoebaAngleForce();
double angle = 100.0;
double quadraticK = 1.0;
double cubicK = 1.0e-01;
double quarticK = 1.0e-02;
double penticK = 1.0e-03;
double sexticK = 1.0e-04;
amoebaAngleForce->addAngle(0, 1, 2, angle, quadraticK);
amoebaAngleForce->setAmoebaGlobalAngleCubic(cubicK);
amoebaAngleForce->setAmoebaGlobalAngleQuartic(quarticK);
amoebaAngleForce->setAmoebaGlobalAnglePentic(penticK);
amoebaAngleForce->setAmoebaGlobalAngleSextic(sexticK);
system.addForce(amoebaAngleForce);
Context context(system, integrator, Platform::getPlatformByName("CUDA"));
std::vector<Vec3> positions(numberOfParticles);
positions[0] = Vec3(0, 1, 0);
positions[1] = Vec3(0, 0, 0);
positions[2] = Vec3(0, 0, 1);
context.setPositions(positions);
compareWithExpectedForceAndEnergy(context, *amoebaAngleForce, TOL, "testOneAngle");
// Try changing the angle parameters and make sure it's still correct.
amoebaAngleForce->setAngleParameters(0, 0, 1, 2, 1.1*angle, 1.4*quadraticK);
bool exceptionThrown = false;
try {
// This should throw an exception.
compareWithExpectedForceAndEnergy(context, *amoebaAngleForce, TOL, "testOneAngle");
}
catch (std::exception ex) {
exceptionThrown = true;
}
ASSERT(exceptionThrown);
amoebaAngleForce->updateParametersInContext(context);
compareWithExpectedForceAndEnergy(context, *amoebaAngleForce, TOL, "testOneAngle");
}
void testPeriodic() {
// Create a force that uses periodic boundary conditions, then compare to an identical custom force.
System system;
system.setDefaultPeriodicBoxVectors(Vec3(3, 0, 0), Vec3(0, 3, 0), Vec3(0, 0, 3));
int numParticles = 3;
for (int ii = 0; ii < numParticles; ii++)
system.addParticle(1.0);
LangevinIntegrator integrator(0.0, 0.1, 0.01);
AmoebaAngleForce* amoebaAngleForce = new AmoebaAngleForce();
double angle = 100.0;
double quadraticK = 1.0;
double cubicK = 1.0e-01;
double quarticK = 1.0e-02;
double penticK = 1.0e-03;
double sexticK = 1.0e-04;
amoebaAngleForce->addAngle(0, 1, 2, angle, quadraticK);
amoebaAngleForce->setAmoebaGlobalAngleCubic(cubicK);
amoebaAngleForce->setAmoebaGlobalAngleQuartic(quarticK);
amoebaAngleForce->setAmoebaGlobalAnglePentic(penticK);
amoebaAngleForce->setAmoebaGlobalAngleSextic(sexticK);
amoebaAngleForce->setUsesPeriodicBoundaryConditions(true);
system.addForce(amoebaAngleForce);
CustomAngleForce* customForce = new CustomAngleForce("k2*delta^2 + k3*delta^3 + k4*delta^4 + k5*delta^5 + k6*delta^6; delta=theta-theta0");
customForce->addGlobalParameter("theta0", angle*M_PI/180);
customForce->addGlobalParameter("k2", quadraticK*pow(180/M_PI, 2.0));
customForce->addGlobalParameter("k3", cubicK*pow(180/M_PI, 3.0));
customForce->addGlobalParameter("k4", quarticK*pow(180/M_PI, 4.0));
customForce->addGlobalParameter("k5", penticK*pow(180/M_PI, 5.0));
customForce->addGlobalParameter("k6", sexticK*pow(180/M_PI, 6.0));
customForce->addAngle(0, 1, 2);
customForce->setUsesPeriodicBoundaryConditions(true);
customForce->setForceGroup(1);
system.addForce(customForce);
Context context(system, integrator, Platform::getPlatformByName("CUDA"));
std::vector<Vec3> positions(numParticles);
positions[0] = Vec3(0, 1, 0);
positions[1] = Vec3(0, 0, 0);
positions[2] = Vec3(0, 0, 2);
context.setPositions(positions);
State s1 = context.getState(State::Forces | State::Energy, true, 1);
State s2 = context.getState(State::Forces | State::Energy, true, 2);
ASSERT_EQUAL_TOL(s2.getPotentialEnergy(), s1.getPotentialEnergy(), 1e-5);
for (int i = 0; i < numParticles; i++)
ASSERT_EQUAL_VEC(s2.getForces()[i], s1.getForces()[i], 1e-5);
}
int main(int argc, char* argv[]) {
try {
std::cout << "TestCudaAmoebaAngleForce running test..." << std::endl;
registerAmoebaCudaKernelFactories();
if (argc > 1)
Platform::getPlatformByName("CUDA").setPropertyDefaultValue("Precision", std::string(argv[1]));
testOneAngle();
testPeriodic();
} 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-2016 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 AmoebaBondForce.
*/
#include "openmm/internal/AssertionUtilities.h"
#include "CudaPlatform.h"
#include "openmm/Context.h"
#include "openmm/CustomBondForce.h"
#include "OpenMMAmoeba.h"
#include "openmm/System.h"
#include "openmm/LangevinIntegrator.h"
#include <iostream>
#include <vector>
using namespace OpenMM;
extern "C" void registerAmoebaCudaKernelFactories();
const double TOL = 1e-5;
static void computeAmoebaBondForce(int bondIndex, std::vector<Vec3>& positions, AmoebaBondForce& amoebaBondForce,
std::vector<Vec3>& forces, double* energy) {
int particle1, particle2;
double bondLength;
double quadraticK;
double cubicK = amoebaBondForce.getAmoebaGlobalBondCubic();
double quarticK = amoebaBondForce.getAmoebaGlobalBondQuartic();
amoebaBondForce.getBondParameters(bondIndex, particle1, particle2, bondLength, quadraticK);
double deltaR[3];
double r2 = 0.0;
for (int ii = 0; ii < 3; ii++) {
deltaR[ii] = positions[particle2][ii] - positions[particle1][ii];
r2 += deltaR[ii]*deltaR[ii];
}
double r = sqrt(r2);
double bondDelta = (r - bondLength);
double bondDelta2 = bondDelta*bondDelta;
double dEdR = 1.0 + 1.5*cubicK*bondDelta + 2.0*quarticK*bondDelta2;
dEdR *= (r > 0.0) ? (2.0*quadraticK*bondDelta)/r : 0.0;
forces[particle1][0] += dEdR*deltaR[0];
forces[particle1][1] += dEdR*deltaR[1];
forces[particle1][2] += dEdR*deltaR[2];
forces[particle2][0] -= dEdR*deltaR[0];
forces[particle2][1] -= dEdR*deltaR[1];
forces[particle2][2] -= dEdR*deltaR[2];
*energy += (1.0f + cubicK*bondDelta + quarticK*bondDelta2)*quadraticK*bondDelta2;
}
static void computeAmoebaBondForces(Context& context, AmoebaBondForce& amoebaBondForce,
std::vector<Vec3>& expectedForces, double* expectedEnergy) {
// 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 < amoebaBondForce.getNumBonds(); ii++) {
computeAmoebaBondForce(ii, positions, amoebaBondForce, expectedForces, expectedEnergy);
}
}
void compareWithExpectedForceAndEnergy(Context& context, AmoebaBondForce& amoebaBondForce, double tolerance, const std::string& idString) {
std::vector<Vec3> expectedForces;
double expectedEnergy;
computeAmoebaBondForces(context, amoebaBondForce, expectedForces, &expectedEnergy);
State state = context.getState(State::Forces | State::Energy);
const std::vector<Vec3> forces = state.getForces();
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 testOneBond() {
System system;
system.addParticle(1.0);
system.addParticle(1.0);
LangevinIntegrator integrator(0.0, 0.1, 0.01);
AmoebaBondForce* amoebaBondForce = new AmoebaBondForce();
double bondLength = 1.5;
double quadraticK = 1.0;
double cubicK = 2.0;
double quarticicK = 3.0;
amoebaBondForce->setAmoebaGlobalBondCubic(cubicK);
amoebaBondForce->setAmoebaGlobalBondQuartic(quarticicK);
amoebaBondForce->addBond(0, 1, bondLength, quadraticK);
system.addForce(amoebaBondForce);
Context context(system, integrator, Platform::getPlatformByName("CUDA"));
std::vector<Vec3> positions(2);
positions[0] = Vec3(0, 1, 0);
positions[1] = Vec3(0, 0, 0);
context.setPositions(positions);
compareWithExpectedForceAndEnergy(context, *amoebaBondForce, TOL, "testOneBond");
}
void testTwoBond() {
System system;
system.addParticle(1.0);
system.addParticle(1.0);
system.addParticle(1.0);
LangevinIntegrator integrator(0.0, 0.1, 0.01);
AmoebaBondForce* amoebaBondForce = new AmoebaBondForce();
double bondLength = 1.5;
double quadraticK = 1.0;
double cubicK = 2.0;
double quarticicK = 3.0;
amoebaBondForce->setAmoebaGlobalBondCubic(cubicK);
amoebaBondForce->setAmoebaGlobalBondQuartic(quarticicK);
amoebaBondForce->addBond(0, 1, bondLength, quadraticK);
amoebaBondForce->addBond(1, 2, bondLength, quadraticK);
system.addForce(amoebaBondForce);
Context context(system, integrator, Platform::getPlatformByName("CUDA"));
//Context context(system, integrator, platform);
std::vector<Vec3> positions(3);
positions[0] = Vec3(0, 1, 0);
positions[1] = Vec3(0, 0, 0);
positions[2] = Vec3(1, 0, 1);
context.setPositions(positions);
compareWithExpectedForceAndEnergy(context, *amoebaBondForce, TOL, "testTwoBond");
// Try changing the bond parameters and make sure it's still correct.
amoebaBondForce->setBondParameters(0, 0, 1, 1.1*bondLength, 1.4*quadraticK);
amoebaBondForce->setBondParameters(1, 1, 2, 1.2*bondLength, 0.9*quadraticK);
bool exceptionThrown = false;
try {
// This should throw an exception.
compareWithExpectedForceAndEnergy(context, *amoebaBondForce, TOL, "testTwoBond");
}
catch (std::exception ex) {
exceptionThrown = true;
}
ASSERT(exceptionThrown);
amoebaBondForce->updateParametersInContext(context);
compareWithExpectedForceAndEnergy(context, *amoebaBondForce, TOL, "testTwoBond");
}
void testPeriodic() {
// Create a force that uses periodic boundary conditions, then compare to an identical custom force.
System system;
system.setDefaultPeriodicBoxVectors(Vec3(3, 0, 0), Vec3(0, 3, 0), Vec3(0, 0, 3));
int numParticles = 2;
for (int ii = 0; ii < numParticles; ii++)
system.addParticle(1.0);
LangevinIntegrator integrator(0.0, 0.1, 0.01);
AmoebaBondForce* amoebaBondForce = new AmoebaBondForce();
double bondLength = 1.5;
double quadraticK = 1.0;
double cubicK = 2.0;
double quarticK = 3.0;
amoebaBondForce->setAmoebaGlobalBondCubic(cubicK);
amoebaBondForce->setAmoebaGlobalBondQuartic(quarticK);
amoebaBondForce->addBond(0, 1, bondLength, quadraticK);
amoebaBondForce->setUsesPeriodicBoundaryConditions(true);
system.addForce(amoebaBondForce);
CustomBondForce* customForce = new CustomBondForce("k2*delta^2 + k3*delta^3 + k4*delta^4; delta=r-r0");
customForce->addGlobalParameter("r0", bondLength);
customForce->addGlobalParameter("k2", quadraticK);
customForce->addGlobalParameter("k3", cubicK);
customForce->addGlobalParameter("k4", quarticK);
customForce->addBond(0, 1);
customForce->setUsesPeriodicBoundaryConditions(true);
customForce->setForceGroup(1);
system.addForce(customForce);
Context context(system, integrator, Platform::getPlatformByName("CUDA"));
std::vector<Vec3> positions(numParticles);
positions[0] = Vec3(0, 2, 0);
positions[1] = Vec3(0, 0, 0);
context.setPositions(positions);
State s1 = context.getState(State::Forces | State::Energy, true, 1);
State s2 = context.getState(State::Forces | State::Energy, true, 2);
ASSERT_EQUAL_TOL(s2.getPotentialEnergy(), s1.getPotentialEnergy(), 1e-5);
for (int i = 0; i < numParticles; i++)
ASSERT_EQUAL_VEC(s2.getForces()[i], s1.getForces()[i], 1e-5);
}
int main(int argc, char* argv[]) {
try {
std::cout << "TestCudaAmoebaBondForce running test..." << std::endl;
registerAmoebaCudaKernelFactories();
if (argc > 1)
Platform::getPlatformByName("CUDA").setPropertyDefaultValue("Precision", std::string(argv[1]));
testTwoBond();
testPeriodic();
} 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;
}
......@@ -35,6 +35,7 @@
#include "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.h"
#include "openmm/CustomBondForce.h"
#include "OpenMMAmoeba.h"
#include "openmm/System.h"
#include "openmm/AmoebaMultipoleForce.h"
......@@ -551,17 +552,17 @@ static void setupAndGetForcesEnergyMultipoleWater(AmoebaMultipoleForce::Nonbonde
// 1-2 bonds needed
AmoebaBondForce* amoebaBondForce = new AmoebaBondForce();
CustomBondForce* amoebaBondForce = new CustomBondForce("k*(d^2 - 25.5*d^3 + 379.3125*d^4); d=r-r0");
amoebaBondForce->addPerBondParameter("r0");
amoebaBondForce->addPerBondParameter("k");
// addBond: particle1, particle2, length, quadraticK
for (unsigned int jj = 0; jj < numberOfParticles; jj += 3) {
amoebaBondForce->addBond(jj, jj+1, 0.0000000e+00, 0.0000000e+00);
amoebaBondForce->addBond(jj, jj+2, 0.0000000e+00, 0.0000000e+00);
amoebaBondForce->addBond(jj, jj+1, {0.0000000e+00, 0.0000000e+00});
amoebaBondForce->addBond(jj, jj+2, {0.0000000e+00, 0.0000000e+00});
}
amoebaBondForce->setAmoebaGlobalBondCubic(-2.5500000e+01);
amoebaBondForce->setAmoebaGlobalBondQuartic(3.7931250e+02);
system.addForce(amoebaBondForce);
std::vector<Vec3> positions(numberOfParticles);
......@@ -776,17 +777,17 @@ static void testQuadrupoleValidation() {
}
*/
AmoebaBondForce* amoebaBondForce = new AmoebaBondForce();
CustomBondForce* amoebaBondForce = new CustomBondForce("k*(d^2 - 25.5*d^3 + 379.3125*d^4); d=r-r0");
amoebaBondForce->addPerBondParameter("r0");
amoebaBondForce->addPerBondParameter("k");
// addBond: particle1, particle2, length, quadraticK
for (unsigned int jj = 0; jj < numberOfParticles; jj += 3) {
amoebaBondForce->addBond(jj, jj+1, 0.0000000e+00, 0.0000000e+00);
amoebaBondForce->addBond(jj, jj+2, 0.0000000e+00, 0.0000000e+00);
amoebaBondForce->addBond(jj, jj+1, {0.0000000e+00, 0.0000000e+00});
amoebaBondForce->addBond(jj, jj+2, {0.0000000e+00, 0.0000000e+00});
}
amoebaBondForce->setAmoebaGlobalBondCubic(-2.5500000e+01);
amoebaBondForce->setAmoebaGlobalBondQuartic(3.7931250e+02);
system.addForce(amoebaBondForce);
std::vector<Vec3> positions(numberOfParticles);
......@@ -1029,17 +1030,17 @@ static void setupAndGetForcesEnergyMultipoleIonsAndWater(AmoebaMultipoleForce::N
// 1-2 bonds needed
AmoebaBondForce* amoebaBondForce = new AmoebaBondForce();
CustomBondForce* amoebaBondForce = new CustomBondForce("k*(d^2 - 25.5*d^3 + 379.3125*d^4); d=r-r0");
amoebaBondForce->addPerBondParameter("r0");
amoebaBondForce->addPerBondParameter("k");
// addBond: particle1, particle2, length, quadraticK
for (unsigned int jj = 2; jj < numberOfParticles; jj += 3) {
amoebaBondForce->addBond(jj, jj+1, 0.0000000e+00, 0.0000000e+00);
amoebaBondForce->addBond(jj, jj+2, 0.0000000e+00, 0.0000000e+00);
amoebaBondForce->addBond(jj, jj+1, {0.0000000e+00, 0.0000000e+00});
amoebaBondForce->addBond(jj, jj+2, {0.0000000e+00, 0.0000000e+00});
}
amoebaBondForce->setAmoebaGlobalBondCubic(-2.5500000e+01);
amoebaBondForce->setAmoebaGlobalBondQuartic(3.7931250e+02);
system.addForce(amoebaBondForce);
std::vector<Vec3> positions(numberOfParticles);
......@@ -1261,17 +1262,17 @@ static void setupAndGetForcesEnergyMultipoleLargeWater(AmoebaMultipoleForce::Non
// 1-2 bonds needed
AmoebaBondForce* amoebaBondForce = new AmoebaBondForce();
CustomBondForce* amoebaBondForce = new CustomBondForce("k*(d^2 - 25.5*d^3 + 379.3125*d^4); d=r-r0");
amoebaBondForce->addPerBondParameter("r0");
amoebaBondForce->addPerBondParameter("k");
// addBond: particle1, particle2, length, quadraticK
for (unsigned int jj = 0; jj < numberOfParticles; jj += 3) {
amoebaBondForce->addBond(jj, jj+1, 0.0000000e+00, 0.0000000e+00);
amoebaBondForce->addBond(jj, jj+2, 0.0000000e+00, 0.0000000e+00);
amoebaBondForce->addBond(jj, jj+1, {0.0000000e+00, 0.0000000e+00});
amoebaBondForce->addBond(jj, jj+2, {0.0000000e+00, 0.0000000e+00});
}
amoebaBondForce->setAmoebaGlobalBondCubic(0.0);
amoebaBondForce->setAmoebaGlobalBondQuartic(0.0);
system.addForce(amoebaBondForce);
static std::vector<Vec3> positions; // Static to work around bug in Visual Studio that makes compilation very very slow.
......
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