ea8a7b20c64e4b2f7ac1f6a8768c702b8f2797ea
[alexxy/gromacs.git] / src / programs / mdrun / tests / replicaexchange.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2013,2014, 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
36 /*! \internal \file
37  * \brief
38  * Tests for the mdrun replica exchange functionality
39  *
40  * \author Mark Abraham <mark.j.abraham@gmail.com>
41  * \ingroup module_mdrun
42  */
43 #include "gmxpre.h"
44
45 #include "moduletest.h"
46
47 #include <math.h>
48
49 #include <algorithm>
50
51 #include <gtest/gtest.h>
52
53 #include "gromacs/utility/basenetwork.h"
54 #include "gromacs/utility/path.h"
55 #include "gromacs/utility/real.h"
56 #include "gromacs/utility/stringutil.h"
57
58 #include "programs/mdrun/mdrun_main.h"
59
60 #include "testutils/cmdlinetest.h"
61
62 #include "config.h"
63
64 namespace
65 {
66
67 /*! \brief
68  * Test fixture for replica exchange
69  */
70 class ReplicaExchangeTest : public gmx::test::ParameterizedMdrunTestFixture
71 {
72     public:
73         //! Constructor
74         ReplicaExchangeTest() : size(gmx_node_num()),
75                                 rank(gmx_node_rank())
76         {
77             mdrunCaller.append("mdrun_mpi");
78         }
79
80         /*! \brief Organize the -multidir directories for the test.
81          *
82          * These are created inside the temporary directory for the
83          * test case, and added to the eventual mdrun command
84          * line. The temporary directory for the call to grompp by
85          * this rank is set to the appropriate -multidir directory, so
86          * the grompp output files go to the right place. */
87         void organizeMultidir()
88         {
89             mdrunCaller.append("-multidir");
90
91             std::string futureTestTempDirectory;
92             for (int i = 0; i != size; ++i)
93             {
94                 std::string replicaTempDirectory =
95                     gmx::formatString("%s/multidir_%d",
96                                       fileManager_.getOutputTempDirectory(), i);
97                 mdrunCaller.append(replicaTempDirectory);
98
99                 if (rank == i)
100                 {
101                     gmx::Directory::create(replicaTempDirectory);
102                     futureTestTempDirectory = std::string(replicaTempDirectory);
103                 }
104             }
105             fileManager_.setOutputTempDirectory(futureTestTempDirectory);
106
107             /* Prepare grompp output filenames inside the new
108                temporary directory */
109             mdpInputFileName  = fileManager_.getTemporaryFilePath("input.mdp");
110             mdpOutputFileName = fileManager_.getTemporaryFilePath("output.mdp");
111             tprFileName       = fileManager_.getTemporaryFilePath(".tpr");
112
113             mdrunCaller.addOption("-deffnm", fileManager_.getTestSpecificFileNameRoot());
114         }
115
116         /*! \brief Organize the .mdp file for this rank
117          *
118          * \param controlVariable Allows parameterization to work with
119          * T, P or (later) lamda as the control variable, by passing a
120          * string with "mdp-param = value" such that different paths
121          * in init_replica_exchange() are followed.
122          */
123         void organizeMdpFile(const char *controlVariable)
124         {
125             const real  baseTemperature = 298;
126             const real  basePressure    = 1;
127             std::string mdpFileContents =
128                 gmx::formatString
129                     ("nsteps = 2\n"
130                     "nstlog = 1\n"
131                     "nstcalcenergy = 1\n"
132                     "tcoupl = v-rescale\n"
133                     "tc-grps = System\n"
134                     "tau-t = 1\n"
135                     "ref-t = %f\n"
136                     // pressure coupling (if active)
137                     "tau-p = 1\n"
138                     "ref-p = %f\n"
139                     "compressibility = 4.5e-5\n"
140                     // velocity generation
141                     "gen-vel = yes\n"
142                     "gen-temp = %f\n"
143                     // control variable specification
144                     "%s\n",
145                     baseTemperature + 0.0001*rank,
146                     basePressure * pow(1.01, rank),
147                     /* Set things up so that the initial KE decreases
148                        with increasing replica number, so that the
149                        (identical) starting PE decreases on the first
150                        step more for the replicas with higher number,
151                        which will tend to force replica exchange to
152                        occur. */
153                     std::max(baseTemperature - 10 * rank, real(0)),
154                     controlVariable);
155             useStringAsMdpFile(mdpFileContents);
156         }
157
158         //! MPI process set size
159         int                    size;
160         //! MPI rank of this process
161         int                    rank;
162         //! Object for building the mdrun command line
163         gmx::test::CommandLine mdrunCaller;
164 };
165
166 /* This test ensures mdrun can run NVT REMD under the supported
167  * conditions.
168  *
169  * TODO Preferably, we could test that mdrun correctly refuses to run
170  * replica exchange unless under real MPI with more than one rank
171  * available. However, those cases trigger an error that is currently
172  * fatal to mdrun and also to the test binary. So, in the meantime we
173  * must not test those cases. This is done via disabling the test, so
174  * that there is a reminder that it is disabled. There's no elegant
175  * way to conditionally disable a test. */
176 TEST_P(ReplicaExchangeTest, ExitsNormally)
177 {
178     if (size <= 1)
179     {
180         /* Can't test replica exchange without multiple ranks. */
181         return;
182     }
183
184     organizeMultidir();
185     const char *pcoupl = GetParam();
186     organizeMdpFile(pcoupl);
187     useTopGroAndNdxFromDatabase("spc2");
188     /* Call grompp on every rank - the standard callGrompp() only runs
189        grompp on rank 0. */
190     EXPECT_EQ(0, callGromppOnThisRank());
191
192     mdrunCaller.addOption("-replex", 1);
193     ASSERT_EQ(0, gmx_mdrun(mdrunCaller.argc(), mdrunCaller.argv()));
194 }
195
196 #ifdef GMX_LIB_MPI
197 INSTANTIATE_TEST_CASE_P(WithDifferentControlVariables, ReplicaExchangeTest,
198                             ::testing::Values("pcoupl = no", "pcoupl = Berendsen"));
199 #else
200 INSTANTIATE_TEST_CASE_P(DISABLED_WithDifferentControlVariables, ReplicaExchangeTest,
201                             ::testing::Values("pcoupl = no", "pcoupl = Berendsen"));
202 #endif
203
204 } // namespace