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