2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
5 * Copyright (c) 2018,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.
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.
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.
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.
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.
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.
38 * Implements classes in moduletest.h.
40 * \author Mark Abraham <mark.j.abraham@gmail.com>
41 * \ingroup module_mdrun_integration_tests
45 #include "moduletest.h"
53 #include "gromacs/gmxana/gmx_ana.h"
54 #include "gromacs/gmxpreprocess/grompp.h"
55 #include "gromacs/hardware/detecthardware.h"
56 #include "gromacs/hardware/hw_info.h"
57 #include "gromacs/options/basicoptions.h"
58 #include "gromacs/options/ioptionscontainer.h"
59 #include "gromacs/tools/convert_tpr.h"
60 #include "gromacs/utility/basedefinitions.h"
61 #include "gromacs/utility/basenetwork.h"
62 #include "gromacs/utility/gmxmpi.h"
63 #include "gromacs/utility/physicalnodecommunicator.h"
64 #include "gromacs/utility/textwriter.h"
65 #include "programs/mdrun/mdrun_main.h"
67 #include "testutils/cmdlinetest.h"
68 #include "testutils/mpitest.h"
69 #include "testutils/testfilemanager.h"
70 #include "testutils/testoptions.h"
77 /********************************************************************
84 #if GMX_OPENMP || defined(DOXYGEN)
85 //! Number of OpenMP threads for child mdrun call.
86 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
87 int g_numOpenMPThreads = 1;
90 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
91 GMX_TEST_OPTIONS(MdrunTestOptions, options)
93 GMX_UNUSED_VALUE(options);
96 IntegerOption("ntomp").store(&g_numOpenMPThreads).description("Number of OpenMP threads for child mdrun calls"));
103 SimulationRunner::SimulationRunner(TestFileManager* fileManager) :
104 fullPrecisionTrajectoryFileName_(fileManager->getTemporaryFilePath(".trr")),
105 mdpOutputFileName_(fileManager->getTemporaryFilePath("output.mdp")),
106 tprFileName_(fileManager->getTemporaryFilePath(".tpr")),
107 logFileName_(fileManager->getTemporaryFilePath(".log")),
108 edrFileName_(fileManager->getTemporaryFilePath(".edr")),
109 mtxFileName_(fileManager->getTemporaryFilePath(".mtx")),
112 mdpSource_(SimulationRunnerMdpSource::Undefined),
113 fileManager_(*fileManager)
116 GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
118 // It would be better to also detect this in a thread-MPI build,
119 // but there is no way to do that currently, and it is also not a
120 // problem for such a build. Any code based on such an invalid
121 // test fixture will be found in CI testing, however.
122 GMX_RELEASE_ASSERT(MdrunTestFixtureBase::communicator_ != MPI_COMM_NULL,
123 "SimulationRunner may only be used from a test fixture that inherits from "
124 "MdrunTestFixtureBase");
128 // TODO The combination of defaulting to Verlet cut-off scheme, NVE,
129 // and verlet-buffer-tolerance = -1 gives a grompp error. If we keep
130 // things that way, this function should be renamed. For now,
131 // we use the Verlet scheme and hard-code a tolerance.
132 // TODO There is possible outstanding unexplained behaviour of mdp
133 // input parsing e.g. Issue #2074, so this particular set of mdp
134 // contents is also tested with GetIrTest in gmxpreprocess-test.
135 void SimulationRunner::useEmptyMdpFile()
137 useStringAsMdpFile("");
140 void SimulationRunner::useStringAsMdpFile(const char* mdpString)
142 useStringAsMdpFile(std::string(mdpString));
145 void SimulationRunner::useStringAsMdpFile(const std::string& mdpString)
147 GMX_RELEASE_ASSERT(mdpSource_ != SimulationRunnerMdpSource::File,
148 "Cannot mix .mdp file from database with options set via string.");
149 mdpSource_ = SimulationRunnerMdpSource::String;
150 mdpInputContents_ = mdpString;
153 void SimulationRunner::useStringAsNdxFile(const char* ndxString) const
155 gmx::TextWriter::writeFileFromString(ndxFileName_, ndxString);
158 void SimulationRunner::useTopG96AndNdxFromDatabase(const std::string& name)
160 topFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".top");
161 groFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".g96");
162 ndxFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".ndx");
165 void SimulationRunner::useTopGroAndNdxFromDatabase(const std::string& name)
167 topFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".top");
168 groFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".gro");
169 ndxFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".ndx");
172 void SimulationRunner::useGroFromDatabase(const char* name)
174 groFileName_ = gmx::test::TestFileManager::getInputFilePath((std::string(name) + ".gro").c_str());
177 void SimulationRunner::useNdxFromDatabase(const std::string& name)
179 ndxFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".ndx");
182 void SimulationRunner::useTopGroAndMdpFromFepTestDatabase(const std::string& name)
184 GMX_RELEASE_ASSERT(mdpSource_ != SimulationRunnerMdpSource::String,
185 "Cannot mix .mdp file from database with options set via string.");
186 mdpSource_ = SimulationRunnerMdpSource::File;
187 topFileName_ = gmx::test::TestFileManager::getInputFilePath("freeenergy/" + name + "/topol.top");
188 groFileName_ = gmx::test::TestFileManager::getInputFilePath("freeenergy/" + name + "/conf.gro");
190 gmx::test::TestFileManager::getInputFilePath("freeenergy/" + name + "/grompp.mdp");
193 int SimulationRunner::callGromppOnThisRank(const CommandLine& callerRef)
195 std::string mdpInputFileName;
196 if (mdpSource_ == SimulationRunnerMdpSource::File)
198 mdpInputFileName = mdpFileName_;
202 mdpInputFileName = fileManager_.getTemporaryFilePath("input.mdp");
203 gmx::TextWriter::writeFileFromString(mdpInputFileName, mdpInputContents_);
207 caller.append("grompp");
208 caller.merge(callerRef);
209 caller.addOption("-f", mdpInputFileName);
210 if (!ndxFileName_.empty())
212 caller.addOption("-n", ndxFileName_);
214 caller.addOption("-p", topFileName_);
215 caller.addOption("-c", groFileName_);
216 caller.addOption("-r", groFileName_);
218 caller.addOption("-po", mdpOutputFileName_);
219 caller.addOption("-o", tprFileName_);
221 return gmx_grompp(caller.argc(), caller.argv());
224 int SimulationRunner::callGromppOnThisRank()
226 return callGromppOnThisRank(CommandLine());
229 int SimulationRunner::callGrompp(const CommandLine& callerRef)
233 // When compiled with external MPI, we're trying to run mdrun with
234 // MPI, but we need to make sure that we only do grompp on one
236 if (0 == gmx_node_rank())
239 returnValue = callGromppOnThisRank(callerRef);
242 // Make sure rank zero has written the .tpr file before other
243 // ranks try to read it. Thread-MPI and serial do this just fine
245 MPI_Barrier(MdrunTestFixtureBase::communicator_);
250 int SimulationRunner::callGrompp()
252 return callGrompp(CommandLine());
255 int SimulationRunner::changeTprNsteps(int nsteps) const
258 caller.append("convert-tpr");
259 caller.addOption("-nsteps", nsteps);
260 // Because the operation is to change the .tpr, we replace the
261 // file. TODO Do we need to delete an automatic backup?
262 caller.addOption("-s", tprFileName_);
263 caller.addOption("-o", tprFileName_);
265 return gmx::test::CommandLineTestHelper::runModuleFactory(&gmx::ConvertTprInfo::create, &caller);
268 int SimulationRunner::callNmeig() const
270 /* Conforming to style guide by not passing a non-const reference
271 to this function. Passing a non-const reference might make it
272 easier to write code that incorrectly re-uses callerRef after
273 the call to this function. */
276 caller.append("nmeig");
277 caller.addOption("-s", tprFileName_);
278 caller.addOption("-f", mtxFileName_);
279 // Ignore the overall translation and rotation in the
280 // first six eigenvectors.
281 caller.addOption("-first", "7");
282 // No need to check more than a number of output values.
283 caller.addOption("-last", "50");
284 caller.addOption("-xvg", "none");
286 return gmx_nmeig(caller.argc(), caller.argv());
289 int SimulationRunner::callMdrun(const CommandLine& callerRef)
291 /* Conforming to style guide by not passing a non-const reference
292 to this function. Passing a non-const reference might make it
293 easier to write code that incorrectly re-uses callerRef after
294 the call to this function. */
297 caller.append("mdrun");
298 caller.merge(callerRef);
299 caller.addOption("-s", tprFileName_);
301 caller.addOption("-g", logFileName_);
302 caller.addOption("-e", edrFileName_);
303 caller.addOption("-mtx", mtxFileName_);
304 caller.addOption("-o", fullPrecisionTrajectoryFileName_);
305 caller.addOption("-x", reducedPrecisionTrajectoryFileName_);
306 if (!dhdlFileName_.empty())
308 caller.addOption("-dhdl", dhdlFileName_);
311 caller.addOption("-deffnm", fileManager_.getTemporaryFilePath("state"));
315 caller.addOption("-nsteps", nsteps_);
319 caller.addOption("-ntmpi", getNumberOfTestMpiRanks());
323 caller.addOption("-ntomp", g_numOpenMPThreads);
326 return gmx_mdrun(MdrunTestFixtureBase::communicator_,
327 *MdrunTestFixtureBase::hwinfo_,
332 int SimulationRunner::callMdrun()
334 return callMdrun(CommandLine());
340 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
341 MPI_Comm MdrunTestFixtureBase::communicator_ = MPI_COMM_NULL;
343 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
344 std::unique_ptr<gmx_hw_info_t> MdrunTestFixtureBase::hwinfo_;
347 void MdrunTestFixtureBase::SetUpTestCase()
349 communicator_ = MPI_COMM_WORLD;
351 gmx_detect_hardware(PhysicalNodeCommunicator{ communicator_, gmx_physicalnode_id_hash() });
352 std::swap(hwinfo_, newHwinfo);
356 void MdrunTestFixtureBase::TearDownTestCase()
358 hwinfo_.reset(nullptr);
361 MdrunTestFixtureBase::MdrunTestFixtureBase()
364 GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
368 MdrunTestFixtureBase::~MdrunTestFixtureBase() {}
372 MdrunTestFixture::MdrunTestFixture() : runner_(&fileManager_) {}
374 MdrunTestFixture::~MdrunTestFixture()
377 // fileManager_ should only clean up after all the ranks are done.
378 MPI_Barrier(MdrunTestFixtureBase::communicator_);