Add grompp warning with use of GROMOS FF
[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, 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/gpu_utils/gpu_testutils.h"
59 #include "gromacs/hardware/detecthardware.h"
60 #include "gromacs/hardware/gpu_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/loggerbuilder.h"
65 #include "gromacs/utility/physicalnodecommunicator.h"
66 #include "gromacs/utility/stringutil.h"
67
68 #include "testutils/mpitest.h"
69 #include "testutils/refdata.h"
70
71 #include "energyreader.h"
72 #include "moduletest.h"
73
74 namespace gmx
75 {
76 namespace test
77 {
78 namespace
79 {
80
81 /*! \brief A basic PME runner
82  *
83  * \todo Consider also using GpuTest class. */
84 class PmeTest : public MdrunTestFixture
85 {
86     public:
87         //! Before any test is run, work out whether any compatible GPUs exist.
88         static void SetUpTestCase();
89         //! Store whether any compatible GPUs exist.
90         static bool s_hasCompatibleGpus;
91         //! Convenience typedef
92         using RunModesList = std::map < std::string, std::vector < const char *>>;
93         //! Runs the test with the given inputs
94         void runTest(const RunModesList &runModes);
95 };
96
97 bool PmeTest::s_hasCompatibleGpus = false;
98
99 void PmeTest::SetUpTestCase()
100 {
101     s_hasCompatibleGpus = canComputeOnGpu();
102 }
103
104 void PmeTest::runTest(const RunModesList &runModes)
105 {
106     const std::string inputFile = "spc-and-methanol";
107     runner_.useTopGroAndNdxFromDatabase(inputFile);
108
109     // With single rank we can and will always test PP+PME as part of mdrun-test.
110     // With multiple ranks we can additionally test a single PME-only rank within mdrun-mpi-test.
111     const bool parallelRun    = (getNumberOfTestMpiRanks() > 1);
112     const bool useSeparatePme = parallelRun;
113
114     EXPECT_EQ(0, runner_.callGrompp());
115
116     TestReferenceData    refData;
117     TestReferenceChecker rootChecker(refData.rootChecker());
118     const bool           thisRankChecks = (gmx_node_rank() == 0);
119     if (!thisRankChecks)
120     {
121         EXPECT_NONFATAL_FAILURE(rootChecker.checkUnusedEntries(), ""); // skip checks on other ranks
122     }
123
124     auto hardwareInfo_ = gmx_detect_hardware(MDLogger {},
125                                              PhysicalNodeCommunicator(MPI_COMM_WORLD,
126                                                                       gmx_physicalnode_id_hash()));
127
128     for (const auto &mode : runModes)
129     {
130         auto modeTargetsGpus = (mode.first.find("Gpu") != std::string::npos);
131         if (modeTargetsGpus && !s_hasCompatibleGpus)
132         {
133             // This run mode will cause a fatal error from mdrun when
134             // it can't find GPUs, which is not something we're trying
135             // to test here.
136             continue;
137         }
138         auto modeTargetsPmeOnGpus = (mode.first.find("PmeOnGpu") != std::string::npos);
139         if (modeTargetsPmeOnGpus &&
140             !(pme_gpu_supports_build(nullptr) &&
141               pme_gpu_supports_hardware(*hardwareInfo_, nullptr)))
142         {
143             // This run mode will cause a fatal error from mdrun when
144             // it finds an unsuitable device, which is not something
145             // we're trying to test here.
146             continue;
147         }
148
149         runner_.edrFileName_ = fileManager_.getTemporaryFilePath(inputFile + "_" + mode.first + ".edr");
150
151         CommandLine commandLine(mode.second);
152
153         const bool  usePmeTuning = (mode.first.find("Tune") != std::string::npos);
154         if (usePmeTuning)
155         {
156             commandLine.append("-tunepme");
157             commandLine.addOption("-nstlist", 1); // a new grid every step
158         }
159         else
160         {
161             commandLine.append("-notunepme"); // for reciprocal energy reproducibility
162         }
163         if (useSeparatePme)
164         {
165             commandLine.addOption("-npme", 1);
166         }
167
168         ASSERT_EQ(0, runner_.callMdrun(commandLine));
169
170         if (thisRankChecks)
171         {
172             auto energyReader      = openEnergyFileToReadFields(runner_.edrFileName_, {"Coul. recip.", "Total Energy", "Kinetic En."});
173             auto conservedChecker  = rootChecker.checkCompound("Energy", "Conserved");
174             auto reciprocalChecker = rootChecker.checkCompound("Energy", "Reciprocal");
175             bool firstIteration    = true;
176             while (energyReader->readNextFrame())
177             {
178                 const EnergyFrame &frame            = energyReader->frame();
179                 const std::string  stepName         = frame.frameName();
180                 const real         conservedEnergy  = frame.at("Total Energy");
181                 const real         reciprocalEnergy = frame.at("Coul. recip.");
182                 if (firstIteration)
183                 {
184                     // use first step values as references for tolerance
185                     const real startingKineticEnergy = frame.at("Kinetic En.");
186                     const auto conservedTolerance    = relativeToleranceAsFloatingPoint(startingKineticEnergy, 2e-5);
187                     const auto reciprocalTolerance   = relativeToleranceAsFloatingPoint(reciprocalEnergy, 3e-5);
188                     reciprocalChecker.setDefaultTolerance(reciprocalTolerance);
189                     conservedChecker.setDefaultTolerance(conservedTolerance);
190                     firstIteration = false;
191                 }
192                 conservedChecker.checkReal(conservedEnergy, stepName.c_str());
193                 if (!usePmeTuning) // with PME tuning come differing grids and differing reciprocal energy
194                 {
195                     reciprocalChecker.checkReal(reciprocalEnergy, stepName.c_str());
196                 }
197             }
198         }
199     }
200 }
201
202 TEST_F(PmeTest, ReproducesEnergies)
203 {
204     const int         nsteps     = 20;
205     const std::string theMdpFile = formatString("coulombtype     = PME\n"
206                                                 "nstcalcenergy   = 1\n"
207                                                 "nstenergy       = 1\n"
208                                                 "pme-order       = 4\n"
209                                                 "nsteps          = %d\n",
210                                                 nsteps
211                                                 );
212
213     runner_.useStringAsMdpFile(theMdpFile);
214
215     //TODO test all proper/improper combinations in more thorough way?
216     RunModesList runModes;
217     runModes["PmeOnCpu"]         = {"-pme", "cpu"};
218     runModes["PmeAuto"]          = {"-pme", "auto"};
219     runModes["PmeOnGpuFftOnCpu"] = {"-pme", "gpu", "-pmefft", "cpu"};
220     runModes["PmeOnGpuFftOnGpu"] = {"-pme", "gpu", "-pmefft", "gpu"};
221     runModes["PmeOnGpuFftAuto"]  = {"-pme", "gpu", "-pmefft", "auto"};
222     // same manual modes but marked for PME tuning
223     runModes["PmeOnCpuTune"]         = {"-pme", "cpu"};
224     runModes["PmeOnGpuFftOnCpuTune"] = {"-pme", "gpu", "-pmefft", "cpu"};
225     runModes["PmeOnGpuFftOnGpuTune"] = {"-pme", "gpu", "-pmefft", "gpu"};
226
227     runTest(runModes);
228 }
229
230 TEST_F(PmeTest, ScalesTheBox)
231 {
232     const int         nsteps     = 0;
233     const std::string theMdpFile = formatString("coulombtype     = PME\n"
234                                                 "nstcalcenergy   = 1\n"
235                                                 "nstenergy       = 1\n"
236                                                 "pme-order       = 4\n"
237                                                 "pbc             = xyz\n"
238                                                 "nsteps          = %d\n",
239                                                 nsteps
240                                                 );
241
242     runner_.useStringAsMdpFile(theMdpFile);
243
244     RunModesList runModes;
245     runModes["PmeOnCpu"]         = {"-pme", "cpu"};
246     runModes["PmeOnGpuFftOnCpu"] = {"-pme", "gpu", "-pmefft", "cpu"};
247     runModes["PmeOnGpuFftOnGpu"] = {"-pme", "gpu", "-pmefft", "gpu"};
248
249     runTest(runModes);
250 }
251
252 TEST_F(PmeTest, ScalesTheBoxWithWalls)
253 {
254     const int         nsteps     = 0;
255     const std::string theMdpFile = formatString("coulombtype     = PME\n"
256                                                 "nstcalcenergy   = 1\n"
257                                                 "nstenergy       = 1\n"
258                                                 "pme-order       = 4\n"
259                                                 "pbc             = xy\n"
260                                                 "nwall           = 2\n"
261                                                 "ewald-geometry  = 3dc\n"
262                                                 "wall_atomtype   = CMet H\n"
263                                                 "wall_density    = 9 9.0\n"
264                                                 "wall-ewald-zfac = 5\n"
265                                                 "nsteps          = %d\n",
266                                                 nsteps
267                                                 );
268
269     runner_.useStringAsMdpFile(theMdpFile);
270
271     RunModesList runModes;
272     runModes["PmeOnCpu"]         = {"-pme", "cpu"};
273     runModes["PmeOnGpuFftOnCpu"] = {"-pme", "gpu", "-pmefft", "cpu"};
274     runModes["PmeOnGpuFftOnGpu"] = {"-pme", "gpu", "-pmefft", "gpu"};
275
276     runTest(runModes);
277 }
278
279 }  // namespace
280 }  // namespace test
281 }  // namespace gmx