Remove gmx::File (except for File::exists())
[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, 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 "gromacs/gmxpreprocess/grompp.h"
49 #include "gromacs/options/basicoptions.h"
50 #include "gromacs/options/options.h"
51 #include "gromacs/utility/basedefinitions.h"
52 #include "gromacs/utility/basenetwork.h"
53 #include "gromacs/utility/gmxmpi.h"
54 #include "gromacs/utility/textwriter.h"
55 #include "programs/mdrun/mdrun_main.h"
56
57 #include "testutils/cmdlinetest.h"
58 #include "testutils/integrationtests.h"
59 #include "testutils/testoptions.h"
60
61 namespace gmx
62 {
63 namespace test
64 {
65
66 /********************************************************************
67  * MdrunTestFixture
68  */
69
70 namespace
71 {
72
73 #if defined(GMX_THREAD_MPI) || defined(DOXYGEN)
74 //! Number of tMPI threads for child mdrun call.
75 int g_numThreads = 1;
76 #endif
77 #if defined(GMX_OPENMP) || defined(DOXYGEN)
78 //! Number of OpenMP threads for child mdrun call.
79 int g_numOpenMPThreads = 1;
80 #endif
81 //! \cond
82 GMX_TEST_OPTIONS(MdrunTestOptions, options)
83 {
84     GMX_UNUSED_VALUE(options);
85 #ifdef GMX_THREAD_MPI
86     options->addOption(IntegerOption("nt").store(&g_numThreads)
87                            .description("Number of thread-MPI threads/ranks for child mdrun calls"));
88 #endif
89 #ifdef GMX_OPENMP
90     options->addOption(IntegerOption("nt_omp").store(&g_numOpenMPThreads)
91                            .description("Number of OpenMP threads for child mdrun calls"));
92 #endif
93 }
94 //! \endcond
95
96 }
97
98 SimulationRunner::SimulationRunner(IntegrationTestFixture *fixture) :
99     fixture_(fixture),
100     topFileName_(),
101     groFileName_(),
102     fullPrecisionTrajectoryFileName_(),
103     ndxFileName_(),
104     mdpInputFileName_(fixture_->fileManager_.getTemporaryFilePath("input.mdp")),
105     mdpOutputFileName_(fixture_->fileManager_.getTemporaryFilePath("output.mdp")),
106     tprFileName_(fixture_->fileManager_.getTemporaryFilePath(".tpr")),
107     logFileName_(fixture_->fileManager_.getTemporaryFilePath(".log")),
108     edrFileName_(fixture_->fileManager_.getTemporaryFilePath(".edr")),
109     nsteps_(-2)
110 {
111 #ifdef GMX_LIB_MPI
112     GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
113 #endif
114 }
115
116 // TODO The combination of defaulting to Verlet cut-off scheme, NVE,
117 // and verlet-buffer-tolerance = -1 gives a grompp error. If we keep
118 // things that way, this function should be renamed. For now,
119 // force the use of the group scheme.
120 void
121 SimulationRunner::useEmptyMdpFile()
122 {
123     // TODO When removing the group scheme, update actual and potential users of useEmptyMdpFile
124     useStringAsMdpFile("cutoff-scheme = Group\n");
125 }
126
127 void
128 SimulationRunner::useStringAsMdpFile(const char *mdpString)
129 {
130     useStringAsMdpFile(std::string(mdpString));
131 }
132
133 void
134 SimulationRunner::useStringAsMdpFile(const std::string &mdpString)
135 {
136     gmx::TextWriter::writeFileFromString(mdpInputFileName_, mdpString);
137 }
138
139 void
140 SimulationRunner::useStringAsNdxFile(const char *ndxString)
141 {
142     gmx::TextWriter::writeFileFromString(ndxFileName_, ndxString);
143 }
144
145 void
146 SimulationRunner::useTopGroAndNdxFromDatabase(const char *name)
147 {
148     topFileName_ = fixture_->fileManager_.getInputFilePath((std::string(name) + ".top").c_str());
149     groFileName_ = fixture_->fileManager_.getInputFilePath((std::string(name) + ".gro").c_str());
150     ndxFileName_ = fixture_->fileManager_.getInputFilePath((std::string(name) + ".ndx").c_str());
151 }
152
153 void
154 SimulationRunner::useGroFromDatabase(const char *name)
155 {
156     groFileName_ = fixture_->fileManager_.getInputFilePath((std::string(name) + ".gro").c_str());
157 }
158
159 int
160 SimulationRunner::callGromppOnThisRank()
161 {
162     CommandLine caller;
163     caller.append("grompp");
164     caller.addOption("-f", mdpInputFileName_);
165     caller.addOption("-n", ndxFileName_);
166     caller.addOption("-p", topFileName_);
167     caller.addOption("-c", groFileName_);
168
169     caller.addOption("-po", mdpOutputFileName_);
170     caller.addOption("-o", tprFileName_);
171
172     return gmx_grompp(caller.argc(), caller.argv());
173 }
174
175 int
176 SimulationRunner::callGrompp()
177 {
178     int returnValue = 0;
179 #ifdef GMX_LIB_MPI
180     // When compiled with external MPI, we're trying to run mdrun with
181     // MPI, but we need to make sure that we only do grompp on one
182     // rank
183     if (0 == gmx_node_rank())
184 #endif
185     {
186         returnValue = callGromppOnThisRank();
187     }
188 #ifdef GMX_LIB_MPI
189     // Make sure rank zero has written the .tpr file before other
190     // ranks try to read it. Thread-MPI and serial do this just fine
191     // on their own.
192     MPI_Barrier(MPI_COMM_WORLD);
193 #endif
194     return returnValue;
195 }
196
197 int
198 SimulationRunner::callMdrun(const CommandLine &callerRef)
199 {
200     /* Conforming to style guide by not passing a non-const reference
201        to this function. Passing a non-const reference might make it
202        easier to write code that incorrectly re-uses callerRef after
203        the call to this function. */
204
205     CommandLine caller(callerRef);
206     caller.addOption("-s", tprFileName_);
207
208     caller.addOption("-g", logFileName_);
209     caller.addOption("-e", edrFileName_);
210     caller.addOption("-o", fullPrecisionTrajectoryFileName_);
211     caller.addOption("-x", reducedPrecisionTrajectoryFileName_);
212
213     caller.addOption("-deffnm", fixture_->fileManager_.getTemporaryFilePath("state"));
214
215     if (nsteps_ > -2)
216     {
217         caller.addOption("-nsteps", nsteps_);
218     }
219
220 #ifdef GMX_MPI
221 #  ifdef GMX_GPU
222 #    ifdef GMX_THREAD_MPI
223     int         numGpusNeeded = g_numThreads;
224 #    else   /* Must be real MPI */
225     int         numGpusNeeded = gmx_node_num();
226 #    endif
227     std::string gpuIdString(numGpusNeeded, '0');
228     caller.addOption("-gpu_id", gpuIdString.c_str());
229 #  endif
230 #endif
231
232 #ifdef GMX_THREAD_MPI
233     caller.addOption("-nt", g_numThreads);
234 #endif
235
236 #ifdef GMX_OPENMP
237     caller.addOption("-ntomp", g_numOpenMPThreads);
238 #endif
239
240     return gmx_mdrun(caller.argc(), caller.argv());
241 }
242
243 int
244 SimulationRunner::callMdrun()
245 {
246     CommandLine caller;
247     caller.append("mdrun");
248     return callMdrun(caller);
249 }
250
251 // ====
252
253 MdrunTestFixtureBase::MdrunTestFixtureBase()
254 {
255 #ifdef GMX_LIB_MPI
256     GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
257 #endif
258 }
259
260 MdrunTestFixtureBase::~MdrunTestFixtureBase()
261 {
262 }
263
264 // ====
265
266 MdrunTestFixture::MdrunTestFixture() : runner_(this)
267 {
268 }
269
270 MdrunTestFixture::~MdrunTestFixture()
271 {
272 }
273
274 } // namespace test
275 } // namespace gmx