Commit 1f7866ad authored by peastman's avatar peastman Committed by GitHub
Browse files

Merge pull request #1547 from peastman/paramderivs

Energy derivatives with respect to global parameters
parents 37787af9 7851bad8
...@@ -181,6 +181,41 @@ void testPeriodic() { ...@@ -181,6 +181,41 @@ void testPeriodic() {
ASSERT_EQUAL_TOL(0.5*1.1*(M_PI/6)*(M_PI/6), state.getPotentialEnergy(), TOL); ASSERT_EQUAL_TOL(0.5*1.1*(M_PI/6)*(M_PI/6), state.getPotentialEnergy(), TOL);
} }
void testEnergyParameterDerivatives() {
System system;
system.addParticle(1.0);
system.addParticle(1.0);
system.addParticle(1.0);
VerletIntegrator integrator(0.01);
CustomAngleForce* angles = new CustomAngleForce("k*(theta-theta0)^2");
angles->addGlobalParameter("theta0", 0.0);
angles->addGlobalParameter("k", 0.0);
angles->addEnergyParameterDerivative("theta0");
angles->addEnergyParameterDerivative("k");
vector<double> parameters;
angles->addAngle(0, 1, 2, parameters);
system.addForce(angles);
Context context(system, integrator, platform);
vector<Vec3> positions(3);
positions[0] = Vec3(0, 2, 0);
positions[1] = Vec3(0, 0, 0);
positions[2] = Vec3(1, 1, 0);
context.setPositions(positions);
double theta = M_PI/4;
for (int i = 0; i < 10; i++) {
double theta0 = 0.1*i;
double k = 10-i;
context.setParameter("theta0", theta0);
context.setParameter("k", k);
State state = context.getState(State::ParameterDerivatives);
map<string, double> derivs = state.getEnergyParameterDerivatives();
double dEdtheta0 = -2*k*(theta-theta0);
double dEdk = (theta-theta0)*(theta-theta0);
ASSERT_EQUAL_TOL(dEdtheta0, derivs["theta0"], 1e-5);
ASSERT_EQUAL_TOL(dEdk, derivs["k"], 1e-5);
}
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -189,6 +224,7 @@ int main(int argc, char* argv[]) { ...@@ -189,6 +224,7 @@ int main(int argc, char* argv[]) {
testAngles(); testAngles();
testIllegalVariable(); testIllegalVariable();
testPeriodic(); testPeriodic();
testEnergyParameterDerivatives();
runPlatformTests(); runPlatformTests();
} }
catch(const exception& e) { catch(const exception& e) {
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2008-2015 Stanford University and the Authors. * * Portions copyright (c) 2008-2016 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -178,6 +178,41 @@ void testPeriodic() { ...@@ -178,6 +178,41 @@ void testPeriodic() {
ASSERT_EQUAL_TOL(0.5*0.8*0.9*0.9, state.getPotentialEnergy(), TOL); ASSERT_EQUAL_TOL(0.5*0.8*0.9*0.9, state.getPotentialEnergy(), TOL);
} }
void testEnergyParameterDerivatives() {
System system;
system.addParticle(1.0);
system.addParticle(1.0);
system.addParticle(1.0);
VerletIntegrator integrator(0.01);
CustomBondForce* bonds = new CustomBondForce("k*(r-r0)^2");
bonds->addGlobalParameter("r0", 0.0);
bonds->addGlobalParameter("k", 0.0);
bonds->addEnergyParameterDerivative("k");
bonds->addEnergyParameterDerivative("r0");
vector<double> parameters;
bonds->addBond(0, 1, parameters);
bonds->addBond(1, 2, parameters);
system.addForce(bonds);
Context context(system, integrator, platform);
vector<Vec3> positions(3);
positions[0] = Vec3(0, 2, 0);
positions[1] = Vec3(0, 0, 0);
positions[2] = Vec3(1, 0, 0);
context.setPositions(positions);
for (int i = 0; i < 10; i++) {
double r0 = 0.1*i;
double k = 10-i;
context.setParameter("r0", r0);
context.setParameter("k", k);
State state = context.getState(State::ParameterDerivatives);
map<string, double> derivs = state.getEnergyParameterDerivatives();
double dEdr0 = -2*k*((2-r0)+(1-r0));
double dEdk = (2-r0)*(2-r0) + (1-r0)*(1-r0);
ASSERT_EQUAL_TOL(dEdr0, derivs["r0"], 1e-5);
ASSERT_EQUAL_TOL(dEdk, derivs["k"], 1e-5);
}
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -187,6 +222,7 @@ int main(int argc, char* argv[]) { ...@@ -187,6 +222,7 @@ int main(int argc, char* argv[]) {
testManyParameters(); testManyParameters();
testIllegalVariable(); testIllegalVariable();
testPeriodic(); testPeriodic();
testEnergyParameterDerivatives();
runPlatformTests(); runPlatformTests();
} }
catch(const exception& e) { catch(const exception& e) {
......
...@@ -333,6 +333,65 @@ void testPeriodic() { ...@@ -333,6 +333,65 @@ void testPeriodic() {
ASSERT_EQUAL_VEC(Vec3(2*0.5*(5.0/12.0), 0, 0), state.getForces()[4], TOL); ASSERT_EQUAL_VEC(Vec3(2*0.5*(5.0/12.0), 0, 0), state.getForces()[4], TOL);
} }
void testEnergyParameterDerivatives() {
System system;
system.addParticle(1.0);
system.addParticle(2.0);
system.addParticle(3.0);
system.addParticle(4.0);
system.addParticle(5.0);
CustomCentroidBondForce* force = new CustomCentroidBondForce(2, "k*(distance(g1,g2)-r0)^2");
force->addGlobalParameter("r0", 0.0);
force->addGlobalParameter("k", 0.0);
force->addEnergyParameterDerivative("r0");
force->addEnergyParameterDerivative("k");
vector<int> particles1;
particles1.push_back(0);
particles1.push_back(1);
vector<int> particles2;
particles2.push_back(2);
particles2.push_back(3);
particles2.push_back(4);
force->addGroup(particles1);
force->addGroup(particles2);
vector<int> groups;
groups.push_back(0);
groups.push_back(1);
vector<double> parameters;
force->addBond(groups, parameters);
system.addForce(force);
// The center of mass of group 0 is (1.5, 0, 0).
vector<Vec3> positions(5);
positions[0] = Vec3(2.5, 0, 0);
positions[1] = Vec3(1, 0, 0);
// The center of mass of group 1 is (-1, 0, 0).
positions[2] = Vec3(-6, 0, 0);
positions[3] = Vec3(-1, 0, 0);
positions[4] = Vec3(2, 0, 0);
// Check the derivatives.
VerletIntegrator integrator(0.01);
Context context(system, integrator, platform);
context.setPositions(positions);
for (int i = 0; i < 10; i++) {
double r0 = 0.1*i;
double k = 10-i;
context.setParameter("r0", r0);
context.setParameter("k", k);
State state = context.getState(State::ParameterDerivatives);
map<string, double> derivs = state.getEnergyParameterDerivatives();
double dEdr0 = -2*k*(2.5-r0);
double dEdk = (2.5-r0)*(2.5-r0);
ASSERT_EQUAL_TOL(dEdr0, derivs["r0"], 1e-5);
ASSERT_EQUAL_TOL(dEdk, derivs["k"], 1e-5);
}
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -343,6 +402,7 @@ int main(int argc, char* argv[]) { ...@@ -343,6 +402,7 @@ int main(int argc, char* argv[]) {
testCustomWeights(); testCustomWeights();
testIllegalVariable(); testIllegalVariable();
testPeriodic(); testPeriodic();
testEnergyParameterDerivatives();
runPlatformTests(); runPlatformTests();
} }
catch(const exception& e) { catch(const exception& e) {
......
...@@ -406,6 +406,48 @@ void testPeriodic() { ...@@ -406,6 +406,48 @@ void testPeriodic() {
} }
} }
void testEnergyParameterDerivatives() {
System system;
system.addParticle(1.0);
system.addParticle(1.0);
system.addParticle(1.0);
system.addParticle(1.0);
VerletIntegrator integrator(0.01);
CustomCompoundBondForce* custom = new CustomCompoundBondForce(4, "k*(dihedral(p1,p2,p3,p4)-theta0)^2");
custom->addGlobalParameter("theta0", 0.0);
custom->addGlobalParameter("k", 0.0);
custom->addEnergyParameterDerivative("theta0");
custom->addEnergyParameterDerivative("k");
vector<int> particles(4);
particles[0] = 0;
particles[1] = 1;
particles[2] = 2;
particles[3] = 3;
vector<double> parameters;
custom->addBond(particles, parameters);
system.addForce(custom);
Context context(system, integrator, platform);
vector<Vec3> positions(4);
positions[0] = Vec3(0, 2, 0);
positions[1] = Vec3(0, 0, 0);
positions[2] = Vec3(1, 0, 0);
positions[3] = Vec3(1, 1, 1);
context.setPositions(positions);
double theta = M_PI/4;
for (int i = 0; i < 10; i++) {
double theta0 = 0.1*i;
double k = 10-i;
context.setParameter("theta0", theta0);
context.setParameter("k", k);
State state = context.getState(State::ParameterDerivatives);
map<string, double> derivs = state.getEnergyParameterDerivatives();
double dEdtheta0 = -2*k*(theta-theta0);
double dEdk = (theta-theta0)*(theta-theta0);
ASSERT_EQUAL_TOL(dEdtheta0, derivs["theta0"], 1e-5);
ASSERT_EQUAL_TOL(dEdk, derivs["k"], 1e-5);
}
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -418,6 +460,7 @@ int main(int argc, char* argv[]) { ...@@ -418,6 +460,7 @@ int main(int argc, char* argv[]) {
testMultipleBonds(); testMultipleBonds();
testIllegalVariable(); testIllegalVariable();
testPeriodic(); testPeriodic();
testEnergyParameterDerivatives();
runPlatformTests(); runPlatformTests();
} }
catch(const exception& e) { catch(const exception& e) {
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2008-2015 Stanford University and the Authors. * * Portions copyright (c) 2008-2016 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -488,6 +488,54 @@ void testIllegalVariable() { ...@@ -488,6 +488,54 @@ void testIllegalVariable() {
ASSERT(threwException); ASSERT(threwException);
} }
void testEnergyParameterDerivatives() {
// Create a box of particles.
const int numParticles = 40;
const int numParameters = 4;
const double boxSize = 2.0;
const double delta = 1e-3;
const string paramNames[] = {"A", "B", "C", "D"};
const double paramValues[] = {0.8, 2.1, 3.2, 1.3};
System system;
system.setDefaultPeriodicBoxVectors(Vec3(boxSize, 0, 0), Vec3(0, boxSize, 0), Vec3(0, 0, boxSize));
CustomGBForce* force = new CustomGBForce();
system.addForce(force);
force->addComputedValue("a", "0.5*(r-A)^2", CustomGBForce::ParticlePair);
force->addComputedValue("b", "a+B", CustomGBForce::SingleParticle);
force->addEnergyTerm("C*(a1+b1+a2+b2+r)^0.8", CustomGBForce::ParticlePair);
force->addEnergyTerm("(D-B)*b", CustomGBForce::SingleParticle);
for (int i = 0; i < numParameters; i++)
force->addGlobalParameter(paramNames[i], paramValues[i]);
for (int i = numParameters-1; i >= 0; i--)
force->addEnergyParameterDerivative(paramNames[i]);
force->setNonbondedMethod(CustomGBForce::CutoffPeriodic);
force->setCutoffDistance(1.0);
vector<Vec3> positions;
vector<double> parameters;
OpenMM_SFMT::SFMT sfmt;
init_gen_rand(0, sfmt);
for (int i = 0; i < numParticles; i++) {
system.addParticle(1.0);
force->addParticle(parameters);
positions.push_back(Vec3(genrand_real2(sfmt), genrand_real2(sfmt), genrand_real2(sfmt))*boxSize);
}
// Compute the energy derivative and compare it to a finite difference approximation.
VerletIntegrator integrator(0.01);
Context context(system, integrator, platform);
context.setPositions(positions);
map<string, double> derivs = context.getState(State::ParameterDerivatives).getEnergyParameterDerivatives();
for (int i = 0; i < numParameters; i++) {
context.setParameter(paramNames[i], paramValues[i]+delta);
double energy1 = context.getState(State::Energy).getPotentialEnergy();
context.setParameter(paramNames[i], paramValues[i]-delta);
double energy2 = context.getState(State::Energy).getPotentialEnergy();
ASSERT_EQUAL_TOL((energy1-energy2)/(2*delta), derivs[paramNames[i]], 5e-3);
}
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -502,6 +550,7 @@ int main(int argc, char* argv[]) { ...@@ -502,6 +550,7 @@ int main(int argc, char* argv[]) {
testPositionDependence(); testPositionDependence();
testExclusions(); testExclusions();
testIllegalVariable(); testIllegalVariable();
testEnergyParameterDerivatives();
runPlatformTests(); runPlatformTests();
} }
catch(const exception& e) { catch(const exception& e) {
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2008-2015 Stanford University and the Authors. * * Portions copyright (c) 2008-2016 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -29,15 +29,21 @@ ...@@ -29,15 +29,21 @@
* USE OR OTHER DEALINGS IN THE SOFTWARE. * * USE OR OTHER DEALINGS IN THE SOFTWARE. *
* -------------------------------------------------------------------------- */ * -------------------------------------------------------------------------- */
#ifdef WIN32
#define _USE_MATH_DEFINES // Needed to get M_PI
#endif
#include "openmm/internal/AssertionUtilities.h" #include "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.h" #include "openmm/Context.h"
#include "openmm/AndersenThermostat.h" #include "openmm/AndersenThermostat.h"
#include "openmm/CustomAngleForce.h"
#include "openmm/CustomBondForce.h"
#include "openmm/CustomIntegrator.h"
#include "openmm/HarmonicBondForce.h" #include "openmm/HarmonicBondForce.h"
#include "openmm/NonbondedForce.h" #include "openmm/NonbondedForce.h"
#include "openmm/System.h" #include "openmm/System.h"
#include "openmm/CustomIntegrator.h"
#include "SimTKOpenMMRealType.h" #include "SimTKOpenMMRealType.h"
#include "sfmt/SFMT.h" #include "sfmt/SFMT.h"
#include <cmath>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
...@@ -770,6 +776,88 @@ void testChangingGlobal() { ...@@ -770,6 +776,88 @@ void testChangingGlobal() {
} }
} }
/**
* Test steps that depend on derivatives of the energy with respect to parameters.
*/
void testEnergyParameterDerivatives() {
System system;
for (int i = 0; i < 3; i++)
system.addParticle(1.0);
// Create some custom forces that depend on parameters.
CustomBondForce* bonds = new CustomBondForce("K*(A*r-r0)^2");
system.addForce(bonds);
bonds->addGlobalParameter("K", 2.0);
bonds->addGlobalParameter("A", 1.0);
bonds->addGlobalParameter("r0", 1.5);
bonds->addEnergyParameterDerivative("K");
bonds->addEnergyParameterDerivative("r0");
bonds->addBond(0, 1);
bonds->setForceGroup(0);
CustomAngleForce* angles = new CustomAngleForce("K*(B*theta-theta0)^2");
system.addForce(angles);
angles->addGlobalParameter("K", 2.0);
angles->addGlobalParameter("B", 1.0);
angles->addGlobalParameter("theta0", M_PI/3);
angles->addEnergyParameterDerivative("K");
angles->addEnergyParameterDerivative("theta0");
angles->addAngle(0, 1, 2);
angles->setForceGroup(1);
// Create an integrator that records parameter derivatives.
CustomIntegrator integrator(0.1);
integrator.addGlobalVariable("dEdK", 0.0);
integrator.addGlobalVariable("dEdr0", 0.0);
integrator.addPerDofVariable("dEdtheta0", 0.0);
integrator.addGlobalVariable("dEdK_0", 0.0);
integrator.addPerDofVariable("dEdr0_0", 0.0);
integrator.addGlobalVariable("dEdtheta0_0", 0.0);
integrator.addPerDofVariable("dEdK_1", 0.0);
integrator.addGlobalVariable("dEdr0_1", 0.0);
integrator.addGlobalVariable("dEdtheta0_1", 0.0);
integrator.addComputeGlobal("dEdK", "deriv(energy, K)");
integrator.addComputeGlobal("dEdr0", "deriv(energy, r0)");
integrator.addComputePerDof("dEdtheta0", "deriv(energy, theta0)");
integrator.addComputeGlobal("dEdK_0", "deriv(energy0, K)");
integrator.addComputePerDof("dEdr0_0", "deriv(energy0, r0)");
integrator.addComputeGlobal("dEdtheta0_0", "deriv(energy0, theta0)");
integrator.addComputePerDof("dEdK_1", "deriv(energy1, K)");
integrator.addComputeGlobal("dEdr0_1", "deriv(energy1, r0)");
integrator.addComputeGlobal("dEdtheta0_1", "deriv(energy1, theta0)");
// Create a Context.
Context context(system, integrator, platform);
vector<Vec3> positions(3);
positions[0] = Vec3(0, 1, 0);
positions[1] = Vec3(0, 0, 0);
positions[2] = Vec3(1, 0, 0);
context.setPositions(positions);
// Check the results.
integrator.step(1);
vector<Vec3> values;
double dEdK_0 = (1.0-1.5)*(1.0-1.5);
double dEdK_1 = (M_PI/2-M_PI/3)*(M_PI/2-M_PI/3);
ASSERT_EQUAL_TOL(dEdK_0, integrator.getGlobalVariableByName("dEdK_0"), 1e-5);
integrator.getPerDofVariableByName("dEdK_1", values);
ASSERT_EQUAL_TOL(dEdK_1, values[0][2], 1e-5);
ASSERT_EQUAL_TOL(dEdK_0+dEdK_1, integrator.getGlobalVariableByName("dEdK"), 1e-5);
double dEdr0 = -2.0*2.0*(1.0-1.5);
integrator.getPerDofVariableByName("dEdr0_0", values);
ASSERT_EQUAL_TOL(dEdr0, values[1][0], 1e-5);
ASSERT_EQUAL_TOL(0.0, integrator.getGlobalVariableByName("dEdr0_1"), 1e-5);
ASSERT_EQUAL_TOL(dEdr0, integrator.getGlobalVariableByName("dEdr0"), 1e-5);
double dEdtheta0 = -2.0*2.0*(M_PI/2-M_PI/3);
ASSERT_EQUAL_TOL(0.0, integrator.getGlobalVariableByName("dEdtheta0_0"), 1e-5);
ASSERT_EQUAL_TOL(dEdtheta0, integrator.getGlobalVariableByName("dEdtheta0_1"), 1e-5);
integrator.getPerDofVariableByName("dEdtheta0", values);
ASSERT_EQUAL_TOL(dEdtheta0, values[2][1], 1e-5);
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -790,6 +878,7 @@ int main(int argc, char* argv[]) { ...@@ -790,6 +878,7 @@ int main(int argc, char* argv[]) {
testIfBlock(); testIfBlock();
testWhileBlock(); testWhileBlock();
testChangingGlobal(); testChangingGlobal();
testEnergyParameterDerivatives();
runPlatformTests(); runPlatformTests();
} }
catch(const exception& e) { catch(const exception& e) {
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2008-2015 Stanford University and the Authors. * * Portions copyright (c) 2008-2016 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -1041,6 +1041,84 @@ void testIllegalVariable() { ...@@ -1041,6 +1041,84 @@ void testIllegalVariable() {
ASSERT(threwException); ASSERT(threwException);
} }
void testEnergyParameterDerivatives() {
System system;
system.addParticle(1.0);
system.addParticle(1.0);
system.addParticle(1.0);
VerletIntegrator integrator(0.01);
CustomNonbondedForce* nonbonded = new CustomNonbondedForce("k*(r-r0)^2");
nonbonded->addGlobalParameter("r0", 0.0);
nonbonded->addGlobalParameter("k", 0.0);
nonbonded->addEnergyParameterDerivative("k");
nonbonded->addEnergyParameterDerivative("r0");
vector<double> parameters;
nonbonded->addParticle(parameters);
nonbonded->addParticle(parameters);
nonbonded->addParticle(parameters);
nonbonded->addExclusion(0, 2);
system.addForce(nonbonded);
Context context(system, integrator, platform);
vector<Vec3> positions(3);
positions[0] = Vec3(0, 2, 0);
positions[1] = Vec3(0, 0, 0);
positions[2] = Vec3(1, 0, 0);
context.setPositions(positions);
for (int i = 0; i < 10; i++) {
double r0 = 0.1*i;
double k = 10-i;
context.setParameter("r0", r0);
context.setParameter("k", k);
State state = context.getState(State::ParameterDerivatives);
map<string, double> derivs = state.getEnergyParameterDerivatives();
double dEdr0 = -2*k*((2-r0)+(1-r0));
double dEdk = (2-r0)*(2-r0) + (1-r0)*(1-r0);
ASSERT_EQUAL_TOL(dEdr0, derivs["r0"], 1e-5);
ASSERT_EQUAL_TOL(dEdk, derivs["k"], 1e-5);
}
}
void testEnergyParameterDerivatives2() {
// Create a box of particles.
const int numParticles = 30;
const double boxSize = 2.0;
const double a = 1.0;
const double delta = 1e-3;
System system;
system.setDefaultPeriodicBoxVectors(Vec3(boxSize, 0, 0), Vec3(0, boxSize, 0), Vec3(0, 0, boxSize));
CustomNonbondedForce* nonbonded = new CustomNonbondedForce("(r+a)^-4");
system.addForce(nonbonded);
nonbonded->addGlobalParameter("a", a);
nonbonded->addEnergyParameterDerivative("a");
nonbonded->setNonbondedMethod(CustomNonbondedForce::CutoffPeriodic);
nonbonded->setCutoffDistance(1.0);
nonbonded->setSwitchingDistance(0.9);
nonbonded->setUseSwitchingFunction(true);
nonbonded->setUseLongRangeCorrection(true);
vector<Vec3> positions;
vector<double> parameters;
OpenMM_SFMT::SFMT sfmt;
init_gen_rand(0, sfmt);
for (int i = 0; i < numParticles; i++) {
system.addParticle(1.0);
nonbonded->addParticle(parameters);
positions.push_back(Vec3(genrand_real2(sfmt), genrand_real2(sfmt), genrand_real2(sfmt))*boxSize);
}
// Compute the energy derivative and compare it to a finite difference approximation.
VerletIntegrator integrator(0.01);
Context context(system, integrator, platform);
context.setPositions(positions);
map<string, double> derivs = context.getState(State::ParameterDerivatives).getEnergyParameterDerivatives();
context.setParameter("a", a+delta);
double energy1 = context.getState(State::Energy).getPotentialEnergy();
context.setParameter("a", a-delta);
double energy2 = context.getState(State::Energy).getPotentialEnergy();
ASSERT_EQUAL_TOL((energy1-energy2)/(2*delta), derivs["a"], 1e-4);
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -1067,6 +1145,8 @@ int main(int argc, char* argv[]) { ...@@ -1067,6 +1145,8 @@ int main(int argc, char* argv[]) {
testInteractionGroupTabulatedFunction(); testInteractionGroupTabulatedFunction();
testMultipleCutoffs(); testMultipleCutoffs();
testIllegalVariable(); testIllegalVariable();
testEnergyParameterDerivatives();
testEnergyParameterDerivatives2();
runPlatformTests(); runPlatformTests();
} }
catch(const exception& e) { catch(const exception& e) {
......
...@@ -222,6 +222,43 @@ void testPeriodic() { ...@@ -222,6 +222,43 @@ void testPeriodic() {
ASSERT_EQUAL_TOL(1.1*(1+std::cos(2*M_PI/3)), state.getPotentialEnergy(), TOL); ASSERT_EQUAL_TOL(1.1*(1+std::cos(2*M_PI/3)), state.getPotentialEnergy(), TOL);
} }
void testEnergyParameterDerivatives() {
System system;
system.addParticle(1.0);
system.addParticle(1.0);
system.addParticle(1.0);
system.addParticle(1.0);
VerletIntegrator integrator(0.01);
CustomTorsionForce* torsions = new CustomTorsionForce("k*(theta-theta0)^2");
torsions->addGlobalParameter("theta0", 0.0);
torsions->addGlobalParameter("k", 0.0);
torsions->addEnergyParameterDerivative("theta0");
torsions->addEnergyParameterDerivative("k");
vector<double> parameters;
torsions->addTorsion(0, 1, 2, 3, parameters);
system.addForce(torsions);
Context context(system, integrator, platform);
vector<Vec3> positions(4);
positions[0] = Vec3(0, 2, 0);
positions[1] = Vec3(0, 0, 0);
positions[2] = Vec3(1, 0, 0);
positions[3] = Vec3(1, 1, 1);
context.setPositions(positions);
double theta = M_PI/4;
for (int i = 0; i < 10; i++) {
double theta0 = 0.1*i;
double k = 10-i;
context.setParameter("theta0", theta0);
context.setParameter("k", k);
State state = context.getState(State::ParameterDerivatives);
map<string, double> derivs = state.getEnergyParameterDerivatives();
double dEdtheta0 = -2*k*(theta-theta0);
double dEdk = (theta-theta0)*(theta-theta0);
ASSERT_EQUAL_TOL(dEdtheta0, derivs["theta0"], 1e-5);
ASSERT_EQUAL_TOL(dEdk, derivs["k"], 1e-5);
}
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -231,6 +268,7 @@ int main(int argc, char* argv[]) { ...@@ -231,6 +268,7 @@ int main(int argc, char* argv[]) {
testRange(); testRange();
testIllegalVariable(); testIllegalVariable();
testPeriodic(); testPeriodic();
testEnergyParameterDerivatives();
runPlatformTests(); runPlatformTests();
} }
catch(const exception& e) { catch(const exception& e) {
......
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