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: 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 CudaAmoebaPiTorsionForce.
*/
#include "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.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;
/* ---------------------------------------------------------------------------------------
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 computeAmoebaPiTorsionForce(int bondIndex, std::vector<Vec3>& positions, AmoebaPiTorsionForce& amoebaPiTorsionForce,
std::vector<Vec3>& forces, double* energy) {
int particle1, particle2, particle3, particle4, particle5, particle6;
double kTorsion;
amoebaPiTorsionForce.getPiTorsionParameters(bondIndex, particle1, particle2, particle3, particle4, particle5, particle6, kTorsion);
enum { AD, BD, EC, FC, P, Q, CP, DC, QD, T, U, TU, DP, QC, dT, dU, dP, dQ, dC1, dC2, dD1, dD2, LastDeltaIndex };
double deltaR[LastDeltaIndex][3];
enum { A, B, C, D, E, F, LastAtomIndex };
double d[LastAtomIndex][3];
for (int ii = 0; ii < 3; ii++) {
deltaR[AD][ii] = positions[particle1][ii] - positions[particle4][ii];
deltaR[BD][ii] = positions[particle2][ii] - positions[particle4][ii];
deltaR[EC][ii] = positions[particle5][ii] - positions[particle3][ii];
deltaR[FC][ii] = positions[particle6][ii] - positions[particle3][ii];
}
crossProductVector3(deltaR[AD], deltaR[BD], deltaR[P]);
crossProductVector3(deltaR[EC], deltaR[FC], deltaR[Q]);
for (int ii = 0; ii < 3; ii++) {
deltaR[CP][ii] = -deltaR[P][ii];
deltaR[DC][ii] = positions[particle4][ii] - positions[particle3][ii];
deltaR[QD][ii] = deltaR[Q][ii];
deltaR[P][ii] += positions[particle3][ii];
deltaR[Q][ii] += positions[particle4][ii];
}
crossProductVector3(deltaR[CP], deltaR[DC], deltaR[T] );
crossProductVector3(deltaR[DC], deltaR[QD], deltaR[U] );
crossProductVector3(deltaR[T], deltaR[U], deltaR[TU]);
double rT2 = dotVector3(deltaR[T], deltaR[T]);
double rU2 = dotVector3(deltaR[U], deltaR[U]);
double rTrU = sqrt(rT2*rU2);
if (rTrU <= 0.0) {
return;
}
double rDC = dotVector3(deltaR[DC], deltaR[DC]);
rDC = sqrt(rDC);
double cosine = dotVector3(deltaR[T], deltaR[U]);
cosine /= rTrU;
double sine = dotVector3(deltaR[DC], deltaR[TU]);
sine /= (rDC*rTrU);
double cosine2 = cosine*cosine - sine*sine;
double sine2 = 2.0*cosine*sine;
double phi2 = 1.0 - cosine2;
double dphi2 = 2.0*sine2;
double dedphi = kTorsion*dphi2;
for (int ii = 0; ii < 3; ii++) {
deltaR[DP][ii] = positions[particle4][ii] - deltaR[P][ii];
deltaR[QC][ii] = deltaR[Q][ii] - positions[particle3][ii];
}
double factorT = dedphi/(rDC*rT2);
double factorU = -dedphi/(rDC*rU2);
crossProductVector3(deltaR[T], deltaR[DC], deltaR[dT] );
crossProductVector3(deltaR[U], deltaR[DC], deltaR[dU] );
for (int ii = 0; ii < 3; ii++) {
deltaR[dT][ii] *= factorT;
deltaR[dU][ii] *= factorU;
}
crossProductVector3(deltaR[dT], deltaR[DC], deltaR[dP] );
crossProductVector3(deltaR[dU], deltaR[DC], deltaR[dQ] );
crossProductVector3(deltaR[DP], deltaR[dT], deltaR[dC1] );
crossProductVector3(deltaR[dU], deltaR[QD], deltaR[dC2] );
crossProductVector3(deltaR[dT], deltaR[CP], deltaR[dD1] );
crossProductVector3(deltaR[QC], deltaR[dU], deltaR[dD2] );
crossProductVector3(deltaR[BD], deltaR[dP], d[A] );
crossProductVector3(deltaR[dP], deltaR[AD], d[B] );
crossProductVector3(deltaR[FC], deltaR[dQ], d[E] );
crossProductVector3(deltaR[dQ], deltaR[EC], d[F] );
for (int ii = 0; ii < 3; ii++) {
d[C][ii] = deltaR[dC1][ii] + deltaR[dC2][ii] + deltaR[dP][ii] - d[E][ii] - d[F][ii];
d[D][ii] = deltaR[dD1][ii] + deltaR[dD2][ii] + deltaR[dQ][ii] - d[A][ii] - d[B][ii];
}
// ---------------------------------------------------------------------------------------
// accumulate forces and energy
forces[particle1][0] -= d[0][0];
forces[particle1][1] -= d[0][1];
forces[particle1][2] -= d[0][2];
forces[particle2][0] -= d[1][0];
forces[particle2][1] -= d[1][1];
forces[particle2][2] -= d[1][2];
forces[particle3][0] -= d[2][0];
forces[particle3][1] -= d[2][1];
forces[particle3][2] -= d[2][2];
forces[particle4][0] -= d[3][0];
forces[particle4][1] -= d[3][1];
forces[particle4][2] -= d[3][2];
forces[particle5][0] -= d[4][0];
forces[particle5][1] -= d[4][1];
forces[particle5][2] -= d[4][2];
forces[particle6][0] -= d[5][0];
forces[particle6][1] -= d[5][1];
forces[particle6][2] -= d[5][2];
*energy += kTorsion*phi2;
return;
}
static void computeAmoebaPiTorsionForces(Context& context, AmoebaPiTorsionForce& amoebaPiTorsionForce,
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 < amoebaPiTorsionForce.getNumPiTorsions(); ii++) {
computeAmoebaPiTorsionForce(ii, positions, amoebaPiTorsionForce, expectedForces, expectedEnergy);
}
}
void compareWithExpectedForceAndEnergy(Context& context, AmoebaPiTorsionForce& amoebaPiTorsionForce,
double tolerance, const std::string& idString) {
std::vector<Vec3> expectedForces;
double expectedEnergy;
computeAmoebaPiTorsionForces(context, amoebaPiTorsionForce, 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 testOnePiTorsion() {
System system;
int numberOfParticles = 6;
for (int ii = 0; ii < numberOfParticles; ii++) {
system.addParticle(1.0);
}
LangevinIntegrator integrator(0.0, 0.1, 0.01);
AmoebaPiTorsionForce* amoebaPiTorsionForce = new AmoebaPiTorsionForce();
double kTorsion = 6.85;
amoebaPiTorsionForce->addPiTorsion(0, 1, 2, 3, 4, 5, kTorsion);
system.addForce(amoebaPiTorsionForce);
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.278860000E+02, 0.264630000E+02, 0.426300000E+01);
positions[2] = Vec3(0.269130000E+02, 0.266390000E+02, 0.353100000E+01);
positions[3] = Vec3(0.245568230E+02, 0.250215290E+02, 0.796852800E+01);
positions[4] = Vec3(0.261000000E+02, 0.292530000E+02, 0.520200000E+01);
positions[5] = Vec3(0.254124630E+02, 0.234691880E+02, 0.773335400E+01);
context.setPositions(positions);
compareWithExpectedForceAndEnergy(context, *amoebaPiTorsionForce, TOL, "testOnePiTorsion");
// Try changing the torsion parameters and make sure it's still correct.
amoebaPiTorsionForce->setPiTorsionParameters(0, 0, 1, 2, 3, 4, 5, 1.2*kTorsion);
bool exceptionThrown = false;
try {
// This should throw an exception.
compareWithExpectedForceAndEnergy(context, *amoebaPiTorsionForce, TOL, "testOnePiTorsion");
}
catch (std::exception ex) {
exceptionThrown = true;
}
ASSERT(exceptionThrown);
amoebaPiTorsionForce->updateParametersInContext(context);
compareWithExpectedForceAndEnergy(context, *amoebaPiTorsionForce, TOL, "testOnePiTorsion");
}
void testPeriodic() {
// Create a force that uses periodic boundary conditions.
System system;
system.setDefaultPeriodicBoxVectors(Vec3(3, 0, 0), Vec3(0, 3, 0), Vec3(0, 0, 3));
int numberOfParticles = 6;
for (int ii = 0; ii < numberOfParticles; ii++)
system.addParticle(1.0);
LangevinIntegrator integrator(0.0, 0.1, 0.01);
AmoebaPiTorsionForce* amoebaPiTorsionForce = new AmoebaPiTorsionForce();
double kTorsion = 6.85;
amoebaPiTorsionForce->addPiTorsion(0, 1, 2, 3, 4, 5, kTorsion);
amoebaPiTorsionForce->setUsesPeriodicBoundaryConditions(true);
system.addForce(amoebaPiTorsionForce);
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, 0.5);
positions[3] = Vec3(0.4, 0.4, 0.4);
positions[4] = Vec3(1, 0, 1);
positions[5] = Vec3(1, 1, 0);
context.setPositions(positions);
State s1 = context.getState(State::Forces | State::Energy);
// Move one atom to a position that should give identical results.
positions[0] = Vec3(0, -2, 0);
context.setPositions(positions);
State s2 = context.getState(State::Forces | State::Energy);
ASSERT_EQUAL_TOL(s1.getPotentialEnergy(), s2.getPotentialEnergy(), 1e-5);
for (int i = 0; i < numberOfParticles; i++)
ASSERT_EQUAL_VEC(s1.getForces()[i], s2.getForces()[i], 1e-5);
}
int main(int argc, char* argv[]) {
try {
std::cout << "TestCudaAmoebaPiTorsionForce running test..." << std::endl;
registerAmoebaCudaKernelFactories();
if (argc > 1)
Platform::getPlatformByName("CUDA").setPropertyDefaultValue("Precision", std::string(argv[1]));
testOnePiTorsion();
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 CudaAmoebaStretchBendForce.
*/
#include "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.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-4;
const double DegreesToRadians = PI_M/180.0;
/* ---------------------------------------------------------------------------------------
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) {
int particle1, particle2, particle3;
double abBondLength, cbBondLength, angleStretchBend, kStretchBend, k2StretchBend;
amoebaStretchBendForce.getStretchBendParameters(bondIndex, particle1, particle2, particle3, abBondLength, cbBondLength, angleStretchBend, kStretchBend, k2StretchBend);
angleStretchBend *= RADIAN;
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 dr1 = rAB - abBondLength;
double dr2 = rCB - cbBondLength;
termA = 1.0/rAB;
termC = 1.0/rCB;
double drkk = dr1 * kStretchBend + dr2 * k2StretchBend;
// ---------------------------------------------------------------------------------------
// 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] = kStretchBend*dt*termA*deltaR[AB][jj] + drkk*deltaR[ABxP][jj];
subForce[C][jj] = k2StretchBend*dt*termC*deltaR[CB][jj] + drkk*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 += dt*drkk;
}
static void computeAmoebaStretchBendForces(Context& context, AmoebaStretchBendForce& amoebaStretchBendForce,
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 < amoebaStretchBendForce.getNumStretchBends(); ii++) {
computeAmoebaStretchBendForce(ii, positions, amoebaStretchBendForce, expectedForces, expectedEnergy);
}
}
void compareWithExpectedForceAndEnergy(Context& context, AmoebaStretchBendForce& amoebaStretchBendForce,
double tolerance, const std::string& idString) {
std::vector<Vec3> expectedForces;
double expectedEnergy;
computeAmoebaStretchBendForces(context, amoebaStretchBendForce, 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 testOneStretchBend() {
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, 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");
// Try changing the stretch-bend parameters and make sure it's still correct.
amoebaStretchBendForce->setStretchBendParameters(0, 0, 1, 2, 1.1*abLength, 1.2*cbLength, 1.3*angleStretchBend, 1.4*kStretchBend, 1.4*kStretchBend);
bool exceptionThrown = false;
try {
// This should throw an exception.
compareWithExpectedForceAndEnergy(context, *amoebaStretchBendForce, TOL, "testOneStretchBend");
}
catch (std::exception ex) {
exceptionThrown = true;
}
ASSERT(exceptionThrown);
amoebaStretchBendForce->updateParametersInContext(context);
compareWithExpectedForceAndEnergy(context, *amoebaStretchBendForce, TOL, "testOneStretchBend");
}
void testPeriodic() {
// Create a force that uses periodic boundary conditions.
System system;
system.setDefaultPeriodicBoxVectors(Vec3(3, 0, 0), Vec3(0, 3, 0), Vec3(0, 0, 3));
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 = 1.0;
amoebaStretchBendForce->addStretchBend(0, 1, 2, abLength, cbLength, angleStretchBend, kStretchBend, kStretchBend);
amoebaStretchBendForce->setUsesPeriodicBoundaryConditions(true);
system.addForce(amoebaStretchBendForce);
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);
State s1 = context.getState(State::Forces | State::Energy);
// Move one atom to a position that should give identical results.
positions[2] = Vec3(0, 0, -2);
context.setPositions(positions);
State s2 = context.getState(State::Forces | State::Energy);
ASSERT_EQUAL_TOL(s1.getPotentialEnergy(), s2.getPotentialEnergy(), 1e-5);
for (int i = 0; i < numberOfParticles; i++)
ASSERT_EQUAL_VEC(s1.getForces()[i], s2.getForces()[i], 1e-5);
}
int main(int argc, char* argv[]) {
try {
std::cout << "TestCudaAmoebaStretchBendForce running test..." << std::endl;
registerAmoebaCudaKernelFactories();
if (argc > 1)
Platform::getPlatformByName("CUDA").setPropertyDefaultValue("Precision", std::string(argv[1]));
testOneStretchBend();
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;
}
......@@ -48,12 +48,6 @@ extern "C" OPENMM_EXPORT void registerKernelFactories() {
Platform& platform = Platform::getPlatform(i);
if (dynamic_cast<ReferencePlatform*>(&platform) != NULL) {
AmoebaReferenceKernelFactory* factory = new AmoebaReferenceKernelFactory();
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(CalcAmoebaVdwForceKernel::Name(), factory);
platform.registerKernelFactory(CalcAmoebaMultipoleForceKernel::Name(), factory);
......@@ -69,28 +63,6 @@ extern "C" OPENMM_EXPORT void registerAmoebaReferenceKernelFactories() {
}
KernelImpl* AmoebaReferenceKernelFactory::createKernelImpl(std::string name, const Platform& platform, ContextImpl& context) const {
ReferencePlatform::PlatformData& referencePlatformData = *static_cast<ReferencePlatform::PlatformData*>(context.getPlatformData());
// create AmoebaReferenceData object if contextToAmoebaDataMap does not contain
// key equal to current context
if (name == CalcAmoebaBondForceKernel::Name())
return new ReferenceCalcAmoebaBondForceKernel(name, platform, context.getSystem());
if (name == CalcAmoebaAngleForceKernel::Name())
return new ReferenceCalcAmoebaAngleForceKernel(name, platform, context.getSystem());
if (name == CalcAmoebaInPlaneAngleForceKernel::Name())
return new ReferenceCalcAmoebaInPlaneAngleForceKernel(name, platform, context.getSystem());
if (name == CalcAmoebaPiTorsionForceKernel::Name())
return new ReferenceCalcAmoebaPiTorsionForceKernel(name, platform, context.getSystem());
if (name == CalcAmoebaStretchBendForceKernel::Name())
return new ReferenceCalcAmoebaStretchBendForceKernel(name, platform, context.getSystem());
if (name == CalcAmoebaOutOfPlaneBendForceKernel::Name())
return new ReferenceCalcAmoebaOutOfPlaneBendForceKernel(name, platform, context.getSystem());
if (name == CalcAmoebaTorsionTorsionForceKernel::Name())
return new ReferenceCalcAmoebaTorsionTorsionForceKernel(name, platform, context.getSystem());
......
......@@ -25,12 +25,6 @@
* -------------------------------------------------------------------------- */
#include "AmoebaReferenceKernels.h"
#include "AmoebaReferenceBondForce.h"
#include "AmoebaReferenceAngleForce.h"
#include "AmoebaReferenceInPlaneAngleForce.h"
#include "AmoebaReferencePiTorsionForce.h"
#include "AmoebaReferenceStretchBendForce.h"
#include "AmoebaReferenceOutOfPlaneBendForce.h"
#include "AmoebaReferenceTorsionTorsionForce.h"
#include "AmoebaReferenceWcaDispersionForce.h"
#include "AmoebaReferenceGeneralizedKirkwoodForce.h"
......@@ -82,351 +76,6 @@ static Vec3* extractBoxVectors(ContextImpl& context) {
// ***************************************************************************
ReferenceCalcAmoebaBondForceKernel::ReferenceCalcAmoebaBondForceKernel(const std::string& name, const Platform& platform, const System& system) :
CalcAmoebaBondForceKernel(name, platform), system(system) {
}
ReferenceCalcAmoebaBondForceKernel::~ReferenceCalcAmoebaBondForceKernel() {
}
void ReferenceCalcAmoebaBondForceKernel::initialize(const System& system, const AmoebaBondForce& force) {
numBonds = force.getNumBonds();
for (int ii = 0; ii < numBonds; ii++) {
int particle1Index, particle2Index;
double lengthValue, kValue;
force.getBondParameters(ii, particle1Index, particle2Index, lengthValue, kValue);
particle1.push_back(particle1Index);
particle2.push_back(particle2Index);
length.push_back(static_cast<double>(lengthValue));
kQuadratic.push_back(kValue);
}
globalBondCubic = force.getAmoebaGlobalBondCubic();
globalBondQuartic = force.getAmoebaGlobalBondQuartic();
usePeriodic = force.usesPeriodicBoundaryConditions();
}
double ReferenceCalcAmoebaBondForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
vector<Vec3>& posData = extractPositions(context);
vector<Vec3>& forceData = extractForces(context);
AmoebaReferenceBondForce amoebaReferenceBondForce;
if (usePeriodic)
amoebaReferenceBondForce.setPeriodic(extractBoxVectors(context));
double energy = amoebaReferenceBondForce.calculateForceAndEnergy(numBonds, posData, particle1, particle2, length, kQuadratic,
globalBondCubic, globalBondQuartic,
forceData);
return static_cast<double>(energy);
}
void ReferenceCalcAmoebaBondForceKernel::copyParametersToContext(ContextImpl& context, const AmoebaBondForce& force) {
if (numBonds != force.getNumBonds())
throw OpenMMException("updateParametersInContext: The number of bonds has changed");
// Record the values.
for (int i = 0; i < numBonds; ++i) {
int particle1Index, particle2Index;
double lengthValue, kValue;
force.getBondParameters(i, particle1Index, particle2Index, lengthValue, kValue);
if (particle1Index != particle1[i] || particle2Index != particle2[i])
throw OpenMMException("updateParametersInContext: The set of particles in a bond has changed");
length[i] = lengthValue;
kQuadratic[i] = kValue;
}
}
// ***************************************************************************
ReferenceCalcAmoebaAngleForceKernel::ReferenceCalcAmoebaAngleForceKernel(const std::string& name, const Platform& platform, const System& system) :
CalcAmoebaAngleForceKernel(name, platform), system(system) {
}
ReferenceCalcAmoebaAngleForceKernel::~ReferenceCalcAmoebaAngleForceKernel() {
}
void ReferenceCalcAmoebaAngleForceKernel::initialize(const System& system, const AmoebaAngleForce& force) {
numAngles = force.getNumAngles();
for (int ii = 0; ii < numAngles; ii++) {
int particle1Index, particle2Index, particle3Index;
double angleValue, k;
force.getAngleParameters(ii, particle1Index, particle2Index, particle3Index, angleValue, k);
particle1.push_back(particle1Index);
particle2.push_back(particle2Index);
particle3.push_back(particle3Index);
angle.push_back(angleValue);
kQuadratic.push_back(k);
}
globalAngleCubic = force.getAmoebaGlobalAngleCubic();
globalAngleQuartic = force.getAmoebaGlobalAngleQuartic();
globalAnglePentic = force.getAmoebaGlobalAnglePentic();
globalAngleSextic = force.getAmoebaGlobalAngleSextic();
usePeriodic = force.usesPeriodicBoundaryConditions();
}
double ReferenceCalcAmoebaAngleForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
vector<Vec3>& posData = extractPositions(context);
vector<Vec3>& forceData = extractForces(context);
AmoebaReferenceAngleForce amoebaReferenceAngleForce;
if (usePeriodic)
amoebaReferenceAngleForce.setPeriodic(extractBoxVectors(context));
double energy = amoebaReferenceAngleForce.calculateForceAndEnergy(numAngles,
posData, particle1, particle2, particle3, angle, kQuadratic, globalAngleCubic, globalAngleQuartic, globalAnglePentic, globalAngleSextic, forceData);
return static_cast<double>(energy);
}
void ReferenceCalcAmoebaAngleForceKernel::copyParametersToContext(ContextImpl& context, const AmoebaAngleForce& force) {
if (numAngles != force.getNumAngles())
throw OpenMMException("updateParametersInContext: The number of angles has changed");
// Record the values.
for (int i = 0; i < numAngles; ++i) {
int particle1Index, particle2Index, particle3Index;
double angleValue, k;
force.getAngleParameters(i, particle1Index, particle2Index, particle3Index, angleValue, k);
if (particle1Index != particle1[i] || particle2Index != particle2[i] || particle3Index != particle3[i])
throw OpenMMException("updateParametersInContext: The set of particles in an angle has changed");
angle[i] = angleValue;
kQuadratic[i] = k;
}
}
ReferenceCalcAmoebaInPlaneAngleForceKernel::ReferenceCalcAmoebaInPlaneAngleForceKernel(const std::string& name, const Platform& platform, const System& system) :
CalcAmoebaInPlaneAngleForceKernel(name, platform), system(system) {
}
ReferenceCalcAmoebaInPlaneAngleForceKernel::~ReferenceCalcAmoebaInPlaneAngleForceKernel() {
}
void ReferenceCalcAmoebaInPlaneAngleForceKernel::initialize(const System& system, const AmoebaInPlaneAngleForce& force) {
numAngles = force.getNumAngles();
for (int ii = 0; ii < numAngles; ii++) {
int particle1Index, particle2Index, particle3Index, particle4Index;
double angleValue, k;
force.getAngleParameters(ii, particle1Index, particle2Index, particle3Index, particle4Index, angleValue, k);
particle1.push_back(particle1Index);
particle2.push_back(particle2Index);
particle3.push_back(particle3Index);
particle4.push_back(particle4Index);
angle.push_back(angleValue);
kQuadratic.push_back(k);
}
globalInPlaneAngleCubic = force.getAmoebaGlobalInPlaneAngleCubic();
globalInPlaneAngleQuartic = force.getAmoebaGlobalInPlaneAngleQuartic();
globalInPlaneAnglePentic = force.getAmoebaGlobalInPlaneAnglePentic();
globalInPlaneAngleSextic = force.getAmoebaGlobalInPlaneAngleSextic();
usePeriodic = force.usesPeriodicBoundaryConditions();
}
double ReferenceCalcAmoebaInPlaneAngleForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
vector<Vec3>& posData = extractPositions(context);
vector<Vec3>& forceData = extractForces(context);
AmoebaReferenceInPlaneAngleForce amoebaReferenceInPlaneAngleForce;
if (usePeriodic)
amoebaReferenceInPlaneAngleForce.setPeriodic(extractBoxVectors(context));
double energy = amoebaReferenceInPlaneAngleForce.calculateForceAndEnergy(numAngles, posData, particle1, particle2, particle3, particle4,
angle, kQuadratic, globalInPlaneAngleCubic, globalInPlaneAngleQuartic,
globalInPlaneAnglePentic, globalInPlaneAngleSextic, forceData);
return static_cast<double>(energy);
}
void ReferenceCalcAmoebaInPlaneAngleForceKernel::copyParametersToContext(ContextImpl& context, const AmoebaInPlaneAngleForce& force) {
if (numAngles != force.getNumAngles())
throw OpenMMException("updateParametersInContext: The number of angles has changed");
// Record the values.
for (int i = 0; i < numAngles; ++i) {
int particle1Index, particle2Index, particle3Index, particle4Index;
double angleValue, k;
force.getAngleParameters(i, particle1Index, particle2Index, particle3Index, particle4Index, angleValue, k);
if (particle1Index != particle1[i] || particle2Index != particle2[i] || particle3Index != particle3[i] || particle4Index != particle4[i])
throw OpenMMException("updateParametersInContext: The set of particles in an angle has changed");
angle[i] = angleValue;
kQuadratic[i] = k;
}
}
ReferenceCalcAmoebaPiTorsionForceKernel::ReferenceCalcAmoebaPiTorsionForceKernel(const std::string& name, const Platform& platform, const System& system) :
CalcAmoebaPiTorsionForceKernel(name, platform), system(system) {
}
ReferenceCalcAmoebaPiTorsionForceKernel::~ReferenceCalcAmoebaPiTorsionForceKernel() {
}
void ReferenceCalcAmoebaPiTorsionForceKernel::initialize(const System& system, const AmoebaPiTorsionForce& force) {
numPiTorsions = force.getNumPiTorsions();
for (int ii = 0; ii < numPiTorsions; ii++) {
int particle1Index, particle2Index, particle3Index, particle4Index, particle5Index, particle6Index;
double kTorsionParameter;
force.getPiTorsionParameters(ii, particle1Index, particle2Index, particle3Index, particle4Index, particle5Index, particle6Index, kTorsionParameter);
particle1.push_back(particle1Index);
particle2.push_back(particle2Index);
particle3.push_back(particle3Index);
particle4.push_back(particle4Index);
particle5.push_back(particle5Index);
particle6.push_back(particle6Index);
kTorsion.push_back(kTorsionParameter);
}
usePeriodic = force.usesPeriodicBoundaryConditions();
}
double ReferenceCalcAmoebaPiTorsionForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
vector<Vec3>& posData = extractPositions(context);
vector<Vec3>& forceData = extractForces(context);
AmoebaReferencePiTorsionForce amoebaReferencePiTorsionForce;
if (usePeriodic)
amoebaReferencePiTorsionForce.setPeriodic(extractBoxVectors(context));
double energy = amoebaReferencePiTorsionForce.calculateForceAndEnergy(numPiTorsions, posData, particle1, particle2,
particle3, particle4, particle5, particle6,
kTorsion, forceData);
return static_cast<double>(energy);
}
void ReferenceCalcAmoebaPiTorsionForceKernel::copyParametersToContext(ContextImpl& context, const AmoebaPiTorsionForce& force) {
if (numPiTorsions != force.getNumPiTorsions())
throw OpenMMException("updateParametersInContext: The number of torsions has changed");
// Record the values.
for (int i = 0; i < numPiTorsions; ++i) {
int particle1Index, particle2Index, particle3Index, particle4Index, particle5Index, particle6Index;
double kTorsionParameter;
force.getPiTorsionParameters(i, particle1Index, particle2Index, particle3Index, particle4Index, particle5Index, particle6Index, kTorsionParameter);
if (particle1Index != particle1[i] || particle2Index != particle2[i] || particle3Index != particle3[i] ||
particle4Index != particle4[i] || particle5Index != particle5[i] || particle6Index != particle6[i])
throw OpenMMException("updateParametersInContext: The set of particles in a torsion has changed");
kTorsion[i] = kTorsionParameter;
}
}
ReferenceCalcAmoebaStretchBendForceKernel::ReferenceCalcAmoebaStretchBendForceKernel(const std::string& name, const Platform& platform, const System& system) :
CalcAmoebaStretchBendForceKernel(name, platform), system(system) {
}
ReferenceCalcAmoebaStretchBendForceKernel::~ReferenceCalcAmoebaStretchBendForceKernel() {
}
void ReferenceCalcAmoebaStretchBendForceKernel::initialize(const System& system, const AmoebaStretchBendForce& force) {
numStretchBends = force.getNumStretchBends();
for (int ii = 0; ii < numStretchBends; ii++) {
int particle1Index, particle2Index, particle3Index;
double lengthAB, lengthCB, angle, k1, k2;
force.getStretchBendParameters(ii, particle1Index, particle2Index, particle3Index, lengthAB, lengthCB, angle, k1, k2);
particle1.push_back(particle1Index);
particle2.push_back(particle2Index);
particle3.push_back(particle3Index);
lengthABParameters.push_back(lengthAB);
lengthCBParameters.push_back(lengthCB);
angleParameters.push_back(angle);
k1Parameters.push_back(k1);
k2Parameters.push_back(k2);
}
usePeriodic = force.usesPeriodicBoundaryConditions();
}
double ReferenceCalcAmoebaStretchBendForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
vector<Vec3>& posData = extractPositions(context);
vector<Vec3>& forceData = extractForces(context);
AmoebaReferenceStretchBendForce amoebaReferenceStretchBendForce;
if (usePeriodic)
amoebaReferenceStretchBendForce.setPeriodic(extractBoxVectors(context));
double energy = amoebaReferenceStretchBendForce.calculateForceAndEnergy(numStretchBends, posData, particle1, particle2, particle3,
lengthABParameters, lengthCBParameters, angleParameters, k1Parameters,
k2Parameters, forceData);
return static_cast<double>(energy);
}
void ReferenceCalcAmoebaStretchBendForceKernel::copyParametersToContext(ContextImpl& context, const AmoebaStretchBendForce& force) {
if (numStretchBends != force.getNumStretchBends())
throw OpenMMException("updateParametersInContext: The number of stretch-bends has changed");
// Record the values.
for (int i = 0; i < numStretchBends; ++i) {
int particle1Index, particle2Index, particle3Index;
double lengthAB, lengthCB, angle, k1, k2;
force.getStretchBendParameters(i, particle1Index, particle2Index, particle3Index, lengthAB, lengthCB, angle, k1, k2);
if (particle1Index != particle1[i] || particle2Index != particle2[i] || particle3Index != particle3[i])
throw OpenMMException("updateParametersInContext: The set of particles in a stretch-bend has changed");
lengthABParameters[i] = lengthAB;
lengthCBParameters[i] = lengthCB;
angleParameters[i] = angle;
k1Parameters[i] = k1;
k2Parameters[i] = k2;
}
}
ReferenceCalcAmoebaOutOfPlaneBendForceKernel::ReferenceCalcAmoebaOutOfPlaneBendForceKernel(const std::string& name, const Platform& platform, const System& system) :
CalcAmoebaOutOfPlaneBendForceKernel(name, platform), system(system) {
}
ReferenceCalcAmoebaOutOfPlaneBendForceKernel::~ReferenceCalcAmoebaOutOfPlaneBendForceKernel() {
}
void ReferenceCalcAmoebaOutOfPlaneBendForceKernel::initialize(const System& system, const AmoebaOutOfPlaneBendForce& force) {
numOutOfPlaneBends = force.getNumOutOfPlaneBends();
for (int ii = 0; ii < numOutOfPlaneBends; ii++) {
int particle1Index, particle2Index, particle3Index, particle4Index;
double k;
force.getOutOfPlaneBendParameters(ii, particle1Index, particle2Index, particle3Index, particle4Index, k);
particle1.push_back(particle1Index);
particle2.push_back(particle2Index);
particle3.push_back(particle3Index);
particle4.push_back(particle4Index);
kParameters.push_back(k);
}
globalOutOfPlaneBendAngleCubic = force.getAmoebaGlobalOutOfPlaneBendCubic();
globalOutOfPlaneBendAngleQuartic = force.getAmoebaGlobalOutOfPlaneBendQuartic();
globalOutOfPlaneBendAnglePentic = force.getAmoebaGlobalOutOfPlaneBendPentic();
globalOutOfPlaneBendAngleSextic = force.getAmoebaGlobalOutOfPlaneBendSextic();
usePeriodic = force.usesPeriodicBoundaryConditions();
}
double ReferenceCalcAmoebaOutOfPlaneBendForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
vector<Vec3>& posData = extractPositions(context);
vector<Vec3>& forceData = extractForces(context);
AmoebaReferenceOutOfPlaneBendForce amoebaReferenceOutOfPlaneBendForce;
if (usePeriodic)
amoebaReferenceOutOfPlaneBendForce.setPeriodic(extractBoxVectors(context));
double energy = amoebaReferenceOutOfPlaneBendForce.calculateForceAndEnergy(numOutOfPlaneBends, posData,
particle1, particle2, particle3, particle4,
kParameters,
globalOutOfPlaneBendAngleCubic,
globalOutOfPlaneBendAngleQuartic,
globalOutOfPlaneBendAnglePentic,
globalOutOfPlaneBendAngleSextic, forceData);
return static_cast<double>(energy);
}
void ReferenceCalcAmoebaOutOfPlaneBendForceKernel::copyParametersToContext(ContextImpl& context, const AmoebaOutOfPlaneBendForce& force) {
if (numOutOfPlaneBends != force.getNumOutOfPlaneBends())
throw OpenMMException("updateParametersInContext: The number of out-of-plane bends has changed");
// Record the values.
for (int i = 0; i < numOutOfPlaneBends; ++i) {
int particle1Index, particle2Index, particle3Index, particle4Index;
double k;
force.getOutOfPlaneBendParameters(i, particle1Index, particle2Index, particle3Index, particle4Index, k);
if (particle1Index != particle1[i] || particle2Index != particle2[i] || particle3Index != particle3[i] || particle4Index != particle4[i])
throw OpenMMException("updateParametersInContext: The set of particles in an out-of-plane bend has changed");
kParameters[i] = k;
}
}
ReferenceCalcAmoebaTorsionTorsionForceKernel::ReferenceCalcAmoebaTorsionTorsionForceKernel(const std::string& name, const Platform& platform, const System& system) :
CalcAmoebaTorsionTorsionForceKernel(name, platform), system(system) {
}
......
......@@ -39,273 +39,6 @@
namespace OpenMM {
/**
* This kernel is invoked by AmoebaBondForce to calculate the forces acting on the system and the energy of the system.
*/
class ReferenceCalcAmoebaBondForceKernel : public CalcAmoebaBondForceKernel {
public:
ReferenceCalcAmoebaBondForceKernel(const std::string& name,
const Platform& platform,
const System& system);
~ReferenceCalcAmoebaBondForceKernel();
/**
* 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:
int numBonds;
std::vector<int> particle1;
std::vector<int> particle2;
std::vector<double> length;
std::vector<double> kQuadratic;
double globalBondCubic;
double globalBondQuartic;
const System& system;
bool usePeriodic;
};
/**
* This kernel is invoked by AmoebaAngleForce to calculate the forces acting on the system and the energy of the system.
*/
class ReferenceCalcAmoebaAngleForceKernel : public CalcAmoebaAngleForceKernel {
public:
ReferenceCalcAmoebaAngleForceKernel(const std::string& name, const Platform& platform, const System& system);
~ReferenceCalcAmoebaAngleForceKernel();
/**
* 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:
int numAngles;
std::vector<int> particle1;
std::vector<int> particle2;
std::vector<int> particle3;
std::vector<double> angle;
std::vector<double> kQuadratic;
double globalAngleCubic;
double globalAngleQuartic;
double globalAnglePentic;
double globalAngleSextic;
const System& system;
bool usePeriodic;
};
/**
* This kernel is invoked by AmoebaInPlaneAngleForce to calculate the forces acting on the system and the energy of the system.
*/
class ReferenceCalcAmoebaInPlaneAngleForceKernel : public CalcAmoebaInPlaneAngleForceKernel {
public:
ReferenceCalcAmoebaInPlaneAngleForceKernel(const std::string& name, const Platform& platform, const System& system);
~ReferenceCalcAmoebaInPlaneAngleForceKernel();
/**
* 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:
int numAngles;
std::vector<int> particle1;
std::vector<int> particle2;
std::vector<int> particle3;
std::vector<int> particle4;
std::vector<double> angle;
std::vector<double> kQuadratic;
double globalInPlaneAngleCubic;
double globalInPlaneAngleQuartic;
double globalInPlaneAnglePentic;
double globalInPlaneAngleSextic;
const System& system;
bool usePeriodic;
};
/**
* This kernel is invoked by AmoebaPiTorsionForce to calculate the forces acting on the system and the energy of the system.
*/
class ReferenceCalcAmoebaPiTorsionForceKernel : public CalcAmoebaPiTorsionForceKernel {
public:
ReferenceCalcAmoebaPiTorsionForceKernel(const std::string& name, const Platform& platform, const System& system);
~ReferenceCalcAmoebaPiTorsionForceKernel();
/**
* 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:
int numPiTorsions;
std::vector<int> particle1;
std::vector<int> particle2;
std::vector<int> particle3;
std::vector<int> particle4;
std::vector<int> particle5;
std::vector<int> particle6;
std::vector<double> kTorsion;
const System& system;
bool usePeriodic;
};
/**
* This kernel is invoked by AmoebaStretchBendForce to calculate the forces acting on the system and the energy of the system.
*/
class ReferenceCalcAmoebaStretchBendForceKernel : public CalcAmoebaStretchBendForceKernel {
public:
ReferenceCalcAmoebaStretchBendForceKernel(const std::string& name, const Platform& platform, const System& system);
~ReferenceCalcAmoebaStretchBendForceKernel();
/**
* 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:
int numStretchBends;
std::vector<int> particle1;
std::vector<int> particle2;
std::vector<int> particle3;
std::vector<double> lengthABParameters;
std::vector<double> lengthCBParameters;
std::vector<double> angleParameters;
std::vector<double> k1Parameters;
std::vector<double> k2Parameters;
const System& system;
bool usePeriodic;
};
/**
* This kernel is invoked by AmoebaOutOfPlaneBendForce to calculate the forces acting on the system and the energy of the system.
*/
class ReferenceCalcAmoebaOutOfPlaneBendForceKernel : public CalcAmoebaOutOfPlaneBendForceKernel {
public:
ReferenceCalcAmoebaOutOfPlaneBendForceKernel(const std::string& name, const Platform& platform, const System& system);
~ReferenceCalcAmoebaOutOfPlaneBendForceKernel();
/**
* 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:
int numOutOfPlaneBends;
std::vector<int> particle1;
std::vector<int> particle2;
std::vector<int> particle3;
std::vector<int> particle4;
std::vector<double> kParameters;
double globalOutOfPlaneBendAngleCubic;
double globalOutOfPlaneBendAngleQuartic;
double globalOutOfPlaneBendAnglePentic;
double globalOutOfPlaneBendAngleSextic;
const System& system;
bool usePeriodic;
};
/**
* This kernel is invoked by AmoebaTorsionTorsionForce to calculate the forces acting on the system and the energy of the system.
*/
......
/* Portions copyright (c) 2006-2016 Stanford University and Simbios.
* Contributors: Pande Group
*
* 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 "AmoebaReferenceForce.h"
#include "AmoebaReferenceAngleForce.h"
#include "SimTKOpenMMRealType.h"
using std::vector;
using namespace OpenMM;
void AmoebaReferenceAngleForce::setPeriodic(OpenMM::Vec3* vectors) {
usePeriodic = true;
boxVectors[0] = vectors[0];
boxVectors[1] = vectors[1];
boxVectors[2] = vectors[2];
}
/**---------------------------------------------------------------------------------------
Get dEdT and energy prefactor given cosine of angle :: the calculation for different
the force types is identical
@param cosine cosine of angle
@param idealAngle ideal angle
@param angleK angle k (quadratic prefactor)
@param angleCubic cubic prefactor
@param angleQuartic quartiic prefactor
@param anglePentic pentic prefactor
@param angleSextic sextic prefactor
@param dEdR dEdR
@return energy
--------------------------------------------------------------------------------------- */
double AmoebaReferenceAngleForce::getPrefactorsGivenAngleCosine(double cosine,
double idealAngle, double angleK,
double angleCubic, double angleQuartic,
double anglePentic, double angleSextic,
double* dEdR) const {
double angle;
if (cosine >= 1.0) {
angle = 0.0;
} 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;
*dEdR = (2.0 + 3.0*angleCubic*deltaIdeal +
4.0*angleQuartic*deltaIdeal2 +
5.0*anglePentic*deltaIdeal3 +
6.0*angleSextic*deltaIdeal4);
*dEdR *= RADIAN*angleK*deltaIdeal;
double energy = 1.0f + angleCubic*deltaIdeal + angleQuartic*deltaIdeal2 +
anglePentic*deltaIdeal3 + angleSextic*deltaIdeal4;
energy *= angleK*deltaIdeal2;
return energy;
}
/**---------------------------------------------------------------------------------------
Calculate Amoeba angle ixn (force and energy)
@param positionAtomA Cartesian coordinates of atom A
@param positionAtomB Cartesian coordinates of atom B
@param positionAtomC Cartesian coordinates of atom C
@param angleLength angle
@param angleK quadratic angle force
@param angleCubic cubic angle force parameter
@param angleQuartic quartic angle force parameter
@param anglePentic pentic angle force parameter
@param angleSextic sextic angle force parameter
@param forces force vector
@return energy
--------------------------------------------------------------------------------------- */
double AmoebaReferenceAngleForce::calculateAngleIxn(const Vec3& positionAtomA, const Vec3& positionAtomB,
const Vec3& positionAtomC,
double angle, double angleK,
double angleCubic, double angleQuartic,
double anglePentic, double angleSextic,
Vec3* forces) const {
std::vector<double> deltaR[2];
if (usePeriodic)
AmoebaReferenceForce::loadDeltaRPeriodic(positionAtomA, positionAtomB, deltaR[0], boxVectors);
else
AmoebaReferenceForce::loadDeltaR(positionAtomA, positionAtomB, deltaR[0]);
double rAB2 = AmoebaReferenceForce::getNormSquared3(deltaR[0]);
double rAB = SQRT(rAB2);
if (usePeriodic)
AmoebaReferenceForce::loadDeltaRPeriodic(positionAtomC, positionAtomB, deltaR[1], boxVectors);
else
AmoebaReferenceForce::loadDeltaR(positionAtomC, positionAtomB, deltaR[1]);
double rCB2 = AmoebaReferenceForce::getNormSquared3(deltaR[1]);
double rCB = SQRT(rCB2);
if (rAB <= 0.0 || rCB <= 0.0) {
return 0.0;
}
std::vector<double> pVector(3);
AmoebaReferenceForce::getCrossProduct(deltaR[0], deltaR[1], pVector);
double rp = AmoebaReferenceForce::getNorm3(pVector);
if (rp < 1.0e-06) {
rp = 1.0e-06;
}
double dot = AmoebaReferenceForce::getDotProduct3(deltaR[0], deltaR[1]);
double cosine = dot/(rAB*rCB);
double dEdR;
double energy = getPrefactorsGivenAngleCosine(cosine, angle, angleK, angleCubic, angleQuartic,
anglePentic, angleSextic, &dEdR);
double termA = dEdR/(rAB2*rp);
double termC = -dEdR/(rCB2*rp);
std::vector<double> deltaCrossP[3];
deltaCrossP[0].resize(3);
deltaCrossP[1].resize(3);
deltaCrossP[2].resize(3);
AmoebaReferenceForce::getCrossProduct(deltaR[0], pVector, deltaCrossP[0]);
AmoebaReferenceForce::getCrossProduct(deltaR[1], pVector, deltaCrossP[2]);
for (unsigned int ii = 0; ii < 3; ii++) {
deltaCrossP[0][ii] *= termA;
deltaCrossP[2][ii] *= termC;
deltaCrossP[1][ii] = -1.0f*(deltaCrossP[0][ii] + deltaCrossP[2][ii]);
}
// accumulate forces
for (int jj = 0; jj < 3; jj++) {
forces[jj][0] = deltaCrossP[jj][0];
forces[jj][1] = deltaCrossP[jj][1];
forces[jj][2] = deltaCrossP[jj][2];
}
return energy;
}
double AmoebaReferenceAngleForce::calculateForceAndEnergy(int numAngles, vector<Vec3>& posData,
const std::vector<int>& particle1,
const std::vector<int>& particle2,
const std::vector<int>& particle3,
const std::vector<double>& angle,
const std::vector<double>& kQuadratic,
double angleCubic,
double angleQuartic,
double anglePentic,
double angleSextic,
vector<Vec3>& forceData) const {
double energy = 0.0;
for (unsigned int ii = 0; ii < static_cast<unsigned int>(numAngles); ii++) {
int particle1Index = particle1[ii];
int particle2Index = particle2[ii];
int particle3Index = particle3[ii];
double idealAngle = angle[ii];
double angleK = kQuadratic[ii];
Vec3 forces[3];
energy += calculateAngleIxn(posData[particle1Index], posData[particle2Index], posData[particle3Index],
idealAngle, angleK, angleCubic, angleQuartic, anglePentic, angleSextic, forces);
for (unsigned int jj = 0; jj < 3; jj++) {
forceData[particle1Index][jj] += forces[0][jj];
forceData[particle2Index][jj] += forces[1][jj];
forceData[particle3Index][jj] += forces[2][jj];
}
}
return energy;
}
/* Portions copyright (c) 2006-2016 Stanford University and Simbios.
* Contributors: Pande Group
*
* 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.
*/
#ifndef __AmoebaReferenceAngleForce_H__
#define __AmoebaReferenceAngleForce_H__
#include "openmm/Vec3.h"
#include <vector>
namespace OpenMM {
class AmoebaReferenceAngleForce {
public:
/**---------------------------------------------------------------------------------------
Constructor
--------------------------------------------------------------------------------------- */
AmoebaReferenceAngleForce() : usePeriodic(false) {};
/**---------------------------------------------------------------------------------------
Destructor
--------------------------------------------------------------------------------------- */
~AmoebaReferenceAngleForce() {};
/**---------------------------------------------------------------------------------------
Set the force to use periodic boundary conditions.
@param vectors the vectors defining the periodic box
--------------------------------------------------------------------------------------- */
void setPeriodic(OpenMM::Vec3* vectors);
/**---------------------------------------------------------------------------------------
Calculate Amoeba angle ixns (force and energy)
@param numBonds number of angles
@param posData particle positions
@param particle1 particle 1 indices
@param particle2 particle 2 indices
@param particle3 particle 3 indices
@param angle ideal angle
@param angleK angle force constant
@param angleCubic cubic force parameter
@param angleQuartic quartic force parameter
@param anglePentic pentic force parameter
@param angleSexic sextic force parameter
@param forces output force vector
@return total energy
--------------------------------------------------------------------------------------- */
double calculateForceAndEnergy(int numAngles, std::vector<OpenMM::Vec3>& posData,
const std::vector<int>& particle1,
const std::vector<int>& particle2,
const std::vector<int>& particle3,
const std::vector<double>& angle,
const std::vector<double>& kQuadratic,
double globalAngleCubic,
double globalAngleQuartic,
double globalAnglePentic,
double globalAngleSextic,
std::vector<OpenMM::Vec3>& forceData) const;
private:
bool usePeriodic;
Vec3 boxVectors[3];
/**---------------------------------------------------------------------------------------
Get dEdT and energy prefactor given cosine of angle :: the calculation for different
the force types is identical
@param cosine cosine of angle
@param idealAngle ideal angle
@param angleK angle k (quadratic prefactor)
@param angleCubic cubic prefactor
@param angleQuartic quartiic prefactor
@param anglePentic pentic prefactor
@param angleSextic sextic prefactor
@param dEdR dEdR
@return energy
--------------------------------------------------------------------------------------- */
double getPrefactorsGivenAngleCosine(double cosine, double idealAngle, double angleK,
double angleCubic, double angleQuartic,
double anglePentic, double angleSextic,
double* dEdR) const;
/**---------------------------------------------------------------------------------------
Calculate Amoeba angle ixn (force and energy)
@param positionAtomA Cartesian coordinates of atom A
@param positionAtomB Cartesian coordinates of atom B
@param positionAtomC Cartesian coordinates of atom C
@param angleLength angle
@param angleK quadratic angle force
@param angleCubic cubic angle force parameter
@param angleQuartic quartic angle force parameter
@param anglePentic pentic angle force parameter
@param angleSextic sextic angle force parameter
@param forces force vector
@return energy
--------------------------------------------------------------------------------------- */
double calculateAngleIxn(const OpenMM::Vec3& positionAtomA, const OpenMM::Vec3& positionAtomB,
const OpenMM::Vec3& positionAtomC,
double angle, double angleK,
double angleCubic, double angleQuartic,
double anglePentic, double angleSextic,
OpenMM::Vec3* forces) const;
};
} // namespace OpenMM
#endif // _AmoebaReferenceAngleForce___
/* Portions copyright (c) 2006 Stanford University and Simbios.
* Contributors: Pande Group
*
* 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 "AmoebaReferenceBondForce.h"
#include "AmoebaReferenceForce.h"
using std::vector;
using namespace OpenMM;
void AmoebaReferenceBondForce::setPeriodic(OpenMM::Vec3* vectors) {
usePeriodic = true;
boxVectors[0] = vectors[0];
boxVectors[1] = vectors[1];
boxVectors[2] = vectors[2];
}
/**---------------------------------------------------------------------------------------
Calculate Amoeba bond ixn (force and energy)
@param positionAtomA Cartesian coordinates of atom A
@param positionAtomB Cartesian coordinates of atom B
@param bondLength bond length
@param bondK bond force
@param bondCubic cubic bond force parameter
@param bondQuartic quartic bond force parameter
@param forces force vector
@return energy
--------------------------------------------------------------------------------------- */
double AmoebaReferenceBondForce::calculateBondIxn(const Vec3& positionAtomA, const Vec3& positionAtomB,
double bondLength, double bondK,
double bondCubic, double bondQuartic,
Vec3* forces) const {
// get deltaR, R2, and R between 2 atoms
std::vector<double> deltaR;
if (usePeriodic)
AmoebaReferenceForce::loadDeltaRPeriodic(positionAtomA, positionAtomB, deltaR, boxVectors);
else
AmoebaReferenceForce::loadDeltaR(positionAtomA, positionAtomB, deltaR);
double r = AmoebaReferenceForce::getNorm3(deltaR);
// deltaIdeal = r - r_0
double deltaIdeal = r - bondLength;
double deltaIdeal2 = deltaIdeal*deltaIdeal;
double dEdR = (1.0 + 1.5*bondCubic*deltaIdeal + 2.0*bondQuartic*deltaIdeal2);
dEdR *= 2.0*bondK*deltaIdeal;
dEdR = r > 0.0 ? (dEdR/r) : 0.0;
forces[0][0] = dEdR*deltaR[0];
forces[0][1] = dEdR*deltaR[1];
forces[0][2] = dEdR*deltaR[2];
dEdR *= -1.0;
forces[1][0] = dEdR*deltaR[0];
forces[1][1] = dEdR*deltaR[1];
forces[1][2] = dEdR*deltaR[2];
double energy = bondK*deltaIdeal2*(1.0 + bondCubic*deltaIdeal + bondQuartic*deltaIdeal2);
return energy;
}
double AmoebaReferenceBondForce::calculateForceAndEnergy(int numBonds,
vector<Vec3>& particlePositions,
const std::vector<int>& particle1,
const std::vector<int>& particle2,
const std::vector<double>& length,
const std::vector<double>& kQuadratic,
double globalBondCubic,
double globalBondQuartic,
vector<Vec3>& forceData) const {
double energy = 0.0;
for (int ii = 0; ii < numBonds; ii++) {
int particle1Index = particle1[ii];
int particle2Index = particle2[ii];
double bondLength = length[ii];
double bondK = kQuadratic[ii];
Vec3 forces[2];
energy += calculateBondIxn(particlePositions[particle1Index], particlePositions[particle2Index],
bondLength, bondK, globalBondCubic, globalBondQuartic,
forces);
for (int jj = 0; jj < 3; jj++) {
forceData[particle1Index][jj] += forces[0][jj];
forceData[particle2Index][jj] += forces[1][jj];
}
}
return energy;
}
/* Portions copyright (c) 2006-2016 Stanford University and Simbios.
* Contributors: Pande Group
*
* 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.
*/
#ifndef __AmoebaReferenceBondForce_H__
#define __AmoebaReferenceBondForce_H__
#include "openmm/Vec3.h"
#include <vector>
namespace OpenMM {
class AmoebaReferenceBondForce {
public:
/**---------------------------------------------------------------------------------------
Constructor
--------------------------------------------------------------------------------------- */
AmoebaReferenceBondForce() : usePeriodic(false) {};
/**---------------------------------------------------------------------------------------
Destructor
--------------------------------------------------------------------------------------- */
~AmoebaReferenceBondForce() {};
/**---------------------------------------------------------------------------------------
Set the force to use periodic boundary conditions.
@param vectors the vectors defining the periodic box
--------------------------------------------------------------------------------------- */
void setPeriodic(OpenMM::Vec3* vectors);
/**---------------------------------------------------------------------------------------
Calculate Amoeba bond ixns (force and energy)
@param numBonds number of bonds
@param posData particle positions
@param particle1 particle 1 indices
@param particle2 particle 2 indices
@param bondLength bond length
@param bondK bond force
@param bondCubic cubic bond force parameter
@param bondQuartic quartic bond force parameter
@param forces output force vector
@return total energy
--------------------------------------------------------------------------------------- */
double calculateForceAndEnergy(int numBonds, std::vector<OpenMM::Vec3>& posData,
const std::vector<int>& particle1,
const std::vector<int>& particle2,
const std::vector<double>& bondLength,
const std::vector<double>& bondK,
double bondCubic, double bondQuartic,
std::vector<OpenMM::Vec3>& forceData) const;
private:
bool usePeriodic;
Vec3 boxVectors[3];
/**---------------------------------------------------------------------------------------
Calculate Amoeba bond ixns (force and energy)
@param positionAtomA Cartesian coordinates of atom A
@param positionAtomB Cartesian coordinates of atom B
@param bondLength bond length
@param bondK bond force
@param bondCubic cubic bond force parameter
@param bondQuartic quartic bond force parameter
@param forces force vector
@return energy
--------------------------------------------------------------------------------------- */
double calculateBondIxn(const OpenMM::Vec3& positionAtomA, const OpenMM::Vec3& positionAtomB,
double bondLength, double bondK,
double bondCubic, double bondQuartic,
OpenMM::Vec3* forces) const;
};
} // namespace OpenMM
#endif // _AmoebaReferenceBondForce___
/* Portions copyright (c) 2006 Stanford University and Simbios.
* Contributors: Pande Group
*
* 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 "AmoebaReferenceForce.h"
#include "AmoebaReferenceInPlaneAngleForce.h"
#include "ReferenceForce.h"
#include "SimTKOpenMMRealType.h"
using std::vector;
using namespace OpenMM;
void AmoebaReferenceInPlaneAngleForce::setPeriodic(OpenMM::Vec3* vectors) {
usePeriodic = true;
boxVectors[0] = vectors[0];
boxVectors[1] = vectors[1];
boxVectors[2] = vectors[2];
}
/**---------------------------------------------------------------------------------------
Get dEdT and energy prefactor given cosine of angle :: the calculation for different
the force types is identical
@param cosine cosine of angle
@param idealAngle ideal angle
@param angleK angle k (quadratic prefactor)
@param angleCubic cubic prefactor
@param angleQuartic quartiic prefactor
@param anglePentic pentic prefactor
@param angleSextic sextic prefactor
@param dEdR dEdR
@return energy
--------------------------------------------------------------------------------------- */
double AmoebaReferenceInPlaneAngleForce::getPrefactorsGivenAngleCosine(double cosine,
double idealAngle, double angleK,
double angleCubic, double angleQuartic,
double anglePentic, double angleSextic,
double* dEdR) const {
double angle;
if (cosine >= 1.0) {
angle = 0.0;
} 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;
*dEdR = (2.0 + 3.0*angleCubic*deltaIdeal +
4.0*angleQuartic*deltaIdeal2 +
5.0*anglePentic*deltaIdeal3 +
6.0*angleSextic*deltaIdeal4);
*dEdR *= RADIAN*angleK*deltaIdeal;
double energy = 1.0f + angleCubic*deltaIdeal + angleQuartic*deltaIdeal2 +
anglePentic*deltaIdeal3 + angleSextic*deltaIdeal4;
energy *= angleK*deltaIdeal2;
return energy;
}
/**---------------------------------------------------------------------------------------
Calculate Amoeba angle ixn (force and energy)
@param positionAtomA Cartesian coordinates of atom A
@param positionAtomB Cartesian coordinates of atom B
@param positionAtomC Cartesian coordinates of atom C
@param positionAtomD Cartesian coordinates of atom D
@param angleLength angle
@param angleK quadratic angle force
@param angleCubic cubic angle force parameter
@param angleQuartic quartic angle force parameter
@param anglePentic pentic angle force parameter
@param angleSextic sextic angle force parameter
@param forces force vector
@return energy
--------------------------------------------------------------------------------------- */
double AmoebaReferenceInPlaneAngleForce::calculateAngleIxn(const Vec3& positionAtomA, const Vec3& positionAtomB,
const Vec3& positionAtomC, const Vec3& positionAtomD,
double angle, double angleK,
double angleCubic, double angleQuartic,
double anglePentic, double angleSextic,
Vec3* forces) const {
// T = AD x CD
// P = B + T*delta
// AP = A - P
// CP = A - P
// M = CP x AP
enum { AD, BD, CD, T, AP, P, CP, M, APxM, CPxM, ADxBD, BDxCD, TxCD, ADxT, dBxAD, CDxdB, LastDeltaAtomIndex };
Vec3 deltaR[LastDeltaAtomIndex];
if (usePeriodic) {
deltaR[AD] = ReferenceForce::getDeltaRPeriodic(positionAtomD, positionAtomA, boxVectors);
deltaR[BD] = ReferenceForce::getDeltaRPeriodic(positionAtomD, positionAtomB, boxVectors);
deltaR[CD] = ReferenceForce::getDeltaRPeriodic(positionAtomD, positionAtomC, boxVectors);
}
else {
deltaR[AD] = ReferenceForce::getDeltaR(positionAtomD, positionAtomA);
deltaR[BD] = ReferenceForce::getDeltaR(positionAtomD, positionAtomB);
deltaR[CD] = ReferenceForce::getDeltaR(positionAtomD, positionAtomC);
}
deltaR[T] = deltaR[AD].cross(deltaR[CD]);
double rT2 = deltaR[T].dot(deltaR[T]);
double delta = -deltaR[T].dot(deltaR[BD])/rT2;
deltaR[P] = positionAtomB + deltaR[T]*delta;
if (usePeriodic) {
deltaR[AP] = ReferenceForce::getDeltaRPeriodic(deltaR[P], positionAtomA, boxVectors);
deltaR[CP] = ReferenceForce::getDeltaRPeriodic(deltaR[P], positionAtomC, boxVectors);
}
else {
deltaR[AP] = ReferenceForce::getDeltaR(deltaR[P], positionAtomA);
deltaR[CP] = ReferenceForce::getDeltaR(deltaR[P], positionAtomC);
}
double rAp2 = deltaR[AP].dot(deltaR[AP]);
double rCp2 = deltaR[CP].dot(deltaR[CP]);
if (rAp2 <= 0 && rCp2 <= 0) {
return 0;
}
deltaR[M] = deltaR[CP].cross(deltaR[AP]);
double rm = sqrt(deltaR[M].dot(deltaR[M]));
if (rm < 1.0e-06) {
rm = 1.0e-06;
}
double dot = deltaR[AP].dot(deltaR[CP]);
double cosine = dot/SQRT(rAp2*rCp2);
double dEdR;
double energy = getPrefactorsGivenAngleCosine(cosine, angle, angleK, angleCubic, angleQuartic,
anglePentic, angleSextic, &dEdR);
double termA = -dEdR/(rAp2*rm);
double termC = dEdR/(rCp2*rm);
deltaR[APxM] = deltaR[AP].cross(deltaR[M]);
deltaR[CPxM] = deltaR[CP].cross(deltaR[M]);
// forces will be gathered here
enum { dA, dB, dC, dD, LastDIndex };
Vec3 forceTerm[LastDIndex];
forceTerm[dA] = deltaR[APxM]*termA;
forceTerm[dC] = deltaR[CPxM]*termC;
forceTerm[dB] = -(forceTerm[dA] + forceTerm[dC]);
double pTrT2 = forceTerm[dB].dot(deltaR[T]);
pTrT2 /= rT2;
deltaR[CDxdB] = deltaR[CD].cross(forceTerm[dB]);
deltaR[dBxAD] = forceTerm[dB].cross(deltaR[AD]);
if (FABS(pTrT2) > 1.0e-08) {
double delta2 = delta*2;
deltaR[BDxCD] = forceTerm[dB].cross(deltaR[CD]);
deltaR[TxCD] = forceTerm[T].cross(deltaR[CD]);
deltaR[ADxBD] = forceTerm[AD].cross(deltaR[BD]);
deltaR[ADxT] = forceTerm[AD].cross(deltaR[T]);
Vec3 term = deltaR[BDxCD] + deltaR[TxCD]*delta2;
forceTerm[dA] += deltaR[CDxdB]*delta + term*pTrT2;
term = deltaR[ADxBD] + deltaR[ADxT]*delta2;
forceTerm[dC] += deltaR[dBxAD]*delta + term*pTrT2;
forceTerm[dD] = -(forceTerm[dA] + forceTerm[dB] + forceTerm[dC]);
} else {
forceTerm[dA] += deltaR[CDxdB]*delta;
forceTerm[dC] += deltaR[dBxAD]*delta;
forceTerm[dD] = -(forceTerm[dA] + forceTerm[dB] + forceTerm[dC]);
}
// accumulate forces
for (int jj = 0; jj < 4; jj++)
forces[jj] = forceTerm[jj];
return energy;
}
double AmoebaReferenceInPlaneAngleForce::calculateForceAndEnergy(int numAngles, vector<Vec3>& posData,
const std::vector<int>& particle1,
const std::vector<int>& particle2,
const std::vector<int>& particle3,
const std::vector<int>& particle4,
const std::vector<double>& angle,
const std::vector<double>& kQuadratic,
double angleCubic,
double angleQuartic,
double anglePentic,
double angleSextic,
vector<Vec3>& forceData) const {
double energy = 0.0;
for (unsigned int ii = 0; ii < static_cast<unsigned int>(numAngles); ii++) {
int particle1Index = particle1[ii];
int particle2Index = particle2[ii];
int particle3Index = particle3[ii];
int particle4Index = particle4[ii];
double idealAngle = angle[ii];
double angleK = kQuadratic[ii];
Vec3 forces[4];
energy += calculateAngleIxn(posData[particle1Index], posData[particle2Index], posData[particle3Index], posData[particle4Index],
idealAngle, angleK, angleCubic, angleQuartic, anglePentic, angleSextic, forces);
// accumulate forces
for (int jj = 0; jj < 3; jj++) {
forceData[particle1Index][jj] -= forces[0][jj];
forceData[particle2Index][jj] -= forces[1][jj];
forceData[particle3Index][jj] -= forces[2][jj];
forceData[particle4Index][jj] -= forces[3][jj];
}
}
return energy;
}
/* Portions copyright (c) 2006-2016 Stanford University and Simbios.
* Contributors: Pande Group
*
* 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.
*/
#ifndef __AmoebaReferenceInPlaneAngleForce_H__
#define __AmoebaReferenceInPlaneAngleForce_H__
#include "openmm/Vec3.h"
#include <vector>
namespace OpenMM {
class AmoebaReferenceInPlaneAngleForce {
public:
/**---------------------------------------------------------------------------------------
Constructor
--------------------------------------------------------------------------------------- */
AmoebaReferenceInPlaneAngleForce() : usePeriodic(false) {};
/**---------------------------------------------------------------------------------------
Destructor
--------------------------------------------------------------------------------------- */
~AmoebaReferenceInPlaneAngleForce() {};
/**---------------------------------------------------------------------------------------
Set the force to use periodic boundary conditions.
@param vectors the vectors defining the periodic box
--------------------------------------------------------------------------------------- */
void setPeriodic(OpenMM::Vec3* vectors);
/**---------------------------------------------------------------------------------------
Calculate Amoeba in-plane angle ixns (force and energy)
@param numBonds number of angles
@param posData particle positions
@param particle1 particle 1 indices
@param particle2 particle 2 indices
@param particle3 particle 3 indices
@param particle4 particle 4 indices
@param angle ideal angle
@param angleK angle force constant
@param angleCubic cubic force parameter
@param angleQuartic quartic force parameter
@param anglePentic pentic force parameter
@param angleSexic sextic force parameter
@param forces output force vector
@return total energy
--------------------------------------------------------------------------------------- */
double calculateForceAndEnergy(int numAngles, std::vector<OpenMM::Vec3>& posData,
const std::vector<int>& particle1,
const std::vector<int>& particle2,
const std::vector<int>& particle3,
const std::vector<int>& particle4,
const std::vector<double>& angle,
const std::vector<double>& kQuadratic,
double globalAngleCubic,
double globalAngleQuartic,
double globalAnglePentic,
double globalAngleSextic,
std::vector<OpenMM::Vec3>& forceData) const;
private:
bool usePeriodic;
Vec3 boxVectors[3];
/**---------------------------------------------------------------------------------------
Get dEdT and energy prefactor given cosine of angle :: the calculation for different
the force types is identical
@param cosine cosine of angle
@param idealAngle ideal angle
@param angleK angle k (quadratic prefactor)
@param angleCubic cubic prefactor
@param angleQuartic quartiic prefactor
@param anglePentic pentic prefactor
@param angleSextic sextic prefactor
@param dEdR dEdR
@return energy
--------------------------------------------------------------------------------------- */
double getPrefactorsGivenAngleCosine(double cosine, double idealAngle, double angleK,
double angleCubic, double angleQuartic,
double anglePentic, double angleSextic,
double* dEdR) const;
/**---------------------------------------------------------------------------------------
Calculate Amoeba angle ixn (force and energy)
@param positionAtomA Cartesian coordinates of atom A
@param positionAtomB Cartesian coordinates of atom B
@param positionAtomC Cartesian coordinates of atom C
@param positionAtomD Cartesian coordinates of atom D
@param angle angle
@param angleK quadratic angle force parameter
@param angleCubic cubic angle force parameter
@param angleQuartic quartic angle force parameter
@param anglePentic pentic angle force parameter
@param angleSextic sextic angle force parameter
@param forces force vector
@return energy
--------------------------------------------------------------------------------------- */
double calculateAngleIxn(const OpenMM::Vec3& positionAtomA, const OpenMM::Vec3& positionAtomB,
const OpenMM::Vec3& positionAtomC, const OpenMM::Vec3& positionAtomD,
double angle, double angleK,
double angleCubic, double angleQuartic,
double anglePentic, double angleSextic,
OpenMM::Vec3* forces) const;
};
} // namespace OpenMM
#endif // _AmoebaReferenceInPlaneAngleForce___
/* Portions copyright (c) 2006 Stanford University and Simbios.
* Contributors: Pande Group
*
* 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 "AmoebaReferenceForce.h"
#include "AmoebaReferenceOutOfPlaneBendForce.h"
#include "SimTKOpenMMRealType.h"
using std::vector;
using namespace OpenMM;
void AmoebaReferenceOutOfPlaneBendForce::setPeriodic(OpenMM::Vec3* vectors) {
usePeriodic = true;
boxVectors[0] = vectors[0];
boxVectors[1] = vectors[1];
boxVectors[2] = vectors[2];
}
/**---------------------------------------------------------------------------------------
Calculate Amoeba Out-Of-Plane-Bend ixn (force and energy)
@param positionAtomA Cartesian coordinates of atom A
@param positionAtomB Cartesian coordinates of atom B
@param positionAtomC Cartesian coordinates of atom C
@param positionAtomD Cartesian coordinates of atom D
@param angleLength angle
@param angleK quadratic angle force
@param angleCubic cubic angle force parameter
@param angleQuartic quartic angle force parameter
@param anglePentic pentic angle force parameter
@param angleSextic sextic angle force parameter
@param forces force vector
@return energy
--------------------------------------------------------------------------------------- */
double AmoebaReferenceOutOfPlaneBendForce::calculateOutOfPlaneBendIxn(const Vec3& positionAtomA, const Vec3& positionAtomB,
const Vec3& positionAtomC, const Vec3& positionAtomD,
double angleK,
double angleCubic, double angleQuartic,
double anglePentic, double angleSextic,
Vec3* forces) const {
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
std::vector<double> deltaR[LastDeltaIndex];
for (int ii = 0; ii < LastDeltaIndex; ii++) {
deltaR[ii].resize(3);
}
if (usePeriodic) {
AmoebaReferenceForce::loadDeltaRPeriodic(positionAtomB, positionAtomA, deltaR[AB], boxVectors);
AmoebaReferenceForce::loadDeltaRPeriodic(positionAtomB, positionAtomC, deltaR[CB], boxVectors);
AmoebaReferenceForce::loadDeltaRPeriodic(positionAtomB, positionAtomD, deltaR[DB], boxVectors);
AmoebaReferenceForce::loadDeltaRPeriodic(positionAtomD, positionAtomA, deltaR[AD], boxVectors);
AmoebaReferenceForce::loadDeltaRPeriodic(positionAtomD, positionAtomC, deltaR[CD], boxVectors);
}
else {
AmoebaReferenceForce::loadDeltaR(positionAtomB, positionAtomA, deltaR[AB]);
AmoebaReferenceForce::loadDeltaR(positionAtomB, positionAtomC, deltaR[CB]);
AmoebaReferenceForce::loadDeltaR(positionAtomB, positionAtomD, deltaR[DB]);
AmoebaReferenceForce::loadDeltaR(positionAtomD, positionAtomA, deltaR[AD]);
AmoebaReferenceForce::loadDeltaR(positionAtomD, positionAtomC, deltaR[CD]);
}
double rDB2 = AmoebaReferenceForce::getNormSquared3(deltaR[DB]);
double rAD2 = AmoebaReferenceForce::getNormSquared3(deltaR[AD]);
double rCD2 = AmoebaReferenceForce::getNormSquared3(deltaR[CD]);
std::vector<double> tempVector(3);
AmoebaReferenceForce::getCrossProduct(deltaR[CB], deltaR[DB], tempVector);
double eE = AmoebaReferenceForce::getDotProduct3(deltaR[AB], tempVector);
double dot = AmoebaReferenceForce::getDotProduct3(deltaR[AD], deltaR[CD]);
double cc = rAD2*rCD2 - dot*dot;
if (rDB2 <= 0.0 || cc == 0.0) {
return 0.0;
}
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 = M_PI;
} else {
angle = RADIAN*acos(cosine);
}
// chain rule
double dt = angle;
double dt2 = dt*dt;
double dt3 = dt2*dt;
double dt4 = dt2*dt2;
double dEdDt = 2.0 + 3.0*angleCubic*dt + 4.0*angleQuartic*dt2 +
5.0*anglePentic*dt3 + 6.0*angleSextic*dt4;
dEdDt *= angleK*dt*RADIAN;
double dEdCos = dEdDt/sqrt(cc*bkk2);
if (eE > 0.0) {
dEdCos *= -1.0;
}
double term = eE/cc;
std::vector<double> dccd[LastAtomIndex];
std::vector<double> deed[LastAtomIndex];
std::vector<double> subForce[LastAtomIndex];
for (int ii = 0; ii < LastAtomIndex; ii++) {
dccd[ii].resize(3);
deed[ii].resize(3);
subForce[ii].resize(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]);
}
AmoebaReferenceForce::getCrossProduct(deltaR[DB], deltaR[CB], deed[A]);
AmoebaReferenceForce::getCrossProduct(deltaR[AB], deltaR[DB], deed[C]);
AmoebaReferenceForce::getCrossProduct(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)
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]);
}
}
}
// add in forces
for (int jj = 0; jj < LastAtomIndex; jj++) {
for (int ii = 0; ii < 3; ii++) {
forces[jj][ii] = subForce[jj][ii];
}
}
// ---------------------------------------------------------------------------------------
// calculate energy if 'energy' is set
double energy = 1.0 + angleCubic*dt + angleQuartic*dt2 + anglePentic*dt3 + angleSextic*dt4;
energy *= angleK*dt2;
return energy;
}
double AmoebaReferenceOutOfPlaneBendForce::calculateForceAndEnergy(int numOutOfPlaneBends, vector<Vec3>& posData,
const std::vector<int>& particle1,
const std::vector<int>& particle2,
const std::vector<int>& particle3,
const std::vector<int>& particle4,
const std::vector<double>& kQuadratic,
double angleCubic,
double angleQuartic,
double anglePentic,
double angleSextic,
vector<Vec3>& forceData) const {
double energy = 0.0;
for (unsigned int ii = 0; ii < static_cast<unsigned int>(numOutOfPlaneBends); ii++) {
int particle1Index = particle1[ii];
int particle2Index = particle2[ii];
int particle3Index = particle3[ii];
int particle4Index = particle4[ii];
double kAngle = kQuadratic[ii];
Vec3 forces[4];
energy += calculateOutOfPlaneBendIxn(posData[particle1Index], posData[particle2Index], posData[particle3Index], posData[particle4Index],
kAngle, angleCubic, angleQuartic, anglePentic, angleSextic, forces);
for (int jj = 0; jj < 3; jj++) {
forceData[particle1Index][jj] -= forces[0][jj];
forceData[particle2Index][jj] -= forces[1][jj];
forceData[particle3Index][jj] -= forces[2][jj];
forceData[particle4Index][jj] -= forces[3][jj];
}
}
return energy;
}
/* Portions copyright (c) 2006-2016 Stanford University and Simbios.
* Contributors: Pande Group
*
* 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.
*/
#ifndef __AmoebaReferenceOutOfPlaneBendForce_H__
#define __AmoebaReferenceOutOfPlaneBendForce_H__
#include "openmm/Vec3.h"
#include <vector>
namespace OpenMM {
class AmoebaReferenceOutOfPlaneBendForce {
public:
/**---------------------------------------------------------------------------------------
Constructor
--------------------------------------------------------------------------------------- */
AmoebaReferenceOutOfPlaneBendForce() : usePeriodic(false) {};
/**---------------------------------------------------------------------------------------
Destructor
--------------------------------------------------------------------------------------- */
~AmoebaReferenceOutOfPlaneBendForce() {};
/**---------------------------------------------------------------------------------------
Set the force to use periodic boundary conditions.
@param vectors the vectors defining the periodic box
--------------------------------------------------------------------------------------- */
void setPeriodic(OpenMM::Vec3* vectors);
/**---------------------------------------------------------------------------------------
Calculate Amoeba out-of-plane-bend angle (force and energy)
@param numOutOfPlaneBends number of angles
@param posData particle positions
@param particle1 particle 1 indices
@param particle2 particle 2 indices
@param particle3 particle 3 indices
@param particle4 particle 4 indices
@param kAngle angle force constant
@param angleCubic cubic force parameter
@param angleQuartic quartic force parameter
@param anglePentic pentic force parameter
@param angleSexic sextic force parameter
@param forces output force vector
@return total energy
--------------------------------------------------------------------------------------- */
double calculateForceAndEnergy(int numOutOfPlaneBends, std::vector<OpenMM::Vec3>& posData,
const std::vector<int>& particle1,
const std::vector<int>& particle2,
const std::vector<int>& particle3,
const std::vector<int>& particle4,
const std::vector<double>& kAngle,
double angleCubic,
double angleQuartic,
double anglePentic,
double angleSextic,
std::vector<OpenMM::Vec3>& forceData) const;
private:
bool usePeriodic;
Vec3 boxVectors[3];
/**---------------------------------------------------------------------------------------
Calculate Amoeba Out-Of-Plane-Bend ixn (force and energy)
@param positionAtomA Cartesian coordinates of atom A
@param positionAtomB Cartesian coordinates of atom B
@param positionAtomC Cartesian coordinates of atom C
@param positionAtomD Cartesian coordinates of atom D
@param angleK quadratic angle force parameter
@param angleCubic cubic angle force parameter
@param angleQuartic quartic angle force parameter
@param anglePentic pentic angle force parameter
@param angleSextic sextic angle force parameter
@param forces force vector
@return energy
--------------------------------------------------------------------------------------- */
double calculateOutOfPlaneBendIxn(const OpenMM::Vec3& positionAtomA, const OpenMM::Vec3& positionAtomB,
const OpenMM::Vec3& positionAtomC, const OpenMM::Vec3& positionAtomD,
double angleK,
double angleCubic, double angleQuartic,
double anglePentic, double angleSextic,
OpenMM::Vec3* forces) const;
};
} // namespace OpenMM
#endif // _AmoebaReferenceOutOfPlaneBendForce___
/* Portions copyright (c) 2006-2016 Stanford University and Simbios.
* Contributors: Pande Group
*
* 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 "AmoebaReferenceForce.h"
#include "AmoebaReferencePiTorsionForce.h"
#include <cmath>
#include <vector>
using std::vector;
using namespace OpenMM;
void AmoebaReferencePiTorsionForce::setPeriodic(OpenMM::Vec3* vectors) {
usePeriodic = true;
boxVectors[0] = vectors[0];
boxVectors[1] = vectors[1];
boxVectors[2] = vectors[2];
}
/**---------------------------------------------------------------------------------------
Calculate Amoeba pi-torsion ixn (force and energy)
@param positionAtomA Cartesian coordinates of atom A
@param positionAtomB Cartesian coordinates of atom B
@param positionAtomC Cartesian coordinates of atom C
@param positionAtomD Cartesian coordinates of atom D
@param positionAtomE Cartesian coordinates of atom E
@param positionAtomF Cartesian coordinates of atom F
@param piTorsionK force constant
@param forces force vector
@return energy
--------------------------------------------------------------------------------------- */
double AmoebaReferencePiTorsionForce::calculatePiTorsionIxn(const Vec3& positionAtomA, const Vec3& positionAtomB,
const Vec3& positionAtomC, const Vec3& positionAtomD,
const Vec3& positionAtomE, const Vec3& positionAtomF,
double piTorsionK, Vec3* forces) const {
enum { AD, BD, EC, FC, P, Q, CP, DC, QD, T, U, TU, DP, QC, dT, dU, dP, dQ, dC1, dC2, dD1, dD2, LastDeltaIndex };
std::vector<double> deltaR[LastDeltaIndex];
for (unsigned int ii = 0; ii < LastDeltaIndex; ii++) {
deltaR[ii].resize(3);
}
if (usePeriodic) {
AmoebaReferenceForce::loadDeltaRPeriodic(positionAtomD, positionAtomA, deltaR[AD], boxVectors);
AmoebaReferenceForce::loadDeltaRPeriodic(positionAtomD, positionAtomB, deltaR[BD], boxVectors);
AmoebaReferenceForce::loadDeltaRPeriodic(positionAtomC, positionAtomE, deltaR[EC], boxVectors);
AmoebaReferenceForce::loadDeltaRPeriodic(positionAtomC, positionAtomF, deltaR[FC], boxVectors);
}
else {
AmoebaReferenceForce::loadDeltaR(positionAtomD, positionAtomA, deltaR[AD]);
AmoebaReferenceForce::loadDeltaR(positionAtomD, positionAtomB, deltaR[BD]);
AmoebaReferenceForce::loadDeltaR(positionAtomC, positionAtomE, deltaR[EC]);
AmoebaReferenceForce::loadDeltaR(positionAtomC, positionAtomF, deltaR[FC]);
}
enum { A, B, C, D, E, F, LastAtomIndex };
std::vector<double> d[LastAtomIndex];
for (unsigned int ii = 0; ii < LastAtomIndex; ii++) {
d[ii].resize(3);
}
AmoebaReferenceForce::getCrossProduct(deltaR[AD], deltaR[BD], deltaR[P]);
AmoebaReferenceForce::getCrossProduct(deltaR[EC], deltaR[FC], deltaR[Q]);
for (int ii = 0; ii < 3; ii++) {
deltaR[CP][ii] = -deltaR[P][ii];
deltaR[DC][ii] = positionAtomD[ii] - positionAtomC[ii];
deltaR[QD][ii] = deltaR[Q][ii];
deltaR[P][ii] += positionAtomC[ii];
deltaR[Q][ii] += positionAtomD[ii];
}
AmoebaReferenceForce::getCrossProduct(deltaR[CP], deltaR[DC], deltaR[T]);
AmoebaReferenceForce::getCrossProduct(deltaR[DC], deltaR[QD], deltaR[U]);
AmoebaReferenceForce::getCrossProduct(deltaR[T], deltaR[U], deltaR[TU]);
double rT2 = AmoebaReferenceForce::getNormSquared3(deltaR[T]);
double rU2 = AmoebaReferenceForce::getNormSquared3(deltaR[U]);
double rTrU = sqrt(rT2*rU2);
if (rTrU <= 0.0) {
return 0.0;
}
double rDC = AmoebaReferenceForce::getNorm3(deltaR[DC]);
double cosine = AmoebaReferenceForce::getDotProduct3(deltaR[T], deltaR[U]);
cosine /= rTrU;
double sine = AmoebaReferenceForce::getDotProduct3(deltaR[DC], deltaR[TU]);
sine /= (rDC*rTrU);
double cosine2 = cosine*cosine - sine*sine;
double sine2 = 2.0*cosine*sine;
double phi2 = 1.0 - cosine2;
double dphi2 = 2.0*sine2;
double dedphi = piTorsionK*dphi2;
for (unsigned int ii = 0; ii < 3; ii++) {
deltaR[DP][ii] = positionAtomD[ii] - deltaR[P][ii];
deltaR[QC][ii] = deltaR[Q][ii] - positionAtomC[ii];
}
double factorT = dedphi/(rDC*rT2);
double factorU = -dedphi/(rDC*rU2);
AmoebaReferenceForce::getCrossProduct(deltaR[T], deltaR[DC], deltaR[dT]);
AmoebaReferenceForce::getCrossProduct(deltaR[U], deltaR[DC], deltaR[dU]);
for (int ii = 0; ii < 3; ii++) {
deltaR[dT][ii] *= factorT;
deltaR[dU][ii] *= factorU;
}
AmoebaReferenceForce::getCrossProduct(deltaR[dT], deltaR[DC], deltaR[dP] );
AmoebaReferenceForce::getCrossProduct(deltaR[dU], deltaR[DC], deltaR[dQ] );
AmoebaReferenceForce::getCrossProduct(deltaR[DP], deltaR[dT], deltaR[dC1]);
AmoebaReferenceForce::getCrossProduct(deltaR[dU], deltaR[QD], deltaR[dC2]);
AmoebaReferenceForce::getCrossProduct(deltaR[dT], deltaR[CP], deltaR[dD1]);
AmoebaReferenceForce::getCrossProduct(deltaR[QC], deltaR[dU], deltaR[dD2]);
AmoebaReferenceForce::getCrossProduct(deltaR[BD], deltaR[dP], d[A] );
AmoebaReferenceForce::getCrossProduct(deltaR[dP], deltaR[AD], d[B] );
AmoebaReferenceForce::getCrossProduct(deltaR[FC], deltaR[dQ], d[E] );
AmoebaReferenceForce::getCrossProduct(deltaR[dQ], deltaR[EC], d[F] );
for (int ii = 0; ii < 3; ii++) {
d[C][ii] = deltaR[dC1][ii] + deltaR[dC2][ii] + deltaR[dP][ii] - d[E][ii] - d[F][ii];
d[D][ii] = deltaR[dD1][ii] + deltaR[dD2][ii] + deltaR[dQ][ii] - d[A][ii] - d[B][ii];
}
// ---------------------------------------------------------------------------------------
// add in forces
forces[0][0] = d[0][0];
forces[0][1] = d[0][1];
forces[0][2] = d[0][2];
forces[1][0] = d[1][0];
forces[1][1] = d[1][1];
forces[1][2] = d[1][2];
forces[2][0] = d[2][0];
forces[2][1] = d[2][1];
forces[2][2] = d[2][2];
forces[3][0] = d[3][0];
forces[3][1] = d[3][1];
forces[3][2] = d[3][2];
forces[4][0] = d[4][0];
forces[4][1] = d[4][1];
forces[4][2] = d[4][2];
forces[5][0] = d[5][0];
forces[5][1] = d[5][1];
forces[5][2] = d[5][2];
// ---------------------------------------------------------------------------------------
// calculate energy if 'energy' is set
return piTorsionK*phi2;
}
double AmoebaReferencePiTorsionForce::calculateForceAndEnergy(int numPiTorsions, vector<Vec3>& posData,
const std::vector<int>& particle1,
const std::vector<int>& particle2,
const std::vector<int>& particle3,
const std::vector<int>& particle4,
const std::vector<int>& particle5,
const std::vector<int>& particle6,
const std::vector<double>& kTorsion,
vector<Vec3>& forceData) const {
double energy = 0.0;
for (unsigned int ii = 0; ii < static_cast<unsigned int>(numPiTorsions); ii++) {
int particle1Index = particle1[ii];
int particle2Index = particle2[ii];
int particle3Index = particle3[ii];
int particle4Index = particle4[ii];
int particle5Index = particle5[ii];
int particle6Index = particle6[ii];
Vec3 forces[6];
energy += calculatePiTorsionIxn(posData[particle1Index], posData[particle2Index],
posData[particle3Index], posData[particle4Index],
posData[particle5Index], posData[particle6Index],
kTorsion[ii], forces);
// accumulate forces
for (int jj = 0; jj < 3; jj++) {
forceData[particle1Index][jj] -= forces[0][jj];
forceData[particle2Index][jj] -= forces[1][jj];
forceData[particle3Index][jj] -= forces[2][jj];
forceData[particle4Index][jj] -= forces[3][jj];
forceData[particle5Index][jj] -= forces[4][jj];
forceData[particle6Index][jj] -= forces[5][jj];
}
}
return energy;
}
/* Portions copyright (c) 2006-2016 Stanford University and Simbios.
* Contributors: Pande Group
*
* 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.
*/
#ifndef __AmoebaReferencePiTorsionForce_H__
#define __AmoebaReferencePiTorsionForce_H__
#include "openmm/Vec3.h"
#include <vector>
namespace OpenMM {
class AmoebaReferencePiTorsionForce {
public:
/**---------------------------------------------------------------------------------------
Constructor
--------------------------------------------------------------------------------------- */
AmoebaReferencePiTorsionForce() : usePeriodic(false) {};
/**---------------------------------------------------------------------------------------
Destructor
--------------------------------------------------------------------------------------- */
~AmoebaReferencePiTorsionForce() {};
/**---------------------------------------------------------------------------------------
Set the force to use periodic boundary conditions.
@param vectors the vectors defining the periodic box
--------------------------------------------------------------------------------------- */
void setPeriodic(OpenMM::Vec3* vectors);
/**---------------------------------------------------------------------------------------
Calculate Amoeba torsion ixns (force and energy)
@param numTorsions number of torsions
@param posData particle positions
@param particle1 particle 1 indices
@param particle2 particle 2 indices
@param particle3 particle 3 indices
@param particle4 particle 4 indices
@param particle5 particle 5 indices
@param particle6 particle 6 indices
@param torsionParameters1 first index torsion parameters (amplitude, phase, fold)
@param torsionParameters2 second index torsion parameters (amplitude, phase, fold)
@param torsionParameters3 third index torsion parameters (amplitude, phase, fold)
@param forces output force vector
@return total energy
--------------------------------------------------------------------------------------- */
double calculateForceAndEnergy(int numPiTorsions, std::vector<OpenMM::Vec3>& posData,
const std::vector<int>& particle1,
const std::vector<int>& particle2,
const std::vector<int>& particle3,
const std::vector<int>& particle4,
const std::vector<int>& particle5,
const std::vector<int>& particle6,
const std::vector<double>& kTorsion,
std::vector<OpenMM::Vec3>& forceData) const;
private:
bool usePeriodic;
Vec3 boxVectors[3];
/**---------------------------------------------------------------------------------------
Calculate Amoeba pi-torsion ixn (force and energy)
@param positionAtomA Cartesian coordinates of atom A
@param positionAtomB Cartesian coordinates of atom B
@param positionAtomC Cartesian coordinates of atom C
@param positionAtomD Cartesian coordinates of atom D
@param positionAtomE Cartesian coordinates of atom E
@param positionAtomF Cartesian coordinates of atom F
@param kTorsion k-torsion parameter
@param forces force vector
@return energy
--------------------------------------------------------------------------------------- */
double calculatePiTorsionIxn(const OpenMM::Vec3& positionAtomA, const OpenMM::Vec3& positionAtomB,
const OpenMM::Vec3& positionAtomC, const OpenMM::Vec3& positionAtomD,
const OpenMM::Vec3& positionAtomE, const OpenMM::Vec3& positionAtomF,
double kTorsion, OpenMM::Vec3* forces) const;
};
} // namespace OpenMM
#endif // _AmoebaReferencePiTorsionForce___
/* Portions copyright (c) 2006-2016 Stanford University and Simbios.
* Contributors: Pande Group
*
* 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 "AmoebaReferenceForce.h"
#include "AmoebaReferenceStretchBendForce.h"
#include "SimTKOpenMMRealType.h"
#include <vector>
using std::vector;
using namespace OpenMM;
void AmoebaReferenceStretchBendForce::setPeriodic(OpenMM::Vec3* vectors) {
usePeriodic = true;
boxVectors[0] = vectors[0];
boxVectors[1] = vectors[1];
boxVectors[2] = vectors[2];
}
/**---------------------------------------------------------------------------------------
Calculate Amoeba stretch bend angle ixn (force and energy)
@param positionAtomA Cartesian coordinates of atom A
@param positionAtomB Cartesian coordinates of atom B
@param positionAtomC Cartesian coordinates of atom C
@param positionAtomD Cartesian coordinates of atom D
@param angleLength angle
@param angleK quadratic angle force
@param angleCubic cubic angle force parameter
@param angleQuartic quartic angle force parameter
@param anglePentic pentic angle force parameter
@param angleSextic sextic angle force parameter
@param forces force vector
@return energy
--------------------------------------------------------------------------------------- */
double AmoebaReferenceStretchBendForce::calculateStretchBendIxn(const Vec3& positionAtomA, const Vec3& positionAtomB,
const Vec3& positionAtomC,
double lengthAB, double lengthCB,
double idealAngle, double k1Parameter,
double k2Parameter, Vec3* forces) const {
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
std::vector<double> deltaR[LastDeltaIndex];
for (unsigned int ii = 0; ii < LastDeltaIndex; ii++) {
deltaR[ii].resize(3);
}
if (usePeriodic) {
AmoebaReferenceForce::loadDeltaRPeriodic(positionAtomB, positionAtomA, deltaR[AB], boxVectors);
AmoebaReferenceForce::loadDeltaRPeriodic(positionAtomB, positionAtomC, deltaR[CB], boxVectors);
}
else {
AmoebaReferenceForce::loadDeltaR(positionAtomB, positionAtomA, deltaR[AB]);
AmoebaReferenceForce::loadDeltaR(positionAtomB, positionAtomC, deltaR[CB]);
}
double rAB2 = AmoebaReferenceForce::getNormSquared3(deltaR[AB]);
double rAB = sqrt(rAB2);
double rCB2 = AmoebaReferenceForce::getNormSquared3(deltaR[CB]);
double rCB = sqrt(rCB2);
AmoebaReferenceForce::getCrossProduct(deltaR[CB], deltaR[AB], deltaR[CBxAB]);
double rP = AmoebaReferenceForce::getNorm3(deltaR[CBxAB]);
if (rP <= 0.0) {
return 0.0;
}
double dot = AmoebaReferenceForce::getDotProduct3(deltaR[CB], deltaR[AB]);
double cosine = dot/(rAB*rCB);
double angle;
if (cosine >= 1.0) {
angle = 0.0;
} else if (cosine <= -1.0) {
angle = M_PI;
} else {
angle = RADIAN*acos(cosine);
}
double termA = -RADIAN/(rAB2*rP);
double termC = RADIAN/(rCB2*rP);
// P = CBxAB
AmoebaReferenceForce::getCrossProduct(deltaR[AB], deltaR[CBxAB], deltaR[ABxP]);
AmoebaReferenceForce::getCrossProduct(deltaR[CB], deltaR[CBxAB], deltaR[CBxP]);
for (int ii = 0; ii < 3; ii++) {
deltaR[ABxP][ii] *= termA;
deltaR[CBxP][ii] *= termC;
}
double dr1 = rAB - lengthAB;
double dr2 = rCB - lengthCB;
double drkk = dr1*k1Parameter + dr2*k2Parameter;
termA = 1.0/rAB;
termC = 1.0/rCB;
// ---------------------------------------------------------------------------------------
// forces
// calculate forces for atoms a, b, c
// the force for b is then -(a + c)
std::vector<double> subForce[LastAtomIndex];
for (int ii = 0; ii < LastAtomIndex; ii++) {
subForce[ii].resize(3);
}
double dt = angle - idealAngle*RADIAN;
for (int jj = 0; jj < 3; jj++) {
subForce[A][jj] = k1Parameter*dt*termA*deltaR[AB][jj] + drkk*deltaR[ABxP][jj];
subForce[C][jj] = k2Parameter*dt*termC*deltaR[CB][jj] + drkk*deltaR[CBxP][jj];
subForce[B][jj] = -(subForce[A][jj] + subForce[C][jj]);
}
// add in forces
for (int jj = 0; jj < LastAtomIndex; jj++) {
forces[jj][0] = subForce[jj][0];
forces[jj][1] = subForce[jj][1];
forces[jj][2] = subForce[jj][2];
}
// ---------------------------------------------------------------------------------------
return dt*drkk;
}
double AmoebaReferenceStretchBendForce::calculateForceAndEnergy(int numStretchBends, vector<Vec3>& posData,
const std::vector<int>& particle1,
const std::vector<int>& particle2,
const std::vector<int>& particle3,
const std::vector<double>& lengthABParameters,
const std::vector<double>& lengthCBParameters,
const std::vector<double>& angle,
const std::vector<double>& k1Quadratic,
const std::vector<double>& k2Quadratic,
vector<Vec3>& forceData) const {
double energy = 0.0;
for (unsigned int ii = 0; ii < static_cast<unsigned int>(numStretchBends); ii++) {
int particle1Index = particle1[ii];
int particle2Index = particle2[ii];
int particle3Index = particle3[ii];
double abLength = lengthABParameters[ii];
double cbLength = lengthCBParameters[ii];
double idealAngle = angle[ii];
double angleK1 = k1Quadratic[ii];
double angleK2 = k2Quadratic[ii];
Vec3 forces[3];
energy += calculateStretchBendIxn(posData[particle1Index], posData[particle2Index], posData[particle3Index],
abLength, cbLength, idealAngle, angleK1, angleK2, forces);
// accumulate forces
for (int jj = 0; jj < 3; jj++) {
forceData[particle1Index][jj] -= forces[0][jj];
forceData[particle2Index][jj] -= forces[1][jj];
forceData[particle3Index][jj] -= forces[2][jj];
}
}
return energy;
}
/* Portions copyright (c) 2006-2016 Stanford University and Simbios.
* Contributors: Pande Group
*
* 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.
*/
#ifndef __AmoebaReferenceStretchBendForce_H__
#define __AmoebaReferenceStretchBendForce_H__
#include "openmm/Vec3.h"
#include <vector>
namespace OpenMM {
class AmoebaReferenceStretchBendForce {
public:
/**---------------------------------------------------------------------------------------
Constructor
--------------------------------------------------------------------------------------- */
AmoebaReferenceStretchBendForce() : usePeriodic(false) {};
/**---------------------------------------------------------------------------------------
Destructor
--------------------------------------------------------------------------------------- */
~AmoebaReferenceStretchBendForce() {};
/**---------------------------------------------------------------------------------------
Set the force to use periodic boundary conditions.
@param vectors the vectors defining the periodic box
--------------------------------------------------------------------------------------- */
void setPeriodic(OpenMM::Vec3* vectors);
/**---------------------------------------------------------------------------------------
Calculate Amoeba stretch bend ixns (force and energy)
@param numBonds number of angles
@param posData particle positions
@param particle1 particle 1 indices
@param particle2 particle 2 indices
@param particle3 particle 3 indices
@param lengthABParameters ideal AB bond length
@param lengthCBParameters ideal CB bond length
@param angle ideal angle
@param kQuadratic force constant
@param forces output force vector
@return total energy
--------------------------------------------------------------------------------------- */
double calculateForceAndEnergy(int numAngles, std::vector<OpenMM::Vec3>& posData,
const std::vector<int>& particle1,
const std::vector<int>& particle2,
const std::vector<int>& particle3,
const std::vector<double>& lengthABParameters,
const std::vector<double>& lengthCBParameters,
const std::vector<double>& angle,
const std::vector<double>& k1Quadratic,
const std::vector<double>& k2Quadratic,
std::vector<OpenMM::Vec3>& forceData) const;
private:
bool usePeriodic;
Vec3 boxVectors[3];
/**---------------------------------------------------------------------------------------
Calculate Amoeba stretch bend angle ixn (force and energy)
@param positionAtomA Cartesian coordinates of atom A
@param positionAtomB Cartesian coordinates of atom B
@param positionAtomC Cartesian coordinates of atom C
@param lengthAB ideal AB bondlength
@param lengthCB ideal CB bondlength
@param idealAngle ideal angle
@param k1Parameter k for distance A-B * angle A-B-C
@param k2Parameter k for distance B-C * angle A-B-C
@param forces force vector
@return energy
--------------------------------------------------------------------------------------- */
double calculateStretchBendIxn(const OpenMM::Vec3& positionAtomA, const OpenMM::Vec3& positionAtomB,
const OpenMM::Vec3& positionAtomC,
double lengthAB, double lengthCB,
double idealAngle, double k1Parameter,
double k2Parameter, OpenMM::Vec3* forces) const;
};
} // namespace OpenMM
#endif // _AmoebaReferenceStretchBendForce___
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* 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: Peter Eastman *
* 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" OPENMM_EXPORT void registerAmoebaReferenceKernelFactories();
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);
}
}
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);
ASSERT(!amoebaAngleForce->usesPeriodicBoundaryConditions());
ASSERT(!system.usesPeriodicBoundaryConditions());
Context context(system, integrator, Platform::getPlatformByName("Reference"));
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("Reference"));
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 numberOfArguments, char* argv[]) {
try {
std::cout << "TestReferenceAmoebaAngleForce running test..." << std::endl;
registerAmoebaReferenceKernelFactories();
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 << "PASS - Test succeeded." << std::endl;
std::cout << "Done" << std::endl;
return 0;
}
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* 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: Peter Eastman *
* 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 Reference implementation of AmoebaBondForce.
*/
#include "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.h"
#include "openmm/CustomBondForce.h"
#include "OpenMMAmoeba.h"
#include "openmm/System.h"
#include "openmm/LangevinIntegrator.h"
#include <iostream>
#include <vector>
#include <math.h>
using namespace OpenMM;
extern "C" OPENMM_EXPORT void registerAmoebaReferenceKernelFactories();
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("Reference"));
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);
ASSERT(!amoebaBondForce->usesPeriodicBoundaryConditions());
ASSERT(!system.usesPeriodicBoundaryConditions());
Context context(system, integrator, Platform::getPlatformByName("Reference"));
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("Reference"));
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 numberOfArguments, char* argv[]) {
try {
std::cout << "TestReferenceAmoebaBondForce running test..." << std::endl;
registerAmoebaReferenceKernelFactories();
//testOneBond();
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 << "PASS - Test succeeded." << std::endl;
std::cout << "Done" << std::endl;
return 0;
}
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* 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: Peter Eastman *
* 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 Reference implementation of ReferenceAmoebaInPlaneAngleForce.
*/
#include "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.h"
#include "OpenMMAmoeba.h"
#include "openmm/System.h"
#include "openmm/LangevinIntegrator.h"
#include "SimTKOpenMMRealType.h"
#include <iostream>
#include <vector>
using namespace OpenMM;
extern "C" OPENMM_EXPORT void registerAmoebaReferenceKernelFactories();
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 double dotVector3(double* vectorX, double* vectorY) {
return vectorX[0]*vectorY[0] + vectorX[1]*vectorY[1] + vectorX[2]*vectorY[2];
}
static void getPrefactorsGivenInPlaneAngleCosine(double cosine, double idealInPlaneAngle, 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 - idealInPlaneAngle;
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 computeAmoebaInPlaneAngleForce(int bondIndex, std::vector<Vec3>& positions, AmoebaInPlaneAngleForce& AmoebaInPlaneAngleForce,
std::vector<Vec3>& forces, double* energy) {
int particle1, particle2, particle3, particle4;
double idealInPlaneAngle;
double quadraticK;
AmoebaInPlaneAngleForce.getAngleParameters(bondIndex, particle1, particle2, particle3, particle4, idealInPlaneAngle, quadraticK);
double cubicK = AmoebaInPlaneAngleForce.getAmoebaGlobalInPlaneAngleCubic();
double quarticK = AmoebaInPlaneAngleForce.getAmoebaGlobalInPlaneAngleQuartic();
double penticK = AmoebaInPlaneAngleForce.getAmoebaGlobalInPlaneAnglePentic();
double sexticK = AmoebaInPlaneAngleForce.getAmoebaGlobalInPlaneAngleSextic();
// T = AD x CD
// P = B + T*delta
// AP = A - P
// CP = A - P
// M = CP x AP
enum { AD, BD, CD, T, AP, P, CP, M, APxM, CPxM, ADxBD, BDxCD, TxCD, ADxT, dBxAD, CDxdB, LastDeltaAtomIndex };
// AD 0
// BD 1
// CD 2
// T 3
// AP 4
// P 5
// CP 6
// M 7
// APxM, CPxM, ADxBD, BDxCD, TxCD, ADxT, dBxAD, CDxdB, LastDeltaAtomIndex
double deltaR[LastDeltaAtomIndex][3];
for (int ii = 0; ii < 3; ii++) {
deltaR[AD][ii] = positions[particle1][ii] - positions[particle4][ii];
deltaR[BD][ii] = positions[particle2][ii] - positions[particle4][ii];
deltaR[CD][ii] = positions[particle3][ii] - positions[particle4][ii];
}
crossProductVector3(deltaR[AD], deltaR[CD], deltaR[T]);
double rT2 = dotVector3(deltaR[T], deltaR[T]);
double delta = dotVector3(deltaR[T], deltaR[BD]);
delta *= -1.0/rT2;
for (int ii = 0; ii < 3; ii++) {
deltaR[P][ii] = positions[particle2][ii] + deltaR[T][ii]*delta;
deltaR[AP][ii] = positions[particle1][ii] - deltaR[P][ii];
deltaR[CP][ii] = positions[particle3][ii] - deltaR[P][ii];
}
double rAp2 = dotVector3(deltaR[AP], deltaR[AP]);
double rCp2 = dotVector3(deltaR[CP], deltaR[CP]);
if (rAp2 <= 0.0 && rCp2 <= 0.0) {
return;
}
crossProductVector3(deltaR[CP], deltaR[AP], deltaR[M]);
double rm = dotVector3(deltaR[M], deltaR[M]);
rm = sqrt(rm);
if (rm < 0.000001) {
rm = 0.000001;
}
double dot = dotVector3(deltaR[AP], deltaR[CP]);
double cosine = dot/sqrt(rAp2*rCp2);
double dEdR;
double energyTerm;
getPrefactorsGivenInPlaneAngleCosine(cosine, idealInPlaneAngle, quadraticK, cubicK,
quarticK, penticK, sexticK, &dEdR, &energyTerm);
double termA = -dEdR/(rAp2*rm);
double termC = dEdR/(rCp2*rm);
crossProductVector3(deltaR[AP], deltaR[M], deltaR[APxM]);
crossProductVector3(deltaR[CP], deltaR[M], deltaR[CPxM]);
// forces will be gathered here
enum { dA, dB, dC, dD, LastDIndex };
double forceTerm[LastDIndex][3];
for (int ii = 0; ii < 3; ii++) {
forceTerm[dA][ii] = deltaR[APxM][ii]*termA;
forceTerm[dC][ii] = deltaR[CPxM][ii]*termC;
forceTerm[dB][ii] = -1.0*(forceTerm[dA][ii] + forceTerm[dC][ii]);
}
double pTrT2 = dotVector3(forceTerm[dB], deltaR[T]);
pTrT2 /= rT2;
crossProductVector3(deltaR[CD], forceTerm[dB], deltaR[CDxdB]);
crossProductVector3(forceTerm[dB], deltaR[AD], deltaR[dBxAD]);
if (fabs(pTrT2) > 1.0e-08) {
double delta2 = delta*2.0;
crossProductVector3(deltaR[BD], deltaR[CD], deltaR[BDxCD]);
crossProductVector3(deltaR[T], deltaR[CD], deltaR[TxCD]);
crossProductVector3(deltaR[AD], deltaR[BD], deltaR[ADxBD]);
crossProductVector3(deltaR[AD], deltaR[T], deltaR[ADxT]);
for (int ii = 0; ii < 3; ii++) {
double term = deltaR[BDxCD][ii] + delta2*deltaR[TxCD][ii];
forceTerm[dA][ii] += delta*deltaR[CDxdB][ii] + term*pTrT2;
term = deltaR[ADxBD][ii] + delta2*deltaR[ADxT][ii];
forceTerm[dC][ii] += delta*deltaR[dBxAD][ii] + term*pTrT2;
forceTerm[dD][ii] = -(forceTerm[dA][ii] + forceTerm[dB][ii] + forceTerm[dC][ii]);
}
}
else {
for (int ii = 0; ii < 3; ii++) {
forceTerm[dA][ii] += delta*deltaR[CDxdB][ii];
forceTerm[dC][ii] += delta*deltaR[dBxAD][ii];
forceTerm[dD][ii] = -(forceTerm[dA][ii] + forceTerm[dB][ii] + forceTerm[dC][ii]);
}
}
// accumulate forces and energy
*energy += energyTerm;
forces[particle1][0] -= forceTerm[0][0];
forces[particle1][1] -= forceTerm[0][1];
forces[particle1][2] -= forceTerm[0][2];
forces[particle2][0] -= forceTerm[1][0];
forces[particle2][1] -= forceTerm[1][1];
forces[particle2][2] -= forceTerm[1][2];
forces[particle3][0] -= forceTerm[2][0];
forces[particle3][1] -= forceTerm[2][1];
forces[particle3][2] -= forceTerm[2][2];
forces[particle4][0] -= forceTerm[3][0];
forces[particle4][1] -= forceTerm[3][1];
forces[particle4][2] -= forceTerm[3][2];
}
static void computeAmoebaInPlaneAngleForces(Context& context, AmoebaInPlaneAngleForce& AmoebaInPlaneAngleForce,
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 < AmoebaInPlaneAngleForce.getNumAngles(); ii++) {
computeAmoebaInPlaneAngleForce(ii, positions, AmoebaInPlaneAngleForce, expectedForces, expectedEnergy);
}
}
void compareWithExpectedForceAndEnergy(Context& context, AmoebaInPlaneAngleForce& AmoebaInPlaneAngleForce,
double tolerance, const std::string& idString) {
std::vector<Vec3> expectedForces;
double expectedEnergy;
computeAmoebaInPlaneAngleForces(context, AmoebaInPlaneAngleForce, 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 = 4;
for (int ii = 0; ii < numberOfParticles; ii++) {
system.addParticle(1.0);
}
LangevinIntegrator integrator(0.0, 0.1, 0.01);
AmoebaInPlaneAngleForce* amoebaInPlaneAngleForce = new AmoebaInPlaneAngleForce();
double angle = 65.0;
double quadraticK = 1.0;
double cubicK = 0.0e-01;
double quarticK = 0.0e-02;
double penticK = 0.0e-03;
double sexticK = 0.0e-04;
amoebaInPlaneAngleForce->addAngle(0, 1, 2, 3, angle, quadraticK);
amoebaInPlaneAngleForce->setAmoebaGlobalInPlaneAngleCubic(cubicK);
amoebaInPlaneAngleForce->setAmoebaGlobalInPlaneAngleQuartic(quarticK);
amoebaInPlaneAngleForce->setAmoebaGlobalInPlaneAnglePentic(penticK);
amoebaInPlaneAngleForce->setAmoebaGlobalInPlaneAngleSextic(sexticK);
system.addForce(amoebaInPlaneAngleForce);
ASSERT(!amoebaInPlaneAngleForce->usesPeriodicBoundaryConditions());
ASSERT(!system.usesPeriodicBoundaryConditions());
Context context(system, integrator, Platform::getPlatformByName("Reference"));
std::vector<Vec3> positions(numberOfParticles);
positions[0] = Vec3(0, 1, 0);
positions[1] = Vec3(0, 0, 0);
positions[2] = Vec3(0, 0, 1);
positions[3] = Vec3(1, 1, 1);
context.setPositions(positions);
compareWithExpectedForceAndEnergy(context, *amoebaInPlaneAngleForce, TOL, "testOneInPlaneAngle");
// Try changing the angle parameters and make sure it's still correct.
amoebaInPlaneAngleForce->setAngleParameters(0, 0, 1, 2, 3, 1.1*angle, 1.4*quadraticK);
bool exceptionThrown = false;
try {
// This should throw an exception.
compareWithExpectedForceAndEnergy(context, *amoebaInPlaneAngleForce, TOL, "testOneInPlaneAngle");
}
catch (std::exception ex) {
exceptionThrown = true;
}
ASSERT(exceptionThrown);
amoebaInPlaneAngleForce->updateParametersInContext(context);
compareWithExpectedForceAndEnergy(context, *amoebaInPlaneAngleForce, TOL, "testOneInPlaneAngle");
}
void testPeriodic() {
// Create a force that uses periodic boundary conditions.
System system;
system.setDefaultPeriodicBoxVectors(Vec3(3, 0, 0), Vec3(0, 3, 0), Vec3(0, 0, 3));
int numberOfParticles = 4;
for (int ii = 0; ii < numberOfParticles; ii++)
system.addParticle(1.0);
LangevinIntegrator integrator(0.0, 0.1, 0.01);
AmoebaInPlaneAngleForce* amoebaInPlaneAngleForce = new AmoebaInPlaneAngleForce();
double angle = 65.0;
double quadraticK = 1.0;
double cubicK = 0.0e-01;
double quarticK = 0.0e-02;
double penticK = 0.0e-03;
double sexticK = 0.0e-04;
amoebaInPlaneAngleForce->addAngle(0, 1, 2, 3, angle, quadraticK);
amoebaInPlaneAngleForce->setAmoebaGlobalInPlaneAngleCubic(cubicK);
amoebaInPlaneAngleForce->setAmoebaGlobalInPlaneAngleQuartic(quarticK);
amoebaInPlaneAngleForce->setAmoebaGlobalInPlaneAnglePentic(penticK);
amoebaInPlaneAngleForce->setAmoebaGlobalInPlaneAngleSextic(sexticK);
amoebaInPlaneAngleForce->setUsesPeriodicBoundaryConditions(true);
system.addForce(amoebaInPlaneAngleForce);
Context context(system, integrator, Platform::getPlatformByName("Reference"));
std::vector<Vec3> positions(numberOfParticles);
positions[0] = Vec3(0, 1, 0);
positions[1] = Vec3(0, 0, 0);
positions[2] = Vec3(0, 0, 1);
positions[3] = Vec3(1, 1, 1);
context.setPositions(positions);
State s1 = context.getState(State::Forces | State::Energy);
// Move one atom to a position that should give identical results.
positions[2] = Vec3(0, 0, -2);
context.setPositions(positions);
State s2 = context.getState(State::Forces | State::Energy);
ASSERT_EQUAL_TOL(s1.getPotentialEnergy(), s2.getPotentialEnergy(), 1e-5);
for (int i = 0; i < numberOfParticles; i++)
ASSERT_EQUAL_VEC(s1.getForces()[i], s2.getForces()[i], 1e-5);
}
int main(int numberOfArguments, char* argv[]) {
try {
std::cout << "TestReferenceAmoebaInPlaneAngleForce running test..." << std::endl;
registerAmoebaReferenceKernelFactories();
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 << "PASS - Test succeeded." << std::endl;
std::cout << "Done" << std::endl;
return 0;
}
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