bbf140cee51e981e0b3ce6aee5ac79e3a7235ae5
[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 by the GROMACS development team.
5  * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 /*! \defgroup module_mdrun_integration_tests Integration test utilities
37  * \ingroup group_mdrun
38  *
39  * \brief Functionality for testing mdrun as a whole
40  */
41 /*! \internal \file
42  * \brief
43  * Declares test fixtures for general mdrun functionality.
44  *
45  * \author Mark Abraham <mark.j.abraham@gmail.com>
46  * \ingroup module_mdrun_integration_tests
47  */
48 #ifndef GMX_MDRUN_TESTS_MODULETEST_H
49 #define GMX_MDRUN_TESTS_MODULETEST_H
50
51 #include <gtest/gtest.h>
52
53 #include "gromacs/utility/classhelpers.h"
54
55 #include "testutils/cmdlinetest.h"
56 #include "testutils/testfilemanager.h"
57
58 namespace gmx
59 {
60 namespace test
61 {
62
63 /*! \internal
64  * \brief How the mdp file of the SimulationRunner is defined
65  */
66 enum class SimulationRunnerMdpSource
67 {
68     //! The default behavior. Will result in an empty mdp file.
69     Undefined,
70     //! Mdp options are set via string using SimulationRunner::useStringAsMdpFile
71     String,
72     //! Mdp options are read from a file set in SimulationRunner::useTopGroAndMdpFromFepTestDatabase
73     File,
74     //! Signals the last enum entry
75     Count
76 };
77
78 /*! \internal
79  * \brief Helper object for running grompp and mdrun in
80  * integration tests of mdrun functionality
81  *
82  * Objects of this class are intended to be owned by
83  * IntegrationTestFixture objects, and an IntegrationTestFixture
84  * object might own more than one SimulationRunner.
85  *
86  * The setup phase creates various temporary files for input and
87  * output that are common for mdrun tests, using the file manager
88  * object of the fixture that owns this object. Individual tests
89  * should create any extra filenames similarly, so that the test
90  * users's current working directory does not get littered with files
91  * left over from tests.
92  *
93  * Any method in this class may throw std::bad_alloc if out of memory.
94  *
95  * By default, the convenience methods callGrompp() and callMdrun()
96  * just prepare and run a default call to mdrun. If there is a need to
97  * customize the command-line for grompp or mdrun (e.g. to invoke
98  * -maxwarn n, or -reprod), then make a CommandLine object with the
99  * appropriate flags and pass that into the routines that accept such.
100  *
101  * \ingroup module_mdrun_integration_tests
102  */
103 class SimulationRunner
104 {
105 public:
106     //! Initializes a runner with given manager for temporary files.
107     explicit SimulationRunner(TestFileManager* fileManager);
108
109     //! Use an empty .mdp file as input to grompp
110     void useEmptyMdpFile();
111     //! Use a given string as input to grompp
112     void useStringAsMdpFile(const char* mdpString);
113     //! Use a given string as input to grompp
114     void useStringAsMdpFile(const std::string& mdpString);
115     //! Use a string as -n input to grompp
116     void useStringAsNdxFile(const char* ndxString);
117     //! Use a standard .top and .g96 file as input to grompp
118     void useTopG96AndNdxFromDatabase(const std::string& name);
119     //! Use a standard .top and .gro file as input to grompp
120     void useTopGroAndNdxFromDatabase(const std::string& name);
121     //! Use a standard .gro file as input to grompp
122     void useGroFromDatabase(const char* name);
123     //! Use .top, .gro, and .mdp from FEP test database
124     void useTopGroAndMdpFromFepTestDatabase(const std::string& name);
125     //! Calls grompp (on rank 0, with a customized command line) to prepare for the mdrun test
126     int callGrompp(const CommandLine& callerRef);
127     //! Convenience wrapper for a default call to \c callGrompp
128     int callGrompp();
129     //! Calls grompp (on this rank, with a customized command line) to prepare for the mdrun test
130     int callGromppOnThisRank(const CommandLine& callerRef);
131     //! Convenience wrapper for a default call to \c callGromppOnThisRank
132     int callGromppOnThisRank();
133     //! Calls nmeig for testing
134     int callNmeig();
135     //! Calls mdrun for testing with a customized command line
136     int callMdrun(const CommandLine& callerRef);
137     /*! \brief Convenience wrapper for calling mdrun for testing
138      * with default command line */
139     int callMdrun();
140     //! Calls convert-tpr on this rank to set a new number of steps in the tpr.
141     int changeTprNsteps(int nsteps);
142
143     //@{
144     /*! \name Names for frequently used grompp and mdrun output files
145      *
146      * These strings can be set to point to files present in the
147      * source tree, or to temporary files created for the test
148      * fixture. In the latter case,
149      * IntegrationTestFixture::fileManager_ should be used to fill
150      * these strings with paths to files, so that they are created
151      * in a temporary directory and (by default behaviour of
152      * TestFileManager) deleted when the test is complete.
153      */
154     std::string topFileName_;
155     std::string groFileName_;
156     std::string mdpFileName_;
157     std::string fullPrecisionTrajectoryFileName_;
158     std::string reducedPrecisionTrajectoryFileName_;
159     std::string groOutputFileName_;
160     std::string ndxFileName_;
161     std::string mdpOutputFileName_;
162     std::string tprFileName_;
163     std::string logFileName_;
164     std::string edrFileName_;
165     std::string mtxFileName_;
166     std::string cptFileName_;
167     std::string swapFileName_;
168     std::string dhdlFileName_;
169     int         nsteps_;
170     //@}
171     //! How the mdp options are defined
172     SimulationRunnerMdpSource mdpSource_;
173     //! What will be written into a temporary mdp file before the grompp call
174     std::string mdpInputContents_;
175
176 private:
177     TestFileManager& fileManager_;
178
179     GMX_DISALLOW_COPY_AND_ASSIGN(SimulationRunner);
180 };
181
182 /*! \internal
183  * \brief Declares test fixture base class for
184  * integration tests of mdrun functionality
185  *
186  * Derived fixture classes (or individual test cases) that might have
187  * specific requirements should assert that behaviour, rather than
188  * hard-code the requirements. A test that (for example) can't run
189  * with more than one thread should report that as a diagnostic, so the
190  * person running the test (or designing the test harness) can get
191  * feedback on what tests need what conditions without having to read
192  * the code of lots of tests.
193  *
194  * Specifying the execution context (such as numbers of threads and
195  * processors) is normally sensible to specify from the test harness
196  * (i.e. when CMake/CTest/the user runs a test executable), because
197  * only there is information about the hardware available. The default
198  * values for such context provided in test fixtures for mdrun should
199  * mirror the defaults for mdrun, but currently mdrun.c hard-codes
200  * those in a gmx_hw_opt_t.
201  *
202  * Any method in this class may throw std::bad_alloc if out of memory.
203  *
204  * \ingroup module_mdrun_integration_tests
205  */
206 class MdrunTestFixtureBase : public ::testing::Test
207 {
208 public:
209     MdrunTestFixtureBase();
210     ~MdrunTestFixtureBase() override;
211 };
212
213 /*! \internal
214  * \brief Declares test fixture class for integration
215  * tests of mdrun functionality that use a single call of mdrun
216  *
217  * Any method in this class may throw std::bad_alloc if out of memory.
218  *
219  * \ingroup module_mdrun_integration_tests
220  */
221 class MdrunTestFixture : public ::testing::Test
222 {
223 public:
224     MdrunTestFixture();
225     ~MdrunTestFixture() override;
226
227     //! Manages temporary files during the test.
228     TestFileManager fileManager_;
229     //! Helper object to manage the preparation for and call of mdrun
230     SimulationRunner runner_;
231 };
232
233 /*! \internal
234  * \brief
235  * Parameterized test fixture for mdrun integration tests
236  */
237 class ParameterizedMdrunTestFixture :
238     public gmx::test::MdrunTestFixture,
239     public ::testing::WithParamInterface<const char*>
240 {
241 };
242
243 } // namespace test
244 } // namespace gmx
245
246 #endif