Merge release-5-0 into master
[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 "moduletest.h"
43
44 #ifdef HAVE_CONFIG_H
45 #  include <config.h>
46 #endif
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
55 #include "programs/mdrun/mdrun_main.h"
56
57 #include "testutils/integrationtests.h"
58 #include "testutils/testoptions.h"
59 #include "testutils/cmdlinetest.h"
60
61 namespace gmx
62 {
63 namespace test
64 {
65
66 /********************************************************************
67  * MdrunTestFixture
68  */
69
70 namespace
71 {
72
73 //! Number of tMPI threads for child mdrun call.
74 int gmx_unused g_numThreads = 1;
75 //! Number of OpenMP threads for child mdrun call.
76 int gmx_unused g_numOpenMPThreads = 1;
77
78 //! \cond
79 GMX_TEST_OPTIONS(MdrunTestOptions, options)
80 {
81     GMX_UNUSED_VALUE(options);
82 #ifdef GMX_THREAD_MPI
83     options->addOption(IntegerOption("nt").store(&g_numThreads)
84                            .description("Number of thread-MPI threads/ranks for child mdrun call"));
85 #endif
86 #ifdef GMX_OPENMP
87     options->addOption(IntegerOption("nt_omp").store(&g_numOpenMPThreads)
88                            .description("Number of OpenMP threads for child mdrun call"));
89 #endif
90 }
91 //! \endcond
92
93 }
94
95 MdrunTestFixture::MdrunTestFixture() :
96     topFileName(),
97     groFileName(),
98     fullPrecisionTrajectoryFileName(),
99     ndxFileName(),
100     mdpInputFileName(fileManager_.getTemporaryFilePath("input.mdp")),
101     mdpOutputFileName(fileManager_.getTemporaryFilePath("output.mdp")),
102     tprFileName(fileManager_.getTemporaryFilePath(".tpr")),
103     logFileName(fileManager_.getTemporaryFilePath(".log")),
104     edrFileName(fileManager_.getTemporaryFilePath(".edr")),
105     nsteps(-2)
106 {
107 #ifdef GMX_LIB_MPI
108     GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
109 #endif
110 }
111
112 MdrunTestFixture::~MdrunTestFixture()
113 {
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 MdrunTestFixture::useEmptyMdpFile()
122 {
123     useStringAsMdpFile("cutoff-scheme = Group\n");
124 }
125
126 void
127 MdrunTestFixture::useStringAsMdpFile(const char *mdpString)
128 {
129     useStringAsMdpFile(std::string(mdpString));
130 }
131
132 void
133 MdrunTestFixture::useStringAsMdpFile(const std::string &mdpString)
134 {
135     gmx::File::writeFileFromString(mdpInputFileName, mdpString);
136 }
137
138 void
139 MdrunTestFixture::useStringAsNdxFile(const char *ndxString)
140 {
141     gmx::File::writeFileFromString(ndxFileName, ndxString);
142 }
143
144 void
145 MdrunTestFixture::useTopGroAndNdxFromDatabase(const char *name)
146 {
147     topFileName = fileManager_.getInputFilePath((std::string(name) + ".top").c_str());
148     groFileName = fileManager_.getInputFilePath((std::string(name) + ".gro").c_str());
149     ndxFileName = fileManager_.getInputFilePath((std::string(name) + ".ndx").c_str());
150 }
151
152 int
153 MdrunTestFixture::callGromppOnThisRank()
154 {
155     CommandLine caller;
156     caller.append("grompp");
157     caller.addOption("-f", mdpInputFileName);
158     caller.addOption("-n", ndxFileName);
159     caller.addOption("-p", topFileName);
160     caller.addOption("-c", groFileName);
161
162     caller.addOption("-po", mdpOutputFileName);
163     caller.addOption("-o", tprFileName);
164
165     return gmx_grompp(caller.argc(), caller.argv());
166 }
167
168 int
169 MdrunTestFixture::callGrompp()
170 {
171 #ifdef GMX_LIB_MPI
172     // When compiled with external MPI, only call one instance of the
173     // grompp function
174     if (0 != gmx_node_rank())
175     {
176         return 0;
177     }
178 #endif
179     return callGromppOnThisRank();
180 }
181
182 int
183 MdrunTestFixture::callMdrun(const CommandLine &callerRef)
184 {
185     /* Conforming to style guide by not passing a non-const reference
186        to this function. Passing a non-const reference might make it
187        easier to write code that incorrectly re-uses callerRef after
188        the call to this function. */
189     CommandLine caller(callerRef);
190     caller.addOption("-s", tprFileName);
191
192     caller.addOption("-g", logFileName);
193     caller.addOption("-e", edrFileName);
194     caller.addOption("-o", fullPrecisionTrajectoryFileName);
195     caller.addOption("-x", reducedPrecisionTrajectoryFileName);
196
197     caller.addOption("-deffnm", fileManager_.getTemporaryFilePath("state"));
198
199     if (nsteps > -2)
200     {
201         caller.addOption("-nsteps", nsteps);
202     }
203
204 #ifdef GMX_THREAD_MPI
205     caller.addOption("-nt", g_numThreads);
206 #endif
207 #ifdef GMX_OPENMP
208     caller.addOption("-ntomp", g_numOpenMPThreads);
209 #endif
210
211     return gmx_mdrun(caller.argc(), caller.argv());
212 }
213
214 int
215 MdrunTestFixture::callMdrun()
216 {
217     CommandLine caller;
218     caller.append("mdrun");
219     return callMdrun(caller);
220 }
221
222 } // namespace test
223 } // namespace gmx