Remove or update comments containing 'group scheme'
[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,2018,2019, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements classes in moduletest.h.
38  *
39  * \author Mark Abraham <mark.j.abraham@gmail.com>
40  * \ingroup module_mdrun_integration_tests
41  */
42 #include "gmxpre.h"
43
44 #include "moduletest.h"
45
46 #include "config.h"
47
48 #include <cstdio>
49
50 #include "gromacs/gmxana/gmx_ana.h"
51 #include "gromacs/gmxpreprocess/grompp.h"
52 #include "gromacs/hardware/detecthardware.h"
53 #include "gromacs/options/basicoptions.h"
54 #include "gromacs/options/ioptionscontainer.h"
55 #include "gromacs/tools/convert_tpr.h"
56 #include "gromacs/utility/basedefinitions.h"
57 #include "gromacs/utility/basenetwork.h"
58 #include "gromacs/utility/gmxmpi.h"
59 #include "gromacs/utility/textwriter.h"
60 #include "programs/mdrun/mdrun_main.h"
61
62 #include "testutils/cmdlinetest.h"
63 #include "testutils/mpitest.h"
64 #include "testutils/testfilemanager.h"
65 #include "testutils/testoptions.h"
66
67 namespace gmx
68 {
69 namespace test
70 {
71
72 /********************************************************************
73  * MdrunTestFixture
74  */
75
76 namespace
77 {
78
79 #if GMX_OPENMP || defined(DOXYGEN)
80 //! Number of OpenMP threads for child mdrun call.
81 int g_numOpenMPThreads = 1;
82 #endif
83 //! \cond
84 GMX_TEST_OPTIONS(MdrunTestOptions, options)
85 {
86     GMX_UNUSED_VALUE(options);
87 #if GMX_OPENMP
88     options->addOption(IntegerOption("ntomp").store(&g_numOpenMPThreads)
89                            .description("Number of OpenMP threads for child mdrun calls"));
90 #endif
91 }
92 //! \endcond
93
94 }       // namespace
95
96 SimulationRunner::SimulationRunner(TestFileManager *fileManager) :
97     fullPrecisionTrajectoryFileName_(fileManager->getTemporaryFilePath(".trr")),
98     mdpOutputFileName_(fileManager->getTemporaryFilePath("output.mdp")),
99     tprFileName_(fileManager->getTemporaryFilePath(".tpr")),
100     logFileName_(fileManager->getTemporaryFilePath(".log")),
101     edrFileName_(fileManager->getTemporaryFilePath(".edr")),
102     mtxFileName_(fileManager->getTemporaryFilePath(".mtx")),
103
104     nsteps_(-2),
105     fileManager_(*fileManager)
106 {
107 #if GMX_LIB_MPI
108     GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
109 #endif
110 }
111
112 // TODO The combination of defaulting to Verlet cut-off scheme, NVE,
113 // and verlet-buffer-tolerance = -1 gives a grompp error. If we keep
114 // things that way, this function should be renamed. For now,
115 // we use the Verlet scheme and hard-code a tolerance.
116 // TODO There is possible outstanding unexplained behaviour of mdp
117 // input parsing e.g. Redmine 2074, so this particular set of mdp
118 // contents is also tested with GetIrTest in gmxpreprocess-test.
119 void
120 SimulationRunner::useEmptyMdpFile()
121 {
122     useStringAsMdpFile("");
123 }
124
125 void
126 SimulationRunner::useStringAsMdpFile(const char *mdpString)
127 {
128     useStringAsMdpFile(std::string(mdpString));
129 }
130
131 void
132 SimulationRunner::useStringAsMdpFile(const std::string &mdpString)
133 {
134     mdpInputContents_ = mdpString;
135 }
136
137 void
138 SimulationRunner::useStringAsNdxFile(const char *ndxString)
139 {
140     gmx::TextWriter::writeFileFromString(ndxFileName_, ndxString);
141 }
142
143 void
144 SimulationRunner::useTopG96AndNdxFromDatabase(const std::string &name)
145 {
146     topFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".top");
147     groFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".g96");
148     ndxFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".ndx");
149 }
150
151 void
152 SimulationRunner::useTopGroAndNdxFromDatabase(const std::string &name)
153 {
154     topFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".top");
155     groFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".gro");
156     ndxFileName_ = gmx::test::TestFileManager::getInputFilePath(name + ".ndx");
157 }
158
159 void
160 SimulationRunner::useGroFromDatabase(const char *name)
161 {
162     groFileName_ = gmx::test::TestFileManager::getInputFilePath((std::string(name) + ".gro").c_str());
163 }
164
165 int
166 SimulationRunner::callGromppOnThisRank(const CommandLine &callerRef)
167 {
168     const std::string mdpInputFileName(fileManager_.getTemporaryFilePath("input.mdp"));
169     gmx::TextWriter::writeFileFromString(mdpInputFileName, mdpInputContents_);
170
171     CommandLine caller;
172     caller.append("grompp");
173     caller.merge(callerRef);
174     caller.addOption("-f", mdpInputFileName);
175     if (!ndxFileName_.empty())
176     {
177         caller.addOption("-n", ndxFileName_);
178     }
179     caller.addOption("-p", topFileName_);
180     caller.addOption("-c", groFileName_);
181     caller.addOption("-r", groFileName_);
182
183     caller.addOption("-po", mdpOutputFileName_);
184     caller.addOption("-o", tprFileName_);
185
186     return gmx_grompp(caller.argc(), caller.argv());
187 }
188
189 int
190 SimulationRunner::callGromppOnThisRank()
191 {
192     return callGromppOnThisRank(CommandLine());
193 }
194
195 int
196 SimulationRunner::callGrompp(const CommandLine &callerRef)
197 {
198     int returnValue = 0;
199 #if GMX_LIB_MPI
200     // When compiled with external MPI, we're trying to run mdrun with
201     // MPI, but we need to make sure that we only do grompp on one
202     // rank
203     if (0 == gmx_node_rank())
204 #endif
205     {
206         returnValue = callGromppOnThisRank(callerRef);
207     }
208 #if GMX_LIB_MPI
209     // Make sure rank zero has written the .tpr file before other
210     // ranks try to read it. Thread-MPI and serial do this just fine
211     // on their own.
212     MPI_Barrier(MPI_COMM_WORLD);
213 #endif
214     return returnValue;
215 }
216
217 int
218 SimulationRunner::callGrompp()
219 {
220     return callGrompp(CommandLine());
221 }
222
223 int
224 SimulationRunner::changeTprNsteps(int nsteps)
225 {
226     CommandLine caller;
227     caller.append("convert-tpr");
228     caller.addOption("-nsteps", nsteps);
229     // Because the operation is to change the .tpr, we replace the
230     // file. TODO Do we need to delete an automatic backup?
231     caller.addOption("-s", tprFileName_);
232     caller.addOption("-o", tprFileName_);
233
234     return gmx_convert_tpr(caller.argc(), caller.argv());
235 }
236
237 int
238 SimulationRunner::callNmeig()
239 {
240     /* Conforming to style guide by not passing a non-const reference
241        to this function. Passing a non-const reference might make it
242        easier to write code that incorrectly re-uses callerRef after
243        the call to this function. */
244
245     CommandLine caller;
246     caller.append("nmeig");
247     caller.addOption("-s", tprFileName_);
248     caller.addOption("-f", mtxFileName_);
249     // Ignore the overall translation and rotation in the
250     // first six eigenvectors.
251     caller.addOption("-first", "7");
252     // No need to check more than a number of output values.
253     caller.addOption("-last", "50");
254     caller.addOption("-xvg", "none");
255
256     return gmx_nmeig(caller.argc(), caller.argv());
257 }
258
259 int
260 SimulationRunner::callMdrun(const CommandLine &callerRef)
261 {
262     /* Conforming to style guide by not passing a non-const reference
263        to this function. Passing a non-const reference might make it
264        easier to write code that incorrectly re-uses callerRef after
265        the call to this function. */
266
267     CommandLine caller;
268     caller.append("mdrun");
269     caller.merge(callerRef);
270     caller.addOption("-s", tprFileName_);
271
272     caller.addOption("-g", logFileName_);
273     caller.addOption("-e", edrFileName_);
274     caller.addOption("-mtx", mtxFileName_);
275     caller.addOption("-o", fullPrecisionTrajectoryFileName_);
276     caller.addOption("-x", reducedPrecisionTrajectoryFileName_);
277
278     caller.addOption("-deffnm", fileManager_.getTemporaryFilePath("state"));
279
280     if (nsteps_ > -2)
281     {
282         caller.addOption("-nsteps", nsteps_);
283     }
284
285 #if GMX_THREAD_MPI
286     caller.addOption("-ntmpi", getNumberOfTestMpiRanks());
287 #endif
288
289 #if GMX_OPENMP
290     caller.addOption("-ntomp", g_numOpenMPThreads);
291 #endif
292
293     return gmx_mdrun(caller.argc(), caller.argv());
294 }
295
296 int
297 SimulationRunner::callMdrun()
298 {
299     return callMdrun(CommandLine());
300 }
301
302 // ====
303
304 MdrunTestFixtureBase::MdrunTestFixtureBase()
305 {
306 #if GMX_LIB_MPI
307     GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
308 #endif
309 }
310
311 MdrunTestFixtureBase::~MdrunTestFixtureBase()
312 {
313 }
314
315 // ====
316
317 MdrunTestFixture::MdrunTestFixture() : runner_(&fileManager_)
318 {
319 }
320
321 MdrunTestFixture::~MdrunTestFixture()
322 {
323 #if GMX_LIB_MPI
324     // fileManager_ should only clean up after all the ranks are done.
325     MPI_Barrier(MPI_COMM_WORLD);
326 #endif
327 }
328
329 } // namespace test
330 } // namespace gmx