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

Bug hunt fixes #225

Merged
merged 28 commits into from
Dec 2, 2022
Merged

Bug hunt fixes #225

merged 28 commits into from
Dec 2, 2022

Conversation

IAlibay
Copy link
Member

@IAlibay IAlibay commented Nov 11, 2022

Work done in this PR:

  • Up to 0b71e23 : Fixes the constraint demapping call (a typo in the assigned order of the map items 😓 )
    • If we can follow this up with some actual tests that'd be grand. For now this fully replicate's perses.
  • 35f37ee : Add virtual bond for imaging which was missed from the vendored HTF code
    • Removed the "center in box" code, which was both problematic and uncessary.
  • Added minimize call on creation of thermodynamic states
  • Added experimental code for CPU fallback when GPU minimisation fails (possibly remove).

To-do:

  • XML diff tests of p38 edge and benzene/phenol edge
  • Coordinates diffs tests

@codecov
Copy link

codecov bot commented Nov 11, 2022

Codecov Report

Base: 92.44% // Head: 92.52% // Increases project coverage by +0.08% 🎉

Coverage data is based on head (cd36a0e) compared to base (f1140ea).
Patch coverage: 99.21% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #225      +/-   ##
==========================================
+ Coverage   92.44%   92.52%   +0.08%     
==========================================
  Files          57       57              
  Lines        4170     4270     +100     
==========================================
+ Hits         3855     3951      +96     
- Misses        315      319       +4     
Impacted Files Coverage Δ
openfe/protocols/openmm_rbfe/equil_rbfe_methods.py 86.47% <85.71%> (-0.33%) ⬇️
...fe/protocols/openmm_rbfe/_rbfe_utils/multistate.py 60.57% <100.00%> (+4.66%) ⬆️
...otocols/openmm_rbfe/_rbfe_utils/topologyhelpers.py 94.48% <100.00%> (+0.73%) ⬆️
openfe/tests/setup/test_openmm_equil_protocols.py 100.00% <100.00%> (ø)
...enfe/protocols/openmm_rbfe/_rbfe_utils/relative.py 80.61% <0.00%> (-0.38%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@IAlibay IAlibay changed the title Fix demapping [wip] Bug hunt fixes Nov 13, 2022
@@ -115,8 +116,7 @@ class creation of LambdaProtocol.
context, context_integrator = context_cache.get_context(
compound_thermostate_copy)
# TODO: move to our own MD tasks
# feptasks.minimize(compound_thermodynamic_state_copy,
# sampler_state)
#minimize(compound_thermostate_copy, sampler_state)
Copy link
Member Author

Choose a reason for hiding this comment

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

The original perses code does the minimization here, however in my opinion it's a bit problematic to have it hidden here. It would be better for this to be a) controlled, b) easy to spot in the protocol.

Comment on lines 151 to 222
def _minimize_replica(self, replica_id, tolerance, max_iterations):
"""Minimize the specified replica.
"""

# Retrieve thermodynamic and sampler states.
thermodynamic_state_id = self._replica_thermodynamic_states[replica_id]
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)
# inform of platform used in current context
logger.debug(f"{type(integrator).__name__}: Minimize using {context.getPlatform().getName()} platform.")

# Set initial positions and box vectors.
sampler_state.apply_to_context(context)

# Compute the initial energy of the system for logging.
initial_energy = thermodynamic_state.reduced_potential(context)
logger.debug('Replica {}/{}: initial energy {:8.3f}kT'.format(
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

# 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)

# If energy > 0 kT and on a GPU device attempt to use CPU L-BFGS minimizer
if final_energy > 0 and context.getPlatform().getName() in ['CUDA', 'OpenCL']:
logger.debug(f'Positive final FIRE minimizer energy {final_energy}; falling back to CPU L-BFGS')
sampler_state.apply_to_context(context)
integrator = openmm.VerletIntegrator(1.0)
cpu_platform = openmm.Platform.getPlatformByName('CPU')
context = thermodynamic_state.create_context(integrator, cpu_platform)
sampler_state.apply_to_context(context, ignore_velocities=True)
openmm.LocalEnergyMinimizer.minimize(context, tolerance, max_iterations)

# Get the minimized positions
sampler_state.update_from_context(context)

# Get the final energy
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))

# Clean up the integrator
del context

# Return minimized positions.
return sampler_state.positions
Copy link
Member Author

Choose a reason for hiding this comment

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

This probably can go away for now, but we should try to push something like this to openmmtools. Ideally we need a) optional platform choice for minimisation (that isn't in the context cache), and b) fallback to CPU L-BFGS when GPU optimization doesn't get anywhere.

Also - I think here we want the positions to be reset to their pre GPU minimised values before the CPU L-BFGS fallback. I've had some interesting failures yesterday that indicate that the failed GPU minimisation might take things to a bad configurational state.

@@ -2179,6 +2187,86 @@ def _handle_old_new_exceptions(self):
sigma_new, epsilon_new, 0, 1]
)

def _impose_virtual_bonds(self):
Copy link
Member Author

Choose a reason for hiding this comment

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

This should be reasonably easy to test. One option is to make this a static method and get it to return the bond force?

@richardjgowers
Copy link
Contributor

test for constraint detection added

@IAlibay
Copy link
Member Author

IAlibay commented Nov 21, 2022

@RiesBen can this gathering stuff not be in a separate PR? It doesn't seem like part of the original bug issue?

@IAlibay
Copy link
Member Author

IAlibay commented Nov 30, 2022

Virtual bond addition doesn't seem to be working for inter-chain things, for some reason in one of my system I seem to be getting a bond between a monomer protein and water O.o

@@ -143,6 +148,79 @@ class creation of LambdaProtocol.
self.create(thermodynamic_states=thermodynamic_state_list,
sampler_states=sampler_state_list, storage=reporter)

def _minimize_replica(self, replica_id, tolerance, max_iterations):
Copy link
Member Author

Choose a reason for hiding this comment

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

As discussed, let's remove this for now and move it to a separate PR

@richardjgowers richardjgowers changed the title [wip] Bug hunt fixes Bug hunt fixes Dec 2, 2022
@richardjgowers richardjgowers merged commit 78166a7 into main Dec 2, 2022
@richardjgowers richardjgowers deleted the fix-htf-torsions branch December 2, 2022 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants