Commit 0307a0db authored by Yutong Zhao's avatar Yutong Zhao
Browse files

Context constructor now uses const System&, guaranteeing that System won't be modified.

parent 19757775
...@@ -47,10 +47,10 @@ namespace OpenMM { ...@@ -47,10 +47,10 @@ namespace OpenMM {
class PeriodicTorsionForceImpl : public ForceImpl { class PeriodicTorsionForceImpl : public ForceImpl {
public: public:
PeriodicTorsionForceImpl(PeriodicTorsionForce& owner); PeriodicTorsionForceImpl(const PeriodicTorsionForce& owner);
~PeriodicTorsionForceImpl(); ~PeriodicTorsionForceImpl();
void initialize(ContextImpl& context); void initialize(ContextImpl& context);
PeriodicTorsionForce& getOwner() { const PeriodicTorsionForce& getOwner() const {
return owner; return owner;
} }
void updateContextState(ContextImpl& context) { void updateContextState(ContextImpl& context) {
...@@ -63,7 +63,7 @@ public: ...@@ -63,7 +63,7 @@ public:
std::vector<std::string> getKernelNames(); std::vector<std::string> getKernelNames();
void updateParametersInContext(ContextImpl& context); void updateParametersInContext(ContextImpl& context);
private: private:
PeriodicTorsionForce& owner; const PeriodicTorsionForce& owner;
Kernel kernel; Kernel kernel;
}; };
......
...@@ -47,10 +47,10 @@ namespace OpenMM { ...@@ -47,10 +47,10 @@ namespace OpenMM {
class RBTorsionForceImpl : public ForceImpl { class RBTorsionForceImpl : public ForceImpl {
public: public:
RBTorsionForceImpl(RBTorsionForce& owner); RBTorsionForceImpl(const RBTorsionForce& owner);
~RBTorsionForceImpl(); ~RBTorsionForceImpl();
void initialize(ContextImpl& context); void initialize(ContextImpl& context);
RBTorsionForce& getOwner() { const RBTorsionForce& getOwner() const {
return owner; return owner;
} }
void updateContextState(ContextImpl& context) { void updateContextState(ContextImpl& context) {
...@@ -63,7 +63,7 @@ public: ...@@ -63,7 +63,7 @@ public:
std::vector<std::string> getKernelNames(); std::vector<std::string> getKernelNames();
void updateParametersInContext(ContextImpl& context); void updateParametersInContext(ContextImpl& context);
private: private:
RBTorsionForce& owner; const RBTorsionForce& owner;
Kernel kernel; Kernel kernel;
}; };
......
...@@ -40,6 +40,6 @@ AndersenThermostat::AndersenThermostat(double defaultTemperature, double default ...@@ -40,6 +40,6 @@ AndersenThermostat::AndersenThermostat(double defaultTemperature, double default
setRandomNumberSeed((int) time(NULL)); setRandomNumberSeed((int) time(NULL));
} }
ForceImpl* AndersenThermostat::createImpl() { ForceImpl* AndersenThermostat::createImpl() const {
return new AndersenThermostatImpl(*this); return new AndersenThermostatImpl(*this);
} }
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
using namespace OpenMM; using namespace OpenMM;
using std::vector; using std::vector;
AndersenThermostatImpl::AndersenThermostatImpl(AndersenThermostat& owner) : owner(owner) { AndersenThermostatImpl::AndersenThermostatImpl(const AndersenThermostat& owner) : owner(owner) {
} }
void AndersenThermostatImpl::initialize(ContextImpl& context) { void AndersenThermostatImpl::initialize(ContextImpl& context) {
......
...@@ -92,7 +92,7 @@ void CMAPTorsionForce::setTorsionParameters(int index, int map, int a1, int a2, ...@@ -92,7 +92,7 @@ void CMAPTorsionForce::setTorsionParameters(int index, int map, int a1, int a2,
torsions[index].b4 = b4; torsions[index].b4 = b4;
} }
ForceImpl* CMAPTorsionForce::createImpl() { ForceImpl* CMAPTorsionForce::createImpl() const {
return new CMAPTorsionForceImpl(*this); return new CMAPTorsionForceImpl(*this);
} }
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
using namespace OpenMM; using namespace OpenMM;
using namespace std; using namespace std;
CMAPTorsionForceImpl::CMAPTorsionForceImpl(CMAPTorsionForce& owner) : owner(owner) { CMAPTorsionForceImpl::CMAPTorsionForceImpl(const CMAPTorsionForce& owner) : owner(owner) {
} }
CMAPTorsionForceImpl::~CMAPTorsionForceImpl() { CMAPTorsionForceImpl::~CMAPTorsionForceImpl() {
......
...@@ -37,6 +37,6 @@ using namespace OpenMM; ...@@ -37,6 +37,6 @@ using namespace OpenMM;
CMMotionRemover::CMMotionRemover(int frequency) : frequency(frequency) { CMMotionRemover::CMMotionRemover(int frequency) : frequency(frequency) {
} }
ForceImpl* CMMotionRemover::createImpl() { ForceImpl* CMMotionRemover::createImpl() const {
return new CMMotionRemoverImpl(*this); return new CMMotionRemoverImpl(*this);
} }
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
using namespace OpenMM; using namespace OpenMM;
using std::vector; using std::vector;
CMMotionRemoverImpl::CMMotionRemoverImpl(CMMotionRemover& owner) : owner(owner) { CMMotionRemoverImpl::CMMotionRemoverImpl(const CMMotionRemover& owner) : owner(owner) {
} }
void CMMotionRemoverImpl::initialize(ContextImpl& context) { void CMMotionRemoverImpl::initialize(ContextImpl& context) {
......
...@@ -40,15 +40,15 @@ ...@@ -40,15 +40,15 @@
using namespace OpenMM; using namespace OpenMM;
using namespace std; using namespace std;
Context::Context(System& system, Integrator& integrator) : properties(map<string, string>()) { Context::Context(const System& system, Integrator& integrator) : properties(map<string, string>()) {
impl = new ContextImpl(*this, system, integrator, 0, properties); impl = new ContextImpl(*this, system, integrator, 0, properties);
} }
Context::Context(System& system, Integrator& integrator, Platform& platform) : properties(map<string, string>()) { Context::Context(const System& system, Integrator& integrator, Platform& platform) : properties(map<string, string>()) {
impl = new ContextImpl(*this, system, integrator, &platform, properties); impl = new ContextImpl(*this, system, integrator, &platform, properties);
} }
Context::Context(System& system, Integrator& integrator, Platform& platform, const map<string, string>& properties) : properties(properties) { Context::Context(const System& system, Integrator& integrator, Platform& platform, const map<string, string>& properties) : properties(properties) {
impl = new ContextImpl(*this, system, integrator, &platform, properties); impl = new ContextImpl(*this, system, integrator, &platform, properties);
} }
...@@ -61,10 +61,6 @@ const System& Context::getSystem() const { ...@@ -61,10 +61,6 @@ const System& Context::getSystem() const {
} }
System& Context::getSystem() {
return impl->getSystem();
}
const Integrator& Context::getIntegrator() const { const Integrator& Context::getIntegrator() const {
return impl->getIntegrator(); return impl->getIntegrator();
} }
...@@ -204,7 +200,7 @@ void Context::setVelocities(const vector<Vec3>& velocities) { ...@@ -204,7 +200,7 @@ void Context::setVelocities(const vector<Vec3>& velocities) {
} }
void Context::setVelocitiesToTemperature(double temperature, int randomSeed) { void Context::setVelocitiesToTemperature(double temperature, int randomSeed) {
System& system = impl->getSystem(); const System& system = impl->getSystem();
// Generate the list of Gaussian random numbers. // Generate the list of Gaussian random numbers.
...@@ -263,7 +259,7 @@ void Context::computeVirtualSites() { ...@@ -263,7 +259,7 @@ void Context::computeVirtualSites() {
} }
void Context::reinitialize() { void Context::reinitialize() {
System& system = impl->getSystem(); const System& system = impl->getSystem();
Integrator& integrator = impl->getIntegrator(); Integrator& integrator = impl->getIntegrator();
Platform& platform = impl->getPlatform(); Platform& platform = impl->getPlatform();
integrator.cleanup(); integrator.cleanup();
......
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
using namespace OpenMM; using namespace OpenMM;
using namespace std; using namespace std;
ContextImpl::ContextImpl(Context& owner, System& system, Integrator& integrator, Platform* platform, const map<string, string>& properties) : ContextImpl::ContextImpl(Context& owner, const System& system, Integrator& integrator, Platform* platform, const map<string, string>& properties) :
owner(owner), system(system), integrator(integrator), hasInitializedForces(false), hasSetPositions(false), integratorIsDeleted(false), owner(owner), system(system), integrator(integrator), hasInitializedForces(false), hasSetPositions(false), integratorIsDeleted(false),
lastForceGroups(-1), platform(platform), platformData(NULL) { lastForceGroups(-1), platform(platform), platformData(NULL) {
if (system.getNumParticles() == 0) if (system.getNumParticles() == 0)
......
...@@ -116,7 +116,7 @@ void CustomAngleForce::setAngleParameters(int index, int particle1, int particle ...@@ -116,7 +116,7 @@ void CustomAngleForce::setAngleParameters(int index, int particle1, int particle
angles[index].particle3 = particle3; angles[index].particle3 = particle3;
} }
ForceImpl* CustomAngleForce::createImpl() { ForceImpl* CustomAngleForce::createImpl() const {
return new CustomAngleForceImpl(*this); return new CustomAngleForceImpl(*this);
} }
......
...@@ -43,7 +43,7 @@ using std::set; ...@@ -43,7 +43,7 @@ using std::set;
using std::string; using std::string;
using std::stringstream; using std::stringstream;
CustomAngleForceImpl::CustomAngleForceImpl(CustomAngleForce& owner) : owner(owner) { CustomAngleForceImpl::CustomAngleForceImpl(const CustomAngleForce& owner) : owner(owner) {
} }
CustomAngleForceImpl::~CustomAngleForceImpl() { CustomAngleForceImpl::~CustomAngleForceImpl() {
...@@ -53,8 +53,7 @@ void CustomAngleForceImpl::initialize(ContextImpl& context) { ...@@ -53,8 +53,7 @@ void CustomAngleForceImpl::initialize(ContextImpl& context) {
kernel = context.getPlatform().createKernel(CalcCustomAngleForceKernel::Name(), context); kernel = context.getPlatform().createKernel(CalcCustomAngleForceKernel::Name(), context);
// Check for errors in the specification of bonds. // Check for errors in the specification of bonds.
const System& system = context.getSystem();
System& system = context.getSystem();
vector<double> parameters; vector<double> parameters;
int numParameters = owner.getNumPerAngleParameters(); int numParameters = owner.getNumPerAngleParameters();
for (int i = 0; i < owner.getNumAngles(); i++) { for (int i = 0; i < owner.getNumAngles(); i++) {
......
...@@ -114,7 +114,7 @@ void CustomBondForce::setBondParameters(int index, int particle1, int particle2, ...@@ -114,7 +114,7 @@ void CustomBondForce::setBondParameters(int index, int particle1, int particle2,
bonds[index].particle2 = particle2; bonds[index].particle2 = particle2;
} }
ForceImpl* CustomBondForce::createImpl() { ForceImpl* CustomBondForce::createImpl() const {
return new CustomBondForceImpl(*this); return new CustomBondForceImpl(*this);
} }
......
...@@ -43,7 +43,7 @@ using std::set; ...@@ -43,7 +43,7 @@ using std::set;
using std::string; using std::string;
using std::stringstream; using std::stringstream;
CustomBondForceImpl::CustomBondForceImpl(CustomBondForce& owner) : owner(owner) { CustomBondForceImpl::CustomBondForceImpl(const CustomBondForce& owner) : owner(owner) {
} }
CustomBondForceImpl::~CustomBondForceImpl() { CustomBondForceImpl::~CustomBondForceImpl() {
...@@ -54,7 +54,7 @@ void CustomBondForceImpl::initialize(ContextImpl& context) { ...@@ -54,7 +54,7 @@ void CustomBondForceImpl::initialize(ContextImpl& context) {
// Check for errors in the specification of bonds. // Check for errors in the specification of bonds.
System& system = context.getSystem(); const System& system = context.getSystem();
vector<double> parameters; vector<double> parameters;
int numParameters = owner.getNumPerBondParameters(); int numParameters = owner.getNumPerBondParameters();
for (int i = 0; i < owner.getNumBonds(); i++) { for (int i = 0; i < owner.getNumBonds(); i++) {
......
...@@ -149,7 +149,7 @@ void CustomCompoundBondForce::setFunctionParameters(int index, const std::string ...@@ -149,7 +149,7 @@ void CustomCompoundBondForce::setFunctionParameters(int index, const std::string
functions[index].max = max; functions[index].max = max;
} }
ForceImpl* CustomCompoundBondForce::createImpl() { ForceImpl* CustomCompoundBondForce::createImpl() const {
return new CustomCompoundBondForceImpl(*this); return new CustomCompoundBondForceImpl(*this);
} }
......
...@@ -71,7 +71,7 @@ public: ...@@ -71,7 +71,7 @@ public:
} }
}; };
CustomCompoundBondForceImpl::CustomCompoundBondForceImpl(CustomCompoundBondForce& owner) : owner(owner) { CustomCompoundBondForceImpl::CustomCompoundBondForceImpl(const CustomCompoundBondForce& owner) : owner(owner) {
} }
CustomCompoundBondForceImpl::~CustomCompoundBondForceImpl() { CustomCompoundBondForceImpl::~CustomCompoundBondForceImpl() {
...@@ -82,7 +82,7 @@ void CustomCompoundBondForceImpl::initialize(ContextImpl& context) { ...@@ -82,7 +82,7 @@ void CustomCompoundBondForceImpl::initialize(ContextImpl& context) {
// Check for errors in the specification of parameters and exclusions. // Check for errors in the specification of parameters and exclusions.
System& system = context.getSystem(); const System& system = context.getSystem();
vector<int> particles; vector<int> particles;
vector<double> parameters; vector<double> parameters;
int numBondParameters = owner.getNumPerBondParameters(); int numBondParameters = owner.getNumPerBondParameters();
......
...@@ -112,7 +112,7 @@ void CustomExternalForce::setParticleParameters(int index, int particle, const v ...@@ -112,7 +112,7 @@ void CustomExternalForce::setParticleParameters(int index, int particle, const v
particles[index].particle = particle; particles[index].particle = particle;
} }
ForceImpl* CustomExternalForce::createImpl() { ForceImpl* CustomExternalForce::createImpl() const {
return new CustomExternalForceImpl(*this); return new CustomExternalForceImpl(*this);
} }
......
...@@ -43,7 +43,7 @@ using std::set; ...@@ -43,7 +43,7 @@ using std::set;
using std::string; using std::string;
using std::stringstream; using std::stringstream;
CustomExternalForceImpl::CustomExternalForceImpl(CustomExternalForce& owner) : owner(owner) { CustomExternalForceImpl::CustomExternalForceImpl(const CustomExternalForce& owner) : owner(owner) {
} }
CustomExternalForceImpl::~CustomExternalForceImpl() { CustomExternalForceImpl::~CustomExternalForceImpl() {
...@@ -54,7 +54,7 @@ void CustomExternalForceImpl::initialize(ContextImpl& context) { ...@@ -54,7 +54,7 @@ void CustomExternalForceImpl::initialize(ContextImpl& context) {
// Check for errors in the specification of bonds. // Check for errors in the specification of bonds.
System& system = context.getSystem(); const System& system = context.getSystem();
vector<double> parameters; vector<double> parameters;
int numParameters = owner.getNumPerParticleParameters(); int numParameters = owner.getNumPerParticleParameters();
for (int i = 0; i < owner.getNumParticles(); i++) { for (int i = 0; i < owner.getNumParticles(); i++) {
......
...@@ -202,7 +202,7 @@ void CustomGBForce::setFunctionParameters(int index, const std::string& name, co ...@@ -202,7 +202,7 @@ void CustomGBForce::setFunctionParameters(int index, const std::string& name, co
functions[index].max = max; functions[index].max = max;
} }
ForceImpl* CustomGBForce::createImpl() { ForceImpl* CustomGBForce::createImpl() const {
return new CustomGBForceImpl(*this); return new CustomGBForceImpl(*this);
} }
......
...@@ -43,7 +43,7 @@ using std::set; ...@@ -43,7 +43,7 @@ using std::set;
using std::string; using std::string;
using std::stringstream; using std::stringstream;
CustomGBForceImpl::CustomGBForceImpl(CustomGBForce& owner) : owner(owner) { CustomGBForceImpl::CustomGBForceImpl(const CustomGBForce& owner) : owner(owner) {
} }
CustomGBForceImpl::~CustomGBForceImpl() { CustomGBForceImpl::~CustomGBForceImpl() {
...@@ -54,7 +54,7 @@ void CustomGBForceImpl::initialize(ContextImpl& context) { ...@@ -54,7 +54,7 @@ void CustomGBForceImpl::initialize(ContextImpl& context) {
// Check for errors in the specification of parameters and exclusions. // Check for errors in the specification of parameters and exclusions.
System& system = context.getSystem(); const System& system = context.getSystem();
if (owner.getNumParticles() != system.getNumParticles()) if (owner.getNumParticles() != system.getNumParticles())
throw OpenMMException("CustomGBForce must have exactly as many particles as the System it belongs to."); throw OpenMMException("CustomGBForce must have exactly as many particles as the System it belongs to.");
vector<set<int> > exclusions(owner.getNumParticles()); vector<set<int> > exclusions(owner.getNumParticles());
......
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