Skip to content

Commit

Permalink
Add EQ arg to save samples
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBoothroyd committed Jan 15, 2024
1 parent 9fdad11 commit 245b046
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
22 changes: 17 additions & 5 deletions absolv/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def _run_phase_hremd(
temperature: openmm.unit.Quantity,
prepared_system: PreparedSystem,
platform: femto.md.constants.OpenMMPlatform,
output_dir: pathlib.Path | None = None,
) -> tuple[dict[str, float], dict[str, numpy.ndarray]]:
platform = (
femto.md.constants.OpenMMPlatform.REFERENCE
Expand Down Expand Up @@ -222,10 +223,11 @@ def _run_phase_hremd(
)

with tempfile.TemporaryDirectory() as tmp_dir:
tmp_dir = pathlib.Path(tmp_dir)
output_dir = pathlib.Path(tmp_dir) if output_dir is None else output_dir
output_dir.mkdir(parents=True, exist_ok=True)

femto.md.hremd.run_hremd(simulation, states, hremd_config, tmp_dir)
u_kn, n_k = femto.fe.ddg.load_u_kn(tmp_dir / "samples.arrow")
femto.md.hremd.run_hremd(simulation, states, hremd_config, output_dir)
u_kn, n_k = femto.fe.ddg.load_u_kn(output_dir / "samples.arrow")

return femto.fe.ddg.estimate_ddg(u_kn, n_k, temperature)

Expand All @@ -235,6 +237,7 @@ def run_eq(
prepared_system_a: PreparedSystem,
prepared_system_b: PreparedSystem,
platform: femto.md.constants.OpenMMPlatform = femto.md.constants.OpenMMPlatform.CUDA,
output_dir: pathlib.Path | None = None,
) -> absolv.config.Result:
"""Perform a simulation at each lambda window and for each solvent.
Expand All @@ -243,17 +246,26 @@ def run_eq(
prepared_system_a: The prepared system a. See ``setup`` for more details.
prepared_system_b: The prepared system b. See ``setup`` for more details.
platform: The OpenMM platform to run using.
output_dir: The (optional) directory to save HREMD samples to.
"""

results_a, overlap_a = _run_phase_hremd(
config.alchemical_protocol_a, config.temperature, prepared_system_a, platform
config.alchemical_protocol_a,
config.temperature,
prepared_system_a,
platform,
None if output_dir is None else output_dir / "solvant-a",
)

dg_a, dg_a_std = results_a["ddG_kcal_mol"], results_a["ddG_error_kcal_mol"]
# overlap_a = overlap_a["overlap_0"]

results_b, overlap_b = _run_phase_hremd(
config.alchemical_protocol_b, config.temperature, prepared_system_b, platform
config.alchemical_protocol_b,
config.temperature,
prepared_system_b,
platform,
None if output_dir is None else output_dir / "solvant-b",
)

dg_b, dg_b_std = results_b["ddG_kcal_mol"], results_b["ddG_error_kcal_mol"]
Expand Down
2 changes: 2 additions & 0 deletions regression/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ python run.py --solute methanol \
--replica 0 \
--replica 1
```

The results will be written to `results/{timestamp}/{method}-{solute}-{replica}/results.json`.
20 changes: 15 additions & 5 deletions regression/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,14 @@ def run_replica(
parmed.openmm.load_topology(prepared_system_a.topology.to_openmm()),
)

run_fn = absolv.runner.run_neq if method == "neq" else absolv.runner.run_eq
result = run_fn(config, prepared_system_a, prepared_system_b, "CUDA")
if method == "neq":
result = absolv.runner.run_neq(
config, prepared_system_a, prepared_system_b, "CUDA"
)
else:
result = absolv.runner.run_eq(
config, prepared_system_a, prepared_system_b, "CUDA", output_dir
)

(output_dir / "result.json").write_text(result.model_dump_json(indent=2))

Expand Down Expand Up @@ -220,9 +226,13 @@ def main(solutes: list[str], methods: list[str], replicas: list[str]):
logging.info(f"running {method} {solute} {replica}")

replica_dir = output_dir / f"{method}-{solute}-{replica}"
run_replica(
DEFAULT_SYSTEMS[solute], system_generator, method, replica_dir
)

try:
run_replica(
DEFAULT_SYSTEMS[solute], system_generator, method, replica_dir
)
except BaseException as e:
logging.exception(f"failed to run {method} {solute} {replica}", e)


if __name__ == "__main__":
Expand Down

0 comments on commit 245b046

Please sign in to comment.