Apply clang-format to source tree
[alexxy/gromacs.git] / src / programs / mdrun / tests / simulatorcomparison.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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  * Helper classes for tests that compare the results of equivalent
38  * simulation runs. Currently used for the rerun and the simulator
39  * tests
40  *
41  * \author Mark Abraham <mark.j.abraham@gmail.com>
42  * \author Pascal Merz <pascal.merz@me.com>
43  * \ingroup module_mdrun_integration_tests
44  */
45 #include "gmxpre.h"
46
47 #include "testutils/mpitest.h"
48 #include "testutils/setenv.h"
49 #include "testutils/simulationdatabase.h"
50
51 #include "energycomparison.h"
52 #include "energyreader.h"
53 #include "mdruncomparison.h"
54 #include "moduletest.h"
55 #include "trajectorycomparison.h"
56 #include "trajectoryreader.h"
57
58 namespace gmx
59 {
60 namespace test
61 {
62 namespace
63 {
64
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)
75 {
76     // TODO At some point we should also test PME-only ranks.
77     int numRanksAvailable = getNumberOfTestMpiRanks();
78     if (!isNumberOfPpRanksSupported(simulationName, numRanksAvailable))
79     {
80         fprintf(stdout,
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());
85         return;
86     }
87
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");
93
94     // prepare the .tpr file
95     {
96         // TODO evolve grompp to report the number of warnings issued, so
97         // tests always expect the right number.
98         CommandLine caller;
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));
105     }
106
107     char* environmentVariableBackup = nullptr;
108     if (doEnvironmentVariable)
109     {
110         // save state of environment variable
111         environmentVariableBackup = getenv(environmentVariable.c_str());
112     }
113
114     // do the first mdrun
115     {
116         runner->fullPrecisionTrajectoryFileName_ = simulator1TrajectoryFileName;
117         runner->edrFileName_                     = simulator1EdrFileName;
118         runner->tprFileName_                     = simulatorTprFileName;
119         CommandLine simulator1Caller;
120         simulator1Caller.append("mdrun");
121         if (doEnvironmentVariable)
122         {
123             // unset environment variable
124             gmxUnsetenv(environmentVariable.c_str());
125         }
126         ASSERT_EQ(0, runner->callMdrun(simulator1Caller));
127     }
128
129     // do the second mdrun
130     {
131         runner->fullPrecisionTrajectoryFileName_ = simulator2TrajectoryFileName;
132         runner->edrFileName_                     = simulator2EdrFileName;
133         runner->tprFileName_                     = simulatorTprFileName;
134         CommandLine simulator2Caller;
135         simulator2Caller.append("mdrun");
136         if (doEnvironmentVariable)
137         {
138             // set environment variable
139             gmxSetenv(environmentVariable.c_str(), "ON", true);
140         }
141         if (doRerun)
142         {
143             simulator2Caller.addOption("-rerun", simulator1TrajectoryFileName);
144         }
145         ASSERT_EQ(0, runner->callMdrun(simulator2Caller));
146     }
147
148     if (doEnvironmentVariable)
149     {
150         if (environmentVariableBackup != nullptr)
151         {
152             // set environment variable
153             gmxSetenv(environmentVariable.c_str(), environmentVariableBackup, true);
154         }
155         else
156         {
157             // unset environment variable
158             gmxUnsetenv(environmentVariable.c_str());
159         }
160     }
161
162     // Build the functor that will compare energy frames on the chosen
163     // energy terms.
164     EnergyComparison energyComparison(energyTermsToCompare);
165
166     // Build the manager that will present matching pairs of frames to compare.
167     //
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);
176
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);
183 }
184 } // namespace
185
186 template<typename... Args>
187 void executeSimulatorComparisonTest(const std::string& environmentVariable, Args&&... args)
188 {
189     executeSimulatorComparisonTestImpl<true, false>(std::forward<Args>(args)..., environmentVariable);
190 }
191
192 template<typename... Args>
193 void executeRerunTest(Args&&... args)
194 {
195     executeSimulatorComparisonTestImpl<false, true>(std::forward<Args>(args)..., "");
196 }
197
198 } // namespace test
199 } // namespace gmx