Change acceptable precision in minimizer test
[gromacs.git] / src / programs / mdrun / tests / minimize.cpp
blobb77d2710f9d5c7cc2a1da965c40a5f60a0d4f465
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018, 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.
36 /*! \internal \file
37 * \brief
38 * Tests for the energy minimization functionality.
40 * \author Mark Abraham <mark.j.abraham@gmail.com>
41 * \ingroup module_mdrun_integration_tests
43 #include "gmxpre.h"
45 #include <map>
46 #include <memory>
47 #include <string>
48 #include <tuple>
49 #include <vector>
51 #include <gtest/gtest.h>
53 #include "gromacs/compat/make_unique.h"
54 #include "gromacs/options/filenameoption.h"
55 #include "gromacs/topology/idef.h"
56 #include "gromacs/topology/ifunc.h"
57 #include "gromacs/trajectory/energyframe.h"
58 #include "gromacs/trajectory/trajectoryframe.h"
59 #include "gromacs/utility/basenetwork.h"
60 #include "gromacs/utility/stringutil.h"
62 #include "testutils/mpitest.h"
63 #include "testutils/refdata.h"
64 #include "testutils/testasserts.h"
66 #include "energycomparison.h"
67 #include "energyreader.h"
68 #include "moduletest.h"
69 #include "simulationdatabase.h"
71 namespace gmx
73 namespace test
75 namespace
78 /*! \brief Test fixture base for energy minimizaiton
80 * This test ensures mdrun can run an energy minimization, reaching
81 * a reproducible final energy.
83 * The choices for tolerance are arbitrary but sufficient. */
84 class EnergyMinimizationTest : public MdrunTestFixture,
85 public ::testing::WithParamInterface <
86 std::tuple < std::string, std::string>>
90 /*! \brief Database of empirical tolerances for EM integrators on the various systems. */
91 std::unordered_map<std::string, FloatingPointTolerance> potentialEnergyToleranceForSystem_g =
94 "argon12",
95 relativeToleranceAsPrecisionDependentUlp(-1, 10, 200)
98 "spc5",
99 relativeToleranceAsPrecisionDependentUlp(-50, 150, 3500)
102 "glycine_vacuo",
103 relativeToleranceAsPrecisionDependentUlp(1000, 100, 100)
106 "alanine_vsite_vacuo",
107 relativeToleranceAsPrecisionDependentUlp(-160, 150, 400)
110 "glycine_no_constraints_vacuo",
111 relativeToleranceAsPrecisionDependentUlp(2000, 100, 100)
115 TEST_P(EnergyMinimizationTest, WithinTolerances)
117 auto params = GetParam();
118 auto simulationName = std::get<0>(params);
119 auto minimizer = std::get<1>(params);
120 SCOPED_TRACE(formatString("Comparing '%s' energy minimization for simulation '%s'",
121 minimizer.c_str(), simulationName.c_str()));
123 auto mdpFieldValues = prepareMdpFieldValues(simulationName.c_str(),
124 minimizer.c_str(),
125 "no", "no");
126 mdpFieldValues["nsteps"] = "4";
128 int maxWarningsTolerated = (minimizer == "l-bfgs") ? 1 : 0;
129 // prepare the .tpr file
131 // TODO evolve grompp to report the number of warnings issued, so
132 // tests always expect the right number.
133 CommandLine caller;
134 caller.append("grompp");
135 caller.addOption("-maxwarn", maxWarningsTolerated);
136 runner_.useTopGroAndNdxFromDatabase(simulationName);
137 runner_.useStringAsMdpFile(prepareMdpFileContents(mdpFieldValues));
138 EXPECT_EQ(0, runner_.callGrompp(caller));
141 // do mdrun, preparing to check the energies later
142 runner_.edrFileName_ = fileManager_.getTemporaryFilePath("minimize.edr");
144 CommandLine mdrunCaller;
145 mdrunCaller.append("mdrun");
146 if (minimizer == "l-bfgs" && getNumberOfTestMpiRanks() > 1)
148 // Ideally we would use this death test, but it is not
149 // stable enough in Jenkins, so we just skip it.
150 //EXPECT_DEATH_IF_SUPPORTED(runner_.callMdrun(mdrunCaller),
151 // "L-BFGS minimization only supports a single rank");
152 return;
154 else
156 ASSERT_EQ(0, runner_.callMdrun(mdrunCaller));
160 EnergyTolerances energiesToMatch
163 interaction_function[F_EPOT].longname, potentialEnergyToleranceForSystem_g.at(simulationName)
167 TestReferenceData refData;
168 auto checker = refData.rootChecker()
169 .checkCompound("Simulation", simulationName)
170 .checkCompound("Minimizer", minimizer);
171 checkEnergiesAgainstReferenceData(runner_.edrFileName_,
172 energiesToMatch,
173 &checker);
176 //! Containers of systems and integrators to test.
177 //! \{
178 std::vector<std::string> unconstrainedSystemsToTest_g = { "argon12", "glycine_no_constraints_vacuo" };
179 std::vector<std::string> minimizersToTest_g = { "steep", "cg", "l-bfgs" };
181 std::vector<std::string> constrainedSystemsToTest_g = { "spc5", "glycine_vacuo", "alanine_vsite_vacuo" };
182 std::vector<std::string> minimizersToTestWithConstraints_g = { "steep", "cg" };
183 //! \}
185 // The time for OpenCL kernel compilation means these tests might time
186 // out. If that proves to be a problem, these can be disabled for
187 // OpenCL builds. However, once that compilation is cached for the
188 // lifetime of the whole test binary process, these tests should run in
189 // such configurations.
190 INSTANTIATE_TEST_CASE_P(MinimizersWorkWithConstraints, EnergyMinimizationTest,
191 ::testing::Combine(::testing::ValuesIn(constrainedSystemsToTest_g),
192 ::testing::ValuesIn(minimizersToTestWithConstraints_g)));
193 INSTANTIATE_TEST_CASE_P(MinimizersWork, EnergyMinimizationTest,
194 ::testing::Combine(::testing::ValuesIn(unconstrainedSystemsToTest_g),
195 ::testing::ValuesIn(minimizersToTest_g)));
197 } // namespace
198 } // namespace test
199 } // namespace gmx