b2b3fe0fc39da6c03d9c3fddcd72b07456acfd62
[alexxy/gromacs.git] / src / gromacs / mdlib / tests / freeenergyparameters.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  *
37  * \brief Tests routines in freeenergyparameters.h .
38  *
39  * \author Christian Blau <blau@kth.se>
40  */
41
42 #include "gmxpre.h"
43
44 #include "gromacs/mdlib/freeenergyparameters.h"
45
46 #include <gtest/gtest.h>
47
48 #include "gromacs/mdtypes/inputrec.h"
49
50 #include "gromacs/utility/arrayref.h"
51 #include "gromacs/utility/enumerationhelpers.h"
52 #include "testutils/testasserts.h"
53 #include "testutils/testmatchers.h"
54
55
56 namespace gmx
57 {
58 namespace test
59 {
60 namespace
61 {
62
63
64 /*! \brief Parameters that will vary from test to test.
65  */
66 struct FreeEnergyParameterTestParameters
67 {
68     //! current state of lambda in the simulation, -1 if not set
69     int currentLambdaState = -1;
70     //! Fractional value of lambda to start from, -1 if not set
71     double initLambda = -1;
72     //! The initial number of the state, -1 if not set
73     int initFepState = -1;
74     //! Change of lambda per time step (fraction of (0.1)
75     double deltaLambda = 0;
76     //! number of lambda entries
77     int nLambda = 0;
78     //! the current simulation step
79     int64_t step = 0;
80     //! the expected lambda at the current simulation step
81     gmx::EnumerationArray<FreeEnergyPerturbationCouplingType, real> expectedLambdas = { -1, -1, -1,
82                                                                                         -1, -1, -1,
83                                                                                         -1 };
84 };
85
86
87 /*! \brief Sets of parameters on which to run the tests.
88  */
89 const FreeEnergyParameterTestParameters freeEnergyParameterSets[] = {
90     // no parameters set at all
91     { -1, -1, -1, 0, 1, 0, { -1, -1, -1, -1, -1, -1, -1 } },
92     // setting current lambda state to 0, no other variables set, using eftpNR * [0.8] as lambda state matrix
93     { 0, -1, -1, 0, 1, 1, { 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8 } },
94     // setting current lambda state to 1, no other variables set, using eftpNR * [0.2,0.8] as lambda state matrix
95     { 1, -1, -1, 0, 2, 1, { 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8 } },
96     // test that non-zero deltaLambda trumps current lambda state
97     // setting current lambda state to 1, using deltaLambda 0.1 and setp 0 and eftpNR * [0.2,0.8] as lambda state matrix
98     { 1, -1, -1, 0.1, 2, 0, { 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2 } },
99     // test that interpolating lambda values starting
100     // from lambda = 0, deltaLambda = 0.1, step = 10 results in values at end-state
101     { 1, 0, -1, 0.1, 2, 10, { 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8 } },
102     // interpolating half the way
103     { 1, 0, -1, 0.1, 2, 5, { 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 } },
104     // starting from end-state 1 and move lambda half-way with negative deltaLambda = -0.1
105     { -1, -1, 1, -0.1, 2, 5, { 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 } },
106     // starting from end-state 1 and move one step backwards with negative deltaLambda = -0.1
107     { -1, -1, 1, -0.1, 2, 1, { 0.74, 0.74, 0.74, 0.74, 0.74, 0.74, 0.74 } },
108     // three lambda states, the last two equal ([0.2,0.8,0.8]), move forward with deltaLambda = 0.1
109     { -1, -1, 0, 0.1, 3, 0, { 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2 } },
110     { -1, -1, 0, 0.1, 3, 3, { 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56 } },
111     { -1, -1, 0, 0.1, 3, 7, { 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8 } },
112     { -1, -1, 0, 0.1, 3, 8, { 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8 } },
113     { -1, -1, 0, 0.1, 3, 10, { 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8 } },
114     // three lambda states, the last two equal ([0.2,0.8,0.8]), move backwards
115     { -1, -1, 2, -0.1, 3, 1, { 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8 } },
116     { -1, -1, 2, -0.1, 3, 2, { 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8 } },
117     { -1, -1, 2, -0.1, 3, 3, { 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8 } },
118     { -1, -1, 2, -0.1, 3, 5, { 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8 } },
119     { -1, -1, 2, -0.1, 3, 7, { 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56 } },
120     { -1, -1, 2, -0.1, 3, 8, { 0.44, 0.44, 0.44, 0.44, 0.44, 0.44, 0.44 } },
121     { -1, -1, 2, -0.1, 3, 10, { 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2 } },
122     // three lambda states, the last two equal ([0.2,0.8,0.8]), start in middle state, move backwards
123     { -1, -1, 1, -0.1, 3, 0, { 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8 } },
124     { -1, -1, 1, -0.1, 3, 2, { 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56 } },
125     { -1, -1, 1, -0.1, 3, 3, { 0.44, 0.44, 0.44, 0.44, 0.44, 0.44, 0.44 } },
126 };
127
128 /*! \brief Test for setting free energy parameters.
129  */
130 class FreeEnergyParameterTest : public ::testing::TestWithParam<FreeEnergyParameterTestParameters>
131 {
132 public:
133     //! Fills in the required FEP values for testing from the test parameters
134     t_lambda getFepVals()
135     {
136         t_lambda fepvals;
137         fepvals.init_fep_state = GetParam().initFepState;
138         fepvals.init_lambda    = GetParam().initLambda;
139         fepvals.delta_lambda   = GetParam().deltaLambda;
140         std::fill(fepvals.all_lambda.begin(),
141                   fepvals.all_lambda.end(),
142                   defaultLambdaArrayForTest_[GetParam().nLambda]);
143         fepvals.n_lambda = GetParam().nLambda;
144         return fepvals;
145     }
146
147 private:
148     //! a set of default lambda arrays for different lengths
149     std::vector<std::vector<double>> defaultLambdaArrayForTest_ = { {}, { 0.8 }, { 0.2, 0.8 }, { 0.2, 0.8, 0.8 } };
150 };
151
152 TEST_P(FreeEnergyParameterTest, CorrectLambdas)
153 {
154     EXPECT_THAT(GetParam().expectedLambdas,
155                 Pointwise(RealEq(defaultRealTolerance()),
156                           currentLambdas(GetParam().step, getFepVals(), GetParam().currentLambdaState)));
157 }
158
159 INSTANTIATE_TEST_CASE_P(WithParameters, FreeEnergyParameterTest, ::testing::ValuesIn(freeEnergyParameterSets));
160
161 } // namespace
162
163 } // namespace test
164
165 } // namespace gmx