@@ -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 }
0 commit comments