Update bundled GoogleTest to current HEAD
[alexxy/gromacs.git] / src / gromacs / mdlib / tests / expanded.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief Tests for expanded ensemble
37  *
38  * This file contains unit tests for functions used by the expanded
39  * ensemble.
40  *
41  * \todo Add more tests as the expanded ensemble implementation
42  *       gets more modular (#3848).
43  *
44  * \author Pascal Merz <pascal.merz@me.com>
45  * \author Michael Shirts <michael.shirts@colorado.edu>
46  * \ingroup module_mdlib
47  */
48 #include "gmxpre.h"
49
50 #include <cmath>
51
52 #include <gtest/gtest.h>
53
54 #include "gromacs/mdlib/expanded_internal.h"
55 #include "gromacs/mdtypes/md_enums.h"
56
57 #include "gromacs/utility/enumerationhelpers.h"
58 #include "testutils/testasserts.h"
59
60 namespace gmx
61 {
62 namespace test
63 {
64 namespace
65 {
66
67 //! Test fixture accepting a value to pass into calculateAcceptanceWeight
68 class CalculateAcceptanceWeightSimple : public ::testing::Test, public ::testing::WithParamInterface<real>
69 {
70 };
71 // Check that unimplemented calculation modes throw
72 TEST_P(CalculateAcceptanceWeightSimple, UnknownCalculationModeThrows)
73 {
74     for (auto calculationMode : gmx::EnumerationArray<LambdaWeightCalculation, bool>::keys())
75     {
76         if (calculationMode != LambdaWeightCalculation::Barker
77             && calculationMode != LambdaWeightCalculation::Minvar
78             && calculationMode != LambdaWeightCalculation::Metropolis)
79         {
80             EXPECT_THROW_GMX(calculateAcceptanceWeight(calculationMode, GetParam()), NotImplementedError);
81         }
82     }
83 }
84 // Check that implemented calculation modes don't throw
85 TEST_P(CalculateAcceptanceWeightSimple, KnownCalculationModeDoesNotThrow)
86 {
87     EXPECT_NO_THROW(calculateAcceptanceWeight(LambdaWeightCalculation::Metropolis, GetParam()));
88     EXPECT_NO_THROW(calculateAcceptanceWeight(LambdaWeightCalculation::Barker, GetParam()));
89     EXPECT_NO_THROW(calculateAcceptanceWeight(LambdaWeightCalculation::Minvar, GetParam()));
90 }
91 // Barker and MinVar are expected to be equal
92 TEST_P(CalculateAcceptanceWeightSimple, BarkerAndMinVarAreIdentical)
93 {
94     EXPECT_EQ(calculateAcceptanceWeight(LambdaWeightCalculation::Barker, GetParam()),
95               calculateAcceptanceWeight(LambdaWeightCalculation::Minvar, GetParam()));
96 }
97
98 /*! \brief Test fixture accepting a calculation mode and an input value for
99  *         calculateAcceptanceWeight as well as the expected output value
100  */
101 using RegressionTuple = std::tuple<LambdaWeightCalculation, real, real>;
102 class CalculateAcceptanceWeightRangeRegression :
103     public ::testing::Test,
104     public ::testing::WithParamInterface<RegressionTuple>
105 {
106 };
107 // Check that output is as expected
108 TEST_P(CalculateAcceptanceWeightRangeRegression, ValuesMatch)
109 {
110     const auto calculationMode = std::get<0>(GetParam());
111     const auto inputValue      = std::get<1>(GetParam());
112     const auto expectedOutput  = std::get<2>(GetParam());
113
114     EXPECT_REAL_EQ(expectedOutput, calculateAcceptanceWeight(calculationMode, inputValue));
115 }
116
117 INSTANTIATE_TEST_SUITE_P(
118         SimpleTests,
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(
122         RegressionTests,
123         CalculateAcceptanceWeightRangeRegression,
124         ::testing::Values(
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 }));
139
140 } // namespace
141 } // namespace test
142 } // namespace gmx