2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2017,2018,2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 #include "gromacs/awh/bias.h"
45 #include <gmock/gmock.h>
46 #include <gtest/gtest.h>
48 #include "gromacs/awh/correlationgrid.h"
49 #include "gromacs/awh/pointstate.h"
50 #include "gromacs/mdtypes/awh_params.h"
51 #include "gromacs/utility/stringutil.h"
53 #include "testutils/refdata.h"
54 #include "testutils/testasserts.h"
63 * Struct that gathers all input for setting up and using a Bias
65 struct AwhTestParameters
67 AwhTestParameters() = default;
69 AwhTestParameters(AwhTestParameters&& o) noexcept :
71 awhDimParams(o.awhDimParams),
72 awhBiasParams(o.awhBiasParams),
73 awhParams(o.awhParams),
74 dimParams(std::move(o.dimParams))
76 awhBiasParams.dimParams = &awhDimParams;
77 awhParams.awhBiasParams = &awhBiasParams;
79 double beta; //!< 1/(kB*T)
81 AwhDimParams awhDimParams; //!< Dimension parameters pointed to by \p awhBiasParams
82 AwhBiasParams awhBiasParams; //!< Bias parameters pointed to by \[ awhParams
83 AwhParams awhParams; //!< AWH parameters, this is the struct to actually use
85 std::vector<DimParams> dimParams; //!< Dimension parameters for setting up Bias
88 //! Helper function to set up the C-style AWH parameters for the test
89 static AwhTestParameters getAwhTestParameters(int eawhgrowth, int eawhpotential)
91 AwhTestParameters params;
95 AwhDimParams& awhDimParams = params.awhDimParams;
97 awhDimParams.period = 0;
98 awhDimParams.diffusion = 0.1;
99 awhDimParams.origin = 0.5;
100 awhDimParams.end = 1.5;
101 awhDimParams.coordValueInit = awhDimParams.origin;
102 awhDimParams.coverDiameter = 0;
104 AwhBiasParams& awhBiasParams = params.awhBiasParams;
106 awhBiasParams.ndim = 1;
107 awhBiasParams.dimParams = &awhDimParams;
108 awhBiasParams.eTarget = eawhtargetCONSTANT;
109 awhBiasParams.targetBetaScaling = 0;
110 awhBiasParams.targetCutoff = 0;
111 awhBiasParams.eGrowth = eawhgrowth;
112 awhBiasParams.bUserData = FALSE;
113 awhBiasParams.errorInitial = 0.5 / params.beta;
114 awhBiasParams.shareGroup = 0;
115 awhBiasParams.equilibrateHistogram = FALSE;
117 double convFactor = 1;
119 int64_t seed = 93471803;
121 params.dimParams.emplace_back(convFactor, k, params.beta);
123 AwhParams& awhParams = params.awhParams;
125 awhParams.numBias = 1;
126 awhParams.awhBiasParams = &awhBiasParams;
127 awhParams.seed = seed;
128 awhParams.nstOut = 0;
129 awhParams.nstSampleCoord = 1;
130 awhParams.numSamplesUpdateFreeEnergy = 10;
131 awhParams.ePotential = eawhpotential;
132 awhParams.shareBiasMultisim = FALSE;
137 //! Database of 21 test coordinates that represent a trajectory */
138 const double g_coords[] = { 0.62, 0.70, 0.68, 0.80, 0.93, 0.87, 1.16, 1.14, 0.95, 0.89, 0.91,
139 0.86, 0.88, 0.79, 0.75, 0.82, 0.74, 0.70, 0.68, 0.71, 0.73 };
141 //! Convenience typedef: growth type enum, potential type enum, disable update skips
142 typedef std::tuple<int, int, BiasParams::DisableUpdateSkips> BiasTestParameters;
144 /*! \brief Test fixture for testing Bias updates
146 class BiasTest : public ::testing::TestWithParam<BiasTestParameters>
149 //! Random seed for AWH MC sampling
152 //! Coordinates representing a trajectory in time
153 std::vector<double> coordinates_;
155 std::unique_ptr<Bias> bias_;
157 BiasTest() : coordinates_(std::begin(g_coords), std::end(g_coords))
159 /* We test all combinations of:
161 * eawhgrowthLINEAR: final, normal update phase
162 * ewahgrowthEXP_LINEAR: intial phase, updated size is constant
163 * eawhpotential (should only affect the force output):
164 * eawhpotentialUMBRELLA: MC on lambda (umbrella potential location)
165 * eawhpotentialCONVOLVED: MD on a convolved potential landscape
166 * disableUpdateSkips (should not affect the results):
167 * BiasParams::DisableUpdateSkips::yes: update the point state for every sample
168 * BiasParams::DisableUpdateSkips::no: update the point state at an interval > 1 sample
170 * Note: It would be nice to explicitly check that eawhpotential
171 * and disableUpdateSkips do not affect the point state.
172 * But the reference data will also ensure this.
176 BiasParams::DisableUpdateSkips disableUpdateSkips;
177 std::tie(eawhgrowth, eawhpotential, disableUpdateSkips) = GetParam();
179 /* Set up a basic AWH setup with a single, 1D bias with parameters
180 * such that we can measure the effects of different parameters.
181 * The idea is to, among other things, have part of the interval
182 * not covered by samples.
184 const AwhTestParameters params = getAwhTestParameters(eawhgrowth, eawhpotential);
186 seed_ = params.awhParams.seed;
188 double mdTimeStep = 0.1;
190 int numSamples = coordinates_.size() - 1; // No sample taken at step 0
191 GMX_RELEASE_ASSERT(numSamples % params.awhParams.numSamplesUpdateFreeEnergy == 0,
192 "This test is intended to reproduce the situation when the might need "
193 "to write output during a normal AWH run, therefore the number of "
194 "samples should be a multiple of the free-energy update interval (but "
195 "the test should also runs fine without this condition).");
197 bias_ = std::make_unique<Bias>(-1, params.awhParams, params.awhBiasParams, params.dimParams,
198 params.beta, mdTimeStep, 1, "", Bias::ThisRankWillDoIO::No,
203 TEST_P(BiasTest, ForcesBiasPmf)
205 gmx::test::TestReferenceData data;
206 gmx::test::TestReferenceChecker checker(data.rootChecker());
210 /* Make strings with the properties we expect to be different in the tests.
211 * These also helps to interpret the reference data.
213 std::vector<std::string> props;
214 props.push_back(formatString("stage: %s", bias.state().inInitialStage() ? "initial" : "final"));
215 props.push_back(formatString("convolve forces: %s", bias.params().convolveForce ? "yes" : "no"));
216 props.push_back(formatString("skip updates: %s", bias.params().skipUpdates() ? "yes" : "no"));
218 SCOPED_TRACE(gmx::formatString("%s, %s, %s", props[0].c_str(), props[1].c_str(), props[2].c_str()));
220 std::vector<double> force, pot, potJump;
222 double coordMaxValue = 0;
223 double potentialJump = 0;
225 for (auto& coord : coordinates_)
227 coordMaxValue = std::max(coordMaxValue, std::abs(coord));
229 awh_dvec coordValue = { coord, 0, 0, 0 };
230 double potential = 0;
231 gmx::ArrayRef<const double> biasForce = bias.calcForceAndUpdateBias(
232 coordValue, &potential, &potentialJump, nullptr, nullptr, step, step, seed_, nullptr);
234 force.push_back(biasForce[0]);
235 pot.push_back(potential);
236 potJump.push_back(potentialJump);
241 /* When skipping updates, ensure all skipped updates are performed here.
242 * This should result in the same bias state as at output in a normal run.
244 if (bias.params().skipUpdates())
246 bias.doSkippedUpdatesForAllPoints();
249 std::vector<double> pointBias, logPmfsum;
250 for (auto& point : bias.state().points())
252 pointBias.push_back(point.bias());
253 logPmfsum.push_back(point.logPmfSum());
256 /* The umbrella force is computed from the coordinate deviation.
257 * In taking this deviation we lose a lot of precision, so we should
258 * compare against k*max(coord) instead of the instantaneous force.
260 const double kCoordMax = bias.dimParams()[0].k * coordMaxValue;
262 constexpr int ulpTol = 10;
264 checker.checkSequence(props.begin(), props.end(), "Properties");
265 checker.setDefaultTolerance(absoluteTolerance(kCoordMax * GMX_DOUBLE_EPS * ulpTol));
266 checker.checkSequence(force.begin(), force.end(), "Force");
267 checker.checkSequence(pot.begin(), pot.end(), "Potential");
268 checker.checkSequence(potJump.begin(), potJump.end(), "PotentialJump");
269 checker.setDefaultTolerance(relativeToleranceAsUlp(1.0, ulpTol));
270 checker.checkSequence(pointBias.begin(), pointBias.end(), "PointBias");
271 checker.checkSequence(logPmfsum.begin(), logPmfsum.end(), "PointLogPmfsum");
274 /* Scan initial/final phase, MC/convolved force and update skip (not) allowed
275 * Both the convolving and skipping should not affect the bias and PMF.
276 * It would be nice if the test would explicitly check for this.
277 * Currently this is tested through identical reference data.
279 INSTANTIATE_TEST_CASE_P(WithParameters,
281 ::testing::Combine(::testing::Values(eawhgrowthLINEAR, eawhgrowthEXP_LINEAR),
282 ::testing::Values(eawhpotentialUMBRELLA, eawhpotentialCONVOLVED),
283 ::testing::Values(BiasParams::DisableUpdateSkips::yes,
284 BiasParams::DisableUpdateSkips::no)));
286 // Test that we detect coverings and exit the initial stage at the correct step
287 TEST(BiasTest, DetectsCovering)
289 const AwhTestParameters params = getAwhTestParameters(eawhgrowthEXP_LINEAR, eawhpotentialCONVOLVED);
290 const AwhDimParams& awhDimParams = params.awhParams.awhBiasParams[0].dimParams[0];
292 const double mdTimeStep = 0.1;
294 Bias bias(-1, params.awhParams, params.awhBiasParams, params.dimParams, params.beta, mdTimeStep,
295 1, "", Bias::ThisRankWillDoIO::No);
297 /* We use a trajectory of the sum of two sines to cover the reaction
298 * coordinate range in a semi-realistic way. The period is 4*pi=12.57.
299 * We get out of the initial stage after 4 coverings at step 300.
301 const int64_t exitStepRef = 300;
302 const double midPoint = 0.5 * (awhDimParams.end + awhDimParams.origin);
303 const double halfWidth = 0.5 * (awhDimParams.end - awhDimParams.origin);
305 bool inInitialStage = bias.state().inInitialStage();
306 /* Normally this loop exits at exitStepRef, but we extend with failure */
308 for (step = 0; step <= 2 * exitStepRef; step++)
310 double t = step * mdTimeStep;
311 double coord = midPoint + halfWidth * (0.5 * std::sin(t) + 0.55 * std::sin(1.5 * t));
313 awh_dvec coordValue = { coord, 0, 0, 0 };
314 double potential = 0;
315 double potentialJump = 0;
316 bias.calcForceAndUpdateBias(coordValue, &potential, &potentialJump, nullptr, nullptr, step,
317 step, params.awhParams.seed, nullptr);
319 inInitialStage = bias.state().inInitialStage();
326 EXPECT_EQ(false, inInitialStage);
329 EXPECT_EQ(exitStepRef, step);