f8d999682db6f1a22c6fe98856fe60483e01c701
[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
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 #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 call"));
88 #endif
89 #ifdef GMX_OPENMP
90     options->addOption(IntegerOption("nt_omp").store(&g_numOpenMPThreads)
91                            .description("Number of OpenMP threads for child mdrun call"));
92 #endif
93 }
94 //! \endcond
95
96 }
97
98 MdrunTestFixture::MdrunTestFixture() :
99     topFileName(),
100     groFileName(),
101     fullPrecisionTrajectoryFileName(),
102     ndxFileName(),
103     mdpInputFileName(fileManager_.getTemporaryFilePath("input.mdp")),
104     mdpOutputFileName(fileManager_.getTemporaryFilePath("output.mdp")),
105     tprFileName(fileManager_.getTemporaryFilePath(".tpr")),
106     logFileName(fileManager_.getTemporaryFilePath(".log")),
107     edrFileName(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 MdrunTestFixture::~MdrunTestFixture()
116 {
117 }
118
119 // TODO The combination of defaulting to Verlet cut-off scheme, NVE,
120 // and verlet-buffer-tolerance = -1 gives a grompp error. If we keep
121 // things that way, this function should be renamed. For now,
122 // force the use of the group scheme.
123 void
124 MdrunTestFixture::useEmptyMdpFile()
125 {
126     useStringAsMdpFile("cutoff-scheme = Group\n");
127 }
128
129 void
130 MdrunTestFixture::useStringAsMdpFile(const char *mdpString)
131 {
132     useStringAsMdpFile(std::string(mdpString));
133 }
134
135 void
136 MdrunTestFixture::useStringAsMdpFile(const std::string &mdpString)
137 {
138     gmx::File::writeFileFromString(mdpInputFileName, mdpString);
139 }
140
141 void
142 MdrunTestFixture::useStringAsNdxFile(const char *ndxString)
143 {
144     gmx::File::writeFileFromString(ndxFileName, ndxString);
145 }
146
147 void
148 MdrunTestFixture::useTopGroAndNdxFromDatabase(const char *name)
149 {
150     topFileName = fileManager_.getInputFilePath((std::string(name) + ".top").c_str());
151     groFileName = fileManager_.getInputFilePath((std::string(name) + ".gro").c_str());
152     ndxFileName = fileManager_.getInputFilePath((std::string(name) + ".ndx").c_str());
153 }
154
155 int
156 MdrunTestFixture::callGromppOnThisRank()
157 {
158     CommandLine caller;
159     caller.append("grompp");
160     caller.addOption("-f", mdpInputFileName);
161     caller.addOption("-n", ndxFileName);
162     caller.addOption("-p", topFileName);
163     caller.addOption("-c", groFileName);
164
165     caller.addOption("-po", mdpOutputFileName);
166     caller.addOption("-o", tprFileName);
167
168     return gmx_grompp(caller.argc(), caller.argv());
169 }
170
171 int
172 MdrunTestFixture::callGrompp()
173 {
174 #ifdef GMX_LIB_MPI
175     // When compiled with external MPI, only call one instance of the
176     // grompp function
177     if (0 != gmx_node_rank())
178     {
179         return 0;
180     }
181 #endif
182     return callGromppOnThisRank();
183 }
184
185 int
186 MdrunTestFixture::callMdrun(const CommandLine &callerRef)
187 {
188     /* Conforming to style guide by not passing a non-const reference
189        to this function. Passing a non-const reference might make it
190        easier to write code that incorrectly re-uses callerRef after
191        the call to this function. */
192     CommandLine caller(callerRef);
193     caller.addOption("-s", tprFileName);
194
195     caller.addOption("-g", logFileName);
196     caller.addOption("-e", edrFileName);
197     caller.addOption("-o", fullPrecisionTrajectoryFileName);
198     caller.addOption("-x", reducedPrecisionTrajectoryFileName);
199
200     caller.addOption("-deffnm", fileManager_.getTemporaryFilePath("state"));
201
202     if (nsteps > -2)
203     {
204         caller.addOption("-nsteps", nsteps);
205     }
206
207 #ifdef GMX_THREAD_MPI
208     caller.addOption("-nt", g_numThreads);
209 #endif
210 #ifdef GMX_OPENMP
211     caller.addOption("-ntomp", g_numOpenMPThreads);
212 #endif
213
214     return gmx_mdrun(caller.argc(), caller.argv());
215 }
216
217 int
218 MdrunTestFixture::callMdrun()
219 {
220     CommandLine caller;
221     caller.append("mdrun");
222     return callMdrun(caller);
223 }
224
225 } // namespace test
226 } // namespace gmx