Commit 1595bb7b authored by Peter Eastman's avatar Peter Eastman
Browse files

getState() can specify which force groups to use

parent 1878512f
......@@ -123,8 +123,10 @@ public:
* @param enforcePeriodicBox if false, the position of each particle will be whatever position
* is stored in the Context, regardless of periodic boundary conditions. If true, particle
* positions will be translated so the center of every molecule lies in the same periodic box.
* @param groups a set of bit flags for which force groups to include when computing forces
* and energies. Group i will be included if (groups&(1<<i)) != 0. The default value includes all groups.
*/
State getState(int types, bool enforcePeriodicBox=false) const;
State getState(int types, bool enforcePeriodicBox=false, int groups=0xFFFFFFFF) const;
/**
* Set the current time of the simulation (in picoseconds).
*/
......
......@@ -78,7 +78,7 @@ Platform& Context::getPlatform() {
return impl->getPlatform();
}
State Context::getState(int types, bool enforcePeriodicBox) const {
State Context::getState(int types, bool enforcePeriodicBox, int groups) const {
State state(impl->getTime(), impl->getSystem().getNumParticles(), types);
Vec3 periodicBoxSize[3];
impl->getPeriodicBoxVectors(periodicBoxSize[0], periodicBoxSize[1], periodicBoxSize[2]);
......@@ -86,7 +86,7 @@ State Context::getState(int types, bool enforcePeriodicBox) const {
bool includeForces = types&State::Forces;
bool includeEnergy = types&State::Energy;
if (includeForces || includeEnergy) {
double energy = impl->calcForcesAndEnergy(includeForces, includeEnergy);
double energy = impl->calcForcesAndEnergy(includeForces, includeEnergy, groups);
if (includeEnergy)
state.setEnergy(impl->calcKineticEnergy(), energy);
if (includeForces)
......
......@@ -568,6 +568,19 @@ void testForceGroups() {
ASSERT_EQUAL_VEC(Vec3(138.935456*0.2*0.2/4.0, 0, 0), f2[1], 1e-5);
ASSERT_EQUAL_VEC(f1[0]+f2[0], f[0], 1e-5);
ASSERT_EQUAL_VEC(f1[1]+f2[1], f[1], 1e-5);
// Make sure they also match the values returned by the Context.
vector<Vec3> c, c1, c2;
c = context.getState(State::Forces, false).getForces();
c1 = context.getState(State::Forces, false, 2).getForces();
c2 = context.getState(State::Forces, false, 4).getForces();
ASSERT_EQUAL_VEC(f[0], c[0], 1e-5);
ASSERT_EQUAL_VEC(f[1], c[1], 1e-5);
ASSERT_EQUAL_VEC(f1[0], c1[0], 1e-5);
ASSERT_EQUAL_VEC(f1[1], c1[1], 1e-5);
ASSERT_EQUAL_VEC(f2[0], c2[0], 1e-5);
ASSERT_EQUAL_VEC(f2[1], c2[1], 1e-5);
}
/**
......
......@@ -553,6 +553,19 @@ void testForceGroups() {
ASSERT_EQUAL_VEC(Vec3(138.935456*0.2*0.2/4.0, 0, 0), f2[1], 1e-5);
ASSERT_EQUAL_VEC(f1[0]+f2[0], f[0], 1e-5);
ASSERT_EQUAL_VEC(f1[1]+f2[1], f[1], 1e-5);
// Make sure they also match the values returned by the Context.
vector<Vec3> c, c1, c2;
c = context.getState(State::Forces, false).getForces();
c1 = context.getState(State::Forces, false, 2).getForces();
c2 = context.getState(State::Forces, false, 4).getForces();
ASSERT_EQUAL_VEC(f[0], c[0], 1e-5);
ASSERT_EQUAL_VEC(f[1], c[1], 1e-5);
ASSERT_EQUAL_VEC(f1[0], c1[0], 1e-5);
ASSERT_EQUAL_VEC(f1[1], c1[1], 1e-5);
ASSERT_EQUAL_VEC(f2[0], c2[0], 1e-5);
ASSERT_EQUAL_VEC(f2[1], c2[1], 1e-5);
}
/**
......
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