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 1 commit
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
25 changes: 3 additions & 22 deletions openmmtools/multistate/multistatesampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,11 +1356,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,31 +1370,15 @@ 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))
logger.debug(f'Replica {replica_id + 1}/{self.n_replicas}: final energy {final_energy:8.3f}kT')
# TODO if energy > 0, use slower openmm minimizer

# Clean up the integrator
del context
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ijpulidos : I think we might need to remove the del context if we're using self.energy_context_cache.get_context() since this would delete a Context object under the management of the context cache. I can't recall what the correct behavior is here---I think it was to leave the Context under management.

This was perhaps different for the FIRE minimizer since a different integrator would lead to a different Context we would not re-use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good point, I think we should be okay by removing the del context here.


Expand Down