48665c68e0ff2024cd5d952e8dba8ff92a0c5323
[alexxy/gromacs.git] / src / programs / mdrun / tests / pull.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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 mdrun pull functionality.
39  *
40  * \author Berk Hess <hess@kth.se>
41  * \ingroup module_mdrun_integration_tests
42  */
43 #include "gmxpre.h"
44
45 #include <map>
46 #include <memory>
47 #include <string>
48 #include <unordered_map>
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/utility/stringutil.h"
57
58 #include "testutils/mpitest.h"
59 #include "testutils/refdata.h"
60 #include "testutils/simulationdatabase.h"
61 #include "testutils/testasserts.h"
62
63 #include "energycomparison.h"
64 #include "moduletest.h"
65
66 namespace gmx
67 {
68 namespace test
69 {
70 namespace
71 {
72
73 /*! \brief Database of energy tolerances on the various systems. */
74 std::unordered_map<std::string, FloatingPointTolerance> energyToleranceForSystem_g = {
75     { { "spc216", relativeToleranceAsFloatingPoint(1, 1e-4) } }
76 };
77
78 /*! \brief Database of pressure tolerances on the various systems. */
79 std::unordered_map<std::string, FloatingPointTolerance> pressureToleranceForSystem_g = {
80     { { "spc216", relativeToleranceAsFloatingPoint(1, 2e-4) } }
81 };
82
83 const std::unordered_map<std::string, std::vector<std::pair<std::string, std::string>>> c_mdpPullParams = {
84     { { "umbrella-3D",
85         { { "pull-ngroups", "2" },
86           { "pull-ncoords", "1" },
87           { "pull-nstxout", "0" },
88           { "pull-nstfout", "0" },
89           { "pull-group1-name", "r_1" },
90           { "pull-group2-name", "r_2" },
91           { "pull-coord1-groups", "1 2" },
92           { "pull-coord1-type", "umbrella" },
93           { "pull-coord1-geometry", "distance" },
94           { "pull-coord1-dim", "Y Y Y" },
95           { "pull-coord1-init", "0.6" },
96           { "pull-coord1-k", "100" } } },
97       { "umbrella-2D",
98         { { "pull-ngroups", "2" },
99           { "pull-ncoords", "1" },
100           { "pull-nstxout", "0" },
101           { "pull-nstfout", "0" },
102           { "pull-group1-name", "r_1" },
103           { "pull-group2-name", "r_2" },
104           { "pull-coord1-groups", "1 2" },
105           { "pull-coord1-type", "umbrella" },
106           { "pull-coord1-geometry", "distance" },
107           { "pull-coord1-dim", "Y Y N" },
108           { "pull-coord1-init", "0.4" },
109           { "pull-coord1-k", "100" } } },
110       { "constraint-flatbottom",
111         { { "pull-ngroups", "3" },
112           { "pull-ncoords", "2" },
113           { "pull-nstxout", "0" },
114           { "pull-nstfout", "0" },
115           { "pull-group1-name", "r_1" },
116           { "pull-group2-name", "r_2" },
117           { "pull-group3-name", "r_3" },
118           { "pull-coord1-groups", "1 2" },
119           { "pull-coord1-type", "constraint" },
120           { "pull-coord1-geometry", "distance" },
121           { "pull-coord1-dim", "Y Y Y" },
122           { "pull-coord1-init", "0.5" },
123           { "pull-coord2-groups", "1 3" },
124           { "pull-coord2-type", "flat-bottom" },
125           { "pull-coord2-geometry", "distance" },
126           { "pull-coord2-dim", "Y Y Y" },
127           { "pull-coord2-init", "0.4" },
128           { "pull-coord2-k", "100" } } } }
129 };
130
131 //! Helper type
132 using MdpField = MdpFieldValues::value_type;
133
134 /*! \brief Test fixture base for simple mdrun systems
135  *
136  * This test ensures mdrun can run a simulation, reaching
137  * reproducible energies.
138  *
139  * The choices for tolerance are arbitrary but sufficient. */
140 class PullIntegrationTest :
141     public MdrunTestFixture,
142     public ::testing::WithParamInterface<std::tuple<std::string, std::string>>
143 {
144 };
145
146 //! Adds integrator and nonbonded parameter setup
147 void addBasicMdpValues(MdpFieldValues* mdpFieldValues)
148 {
149     (*mdpFieldValues)["nsteps"]        = "20";
150     (*mdpFieldValues)["nstcomm"]       = "10";
151     (*mdpFieldValues)["nstlist"]       = "10";
152     (*mdpFieldValues)["nstcalcenergy"] = "5";
153     (*mdpFieldValues)["nstenergy"]     = "5";
154     (*mdpFieldValues)["coulombtype"]   = "Reaction-field";
155     (*mdpFieldValues)["vdwtype"]       = "Cut-off";
156 }
157
158 TEST_P(PullIntegrationTest, WithinTolerances)
159 {
160     auto params         = GetParam();
161     auto simulationName = std::get<0>(params);
162     auto pullSetup      = std::get<1>(params);
163     SCOPED_TRACE(formatString("Comparing simple mdrun for '%s'", simulationName.c_str()));
164
165     // TODO At some point we should also test PME-only ranks.
166     int numRanksAvailable = getNumberOfTestMpiRanks();
167     if (!isNumberOfPpRanksSupported(simulationName, numRanksAvailable))
168     {
169         fprintf(stdout,
170                 "Test system '%s' cannot run with %d ranks.\n"
171                 "The supported numbers are: %s\n",
172                 simulationName.c_str(),
173                 numRanksAvailable,
174                 reportNumbersOfPpRanksSupported(simulationName).c_str());
175         return;
176     }
177     auto mdpFieldValues = prepareMdpFieldValues(simulationName.c_str(), "md", "no", "no");
178     addBasicMdpValues(&mdpFieldValues);
179
180     // Add the pull parameters
181     mdpFieldValues["pull"]    = "yes";
182     const auto& mdpPullParams = c_mdpPullParams.at(pullSetup);
183     for (const auto& param : mdpPullParams)
184     {
185         mdpFieldValues[param.first] = param.second;
186     }
187
188     // Prepare the .tpr file
189     {
190         CommandLine caller;
191         runner_.useTopGroAndNdxFromDatabase(simulationName);
192         runner_.useNdxFromDatabase(simulationName + "_pull");
193         runner_.useStringAsMdpFile(prepareMdpFileContents(mdpFieldValues));
194         EXPECT_EQ(0, runner_.callGrompp(caller));
195     }
196     // Do mdrun
197     {
198         CommandLine mdrunCaller;
199         ASSERT_EQ(0, runner_.callMdrun(mdrunCaller));
200         EnergyTermsToCompare energyTermsToCompare{ {
201                 { interaction_function[F_COM_PULL].longname, energyToleranceForSystem_g.at(simulationName) },
202                 { interaction_function[F_EPOT].longname, energyToleranceForSystem_g.at(simulationName) },
203                 { interaction_function[F_EKIN].longname, energyToleranceForSystem_g.at(simulationName) },
204                 { interaction_function[F_PRES].longname, pressureToleranceForSystem_g.at(simulationName) },
205         } };
206         TestReferenceData    refData;
207         auto                 checker = refData.rootChecker()
208                                .checkCompound("Simulation", simulationName)
209                                .checkCompound("PullSetup", pullSetup);
210         checkEnergiesAgainstReferenceData(runner_.edrFileName_, energyTermsToCompare, &checker);
211     }
212 }
213
214 INSTANTIATE_TEST_SUITE_P(PullTest,
215                          PullIntegrationTest,
216                          ::testing::Combine(::testing::Values("spc216"),
217                                             ::testing::Values("umbrella-3D",
218                                                               "umbrella-2D",
219                                                               "constraint-flatbottom")));
220
221 } // namespace
222 } // namespace test
223 } // namespace gmx