Skip to content

Commit 208c15f

Browse files
authored
Merge pull request #403 from OpenBioSim/fix_somd_124
Fix SOMD #124
2 parents d6f62cc + 2049dec commit 208c15f

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

doc/source/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ organisation on `GitHub <https://github.com/openbiosim/sire>`__.
2121
incorrectly for systems containing more than one molecule with CMAP terms (e.g.
2222
multi-chain glycoproteins).
2323

24+
* Add convenience function to ``sire.mol.dynamics`` to get current energy trajectory records.
25+
2426
`2025.4.0 <https://github.com/openbiosim/sire/compare/2025.3.0...2025.4.0>`__ - February 2026
2527
---------------------------------------------------------------------------------------------
2628

src/sire/mol/_dynamics.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ def _exit_dynamics_block(
434434
if save_energy:
435435
# should save energy here
436436
nrgs = {}
437+
nrgs_array = []
437438

438439
nrgs["kinetic"] = (
439440
self._omm_state.getKineticEnergy().value_in_unit(
@@ -475,7 +476,8 @@ def _exit_dynamics_block(
475476
nrg += self._pressure * volume
476477
if excess_chemical_potential is not None:
477478
nrg += excess_chemical_potential * num_waters
478-
nrgs[str(sim_lambda_value)] = nrg * kcal_per_mol
479+
# Store the potential energy for the current lambda value.
480+
nrg_sim_lambda_value = nrg
479481

480482
if lambda_windows is not None:
481483
# get the index of the simulation lambda value in the
@@ -494,6 +496,7 @@ def _exit_dynamics_block(
494496
not has_lambda_index
495497
or abs(lambda_index - i) <= num_energy_neighbours
496498
):
499+
key = f"{lambda_value:.5f}"
497500
self._omm_mols.set_lambda(
498501
lambda_value,
499502
rest2_scale=rest2_scale,
@@ -506,9 +509,16 @@ def _exit_dynamics_block(
506509
nrg += self._pressure * volume
507510
if excess_chemical_potential is not None:
508511
nrg += excess_chemical_potential * num_waters
509-
nrgs[str(lambda_value)] = nrg * kcal_per_mol
512+
nrgs[key] = nrg * kcal_per_mol
513+
nrgs_array.append(nrg)
510514
else:
511-
nrgs[str(lambda_value)] = null_energy * kcal_per_mol
515+
nrgs_array.append(null_energy)
516+
nrgs[key] = null_energy * kcal_per_mol
517+
else:
518+
nrgs[f"{sim_lambda_value:.5f}"] = (
519+
nrg_sim_lambda_value * kcal_per_mol
520+
)
521+
nrgs_array.append(nrg_sim_lambda_value)
512522

513523
self._omm_mols.set_lambda(
514524
sim_lambda_value,
@@ -525,6 +535,10 @@ def _exit_dynamics_block(
525535
self._current_time, nrgs, {"lambda": str(sim_lambda_value)}
526536
)
527537

538+
# Store the current energies.
539+
self._nrgs = nrgs
540+
self._nrgs_array = nrgs_array
541+
528542
# update the interpolation lambda value
529543
if self._is_interpolate:
530544
if delta_lambda:
@@ -861,6 +875,14 @@ def current_kinetic_energy(self):
861875
def energy_trajectory(self):
862876
return self._energy_trajectory.clone()
863877

878+
def _current_energy_array(self):
879+
try:
880+
import numpy as np
881+
882+
return np.array(self._nrgs_array)
883+
except Exception:
884+
return None
885+
864886
def step(self, num_steps: int = 1):
865887
"""
866888
Just perform 'num_steps' steps of dynamics, without saving
@@ -2195,6 +2217,13 @@ def energy_trajectory(self, to_pandas: bool = False, to_alchemlyb: bool = False)
21952217
else:
21962218
return t
21972219

2220+
def _current_energy_array(self):
2221+
"""
2222+
Return the current energies as a numpy array, in the same order
2223+
as the energy trajectory columns.
2224+
"""
2225+
return self._d._current_energy_array()
2226+
21982227
def to_xml(self, f=None):
21992228
"""
22002229
Save the current state of the dynamics to XML.

0 commit comments

Comments
 (0)