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

[WIP] Do an MD pre-equilibration for AFE simulations #614

Closed
wants to merge 5 commits into from
Closed
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
84 changes: 63 additions & 21 deletions openfe/protocols/openmm_afe/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,37 +148,77 @@

return atom_ids

@staticmethod
def _pre_minimize(system: openmm.System,
positions: omm_unit.Quantity) -> npt.NDArray:
def _pre_equilibrate(
Copy link
Member Author

@IAlibay IAlibay Nov 2, 2023

Choose a reason for hiding this comment

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

I'm wondering if we could modularize the "run a simulation" part of #516 so this could just be imported here? Looking at the TODO items, seems like I'll just end up copying what @hannahbaumann already wrote.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah if that just defined an MDUnit then either protocol could issue that in their DAGs

Copy link
Member Author

Choose a reason for hiding this comment

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

Short term I'd probably not look to break this up into more units, there's a ton of "half way" stuff by this point which we would need to serialise out and throw across units, that's a pain right now - probably post 1.0.

Maybe to explain the short term need here - this is something I need for the ABFE stuff.

maybe the answer is to just go as-is with the code duplication and clean up later?

self,
system: openmm.System,
topology: openmm.app.Topology,
positions: omm_unit.Quantity,
settings: dict[str, SettingsBaseModel],
dry: bool
) -> npt.NDArray:
"""
Short CPU minization of System to avoid GPU NaNs
Run an equilibration to make sure the system is stable
before we throw it at the alchemical world.

Parameters
----------
system : openmm.System
An OpenMM System to minimize.
positionns : openmm.unit.Quantity
Initial positions for the system.
An OpenMM System to equilibrate
topology : openmm.app.Topology
positions : openmm.unit.Quantity
Initial positions for the system
settings : dict[str, SettingsBaseModel]
A dictionary of settings object for the unit.
dry : bool
Whether or not this is a dry run.

Returns
-------
minimized_positions : npt.NDArray
Minimized positions
equilibrated_positions : npt.NDArray
Equilibrated system positions
"""
integrator = openmm.VerletIntegrator(0.001)
context = openmm.Context(
system, integrator,
openmm.Platform.getPlatformByName('CPU'),
if dry:
return positions

platform = compute.get_openmm_platform(

Check warning on line 183 in openfe/protocols/openmm_afe/base.py

View check run for this annotation

Codecov / codecov/patch

openfe/protocols/openmm_afe/base.py#L183

Added line #L183 was not covered by tests
settings['engine_settings'].compute_platform,
)
context.setPositions(positions)
# Do a quick 100 steps minimization, usually avoids NaNs
openmm.LocalEnergyMinimizer.minimize(
context, maxIterations=100

integrator = openmm.LangevinMiddleIntegrator(

Check warning on line 187 in openfe/protocols/openmm_afe/base.py

View check run for this annotation

Codecov / codecov/patch

openfe/protocols/openmm_afe/base.py#L187

Added line #L187 was not covered by tests
to_openmm(settings['thermo_settings'].temperature),
to_openmm(settings['integrator_settings'].collision_rate),
to_openmm(settings['integrator_settings'].timestep),
)

simulation = openmm.app.Simulation(

Check warning on line 193 in openfe/protocols/openmm_afe/base.py

View check run for this annotation

Codecov / codecov/patch

openfe/protocols/openmm_afe/base.py#L193

Added line #L193 was not covered by tests
topology=topology,
system=system,
integrator=integrator,
platform=platform,
)

simulation.context.setPositions(positions)

Check warning on line 200 in openfe/protocols/openmm_afe/base.py

View check run for this annotation

Codecov / codecov/patch

openfe/protocols/openmm_afe/base.py#L200

Added line #L200 was not covered by tests

# TODO: serialize out a PDB of the minimized system
# TODO: actually pass through user settings here
# Minimize
simulation.minimizeEnergy(

Check warning on line 205 in openfe/protocols/openmm_afe/base.py

View check run for this annotation

Codecov / codecov/patch

openfe/protocols/openmm_afe/base.py#L205

Added line #L205 was not covered by tests
maxIterations=100000,
)
state = context.getState(getPositions=True)
minimized_positions = state.getPositions(asNumpy=True)
return minimized_positions

# TODO: write out an XTC of the equilibrated MD?
# TODO: actually pass through user settings here
# Equilibrate
simulation.step(250000)

Check warning on line 212 in openfe/protocols/openmm_afe/base.py

View check run for this annotation

Codecov / codecov/patch

openfe/protocols/openmm_afe/base.py#L212

Added line #L212 was not covered by tests

# Get the positions out
state = simulation.context.getState(getPositions=True)
equilibrated_positions = state.getPositions(asNumpy=True)

Check warning on line 216 in openfe/protocols/openmm_afe/base.py

View check run for this annotation

Codecov / codecov/patch

openfe/protocols/openmm_afe/base.py#L215-L216

Added lines #L215 - L216 were not covered by tests

# Cautiously delete out the context & integrator
del simulation.context, integrator

Check warning on line 219 in openfe/protocols/openmm_afe/base.py

View check run for this annotation

Codecov / codecov/patch

openfe/protocols/openmm_afe/base.py#L219

Added line #L219 was not covered by tests

return equilibrated_positions

Check warning on line 221 in openfe/protocols/openmm_afe/base.py

View check run for this annotation

Codecov / codecov/patch

openfe/protocols/openmm_afe/base.py#L221

Added line #L221 was not covered by tests

def _prepare(
self, verbose: bool,
Expand Down Expand Up @@ -858,7 +898,9 @@
)

# 6. Pre-minimize System (Test + Avoid NaNs)
positions = self._pre_minimize(omm_system, positions)
positions = self._pre_equilibrate(
omm_system, omm_topology, positions, settings, dry
)

# 7. Get lambdas
lambdas = self._get_lambda_schedule(settings)
Expand Down Expand Up @@ -928,7 +970,7 @@

if not dry:
nc = self.shared_basepath / settings['simulation_settings'].output_filename
chk = settings['simulation_settings'].checkpoint_storage

Check warning on line 973 in openfe/protocols/openmm_afe/base.py

View check run for this annotation

Codecov / codecov/patch

openfe/protocols/openmm_afe/base.py#L973

Added line #L973 was not covered by tests
return {
'nc': nc,
'last_checkpoint': chk,
Expand Down
Loading