Unverified Commit f652673c authored by peastman's avatar peastman Committed by GitHub
Browse files

Merge pull request #2701 from peastman/integrationgroups

Added Integrator.setIntegrationForceGroups()
parents 8616c593 20185acc
...@@ -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) 2013-2019 Stanford University and the Authors. * * Portions copyright (c) 2013-2020 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -516,7 +516,7 @@ static lbfgsfloatval_t evaluate(void *instance, const lbfgsfloatval_t *x, lbfgsf ...@@ -516,7 +516,7 @@ static lbfgsfloatval_t evaluate(void *instance, const lbfgsfloatval_t *x, lbfgsf
// Compute the forces and energy for this configuration. // Compute the forces and energy for this configuration.
double energy = context.calcForcesAndEnergy(true, true); double energy = context.calcForcesAndEnergy(true, true, context.getIntegrator().getIntegrationForceGroups());
long long* force = (long long*) cc.getPinnedBuffer(); long long* force = (long long*) cc.getPinnedBuffer();
cc.getLongForceBuffer().download(force); cc.getLongForceBuffer().download(force);
double forceScale = -1.0/0x100000000; double forceScale = -1.0/0x100000000;
......
...@@ -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) 2011-2014 Stanford University and the Authors. * * Portions copyright (c) 2011-2020 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -497,7 +497,7 @@ static lbfgsfloatval_t evaluate(void *instance, const lbfgsfloatval_t *x, lbfgsf ...@@ -497,7 +497,7 @@ static lbfgsfloatval_t evaluate(void *instance, const lbfgsfloatval_t *x, lbfgsf
vector<Vec3>& force = extractForces(context); vector<Vec3>& force = extractForces(context);
for (int i = 0; i < numDrudeParticles; i++) for (int i = 0; i < numDrudeParticles; i++)
pos[drudeParticles[i]] = Vec3(x[3*i], x[3*i+1], x[3*i+2]); pos[drudeParticles[i]] = Vec3(x[3*i], x[3*i+1], x[3*i+2]);
double energy = context.calcForcesAndEnergy(true, true); double energy = context.calcForcesAndEnergy(true, true, context.getIntegrator().getIntegrationForceGroups());
for (int i = 0; i < numDrudeParticles; i++) { for (int i = 0; i < numDrudeParticles; i++) {
Vec3 f = force[drudeParticles[i]]; Vec3 f = force[drudeParticles[i]];
g[3*i] = -f[0]; g[3*i] = -f[0];
......
...@@ -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) 2011-2018 Stanford University and the Authors. * * Portions copyright (c) 2011-2020 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -123,6 +123,7 @@ void CudaIntegrateRPMDStepKernel::initialize(const System& system, const RPMDInt ...@@ -123,6 +123,7 @@ void CudaIntegrateRPMDStepKernel::initialize(const System& system, const RPMDInt
groupsNotContracted -= 1<<group; groupsNotContracted -= 1<<group;
} }
} }
groupsNotContracted &= integrator.getIntegrationForceGroups();
if (maxContractedCopies > 0) { if (maxContractedCopies > 0) {
contractedForces.initialize<long long>(cu, maxContractedCopies*paddedParticles*3, "rpmdContractedForces"); contractedForces.initialize<long long>(cu, maxContractedCopies*paddedParticles*3, "rpmdContractedForces");
contractedPositions.initialize(cu, maxContractedCopies*paddedParticles, elementSize, "rpmdContractedPositions"); contractedPositions.initialize(cu, maxContractedCopies*paddedParticles, elementSize, "rpmdContractedPositions");
......
...@@ -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) 2011-2018 Stanford University and the Authors. * * Portions copyright (c) 2011-2020 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -103,6 +103,7 @@ void OpenCLIntegrateRPMDStepKernel::initialize(const System& system, const RPMDI ...@@ -103,6 +103,7 @@ void OpenCLIntegrateRPMDStepKernel::initialize(const System& system, const RPMDI
groupsNotContracted -= 1<<group; groupsNotContracted -= 1<<group;
} }
} }
groupsNotContracted &= integrator.getIntegrationForceGroups();
if (maxContractedCopies > 0) { if (maxContractedCopies > 0) {
contractedForces.initialize(cl, maxContractedCopies*paddedParticles, forceElementSize, "rpmdContractedForces"); contractedForces.initialize(cl, maxContractedCopies*paddedParticles, forceElementSize, "rpmdContractedForces");
contractedPositions.initialize(cl, maxContractedCopies*paddedParticles, elementSize, "rpmdContractedPositions"); contractedPositions.initialize(cl, maxContractedCopies*paddedParticles, elementSize, "rpmdContractedPositions");
......
...@@ -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) 2011-2013 Stanford University and the Authors. * * Portions copyright (c) 2011-2020 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -99,6 +99,7 @@ void ReferenceIntegrateRPMDStepKernel::initialize(const System& system, const RP ...@@ -99,6 +99,7 @@ void ReferenceIntegrateRPMDStepKernel::initialize(const System& system, const RP
groupsNotContracted -= 1<<group; groupsNotContracted -= 1<<group;
} }
} }
groupsNotContracted &= integrator.getIntegrationForceGroups();
// Create workspace for doing contractions. // Create workspace for doing contractions.
......
...@@ -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-2020 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "openmm/internal/AssertionUtilities.h" #include "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.h" #include "openmm/Context.h"
#include "openmm/CustomExternalForce.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"
...@@ -279,6 +280,31 @@ void testInitialTemperature() { ...@@ -279,6 +280,31 @@ void testInitialTemperature() {
ASSERT_USUALLY_EQUAL_TOL(targetTemperature, temperature, 0.01); ASSERT_USUALLY_EQUAL_TOL(targetTemperature, temperature, 0.01);
} }
void testForceGroups() {
System system;
system.addParticle(1.0);
BrownianIntegrator integrator(0, 1.0, 0.001);
integrator.setIntegrationForceGroups(1<<1);
CustomExternalForce* f1 = new CustomExternalForce("x");
f1->addParticle(0);
f1->setForceGroup(1);
CustomExternalForce* f2 = new CustomExternalForce("y");
f2->addParticle(0);
f2->setForceGroup(2);
system.addForce(f1);
system.addForce(f2);
Context context(system, integrator, platform);
context.setPositions(vector<Vec3>(1));
// Take one step and verify that the position was updated based only on f1.
integrator.step(1);
Vec3 pos = context.getState(State::Positions).getPositions()[0];
ASSERT(pos[0] < 0);
ASSERT(pos[1] == 0);
ASSERT(pos[2] == 0);
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -290,6 +316,7 @@ int main(int argc, char* argv[]) { ...@@ -290,6 +316,7 @@ int main(int argc, char* argv[]) {
testConstrainedMasslessParticles(); testConstrainedMasslessParticles();
testRandomSeed(); testRandomSeed();
testInitialTemperature(); testInitialTemperature();
testForceGroups();
runPlatformTests(); runPlatformTests();
} }
catch(const exception& e) { catch(const exception& e) {
......
...@@ -597,6 +597,7 @@ void testForceGroups() { ...@@ -597,6 +597,7 @@ void testForceGroups() {
integrator.addComputeGlobal("oute", "energy"); integrator.addComputeGlobal("oute", "energy");
integrator.addComputeGlobal("oute1", "energy1"); integrator.addComputeGlobal("oute1", "energy1");
integrator.addComputeGlobal("oute2", "energy2"); integrator.addComputeGlobal("oute2", "energy2");
integrator.setIntegrationForceGroups((1<<1) + (1<<2));
HarmonicBondForce* bonds = new HarmonicBondForce(); HarmonicBondForce* bonds = new HarmonicBondForce();
bonds->addBond(0, 1, 1.5, 1.1); bonds->addBond(0, 1, 1.5, 1.1);
bonds->setForceGroup(1); bonds->setForceGroup(1);
...@@ -606,6 +607,10 @@ void testForceGroups() { ...@@ -606,6 +607,10 @@ void testForceGroups() {
nb->addParticle(0.2, 1, 0); nb->addParticle(0.2, 1, 0);
nb->setForceGroup(2); nb->setForceGroup(2);
system.addForce(nb); system.addForce(nb);
CustomExternalForce* external = new CustomExternalForce("x");
external->addParticle(0);
external->setForceGroup(3);
system.addForce(external);
Context context(system, integrator, platform); Context context(system, integrator, platform);
vector<Vec3> positions(2); vector<Vec3> positions(2);
positions[0] = Vec3(-1, 0, 0); positions[0] = Vec3(-1, 0, 0);
...@@ -633,13 +638,13 @@ void testForceGroups() { ...@@ -633,13 +638,13 @@ void testForceGroups() {
// Make sure they also match the values returned by the Context. // Make sure they also match the values returned by the Context.
State s = context.getState(State::Forces | State::Energy, false); State s = context.getState(State::Forces | State::Energy, false, 6);
State s1 = context.getState(State::Forces | State::Energy, false, 2); State s1 = context.getState(State::Forces | State::Energy, false, 2);
State s2 = context.getState(State::Forces | State::Energy, false, 4); State s2 = context.getState(State::Forces | State::Energy, false, 4);
vector<Vec3> c, c1, c2; vector<Vec3> c, c1, c2;
c = context.getState(State::Forces, false).getForces(); c = s.getForces();
c1 = context.getState(State::Forces, false, 2).getForces(); c1 = s1.getForces();
c2 = context.getState(State::Forces, false, 4).getForces(); c2 = s2.getForces();
ASSERT_EQUAL_VEC(f[0], c[0], 1e-5); ASSERT_EQUAL_VEC(f[0], c[0], 1e-5);
ASSERT_EQUAL_VEC(f[1], c[1], 1e-5); ASSERT_EQUAL_VEC(f[1], c[1], 1e-5);
ASSERT_EQUAL_VEC(f1[0], c1[0], 1e-5); ASSERT_EQUAL_VEC(f1[0], c1[0], 1e-5);
......
...@@ -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-2016 Stanford University and the Authors. * * Portions copyright (c) 2008-2020 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "openmm/internal/AssertionUtilities.h" #include "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.h" #include "openmm/Context.h"
#include "openmm/CustomExternalForce.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"
...@@ -286,6 +287,31 @@ void testInitialTemperature() { ...@@ -286,6 +287,31 @@ void testInitialTemperature() {
ASSERT_USUALLY_EQUAL_TOL(targetTemperature, temperature, 0.01); ASSERT_USUALLY_EQUAL_TOL(targetTemperature, temperature, 0.01);
} }
void testForceGroups() {
System system;
system.addParticle(1.0);
LangevinIntegrator integrator(0.0, 1.0, 0.01);
integrator.setIntegrationForceGroups(1<<1);
CustomExternalForce* f1 = new CustomExternalForce("x");
f1->addParticle(0);
f1->setForceGroup(1);
CustomExternalForce* f2 = new CustomExternalForce("y");
f2->addParticle(0);
f2->setForceGroup(2);
system.addForce(f1);
system.addForce(f2);
Context context(system, integrator, platform);
context.setPositions(vector<Vec3>(1));
// Take one step and verify that the position was updated based only on f1.
integrator.step(1);
Vec3 pos = context.getState(State::Positions).getPositions()[0];
ASSERT(pos[0] < 0);
ASSERT(pos[1] == 0);
ASSERT(pos[2] == 0);
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -297,6 +323,7 @@ int main(int argc, char* argv[]) { ...@@ -297,6 +323,7 @@ int main(int argc, char* argv[]) {
testConstrainedMasslessParticles(); testConstrainedMasslessParticles();
testRandomSeed(); testRandomSeed();
testInitialTemperature(); testInitialTemperature();
testForceGroups();
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-2019 Stanford University and the Authors. * * Portions copyright (c) 2008-2020 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "openmm/internal/AssertionUtilities.h" #include "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.h" #include "openmm/Context.h"
#include "openmm/CustomExternalForce.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"
...@@ -289,6 +290,31 @@ void testInitialTemperature() { ...@@ -289,6 +290,31 @@ void testInitialTemperature() {
ASSERT_USUALLY_EQUAL_TOL(targetTemperature, temperature, 0.01); ASSERT_USUALLY_EQUAL_TOL(targetTemperature, temperature, 0.01);
} }
void testForceGroups() {
System system;
system.addParticle(1.0);
LangevinMiddleIntegrator integrator(0.0, 1.0, 0.01);
integrator.setIntegrationForceGroups(1<<1);
CustomExternalForce* f1 = new CustomExternalForce("x");
f1->addParticle(0);
f1->setForceGroup(1);
CustomExternalForce* f2 = new CustomExternalForce("y");
f2->addParticle(0);
f2->setForceGroup(2);
system.addForce(f1);
system.addForce(f2);
Context context(system, integrator, platform);
context.setPositions(vector<Vec3>(1));
// Take one step and verify that the position was updated based only on f1.
integrator.step(1);
Vec3 pos = context.getState(State::Positions).getPositions()[0];
ASSERT(pos[0] < 0);
ASSERT(pos[1] == 0);
ASSERT(pos[2] == 0);
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -300,6 +326,7 @@ int main(int argc, char* argv[]) { ...@@ -300,6 +326,7 @@ int main(int argc, char* argv[]) {
testConstrainedMasslessParticles(); testConstrainedMasslessParticles();
testRandomSeed(); testRandomSeed();
testInitialTemperature(); testInitialTemperature();
testForceGroups();
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) 2010-2018 Stanford University and the Authors. * * Portions copyright (c) 2010-2020 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -231,6 +231,34 @@ void testLargeForces() { ...@@ -231,6 +231,34 @@ void testLargeForces() {
ASSERT(maxdist < 10.0); ASSERT(maxdist < 10.0);
} }
void testForceGroups() {
// Create a system with two forces, only one of which is in the standard
// integration force groups.
System system;
system.addParticle(1.0);
system.addParticle(1.0);
HarmonicBondForce* bonds1 = new HarmonicBondForce();
HarmonicBondForce* bonds2 = new HarmonicBondForce();
system.addForce(bonds1);
system.addForce(bonds2);
bonds1->addBond(0, 1, 2.0, 1);
bonds2->addBond(0, 1, 4.0, 1);
bonds1->setForceGroup(1);
bonds2->setForceGroup(2);
// Minimize it and check that the bond has the correct length.
VerletIntegrator integrator(0.01);
integrator.setIntegrationForceGroups(1<<1);
Context context(system, integrator, platform);
context.setPositions({Vec3(0, 0, 0), Vec3(5, 0, 0)});
LocalEnergyMinimizer::minimize(context, 1e-5);
State state = context.getState(State::Positions);
Vec3 delta = state.getPositions()[0]-state.getPositions()[1];
ASSERT_EQUAL_TOL(2.0, sqrt(delta.dot(delta)), 1e-4);
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -240,6 +268,7 @@ int main(int argc, char* argv[]) { ...@@ -240,6 +268,7 @@ int main(int argc, char* argv[]) {
testLargeSystem(); testLargeSystem();
testVirtualSites(); testVirtualSites();
testLargeForces(); testLargeForces();
testForceGroups();
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) 2019 Stanford University and the Authors. * * Portions copyright (c) 2019-2020 Stanford University and the Authors. *
* Authors: Andreas Krämer and Andrew C. Simmonett * * Authors: Andreas Krämer and Andrew C. Simmonett *
* Contributors: * * Contributors: *
* * * *
...@@ -510,6 +510,31 @@ void testAPIChangeNumParticles() { ...@@ -510,6 +510,31 @@ void testAPIChangeNumParticles() {
} }
void testForceGroups() {
System system;
system.addParticle(1.0);
NoseHooverIntegrator integrator(1, 1.0, 0.001);
integrator.setIntegrationForceGroups(1<<1);
CustomExternalForce* f1 = new CustomExternalForce("x");
f1->addParticle(0);
f1->setForceGroup(1);
CustomExternalForce* f2 = new CustomExternalForce("y");
f2->addParticle(0);
f2->setForceGroup(2);
system.addForce(f1);
system.addForce(f2);
Context context(system, integrator, platform);
context.setPositions(vector<Vec3>(1));
// Take one step and verify that the position was updated based only on f1.
integrator.step(1);
Vec3 pos = context.getState(State::Positions).getPositions()[0];
ASSERT(pos[0] < 0);
ASSERT(pos[1] == 0);
ASSERT(pos[2] == 0);
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -528,6 +553,7 @@ int main(int argc, char* argv[]) { ...@@ -528,6 +553,7 @@ int main(int argc, char* argv[]) {
constrain = false; testDimerBox(constrain); constrain = false; testDimerBox(constrain);
constrain = true; testDimerBox(constrain); constrain = true; testDimerBox(constrain);
testCheckpoints(); testCheckpoints();
testForceGroups();
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-2016 Stanford University and the Authors. * * Portions copyright (c) 2008-2020 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "openmm/internal/AssertionUtilities.h" #include "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.h" #include "openmm/Context.h"
#include "openmm/CustomExternalForce.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"
...@@ -358,6 +359,31 @@ void testInitialTemperature() { ...@@ -358,6 +359,31 @@ void testInitialTemperature() {
ASSERT_USUALLY_EQUAL_TOL(targetTemperature, temperature, 0.01); ASSERT_USUALLY_EQUAL_TOL(targetTemperature, temperature, 0.01);
} }
void testForceGroups() {
System system;
system.addParticle(1.0);
VariableLangevinIntegrator integrator(0.0, 1.0, 1e-5);
integrator.setIntegrationForceGroups(1<<1);
CustomExternalForce* f1 = new CustomExternalForce("x");
f1->addParticle(0);
f1->setForceGroup(1);
CustomExternalForce* f2 = new CustomExternalForce("y");
f2->addParticle(0);
f2->setForceGroup(2);
system.addForce(f1);
system.addForce(f2);
Context context(system, integrator, platform);
context.setPositions(vector<Vec3>(1));
// Take one step and verify that the position was updated based only on f1.
integrator.step(1);
Vec3 pos = context.getState(State::Positions).getPositions()[0];
ASSERT(pos[0] < 0);
ASSERT(pos[1] == 0);
ASSERT(pos[2] == 0);
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -370,6 +396,7 @@ int main(int argc, char* argv[]) { ...@@ -370,6 +396,7 @@ int main(int argc, char* argv[]) {
testRandomSeed(); testRandomSeed();
testArgonBox(); testArgonBox();
testInitialTemperature(); testInitialTemperature();
testForceGroups();
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-2020 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "openmm/internal/AssertionUtilities.h" #include "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.h" #include "openmm/Context.h"
#include "openmm/CustomExternalForce.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"
...@@ -337,6 +338,31 @@ void testInitialTemperature() { ...@@ -337,6 +338,31 @@ void testInitialTemperature() {
ASSERT_USUALLY_EQUAL_TOL(targetTemperature, temperature, 0.01); ASSERT_USUALLY_EQUAL_TOL(targetTemperature, temperature, 0.01);
} }
void testForceGroups() {
System system;
system.addParticle(1.0);
VariableVerletIntegrator integrator(0.001);
integrator.setIntegrationForceGroups(1<<1);
CustomExternalForce* f1 = new CustomExternalForce("x");
f1->addParticle(0);
f1->setForceGroup(1);
CustomExternalForce* f2 = new CustomExternalForce("y");
f2->addParticle(0);
f2->setForceGroup(2);
system.addForce(f1);
system.addForce(f2);
Context context(system, integrator, platform);
context.setPositions(vector<Vec3>(1));
// Take one step and verify that the position was updated based only on f1.
integrator.step(1);
Vec3 pos = context.getState(State::Positions).getPositions()[0];
ASSERT(pos[0] < 0);
ASSERT(pos[1] == 0);
ASSERT(pos[2] == 0);
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -348,6 +374,7 @@ int main(int argc, char* argv[]) { ...@@ -348,6 +374,7 @@ int main(int argc, char* argv[]) {
testConstrainedMasslessParticles(); testConstrainedMasslessParticles();
testArgonBox(); testArgonBox();
testInitialTemperature(); testInitialTemperature();
testForceGroups();
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-2020 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "openmm/internal/AssertionUtilities.h" #include "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.h" #include "openmm/Context.h"
#include "openmm/CustomExternalForce.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"
...@@ -254,6 +255,31 @@ void testInitialTemperature() { ...@@ -254,6 +255,31 @@ void testInitialTemperature() {
ASSERT_USUALLY_EQUAL_TOL(targetTemperature, temperature, 0.01); ASSERT_USUALLY_EQUAL_TOL(targetTemperature, temperature, 0.01);
} }
void testForceGroups() {
System system;
system.addParticle(1.0);
VerletIntegrator integrator(0.01);
integrator.setIntegrationForceGroups(1<<1);
CustomExternalForce* f1 = new CustomExternalForce("x");
f1->addParticle(0);
f1->setForceGroup(1);
CustomExternalForce* f2 = new CustomExternalForce("y");
f2->addParticle(0);
f2->setForceGroup(2);
system.addForce(f1);
system.addForce(f2);
Context context(system, integrator, platform);
context.setPositions(vector<Vec3>(1));
// Take one step and verify that the position was updated based only on f1.
integrator.step(1);
Vec3 pos = context.getState(State::Positions).getPositions()[0];
ASSERT(pos[0] < 0);
ASSERT(pos[1] == 0);
ASSERT(pos[2] == 0);
}
void runPlatformTests(); void runPlatformTests();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
...@@ -264,6 +290,7 @@ int main(int argc, char* argv[]) { ...@@ -264,6 +290,7 @@ int main(int argc, char* argv[]) {
testConstrainedClusters(); testConstrainedClusters();
testConstrainedMasslessParticles(); testConstrainedMasslessParticles();
testInitialTemperature(); testInitialTemperature();
testForceGroups();
runPlatformTests(); runPlatformTests();
} }
catch(const exception& e) { catch(const exception& e) {
......
...@@ -6,7 +6,7 @@ Simbios, the NIH National Center for Physics-Based Simulation of ...@@ -6,7 +6,7 @@ Simbios, the NIH National Center for Physics-Based Simulation of
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) 2012-2015 Stanford University and the Authors. Portions copyright (c) 2012-2020 Stanford University and the Authors.
Authors: Peter Eastman Authors: Peter Eastman
Contributors: Contributors:
...@@ -250,7 +250,8 @@ class Simulation(object): ...@@ -250,7 +250,8 @@ class Simulation(object):
if next[4]: if next[4]:
getEnergy = True getEnergy = True
state = self.context.getState(getPositions=getPositions, getVelocities=getVelocities, getForces=getForces, state = self.context.getState(getPositions=getPositions, getVelocities=getVelocities, getForces=getForces,
getEnergy=getEnergy, getParameters=True, enforcePeriodicBox=periodic) getEnergy=getEnergy, getParameters=True, enforcePeriodicBox=periodic,
groups=self.context.getIntegrator().getIntegrationForceGroups())
for reporter, next in reports: for reporter, next in reports:
reporter.report(self, state) reporter.report(self, state)
......
...@@ -106,6 +106,33 @@ Parameters: ...@@ -106,6 +106,33 @@ Parameters:
} }
} }
%extend OpenMM::Integrator {
%pythoncode %{
def setIntegrationForceGroups(self, groups):
"""Set which force groups to use for integration. By default, all force groups are included.
Parameters
----------
groups : set or int
a set of indices for which force groups to include when integrating the equations of motion.
Alternatively, the groups can be passed as a single unsigned integer interpreted as a bitmask,
in which case group i will be included if (groups&(1<<i)) != 0.
"""
try:
# is the input integer-like?
groups_mask = int(groups)
except TypeError:
if isinstance(groups, set):
groups_mask = functools.reduce(operator.or_,
((1<<x) & 0xffffffff for x in groups))
else:
raise TypeError('%s is neither an int nor set' % groups)
if groups_mask >= 0x80000000:
groups_mask -= 0x100000000
_openmm.Integrator_setIntegrationForceGroups(self, groups_mask)
%}
}
%extend OpenMM::RPMDIntegrator { %extend OpenMM::RPMDIntegrator {
%pythoncode %{ %pythoncode %{
def getState(self, def getState(self,
......
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