Prune change-management.rst and update links.
[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 "gromacs/gmxana/gmx_ana.h"
52 #include "gromacs/gmxpreprocess/grompp.h"
53 #include "gromacs/hardware/detecthardware.h"
54 #include "gromacs/options/basicoptions.h"
55 #include "gromacs/options/ioptionscontainer.h"
56 #include "gromacs/tools/convert_tpr.h"
57 #include "gromacs/utility/basedefinitions.h"
58 #include "gromacs/utility/basenetwork.h"
59 #include "gromacs/utility/gmxmpi.h"
60 #include "gromacs/utility/textwriter.h"
61 #include "programs/mdrun/mdrun_main.h"
62
63 #include "testutils/cmdlinetest.h"
64 #include "testutils/mpitest.h"
65 #include "testutils/testfilemanager.h"
66 #include "testutils/testoptions.h"
67
68 namespace gmx
69 {
70 namespace test
71 {
72
73 /********************************************************************
74  * MdrunTestFixture
75  */
76
77 namespace
78 {
79
80 #if GMX_OPENMP || defined(DOXYGEN)
81 //! Number of OpenMP threads for child mdrun call.
82 int g_numOpenMPThreads = 1;
83 #endif
84 //! \cond
85 GMX_TEST_OPTIONS(MdrunTestOptions, options)
86 {
87     GMX_UNUSED_VALUE(options);
88 #if GMX_OPENMP
89     options->addOption(
90             IntegerOption("ntomp").store(&g_numOpenMPThreads).description("Number of OpenMP threads for child mdrun calls"));
91 #endif
92 }
93 //! \endcond
94
95 } // namespace
96
97 SimulationRunner::SimulationRunner(TestFileManager* fileManager) :
98     fullPrecisionTrajectoryFileName_(fileManager->getTemporaryFilePath(".trr")),
99     mdpOutputFileName_(fileManager->getTemporaryFilePath("output.mdp")),
100     tprFileName_(fileManager->getTemporaryFilePath(".tpr")),
101     logFileName_(fileManager->getTemporaryFilePath(".log")),
102     edrFileName_(fileManager->getTemporaryFilePath(".edr")),
103     mtxFileName_(fileManager->getTemporaryFilePath(".mtx")),
104
105     nsteps_(-2),
106     fileManager_(*fileManager)
107 {
108 #if GMX_LIB_MPI
109     GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
110 #endif
111 }
112
113 // TODO The combination of defaulting to Verlet cut-off scheme, NVE,
114 // and verlet-buffer-tolerance = -1 gives a grompp error. If we keep
115 // things that way, this function should be renamed. For now,
116 // we use the Verlet scheme and hard-code a tolerance.
117 // TODO There is possible outstanding unexplained behaviour of mdp
118 // input parsing e.g. Issue #2074, so this particular set of mdp
119 // contents is also tested with GetIrTest in gmxpreprocess-test.
120 void SimulationRunner::useEmptyMdpFile()
121 {
122     useStringAsMdpFile("");
123 }
124
125 void SimulationRunner::useStringAsMdpFile(const char* mdpString)
126 {
127     useStringAsMdpFile(std::string(mdpString));
128 }
129
130 void SimulationRunner::useStringAsMdpFile(const std::string& mdpString)
131 {
132     mdpInputContents_ = mdpString;
133 }
134
135 void SimulationRunner::useStringAsNdxFile(const char* ndxString)
136 {
137     gmx::TextWriter::writeFileFromString(ndxFileName_, ndxString);
138 }
139
140 void SimulationRunner::useTopG96AndNdxFromDatabase(const std::string& name)
141 {
142     topFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".top");
143     groFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".g96");
144     ndxFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".ndx");
145 }
146
147 void SimulationRunner::useTopGroAndNdxFromDatabase(const std::string& name)
148 {
149     topFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".top");
150     groFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".gro");
151     ndxFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".ndx");
152 }
153
154 void SimulationRunner::useGroFromDatabase(const char* name)
155 {
156     groFileName_ = gmx::test::TestFileManager::getInputFilePath((std::string(name) + ".gro").c_str());
157 }
158
159 int SimulationRunner::callGromppOnThisRank(const CommandLine& callerRef)
160 {
161     const std::string mdpInputFileName(fileManager_.getTemporaryFilePath("input.mdp"));
162     gmx::TextWriter::writeFileFromString(mdpInputFileName, mdpInputContents_);
163
164     CommandLine caller;
165     caller.append("grompp");
166     caller.merge(callerRef);
167     caller.addOption("-f", mdpInputFileName);
168     if (!ndxFileName_.empty())
169     {
170         caller.addOption("-n", ndxFileName_);
171     }
172     caller.addOption("-p", topFileName_);
173     caller.addOption("-c", groFileName_);
174     caller.addOption("-r", groFileName_);
175
176     caller.addOption("-po", mdpOutputFileName_);
177     caller.addOption("-o", tprFileName_);
178
179     return gmx_grompp(caller.argc(), caller.argv());
180 }
181
182 int SimulationRunner::callGromppOnThisRank()
183 {
184     return callGromppOnThisRank(CommandLine());
185 }
186
187 int SimulationRunner::callGrompp(const CommandLine& callerRef)
188 {
189     int returnValue = 0;
190 #if GMX_LIB_MPI
191     // When compiled with external MPI, we're trying to run mdrun with
192     // MPI, but we need to make sure that we only do grompp on one
193     // rank
194     if (0 == gmx_node_rank())
195 #endif
196     {
197         returnValue = callGromppOnThisRank(callerRef);
198     }
199 #if GMX_LIB_MPI
200     // Make sure rank zero has written the .tpr file before other
201     // ranks try to read it. Thread-MPI and serial do this just fine
202     // on their own.
203     MPI_Barrier(MPI_COMM_WORLD);
204 #endif
205     return returnValue;
206 }
207
208 int SimulationRunner::callGrompp()
209 {
210     return callGrompp(CommandLine());
211 }
212
213 int SimulationRunner::changeTprNsteps(int nsteps)
214 {
215     CommandLine caller;
216     caller.append("convert-tpr");
217     caller.addOption("-nsteps", nsteps);
218     // Because the operation is to change the .tpr, we replace the
219     // file. TODO Do we need to delete an automatic backup?
220     caller.addOption("-s", tprFileName_);
221     caller.addOption("-o", tprFileName_);
222
223     return gmx_convert_tpr(caller.argc(), caller.argv());
224 }
225
226 int SimulationRunner::callNmeig()
227 {
228     /* Conforming to style guide by not passing a non-const reference
229        to this function. Passing a non-const reference might make it
230        easier to write code that incorrectly re-uses callerRef after
231        the call to this function. */
232
233     CommandLine caller;
234     caller.append("nmeig");
235     caller.addOption("-s", tprFileName_);
236     caller.addOption("-f", mtxFileName_);
237     // Ignore the overall translation and rotation in the
238     // first six eigenvectors.
239     caller.addOption("-first", "7");
240     // No need to check more than a number of output values.
241     caller.addOption("-last", "50");
242     caller.addOption("-xvg", "none");
243
244     return gmx_nmeig(caller.argc(), caller.argv());
245 }
246
247 int SimulationRunner::callMdrun(const CommandLine& callerRef)
248 {
249     /* Conforming to style guide by not passing a non-const reference
250        to this function. Passing a non-const reference might make it
251        easier to write code that incorrectly re-uses callerRef after
252        the call to this function. */
253
254     CommandLine caller;
255     caller.append("mdrun");
256     caller.merge(callerRef);
257     caller.addOption("-s", tprFileName_);
258
259     caller.addOption("-g", logFileName_);
260     caller.addOption("-e", edrFileName_);
261     caller.addOption("-mtx", mtxFileName_);
262     caller.addOption("-o", fullPrecisionTrajectoryFileName_);
263     caller.addOption("-x", reducedPrecisionTrajectoryFileName_);
264
265     caller.addOption("-deffnm", fileManager_.getTemporaryFilePath("state"));
266
267     if (nsteps_ > -2)
268     {
269         caller.addOption("-nsteps", nsteps_);
270     }
271
272 #if GMX_THREAD_MPI
273     caller.addOption("-ntmpi", getNumberOfTestMpiRanks());
274 #endif
275
276 #if GMX_OPENMP
277     caller.addOption("-ntomp", g_numOpenMPThreads);
278 #endif
279
280     return gmx_mdrun(caller.argc(), caller.argv());
281 }
282
283 int SimulationRunner::callMdrun()
284 {
285     return callMdrun(CommandLine());
286 }
287
288 // ====
289
290 MdrunTestFixtureBase::MdrunTestFixtureBase()
291 {
292 #if GMX_LIB_MPI
293     GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
294 #endif
295 }
296
297 MdrunTestFixtureBase::~MdrunTestFixtureBase() {}
298
299 // ====
300
301 MdrunTestFixture::MdrunTestFixture() : runner_(&fileManager_) {}
302
303 MdrunTestFixture::~MdrunTestFixture()
304 {
305 #if GMX_LIB_MPI
306     // fileManager_ should only clean up after all the ranks are done.
307     MPI_Barrier(MPI_COMM_WORLD);
308 #endif
309 }
310
311 } // namespace test
312 } // namespace gmx