Refactor mdrun integration tests
[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, 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
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/file.h"
54 #include "programs/mdrun/mdrun_main.h"
55
56 #include "testutils/cmdlinetest.h"
57 #include "testutils/integrationtests.h"
58 #include "testutils/testoptions.h"
59
60 namespace gmx
61 {
62 namespace test
63 {
64
65 /********************************************************************
66  * MdrunTestFixture
67  */
68
69 namespace
70 {
71
72 #if defined(GMX_THREAD_MPI) || defined(DOXYGEN)
73 //! Number of tMPI threads for child mdrun call.
74 int g_numThreads = 1;
75 #endif
76 #if defined(GMX_OPENMP) || defined(DOXYGEN)
77 //! Number of OpenMP threads for child mdrun call.
78 int g_numOpenMPThreads = 1;
79 #endif
80 //! \cond
81 GMX_TEST_OPTIONS(MdrunTestOptions, options)
82 {
83     GMX_UNUSED_VALUE(options);
84 #ifdef GMX_THREAD_MPI
85     options->addOption(IntegerOption("nt").store(&g_numThreads)
86                            .description("Number of thread-MPI threads/ranks for child mdrun calls"));
87 #endif
88 #ifdef GMX_OPENMP
89     options->addOption(IntegerOption("nt_omp").store(&g_numOpenMPThreads)
90                            .description("Number of OpenMP threads for child mdrun calls"));
91 #endif
92 }
93 //! \endcond
94
95 }
96
97 SimulationRunner::SimulationRunner(IntegrationTestFixture *fixture) :
98     fixture_(fixture),
99     topFileName_(),
100     groFileName_(),
101     fullPrecisionTrajectoryFileName_(),
102     ndxFileName_(),
103     mdpInputFileName_(fixture_->fileManager_.getTemporaryFilePath("input.mdp")),
104     mdpOutputFileName_(fixture_->fileManager_.getTemporaryFilePath("output.mdp")),
105     tprFileName_(fixture_->fileManager_.getTemporaryFilePath(".tpr")),
106     logFileName_(fixture_->fileManager_.getTemporaryFilePath(".log")),
107     edrFileName_(fixture_->fileManager_.getTemporaryFilePath(".edr")),
108     nsteps_(-2)
109 {
110 #ifdef GMX_LIB_MPI
111     GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
112 #endif
113 }
114
115 // TODO The combination of defaulting to Verlet cut-off scheme, NVE,
116 // and verlet-buffer-tolerance = -1 gives a grompp error. If we keep
117 // things that way, this function should be renamed. For now,
118 // force the use of the group scheme.
119 void
120 SimulationRunner::useEmptyMdpFile()
121 {
122     useStringAsMdpFile("cutoff-scheme = Group\n");
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     gmx::File::writeFileFromString(mdpInputFileName_, mdpString);
135 }
136
137 void
138 SimulationRunner::useStringAsNdxFile(const char *ndxString)
139 {
140     gmx::File::writeFileFromString(ndxFileName_, ndxString);
141 }
142
143 void
144 SimulationRunner::useTopGroAndNdxFromDatabase(const char *name)
145 {
146     topFileName_ = fixture_->fileManager_.getInputFilePath((std::string(name) + ".top").c_str());
147     groFileName_ = fixture_->fileManager_.getInputFilePath((std::string(name) + ".gro").c_str());
148     ndxFileName_ = fixture_->fileManager_.getInputFilePath((std::string(name) + ".ndx").c_str());
149 }
150
151 int
152 SimulationRunner::callGromppOnThisRank()
153 {
154     CommandLine caller;
155     caller.append("grompp");
156     caller.addOption("-f", mdpInputFileName_);
157     caller.addOption("-n", ndxFileName_);
158     caller.addOption("-p", topFileName_);
159     caller.addOption("-c", groFileName_);
160
161     caller.addOption("-po", mdpOutputFileName_);
162     caller.addOption("-o", tprFileName_);
163
164     return gmx_grompp(caller.argc(), caller.argv());
165 }
166
167 int
168 SimulationRunner::callGrompp()
169 {
170 #ifdef GMX_LIB_MPI
171     // When compiled with external MPI, only call one instance of the
172     // grompp function
173     if (0 != gmx_node_rank())
174     {
175         return 0;
176     }
177 #endif
178     return callGromppOnThisRank();
179 }
180
181 int
182 SimulationRunner::callMdrun(const CommandLine &callerRef)
183 {
184     /* Conforming to style guide by not passing a non-const reference
185        to this function. Passing a non-const reference might make it
186        easier to write code that incorrectly re-uses callerRef after
187        the call to this function. */
188     CommandLine caller(callerRef);
189     caller.addOption("-s", tprFileName_);
190
191     caller.addOption("-g", logFileName_);
192     caller.addOption("-e", edrFileName_);
193     caller.addOption("-o", fullPrecisionTrajectoryFileName_);
194     caller.addOption("-x", reducedPrecisionTrajectoryFileName_);
195
196     caller.addOption("-deffnm", fixture_->fileManager_.getTemporaryFilePath("state"));
197
198     if (nsteps_ > -2)
199     {
200         caller.addOption("-nsteps", nsteps_);
201     }
202
203 #ifdef GMX_THREAD_MPI
204     caller.addOption("-nt", g_numThreads);
205 #endif
206 #ifdef GMX_OPENMP
207     caller.addOption("-ntomp", g_numOpenMPThreads);
208 #endif
209
210     return gmx_mdrun(caller.argc(), caller.argv());
211 }
212
213 int
214 SimulationRunner::callMdrun()
215 {
216     CommandLine caller;
217     caller.append("mdrun");
218     return callMdrun(caller);
219 }
220
221 // ====
222
223 MdrunTestFixtureBase::MdrunTestFixtureBase()
224 {
225 #ifdef GMX_LIB_MPI
226     GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
227 #endif
228 }
229
230 MdrunTestFixtureBase::~MdrunTestFixtureBase()
231 {
232 }
233
234 // ====
235
236 MdrunTestFixture::MdrunTestFixture() : runner_(this)
237 {
238 }
239
240 MdrunTestFixture::~MdrunTestFixture()
241 {
242 }
243
244 } // namespace test
245 } // namespace gmx