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