Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing Fire minimization with LocalEnergyMinimizer #672

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 3 additions & 27 deletions openmmtools/multistate/multistatesampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
from openmmtools.multistate.utils import SimulationNaNError
from openmmtools.multistate.pymbar import ParameterError

from openmmtools.integrators import FIREMinimizationIntegrator

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -1356,11 +1354,8 @@ def _minimize_replica(self, replica_id, tolerance, max_iterations):
thermodynamic_state = self._thermodynamic_states[thermodynamic_state_id]
sampler_state = self._sampler_states[replica_id]

# Use the FIRE minimizer
integrator = FIREMinimizationIntegrator(tolerance=tolerance)

# Get context and bound integrator from energy_context_cache
context, integrator = self.energy_context_cache.get_context(thermodynamic_state, integrator)
context, integrator = self.energy_context_cache.get_context(thermodynamic_state)
# inform of platform used in current context
logger.debug(f"{type(integrator).__name__}: Minimize using {context.getPlatform().getName()} platform.")

Expand All @@ -1373,33 +1368,14 @@ def _minimize_replica(self, replica_id, tolerance, max_iterations):
replica_id + 1, self.n_replicas, initial_energy))

# Minimize energy.
try:
if max_iterations == 0:
logger.debug('Using FIRE: tolerance {} minimizing to convergence'.format(tolerance))
while integrator.getGlobalVariableByName('converged') < 1:
integrator.step(50)
else:
logger.debug('Using FIRE: tolerance {} max_iterations {}'.format(tolerance, max_iterations))
integrator.step(max_iterations)
except Exception as e:
if str(e) == 'Particle coordinate is nan':
logger.debug('NaN encountered in FIRE minimizer; falling back to L-BFGS after resetting positions')
sampler_state.apply_to_context(context)
openmm.LocalEnergyMinimizer.minimize(context, tolerance, max_iterations)
else:
raise e
openmm.LocalEnergyMinimizer.minimize(context, tolerance, max_iterations)

# Get the minimized positions.
sampler_state.update_from_context(context)

# Compute the final energy of the system for logging.
final_energy = thermodynamic_state.reduced_potential(sampler_state)
logger.debug('Replica {}/{}: final energy {:8.3f}kT'.format(
replica_id + 1, self.n_replicas, final_energy))
# TODO if energy > 0, use slower openmm minimizer

# Clean up the integrator
del context
logger.debug(f'Replica {replica_id + 1}/{self.n_replicas}: final energy {final_energy:8.3f}kT')

# Return minimized positions.
return sampler_state.positions
Expand Down