2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
38 * Tests to compare two simulators which are expected to be identical
40 * \author Mark Abraham <mark.j.abraham@gmail.com>
41 * \author Pascal Merz <pascal.merz@me.com>
42 * \ingroup module_mdrun_integration_tests
48 #include "gromacs/topology/ifunc.h"
49 #include "gromacs/utility/stringutil.h"
51 #include "testutils/mpitest.h"
52 #include "testutils/setenv.h"
53 #include "testutils/simulationdatabase.h"
55 #include "moduletest.h"
56 #include "simulatorcomparison.h"
65 /*! \brief Test fixture base for two equivalent simulators
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.
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
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>
83 TEST_P(SimulatorComparisonTest, WithinTolerances)
85 const auto& params = GetParam();
86 const auto& mdpParams = std::get<0>(params);
87 const auto& simulationName = std::get<0>(mdpParams);
88 const auto& integrator = std::get<1>(mdpParams);
89 const auto& tcoupling = std::get<2>(mdpParams);
90 const auto& pcoupling = std::get<3>(mdpParams);
91 const auto& environmentVariable = std::get<1>(params);
93 // TODO At some point we should also test PME-only ranks.
94 const int numRanksAvailable = getNumberOfTestMpiRanks();
95 if (!isNumberOfPpRanksSupported(simulationName, numRanksAvailable))
98 "Test system '%s' cannot run with %d ranks.\n"
99 "The supported numbers are: %s\n",
100 simulationName.c_str(),
102 reportNumbersOfPpRanksSupported(simulationName).c_str());
106 if (integrator == "md-vv" && pcoupling == "Parrinello-Rahman")
108 // do_md calls this MTTK, requires Nose-Hoover, and
109 // does not work with constraints or anisotropically
113 const auto hasConservedField = !(tcoupling == "no" && pcoupling == "no");
115 SCOPED_TRACE(formatString(
116 "Comparing two simulations of '%s' "
117 "with integrator '%s', '%s' temperature coupling, and '%s' pressure coupling "
118 "switching environment variable '%s'",
119 simulationName.c_str(),
123 environmentVariable.c_str()));
125 const auto mdpFieldValues = prepareMdpFieldValues(
126 simulationName.c_str(), integrator.c_str(), tcoupling.c_str(), pcoupling.c_str());
128 EnergyTermsToCompare energyTermsToCompare{ {
129 { interaction_function[F_EPOT].longname, relativeToleranceAsPrecisionDependentUlp(10.0, 100, 80) },
130 { interaction_function[F_EKIN].longname, relativeToleranceAsPrecisionDependentUlp(60.0, 100, 80) },
131 { interaction_function[F_PRES].longname,
132 relativeToleranceAsPrecisionDependentFloatingPoint(10.0, 0.01, 0.001) },
134 if (hasConservedField)
136 energyTermsToCompare.emplace(interaction_function[F_ECONSERVED].longname,
137 relativeToleranceAsPrecisionDependentUlp(50.0, 100, 80));
140 if (simulationName == "argon12")
142 // Without constraints, we can be more strict
143 energyTermsToCompare = { {
144 { interaction_function[F_EPOT].longname,
145 relativeToleranceAsPrecisionDependentUlp(10.0, 24, 80) },
146 { interaction_function[F_EKIN].longname,
147 relativeToleranceAsPrecisionDependentUlp(10.0, 24, 80) },
148 { interaction_function[F_PRES].longname,
149 relativeToleranceAsPrecisionDependentFloatingPoint(10.0, 0.001, 0.0001) },
151 if (hasConservedField)
153 energyTermsToCompare.emplace(interaction_function[F_ECONSERVED].longname,
154 relativeToleranceAsPrecisionDependentUlp(10.0, 24, 80));
158 // Specify how trajectory frame matching must work.
159 const TrajectoryFrameMatchSettings trajectoryMatchSettings{ true,
162 ComparisonConditions::MustCompare,
163 ComparisonConditions::MustCompare,
164 ComparisonConditions::MustCompare,
165 MaxNumFrames::compareAllFrames() };
166 TrajectoryTolerances trajectoryTolerances = TrajectoryComparison::s_defaultTrajectoryTolerances;
167 if (simulationName != "argon12")
169 trajectoryTolerances.velocities = trajectoryTolerances.coordinates;
172 // Build the functor that will compare reference and test
173 // trajectory frames in the chosen way.
174 const TrajectoryComparison trajectoryComparison{ trajectoryMatchSettings, trajectoryTolerances };
177 const auto simulator1TrajectoryFileName = fileManager_.getTemporaryFilePath("sim1.trr");
178 const auto simulator1EdrFileName = fileManager_.getTemporaryFilePath("sim1.edr");
179 const auto simulator2TrajectoryFileName = fileManager_.getTemporaryFilePath("sim2.trr");
180 const auto simulator2EdrFileName = fileManager_.getTemporaryFilePath("sim2.edr");
183 runner_.tprFileName_ = fileManager_.getTemporaryFilePath("sim.tpr");
184 runner_.useTopGroAndNdxFromDatabase(simulationName);
185 runner_.useStringAsMdpFile(prepareMdpFileContents(mdpFieldValues));
188 // Backup current state of environment variable and unset it
189 const char* environmentVariableBackup = getenv(environmentVariable.c_str());
190 gmxUnsetenv(environmentVariable.c_str());
193 runner_.fullPrecisionTrajectoryFileName_ = simulator1TrajectoryFileName;
194 runner_.edrFileName_ = simulator1EdrFileName;
197 // Set environment variable
198 const int overWriteEnvironmentVariable = 1;
199 gmxSetenv(environmentVariable.c_str(), "ON", overWriteEnvironmentVariable);
202 runner_.fullPrecisionTrajectoryFileName_ = simulator2TrajectoryFileName;
203 runner_.edrFileName_ = simulator2EdrFileName;
206 // Reset or unset environment variable to leave further tests undisturbed
207 if (environmentVariableBackup != nullptr)
209 // set environment variable
210 gmxSetenv(environmentVariable.c_str(), environmentVariableBackup, overWriteEnvironmentVariable);
214 // unset environment variable
215 gmxUnsetenv(environmentVariable.c_str());
218 // Compare simulation results
219 compareEnergies(simulator1EdrFileName, simulator2EdrFileName, energyTermsToCompare);
220 compareTrajectories(simulator1TrajectoryFileName, simulator2TrajectoryFileName, trajectoryComparison);
223 // TODO: The time for OpenCL kernel compilation means these tests time
224 // out. Once that compilation is cached for the whole process, these
225 // tests can run in such configurations.
226 // These tests are very sensitive, so we only run them in double precision.
227 // As we change call ordering, they might actually become too strict to be useful.
228 #if !GMX_GPU_OPENCL && GMX_DOUBLE
229 INSTANTIATE_TEST_CASE_P(
230 SimulatorsAreEquivalentDefaultModular,
231 SimulatorComparisonTest,
232 ::testing::Combine(::testing::Combine(::testing::Values("argon12", "tip3p5"),
233 ::testing::Values("md-vv"),
234 ::testing::Values("no", "v-rescale", "berendsen"),
235 ::testing::Values("no")),
236 ::testing::Values("GMX_DISABLE_MODULAR_SIMULATOR")));
237 INSTANTIATE_TEST_CASE_P(
238 SimulatorsAreEquivalentDefaultLegacy,
239 SimulatorComparisonTest,
240 ::testing::Combine(::testing::Combine(::testing::Values("argon12", "tip3p5"),
241 ::testing::Values("md"),
242 ::testing::Values("no", "v-rescale", "berendsen"),
243 ::testing::Values("no", "Parrinello-Rahman")),
244 ::testing::Values("GMX_USE_MODULAR_SIMULATOR")));
246 INSTANTIATE_TEST_CASE_P(
247 DISABLED_SimulatorsAreEquivalentDefaultModular,
248 SimulatorComparisonTest,
249 ::testing::Combine(::testing::Combine(::testing::Values("argon12", "tip3p5"),
250 ::testing::Values("md-vv"),
251 ::testing::Values("no", "v-rescale", "berendsen"),
252 ::testing::Values("no")),
253 ::testing::Values("GMX_DISABLE_MODULAR_SIMULATOR")));
254 INSTANTIATE_TEST_CASE_P(
255 DISABLED_SimulatorsAreEquivalentDefaultLegacy,
256 SimulatorComparisonTest,
257 ::testing::Combine(::testing::Combine(::testing::Values("argon12", "tip3p5"),
258 ::testing::Values("md"),
259 ::testing::Values("no", "v-rescale", "berendsen"),
260 ::testing::Values("no", "Parrinello-Rahman")),
261 ::testing::Values("GMX_USE_MODULAR_SIMULATOR")));