2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2020,2021, 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.
36 * \brief Tests for expanded ensemble
38 * This file contains unit tests for functions used by the expanded
41 * \todo Add more tests as the expanded ensemble implementation
42 * gets more modular (#3848).
44 * \author Pascal Merz <pascal.merz@me.com>
45 * \author Michael Shirts <michael.shirts@colorado.edu>
46 * \ingroup module_mdlib
52 #include <gtest/gtest.h>
54 #include "gromacs/mdlib/expanded_internal.h"
55 #include "gromacs/mdtypes/md_enums.h"
57 #include "gromacs/utility/enumerationhelpers.h"
58 #include "testutils/testasserts.h"
67 //! Test fixture accepting a value to pass into calculateAcceptanceWeight
68 class CalculateAcceptanceWeightSimple : public ::testing::Test, public ::testing::WithParamInterface<real>
71 // Check that unimplemented calculation modes throw
72 TEST_P(CalculateAcceptanceWeightSimple, UnknownCalculationModeThrows)
74 for (auto calculationMode : gmx::EnumerationArray<LambdaWeightCalculation, bool>::keys())
76 if (calculationMode != LambdaWeightCalculation::Barker
77 && calculationMode != LambdaWeightCalculation::Minvar
78 && calculationMode != LambdaWeightCalculation::Metropolis)
80 EXPECT_THROW_GMX(calculateAcceptanceWeight(calculationMode, GetParam()), NotImplementedError);
84 // Check that implemented calculation modes don't throw
85 TEST_P(CalculateAcceptanceWeightSimple, KnownCalculationModeDoesNotThrow)
87 EXPECT_NO_THROW(calculateAcceptanceWeight(LambdaWeightCalculation::Metropolis, GetParam()));
88 EXPECT_NO_THROW(calculateAcceptanceWeight(LambdaWeightCalculation::Barker, GetParam()));
89 EXPECT_NO_THROW(calculateAcceptanceWeight(LambdaWeightCalculation::Minvar, GetParam()));
91 // Barker and MinVar are expected to be equal
92 TEST_P(CalculateAcceptanceWeightSimple, BarkerAndMinVarAreIdentical)
94 EXPECT_EQ(calculateAcceptanceWeight(LambdaWeightCalculation::Barker, GetParam()),
95 calculateAcceptanceWeight(LambdaWeightCalculation::Minvar, GetParam()));
98 /*! \brief Test fixture accepting a calculation mode and an input value for
99 * calculateAcceptanceWeight as well as the expected output value
101 using RegressionTuple = std::tuple<LambdaWeightCalculation, real, real>;
102 class CalculateAcceptanceWeightRangeRegression :
103 public ::testing::Test,
104 public ::testing::WithParamInterface<RegressionTuple>
107 // Check that output is as expected
108 TEST_P(CalculateAcceptanceWeightRangeRegression, ValuesMatch)
110 const auto calculationMode = std::get<0>(GetParam());
111 const auto inputValue = std::get<1>(GetParam());
112 const auto expectedOutput = std::get<2>(GetParam());
114 EXPECT_REAL_EQ(expectedOutput, calculateAcceptanceWeight(calculationMode, inputValue));
117 INSTANTIATE_TEST_SUITE_P(
119 CalculateAcceptanceWeightSimple,
120 ::testing::Values(1., -1., 0., GMX_REAL_NEGZERO, GMX_REAL_EPS, -GMX_REAL_EPS, GMX_REAL_MAX, -GMX_REAL_MAX));
121 INSTANTIATE_TEST_SUITE_P(
123 CalculateAcceptanceWeightRangeRegression,
125 RegressionTuple{ LambdaWeightCalculation::Metropolis, 0.0, 1.0 },
126 RegressionTuple{ LambdaWeightCalculation::Metropolis, GMX_REAL_NEGZERO, 1.0 },
127 RegressionTuple{ LambdaWeightCalculation::Metropolis, GMX_REAL_EPS, 1.0 },
128 RegressionTuple{ LambdaWeightCalculation::Metropolis, -1.0, 1.0 },
129 RegressionTuple{ LambdaWeightCalculation::Metropolis, -GMX_REAL_MAX, 1.0 },
130 RegressionTuple{ LambdaWeightCalculation::Metropolis, 1.0, std::exp(-1.0) },
131 RegressionTuple{ LambdaWeightCalculation::Metropolis, GMX_REAL_MAX, 0.0 },
132 RegressionTuple{ LambdaWeightCalculation::Barker, 0.0, 0.5 },
133 RegressionTuple{ LambdaWeightCalculation::Barker, GMX_REAL_NEGZERO, 0.5 },
134 RegressionTuple{ LambdaWeightCalculation::Barker, GMX_REAL_EPS, 0.5 },
135 RegressionTuple{ LambdaWeightCalculation::Barker, -1.0, 1.0 / (1.0 + std::exp(-1.0)) },
136 RegressionTuple{ LambdaWeightCalculation::Barker, -GMX_REAL_MAX, 1.0 },
137 RegressionTuple{ LambdaWeightCalculation::Barker, 1.0, 1.0 / (1.0 + std::exp(1.0)) },
138 RegressionTuple{ LambdaWeightCalculation::Barker, GMX_REAL_MAX, 0.0 }));