Skip to content

Commit db80aed

Browse files
Fix ENERGY CV in gromacs2024 (#1444)
1 parent 361dcf4 commit db80aed

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

CHANGES/v2.9.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,9 @@ Changes from version 2.8 which are relevant for users:
7979
- Added FMT keyword in \ref METAINFERENCE
8080
- Several fixes in regression tests and in CI
8181
- Fixed an issue where the checkpoint step remains true forever, (see \issue{1428})
82+
83+
## Version 2.9.6 (in progress)
84+
- Fixed the GROMACS 2024 patch so that the potential energy is computed on every step
85+
where PLUMED needs it (ENERGY CV, OPES multithermal, well-tempered ensemble). See \issue{1205}.
86+
- Documentation: \ref ENERGY does include the GROMACS long tail (`DispCorr`) corrections,
87+
contrary to what the manual stated. See \issue{567}.

patches/gromacs-2024.3.diff/src/gromacs/mdrun/md.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,14 @@ void gmx::LegacySimulator::do_md()
13261326
force_flags = (GMX_FORCE_STATECHANGED | ((inputrecDynamicBox(ir)) ? GMX_FORCE_DYNAMICBOX : 0)
13271327
| GMX_FORCE_ALLFORCES | (bCalcVir ? GMX_FORCE_VIRIAL : 0)
13281328
| (bCalcEner ? GMX_FORCE_ENERGY : 0) | (computeDHDL ? GMX_FORCE_DHDL : 0));
1329+
/* PLUMED */
1330+
if (plumedswitch && plumedNeedsEnergy)
1331+
{
1332+
/* GROMACS >= 2024 snapshots force_flags into stepWork below.
1333+
Keep computing energy/virial if PLUMED needed them last step. */
1334+
force_flags |= GMX_FORCE_ENERGY | GMX_FORCE_VIRIAL;
1335+
}
1336+
/* END PLUMED */
13291337
if (simulationWork.useMts && !do_per_step(step, ir->nstfout))
13301338
{
13311339
// TODO: merge this with stepWork.useOnlyMtsCombinedForceBuffer
@@ -1372,7 +1380,7 @@ void gmx::LegacySimulator::do_md()
13721380
{
13731381
// Reset graph on search step (due to changing neighbour list etc)
13741382
// or virial step (due to changing shifts and box).
1375-
if (bNS || bCalcVir)
1383+
if (bNS || bCalcVir || plumedNeedsEnergy)
13761384
{
13771385
fr_->mdGraph[MdGraphEvenOrOddStep::EvenStep]->reset();
13781386
fr_->mdGraph[MdGraphEvenOrOddStep::OddStep]->reset();
@@ -1381,7 +1389,7 @@ void gmx::LegacySimulator::do_md()
13811389
{
13821390
mdGraph->setUsedGraphLastStep(usedMdGpuGraphLastStep);
13831391
bool canUseMdGpuGraphThisStep =
1384-
!bNS && !bCalcVir && !doTemperatureScaling && !doParrinelloRahman && !bGStat
1392+
!bNS && !bCalcVir && !plumedNeedsEnergy && !doTemperatureScaling && !doParrinelloRahman && !bGStat
13851393
&& !needHalfStepKineticEnergy && !do_per_step(step, ir->nstxout)
13861394
&& !do_per_step(step, ir->nstxout_compressed)
13871395
&& !do_per_step(step, ir->nstvout) && !do_per_step(step, ir->nstfout)
@@ -1471,7 +1479,21 @@ void gmx::LegacySimulator::do_md()
14711479
if(pversion>3) plumed_cmd(plumedmain,"doCheckPoint",&checkp);
14721480
plumed_cmd(plumedmain,"setForces",&f.view().force()[0][0]);
14731481
plumed_cmd(plumedmain,"isEnergyNeeded",&plumedNeedsEnergy);
1474-
if(plumedNeedsEnergy) force_flags |= GMX_FORCE_ENERGY | GMX_FORCE_VIRIAL;
1482+
if(plumedNeedsEnergy) {
1483+
/* GROMACS >= 2024 ignores force_flags at the do_force call:
1484+
stepWork was already built above, so rebuild it here. */
1485+
force_flags |= GMX_FORCE_ENERGY | GMX_FORCE_VIRIAL;
1486+
runScheduleWork_->stepWork = setupStepWorkload(
1487+
legacyForceFlags | GMX_FORCE_ENERGY | GMX_FORCE_VIRIAL,
1488+
ir->mtsLevels,
1489+
step,
1490+
runScheduleWork_->domainWork,
1491+
simulationWork);
1492+
if(!runScheduleWork_->stepWork.computeEnergy)
1493+
{
1494+
gmx_fatal(FARGS, "PLUMED needs the potential energy but GROMACS is not computing it this step");
1495+
}
1496+
}
14751497
clear_mat(plumed_vir);
14761498
plumed_cmd(plumedmain,"setVirial",&plumed_vir[0][0]);
14771499
}

src/colvar/Energy.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ it is available, and when also replica exchange is available,
3838
metadynamics applied to ENERGY can be used to decrease the
3939
number of required replicas.
4040
41-
\bug This \ref ENERGY does not include long tail corrections.
42-
Thus when using e.g. LAMMPS `"pair_modify tail yes"` or GROMACS `"DispCorr Ener"` (or `"DispCorr EnerPres"`),
43-
the potential energy from \ref ENERGY will be slightly different form the one of the MD code.
44-
You should still be able to use \ref ENERGY and then reweight your simulation with the correct MD energy value.
41+
\warning Some MD engines communicate to PLUMED the energy without the long tail corrections,
42+
leading to a small mismatch. If this is the case, use the energy from the MD engine for
43+
reweighting. See \issue{567}.
4544
4645
\bug Acceptance for replica exchange when \ref ENERGY is biased
4746
is computed correctly only if all the replicas have the same

0 commit comments

Comments
 (0)