Merge branch release-2021
[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,
118             baseTemperature + 0.0001 * rank_,
119             basePressure * std::pow(1.01, rank_),
120             /* Set things up so that the initial KE decreases with
121                increasing replica number, so that the (identical)
122                starting PE decreases on the first step more for the
123                replicas with higher number, which will tend to force
124                replica exchange to occur. */
125             std::max(baseTemperature - 10 * rank_, real(0)),
126             controlVariable);
127     runner->useStringAsMdpFile(mdpFileContents);
128 }
129
130 void MultiSimTest::runExitsNormallyTest()
131 {
132     if (size_ <= 1)
133     {
134         /* Can't test multi-sim without multiple ranks. */
135         return;
136     }
137
138     SimulationRunner runner(&fileManager_);
139     runner.useTopGroAndNdxFromDatabase("spc2");
140
141     const char* pcoupl = GetParam();
142     organizeMdpFile(&runner, pcoupl);
143     /* Call grompp on every rank - the standard callGrompp() only runs
144        grompp on rank 0. */
145     EXPECT_EQ(0, runner.callGromppOnThisRank());
146
147     ASSERT_EQ(0, runner.callMdrun(*mdrunCaller_));
148 }
149
150 void MultiSimTest::runMaxhTest()
151 {
152     if (size_ <= 1)
153     {
154         /* Can't test replica exchange without multiple ranks. */
155         return;
156     }
157
158     SimulationRunner runner(&fileManager_);
159     runner.useTopGroAndNdxFromDatabase("spc2");
160
161     TerminationHelper helper(&fileManager_, mdrunCaller_.get(), &runner);
162     // Make sure -maxh has a chance to propagate
163     int numSteps = 100;
164     organizeMdpFile(&runner, "pcoupl = no", numSteps);
165     /* Call grompp on every rank - the standard callGrompp() only runs
166        grompp on rank 0. */
167     EXPECT_EQ(0, runner.callGromppOnThisRank());
168
169     helper.runFirstMdrun(runner.cptFileName_);
170     helper.runSecondMdrun();
171 }
172
173 } // namespace test
174 } // namespace gmx