1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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  * Simple tests for the mdrun 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 <unordered_map>
50 #include <vector>
51 
52 #include <gtest/gtest.h>
53 
54 #include "gromacs/options/filenameoption.h"
55 #include "gromacs/topology/idef.h"
56 #include "gromacs/topology/ifunc.h"
57 #include "gromacs/trajectory/trajectoryframe.h"
58 #include "gromacs/utility/basenetwork.h"
59 #include "gromacs/utility/filestream.h"
60 #include "gromacs/utility/strconvert.h"
61 #include "gromacs/utility/stringutil.h"
62 
63 #include "testutils/mpitest.h"
64 #include "testutils/refdata.h"
65 #include "testutils/simulationdatabase.h"
66 #include "testutils/testasserts.h"
67 #include "testutils/xvgtest.h"
68 
69 #include "energycomparison.h"
70 #include "moduletest.h"
71 #include "trajectoryreader.h"
72 
73 namespace gmx
74 {
75 namespace test
76 {
77 namespace
78 {
79 
80 /*! \brief Database of enerngy tolerances for MD integrator on the various systems. */
81 std::unordered_map<std::string, FloatingPointTolerance> energyToleranceForSystem_g = {
82     { { "angles1", relativeToleranceAsFloatingPoint(1, 1e-4) } }
83 };
84 
85 /*! \brief Database of pressure
86    tolerances for MD integrator on the various systems. */
87 std::unordered_map<std::string, FloatingPointTolerance> pressureToleranceForSystem_g = {
88     { { "angles1", relativeToleranceAsFloatingPoint(1, 1e-4) } }
89 };
90 
91 //! Helper type
92 using MdpField = MdpFieldValues::value_type;
93 
94 /*! \brief Test fixture base for simple mdrun systems
95  *
96  * This test ensures mdrun can run a simulation, reaching
97  * reproducible energies.
98  *
99  * The choices for tolerance are arbitrary but sufficient. */
100 class SimpleMdrunTest :
101     public MdrunTestFixture,
102     public ::testing::WithParamInterface<std::tuple<std::string, std::string>>
103 {
104 };
105 
TEST_P(SimpleMdrunTest,WithinTolerances)106 TEST_P(SimpleMdrunTest, WithinTolerances)
107 {
108     auto params         = GetParam();
109     auto simulationName = std::get<0>(params);
110     auto integrator     = std::get<1>(params);
111     SCOPED_TRACE(formatString("Comparing simple mdrun for '%s'", simulationName.c_str()));
112 
113     // TODO At some point we should also test PME-only ranks.
114     int numRanksAvailable = getNumberOfTestMpiRanks();
115     if (!isNumberOfPpRanksSupported(simulationName, numRanksAvailable))
116     {
117         fprintf(stdout,
118                 "Test system '%s' cannot run with %d ranks.\n"
119                 "The supported numbers are: %s\n",
120                 simulationName.c_str(), numRanksAvailable,
121                 reportNumbersOfPpRanksSupported(simulationName).c_str());
122         return;
123     }
124     auto mdpFieldValues =
125             prepareMdpFieldValues(simulationName.c_str(), integrator.c_str(), "no", "no");
126     mdpFieldValues["nsteps"]        = "50";
127     mdpFieldValues["nstfout"]       = "4";
128     mdpFieldValues["constraints"]   = "none";
129     mdpFieldValues["nstcalcenergy"] = "4";
130     mdpFieldValues["coulombtype"]   = "Cut-off";
131     mdpFieldValues["vdwtype"]       = "Cut-off";
132 
133     // Prepare the .tpr file
134     {
135         CommandLine caller;
136         runner_.useTopGroAndNdxFromDatabase(simulationName);
137         runner_.useStringAsMdpFile(prepareMdpFileContents(mdpFieldValues));
138         EXPECT_EQ(0, runner_.callGrompp(caller));
139     }
140     // Do mdrun
141     {
142         CommandLine mdrunCaller;
143         ASSERT_EQ(0, runner_.callMdrun(mdrunCaller));
144         EnergyTermsToCompare energyTermsToCompare{ {
145                 { interaction_function[F_EPOT].longname, energyToleranceForSystem_g.at(simulationName) },
146                 { interaction_function[F_EKIN].longname, energyToleranceForSystem_g.at(simulationName) },
147                 { interaction_function[F_PRES].longname, pressureToleranceForSystem_g.at(simulationName) },
148         } };
149         TestReferenceData    refData;
150         auto                 checker = refData.rootChecker()
151                                .checkCompound("Simulation", simulationName)
152                                .checkCompound("Mdrun", integrator);
153         checkEnergiesAgainstReferenceData(runner_.edrFileName_, energyTermsToCompare, &checker);
154         // Now check the forces
155         TrajectoryFrameReader reader(runner_.fullPrecisionTrajectoryFileName_);
156         checker.setDefaultTolerance(relativeToleranceAsFloatingPoint(1, 1e-4));
157         do
158         {
159             auto frame = reader.frame();
160             auto force = frame.f();
161             int  atom  = 0;
162             for (auto& f : force)
163             {
164                 std::string forceName = frame.frameName() + " F[" + toString(atom) + "]";
165 
166                 checker.checkVector(f, forceName.c_str());
167                 atom++;
168             }
169         } while (reader.readNextFrame());
170     }
171 }
172 
173 // The time for OpenCL kernel compilation means these tests might time
174 // out. If that proves to be a problem, these can be disabled for
175 // OpenCL builds. However, once that compilation is cached for the
176 // lifetime of the whole test binary process, these tests should run in
177 // such configurations.
178 #if GMX_DOUBLE
179 
180 //! Containers of systems to test.
181 //! \{
182 std::vector<std::string> systemsToTest_g = { "angles1" };
183 std::vector<std::string> md_g            = { "md", "md-vv" };
184 //! \}
185 
186 INSTANTIATE_TEST_CASE_P(Angles1,
187                         SimpleMdrunTest,
188                         ::testing::Combine(::testing::ValuesIn(systemsToTest_g), ::testing::ValuesIn(md_g)));
189 #endif
190 } // namespace
191 } // namespace test
192 } // namespace gmx
193