f3bef5f6e0dcf33358a7febf0f079305872991aa
[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, 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 "gromacs/gmxpreprocess/grompp.h"
45 #include "gromacs/math/vec.h"
46 #include "gromacs/utility/stringutil.h"
47 #include "gromacs/utility/textwriter.h"
48
49 #include "testutils/cmdlinetest.h"
50 #include "testutils/testfilemanager.h"
51 #include "programs/mdrun/tests/moduletest.h"
52
53 namespace gmxapi
54 {
55
56 namespace testing
57 {
58
59 /*! \brief Helper function to get step size as floating point number.
60  *
61  * A step size of 0.002ps is suitable for the simulation.
62  * We prefer to guarantee that the testing tools easily anticipate TPR time step size
63  * and time value in trajectory outputs, so we explicitly choose a time step that is
64  * exactly representable in binary. 2^-9 is just smaller than 0.002 and requires very
65  * little floating point precision (mantissa == 0). This means that sums and multiples
66  * of the timestep are also exactly representable, and thus particularly convenient for tests."
67  *
68  * For human readability we use the decimal representation, 1.0 x 2^-9 = 0.001953125.
69  *
70  * \returns Step size for tests.
71  */
72 inline real getTestStepSize()
73 {
74     return 0.001953125;
75 }
76
77 //! Provide command-line infrastructure for gmxapi tests.
78 class GmxApiTest : public gmx::test::MdrunTestFixture
79 {
80 public:
81     GmxApiTest() {}
82
83     /* \brief
84      * Prepare a tpr to run the test with.
85      *
86      * Sets up the TPR to run a test of the GMXAPI with a set number of \p steps
87      * defined in the test.
88      *
89      * \param[in] steps Number of steps for test to run.
90      */
91     void makeTprFile(int steps)
92     {
93         runner_.useTopGroAndNdxFromDatabase("spc_and_methane");
94         runner_.useStringAsMdpFile(
95                 gmx::formatString("integrator = md\n"
96                                   "cutoff-scheme = Verlet\n"
97                                   "nsteps = %d\n"
98                                   "dt = %11.9f\n"
99                                   "nstxout = 2\n"
100                                   "nstvout = 2\n"
101                                   "nstfout = 4\n"
102                                   "nstxout-compressed = 5\n"
103                                   "tcoupl = v-rescale\n"
104                                   "tc-grps = System\n"
105                                   "tau-t = 1\n"
106                                   "ref-t = 298\n"
107                                   "compressed-x-grps = Sol\n",
108                                   steps, getTestStepSize()));
109
110         EXPECT_EQ(0, runner_.callGromppOnThisRank());
111     }
112
113     //! Make the md arguments to work with
114     std::vector<std::string> makeMdArgs() const
115     {
116         std::vector<std::string> mdArgs;
117
118         mdArgs.emplace_back("-o");
119         mdArgs.emplace_back(runner_.fullPrecisionTrajectoryFileName_);
120         mdArgs.emplace_back("-x");
121         mdArgs.emplace_back(runner_.reducedPrecisionTrajectoryFileName_);
122         mdArgs.emplace_back("-c");
123         mdArgs.emplace_back(runner_.groOutputFileName_);
124         mdArgs.emplace_back("-g");
125         mdArgs.emplace_back(runner_.logFileName_);
126         mdArgs.emplace_back("-e");
127         mdArgs.emplace_back(runner_.edrFileName_);
128         mdArgs.emplace_back("-cpo");
129         mdArgs.emplace_back(runner_.cptFileName_);
130
131         return mdArgs;
132     }
133 };
134
135 } // namespace testing
136
137 } // end namespace gmxapi
138
139
140 #endif // GROMACS_TESTINGCONFIGURATION_H