From 0ceb7eae7bcf85e4e590966ce37f990a9865f2e3 Mon Sep 17 00:00:00 2001 From: Paul Bauer Date: Thu, 4 Mar 2021 09:39:30 +0100 Subject: [PATCH] Modernize AWH enumerations Changed enums to enum classes. Use EnumerationArray for strings. --- src/gromacs/applied_forces/awh/awh.cpp | 10 +-- src/gromacs/applied_forces/awh/biasgrid.cpp | 4 +- src/gromacs/applied_forces/awh/biasparams.cpp | 16 ++-- src/gromacs/applied_forces/awh/biasparams.h | 11 +-- src/gromacs/applied_forces/awh/biasstate.cpp | 8 +- .../applied_forces/awh/histogramsize.cpp | 4 +- src/gromacs/applied_forces/awh/pointstate.h | 11 +-- .../applied_forces/awh/read_params.cpp | 87 +++++++++++------- src/gromacs/applied_forces/awh/tests/bias.cpp | 23 ++--- .../awh/tests/bias_fep_lambda_state.cpp | 25 +++--- .../applied_forces/awh/tests/biasstate.cpp | 12 +-- src/gromacs/fileio/tpxio.cpp | 8 +- src/gromacs/mdtypes/awh_params.h | 88 +++++++++---------- src/gromacs/mdtypes/inputrec.cpp | 15 ++-- 14 files changed, 174 insertions(+), 148 deletions(-) diff --git a/src/gromacs/applied_forces/awh/awh.cpp b/src/gromacs/applied_forces/awh/awh.cpp index a772d31a34..8c80087391 100644 --- a/src/gromacs/applied_forces/awh/awh.cpp +++ b/src/gromacs/applied_forces/awh/awh.cpp @@ -117,7 +117,7 @@ static bool anyDimUsesPull(const AwhBiasParams& awhBiasParams) for (int d = 0; d < awhBiasParams.ndim; d++) { const AwhDimParams& awhDimParams = awhBiasParams.dimParams[d]; - if (awhDimParams.eCoordProvider == eawhcoordproviderPULL) + if (awhDimParams.eCoordProvider == AwhCoordinateProviderType::Pull) { return true; } @@ -225,14 +225,14 @@ Awh::Awh(FILE* fplog, for (int d = 0; d < awhBiasParams.ndim; d++) { const AwhDimParams& awhDimParams = awhBiasParams.dimParams[d]; - if (awhDimParams.eCoordProvider != eawhcoordproviderPULL - && awhDimParams.eCoordProvider != eawhcoordproviderFREE_ENERGY_LAMBDA) + if (awhDimParams.eCoordProvider != AwhCoordinateProviderType::Pull + && awhDimParams.eCoordProvider != AwhCoordinateProviderType::FreeEnergyLambda) { GMX_THROW( InvalidInputError("Currently only the pull code and lambda are supported " "as coordinate providers")); } - if (awhDimParams.eCoordProvider == eawhcoordproviderPULL) + if (awhDimParams.eCoordProvider == AwhCoordinateProviderType::Pull) { const t_pull_coord& pullCoord = inputRecord.pull->coord[awhDimParams.coordIndex]; if (pullCoord.eGeom == PullGroupGeometry::DirectionPBC) @@ -487,7 +487,7 @@ void Awh::registerAwhWithPull(const AwhParams& awhParams, pull_t* pull_work) for (int d = 0; d < biasParams.ndim; d++) { - if (biasParams.dimParams[d].eCoordProvider == eawhcoordproviderPULL) + if (biasParams.dimParams[d].eCoordProvider == AwhCoordinateProviderType::Pull) { register_external_pull_potential( pull_work, biasParams.dimParams[d].coordIndex, Awh::externalPotentialString()); diff --git a/src/gromacs/applied_forces/awh/biasgrid.cpp b/src/gromacs/applied_forces/awh/biasgrid.cpp index 16d9d655b6..af639fbf6f 100644 --- a/src/gromacs/applied_forces/awh/biasgrid.cpp +++ b/src/gromacs/applied_forces/awh/biasgrid.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2015,2016,2017,2018,2019,2020, by the GROMACS development team, led by + * Copyright (c) 2015,2016,2017,2018,2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -820,7 +820,7 @@ BiasGrid::BiasGrid(const std::vector& dimParams, const AwhDimParams* { double origin = dimParams[d].scaleUserInputToInternal(awhDimParams[d].origin); double end = dimParams[d].scaleUserInputToInternal(awhDimParams[d].end); - if (awhDimParams[d].eCoordProvider == eawhcoordproviderPULL) + if (awhDimParams[d].eCoordProvider == AwhCoordinateProviderType::Pull) { period[d] = dimParams[d].scaleUserInputToInternal(awhDimParams[d].period); static_assert( diff --git a/src/gromacs/applied_forces/awh/biasparams.cpp b/src/gromacs/applied_forces/awh/biasparams.cpp index 1827130077..1e66ba164b 100644 --- a/src/gromacs/applied_forces/awh/biasparams.cpp +++ b/src/gromacs/applied_forces/awh/biasparams.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2015,2016,2017,2018,2019,2020, by the GROMACS development team, led by + * Copyright (c) 2015,2016,2017,2018,2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -83,9 +83,9 @@ int64_t calcTargetUpdateInterval(const AwhParams& awhParams, const AwhBiasParams */ switch (awhBiasParams.eTarget) { - case eawhtargetCONSTANT: numStepsUpdateTarget = 0; break; - case eawhtargetCUTOFF: - case eawhtargetBOLTZMANN: + case AwhTargetType::Constant: numStepsUpdateTarget = 0; break; + case AwhTargetType::Cutoff: + case AwhTargetType::Boltzmann: /* Updating the target generally requires updating the whole grid so to keep the cost down we generally update the target less often than the free energy (unless the free energy update step is set to > 100 samples). */ @@ -93,7 +93,7 @@ int64_t calcTargetUpdateInterval(const AwhParams& awhParams, const AwhBiasParams awhParams.numSamplesUpdateFreeEnergy) * awhParams.nstSampleCoord; break; - case eawhtargetLOCALBOLTZMANN: + case AwhTargetType::LocalBoltzmann: /* The target distribution is set equal to the reference histogram which is updated every free energy update. So the target has to be updated at the same time. This leads to a global update each time because it is assumed that a target distribution update can take any form. This is a bit unfortunate for a "local" @@ -238,14 +238,14 @@ BiasParams::BiasParams(const AwhParams& awhParams, eTarget(awhBiasParams.eTarget), freeEnergyCutoffInKT(beta * awhBiasParams.targetCutoff), temperatureScaleFactor(awhBiasParams.targetBetaScaling), - idealWeighthistUpdate(eTarget != eawhtargetLOCALBOLTZMANN), + idealWeighthistUpdate(eTarget != AwhTargetType::LocalBoltzmann), numSharedUpdate(getNumSharedUpdate(awhBiasParams, numSharingSimulations)), updateWeight(numSamplesUpdateFreeEnergy_ * numSharedUpdate), - localWeightScaling(eTarget == eawhtargetLOCALBOLTZMANN ? temperatureScaleFactor : 1), + localWeightScaling(eTarget == AwhTargetType::LocalBoltzmann ? temperatureScaleFactor : 1), initialErrorInKT(beta * awhBiasParams.errorInitial), initialHistogramSize( getInitialHistogramSizeEstimate(awhBiasParams, gridAxis, beta, numStepsSampleCoord_ * mdTimeStep)), - convolveForce(awhParams.ePotential == eawhpotentialCONVOLVED), + convolveForce(awhParams.ePotential == AwhPotentialType::Convolved), biasIndex(biasIndex), disableUpdateSkips_(disableUpdateSkips == DisableUpdateSkips::yes) { diff --git a/src/gromacs/applied_forces/awh/biasparams.h b/src/gromacs/applied_forces/awh/biasparams.h index 6b24e2bd09..9fed42fa07 100644 --- a/src/gromacs/applied_forces/awh/biasparams.h +++ b/src/gromacs/applied_forces/awh/biasparams.h @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2015,2016,2017,2018,2019,2020, by the GROMACS development team, led by + * Copyright (c) 2015,2016,2017,2018,2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -64,6 +64,7 @@ struct AwhBiasParams; struct AwhParams; struct DimParams; class GridAxis; +enum class AwhTargetType : int; /*! \internal \brief Constant parameters for the bias. */ @@ -208,10 +209,10 @@ private: const int64_t numStepsUpdateTarget_; /**< Number of steps per updating the target distribution. */ const int64_t numStepsCheckCovering_; /**< Number of steps per checking for covering. */ public: - const int eTarget; /**< Type of target distribution. */ - const double freeEnergyCutoffInKT; /**< Free energy cut-off in kT for cut-off target distribution. */ - const double temperatureScaleFactor; /**< Temperature scaling factor for temperature scaled targed distributions. */ - const bool idealWeighthistUpdate; /**< Update reference weighthistogram using the target distribution? Otherwise use the realized distribution. */ + const AwhTargetType eTarget; /**< Type of target distribution. */ + const double freeEnergyCutoffInKT; /**< Free energy cut-off in kT for cut-off target distribution. */ + const double temperatureScaleFactor; /**< Temperature scaling factor for temperature scaled targed distributions. */ + const bool idealWeighthistUpdate; /**< Update reference weighthistogram using the target distribution? Otherwise use the realized distribution. */ const int numSharedUpdate; /**< The number of (multi-)simulations sharing the bias update */ const double updateWeight; /**< The probability weight accumulated for each update. */ const double localWeightScaling; /**< Scaling factor applied to a sample before adding it to the reference weight histogram (= 1, usually). */ diff --git a/src/gromacs/applied_forces/awh/biasstate.cpp b/src/gromacs/applied_forces/awh/biasstate.cpp index b5d68415bc..783cfab1f8 100644 --- a/src/gromacs/applied_forces/awh/biasstate.cpp +++ b/src/gromacs/applied_forces/awh/biasstate.cpp @@ -351,7 +351,7 @@ namespace void updateTargetDistribution(gmx::ArrayRef pointState, const BiasParams& params) { double freeEnergyCutoff = 0; - if (params.eTarget == eawhtargetCUTOFF) + if (params.eTarget == AwhTargetType::Cutoff) { freeEnergyCutoff = freeEnergyMinimumValue(pointState) + params.freeEnergyCutoffInKT; } @@ -996,7 +996,7 @@ bool BiasState::isSamplingRegionCovered(const BiasParams& params, /* Set the free energy cutoff */ double maxFreeEnergy = GMX_FLOAT_MAX; - if (params.eTarget == eawhtargetCUTOFF) + if (params.eTarget == AwhTargetType::Cutoff) { maxFreeEnergy = freeEnergyMinimumValue(points_) + params.freeEnergyCutoffInKT; } @@ -1158,7 +1158,7 @@ void BiasState::updateFreeEnergyAndAddSamplesToHistogram(const std::vector awhTargetTypeNames = { + "constant", "cutoff", "boltzmann", "local-boltzmann" + }; + return awhTargetTypeNames[enumValue]; +} -const char* eawhgrowth_names[eawhgrowthNR + 1] = { "exp-linear", "linear", nullptr }; +const char* enumValueToString(AwhHistogramGrowthType enumValue) +{ + constexpr gmx::EnumerationArray awhHistogramGrowthTypeNames = { + "exp-linear", "linear" + }; + return awhHistogramGrowthTypeNames[enumValue]; +} -const char* eawhpotential_names[eawhpotentialNR + 1] = { "convolved", "umbrella", nullptr }; +const char* enumValueToString(AwhPotentialType enumValue) +{ + constexpr gmx::EnumerationArray awhPotentialTypeNames = { + "convolved", "umbrella" + }; + return awhPotentialTypeNames[enumValue]; +} -const char* eawhcoordprovider_names[eawhcoordproviderNR + 1] = { "pull", "fep-lambda", nullptr }; +const char* enumValueToString(AwhCoordinateProviderType enumValue) +{ + constexpr gmx::EnumerationArray awhCoordinateProviderTypeNames = { + "pull", "fep-lambda" + }; + return awhCoordinateProviderTypeNames[enumValue]; +} namespace { @@ -104,8 +125,8 @@ void checkMtsConsistency(const t_inputrec& inputrec, warninp_t wi) { switch (biasParams.dimParams[d].eCoordProvider) { - case eawhcoordproviderPULL: usesPull = true; break; - case eawhcoordproviderFREE_ENERGY_LAMBDA: usesFep = true; break; + case AwhCoordinateProviderType::Pull: usesPull = true; break; + case AwhCoordinateProviderType::FreeEnergyLambda: usesFep = true; break; default: GMX_RELEASE_ASSERT(false, "Unsupported coord provider"); } } @@ -371,7 +392,7 @@ void readDimParams(std::vector* inp, "currently only 'pull' and 'fep-lambda' (free energy lambda state) is supported"); } opt = prefix + "-coord-provider"; - dimParams->eCoordProvider = get_eeenum(inp, opt, eawhcoordprovider_names, wi); + dimParams->eCoordProvider = getEnum(inp, opt.c_str(), wi); if (bComment) { @@ -429,7 +450,7 @@ void readDimParams(std::vector* inp, */ void checkDimParams(const std::string& prefix, AwhDimParams* dimParams, const t_inputrec* ir, warninp_t wi) { - if (dimParams->eCoordProvider == eawhcoordproviderPULL) + if (dimParams->eCoordProvider == AwhCoordinateProviderType::Pull) { if (!ir->bPull) { @@ -439,7 +460,7 @@ void checkDimParams(const std::string& prefix, AwhDimParams* dimParams, const t_ } checkPullDimParams(prefix, dimParams, *ir->pull, wi); } - else if (dimParams->eCoordProvider == eawhcoordproviderFREE_ENERGY_LAMBDA) + else if (dimParams->eCoordProvider == AwhCoordinateProviderType::FreeEnergyLambda) { if (ir->efep == FreeEnergyPerturbationType::No) { @@ -507,7 +528,7 @@ void readBiasParams(std::vector* inp, "size: exp-linear or linear"); } opt = prefix + "-growth"; - awhBiasParams->eGrowth = get_eeenum(inp, opt, eawhgrowth_names, wi); + awhBiasParams->eGrowth = getEnum(inp, opt.c_str(), wi); if (bComment) { @@ -524,7 +545,7 @@ void readBiasParams(std::vector* inp, inp, "Target distribution type: constant, cutoff, boltzmann or local-boltzmann"); } opt = prefix + "-target"; - awhBiasParams->eTarget = get_eeenum(inp, opt, eawhtarget_names, wi); + awhBiasParams->eTarget = getEnum(inp, opt.c_str(), wi); if (bComment) { @@ -594,40 +615,40 @@ void checkBiasParams(const AwhBiasParams* awhBiasParams, const std::string& pref } opt = prefix + "-equilibrate-histogram"; - if (awhBiasParams->equilibrateHistogram && awhBiasParams->eGrowth != eawhgrowthEXP_LINEAR) + if (awhBiasParams->equilibrateHistogram && awhBiasParams->eGrowth != AwhHistogramGrowthType::ExponentialLinear) { auto message = formatString("Option %s will only have an effect for histogram growth type '%s'.", opt.c_str(), - EAWHGROWTH(eawhgrowthEXP_LINEAR)); + enumValueToString(AwhHistogramGrowthType::ExponentialLinear)); warning(wi, message); } - if ((awhBiasParams->eTarget == eawhtargetLOCALBOLTZMANN) - && (awhBiasParams->eGrowth == eawhgrowthEXP_LINEAR)) + if ((awhBiasParams->eTarget == AwhTargetType::LocalBoltzmann) + && (awhBiasParams->eGrowth == AwhHistogramGrowthType::ExponentialLinear)) { auto message = formatString( "Target type '%s' combined with histogram growth type '%s' is not " "expected to give stable bias updates. You probably want to use growth type " "'%s' instead.", - EAWHTARGET(eawhtargetLOCALBOLTZMANN), - EAWHGROWTH(eawhgrowthEXP_LINEAR), - EAWHGROWTH(eawhgrowthLINEAR)); + enumValueToString(AwhTargetType::LocalBoltzmann), + enumValueToString(AwhHistogramGrowthType::ExponentialLinear), + enumValueToString(AwhHistogramGrowthType::Linear)); warning(wi, message); } opt = prefix + "-target-beta-scaling"; switch (awhBiasParams->eTarget) { - case eawhtargetBOLTZMANN: - case eawhtargetLOCALBOLTZMANN: + case AwhTargetType::Boltzmann: + case AwhTargetType::LocalBoltzmann: if (awhBiasParams->targetBetaScaling < 0 || awhBiasParams->targetBetaScaling > 1) { gmx_fatal(FARGS, "%s = %g is not useful for target type %s.", opt.c_str(), awhBiasParams->targetBetaScaling, - EAWHTARGET(awhBiasParams->eTarget)); + enumValueToString(awhBiasParams->eTarget)); } break; default: @@ -638,7 +659,7 @@ void checkBiasParams(const AwhBiasParams* awhBiasParams, const std::string& pref "Value for %s (%g) set explicitly but will not be used for target type %s.", opt.c_str(), awhBiasParams->targetBetaScaling, - EAWHTARGET(awhBiasParams->eTarget)); + enumValueToString(awhBiasParams->eTarget)); } break; } @@ -646,14 +667,14 @@ void checkBiasParams(const AwhBiasParams* awhBiasParams, const std::string& pref opt = prefix + "-target-cutoff"; switch (awhBiasParams->eTarget) { - case eawhtargetCUTOFF: + case AwhTargetType::Cutoff: if (awhBiasParams->targetCutoff <= 0) { gmx_fatal(FARGS, "%s = %g is not useful for target type %s.", opt.c_str(), awhBiasParams->targetCutoff, - EAWHTARGET(awhBiasParams->eTarget)); + enumValueToString(awhBiasParams->eTarget)); } break; default: @@ -664,7 +685,7 @@ void checkBiasParams(const AwhBiasParams* awhBiasParams, const std::string& pref "Value for %s (%g) set explicitly but will not be used for target type %s.", opt.c_str(), awhBiasParams->targetCutoff, - EAWHTARGET(awhBiasParams->eTarget)); + enumValueToString(awhBiasParams->eTarget)); } break; } @@ -723,7 +744,7 @@ void checkInputConsistencyAwh(const AwhParams& awhParams, warninp_t wi) { for (int d1 = 0; d1 < awhBiasParams1.ndim; d1++) { - if (awhBiasParams1.dimParams[d1].eCoordProvider == eawhcoordproviderFREE_ENERGY_LAMBDA) + if (awhBiasParams1.dimParams[d1].eCoordProvider == AwhCoordinateProviderType::FreeEnergyLambda) { continue; } @@ -732,7 +753,7 @@ void checkInputConsistencyAwh(const AwhParams& awhParams, warninp_t wi) /* d1 is the reference dimension of the reference AWH. d2 is the dim index of the AWH to compare with. */ for (int d2 = 0; d2 < awhBiasParams2.ndim; d2++) { - if (awhBiasParams2.dimParams[d2].eCoordProvider == eawhcoordproviderFREE_ENERGY_LAMBDA) + if (awhBiasParams2.dimParams[d2].eCoordProvider == AwhCoordinateProviderType::FreeEnergyLambda) { continue; } @@ -785,7 +806,7 @@ AwhParams* readAwhParams(std::vector* inp, warninp_t wi) printStringNoNewline(inp, "The way to apply the biasing potential: convolved or umbrella"); opt = "awh-potential"; - awhParams->ePotential = get_eeenum(inp, opt, eawhpotential_names, wi); + awhParams->ePotential = getEnum(inp, opt.c_str(), wi); printStringNoNewline(inp, "The random seed used for sampling the umbrella center in the case of " @@ -1168,7 +1189,7 @@ void setStateDependentAwhParams(AwhParams* awhParams, for (int d = 0; d < awhBiasParams->ndim; d++) { AwhDimParams* dimParams = &awhBiasParams->dimParams[d]; - if (dimParams->eCoordProvider == eawhcoordproviderPULL) + if (dimParams->eCoordProvider == AwhCoordinateProviderType::Pull) { setStateDependentAwhPullDimParams( dimParams, k, d, &pull_params, pull_work, pbc, compressibility, wi); diff --git a/src/gromacs/applied_forces/awh/tests/bias.cpp b/src/gromacs/applied_forces/awh/tests/bias.cpp index b64d01123e..5abe89de9d 100644 --- a/src/gromacs/applied_forces/awh/tests/bias.cpp +++ b/src/gromacs/applied_forces/awh/tests/bias.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2017,2018,2019,2020, by the GROMACS development team, led by + * Copyright (c) 2017,2018,2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -86,7 +86,7 @@ struct AwhTestParameters }; //! Helper function to set up the C-style AWH parameters for the test -static AwhTestParameters getAwhTestParameters(int eawhgrowth, int eawhpotential) +static AwhTestParameters getAwhTestParameters(AwhHistogramGrowthType eawhgrowth, AwhPotentialType eawhpotential) { AwhTestParameters params; @@ -101,13 +101,13 @@ static AwhTestParameters getAwhTestParameters(int eawhgrowth, int eawhpotential) awhDimParams.end = 1.5; awhDimParams.coordValueInit = awhDimParams.origin; awhDimParams.coverDiameter = 0; - awhDimParams.eCoordProvider = eawhcoordproviderPULL; + awhDimParams.eCoordProvider = AwhCoordinateProviderType::Pull; AwhBiasParams& awhBiasParams = params.awhBiasParams; awhBiasParams.ndim = 1; awhBiasParams.dimParams = &awhDimParams; - awhBiasParams.eTarget = eawhtargetCONSTANT; + awhBiasParams.eTarget = AwhTargetType::Constant; awhBiasParams.targetBetaScaling = 0; awhBiasParams.targetCutoff = 0; awhBiasParams.eGrowth = eawhgrowth; @@ -141,7 +141,7 @@ const double g_coords[] = { 0.62, 0.70, 0.68, 0.80, 0.93, 0.87, 1.16, 1.14, 0.95 0.86, 0.88, 0.79, 0.75, 0.82, 0.74, 0.70, 0.68, 0.71, 0.73 }; //! Convenience typedef: growth type enum, potential type enum, disable update skips -typedef std::tuple BiasTestParameters; +typedef std::tuple BiasTestParameters; /*! \brief Test fixture for testing Bias updates */ @@ -173,8 +173,8 @@ public: * and disableUpdateSkips do not affect the point state. * But the reference data will also ensure this. */ - int eawhgrowth; - int eawhpotential; + AwhHistogramGrowthType eawhgrowth; + AwhPotentialType eawhpotential; BiasParams::DisableUpdateSkips disableUpdateSkips; std::tie(eawhgrowth, eawhpotential, disableUpdateSkips) = GetParam(); @@ -287,15 +287,18 @@ TEST_P(BiasTest, ForcesBiasPmf) */ INSTANTIATE_TEST_CASE_P(WithParameters, BiasTest, - ::testing::Combine(::testing::Values(eawhgrowthLINEAR, eawhgrowthEXP_LINEAR), - ::testing::Values(eawhpotentialUMBRELLA, eawhpotentialCONVOLVED), + ::testing::Combine(::testing::Values(AwhHistogramGrowthType::Linear, + AwhHistogramGrowthType::ExponentialLinear), + ::testing::Values(AwhPotentialType::Umbrella, + AwhPotentialType::Convolved), ::testing::Values(BiasParams::DisableUpdateSkips::yes, BiasParams::DisableUpdateSkips::no))); // Test that we detect coverings and exit the initial stage at the correct step TEST(BiasTest, DetectsCovering) { - const AwhTestParameters params = getAwhTestParameters(eawhgrowthEXP_LINEAR, eawhpotentialCONVOLVED); + const AwhTestParameters params = getAwhTestParameters(AwhHistogramGrowthType::ExponentialLinear, + AwhPotentialType::Convolved); const AwhDimParams& awhDimParams = params.awhParams.awhBiasParams[0].dimParams[0]; const double mdTimeStep = 0.1; diff --git a/src/gromacs/applied_forces/awh/tests/bias_fep_lambda_state.cpp b/src/gromacs/applied_forces/awh/tests/bias_fep_lambda_state.cpp index de0d6de7c0..b53777cfd8 100644 --- a/src/gromacs/applied_forces/awh/tests/bias_fep_lambda_state.cpp +++ b/src/gromacs/applied_forces/awh/tests/bias_fep_lambda_state.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2017,2018,2019,2020, by the GROMACS development team, led by + * Copyright (c) 2017,2018,2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -88,7 +88,8 @@ struct AwhFepLambdaStateTestParameters }; //! Helper function to set up the C-style AWH parameters for the test -static AwhFepLambdaStateTestParameters getAwhFepLambdaTestParameters(int eawhgrowth, int eawhpotential) +static AwhFepLambdaStateTestParameters getAwhFepLambdaTestParameters(AwhHistogramGrowthType eawhgrowth, + AwhPotentialType eawhpotential) { AwhFepLambdaStateTestParameters params; @@ -103,13 +104,13 @@ static AwhFepLambdaStateTestParameters getAwhFepLambdaTestParameters(int eawhgro awhDimParams.end = numLambdaStates - 1; awhDimParams.coordValueInit = awhDimParams.origin; awhDimParams.coverDiameter = 0; - awhDimParams.eCoordProvider = eawhcoordproviderFREE_ENERGY_LAMBDA; + awhDimParams.eCoordProvider = AwhCoordinateProviderType::FreeEnergyLambda; AwhBiasParams& awhBiasParams = params.awhBiasParams; awhBiasParams.ndim = 1; awhBiasParams.dimParams = &awhDimParams; - awhBiasParams.eTarget = eawhtargetCONSTANT; + awhBiasParams.eTarget = AwhTargetType::Constant; awhBiasParams.targetBetaScaling = 0; awhBiasParams.targetCutoff = 0; awhBiasParams.eGrowth = eawhgrowth; @@ -137,7 +138,7 @@ static AwhFepLambdaStateTestParameters getAwhFepLambdaTestParameters(int eawhgro } //! Convenience typedef: growth type enum, potential type enum, disable update skips -typedef std::tuple BiasTestParameters; +typedef std::tuple BiasTestParameters; /*! \brief Test fixture for testing Bias updates */ @@ -168,8 +169,8 @@ public: * and disableUpdateSkips do not affect the point state. * But the reference data will also ensure this. */ - int eawhgrowth; - int eawhpotential; + AwhHistogramGrowthType eawhgrowth; + AwhPotentialType eawhpotential; BiasParams::DisableUpdateSkips disableUpdateSkips; std::tie(eawhgrowth, eawhpotential, disableUpdateSkips) = GetParam(); @@ -283,16 +284,18 @@ TEST_P(BiasFepLambdaStateTest, ForcesBiasPmf) */ INSTANTIATE_TEST_CASE_P(WithParameters, BiasFepLambdaStateTest, - ::testing::Combine(::testing::Values(eawhgrowthLINEAR, eawhgrowthEXP_LINEAR), - ::testing::Values(eawhpotentialUMBRELLA, eawhpotentialCONVOLVED), + ::testing::Combine(::testing::Values(AwhHistogramGrowthType::Linear, + AwhHistogramGrowthType::ExponentialLinear), + ::testing::Values(AwhPotentialType::Umbrella, + AwhPotentialType::Convolved), ::testing::Values(BiasParams::DisableUpdateSkips::yes, BiasParams::DisableUpdateSkips::no))); // Test that we detect coverings and exit the initial stage at the correct step TEST(BiasFepLambdaStateTest, DetectsCovering) { - const AwhFepLambdaStateTestParameters params = - getAwhFepLambdaTestParameters(eawhgrowthEXP_LINEAR, eawhpotentialCONVOLVED); + const AwhFepLambdaStateTestParameters params = getAwhFepLambdaTestParameters( + AwhHistogramGrowthType::ExponentialLinear, AwhPotentialType::Convolved); const double mdTimeStep = 0.1; diff --git a/src/gromacs/applied_forces/awh/tests/biasstate.cpp b/src/gromacs/applied_forces/awh/tests/biasstate.cpp index 4349d9b997..c78d55061a 100644 --- a/src/gromacs/applied_forces/awh/tests/biasstate.cpp +++ b/src/gromacs/applied_forces/awh/tests/biasstate.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2017,2018,2019,2020, by the GROMACS development team, led by + * Copyright (c) 2017,2018,2019,2020,2021, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -92,7 +92,7 @@ static AwhTestParameters getAwhTestParameters() awhDimParams0.end = 1.5; awhDimParams0.coordValueInit = awhDimParams0.origin; awhDimParams0.coverDiameter = 0; - awhDimParams0.eCoordProvider = eawhcoordproviderPULL; + awhDimParams0.eCoordProvider = AwhCoordinateProviderType::Pull; AwhDimParams& awhDimParams1 = awhBiasParams.dimParams[1]; @@ -102,13 +102,13 @@ static AwhTestParameters getAwhTestParameters() awhDimParams1.end = 1.3; awhDimParams1.coordValueInit = awhDimParams1.origin; awhDimParams1.coverDiameter = 0; - awhDimParams1.eCoordProvider = eawhcoordproviderPULL; + awhDimParams1.eCoordProvider = AwhCoordinateProviderType::Pull; awhBiasParams.ndim = 2; - awhBiasParams.eTarget = eawhtargetCONSTANT; + awhBiasParams.eTarget = AwhTargetType::Constant; awhBiasParams.targetBetaScaling = 0; awhBiasParams.targetCutoff = 0; - awhBiasParams.eGrowth = eawhgrowthLINEAR; + awhBiasParams.eGrowth = AwhHistogramGrowthType::Linear; awhBiasParams.bUserData = TRUE; awhBiasParams.errorInitial = 0.5; awhBiasParams.shareGroup = 0; @@ -119,7 +119,7 @@ static AwhTestParameters getAwhTestParameters() awhParams.nstOut = 0; awhParams.nstSampleCoord = 1; awhParams.numSamplesUpdateFreeEnergy = 10; - awhParams.ePotential = eawhpotentialCONVOLVED; + awhParams.ePotential = AwhPotentialType::Convolved; awhParams.shareBiasMultisim = FALSE; return params; diff --git a/src/gromacs/fileio/tpxio.cpp b/src/gromacs/fileio/tpxio.cpp index 448bb9dd58..2336b231ff 100644 --- a/src/gromacs/fileio/tpxio.cpp +++ b/src/gromacs/fileio/tpxio.cpp @@ -657,10 +657,10 @@ static void do_fepvals(gmx::ISerializer* serializer, t_lambda* fepvals, int file static void do_awhBias(gmx::ISerializer* serializer, gmx::AwhBiasParams* awhBiasParams, int gmx_unused file_version) { - serializer->doInt(&awhBiasParams->eTarget); + serializer->doEnumAsInt(&awhBiasParams->eTarget); serializer->doDouble(&awhBiasParams->targetBetaScaling); serializer->doDouble(&awhBiasParams->targetCutoff); - serializer->doInt(&awhBiasParams->eGrowth); + serializer->doEnumAsInt(&awhBiasParams->eGrowth); if (serializer->reading()) { int temp = 0; @@ -686,7 +686,7 @@ static void do_awhBias(gmx::ISerializer* serializer, gmx::AwhBiasParams* awhBias { gmx::AwhDimParams* dimParams = &awhBiasParams->dimParams[d]; - serializer->doInt(&dimParams->eCoordProvider); + serializer->doEnumAsInt(&dimParams->eCoordProvider); serializer->doInt(&dimParams->coordIndex); serializer->doDouble(&dimParams->origin); serializer->doDouble(&dimParams->end); @@ -705,7 +705,7 @@ static void do_awh(gmx::ISerializer* serializer, gmx::AwhParams* awhParams, int serializer->doInt64(&awhParams->seed); serializer->doInt(&awhParams->nstSampleCoord); serializer->doInt(&awhParams->numSamplesUpdateFreeEnergy); - serializer->doInt(&awhParams->ePotential); + serializer->doEnumAsInt(&awhParams->ePotential); serializer->doBool(&awhParams->shareBiasMultisim); if (awhParams->numBias > 0) diff --git a/src/gromacs/mdtypes/awh_params.h b/src/gromacs/mdtypes/awh_params.h index c047978b15..debbb84672 100644 --- a/src/gromacs/mdtypes/awh_params.h +++ b/src/gromacs/mdtypes/awh_params.h @@ -58,66 +58,62 @@ namespace gmx { //! Target distribution enum. -enum +enum class AwhTargetType : int { - eawhtargetCONSTANT, - eawhtargetCUTOFF, - eawhtargetBOLTZMANN, - eawhtargetLOCALBOLTZMANN, - eawhtargetNR + Constant, + Cutoff, + Boltzmann, + LocalBoltzmann, + Count, + Default = Constant }; //! String for target distribution. -extern const char* eawhtarget_names[eawhtargetNR + 1]; -//! Macro for target distribution string. -#define EAWHTARGET(e) enum_name(e, gmx::eawhtargetNR, gmx::eawhtarget_names) +const char* enumValueToString(AwhTargetType enumValue); //! Weight histogram growth enum. -enum +enum class AwhHistogramGrowthType : int { - eawhgrowthEXP_LINEAR, - eawhgrowthLINEAR, - eawhgrowthNR + ExponentialLinear, + Linear, + Count, + Default = ExponentialLinear }; //! String for weight histogram growth -extern const char* eawhgrowth_names[eawhgrowthNR + 1]; -//! Macro for weight histogram growth string. -#define EAWHGROWTH(e) enum_name(e, gmx::eawhgrowthNR, gmx::eawhgrowth_names) +const char* enumValueToString(AwhHistogramGrowthType enumValue); //! AWH potential type enum. -enum +enum class AwhPotentialType : int { - eawhpotentialCONVOLVED, - eawhpotentialUMBRELLA, - eawhpotentialNR + Convolved, + Umbrella, + Count, + Default = Convolved }; //! String for AWH potential type -extern const char* eawhpotential_names[eawhpotentialNR + 1]; -//! Macro for AWH potential type string. -#define EAWHPOTENTIAL(e) enum_name(e, gmx::eawhpotentialNR, gmx::eawhpotential_names) +const char* enumValueToString(AwhPotentialType enumValue); //! AWH bias reaction coordinate provider -enum +enum class AwhCoordinateProviderType : int { - eawhcoordproviderPULL, - eawhcoordproviderFREE_ENERGY_LAMBDA, - eawhcoordproviderNR + Pull, + FreeEnergyLambda, + Count, + Default = Pull }; //! String for AWH bias reaction coordinate provider. -extern const char* eawhcoordprovider_names[eawhcoordproviderNR + 1]; -//! Macro for AWH bias reaction coordinate provider. -#define EAWHCOORDPROVIDER(e) enum_name(e, gmx::eawhcoordproviderNR, gmx::eawhcoordprovider_names) +const char* enumValueToString(AwhCoordinateProviderType enumValue); /*! \cond INTERNAL */ //! Parameters for an AWH coordinate dimension. struct AwhDimParams { - int eCoordProvider; /**< The module providing the reaction coordinate. */ - int coordIndex; /**< Index of reaction coordinate in the provider. */ - double origin; /**< Start value of the interval. */ - double end; /**< End value of the interval. */ - double period; /**< The period of this dimension (= 0 if not periodic). */ - double forceConstant; /**< The force constant in kJ/mol/nm^2, kJ/mol/rad^2 */ + AwhCoordinateProviderType eCoordProvider; /**< The module providing the reaction coordinate. */ + int coordIndex; /**< Index of reaction coordinate in the provider. */ + double origin; /**< Start value of the interval. */ + double end; /**< End value of the interval. */ + double period; /**< The period of this dimension (= 0 if not periodic). */ + double forceConstant; /**< The force constant in kJ/mol/nm^2, kJ/mol/rad^2 */ double diffusion; /**< Estimated diffusion constant in units of nm^2/ps, rad^2/ps or ps^-1. */ double coordValueInit; /**< The initial coordinate value. */ double coverDiameter; /**< The diameter that needs to be sampled around a point before it is considered covered. */ @@ -127,12 +123,12 @@ struct AwhDimParams struct AwhBiasParams { // TODO: Turn dimParams into a std::vector when moved into AWH module - int ndim; /**< Dimension of the coordinate space. */ - AwhDimParams* dimParams; /**< AWH parameters per dimension. */ - int eTarget; /**< Type of target distribution. */ - double targetBetaScaling; /**< Beta scaling value for Boltzmann type target distributions. */ - double targetCutoff; /**< Free energy cutoff value for cutoff type target distribution in kJ/mol.*/ - int eGrowth; /**< How the biasing histogram grows. */ + int ndim; /**< Dimension of the coordinate space. */ + AwhDimParams* dimParams; /**< AWH parameters per dimension. */ + AwhTargetType eTarget; /**< Type of target distribution. */ + double targetBetaScaling; /**< Beta scaling value for Boltzmann type target distributions. */ + double targetCutoff; /**< Free energy cutoff value for cutoff type target distribution in kJ/mol.*/ + AwhHistogramGrowthType eGrowth; /**< How the biasing histogram grows. */ bool bUserData; /**< Is there a user-defined initial PMF estimate and target estimate? */ double errorInitial; /**< Estimated initial free energy error in kJ/mol. */ int shareGroup; /**< When >0, the bias is shared with biases of the same group and across multiple simulations when shareBiasMultisim=true */ @@ -147,10 +143,10 @@ struct AwhParams AwhBiasParams* awhBiasParams; /**< AWH bias parameters.*/ int64_t seed; /**< Random seed.*/ int nstOut; /**< Output step interval.*/ - int nstSampleCoord; /**< Number of samples per coordinate sample (also used for PMF) */ - int numSamplesUpdateFreeEnergy; /**< Number of samples per free energy update. */ - int ePotential; /**< Type of potential. */ - gmx_bool shareBiasMultisim; /**< When true, share biases with shareGroup>0 between multi-simulations */ + int nstSampleCoord; /**< Number of samples per coordinate sample (also used for PMF) */ + int numSamplesUpdateFreeEnergy; /**< Number of samples per free energy update. */ + AwhPotentialType ePotential; /**< Type of potential. */ + gmx_bool shareBiasMultisim; /**< When true, share biases with shareGroup>0 between multi-simulations */ }; /*! \endcond */ diff --git a/src/gromacs/mdtypes/inputrec.cpp b/src/gromacs/mdtypes/inputrec.cpp index e5abdb4f5e..b0853f0de3 100644 --- a/src/gromacs/mdtypes/inputrec.cpp +++ b/src/gromacs/mdtypes/inputrec.cpp @@ -622,7 +622,7 @@ static void pr_awh_bias_dim(FILE* fp, int indent, gmx::AwhDimParams* awhDimParam pr_indent(fp, indent); indent++; fprintf(fp, "%s:\n", prefix); - PS("coord-provider", EAWHCOORDPROVIDER(awhDimParams->eCoordProvider)); + PS("coord-provider", enumValueToString(awhDimParams->eCoordProvider)); PI("coord-index", awhDimParams->coordIndex + 1); PR("start", awhDimParams->origin); PR("end", awhDimParams->end); @@ -639,9 +639,9 @@ static void pr_awh_bias(FILE* fp, int indent, gmx::AwhBiasParams* awhBiasParams, sprintf(opt, "%s-error-init", prefix); PR(opt, awhBiasParams->errorInitial); sprintf(opt, "%s-growth", prefix); - PS(opt, EAWHGROWTH(awhBiasParams->eGrowth)); + PS(opt, enumValueToString(awhBiasParams->eGrowth)); sprintf(opt, "%s-target", prefix); - PS(opt, EAWHTARGET(awhBiasParams->eTarget)); + PS(opt, enumValueToString(awhBiasParams->eTarget)); sprintf(opt, "%s-target-beta-scalng", prefix); PR(opt, awhBiasParams->targetBetaScaling); sprintf(opt, "%s-target-cutoff", prefix); @@ -665,7 +665,7 @@ static void pr_awh_bias(FILE* fp, int indent, gmx::AwhBiasParams* awhBiasParams, static void pr_awh(FILE* fp, int indent, gmx::AwhParams* awhParams) { - PS("awh-potential", EAWHPOTENTIAL(awhParams->ePotential)); + PS("awh-potential", enumValueToString(awhParams->ePotential)); PI("awh-seed", awhParams->seed); PI("awh-nstout", awhParams->nstOut); PI("awh-nstsample", awhParams->nstSampleCoord); @@ -1141,7 +1141,7 @@ static void cmp_awhBiasParams(FILE* fp, real abstol) { cmp_int(fp, "inputrec->awhParams->ndim", biasIndex, bias1->ndim, bias2->ndim); - cmp_int(fp, "inputrec->awhParams->biaseTarget", biasIndex, bias1->eTarget, bias2->eTarget); + cmpEnum(fp, "inputrec->awhParams->biaseTarget", bias1->eTarget, bias2->eTarget); cmp_double(fp, "inputrec->awhParams->biastargetBetaScaling", biasIndex, @@ -1156,7 +1156,8 @@ static void cmp_awhBiasParams(FILE* fp, bias2->targetCutoff, ftol, abstol); - cmp_int(fp, "inputrec->awhParams->biaseGrowth", biasIndex, bias1->eGrowth, bias2->eGrowth); + cmpEnum( + fp, "inputrec->awhParams->biaseGrowth", bias1->eGrowth, bias2->eGrowth); cmp_bool(fp, "inputrec->awhParams->biasbUserData", biasIndex, bias1->bUserData, bias2->bUserData); cmp_double(fp, "inputrec->awhParams->biaserror_initial", @@ -1184,7 +1185,7 @@ static void cmp_awhParams(FILE* fp, const gmx::AwhParams* awh1, const gmx::AwhPa -1, awh1->numSamplesUpdateFreeEnergy, awh2->numSamplesUpdateFreeEnergy); - cmp_int(fp, "inputrec->awhParams->ePotential", -1, awh1->ePotential, awh2->ePotential); + cmpEnum(fp, "inputrec->awhParams->ePotential", awh1->ePotential, awh2->ePotential); cmp_bool(fp, "inputrec->awhParams->shareBiasMultisim", -1, awh1->shareBiasMultisim, awh2->shareBiasMultisim); if (awh1->numBias == awh2->numBias) -- 2.22.0