1 /*
2 * This file is part of the GROMACS molecular simulation package.
3 *
4 * Copyright (c) 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 /*! \internal \file
36 * \brief
37 * Tests for gmx energy
38 *
39 * \todo These will be superseded by tests of the energyanalysis
40 * modules.
41 *
42 * \author Mark Abraham <mark.j.abraham@gmail.com>
43 */
44
45 #include "gmxpre.h"
46
47 #include <cstring>
48
49 #include <string>
50
51 #include "gromacs/gmxana/gmx_ana.h"
52
53 #include "testutils/cmdlinetest.h"
54 #include "testutils/stdiohelper.h"
55 #include "testutils/testasserts.h"
56 #include "testutils/textblockmatchers.h"
57 #include "testutils/xvgtest.h"
58
59 namespace gmx
60 {
61 namespace test
62 {
63 namespace
64 {
65
66 class DhdlTest : public CommandLineTestBase
67 {
68 public:
runTest()69 void runTest()
70 {
71 auto& cmdline = commandLine();
72
73 setInputFile("-s", "dhdl.tpr");
74 setInputFile("-f", "dhdl.edr");
75 setOutputFile("-odh", "dhdl.xvg", XvgMatch());
76
77 ASSERT_EQ(0, gmx_energy(cmdline.argc(), cmdline.argv()));
78
79 checkOutputFiles();
80 }
81 };
82
TEST_F(DhdlTest,ExtractDhdl)83 TEST_F(DhdlTest, ExtractDhdl)
84 {
85 runTest();
86 }
87
88 class OriresTest : public CommandLineTestBase
89 {
90 public:
runTest(const char * stringForStdin)91 void runTest(const char* stringForStdin)
92 {
93 auto& cmdline = commandLine();
94
95 setInputFile("-s", "orires.tpr");
96 setInputFile("-f", "orires.edr");
97 test::XvgMatch xvg;
98 test::XvgMatch& toler = xvg.tolerance(gmx::test::relativeToleranceAsFloatingPoint(1, 1e-4));
99
100 setOutputFile("-oten", ".xvg", toler);
101 setOutputFile("-ora", ".xvg", toler);
102 setOutputFile("-ort", ".xvg", toler);
103 setOutputFile("-oda", ".xvg", toler);
104 setOutputFile("-odr", ".xvg", toler);
105
106 StdioTestHelper stdioHelper(&fileManager());
107 stdioHelper.redirectStringToStdin(stringForStdin);
108 ASSERT_EQ(0, gmx_nmr(cmdline.argc(), cmdline.argv()));
109
110 checkOutputFiles();
111 }
112 };
113
TEST_F(OriresTest,ExtractOrires)114 TEST_F(OriresTest, ExtractOrires)
115 {
116 runTest("-1\n");
117 }
118
119 class EnergyTest : public CommandLineTestBase
120 {
121 public:
runTest(const char * stringForStdin)122 void runTest(const char* stringForStdin)
123 {
124 auto& cmdline = commandLine();
125
126 setInputFile("-f", "ener.edr");
127 setOutputFile("-o", "energy.xvg", XvgMatch());
128
129 StdioTestHelper stdioHelper(&fileManager());
130 stdioHelper.redirectStringToStdin(stringForStdin);
131 ASSERT_EQ(0, gmx_energy(cmdline.argc(), cmdline.argv()));
132
133 // All the .edr files used in the tests contain only
134 // single-precision values, so even from a
135 // double-precision build they should conform to
136 // tolerances suitable for single-precision values.
137 setDefaultTolerance(defaultFloatTolerance());
138 checkOutputFiles();
139 }
140 };
141
TEST_F(EnergyTest,ExtractEnergy)142 TEST_F(EnergyTest, ExtractEnergy)
143 {
144 runTest("Potential\nKinetic-En.\nTotal-Energy\n");
145 }
146
TEST_F(EnergyTest,ExtractEnergyByNumber)147 TEST_F(EnergyTest, ExtractEnergyByNumber)
148 {
149 runTest("4 6 9");
150 }
151
TEST_F(EnergyTest,ExtractEnergyMixed)152 TEST_F(EnergyTest, ExtractEnergyMixed)
153 {
154 runTest("Pressu\n7\nbox-z\nvol\n");
155 }
156
157 class ViscosityTest : public CommandLineTestBase
158 {
159 public:
runTest()160 void runTest()
161 {
162 auto& cmdline = commandLine();
163 setInputFile("-f", "ener.edr");
164 setOutputFile("-vis", "visco.xvg", NoTextMatch());
165 setOutputFile("-o", "energy.xvg", NoTextMatch());
166
167 /* -vis can write a lot of non-conditional output files,
168 so we use temporary paths to clean up files that are
169 not the ones being tested in this test */
170 if (!cmdline.contains("-evisco"))
171 {
172 setOutputFile("-evisco", "evisco.xvg", NoTextMatch());
173 }
174 if (!cmdline.contains("-eviscoi"))
175 {
176 setOutputFile("-eviscoi", "eviscoi.xvg", NoTextMatch());
177 }
178 if (!cmdline.contains("-corr"))
179 {
180 setOutputFile("-corr", "corr.xvg", NoTextMatch());
181 }
182
183 ASSERT_EQ(0, gmx_energy(cmdline.argc(), cmdline.argv()));
184
185 checkOutputFiles();
186 }
187 };
188
TEST_F(ViscosityTest,EinsteinViscosity)189 TEST_F(ViscosityTest, EinsteinViscosity)
190 {
191 auto tolerance = relativeToleranceAsFloatingPoint(1e-4, 1e-5);
192 setOutputFile("-evisco", "evisco.xvg", XvgMatch().tolerance(tolerance));
193 runTest();
194 }
195
TEST_F(ViscosityTest,EinsteinViscosityIntegral)196 TEST_F(ViscosityTest, EinsteinViscosityIntegral)
197 {
198 auto tolerance = relativeToleranceAsFloatingPoint(1e-4, 1e-5);
199 setOutputFile("-eviscoi", "eviscoi.xvg", XvgMatch().tolerance(tolerance));
200 runTest();
201 }
202
203 } // namespace
204 } // namespace test
205 } // namespace gmx
206