Merge branch origin/release-2020 into master
[alexxy/gromacs.git] / src / programs / mdrun / tests / multisimtest.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2013,2014,2015,2016,2018 by the GROMACS development team.
5  * Copyright (c) 2019,2020, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36
37 /*! \internal \file
38  * \brief
39  * Tests for the mdrun multi-simulation functionality
40  *
41  * \author Mark Abraham <mark.j.abraham@gmail.com>
42  * \ingroup module_mdrun_integration_tests
43  */
44 #include "gmxpre.h"
45
46 #include "multisimtest.h"
47
48 #include <cmath>
49
50 #include <algorithm>
51 #include <string>
52
53 #include <gtest/gtest.h>
54
55 #include "gromacs/utility/basenetwork.h"
56 #include "gromacs/utility/path.h"
57 #include "gromacs/utility/real.h"
58 #include "gromacs/utility/stringutil.h"
59
60 #include "testutils/cmdlinetest.h"
61
62 #include "moduletest.h"
63 #include "terminationhelper.h"
64
65 namespace gmx
66 {
67 namespace test
68 {
69
70 MultiSimTest::MultiSimTest() :
71     size_(gmx_node_num()),
72     rank_(gmx_node_rank()),
73     mdrunCaller_(new CommandLine)
74
75 {
76     const char* directoryNameFormat = "sim_%d";
77
78     // Modify the file manager to have a temporary directory unique to
79     // each simulation. No need to have a mutex on this, nobody else
80     // can access the fileManager_ yet because we only just
81     // constructed it.
82     std::string originalTempDirectory = fileManager_.getOutputTempDirectory();
83     std::string newTempDirectory =
84             Path::join(originalTempDirectory, formatString(directoryNameFormat, rank_));
85     Directory::create(newTempDirectory);
86     fileManager_.setOutputTempDirectory(newTempDirectory);
87
88     mdrunCaller_->append("mdrun");
89     mdrunCaller_->addOption("-multidir");
90     for (int i = 0; i != size_; ++i)
91     {
92         mdrunCaller_->append(Path::join(originalTempDirectory, formatString(directoryNameFormat, i)));
93     }
94 }
95
96 void MultiSimTest::organizeMdpFile(SimulationRunner* runner, const char* controlVariable, int numSteps)
97 {
98     const real  baseTemperature = 298;
99     const real  basePressure    = 1;
100     std::string mdpFileContents = formatString(
101             "nsteps = %d\n"
102             "nstlog = 1\n"
103             "nstcalcenergy = 1\n"
104             "tcoupl = v-rescale\n"
105             "tc-grps = System\n"
106             "tau-t = 1\n"
107             "ref-t = %f\n"
108             // pressure coupling (if active)
109             "tau-p = 1\n"
110             "ref-p = %f\n"
111             "compressibility = 4.5e-5\n"
112             // velocity generation
113             "gen-vel = yes\n"
114             "gen-temp = %f\n"
115             // control variable specification
116             "%s\n",
117             numSteps, baseTemperature + 0.0001 * rank_, basePressure * std::pow(1.01, rank_),
118             /* Set things up so that the initial KE decreases with
119                increasing replica number, so that the (identical)
120                starting PE decreases on the first step more for the
121                replicas with higher number, which will tend to force
122                replica exchange to occur. */
123             std::max(baseTemperature - 10 * rank_, real(0)), controlVariable);
124     runner->useStringAsMdpFile(mdpFileContents);
125 }
126
127 void MultiSimTest::runExitsNormallyTest()
128 {
129     if (size_ <= 1)
130     {
131         /* Can't test multi-sim without multiple ranks. */
132         return;
133     }
134
135     SimulationRunner runner(&fileManager_);
136     runner.useTopGroAndNdxFromDatabase("spc2");
137
138     const char* pcoupl = GetParam();
139     organizeMdpFile(&runner, pcoupl);
140     /* Call grompp on every rank - the standard callGrompp() only runs
141        grompp on rank 0. */
142     EXPECT_EQ(0, runner.callGromppOnThisRank());
143
144     ASSERT_EQ(0, runner.callMdrun(*mdrunCaller_));
145 }
146
147 void MultiSimTest::runMaxhTest()
148 {
149     if (size_ <= 1)
150     {
151         /* Can't test replica exchange without multiple ranks. */
152         return;
153     }
154
155     SimulationRunner runner(&fileManager_);
156     runner.useTopGroAndNdxFromDatabase("spc2");
157
158     TerminationHelper helper(&fileManager_, mdrunCaller_.get(), &runner);
159     // Make sure -maxh has a chance to propagate
160     int numSteps = 100;
161     organizeMdpFile(&runner, "pcoupl = no", numSteps);
162     /* Call grompp on every rank - the standard callGrompp() only runs
163        grompp on rank 0. */
164     EXPECT_EQ(0, runner.callGromppOnThisRank());
165
166     helper.runFirstMdrun(runner.cptFileName_);
167     helper.runSecondMdrun();
168 }
169
170 } // namespace test
171 } // namespace gmx