Simplified uniform GPU selection in CMake
[alexxy/gromacs.git] / src / programs / mdrun / tests / simulator.cpp
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  * Tests to compare two simulators which are expected to be identical
39  *
40  * \author Mark Abraham <mark.j.abraham@gmail.com>
41  * \author Pascal Merz <pascal.merz@me.com>
42  * \ingroup module_mdrun_integration_tests
43  */
44 #include "gmxpre.h"
45
46 #include "config.h"
47
48 #include "gromacs/topology/ifunc.h"
49 #include "gromacs/utility/stringutil.h"
50
51 #include "testutils/mpitest.h"
52 #include "testutils/setenv.h"
53 #include "testutils/simulationdatabase.h"
54
55 #include "moduletest.h"
56 #include "simulatorcomparison.h"
57
58 namespace gmx
59 {
60 namespace test
61 {
62 namespace
63 {
64
65 /*! \brief Test fixture base for two equivalent simulators
66  *
67  * This test ensures that two simulator code paths (called via different mdp
68  * options and/or environment variables) yield identical coordinate, velocity,
69  * box, force and energy trajectories, up to some (arbitrary) precision.
70  *
71  * These tests are useful to check that re-implementations of existing simulators
72  * are correct, and that different code paths expected to yield identical results
73  * are equivalent.
74  */
75 using SimulatorComparisonTestParams =
76         std::tuple<std::tuple<std::string, std::string, std::string, std::string>, std::string>;
77 class SimulatorComparisonTest :
78     public MdrunTestFixture,
79     public ::testing::WithParamInterface<SimulatorComparisonTestParams>
80 {
81 };
82
83 TEST_P(SimulatorComparisonTest, WithinTolerances)
84 {
85     auto params              = GetParam();
86     auto mdpParams           = std::get<0>(params);
87     auto simulationName      = std::get<0>(mdpParams);
88     auto integrator          = std::get<1>(mdpParams);
89     auto tcoupling           = std::get<2>(mdpParams);
90     auto pcoupling           = std::get<3>(mdpParams);
91     auto environmentVariable = std::get<1>(params);
92
93     // TODO At some point we should also test PME-only ranks.
94     int numRanksAvailable = getNumberOfTestMpiRanks();
95     if (!isNumberOfPpRanksSupported(simulationName, numRanksAvailable))
96     {
97         fprintf(stdout,
98                 "Test system '%s' cannot run with %d ranks.\n"
99                 "The supported numbers are: %s\n",
100                 simulationName.c_str(), numRanksAvailable,
101                 reportNumbersOfPpRanksSupported(simulationName).c_str());
102         return;
103     }
104
105     if (integrator == "md-vv" && pcoupling == "Parrinello-Rahman")
106     {
107         // do_md calls this MTTK, requires Nose-Hoover, and
108         // does not work with constraints or anisotropically
109         return;
110     }
111
112     SCOPED_TRACE(formatString(
113             "Comparing two simulations of '%s' "
114             "with integrator '%s' and '%s' temperature coupling, "
115             "switching environment variable '%s'",
116             simulationName.c_str(), integrator.c_str(), tcoupling.c_str(), environmentVariable.c_str()));
117
118     auto mdpFieldValues = prepareMdpFieldValues(simulationName.c_str(), integrator.c_str(),
119                                                 tcoupling.c_str(), pcoupling.c_str());
120
121     EnergyTermsToCompare energyTermsToCompare{ {
122             { interaction_function[F_EPOT].longname, relativeToleranceAsPrecisionDependentUlp(10.0, 100, 80) },
123             { interaction_function[F_EKIN].longname, relativeToleranceAsPrecisionDependentUlp(60.0, 100, 80) },
124             { interaction_function[F_PRES].longname,
125               relativeToleranceAsPrecisionDependentFloatingPoint(10.0, 0.01, 0.001) },
126     } };
127
128     if (simulationName == "argon12")
129     {
130         // Without constraints, we can be more strict
131         energyTermsToCompare = { {
132                 { interaction_function[F_EPOT].longname,
133                   relativeToleranceAsPrecisionDependentUlp(10.0, 24, 80) },
134                 { interaction_function[F_EKIN].longname,
135                   relativeToleranceAsPrecisionDependentUlp(10.0, 24, 80) },
136                 { interaction_function[F_PRES].longname,
137                   relativeToleranceAsPrecisionDependentFloatingPoint(10.0, 0.001, 0.0001) },
138         } };
139     }
140
141     // Specify how trajectory frame matching must work.
142     TrajectoryFrameMatchSettings trajectoryMatchSettings{ true,
143                                                           true,
144                                                           true,
145                                                           ComparisonConditions::MustCompare,
146                                                           ComparisonConditions::MustCompare,
147                                                           ComparisonConditions::MustCompare };
148     TrajectoryTolerances trajectoryTolerances = TrajectoryComparison::s_defaultTrajectoryTolerances;
149     if (simulationName != "argon12")
150     {
151         trajectoryTolerances.velocities = trajectoryTolerances.coordinates;
152     }
153
154     // Build the functor that will compare reference and test
155     // trajectory frames in the chosen way.
156     TrajectoryComparison trajectoryComparison{ trajectoryMatchSettings, trajectoryTolerances };
157
158     // Set file names
159     auto simulator1TrajectoryFileName = fileManager_.getTemporaryFilePath("sim1.trr");
160     auto simulator1EdrFileName        = fileManager_.getTemporaryFilePath("sim1.edr");
161     auto simulator2TrajectoryFileName = fileManager_.getTemporaryFilePath("sim2.trr");
162     auto simulator2EdrFileName        = fileManager_.getTemporaryFilePath("sim2.edr");
163
164     // Run grompp
165     runner_.tprFileName_ = fileManager_.getTemporaryFilePath("sim.tpr");
166     runner_.useTopGroAndNdxFromDatabase(simulationName);
167     runner_.useStringAsMdpFile(prepareMdpFileContents(mdpFieldValues));
168     runGrompp(&runner_);
169
170     // Backup current state of environment variable and unset it
171     char* environmentVariableBackup = getenv(environmentVariable.c_str());
172     gmxUnsetenv(environmentVariable.c_str());
173
174     // Do first mdrun
175     runner_.fullPrecisionTrajectoryFileName_ = simulator1TrajectoryFileName;
176     runner_.edrFileName_                     = simulator1EdrFileName;
177     runMdrun(&runner_);
178
179     // Set environment variable
180     const int overWriteEnvironmentVariable = 1;
181     gmxSetenv(environmentVariable.c_str(), "ON", overWriteEnvironmentVariable);
182
183     // Do second mdrun
184     runner_.fullPrecisionTrajectoryFileName_ = simulator2TrajectoryFileName;
185     runner_.edrFileName_                     = simulator2EdrFileName;
186     runMdrun(&runner_);
187
188     // Reset or unset environment variable to leave further tests undisturbed
189     if (environmentVariableBackup != nullptr)
190     {
191         // set environment variable
192         gmxSetenv(environmentVariable.c_str(), environmentVariableBackup, overWriteEnvironmentVariable);
193     }
194     else
195     {
196         // unset environment variable
197         gmxUnsetenv(environmentVariable.c_str());
198     }
199
200     // Compare simulation results
201     compareEnergies(simulator1EdrFileName, simulator2EdrFileName, energyTermsToCompare);
202     compareTrajectories(simulator1TrajectoryFileName, simulator2TrajectoryFileName, trajectoryComparison);
203 }
204
205 // TODO: The time for OpenCL kernel compilation means these tests time
206 //       out. Once that compilation is cached for the whole process, these
207 //       tests can run in such configurations.
208 // These tests are very sensitive, so we only run them in double precision.
209 // As we change call ordering, they might actually become too strict to be useful.
210 #if !GMX_GPU_OPENCL && GMX_DOUBLE
211 INSTANTIATE_TEST_CASE_P(SimulatorsAreEquivalentDefaultModular,
212                         SimulatorComparisonTest,
213                         ::testing::Combine(::testing::Combine(::testing::Values("argon12", "tip3p5"),
214                                                               ::testing::Values("md-vv"),
215                                                               ::testing::Values("no", "v-rescale"),
216                                                               ::testing::Values("no")),
217                                            ::testing::Values("GMX_DISABLE_MODULAR_SIMULATOR")));
218 INSTANTIATE_TEST_CASE_P(
219         SimulatorsAreEquivalentDefaultLegacy,
220         SimulatorComparisonTest,
221         ::testing::Combine(::testing::Combine(::testing::Values("argon12", "tip3p5"),
222                                               ::testing::Values("md"),
223                                               ::testing::Values("no", "v-rescale"),
224                                               ::testing::Values("no", "Parrinello-Rahman")),
225                            ::testing::Values("GMX_USE_MODULAR_SIMULATOR")));
226 #else
227 INSTANTIATE_TEST_CASE_P(DISABLED_SimulatorsAreEquivalentDefaultModular,
228                         SimulatorComparisonTest,
229                         ::testing::Combine(::testing::Combine(::testing::Values("argon12", "tip3p5"),
230                                                               ::testing::Values("md-vv"),
231                                                               ::testing::Values("no", "v-rescale"),
232                                                               ::testing::Values("no")),
233                                            ::testing::Values("GMX_DISABLE_MODULAR_SIMULATOR")));
234 INSTANTIATE_TEST_CASE_P(
235         DISABLED_SimulatorsAreEquivalentDefaultLegacy,
236         SimulatorComparisonTest,
237         ::testing::Combine(::testing::Combine(::testing::Values("argon12", "tip3p5"),
238                                               ::testing::Values("md"),
239                                               ::testing::Values("no", "v-rescale"),
240                                               ::testing::Values("no", "Parrinello-Rahman")),
241                            ::testing::Values("GMX_USE_MODULAR_SIMULATOR")));
242 #endif
243
244 } // namespace
245 } // namespace test
246 } // namespace gmx