3790cd220d1a0f46b6a7532310add1e8eaf574a1
[alexxy/gromacs.git] / src / programs / mdrun / tests / moduletest.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2013,2014,2015,2016,2017,2018,2019, 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 /*! \defgroup module_mdrun_integration_tests Integration test utilities
36  * \ingroup group_mdrun
37  *
38  * \brief Functionality for testing mdrun as a whole
39  */
40 /*! \internal \file
41  * \brief
42  * Declares test fixtures for general mdrun functionality.
43  *
44  * \author Mark Abraham <mark.j.abraham@gmail.com>
45  * \ingroup module_mdrun_integration_tests
46  */
47 #ifndef GMX_MDRUN_TESTS_MODULETEST_H
48 #define GMX_MDRUN_TESTS_MODULETEST_H
49
50 #include <gtest/gtest.h>
51
52 #include "gromacs/utility/classhelpers.h"
53
54 #include "testutils/cmdlinetest.h"
55 #include "testutils/testfilemanager.h"
56
57 namespace gmx
58 {
59 namespace test
60 {
61
62 /*! \internal
63  * \brief Helper object for running grompp and mdrun in
64  * integration tests of mdrun functionality
65  *
66  * Objects of this class are intended to be owned by
67  * IntegrationTestFixture objects, and an IntegrationTestFixture
68  * object might own more than one SimulationRunner.
69  *
70  * The setup phase creates various temporary files for input and
71  * output that are common for mdrun tests, using the file manager
72  * object of the fixture that owns this object. Individual tests
73  * should create any extra filenames similarly, so that the test
74  * users's current working directory does not get littered with files
75  * left over from tests.
76  *
77  * Any method in this class may throw std::bad_alloc if out of memory.
78  *
79  * By default, the convenience methods callGrompp() and callMdrun()
80  * just prepare and run a default call to mdrun. If there is a need to
81  * customize the command-line for grompp or mdrun (e.g. to invoke
82  * -maxwarn n, or -reprod), then make a CommandLine object with the
83  * appropriate flags and pass that into the routines that accept such.
84  *
85  * \ingroup module_mdrun_integration_tests
86  */
87 class SimulationRunner
88 {
89 public:
90     //! Initializes a runner with given manager for temporary files.
91     explicit SimulationRunner(TestFileManager* fileManager);
92
93     //! Use an empty .mdp file as input to grompp
94     void useEmptyMdpFile();
95     //! Use a given string as input to grompp
96     void useStringAsMdpFile(const char* mdpString);
97     //! Use a given string as input to grompp
98     void useStringAsMdpFile(const std::string& mdpString);
99     //! Use a string as -n input to grompp
100     void useStringAsNdxFile(const char* ndxString);
101     //! Use a standard .top and .g96 file as input to grompp
102     void useTopG96AndNdxFromDatabase(const std::string& name);
103     //! Use a standard .top and .gro file as input to grompp
104     void useTopGroAndNdxFromDatabase(const std::string& name);
105     //! Use a standard .gro file as input to grompp
106     void useGroFromDatabase(const char* name);
107     //! Calls grompp (on rank 0, with a customized command line) to prepare for the mdrun test
108     int callGrompp(const CommandLine& callerRef);
109     //! Convenience wrapper for a default call to \c callGrompp
110     int callGrompp();
111     //! Calls grompp (on this rank, with a customized command line) to prepare for the mdrun test
112     int callGromppOnThisRank(const CommandLine& callerRef);
113     //! Convenience wrapper for a default call to \c callGromppOnThisRank
114     int callGromppOnThisRank();
115     //! Calls nmeig for testing
116     int callNmeig();
117     //! Calls mdrun for testing with a customized command line
118     int callMdrun(const CommandLine& callerRef);
119     /*! \brief Convenience wrapper for calling mdrun for testing
120      * with default command line */
121     int callMdrun();
122     //! Calls convert-tpr on this rank to set a new number of steps in the tpr.
123     int changeTprNsteps(int nsteps);
124
125     //@{
126     /*! \name Names for frequently used grompp and mdrun output files
127      *
128      * These strings can be set to point to files present in the
129      * source tree, or to temporary files created for the test
130      * fixture. In the latter case,
131      * IntegrationTestFixture::fileManager_ should be used to fill
132      * these strings with paths to files, so that they are created
133      * in a temporary directory and (by default behaviour of
134      * TestFileManager) deleted when the test is complete.
135      */
136     std::string topFileName_;
137     std::string groFileName_;
138     std::string fullPrecisionTrajectoryFileName_;
139     std::string reducedPrecisionTrajectoryFileName_;
140     std::string groOutputFileName_;
141     std::string ndxFileName_;
142     std::string mdpOutputFileName_;
143     std::string tprFileName_;
144     std::string logFileName_;
145     std::string edrFileName_;
146     std::string mtxFileName_;
147     std::string cptFileName_;
148     std::string swapFileName_;
149     int         nsteps_;
150     //@}
151     //! What will be written into a temporary mdp file before the grompp call
152     std::string mdpInputContents_;
153
154 private:
155     TestFileManager& fileManager_;
156
157     GMX_DISALLOW_COPY_AND_ASSIGN(SimulationRunner);
158 };
159
160 /*! \internal
161  * \brief Declares test fixture base class for
162  * integration tests of mdrun functionality
163  *
164  * Derived fixture classes (or individual test cases) that might have
165  * specific requirements should assert that behaviour, rather than
166  * hard-code the requirements. A test that (for example) can't run
167  * with more than one thread should report that as a diagnostic, so the
168  * person running the test (or designing the test harness) can get
169  * feedback on what tests need what conditions without having to read
170  * the code of lots of tests.
171  *
172  * Specifying the execution context (such as numbers of threads and
173  * processors) is normally sensible to specify from the test harness
174  * (i.e. when CMake/CTest/the user runs a test executable), because
175  * only there is information about the hardware available. The default
176  * values for such context provided in test fixtures for mdrun should
177  * mirror the defaults for mdrun, but currently mdrun.c hard-codes
178  * those in a gmx_hw_opt_t.
179  *
180  * Any method in this class may throw std::bad_alloc if out of memory.
181  *
182  * \ingroup module_mdrun_integration_tests
183  */
184 class MdrunTestFixtureBase : public ::testing::Test
185 {
186 public:
187     MdrunTestFixtureBase();
188     ~MdrunTestFixtureBase() override;
189 };
190
191 /*! \internal
192  * \brief Declares test fixture class for integration
193  * tests of mdrun functionality that use a single call of mdrun
194  *
195  * Any method in this class may throw std::bad_alloc if out of memory.
196  *
197  * \ingroup module_mdrun_integration_tests
198  */
199 class MdrunTestFixture : public ::testing::Test
200 {
201 public:
202     MdrunTestFixture();
203     ~MdrunTestFixture() override;
204
205     //! Manages temporary files during the test.
206     TestFileManager fileManager_;
207     //! Helper object to manage the preparation for and call of mdrun
208     SimulationRunner runner_;
209 };
210
211 /*! \internal
212  * \brief
213  * Parameterized test fixture for mdrun integration tests
214  */
215 class ParameterizedMdrunTestFixture :
216     public gmx::test::MdrunTestFixture,
217     public ::testing::WithParamInterface<const char*>
218 {
219 };
220
221 } // namespace test
222 } // namespace gmx
223
224 #endif