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