Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / src / programs / mdrun / tests / moduletest.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
5  * Copyright (c) 2018,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 /*! \internal \file
37  * \brief
38  * Implements classes in moduletest.h.
39  *
40  * \author Mark Abraham <mark.j.abraham@gmail.com>
41  * \ingroup module_mdrun_integration_tests
42  */
43 #include "gmxpre.h"
44
45 #include "moduletest.h"
46
47 #include "config.h"
48
49 #include <cstdio>
50
51 #include <utility>
52
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"
66
67 #include "testutils/cmdlinetest.h"
68 #include "testutils/mpitest.h"
69 #include "testutils/testfilemanager.h"
70 #include "testutils/testoptions.h"
71
72 namespace gmx
73 {
74 namespace test
75 {
76
77 /********************************************************************
78  * MdrunTestFixture
79  */
80
81 namespace
82 {
83
84 #if GMX_OPENMP || defined(DOXYGEN)
85 //! Number of OpenMP threads for child mdrun call.
86 int g_numOpenMPThreads = 1;
87 #endif
88 //! \cond
89 GMX_TEST_OPTIONS(MdrunTestOptions, options)
90 {
91     GMX_UNUSED_VALUE(options);
92 #if GMX_OPENMP
93     options->addOption(
94             IntegerOption("ntomp").store(&g_numOpenMPThreads).description("Number of OpenMP threads for child mdrun calls"));
95 #endif
96 }
97 //! \endcond
98
99 } // namespace
100
101 SimulationRunner::SimulationRunner(TestFileManager* fileManager) :
102     fullPrecisionTrajectoryFileName_(fileManager->getTemporaryFilePath(".trr")),
103     mdpOutputFileName_(fileManager->getTemporaryFilePath("output.mdp")),
104     tprFileName_(fileManager->getTemporaryFilePath(".tpr")),
105     logFileName_(fileManager->getTemporaryFilePath(".log")),
106     edrFileName_(fileManager->getTemporaryFilePath(".edr")),
107     mtxFileName_(fileManager->getTemporaryFilePath(".mtx")),
108
109     nsteps_(-2),
110     mdpSource_(SimulationRunnerMdpSource::Undefined),
111     fileManager_(*fileManager)
112 {
113 #if GMX_LIB_MPI
114     GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
115
116     // It would be better to also detect this in a thread-MPI build,
117     // but there is no way to do that currently, and it is also not a
118     // problem for such a build. Any code based on such an invalid
119     // test fixture will be found in CI testing, however.
120     GMX_RELEASE_ASSERT(MdrunTestFixtureBase::communicator_ != MPI_COMM_NULL,
121                        "SimulationRunner may only be used from a test fixture that inherits from "
122                        "MdrunTestFixtureBase");
123 #endif
124 }
125
126 // TODO The combination of defaulting to Verlet cut-off scheme, NVE,
127 // and verlet-buffer-tolerance = -1 gives a grompp error. If we keep
128 // things that way, this function should be renamed. For now,
129 // we use the Verlet scheme and hard-code a tolerance.
130 // TODO There is possible outstanding unexplained behaviour of mdp
131 // input parsing e.g. Issue #2074, so this particular set of mdp
132 // contents is also tested with GetIrTest in gmxpreprocess-test.
133 void SimulationRunner::useEmptyMdpFile()
134 {
135     useStringAsMdpFile("");
136 }
137
138 void SimulationRunner::useStringAsMdpFile(const char* mdpString)
139 {
140     useStringAsMdpFile(std::string(mdpString));
141 }
142
143 void SimulationRunner::useStringAsMdpFile(const std::string& mdpString)
144 {
145     GMX_RELEASE_ASSERT(mdpSource_ != SimulationRunnerMdpSource::File,
146                        "Cannot mix .mdp file from database with options set via string.");
147     mdpSource_        = SimulationRunnerMdpSource::String;
148     mdpInputContents_ = mdpString;
149 }
150
151 void SimulationRunner::useStringAsNdxFile(const char* ndxString)
152 {
153     gmx::TextWriter::writeFileFromString(ndxFileName_, ndxString);
154 }
155
156 void SimulationRunner::useTopG96AndNdxFromDatabase(const std::string& name)
157 {
158     topFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".top");
159     groFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".g96");
160     ndxFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".ndx");
161 }
162
163 void SimulationRunner::useTopGroAndNdxFromDatabase(const std::string& name)
164 {
165     topFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".top");
166     groFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".gro");
167     ndxFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".ndx");
168 }
169
170 void SimulationRunner::useGroFromDatabase(const char* name)
171 {
172     groFileName_ = gmx::test::TestFileManager::getInputFilePath((std::string(name) + ".gro").c_str());
173 }
174
175 void SimulationRunner::useTopGroAndMdpFromFepTestDatabase(const std::string& name)
176 {
177     GMX_RELEASE_ASSERT(mdpSource_ != SimulationRunnerMdpSource::String,
178                        "Cannot mix .mdp file from database with options set via string.");
179     mdpSource_   = SimulationRunnerMdpSource::File;
180     topFileName_ = gmx::test::TestFileManager::getInputFilePath("freeenergy/" + name + "/topol.top");
181     groFileName_ = gmx::test::TestFileManager::getInputFilePath("freeenergy/" + name + "/conf.gro");
182     mdpFileName_ =
183             gmx::test::TestFileManager::getInputFilePath("freeenergy/" + name + "/grompp.mdp");
184 }
185
186 int SimulationRunner::callGromppOnThisRank(const CommandLine& callerRef)
187 {
188     std::string mdpInputFileName;
189     if (mdpSource_ == SimulationRunnerMdpSource::File)
190     {
191         mdpInputFileName = mdpFileName_;
192     }
193     else
194     {
195         mdpInputFileName = fileManager_.getTemporaryFilePath("input.mdp");
196         gmx::TextWriter::writeFileFromString(mdpInputFileName, mdpInputContents_);
197     }
198
199     CommandLine caller;
200     caller.append("grompp");
201     caller.merge(callerRef);
202     caller.addOption("-f", mdpInputFileName);
203     if (!ndxFileName_.empty())
204     {
205         caller.addOption("-n", ndxFileName_);
206     }
207     caller.addOption("-p", topFileName_);
208     caller.addOption("-c", groFileName_);
209     caller.addOption("-r", groFileName_);
210
211     caller.addOption("-po", mdpOutputFileName_);
212     caller.addOption("-o", tprFileName_);
213
214     return gmx_grompp(caller.argc(), caller.argv());
215 }
216
217 int SimulationRunner::callGromppOnThisRank()
218 {
219     return callGromppOnThisRank(CommandLine());
220 }
221
222 int SimulationRunner::callGrompp(const CommandLine& callerRef)
223 {
224     int returnValue = 0;
225 #if GMX_LIB_MPI
226     // When compiled with external MPI, we're trying to run mdrun with
227     // MPI, but we need to make sure that we only do grompp on one
228     // rank
229     if (0 == gmx_node_rank())
230 #endif
231     {
232         returnValue = callGromppOnThisRank(callerRef);
233     }
234 #if GMX_LIB_MPI
235     // Make sure rank zero has written the .tpr file before other
236     // ranks try to read it. Thread-MPI and serial do this just fine
237     // on their own.
238     MPI_Barrier(MdrunTestFixtureBase::communicator_);
239 #endif
240     return returnValue;
241 }
242
243 int SimulationRunner::callGrompp()
244 {
245     return callGrompp(CommandLine());
246 }
247
248 int SimulationRunner::changeTprNsteps(int nsteps)
249 {
250     CommandLine caller;
251     caller.append("convert-tpr");
252     caller.addOption("-nsteps", nsteps);
253     // Because the operation is to change the .tpr, we replace the
254     // file. TODO Do we need to delete an automatic backup?
255     caller.addOption("-s", tprFileName_);
256     caller.addOption("-o", tprFileName_);
257
258     return gmx::test::CommandLineTestHelper::runModuleFactory(&gmx::ConvertTprInfo::create, &caller);
259 }
260
261 int SimulationRunner::callNmeig()
262 {
263     /* Conforming to style guide by not passing a non-const reference
264        to this function. Passing a non-const reference might make it
265        easier to write code that incorrectly re-uses callerRef after
266        the call to this function. */
267
268     CommandLine caller;
269     caller.append("nmeig");
270     caller.addOption("-s", tprFileName_);
271     caller.addOption("-f", mtxFileName_);
272     // Ignore the overall translation and rotation in the
273     // first six eigenvectors.
274     caller.addOption("-first", "7");
275     // No need to check more than a number of output values.
276     caller.addOption("-last", "50");
277     caller.addOption("-xvg", "none");
278
279     return gmx_nmeig(caller.argc(), caller.argv());
280 }
281
282 int SimulationRunner::callMdrun(const CommandLine& callerRef)
283 {
284     /* Conforming to style guide by not passing a non-const reference
285        to this function. Passing a non-const reference might make it
286        easier to write code that incorrectly re-uses callerRef after
287        the call to this function. */
288
289     CommandLine caller;
290     caller.append("mdrun");
291     caller.merge(callerRef);
292     caller.addOption("-s", tprFileName_);
293
294     caller.addOption("-g", logFileName_);
295     caller.addOption("-e", edrFileName_);
296     caller.addOption("-mtx", mtxFileName_);
297     caller.addOption("-o", fullPrecisionTrajectoryFileName_);
298     caller.addOption("-x", reducedPrecisionTrajectoryFileName_);
299     if (!dhdlFileName_.empty())
300     {
301         caller.addOption("-dhdl", dhdlFileName_);
302     }
303
304     caller.addOption("-deffnm", fileManager_.getTemporaryFilePath("state"));
305
306     if (nsteps_ > -2)
307     {
308         caller.addOption("-nsteps", nsteps_);
309     }
310
311 #if GMX_THREAD_MPI
312     caller.addOption("-ntmpi", getNumberOfTestMpiRanks());
313 #endif
314
315 #if GMX_OPENMP
316     caller.addOption("-ntomp", g_numOpenMPThreads);
317 #endif
318
319     return gmx_mdrun(MdrunTestFixtureBase::communicator_,
320                      *MdrunTestFixtureBase::hwinfo_,
321                      caller.argc(),
322                      caller.argv());
323 }
324
325 int SimulationRunner::callMdrun()
326 {
327     return callMdrun(CommandLine());
328 }
329
330 // ====
331
332 // static
333 MPI_Comm MdrunTestFixtureBase::communicator_ = MPI_COMM_NULL;
334 // static
335 std::unique_ptr<gmx_hw_info_t> MdrunTestFixtureBase::hwinfo_;
336
337 // static
338 void MdrunTestFixtureBase::SetUpTestCase()
339 {
340     communicator_ = MPI_COMM_WORLD;
341     auto newHwinfo =
342             gmx_detect_hardware(PhysicalNodeCommunicator{ communicator_, gmx_physicalnode_id_hash() });
343     std::swap(hwinfo_, newHwinfo);
344 }
345
346 // static
347 void MdrunTestFixtureBase::TearDownTestCase()
348 {
349     hwinfo_.reset(nullptr);
350 }
351
352 MdrunTestFixtureBase::MdrunTestFixtureBase()
353 {
354 #if GMX_LIB_MPI
355     GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
356 #endif
357 }
358
359 MdrunTestFixtureBase::~MdrunTestFixtureBase() {}
360
361 // ====
362
363 MdrunTestFixture::MdrunTestFixture() : runner_(&fileManager_) {}
364
365 MdrunTestFixture::~MdrunTestFixture()
366 {
367 #if GMX_LIB_MPI
368     // fileManager_ should only clean up after all the ranks are done.
369     MPI_Barrier(MdrunTestFixtureBase::communicator_);
370 #endif
371 }
372
373 } // namespace test
374 } // namespace gmx