b91d71d543ce95310a1099cd2a6550642adad196
[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, 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 test (using single-rank mdrun).
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  * TODO: implement multi-rank tests as well.
41  *
42  * \author Aleksei Iupinov <a.yupinov@gmail.com>
43  * \ingroup module_mdrun_integration_tests
44  */
45 #include "gmxpre.h"
46
47 #include "config.h"
48
49 #include <map>
50 #include <string>
51 #include <vector>
52
53 #include "gromacs/gpu_utils/gpu_utils.h"
54 #include "gromacs/hardware/gpu_hw_info.h"
55 #include "gromacs/utility/cstringutil.h"
56 #include "gromacs/utility/stringutil.h"
57
58 #include "testutils/refdata.h"
59
60 #include "energyreader.h"
61 #include "moduletest.h"
62
63 namespace gmx
64 {
65 namespace test
66 {
67 namespace
68 {
69
70 //! A basic PME runner
71 class PmeTest : public MdrunTestFixture
72 {
73     public:
74         //! Before any test is run, work out whether any compatible GPUs exist.
75         static void SetUpTestCase();
76         //! Store whether any compatible GPUs exist.
77         static bool s_hasCompatibleCudaGpus;
78 };
79
80 bool PmeTest::s_hasCompatibleCudaGpus = false;
81
82 void PmeTest::SetUpTestCase()
83 {
84     gmx_gpu_info_t gpuInfo {};
85     char           detection_error[STRLEN];
86     GMX_UNUSED_VALUE(detection_error); //TODO
87     // It would be nicer to do this detection once and have mdrun
88     // re-use it, but this is OK. Note that this also caters for when
89     // there is no GPU support in the build.
90     if (GMX_GPU == GMX_GPU_CUDA &&
91         (detect_gpus(&gpuInfo, detection_error) >= 0) &&
92         gpuInfo.n_dev_compatible > 0)
93     {
94         s_hasCompatibleCudaGpus = true;
95     }
96     free_gpu_info(&gpuInfo);
97 }
98
99 TEST_F(PmeTest, ReproducesEnergies)
100 {
101     const int   nsteps     = 20;
102     std::string theMdpFile = formatString("coulombtype     = PME\n"
103                                           "nstcalcenergy   = 1\n"
104                                           "nstenergy       = 1\n"
105                                           "pme-order       = 4\n"
106                                           "nsteps          = %d\n",
107                                           nsteps);
108
109     runner_.useStringAsMdpFile(theMdpFile);
110
111     const std::string inputFile = "spc-and-methanol";
112     runner_.useTopGroAndNdxFromDatabase(inputFile.c_str());
113
114     EXPECT_EQ(0, runner_.callGrompp());
115
116     //TODO test all proper/improper combinations in more thorough way?
117     std::map < std::string, std::vector < const char *>> runModes;
118     runModes["PmeOnCpu"]         = {"-pme", "cpu"};
119     runModes["PmeAuto"]          = {"-pme", "auto"};
120     runModes["PmeOnGpuFftOnCpu"] = {"-pme", "gpu", "-pmefft", "cpu"};
121     runModes["PmeOnGpuFftOnGpu"] = {"-pme", "gpu", "-pmefft", "gpu"};
122     runModes["PmeOnGpuFftAuto"]  = {"-pme", "gpu", "-pmefft", "auto"};
123     TestReferenceData    refData;
124     TestReferenceChecker rootChecker(refData.rootChecker());
125
126     for (const auto &mode : runModes)
127     {
128         auto modeTargetsGpus = (mode.first.find("Gpu") != std::string::npos);
129         if (modeTargetsGpus && !s_hasCompatibleCudaGpus)
130         {
131             // This run mode will cause a fatal error from mdrun when
132             // it can't find GPUs, which is not something we're trying
133             // to test here.
134             continue;
135         }
136
137         runner_.edrFileName_ = fileManager_.getTemporaryFilePath(inputFile + "_" + mode.first + ".edr");
138
139         CommandLine commandLine(mode.second);
140         commandLine.append("-notunepme"); // for reciprocal energy reproducibility
141         ASSERT_EQ(0, runner_.callMdrun(commandLine));
142
143         auto energyReader      = openEnergyFileToReadFields(runner_.edrFileName_, {"Coul. recip.", "Total Energy", "Kinetic En."});
144         auto conservedChecker  = rootChecker.checkCompound("Energy", "Conserved");
145         auto reciprocalChecker = rootChecker.checkCompound("Energy", "Reciprocal");
146         for (int i = 0; i <= nsteps; i++)
147         {
148             EnergyFrame frame            = energyReader->frame();
149             std::string stepNum          = gmx::formatString("%d", i);
150             const real  conservedEnergy  = frame.at("Total Energy");
151             const real  reciprocalEnergy = frame.at("Coul. recip.");
152             if (i == 0)
153             {
154                 // use first step values as references for tolerance
155                 const real startingKineticEnergy = frame.at("Kinetic En.");
156                 const auto conservedTolerance    = relativeToleranceAsFloatingPoint(startingKineticEnergy, 2e-5);
157                 const auto reciprocalTolerance   = relativeToleranceAsFloatingPoint(reciprocalEnergy, 2e-5);
158                 reciprocalChecker.setDefaultTolerance(reciprocalTolerance);
159                 conservedChecker.setDefaultTolerance(conservedTolerance);
160             }
161             conservedChecker.checkReal(conservedEnergy, stepNum.c_str());
162             reciprocalChecker.checkReal(reciprocalEnergy, stepNum.c_str());
163         }
164     }
165 }
166
167 }
168 }
169 }