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