Fix ENERGY CV in gromacs2024 - #1444
Merged
carlocamilloni merged 3 commits intoAug 15, 2026
Merged
Conversation
GROMACS 2024 snapshots force_flags into stepWork before the PLUMED isEnergyNeeded query, so OR-ing GMX_FORCE_ENERGY was ignored and ENERGY was left at 0 except on nstcalcenergy steps. Rebuild the workload when PLUMED needs the potential energy. Fixes plumed#1205 Co-authored-by: Cursor <cursoragent@cursor.com>
The manual stated that ENERGY does not include long tail corrections. For GROMACS that is not the case: the correction is computed inside do_force() and folded into F_EPOT by sum_epot() before PLUMED reads it. Verified against the GROMACS energy file with DispCorr=EnerPres on 2023.5, 2024.3 and 2026.3, in NVT and in NPT where the volume varies by up to 29% and the correction itself by 40 kJ/mol: PLUMED tracks the GROMACS potential energy to 0.005 kJ/mol over 401 frames. The claim is left in place for other codes, which were not tested. Addresses plumed#567 Co-authored-by: Cursor <cursoragent@cursor.com>
Keep the note short and engine-agnostic: what matters to the user is that the energy PLUMED receives may not contain the long tail corrections, and that the energy of the MD engine should then be used for reweighting. Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix the ENERGY collective variable with GROMACS 2024
Branch:
fix-1205-gromacs2024-energy→ basev2.9· Fixes: #1205 (and the root cause behind #1384)Description
With GROMACS 2024 the
ENERGYcollective variable silently returns0.0on every step that isnot a multiple of
nstcalcenergy(default 100), so PLUMED sees a garbage energy on 99 steps outof 100.
OPES_EXPANDEDwithECV_MULTITHERMAL, the well-tempered ensemble,PT-WTE and reweighting on
ENERGYare all affected. Nothing warns the user: the patch appliescleanly, compiles without warnings, and the run completes.
The patch asks GROMACS for the energy by OR-ing bits into
force_flags:In 2023 this worked because
force_flagswent straight into thedo_force()call a few linesbelow. In 2024
do_force()no longer takes a flags argument: the step workload is built bysetupStepWorkload()about 120 lines earlier and stored in the run-schedule object, so thePLUMED line became dead code.
do_force()zeroesterm[F_EPOT]every step viareset_enerdata()and only refills it whenstepWork.computeEnergyis set, sosetEnergyhands PLUMED exactly0.0the rest of the time.GMX_FORCE_VIRIALis lost the sameway, so the virial rescaling PLUMED performs when biasing the energy is wrong as well.
This changes only
patches/gromacs-2024.3.diff/src/gromacs/mdrun/md.cpp, plus aCHANGESentry:force_flagsbeforesetupStepWorkload();runScheduleWork_->stepWorkright afterisEnergyNeededhas answered for this step,which makes the fix exact rather than one step late — this also covers the first step of a run
or of a restart, and input where the energy is needed only on some steps (
PRINT ARG=ene STRIDE=7);gmx_fatalif the energy is still not scheduled, so this cannot regress silently;plumedNeedsEnergylikebCalcVirin the MD GPU graph reset and reuse conditions,since the step workload changes on exactly those steps.
Reproducer.
nstcalcenergy = 100withene: ENERGYandPRINT ARG=ene STRIDE=1: before thefix 99 of every 100 lines read
0.000000; after it, every line is a sane potential energy.Verification
216 SPC waters (648 atoms), OPLS-AA, 0.8 nm cut-offs,
dt = 2 fs, v-rescale at 300 K, LINCS onh-bonds,
nstcalcenergy = 100.ENERGYprinted every step over 250 steps goes from 4 / 251finite non-zero values to 251 / 251; the four survivors in the broken run are exactly the
nstcalcenergysteps. OPES multithermal (ECV_MULTITHERMAL300–350 K,PACE=25,OBSERVATION_STEPS=8, 1500 steps) was checked against a GROMACS 2023.5 run of the same input,which predates the regression:
ENERGY == 0∂V/∂Enon-zero after observationThe temperature count is the sharpest check, because OPES sizes that grid from the energy
fluctuations measured during the observation phase: with the bug present the grid collapses to 2
temperatures ("range very narrow") and every
DeltaFdegenerates. Trajectories are notbit-identical across GROMACS versions, so the comparison is semi-quantitative by construction.
Also: the long tail correction warning in the docs is stale (#567). While checking what the
energy PLUMED receives actually contains, it turned out that the
\bugnote onENERGY— "doesnot include long tail corrections … GROMACS
DispCorr Ener" — no longer holds for GROMACS. Thecorrection is computed inside
do_force()and folded intoF_EPOTbysum_epot(), both beforePLUMED reads the energy. With
DispCorr = EnerPres,ENERGYmatches thePotentialterm in theGROMACS energy file to 0.005 kJ/mol on 2023.5, 2024.3 and 2026.3, while the correction itself
is −154 kJ/mol. Under NPT, where the volume varies by up to 29% and the correction with it by
40 kJ/mol,
ENERGYstill tracks GROMACS to 0.005 kJ/mol over 401 frames — so the correction isnot merely present but correctly updated. The note is reworded rather than deleted, and kept short
and engine-agnostic: codes other than GROMACS, such as LAMMPS with
pair_modify tail yes, were nottested here, and where the mismatch is real the advice to reweight with the energy of the engine
still stands.
Remaining gaps. The
isEnergyNeededmechanism only exists in thedo_forcebranch, sorelax_shell_flexconand the modular simulator are untouched, as before. Everything above wasvalidated on CPU, so the MD GPU graph conditions were not exercised at runtime.
Target release
I would like my code to appear in release 2.9.
Type of contribution
Copyright
Tests
No regtest is added, for the reason the existing
patches/tree carries none: nothing underpatches/is compiled by PLUMED, so the test suite cannot exercise it. It is consumed only whena user runs
plumed patchagainst a GROMACS source tree, at which point GROMACS compiles it. TheGROMACS builds and MD runs above are the substitute.