Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GROMACS expanded ensemble patches #793

Merged
merged 1 commit into from Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 35 additions & 9 deletions patches/gromacs-2020.6.diff/src/gromacs/mdlib/expanded.cpp
Expand Up @@ -75,6 +75,7 @@ extern plumed plumedmain;
#include "gromacs/timing/wallcycle.h"
#include "gromacs/utility/fatalerror.h"
#include "gromacs/utility/gmxmpi.h"
#include "gromacs/utility/logger.h"
#include "gromacs/utility/smalloc.h"

#include "expanded_internal.h"
Expand All @@ -92,12 +93,39 @@ static void init_df_history_weights(df_history_t* dfhist, const t_expanded* expa

/* Eventually should contain all the functions needed to initialize expanded ensemble
before the md loop starts */
void init_expanded_ensemble(gmx_bool bStateFromCP, const t_inputrec* ir, df_history_t* dfhist)
void init_expanded_ensemble(gmx_bool bStateFromCP, const t_inputrec* ir, df_history_t* dfhist, const gmx::MDLogger& mdlog)
{
if (!bStateFromCP)
{
init_df_history_weights(dfhist, ir->expandedvals, ir->fepvals->n_lambda);
}
if (plumedswitch)
{
if (ir->expandedvals->elamstats == elamstatsNO)
{
// No weight updating was chosen, use PLUMED weights
int plumedVersion=0;
plumed_cmd(plumedmain, "getApiVersion", &plumedVersion);
GMX_RELEASE_ASSERT(
plumedVersion >= 8,
"Please use PLUMED v2.8 or newer to use alchemical metadynamics with expanded ensemble");

GMX_LOG(mdlog.info).asParagraph().appendText(
"You requested an expanded ensemble simulation with lmc-stats = no and activated PLUMED.\n"
"As a result, this simulation will use the bias provided by PLUMED and ignore all\n"
"expanded ensemble settings related to weight updates.\n"
"If you want to use lambda weights updated by GROMACS in the expanded ensemble calculation,\n"
"set lmc-stats != no.");
}
else
{
GMX_LOG(mdlog.info).asParagraph().appendText(
"You requested an expanded ensemble simulation with lmc-stats != no and activated PLUMED.\n"
"As a result, this simulation will use lambda weights managed by GROMACS and will not\n"
"explicitly use the PLUMED bias in the expanded ensemble calculation.\n"
"If you want to use the PLUMED bias as lambda weights, set lmc-stats = no.");
}
}
}

static void GenerateGibbsProbabilities(const real* ene, double* p_k, double* pks, int minfep, int maxfep)
Expand Down Expand Up @@ -878,7 +906,7 @@ static int ChooseNewLambda(int nlim,
lamnew = fep_state; /* so that there is a default setting -- stays the same */

// Don't equilibrate weights when using Plumed
if (!plumedswitch)
if (!plumedswitch || expand->elamstats != elamstatsNO)
{
if (!EWL(expand->elamstats)) /* ignore equilibrating the weights if using WL */
{
Expand Down Expand Up @@ -1191,8 +1219,7 @@ void PrintFreeEnergyInfoToFile(FILE* outfile,
if (step % frequency == 0)
{
fprintf(outfile, " MC-lambda information\n");
// Ignore Wang-Landau when using plumed
if (EWL(expand->elamstats) && (!(dfhist->bEquil)) && !plumedswitch)
if (EWL(expand->elamstats) && (!(dfhist->bEquil)))
{
fprintf(outfile, " Wang-Landau incrementor is: %11.5g\n", dfhist->wl_delta);
}
Expand Down Expand Up @@ -1244,8 +1271,7 @@ void PrintFreeEnergyInfoToFile(FILE* outfile,
fprintf(outfile, "%9.3f", simtemp->temperatures[ifep]);
}
}
// No Wang-Landau when using Plumed
if (EWL(expand->elamstats) && (!plumedswitch)
if (EWL(expand->elamstats)
&& (!(dfhist->bEquil))) /* if performing WL and still haven't equilibrated */
{
if (expand->elamstats == elamstatsWL)
Expand Down Expand Up @@ -1465,7 +1491,7 @@ int ExpandedEnsembleDynamics(FILE* log,
weighted_lamee[i] -= maxweighted;
}

if (plumedswitch)
if (plumedswitch && expand->elamstats == elamstatsNO)
{
// Update weights at all lambda states with current values from Plumed.
// For acceptance criterion, expanded ensemble is expecting the weight at
Expand All @@ -1476,7 +1502,7 @@ int ExpandedEnsembleDynamics(FILE* log,
*realFepState = i;
real bias = 0;
plumed_cmd(plumedmain, "prepareCalc", nullptr);
plumed_cmd(plumedmain, "performCalcNoUpdate", nullptr);
plumed_cmd(plumedmain, "performCalcNoForces", nullptr);
plumed_cmd(plumedmain, "getBias", &bias);
bias /= expand->mc_temp * BOLTZ;
if (i == 0)
Expand All @@ -1503,7 +1529,7 @@ int ExpandedEnsembleDynamics(FILE* log,
}
}

// Accept / reject is handled by GROMACS (with Plumed weights).
// Accept / reject is handled by GROMACS (possibly with Plumed weights).
lamnew = ChooseNewLambda(nlim, expand, dfhist, fep_state, weighted_lamee, p_k,
ir->expandedvals->lmc_seed, step);
/* if using simulated tempering, we need to adjust the temperatures */
Expand Down
7 changes: 6 additions & 1 deletion patches/gromacs-2020.6.diff/src/gromacs/mdlib/expanded.h
Expand Up @@ -50,9 +50,14 @@ struct t_mdatoms;
struct t_simtemp;
class t_state;

namespace gmx
{
class MDLogger;
} // namespace gmx

void init_npt_masses(const t_inputrec* ir, t_state* state, t_extmass* MassQ, gmx_bool bInit);

void init_expanded_ensemble(gmx_bool bStateFromCP, const t_inputrec* ir, df_history_t* dfhist);
void init_expanded_ensemble(gmx_bool bStateFromCP, const t_inputrec* ir, df_history_t* dfhist, const gmx::MDLogger& mdlog);

int ExpandedEnsembleDynamics(FILE* log,
const t_inputrec* ir,
Expand Down
2 changes: 1 addition & 1 deletion patches/gromacs-2020.6.diff/src/gromacs/mdrun/md.cpp
Expand Up @@ -448,7 +448,7 @@ void gmx::LegacySimulator::do_md()
gmx_fatal(FARGS,
"With expanded ensemble, nstexpanded should be a multiple of nstcalcenergy");
}
init_expanded_ensemble(startingBehavior != StartingBehavior::NewSimulation, ir, state->dfhist);
init_expanded_ensemble(startingBehavior != StartingBehavior::NewSimulation, ir, state->dfhist, mdlog);
}

if (MASTER(cr))
Expand Down
44 changes: 35 additions & 9 deletions patches/gromacs-2021.4.diff/src/gromacs/mdlib/expanded.cpp
Expand Up @@ -75,6 +75,7 @@ extern plumed plumedmain;
#include "gromacs/timing/wallcycle.h"
#include "gromacs/utility/fatalerror.h"
#include "gromacs/utility/gmxmpi.h"
#include "gromacs/utility/logger.h"
#include "gromacs/utility/smalloc.h"

#include "expanded_internal.h"
Expand All @@ -92,12 +93,39 @@ static void init_df_history_weights(df_history_t* dfhist, const t_expanded* expa

/* Eventually should contain all the functions needed to initialize expanded ensemble
before the md loop starts */
void init_expanded_ensemble(gmx_bool bStateFromCP, const t_inputrec* ir, df_history_t* dfhist)
void init_expanded_ensemble(gmx_bool bStateFromCP, const t_inputrec* ir, df_history_t* dfhist, const gmx::MDLogger& mdlog)
{
if (!bStateFromCP)
{
init_df_history_weights(dfhist, ir->expandedvals, ir->fepvals->n_lambda);
}
if (plumedswitch)
{
if (ir->expandedvals->elamstats == elamstatsNO)
{
// No weight updating was chosen, use PLUMED weights
int plumedVersion=0;
plumed_cmd(plumedmain, "getApiVersion", &plumedVersion);
GMX_RELEASE_ASSERT(
plumedVersion >= 8,
"Please use PLUMED v2.8 or newer to use alchemical metadynamics with expanded ensemble");

GMX_LOG(mdlog.info).asParagraph().appendText(
"You requested an expanded ensemble simulation with lmc-stats = no and activated PLUMED.\n"
"As a result, this simulation will use the bias provided by PLUMED and ignore all\n"
"expanded ensemble settings related to weight updates.\n"
"If you want to use lambda weights updated by GROMACS in the expanded ensemble calculation,\n"
"set lmc-stats != no.");
}
else
{
GMX_LOG(mdlog.info).asParagraph().appendText(
"You requested an expanded ensemble simulation with lmc-stats != no and activated PLUMED.\n"
"As a result, this simulation will use lambda weights managed by GROMACS and will not\n"
"explicitly use the PLUMED bias in the expanded ensemble calculation.\n"
"If you want to use the PLUMED bias as lambda weights, set lmc-stats = no.");
}
}
}

static void GenerateGibbsProbabilities(const real* ene, double* p_k, double* pks, int minfep, int maxfep)
Expand Down Expand Up @@ -878,7 +906,7 @@ static int ChooseNewLambda(int nlim,
lamnew = fep_state; /* so that there is a default setting -- stays the same */

// Don't equilibrate weights when using Plumed
if (!plumedswitch)
if (!plumedswitch || expand->elamstats != elamstatsNO)
{
if (!EWL(expand->elamstats)) /* ignore equilibrating the weights if using WL */
{
Expand Down Expand Up @@ -1191,8 +1219,7 @@ void PrintFreeEnergyInfoToFile(FILE* outfile,
if (step % frequency == 0)
{
fprintf(outfile, " MC-lambda information\n");
// Ignore Wang-Landau when using plumed
if (EWL(expand->elamstats) && (!(dfhist->bEquil)) && !plumedswitch)
if (EWL(expand->elamstats) && (!(dfhist->bEquil)))
{
fprintf(outfile, " Wang-Landau incrementor is: %11.5g\n", dfhist->wl_delta);
}
Expand Down Expand Up @@ -1244,8 +1271,7 @@ void PrintFreeEnergyInfoToFile(FILE* outfile,
fprintf(outfile, "%9.3f", simtemp->temperatures[ifep]);
}
}
// No Wang-Landau when using Plumed
if (EWL(expand->elamstats) && (!plumedswitch)
if (EWL(expand->elamstats)
&& (!(dfhist->bEquil))) /* if performing WL and still haven't equilibrated */
{
if (expand->elamstats == elamstatsWL)
Expand Down Expand Up @@ -1463,7 +1489,7 @@ int ExpandedEnsembleDynamics(FILE* log,
weighted_lamee[i] -= maxweighted;
}

if (plumedswitch)
if (plumedswitch && expand->elamstats == elamstatsNO)
{
// Update weights at all lambda states with current values from Plumed.
// For acceptance criterion, expanded ensemble is expecting the weight at
Expand All @@ -1474,7 +1500,7 @@ int ExpandedEnsembleDynamics(FILE* log,
*realFepState = i;
real bias = 0;
plumed_cmd(plumedmain, "prepareCalc", nullptr);
plumed_cmd(plumedmain, "performCalcNoUpdate", nullptr);
plumed_cmd(plumedmain, "performCalcNoForces", nullptr);
plumed_cmd(plumedmain, "getBias", &bias);
bias /= expand->mc_temp * BOLTZ;
if (i == 0)
Expand All @@ -1501,7 +1527,7 @@ int ExpandedEnsembleDynamics(FILE* log,
}
}

// Accept / reject is handled by GROMACS (with Plumed weights).
// Accept / reject is handled by GROMACS (possibly with Plumed weights).
lamnew = ChooseNewLambda(nlim, expand, dfhist, fep_state, weighted_lamee, p_k,
ir->expandedvals->lmc_seed, step);
/* if using simulated tempering, we need to adjust the temperatures */
Expand Down
7 changes: 6 additions & 1 deletion patches/gromacs-2021.4.diff/src/gromacs/mdlib/expanded.h
Expand Up @@ -50,9 +50,14 @@ struct t_mdatoms;
struct t_simtemp;
class t_state;

namespace gmx
{
class MDLogger;
} // namespace gmx

void init_npt_masses(const t_inputrec* ir, t_state* state, t_extmass* MassQ, gmx_bool bInit);

void init_expanded_ensemble(gmx_bool bStateFromCP, const t_inputrec* ir, df_history_t* dfhist);
void init_expanded_ensemble(gmx_bool bStateFromCP, const t_inputrec* ir, df_history_t* dfhist, const gmx::MDLogger& mdlog);

int ExpandedEnsembleDynamics(FILE* log,
const t_inputrec* ir,
Expand Down
2 changes: 1 addition & 1 deletion patches/gromacs-2021.4.diff/src/gromacs/mdrun/md.cpp
Expand Up @@ -464,7 +464,7 @@ void gmx::LegacySimulator::do_md()
gmx_fatal(FARGS,
"With expanded ensemble, nstexpanded should be a multiple of nstcalcenergy");
}
init_expanded_ensemble(startingBehavior != StartingBehavior::NewSimulation, ir, state->dfhist);
init_expanded_ensemble(startingBehavior != StartingBehavior::NewSimulation, ir, state->dfhist, mdlog);
}

if (MASTER(cr))
Expand Down