Commit 87df7223 authored by Peter Eastman's avatar Peter Eastman
Browse files

Throw exception if the user tries to compute forces without setting positions first.

parent d8545b79
......@@ -246,7 +246,7 @@ private:
std::vector<ForceImpl*> forceImpls;
std::map<std::string, double> parameters;
mutable std::vector<std::vector<int> > molecules;
bool hasInitializedForces;
bool hasInitializedForces, hasSetPositions;
int lastForceGroups;
Platform* platform;
Kernel initializeForcesKernel, kineticEnergyKernel, updateStateDataKernel, applyConstraintsKernel, virtualSitesKernel;
......
......@@ -48,7 +48,7 @@ using namespace OpenMM;
using namespace std;
ContextImpl::ContextImpl(Context& owner, System& system, Integrator& integrator, Platform* platform, const map<string, string>& properties) :
owner(owner), system(system), integrator(integrator), hasInitializedForces(false), lastForceGroups(-1), platform(platform), platformData(NULL) {
owner(owner), system(system), integrator(integrator), hasInitializedForces(false), hasSetPositions(false), lastForceGroups(-1), platform(platform), platformData(NULL) {
if (system.getNumParticles() == 0)
throw OpenMMException("Cannot create a Context for a System with no particles");
......@@ -143,6 +143,7 @@ void ContextImpl::getPositions(std::vector<Vec3>& positions) {
}
void ContextImpl::setPositions(const std::vector<Vec3>& positions) {
hasSetPositions = true;
updateStateDataKernel.getAs<UpdateStateDataKernel>().setPositions(*this, positions);
integrator.stateChanged(State::Positions);
}
......@@ -200,6 +201,8 @@ void ContextImpl::computeVirtualSites() {
}
double ContextImpl::calcForcesAndEnergy(bool includeForces, bool includeEnergy, int groups) {
if (!hasSetPositions)
throw OpenMMException("Particle positions have not been set");
lastForceGroups = groups;
CalcForcesAndEnergyKernel& kernel = initializeForcesKernel.getAs<CalcForcesAndEnergyKernel>();
double energy = 0.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