887a7913f35fe9eddf7ea98267370b06ffb180a1
[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 "testutils/testasserts.h"
58
59 namespace gmx
60 {
61 namespace test
62 {
63 namespace
64 {
65
66 //! Test fixture accepting a value to pass into calculateAcceptanceWeight
67 class CalculateAcceptanceWeightSimple : public ::testing::Test, public ::testing::WithParamInterface<real>
68 {
69 };
70 // Check that unimplemented calculation modes throw
71 TEST_P(CalculateAcceptanceWeightSimple, UnknownCalculationModeThrows)
72 {
73     for (auto calculationMode = 0; calculationMode < elamstatsNR; ++calculationMode)
74     {
75         if (calculationMode != elamstatsBARKER && calculationMode != elamstatsMINVAR
76             && calculationMode != elamstatsMETROPOLIS)
77         {
78             EXPECT_THROW_GMX(calculateAcceptanceWeight(calculationMode, GetParam()), NotImplementedError);
79         }
80     }
81 }
82 // Check that implemented calculation modes don't throw
83 TEST_P(CalculateAcceptanceWeightSimple, KnownCalculationModeDoesNotThrow)
84 {
85     EXPECT_NO_THROW(calculateAcceptanceWeight(elamstatsMETROPOLIS, GetParam()));
86     EXPECT_NO_THROW(calculateAcceptanceWeight(elamstatsBARKER, GetParam()));
87     EXPECT_NO_THROW(calculateAcceptanceWeight(elamstatsMINVAR, GetParam()));
88 }
89 // Barker and MinVar are expected to be equal
90 TEST_P(CalculateAcceptanceWeightSimple, BarkerAndMinVarAreIdentical)
91 {
92     EXPECT_EQ(calculateAcceptanceWeight(elamstatsBARKER, GetParam()),
93               calculateAcceptanceWeight(elamstatsMINVAR, GetParam()));
94 }
95
96 /*! \brief Test fixture accepting a calculation mode and an input value for
97  *         calculateAcceptanceWeight as well as the expected output value
98  */
99 using RegressionTuple = std::tuple<int, real, real>;
100 class CalculateAcceptanceWeightRangeRegression :
101     public ::testing::Test,
102     public ::testing::WithParamInterface<RegressionTuple>
103 {
104 };
105 // Check that output is as expected
106 TEST_P(CalculateAcceptanceWeightRangeRegression, ValuesMatch)
107 {
108     const auto calculationMode = std::get<0>(GetParam());
109     const auto inputValue      = std::get<1>(GetParam());
110     const auto expectedOutput  = std::get<2>(GetParam());
111
112     EXPECT_REAL_EQ(expectedOutput, calculateAcceptanceWeight(calculationMode, inputValue));
113 }
114
115 INSTANTIATE_TEST_CASE_P(
116         SimpleTests,
117         CalculateAcceptanceWeightSimple,
118         ::testing::Values(1., -1., 0., GMX_REAL_NEGZERO, GMX_REAL_EPS, -GMX_REAL_EPS, GMX_REAL_MAX, -GMX_REAL_MAX));
119 INSTANTIATE_TEST_CASE_P(
120         RegressionTests,
121         CalculateAcceptanceWeightRangeRegression,
122         ::testing::Values(RegressionTuple{ elamstatsMETROPOLIS, 0.0, 1.0 },
123                           RegressionTuple{ elamstatsMETROPOLIS, GMX_REAL_NEGZERO, 1.0 },
124                           RegressionTuple{ elamstatsMETROPOLIS, GMX_REAL_EPS, 1.0 },
125                           RegressionTuple{ elamstatsMETROPOLIS, -1.0, 1.0 },
126                           RegressionTuple{ elamstatsMETROPOLIS, -GMX_REAL_MAX, 1.0 },
127                           RegressionTuple{ elamstatsMETROPOLIS, 1.0, std::exp(-1.0) },
128                           RegressionTuple{ elamstatsMETROPOLIS, GMX_REAL_MAX, 0.0 },
129                           RegressionTuple{ elamstatsBARKER, 0.0, 0.5 },
130                           RegressionTuple{ elamstatsBARKER, GMX_REAL_NEGZERO, 0.5 },
131                           RegressionTuple{ elamstatsBARKER, GMX_REAL_EPS, 0.5 },
132                           RegressionTuple{ elamstatsBARKER, -1.0, 1.0 / (1.0 + std::exp(-1.0)) },
133                           RegressionTuple{ elamstatsBARKER, -GMX_REAL_MAX, 1.0 },
134                           RegressionTuple{ elamstatsBARKER, 1.0, 1.0 / (1.0 + std::exp(1.0)) },
135                           RegressionTuple{ elamstatsBARKER, GMX_REAL_MAX, 0.0 }));
136
137 } // namespace
138 } // namespace test
139 } // namespace gmx