Merge branch release-2018
[alexxy/gromacs.git] / src / gromacs / energyanalysis / tests / legacyenergy.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2017,2018, 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:
69         void runTest()
70         {
71             auto &cmdline = commandLine();
72             cmdline.append("energy");
73
74             setInputFile("-s", "dhdl.tpr");
75             setInputFile("-f", "dhdl.edr");
76             setOutputFile("-odh", "dhdl.xvg", XvgMatch());
77
78             ASSERT_EQ(0, gmx_energy(cmdline.argc(), cmdline.argv()));
79
80             checkOutputFiles();
81         }
82 };
83
84 TEST_F(DhdlTest, ExtractDhdl)
85 {
86     runTest();
87 }
88
89 class OriresTest : public CommandLineTestBase
90 {
91     public:
92         void runTest(const char *stringForStdin)
93         {
94             auto &cmdline = commandLine();
95             cmdline.append("energy");
96
97             setInputFile("-s", "orires.tpr");
98             setInputFile("-f", "orires.edr");
99             test::XvgMatch    xvg;
100             test::XvgMatch   &toler     = xvg.tolerance(gmx::test::relativeToleranceAsFloatingPoint(1, 1e-4));
101
102             setOutputFile("-oten", ".xvg", toler);
103             setOutputFile("-ora", ".xvg", toler);
104             setOutputFile("-ort", ".xvg", toler);
105             setOutputFile("-oda", ".xvg", toler);
106             setOutputFile("-odr", ".xvg", toler);
107
108             StdioTestHelper stdioHelper(&fileManager());
109             stdioHelper.redirectStringToStdin(stringForStdin);
110             ASSERT_EQ(0, gmx_nmr(cmdline.argc(), cmdline.argv()));
111
112             checkOutputFiles();
113         }
114 };
115
116 TEST_F(OriresTest, ExtractOrires)
117 {
118     runTest("-1\n");
119 }
120
121 class EnergyTest : public CommandLineTestBase
122 {
123     public:
124         void runTest(const char *stringForStdin)
125         {
126             auto &cmdline = commandLine();
127             cmdline.append("energy");
128
129             setInputFile("-f", "ener.edr");
130             setOutputFile("-o", "energy.xvg", XvgMatch());
131
132             StdioTestHelper stdioHelper(&fileManager());
133             stdioHelper.redirectStringToStdin(stringForStdin);
134             ASSERT_EQ(0, gmx_energy(cmdline.argc(), cmdline.argv()));
135
136             // All the .edr files used in the tests contain only
137             // single-precision values, so even from a
138             // double-precision build they should conform to
139             // tolerances suitable for single-precision values.
140             setDefaultTolerance(defaultFloatTolerance());
141             checkOutputFiles();
142         }
143 };
144
145 TEST_F(EnergyTest, ExtractEnergy)
146 {
147     runTest("Potential\nKinetic-En.\nTotal-Energy\n");
148 }
149
150 TEST_F(EnergyTest, ExtractEnergyByNumber)
151 {
152     runTest("4 6 9");
153 }
154
155 TEST_F(EnergyTest, ExtractEnergyMixed)
156 {
157     runTest("Pressu\n7\nbox-z\nvol\n");
158 }
159
160 class ViscosityTest : public CommandLineTestBase
161 {
162     public:
163         void runTest()
164         {
165             auto &cmdline = commandLine();
166             setInputFile("-f", "ener.edr");
167             setOutputFile("-vis", "visco.xvg", NoTextMatch());
168             setOutputFile("-o", "energy.xvg", NoTextMatch());
169
170             /* -vis can write a lot of non-conditional output files,
171                 so we use temporary paths to clean up files that are
172                 not the ones being tested in this test */
173             if (!cmdline.contains("-evisco"))
174             {
175                 setOutputFile("-evisco", "evisco.xvg", NoTextMatch());
176             }
177             if (!cmdline.contains("-eviscoi"))
178             {
179                 setOutputFile("-eviscoi", "eviscoi.xvg", NoTextMatch());
180             }
181             if (!cmdline.contains("-corr"))
182             {
183                 setOutputFile("-corr", "corr.xvg", NoTextMatch());
184             }
185
186             ASSERT_EQ(0, gmx_energy(cmdline.argc(), cmdline.argv()));
187
188             checkOutputFiles();
189         }
190 };
191
192 TEST_F(ViscosityTest, EinsteinViscosity)
193 {
194     auto tolerance = relativeToleranceAsFloatingPoint(1e-4, 1e-5);
195     setOutputFile("-evisco", "evisco.xvg", XvgMatch().tolerance(tolerance));
196     runTest();
197 }
198
199 TEST_F(ViscosityTest, EinsteinViscosityIntegral)
200 {
201     auto tolerance = relativeToleranceAsFloatingPoint(1e-4, 1e-5);
202     setOutputFile("-eviscoi", "eviscoi.xvg", XvgMatch().tolerance(tolerance));
203     runTest();
204 }
205
206 } // namespace
207 } // namespace test
208 } // namespace gmx