Commit a711ce2a authored by Peter Eastman's avatar Peter Eastman
Browse files

Refactored ForceImpl, lots of KernelImpl subclasses, and other related classes...

Refactored ForceImpl, lots of KernelImpl subclasses, and other related classes to avoid redundant calculations when requesting a State with both forces and energies
parent 767ea1bd
......@@ -82,36 +82,26 @@ public:
*/
virtual void initialize(const System& system) = 0;
/**
* This is called at the beginning of each force computation, before calcForces() has been called on
* This is called at the beginning of each force/energy computation, before calcForcesAndEnergy() has been called on
* any ForceImpl.
*
* @param context the context in which to execute this kernel
* @param includeForce true if forces should be computed
* @param includeEnergy true if potential energy should be computed
*/
virtual void beginForceComputation(ContextImpl& context) = 0;
virtual void beginComputation(ContextImpl& context, bool includeForce, bool includeEnergy) = 0;
/**
* This is called at the end of each force computation, after calcForces() has been called on
* every ForceImpl.
*
* @param context the context in which to execute this kernel
*/
virtual void finishForceComputation(ContextImpl& context) = 0;
/**
* This is called at the beginning of each energy computation, before calcEnergy() has been called on
* any ForceImpl.
*
* @param context the context in which to execute this kernel
*/
virtual void beginEnergyComputation(ContextImpl& context) = 0;
/**
* This is called at the end of each energy computation, after calcEnergy() has been called on
* This is called at the end of each force/energy computation, after calcForcesAndEnergy() has been called on
* every ForceImpl.
*
* @param context the context in which to execute this kernel
* @param includeForce true if forces should be computed
* @param includeEnergy true if potential energy should be computed
* @return the potential energy of the system. This value is added to all values returned by ForceImpls'
* calcEnergy() methods. That is, each force kernel may <i>either</i> return its contribution to the
* calcForcesAndEnergy() methods. That is, each force kernel may <i>either</i> return its contribution to the
* energy directly, <i>or</i> add it to an internal buffer so that it will be included here.
*/
virtual double finishEnergyComputation(ContextImpl& context) = 0;
virtual double finishComputation(ContextImpl& context, bool includeForce, bool includeEnergy) = 0;
};
/**
......@@ -235,18 +225,14 @@ public:
*/
virtual void initialize(const System& system, const HarmonicBondForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the HarmonicBondForce
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -267,18 +253,14 @@ public:
*/
virtual void initialize(const System& system, const CustomBondForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the CustomBondForce
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -299,18 +281,14 @@ public:
*/
virtual void initialize(const System& system, const HarmonicAngleForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the HarmonicAngleForce
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -331,18 +309,14 @@ public:
*/
virtual void initialize(const System& system, const CustomAngleForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the CustomAngleForce
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -363,18 +337,14 @@ public:
*/
virtual void initialize(const System& system, const PeriodicTorsionForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the PeriodicTorsionForce
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -395,18 +365,14 @@ public:
*/
virtual void initialize(const System& system, const RBTorsionForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the RBTorsionForce
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -427,18 +393,14 @@ public:
*/
virtual void initialize(const System& system, const CMAPTorsionForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the CMAPTorsionForce
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -459,18 +421,14 @@ public:
*/
virtual void initialize(const System& system, const CustomTorsionForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the CustomTorsionForce
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -498,18 +456,14 @@ public:
*/
virtual void initialize(const System& system, const NonbondedForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the NonbondedForce
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -535,18 +489,14 @@ public:
*/
virtual void initialize(const System& system, const CustomNonbondedForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the CustomNonbondedForce
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -567,18 +517,14 @@ public:
*/
virtual void initialize(const System& system, const GBSAOBCForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the GBSAOBCForce
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -600,18 +546,14 @@ public:
*/
virtual void initialize(const System& system, const GBVIForce& force, const std::vector<double>& scaledRadii) = 0;
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the GBVIForce
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -637,18 +579,14 @@ public:
*/
virtual void initialize(const System& system, const CustomGBForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the CustomGBForce
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -669,18 +607,14 @@ public:
*/
virtual void initialize(const System& system, const CustomExternalForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the CustomExternalForce
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......@@ -706,18 +640,14 @@ public:
*/
virtual void initialize(const System& system, const CustomHbondForce& force) = 0;
/**
* Execute the kernel to calculate the forces.
*
* @param context the context in which to execute this kernel
*/
virtual void executeForces(ContextImpl& context) = 0;
/**
* Execute the kernel to calculate the energy.
* Execute the kernel to calculate the forces and/or energy.
*
* @param context the context in which to execute this kernel
* @return the potential energy due to the CustomHbondForce
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy due to the force
*/
virtual double executeEnergy(ContextImpl& context) = 0;
virtual double execute(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
};
/**
......
......@@ -51,11 +51,9 @@ public:
return owner;
}
void updateContextState(ContextImpl& context);
void calcForces(ContextImpl& context) {
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy) {
// This force doesn't apply forces to particles.
}
double calcEnergy(ContextImpl& context) {
return 0.0; // This force doesn't contribute to the potential energy.
return 0.0;
}
std::map<std::string, double> getDefaultParameters();
std::vector<std::string> getKernelNames();
......
......@@ -55,8 +55,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters() {
return std::map<std::string, double>(); // This force field doesn't define any parameters.
}
......
......@@ -50,7 +50,7 @@ public:
return owner;
}
void updateContextState(ContextImpl& context);
void calcForces(ContextImpl& context) {
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy) {
// This force doesn't apply forces to particles.
}
double calcEnergy(ContextImpl& context) {
......
......@@ -164,18 +164,18 @@ public:
*/
void applyConstraints(double tol);
/**
* Recalculate all of the forces in the system. After calling this, use getForces() to retrieve
* the forces that were calculated.
* Recalculate all of the forces in the system and/or the potential energy of the system (in kJ/mol).
* After calling this, use getForces() to retrieve the forces that were calculated.
*
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return the potential energy of the system, or 0 if includeEnergy is false
*/
void calcForces();
double calcForcesAndEnergy(bool includeForces, bool includeEnergy);
/**
* Calculate the kinetic energy of the system (in kJ/mol).
*/
double calcKineticEnergy();
/**
* Calculate the potential energy of the system (in kJ/mol).
*/
double calcPotentialEnergy();
/**
* This should be called at the start of each time step. It calls updateContextState() on each
* ForceImpl in the system, allowing them to modify the values of state variables.
......
......@@ -56,8 +56,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters();
std::vector<std::string> getKernelNames();
private:
......
......@@ -56,8 +56,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters();
std::vector<std::string> getKernelNames();
std::vector<std::pair<int, int> > getBondedParticles() const;
......
......@@ -56,8 +56,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters();
std::vector<std::string> getKernelNames();
private:
......
......@@ -56,8 +56,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters();
std::vector<std::string> getKernelNames();
private:
......
......@@ -59,8 +59,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters();
std::vector<std::string> getKernelNames();
/**
......
......@@ -56,8 +56,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters();
std::vector<std::string> getKernelNames();
private:
......
......@@ -56,8 +56,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters();
std::vector<std::string> getKernelNames();
private:
......
......@@ -76,22 +76,16 @@ public:
*/
virtual void updateContextState(ContextImpl& context) = 0;
/**
* Calculate the force on each particle generated by this ForceImpl. The forces should be <i>added</i>
* to the values already present in the array that is passed in. If this ForceImpl does not generate
* any new forces, it should simply return without modifying the array.
*
* @param context the context in which the system is being simulated
* @param forces new forces should be added to the value already stored in this
*/
virtual void calcForces(ContextImpl& context) = 0;
/**
* Calculate this ForceImpl's contribution to the potential energy of the system.
* Calculate the force on each particle generated by this ForceImpl and/or this ForceImpl's
* contribution to the potential energy of the system.
*
* @param context the context in which the system is being simulated
* @param includeForces true if forces should be calculated
* @param includeEnergy true if the energy should be calculated
* @return this force's contribution to the potential energy of the system, or 0 if this
* force does not contribute to potential energy
* force does not contribute to potential energy (or if includeEnergy is false)
*/
virtual double calcEnergy(ContextImpl& context) = 0;
virtual double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy) = 0;
/**
* Get a map containing the default values for all adjustable parameters defined by this ForceImpl. These
* parameters and their default values will automatically be added to the Context.
......
......@@ -53,8 +53,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters() {
return std::map<std::string, double>(); // This force field doesn't define any parameters.
}
......
......@@ -62,8 +62,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters() {
return std::map<std::string, double>(); // This force field doesn't define any parameters.
}
......
......@@ -56,8 +56,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters() {
return std::map<std::string, double>(); // This force field doesn't define any parameters.
}
......
......@@ -56,8 +56,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters() {
return std::map<std::string, double>(); // This force field doesn't define any parameters.
}
......
......@@ -52,7 +52,7 @@ public:
return owner;
}
void updateContextState(ContextImpl& context);
void calcForces(ContextImpl& context) {
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy) {
// This force doesn't apply forces to particles.
}
double calcEnergy(ContextImpl& context) {
......
......@@ -58,8 +58,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters() {
return std::map<std::string, double>(); // This force field doesn't define any parameters.
}
......
......@@ -56,8 +56,7 @@ public:
void updateContextState(ContextImpl& context) {
// This force field doesn't update the state directly.
}
void calcForces(ContextImpl& context);
double calcEnergy(ContextImpl& context);
double calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy);
std::map<std::string, double> getDefaultParameters() {
return std::map<std::string, double>(); // This force field doesn't define any parameters.
}
......
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