Increase multsim test flexibility, test multisim + DD
[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,2021, 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/mdtypes/md_enums.h"
56 #include "gromacs/utility/basenetwork.h"
57 #include "gromacs/utility/gmxassert.h"
58 #include "gromacs/utility/path.h"
59 #include "gromacs/utility/real.h"
60 #include "gromacs/utility/stringutil.h"
61
62 #include "testutils/cmdlinetest.h"
63
64 #include "moduletest.h"
65 #include "terminationhelper.h"
66
67 namespace gmx
68 {
69 namespace test
70 {
71
72 MultiSimTest::MultiSimTest() :
73     size_(gmx_node_num()),
74     rank_(gmx_node_rank()),
75     numRanksPerSimulation_(std::get<0>(GetParam())),
76     simulationNumber_(rank_ / numRanksPerSimulation_),
77     mdrunCaller_(new CommandLine)
78
79 {
80     // Zero or less ranks doesn't make sense
81     GMX_RELEASE_ASSERT(numRanksPerSimulation_ > 0, "Invalid number of ranks per simulation.");
82
83     const char* directoryNameFormat = "sim_%d";
84
85     // Modify the file manager to have a temporary directory unique to
86     // each simulation. No need to have a mutex on this, nobody else
87     // can access the fileManager_ yet because we only just
88     // constructed it.
89     std::string originalTempDirectory = fileManager_.getOutputTempDirectory();
90     std::string newTempDirectory =
91             Path::join(originalTempDirectory, formatString(directoryNameFormat, simulationNumber_));
92     if (rank_ % numRanksPerSimulation_ == 0)
93     {
94         // Only one rank per simulation creates directory
95         Directory::create(newTempDirectory);
96     }
97 #if GMX_LIB_MPI
98     // Make sure directories got created.
99     MPI_Barrier(MdrunTestFixtureBase::communicator_);
100 #endif
101     fileManager_.setOutputTempDirectory(newTempDirectory);
102
103     mdrunCaller_->append("mdrun");
104     mdrunCaller_->addOption("-multidir");
105     for (int i = 0; i < size_ / numRanksPerSimulation_; ++i)
106     {
107         mdrunCaller_->append(Path::join(originalTempDirectory, formatString(directoryNameFormat, i)));
108     }
109 }
110
111 bool MultiSimTest::mpiSetupValid() const
112 {
113     // Single simulation case is not implemented in multi-sim
114     const bool haveAtLeastTwoSimulations = ((size_ / numRanksPerSimulation_) >= 2);
115     // Mdrun will throw error if simulations don't have identical number of ranks
116     const bool simulationsHaveIdenticalRankNumber = ((size_ % numRanksPerSimulation_) == 0);
117
118     return (haveAtLeastTwoSimulations && simulationsHaveIdenticalRankNumber);
119 }
120
121 void MultiSimTest::organizeMdpFile(SimulationRunner*    runner,
122                                    IntegrationAlgorithm integrator,
123                                    TemperatureCoupling  tcoupl,
124                                    PressureCoupling     pcoupl,
125                                    int                  numSteps) const
126 {
127     GMX_RELEASE_ASSERT(mpiSetupValid(), "Creating the mdp file without valid MPI setup is useless.");
128     const real  baseTemperature = 298;
129     const real  basePressure    = 1;
130     std::string mdpFileContents = formatString(
131             "integrator = %s\n"
132             "tcoupl = %s\n"
133             "pcoupl = %s\n"
134             "nsteps = %d\n"
135             "nstlog = 1\n"
136             "nstcalcenergy = 1\n"
137             "tc-grps = System\n"
138             "tau-t = 1\n"
139             "ref-t = %f\n"
140             // pressure coupling (if active)
141             "tau-p = 1\n"
142             "ref-p = %f\n"
143             "compressibility = 4.5e-5\n"
144             // velocity generation
145             "gen-vel = yes\n"
146             "gen-temp = %f\n",
147             enumValueToString(integrator),
148             enumValueToString(tcoupl),
149             enumValueToString(pcoupl),
150             numSteps,
151             baseTemperature + 0.0001 * rank_,
152             basePressure * std::pow(1.01, rank_),
153             /* Set things up so that the initial KE decreases with
154                increasing replica number, so that the (identical)
155                starting PE decreases on the first step more for the
156                replicas with higher number, which will tend to force
157                replica exchange to occur. */
158             std::max(baseTemperature - 10 * rank_, real(0)));
159     runner->useStringAsMdpFile(mdpFileContents);
160 }
161
162 void MultiSimTest::runGrompp(SimulationRunner* runner, int numSteps) const
163 {
164     // Call grompp once per simulation
165     if (rank_ % numRanksPerSimulation_ == 0)
166     {
167         const auto& simulator = std::get<1>(GetParam());
168         const auto& tcoupl    = std::get<2>(GetParam());
169         const auto& pcoupl    = std::get<3>(GetParam());
170         organizeMdpFile(runner, simulator, tcoupl, pcoupl, numSteps);
171         EXPECT_EQ(0, runner->callGromppOnThisRank());
172     }
173
174 #if GMX_LIB_MPI
175     // Make sure simulation masters have written the .tpr file before other ranks try to read it.
176     MPI_Barrier(MdrunTestFixtureBase::communicator_);
177 #endif
178 }
179
180 void MultiSimTest::runExitsNormallyTest()
181 {
182     if (!mpiSetupValid())
183     {
184         // Can't test multi-sim without multiple simulations
185         return;
186     }
187
188     SimulationRunner runner(&fileManager_);
189     runner.useTopGroAndNdxFromDatabase("spc2");
190
191     runGrompp(&runner);
192
193     ASSERT_EQ(0, runner.callMdrun(*mdrunCaller_));
194 }
195
196 void MultiSimTest::runMaxhTest()
197 {
198     if (!mpiSetupValid())
199     {
200         // Can't test multi-sim without multiple simulations
201         return;
202     }
203
204     SimulationRunner runner(&fileManager_);
205     runner.useTopGroAndNdxFromDatabase("spc2");
206
207     TerminationHelper helper(&fileManager_, mdrunCaller_.get(), &runner);
208     // Make sure -maxh has a chance to propagate
209     int numSteps = 100;
210     runGrompp(&runner, numSteps);
211
212     helper.runFirstMdrun(runner.cptFileName_);
213     helper.runSecondMdrun();
214 }
215
216 } // namespace test
217 } // namespace gmx