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