diff --git a/modules/mol/mm/doc/simulation.rst b/modules/mol/mm/doc/simulation.rst index bbcf1c8026f823ebcb21633a6505a1b0e8df057d..3fd443aea7a1b473c18e5ddfd6714236c67bb29d 100644 --- a/modules/mol/mm/doc/simulation.rst +++ b/modules/mol/mm/doc/simulation.rst @@ -201,6 +201,10 @@ mapped back to the attached structure at any time. .. method:: AddPositionConstraint(index) Fixes the position of the particle with index given in the argument. + This requires to reinitialize the internal openmm Context + (this is expensive!). Positions, velocities, forces, energies etc. + are properly preserved but e.g. states of random number generators etc. + might be lost. :param index: Particle to be fixed :type index: :class:`int` @@ -211,6 +215,10 @@ mapped back to the attached structure at any time. .. method:: AddPositionConstraints(indices) Fixes the position of the atoms with the indices given in in the argument. + This requires to reinitialize the internal openmm Context + (this is expensive!). Positions, velocities, forces, energies etc. + are properly preserved but e.g. states of random number generators etc. + might be lost. :param indices: Particles to be fixed :type indices: :class:`list` @@ -221,6 +229,10 @@ mapped back to the attached structure at any time. .. method:: ResetPositionConstraints() Removes all position constraints. + This requires to reinitialize the internal openmm Context + (this is expensive!). Positions, velocities, forces, energies etc. + are properly preserved but e.g. states of random number generators etc. + might be lost. .. method:: ResetHarmonicBond(index, bond_length, force_constant) @@ -347,7 +359,11 @@ mapped back to the attached structure at any time. .. method:: ResetDistanceConstraint(index, constraint_length) Update of the distance constraint parameters in the simulation **and** - in the attached :class:`Topology` + in the attached :class:`Topology`. + This requires to reinitialize the internal openmm Context + (this is expensive!). Positions, velocities, forces, energies etc. + are properly preserved but e.g. states of random number generators etc. + might be lost. :param index: Distance constraint to be reset :param constraint_length: New constraint length in nm @@ -445,6 +461,10 @@ mapped back to the attached structure at any time. Update of the mass in the simulation **and** in the attached :class:`Topology` + This requires to reinitialize the internal openmm Context + (this is expensive!). Positions, velocities, forces, energies etc. + are properly preserved but e.g. states of random number generators etc. + might be lost. :param index: Mass to be reset :param mass: New mass diff --git a/modules/mol/mm/src/simulation.cc b/modules/mol/mm/src/simulation.cc index 8288f1a5eadb9560c7108972617b3fce1c0de1e0..09f6465a282302ffc68b27415556ea9ef301e604 100644 --- a/modules/mol/mm/src/simulation.cc +++ b/modules/mol/mm/src/simulation.cc @@ -694,7 +694,7 @@ void Simulation::ResetDistanceConstraint(uint index, Real constraint_length){ int particle1, particle2; system_->getConstraintParameters(index,particle1,particle2,dummy); system_->setConstraintParameters(index,particle1,particle2,constraint_length); - context_->reinitialize(); + this->ReinitializeContext(); top_->SetDistanceConstraintParameters(index, constraint_length); } @@ -703,7 +703,7 @@ void Simulation::AddPositionConstraint(uint index){ throw ost::Error("Provided index exceeds number of atoms!"); } system_->setParticleMass(index,0.0); - context_->reinitialize(); + this->ReinitializeContext(); top_->AddPositionConstraint(index); } @@ -717,7 +717,7 @@ void Simulation::AddPositionConstraints(const std::vector<uint>& index){ system_->setParticleMass(*i,0.0); top_->AddPositionConstraint(*i); } - context_->reinitialize(); + this->ReinitializeContext(); } void Simulation::ResetPositionConstraints(){ @@ -726,7 +726,7 @@ void Simulation::ResetPositionConstraints(){ system_->setParticleMass(i,original_masses[i]); } top_->ResetPositionConstraints(); - context_->reinitialize(); + this->ReinitializeContext(); } void Simulation::ResetHarmonicPositionRestraint(uint index, const geom::Vec3& ref_position, Real k, @@ -872,7 +872,7 @@ void Simulation::ResetMass(uint index, Real mass){ throw ost::Error("Provided index exceeds number of atoms!"); } system_->setParticleMass(index,mass); - context_->reinitialize(); + this->ReinitializeContext(); top_->SetMass(index,mass); } @@ -896,5 +896,19 @@ void Simulation::SetPeriodicBoxExtents(geom::Vec3& vec){ context_->setPeriodicBoxVectors(ucell_a,ucell_b,ucell_c); } +void Simulation::ReinitializeContext() { + // reinitializing requires to reset all those things! + // Be aware, state of random number generators etc might not be + // preserved! + OpenMM::State state = context_->getState(OpenMM::State::Positions | + OpenMM::State::Velocities | + OpenMM::State::Forces | + OpenMM::State::Energy | + OpenMM::State::Parameters | + OpenMM::State::ParameterDerivatives); + context_->reinitialize(); + context_->setState(state); +} + }}} diff --git a/modules/mol/mm/src/simulation.hh b/modules/mol/mm/src/simulation.hh index 52aaaa0019761a165bc98e5799d66ab8fb18af0e..89c58b10eb96f8b2b0a5995986d2ea80a47a43b7 100644 --- a/modules/mol/mm/src/simulation.hh +++ b/modules/mol/mm/src/simulation.hh @@ -147,6 +147,8 @@ private: int TimeToNextNotification(); + void ReinitializeContext(); + // loads plugins from directory for OpenMM BUT only once per unique path! static void EnsurePluginsLoaded(const String& plugin_path);