Skip to content

[BUG] current_potential_energy() is stale after run() unless energy_frequency is set #472

Description

@lohedges

Dynamics.current_potential_energy() returns the energy from before the last run() block unless that block happened to save energies. The integrator advances positions without going through SOMMContext.setPositions(), so the context's _energy_cache is never invalidated, and the only clear on the dynamics path is gated behind if save_energy:.

import sire as sr, openmm

mols = sr.load_test_files("ala.top", "ala.crd")
d = mols.dynamics(timestep="1fs", temper

def direct(d):
    return (
        d.context()
        .getState(getEnergy=True)
        .getPotentialEnergy()
        .value_in_unit(openmm.unit.kilocalorie_per_mole)
    )

print(d.current_potential_energy().value
d.run("5ps")
print(d.current_potential_energy().value
d.run("5ps", energy_frequency="1ps")
print(d.current_potential_energy().value
d.run("5ps")
print(d.current_potential_energy().value
                                       cached         direct
initial                            -5987.8102     -5987.8102
after run(5ps)                     -5987.8102     -6194.3530   <-- stale
after run(5ps, energy_frequency)   -6094.2036     -6094.2036   <-- fine
after run(5ps) again               -6094.2036     -6132.6996   <-- stale

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions