a4e4aa37b532b3829d53afa1f89edc5929c0eda7
[alexxy/gromacs.git] / src / programs / mdrun / tests / pmetest.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2017,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 /*! \internal \file
36  * \brief
37  * This implements basic PME sanity tests.
38  * It runs the input system with PME for several steps (on CPU and GPU, if available),
39  * and checks the reciprocal and conserved energies.
40  * As part of mdrun-test, this will always run single rank PME simulation.
41  * As part of mdrun-mpi-test, this will run same as above when a single rank is requested,
42  * or a simulation with a single separate PME rank ("-npme 1") when multiple ranks are requested.
43  * \todo Extend and generalize this for more multi-rank tests (-npme 0, -npme 2, etc).
44  * \todo Implement death tests (e.g. for PME GPU decomposition).
45  *
46  * \author Aleksei Iupinov <a.yupinov@gmail.com>
47  * \ingroup module_mdrun_integration_tests
48  */
49 #include "gmxpre.h"
50
51 #include <map>
52 #include <string>
53 #include <vector>
54
55 #include <gtest/gtest-spi.h>
56
57 #include "gromacs/ewald/pme.h"
58 #include "gromacs/hardware/detecthardware.h"
59 #include "gromacs/hardware/device_management.h"
60 #include "gromacs/hardware/hw_info.h"
61 #include "gromacs/trajectory/energyframe.h"
62 #include "gromacs/utility/cstringutil.h"
63 #include "gromacs/utility/gmxmpi.h"
64 #include "gromacs/utility/physicalnodecommunicator.h"
65 #include "gromacs/utility/stringutil.h"
66
67 #include "testutils/mpitest.h"
68 #include "testutils/refdata.h"
69
70 #include "energyreader.h"
71 #include "moduletest.h"
72
73 namespace gmx
74 {
75 namespace test
76 {
77 namespace
78 {
79
80 /*! \brief A basic PME runner
81  *
82  * \todo Consider also using GpuTest class. */
83 class PmeTest : public MdrunTestFixture
84 {
85 public:
86     //! Convenience typedef
87     using RunModesList = std::map<std::string, std::vector<const char*>>;
88     //! Runs the test with the given inputs
89     void runTest(const RunModesList& runModes);
90 };
91
92 void PmeTest::runTest(const RunModesList& runModes)
93 {
94     const std::string inputFile = "spc-and-methanol";
95     runner_.useTopGroAndNdxFromDatabase(inputFile);
96
97     // With single rank we can and will always test PP+PME as part of mdrun-test.
98     // With multiple ranks we can additionally test a single PME-only rank within mdrun-mpi-test.
99     const bool parallelRun    = (getNumberOfTestMpiRanks() > 1);
100     const bool useSeparatePme = parallelRun;
101
102     EXPECT_EQ(0, runner_.callGrompp());
103
104     TestReferenceData    refData;
105     TestReferenceChecker rootChecker(refData.rootChecker());
106     const bool           thisRankChecks = (gmx_node_rank() == 0);
107     if (!thisRankChecks)
108     {
109         EXPECT_NONFATAL_FAILURE(rootChecker.checkUnusedEntries(), ""); // skip checks on other ranks
110     }
111
112     auto hardwareInfo_ =
113             gmx_detect_hardware(PhysicalNodeCommunicator(MPI_COMM_WORLD, gmx_physicalnode_id_hash()));
114
115     for (const auto& mode : runModes)
116     {
117         SCOPED_TRACE("mdrun " + joinStrings(mode.second, " "));
118         auto modeTargetsGpus = (mode.first.find("Gpu") != std::string::npos);
119         if (modeTargetsGpus && getCompatibleDevices(hardwareInfo_->deviceInfoList).empty())
120         {
121             // This run mode will cause a fatal error from mdrun when
122             // it can't find GPUs, which is not something we're trying
123             // to test here.
124             continue;
125         }
126         auto modeTargetsPmeOnGpus = (mode.first.find("PmeOnGpu") != std::string::npos);
127         if (modeTargetsPmeOnGpus
128             && !(pme_gpu_supports_build(nullptr) && pme_gpu_supports_hardware(*hardwareInfo_, nullptr)))
129         {
130             // This run mode will cause a fatal error from mdrun when
131             // it finds an unsuitable device, which is not something
132             // we're trying to test here.
133             continue;
134         }
135
136         runner_.edrFileName_ =
137                 fileManager_.getTemporaryFilePath(inputFile + "_" + mode.first + ".edr");
138
139         CommandLine commandLine(mode.second);
140
141         const bool usePmeTuning = (mode.first.find("Tune") != std::string::npos);
142         if (usePmeTuning)
143         {
144             commandLine.append("-tunepme");
145             commandLine.addOption("-nstlist", 1); // a new grid every step
146         }
147         else
148         {
149             commandLine.append("-notunepme"); // for reciprocal energy reproducibility
150         }
151         if (useSeparatePme)
152         {
153             commandLine.addOption("-npme", 1);
154         }
155
156         ASSERT_EQ(0, runner_.callMdrun(commandLine));
157
158         if (thisRankChecks)
159         {
160             auto energyReader = openEnergyFileToReadTerms(
161                     runner_.edrFileName_, { "Coul. recip.", "Total Energy", "Kinetic En." });
162             auto conservedChecker  = rootChecker.checkCompound("Energy", "Conserved");
163             auto reciprocalChecker = rootChecker.checkCompound("Energy", "Reciprocal");
164             bool firstIteration    = true;
165             while (energyReader->readNextFrame())
166             {
167                 const EnergyFrame& frame            = energyReader->frame();
168                 const std::string  stepName         = frame.frameName();
169                 const real         conservedEnergy  = frame.at("Total Energy");
170                 const real         reciprocalEnergy = frame.at("Coul. recip.");
171                 if (firstIteration)
172                 {
173                     // use first step values as references for tolerance
174                     const real startingKineticEnergy = frame.at("Kinetic En.");
175                     const auto conservedTolerance =
176                             relativeToleranceAsFloatingPoint(startingKineticEnergy, 2e-5);
177                     const auto reciprocalTolerance =
178                             relativeToleranceAsFloatingPoint(reciprocalEnergy, 3e-5);
179                     reciprocalChecker.setDefaultTolerance(reciprocalTolerance);
180                     conservedChecker.setDefaultTolerance(conservedTolerance);
181                     firstIteration = false;
182                 }
183                 conservedChecker.checkReal(conservedEnergy, stepName.c_str());
184                 if (!usePmeTuning) // with PME tuning come differing grids and differing reciprocal energy
185                 {
186                     reciprocalChecker.checkReal(reciprocalEnergy, stepName.c_str());
187                 }
188             }
189         }
190     }
191 }
192
193 TEST_F(PmeTest, ReproducesEnergies)
194 {
195     const int         nsteps     = 20;
196     const std::string theMdpFile = formatString(
197             "coulombtype     = PME\n"
198             "nstcalcenergy   = 1\n"
199             "nstenergy       = 1\n"
200             "pme-order       = 4\n"
201             "nsteps          = %d\n",
202             nsteps);
203
204     runner_.useStringAsMdpFile(theMdpFile);
205
206     // TODO test all proper/improper combinations in more thorough way?
207     RunModesList runModes;
208     runModes["PmeOnCpu"]         = { "-pme", "cpu" };
209     runModes["PmeAuto"]          = { "-pme", "auto" };
210     runModes["PmeOnGpuFftOnCpu"] = { "-pme", "gpu", "-pmefft", "cpu" };
211     runModes["PmeOnGpuFftOnGpu"] = { "-pme", "gpu", "-pmefft", "gpu" };
212     runModes["PmeOnGpuFftAuto"]  = { "-pme", "gpu", "-pmefft", "auto" };
213     // same manual modes but marked for PME tuning
214     runModes["PmeOnCpuTune"]         = { "-pme", "cpu" };
215     runModes["PmeOnGpuFftOnCpuTune"] = { "-pme", "gpu", "-pmefft", "cpu" };
216     runModes["PmeOnGpuFftOnGpuTune"] = { "-pme", "gpu", "-pmefft", "gpu" };
217
218     runTest(runModes);
219 }
220
221 TEST_F(PmeTest, ScalesTheBox)
222 {
223     const int         nsteps     = 0;
224     const std::string theMdpFile = formatString(
225             "coulombtype     = PME\n"
226             "nstcalcenergy   = 1\n"
227             "nstenergy       = 1\n"
228             "pme-order       = 4\n"
229             "pbc             = xyz\n"
230             "nsteps          = %d\n",
231             nsteps);
232
233     runner_.useStringAsMdpFile(theMdpFile);
234
235     RunModesList runModes;
236     runModes["PmeOnCpu"]         = { "-pme", "cpu" };
237     runModes["PmeOnGpuFftOnCpu"] = { "-pme", "gpu", "-pmefft", "cpu" };
238     runModes["PmeOnGpuFftOnGpu"] = { "-pme", "gpu", "-pmefft", "gpu" };
239
240     runTest(runModes);
241 }
242
243 TEST_F(PmeTest, ScalesTheBoxWithWalls)
244 {
245     const int         nsteps     = 0;
246     const std::string theMdpFile = formatString(
247             "coulombtype     = PME\n"
248             "nstcalcenergy   = 1\n"
249             "nstenergy       = 1\n"
250             "pme-order       = 4\n"
251             "pbc             = xy\n"
252             "nwall           = 2\n"
253             "ewald-geometry  = 3dc\n"
254             "wall_atomtype   = CMet H\n"
255             "wall_density    = 9 9.0\n"
256             "wall-ewald-zfac = 5\n"
257             "nsteps          = %d\n",
258             nsteps);
259
260     runner_.useStringAsMdpFile(theMdpFile);
261
262     RunModesList runModes;
263     runModes["PmeOnCpu"]         = { "-pme", "cpu" };
264     runModes["PmeOnGpuFftOnCpu"] = { "-pme", "gpu", "-pmefft", "cpu" };
265     runModes["PmeOnGpuFftOnGpu"] = { "-pme", "gpu", "-pmefft", "gpu" };
266
267     runTest(runModes);
268 }
269
270 } // namespace
271 } // namespace test
272 } // namespace gmx