Fix parallel testing
[alexxy/gromacs.git] / src / api / cpp / tests / testingconfiguration.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019,2020, 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
36 #ifndef GROMACS_TESTINGCONFIGURATION_H
37 #define GROMACS_TESTINGCONFIGURATION_H
38
39 #include <gtest/gtest.h>
40
41 #include <string>
42 #include <vector>
43
44 #include "config.h"
45
46 #include "gromacs/gmxpreprocess/grompp.h"
47 #include "gromacs/math/vec.h"
48 #include "gromacs/utility/stringutil.h"
49 #include "gromacs/utility/textwriter.h"
50
51 #include "testutils/cmdlinetest.h"
52 #include "testutils/testfilemanager.h"
53 #include "programs/mdrun/tests/moduletest.h"
54
55 namespace gmxapi
56 {
57
58 namespace testing
59 {
60
61 #if GMX_OPENMP || defined(DOXYGEN)
62 //! Number of OpenMP threads for child mdrun call.
63 static constexpr int g_numOpenMPThreads = 2;
64 #endif
65
66 /*! \brief Helper function to get step size as floating point number.
67  *
68  * A step size of 0.002ps is suitable for the simulation.
69  * We prefer to guarantee that the testing tools easily anticipate TPR time step size
70  * and time value in trajectory outputs, so we explicitly choose a time step that is
71  * exactly representable in binary. 2^-9 is just smaller than 0.002 and requires very
72  * little floating point precision (mantissa == 0). This means that sums and multiples
73  * of the timestep are also exactly representable, and thus particularly convenient for tests."
74  *
75  * For human readability we use the decimal representation, 1.0 x 2^-9 = 0.001953125.
76  *
77  * \returns Step size for tests.
78  */
79 inline real getTestStepSize()
80 {
81     return 0.001953125;
82 }
83
84 //! Provide command-line infrastructure for gmxapi tests.
85 class GmxApiTest : public gmx::test::MdrunTestFixture
86 {
87 public:
88     GmxApiTest() {}
89
90     /* \brief
91      * Prepare a tpr to run the test with.
92      *
93      * Sets up the TPR to run a test of the GMXAPI with a set number of \p steps
94      * defined in the test.
95      *
96      * \param[in] steps Number of steps for test to run.
97      */
98     void makeTprFile(int steps)
99     {
100         runner_.useTopGroAndNdxFromDatabase("spc_and_methane");
101         runner_.useStringAsMdpFile(
102                 gmx::formatString("integrator = md\n"
103                                   "cutoff-scheme = Verlet\n"
104                                   "nsteps = %d\n"
105                                   "dt = %11.9f\n"
106                                   "nstxout = 2\n"
107                                   "nstvout = 2\n"
108                                   "nstfout = 4\n"
109                                   "nstxout-compressed = 5\n"
110                                   "tcoupl = v-rescale\n"
111                                   "tc-grps = System\n"
112                                   "tau-t = 1\n"
113                                   "ref-t = 298\n"
114                                   "compressed-x-grps = Sol\n",
115                                   steps, getTestStepSize()));
116
117         EXPECT_EQ(0, runner_.callGromppOnThisRank());
118     }
119
120     //! Make the md arguments to work with
121     std::vector<std::string> makeMdArgs() const
122     {
123         std::vector<std::string> mdArgs;
124
125         mdArgs.emplace_back("-o");
126         mdArgs.emplace_back(runner_.fullPrecisionTrajectoryFileName_);
127         mdArgs.emplace_back("-x");
128         mdArgs.emplace_back(runner_.reducedPrecisionTrajectoryFileName_);
129         mdArgs.emplace_back("-c");
130         mdArgs.emplace_back(runner_.groOutputFileName_);
131         mdArgs.emplace_back("-g");
132         mdArgs.emplace_back(runner_.logFileName_);
133         mdArgs.emplace_back("-e");
134         mdArgs.emplace_back(runner_.edrFileName_);
135         mdArgs.emplace_back("-cpo");
136         mdArgs.emplace_back(runner_.cptFileName_);
137 #if GMX_THREAD_MPI
138         /* This should be handled through the actual API we have for getting
139          * ranks, but currently this leads to data races right now */
140         mdArgs.emplace_back("-ntmpi");
141         mdArgs.emplace_back("1");
142 #endif
143
144 #if GMX_OPENMP
145         mdArgs.emplace_back("-ntomp");
146         mdArgs.emplace_back(std::to_string(g_numOpenMPThreads));
147 #endif
148
149         return mdArgs;
150     }
151 };
152
153 } // namespace testing
154
155 } // end namespace gmxapi
156
157
158 #endif // GROMACS_TESTINGCONFIGURATION_H