Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / src / programs / mdrun / tests / normalmodes.cpp
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 /*! \internal \file
37  * \brief
38  * Tests for the normal modes functionality.
39  *
40  * \author David van der Spoel <david.vanderspoel@icm.uu.se>
41  * \ingroup module_mdrun_integration_tests
42  */
43 #include "gmxpre.h"
44
45 #include <map>
46 #include <memory>
47 #include <string>
48 #include <tuple>
49 #include <vector>
50
51 #include <gtest/gtest.h>
52
53 #include "gromacs/options/filenameoption.h"
54 #include "gromacs/topology/idef.h"
55 #include "gromacs/topology/ifunc.h"
56 #include "gromacs/trajectory/energyframe.h"
57 #include "gromacs/trajectory/trajectoryframe.h"
58 #include "gromacs/utility/basenetwork.h"
59 #include "gromacs/utility/filestream.h"
60 #include "gromacs/utility/stringutil.h"
61
62 #include "testutils/mpitest.h"
63 #include "testutils/refdata.h"
64 #include "testutils/simulationdatabase.h"
65 #include "testutils/testasserts.h"
66 #include "testutils/xvgtest.h"
67
68 #include "energycomparison.h"
69 #include "energyreader.h"
70 #include "moduletest.h"
71
72 namespace gmx
73 {
74 namespace test
75 {
76 namespace
77 {
78
79 /*! \brief Test fixture base for normal mode analysis
80  *
81  * This test ensures mdrun can run a normal mode analysis, reaching
82  * a reproducible eigenvalues following diagonalization.
83  *
84  * The choices for tolerance are arbitrary but sufficient. */
85 class NormalModesTest :
86     public MdrunTestFixture,
87     public ::testing::WithParamInterface<std::tuple<std::string, std::string>>
88 {
89 };
90
91 TEST_P(NormalModesTest, WithinTolerances)
92 {
93     auto params         = GetParam();
94     auto simulationName = std::get<0>(params);
95     auto integrator     = std::get<1>(params);
96     SCOPED_TRACE(formatString("Comparing normal modes for '%s'", simulationName.c_str()));
97
98     // TODO At some point we should also test PME-only ranks.
99     int numRanksAvailable = getNumberOfTestMpiRanks();
100     if (!isNumberOfPpRanksSupported(simulationName, numRanksAvailable))
101     {
102         fprintf(stdout,
103                 "Test system '%s' cannot run with %d ranks.\n"
104                 "The supported numbers are: %s\n",
105                 simulationName.c_str(),
106                 numRanksAvailable,
107                 reportNumbersOfPpRanksSupported(simulationName).c_str());
108         return;
109     }
110     auto mdpFieldValues =
111             prepareMdpFieldValues(simulationName.c_str(), integrator.c_str(), "no", "no");
112     mdpFieldValues["nsteps"]      = "1";
113     mdpFieldValues["rcoulomb"]    = "5.6";
114     mdpFieldValues["rlist"]       = "5.6";
115     mdpFieldValues["rvdw"]        = "5.6";
116     mdpFieldValues["constraints"] = "none";
117     mdpFieldValues["coulombtype"] = "Cut-off";
118     mdpFieldValues["vdwtype"]     = "Cut-off";
119
120     // prepare the .tpr file
121     {
122         CommandLine caller;
123         runner_.useTopG96AndNdxFromDatabase(simulationName);
124         runner_.useStringAsMdpFile(prepareMdpFileContents(mdpFieldValues));
125         EXPECT_EQ(0, runner_.callGrompp(caller));
126     }
127     // Do mdrun, preparing to check the normal modes later
128     {
129         CommandLine mdrunCaller;
130         ASSERT_EQ(0, runner_.callMdrun(mdrunCaller));
131     }
132     // Now run gmx nmeig and check the output
133     {
134         ASSERT_EQ(0, runner_.callNmeig());
135         TestReferenceData refData;
136         auto              checker = refData.rootChecker()
137                                .checkCompound("System", simulationName)
138                                .checkCompound("Integrator", integrator);
139         auto settings      = XvgMatchSettings();
140         settings.tolerance = relativeToleranceAsFloatingPoint(1.0, 1e-05);
141         TextInputFile input("eigenval.xvg");
142         checkXvgFile(&input, &checker, settings);
143     }
144 }
145
146 // The time for OpenCL kernel compilation means these tests might time
147 // out. If that proves to be a problem, these can be disabled for
148 // OpenCL builds. However, once that compilation is cached for the
149 // lifetime of the whole test binary process, these tests should run in
150 // such configurations.
151 #if GMX_DOUBLE
152
153 //! Containers of systems and integrators to test.
154 //! \{
155 std::vector<std::string> systemsToTest_g     = { "scaled-water",
156                                              "villin",
157                                              "spc-dimer",
158                                              "one-tip5p",
159                                              "sw-dimer" };
160 std::vector<std::string> integratorsToTest_g = { "nm" };
161
162 //! \}
163
164 INSTANTIATE_TEST_CASE_P(NormalModesWorks,
165                         NormalModesTest,
166                         ::testing::Combine(::testing::ValuesIn(systemsToTest_g),
167                                            ::testing::ValuesIn(integratorsToTest_g)));
168 #endif
169 } // namespace
170 } // namespace test
171 } // namespace gmx