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.
37 * Helper classes for tests that compare the results of equivalent
38 * simulation runs. Currently used for the rerun and the simulator
41 * \author Mark Abraham <mark.j.abraham@gmail.com>
42 * \author Pascal Merz <pascal.merz@me.com>
43 * \ingroup module_mdrun_integration_tests
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"
65 //! Run grompp and mdrun for both sets of mdp field values
66 template<bool doEnvironmentVariable, bool doRerun>
67 void executeSimulatorComparisonTestImpl(TestFileManager* fileManager,
68 SimulationRunner* runner,
69 const std::string& simulationName,
70 int maxWarningsTolerated,
71 const MdpFieldValues& mdpFieldValues,
72 const EnergyTermsToCompare& energyTermsToCompare,
73 const TrajectoryComparison& trajectoryComparison,
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))
81 "Test system '%s' cannot run with %d ranks.\n"
82 "The supported numbers are: %s\n",
83 simulationName.c_str(), numRanksAvailable,
84 reportNumbersOfPpRanksSupported(simulationName).c_str());
88 auto simulator1TrajectoryFileName = fileManager->getTemporaryFilePath("sim1.trr");
89 auto simulator1EdrFileName = fileManager->getTemporaryFilePath("sim1.edr");
90 auto simulator2TrajectoryFileName = fileManager->getTemporaryFilePath("sim2.trr");
91 auto simulator2EdrFileName = fileManager->getTemporaryFilePath("sim2.edr");
92 auto simulatorTprFileName = fileManager->getTemporaryFilePath("sim.tpr");
94 // prepare the .tpr file
96 // TODO evolve grompp to report the number of warnings issued, so
97 // tests always expect the right number.
99 caller.append("grompp");
100 caller.addOption("-maxwarn", maxWarningsTolerated);
101 runner->tprFileName_ = simulatorTprFileName;
102 runner->useTopGroAndNdxFromDatabase(simulationName);
103 runner->useStringAsMdpFile(prepareMdpFileContents(mdpFieldValues));
104 EXPECT_EQ(0, runner->callGrompp(caller));
107 char* environmentVariableBackup = nullptr;
108 if (doEnvironmentVariable)
110 // save state of environment variable
111 environmentVariableBackup = getenv(environmentVariable.c_str());
114 // do the first mdrun
116 runner->fullPrecisionTrajectoryFileName_ = simulator1TrajectoryFileName;
117 runner->edrFileName_ = simulator1EdrFileName;
118 runner->tprFileName_ = simulatorTprFileName;
119 CommandLine simulator1Caller;
120 simulator1Caller.append("mdrun");
121 if (doEnvironmentVariable)
123 // unset environment variable
124 gmxUnsetenv(environmentVariable.c_str());
126 ASSERT_EQ(0, runner->callMdrun(simulator1Caller));
129 // do the second mdrun
131 runner->fullPrecisionTrajectoryFileName_ = simulator2TrajectoryFileName;
132 runner->edrFileName_ = simulator2EdrFileName;
133 runner->tprFileName_ = simulatorTprFileName;
134 CommandLine simulator2Caller;
135 simulator2Caller.append("mdrun");
136 if (doEnvironmentVariable)
138 // set environment variable
139 gmxSetenv(environmentVariable.c_str(), "ON", true);
143 simulator2Caller.addOption("-rerun", simulator1TrajectoryFileName);
145 ASSERT_EQ(0, runner->callMdrun(simulator2Caller));
148 if (doEnvironmentVariable)
150 if (environmentVariableBackup != nullptr)
152 // set environment variable
153 gmxSetenv(environmentVariable.c_str(), environmentVariableBackup, true);
157 // unset environment variable
158 gmxUnsetenv(environmentVariable.c_str());
162 // Build the functor that will compare energy frames on the chosen
164 EnergyComparison energyComparison(energyTermsToCompare);
166 // Build the manager that will present matching pairs of frames to compare.
168 // TODO Here is an unnecessary copy of keys (ie. the energy term
169 // names), for convenience. In the future, use a range.
170 auto namesOfEnergiesToMatch = energyComparison.getEnergyNames();
171 FramePairManager<EnergyFrameReader> energyManager(
172 openEnergyFileToReadTerms(simulator1EdrFileName, namesOfEnergiesToMatch),
173 openEnergyFileToReadTerms(simulator2EdrFileName, namesOfEnergiesToMatch));
174 // Compare the energy frames.
175 energyManager.compareAllFramePairs<EnergyFrame>(energyComparison);
177 // Build the manager that will present matching pairs of frames to compare
178 FramePairManager<TrajectoryFrameReader> trajectoryManager(
179 std::make_unique<TrajectoryFrameReader>(simulator1TrajectoryFileName),
180 std::make_unique<TrajectoryFrameReader>(simulator2TrajectoryFileName));
181 // Compare the trajectory frames.
182 trajectoryManager.compareAllFramePairs<TrajectoryFrame>(trajectoryComparison);
186 template<typename... Args>
187 void executeSimulatorComparisonTest(const std::string& environmentVariable, Args&&... args)
189 executeSimulatorComparisonTestImpl<true, false>(std::forward<Args>(args)..., environmentVariable);
192 template<typename... Args>
193 void executeRerunTest(Args&&... args)
195 executeSimulatorComparisonTestImpl<false, true>(std::forward<Args>(args)..., "");