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