Update bundled GoogleTest to current HEAD
[alexxy/gromacs.git] / src / programs / mdrun / tests / minimize.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019,2020,2021, 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 for the energy minimization functionality.
39  *
40  * \author Mark Abraham <mark.j.abraham@gmail.com>
41  * \ingroup module_mdrun_integration_tests
42  */
43 #include "gmxpre.h"
44
45 #include <map>
46 #include <memory>
47 #include <string>
48 #include <tuple>
49 #include <vector>
50
51 #include <gtest/gtest.h>
52
53 #include "gromacs/options/filenameoption.h"
54 #include "gromacs/topology/idef.h"
55 #include "gromacs/topology/ifunc.h"
56 #include "gromacs/trajectory/energyframe.h"
57 #include "gromacs/trajectory/trajectoryframe.h"
58 #include "gromacs/utility/basenetwork.h"
59 #include "gromacs/utility/stringutil.h"
60
61 #include "testutils/mpitest.h"
62 #include "testutils/refdata.h"
63 #include "testutils/simulationdatabase.h"
64 #include "testutils/testasserts.h"
65
66 #include "energycomparison.h"
67 #include "energyreader.h"
68 #include "moduletest.h"
69
70 namespace gmx
71 {
72 namespace test
73 {
74 namespace
75 {
76
77 /*! \brief Test fixture base for energy minimizaiton
78  *
79  * This test ensures mdrun can run an energy minimization, reaching
80  * a reproducible final energy.
81  *
82  * The choices for tolerance are arbitrary but sufficient. */
83 class EnergyMinimizationTest :
84     public MdrunTestFixture,
85     public ::testing::WithParamInterface<std::tuple<std::string, std::string>>
86 {
87 };
88
89 /*! \brief Database of empirical tolerances for EM integrators on the various systems. */
90 std::unordered_map<std::string, FloatingPointTolerance> potentialEnergyToleranceForSystem_g = {
91     { { "argon12", relativeToleranceAsPrecisionDependentUlp(-1, 10, 200) },
92       { "tip3p5", relativeToleranceAsPrecisionDependentUlp(-50, 200, 3800) },
93       { "glycine_vacuo", relativeToleranceAsPrecisionDependentUlp(1000, 100, 100) },
94       { "alanine_vsite_vacuo", relativeToleranceAsPrecisionDependentUlp(-160, 150, 400) },
95       { "glycine_no_constraints_vacuo", relativeToleranceAsPrecisionDependentUlp(2000, 100, 100) } }
96 };
97
98 TEST_P(EnergyMinimizationTest, WithinTolerances)
99 {
100     auto params         = GetParam();
101     auto simulationName = std::get<0>(params);
102     auto minimizer      = std::get<1>(params);
103     SCOPED_TRACE(formatString("Comparing '%s' energy minimization for simulation '%s'",
104                               minimizer.c_str(),
105                               simulationName.c_str()));
106
107     // TODO At some point we should also test PME-only ranks.
108     int numRanksAvailable = getNumberOfTestMpiRanks();
109     if (!isNumberOfPpRanksSupported(simulationName, numRanksAvailable))
110     {
111         fprintf(stdout,
112                 "Test system '%s' cannot run with %d ranks.\n"
113                 "The supported numbers are: %s\n",
114                 simulationName.c_str(),
115                 numRanksAvailable,
116                 reportNumbersOfPpRanksSupported(simulationName).c_str());
117         return;
118     }
119
120     auto mdpFieldValues =
121             prepareMdpFieldValues(simulationName.c_str(), minimizer.c_str(), "no", "no");
122     mdpFieldValues["nsteps"] = "4";
123
124     int maxWarningsTolerated = (minimizer == "l-bfgs") ? 1 : 0;
125     // prepare the .tpr file
126     {
127         // TODO evolve grompp to report the number of warnings issued, so
128         // tests always expect the right number.
129         CommandLine caller;
130         caller.append("grompp");
131         caller.addOption("-maxwarn", maxWarningsTolerated);
132         runner_.useTopGroAndNdxFromDatabase(simulationName);
133         runner_.useStringAsMdpFile(prepareMdpFileContents(mdpFieldValues));
134         EXPECT_EQ(0, runner_.callGrompp(caller));
135     }
136
137     // do mdrun, preparing to check the energies later
138     runner_.edrFileName_ = fileManager_.getTemporaryFilePath("minimize.edr");
139     {
140         CommandLine mdrunCaller;
141         mdrunCaller.append("mdrun");
142         if (minimizer == "l-bfgs" && getNumberOfTestMpiRanks() > 1)
143         {
144             // Ideally we would use this death test, but it is not
145             // stable enough in Jenkins, so we just skip it.
146             // EXPECT_DEATH_IF_SUPPORTED(runner_.callMdrun(mdrunCaller),
147             //                          "L-BFGS minimization only supports a single rank");
148             return;
149         }
150         else
151         {
152             ASSERT_EQ(0, runner_.callMdrun(mdrunCaller));
153         }
154     }
155
156     EnergyTermsToCompare energyTermsToCompare{ {
157             { interaction_function[F_EPOT].longname, potentialEnergyToleranceForSystem_g.at(simulationName) },
158     } };
159
160     TestReferenceData refData;
161     auto              checker = refData.rootChecker()
162                            .checkCompound("Simulation", simulationName)
163                            .checkCompound("Minimizer", minimizer);
164     checkEnergiesAgainstReferenceData(runner_.edrFileName_, energyTermsToCompare, &checker);
165 }
166
167 //! Containers of systems and integrators to test.
168 //! \{
169 std::vector<std::string> unconstrainedSystemsToTest_g = { "argon12",
170                                                           "glycine_no_constraints_vacuo" };
171 std::vector<std::string> minimizersToTest_g           = { "steep", "cg", "l-bfgs" };
172
173 std::vector<std::string> constrainedSystemsToTest_g        = { "tip3p5",
174                                                         "glycine_vacuo",
175                                                         "alanine_vsite_vacuo" };
176 std::vector<std::string> minimizersToTestWithConstraints_g = { "steep", "cg" };
177 //! \}
178
179 // The time for OpenCL kernel compilation means these tests might time
180 // out. If that proves to be a problem, these can be disabled for
181 // OpenCL builds. However, once that compilation is cached for the
182 // lifetime of the whole test binary process, these tests should run in
183 // such configurations.
184 INSTANTIATE_TEST_SUITE_P(MinimizersWorkWithConstraints,
185                          EnergyMinimizationTest,
186                          ::testing::Combine(::testing::ValuesIn(constrainedSystemsToTest_g),
187                                             ::testing::ValuesIn(minimizersToTestWithConstraints_g)));
188 INSTANTIATE_TEST_SUITE_P(MinimizersWork,
189                          EnergyMinimizationTest,
190                          ::testing::Combine(::testing::ValuesIn(unconstrainedSystemsToTest_g),
191                                             ::testing::ValuesIn(minimizersToTest_g)));
192
193 } // namespace
194 } // namespace test
195 } // namespace gmx