Simplify energy-file comparisons in tests
[gromacs.git] / src / programs / mdrun / tests / simulatorcomparison.cpp
blob1383bf5546337c5d38f2e6b6eccd57e66f4d0136
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 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.
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.
35 /*! \internal \file
36 * \brief
37 * Helper classes for tests that compare the results of equivalent
38 * simulation runs. Currently used for the rerun and the simulator
39 * tests
41 * \author Mark Abraham <mark.j.abraham@gmail.com>
42 * \author Pascal Merz <pascal.merz@me.com>
43 * \ingroup module_mdrun_integration_tests
45 #include "gmxpre.h"
47 #include "testutils/mpitest.h"
48 #include "testutils/setenv.h"
49 #include "testutils/simulationdatabase.h"
51 #include "energycomparison.h"
52 #include "energyreader.h"
53 #include "mdruncomparison.h"
54 #include "moduletest.h"
55 #include "trajectorycomparison.h"
56 #include "trajectoryreader.h"
58 namespace gmx
60 namespace test
62 namespace
65 //! Run grompp and mdrun for both sets of mdp field values
66 template <bool doEnvironmentVariable, bool doRerun>
67 void executeSimulatorComparisonTestImpl(
68 TestFileManager *fileManager,
69 SimulationRunner *runner,
70 const std::string &simulationName,
71 int maxWarningsTolerated,
72 const MdpFieldValues &mdpFieldValues,
73 const EnergyTolerances &energiesToMatch,
74 const std::string &environmentVariable)
76 // TODO At some point we should also test PME-only ranks.
77 int numRanksAvailable = getNumberOfTestMpiRanks();
78 if (!isNumberOfPpRanksSupported(simulationName, numRanksAvailable))
80 fprintf(stdout, "Test system '%s' cannot run with %d ranks.\n"
81 "The supported numbers are: %s\n",
82 simulationName.c_str(), numRanksAvailable,
83 reportNumbersOfPpRanksSupported(simulationName).c_str());
84 return;
87 auto simulator1TrajectoryFileName = fileManager->getTemporaryFilePath("sim1.trr");
88 auto simulator1EdrFileName = fileManager->getTemporaryFilePath("sim1.edr");
89 auto simulator2TrajectoryFileName = fileManager->getTemporaryFilePath("sim2.trr");
90 auto simulator2EdrFileName = fileManager->getTemporaryFilePath("sim2.edr");
91 auto simulatorTprFileName = fileManager->getTemporaryFilePath("sim.tpr");
93 // prepare the .tpr file
95 // TODO evolve grompp to report the number of warnings issued, so
96 // tests always expect the right number.
97 CommandLine caller;
98 caller.append("grompp");
99 caller.addOption("-maxwarn", maxWarningsTolerated);
100 runner->tprFileName_ = simulatorTprFileName;
101 runner->useTopGroAndNdxFromDatabase(simulationName);
102 runner->useStringAsMdpFile(prepareMdpFileContents(mdpFieldValues));
103 EXPECT_EQ(0, runner->callGrompp(caller));
106 char *environmentVariableBackup = nullptr;
107 if (doEnvironmentVariable)
109 // save state of environment variable
110 environmentVariableBackup = getenv(environmentVariable.c_str());
113 // do the first mdrun
115 runner->fullPrecisionTrajectoryFileName_ = simulator1TrajectoryFileName;
116 runner->edrFileName_ = simulator1EdrFileName;
117 runner->tprFileName_ = simulatorTprFileName;
118 CommandLine simulator1Caller;
119 simulator1Caller.append("mdrun");
120 if (doEnvironmentVariable)
122 // unset environment variable
123 gmxUnsetenv(environmentVariable.c_str());
125 ASSERT_EQ(0, runner->callMdrun(simulator1Caller));
128 // do the second mdrun
130 runner->fullPrecisionTrajectoryFileName_ = simulator2TrajectoryFileName;
131 runner->edrFileName_ = simulator2EdrFileName;
132 runner->tprFileName_ = simulatorTprFileName;
133 CommandLine simulator2Caller;
134 simulator2Caller.append("mdrun");
135 if (doEnvironmentVariable)
137 // set environment variable
138 gmxSetenv(environmentVariable.c_str(), "ON", true);
140 if (doRerun)
142 simulator2Caller.addOption("-rerun", simulator1TrajectoryFileName);
144 ASSERT_EQ(0, runner->callMdrun(simulator2Caller));
147 if (doEnvironmentVariable)
149 if (environmentVariableBackup != nullptr)
151 // set environment variable
152 gmxSetenv(environmentVariable.c_str(), environmentVariableBackup, true);
154 else
156 // unset environment variable
157 gmxUnsetenv(environmentVariable.c_str());
161 // Build the functor that will compare energy frames on the chosen
162 // energy fields.
163 auto energyComparator = [&energiesToMatch](const EnergyFrame &first, const EnergyFrame &second)
165 compareEnergyFrames(first, second, energiesToMatch);
167 // Build the manager that will present matching pairs of frames to compare.
169 // TODO Here is an unnecessary copy of keys (ie. the energy field
170 // names), for convenience. In the future, use a range.
171 auto namesOfEnergiesToMatch = getKeys(energiesToMatch);
172 FramePairManager<EnergyFrameReader>
173 energyManager(
174 openEnergyFileToReadFields(simulator1EdrFileName, namesOfEnergiesToMatch),
175 openEnergyFileToReadFields(simulator2EdrFileName, namesOfEnergiesToMatch));
176 // Compare the energy frames.
177 energyManager.compareAllFramePairs<EnergyFrame>(energyComparator);
179 // Specify how trajectory frame matching must work.
180 TrajectoryFrameMatchSettings trajectoryMatchSettings {
181 true, true, true, true, !doRerun, true
183 /* Specify the default expected tolerances for trajectory
184 * components for all simulation systems. */
185 TrajectoryTolerances trajectoryTolerances {
186 defaultRealTolerance(), // box
187 relativeToleranceAsFloatingPoint(1.0, 1.0e-3), // positions
188 defaultRealTolerance(), // velocities
189 relativeToleranceAsFloatingPoint(100.0, GMX_DOUBLE ? 1.0e-7 : 1.0e-5) // forces
192 // Build the functor that will compare reference and test
193 // trajectory frames in the chosen way.
194 auto trajectoryComparator = [&trajectoryMatchSettings, &trajectoryTolerances](const TrajectoryFrame &reference, const TrajectoryFrame &test)
196 compareTrajectoryFrames(reference, test, trajectoryMatchSettings, trajectoryTolerances);
198 // Build the manager that will present matching pairs of frames to compare
199 FramePairManager<TrajectoryFrameReader>
200 trajectoryManager(std::make_unique<TrajectoryFrameReader>(simulator1TrajectoryFileName),
201 std::make_unique<TrajectoryFrameReader>(simulator2TrajectoryFileName));
202 // Compare the trajectory frames.
203 trajectoryManager.compareAllFramePairs<TrajectoryFrame>(trajectoryComparator);
205 } // namespace
207 template<typename ... Args>
208 void executeSimulatorComparisonTest(
209 const std::string &environmentVariable,
210 Args && ... args)
212 executeSimulatorComparisonTestImpl<true, false>(
213 std::forward<Args>(args) ..., environmentVariable);
216 template<typename ... Args>
217 void executeRerunTest(Args && ... args)
219 executeSimulatorComparisonTestImpl<false, true>(
220 std::forward<Args>(args) ..., "");
223 } // namespace test
224 } // namespace gmx