@@ -928,6 +928,55 @@ def _equilibrate(self, index):
928928
929929 return True , index , None
930930
931+ def _compute_energies (self , index ):
932+ """
933+ Compute the energies for a given replica by updating the OpenMM state
934+ within the context and re-evaluating the potential energy.
935+
936+ Energies are currently computed internally by Sire at the end of each
937+ dynamics block, but this approach incurs an overhead due to the cost of
938+ updating the force field parameters within the context when changing
939+ lambda. This alternaitve method is left here for performance testing.
940+
941+ Parameters
942+ ----------
943+
944+ index: int
945+ The index of the replica.
946+
947+ Returns
948+ -------
949+
950+ index: int
951+ The index of the replica.
952+
953+ energies: np.ndarray
954+ The energies of the replica and each state.
955+ """
956+ _logger .info (
957+ f"Computing energies for { _lam_sym } = { self ._lambda_values [index ]:.5f} "
958+ )
959+
960+ # Get the dynamics object.
961+ dynamics = self ._dynamics_cache .get (index )
962+
963+ # Create an array to hold the energies.
964+ energies = _np .zeros (self ._config .num_lambda )
965+
966+ # Loop over the states.
967+ for i in range (self ._config .num_lambda ):
968+ # Set the state.
969+ dynamics ._d ._omm_mols .setState (self ._dynamics_cache ._openmm_states [i ])
970+ dynamics ._d ._clear_state ()
971+
972+ # Compute and store the energy for this state.
973+ energies [i ] = dynamics .current_potential_energy ().value ()
974+
975+ # Reset the state.
976+ dynamics ._d ._omm_mols .setState (self ._dynamics_cache ._openmm_states [index ])
977+
978+ return index , energies
979+
931980 def _assemble_results (self , results ):
932981 """
933982 Assemble the results into a matrix.
0 commit comments