d4ae76ae5b8a2b81e3a7dae94334c8e19a9e24b5
[alexxy/gromacs.git] / src / programs / mdrun / tests / initialconstraints.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2017,2018,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  * This implements basic initial constrains test (using single-rank mdrun)
38  *
39  * This test checks that the coordinates from file, which only satisfy
40  * the constraints up to gro precision, are constrained correctly and that
41  * the initial velocity of the center of mass does not contribute to the
42  * kinetic energy at step 0..
43  * It runs the input system for 1 step (no continuation), and compares the total energy.
44  *
45  * \author Aleksei Iupinov <a.yupinov@gmail.com>
46  * \ingroup module_mdrun_integration_tests
47  */
48 #include "gmxpre.h"
49
50 #include <string>
51
52 #include "gromacs/trajectory/energyframe.h"
53 #include "gromacs/utility/stringutil.h"
54
55 #include "energyreader.h"
56 #include "moduletest.h"
57
58 namespace gmx
59 {
60 namespace test
61 {
62 namespace
63 {
64
65 //! This type holds input integrators. Now it's holding names, but ei* enum values from md_enums.h could be used instead.
66 using EnergyIntegratorType = const char*;
67
68 //! Test fixture parametrized on integrators
69 class InitialConstraintsTest :
70     public gmx::test::MdrunTestFixture,
71     public ::testing::WithParamInterface<EnergyIntegratorType>
72 {
73 };
74
75 TEST_P(InitialConstraintsTest, Works)
76 {
77     const int         nsteps     = 1;
78     const float       timestep   = 0.001;
79     auto              integrator = GetParam();
80     const std::string integratorName(integrator);
81     SCOPED_TRACE("Integrating with " + integratorName);
82     const std::string theMdpFile = formatString(
83             "nstcalcenergy           = 1\n"
84             "nstenergy               = 1\n"
85             "comm-mode               = linear\n"
86             "continuation            = no\n"
87             "constraints             = h-bonds\n"
88             "lincs_iter              = 2\n"
89             "verlet-buffer-tolerance = 1e-4\n"
90             "nsttcouple              = 1\n" // for md-vv-avek
91             "nstpcouple              = 1\n" // for md-vv-avek
92             "integrator              = %s\n"
93             "nsteps                  = %d\n"
94             "dt                      = %f\n",
95             integratorName.c_str(), nsteps, timestep);
96
97     runner_.useStringAsMdpFile(theMdpFile);
98
99     const std::string inputFile = "spc-and-methanol";
100     runner_.useTopGroAndNdxFromDatabase(inputFile);
101     EXPECT_EQ(0, runner_.callGrompp());
102
103     runner_.edrFileName_ = fileManager_.getTemporaryFilePath(inputFile + ".edr");
104     ASSERT_EQ(0, runner_.callMdrun());
105
106     auto energyReader =
107             openEnergyFileToReadTerms(runner_.edrFileName_, { "Total Energy", "Kinetic En." });
108     real totalEnergy = 0.0, prevTotalEnergy = 0.0;
109     auto tolerance = ulpTolerance(0); // The real value is set below from starting kinetic energy
110     for (int i = 0; i <= nsteps; i++)
111     {
112         EnergyFrame frame = energyReader->frame();
113         prevTotalEnergy   = totalEnergy;
114         totalEnergy       = frame.at("Total Energy");
115         if (i == 0)
116         {
117             // We set the tolerance for total energy based on magnitude of kinetic energy.
118             // The reason is that the other total energy component, the potential energy, can in theory have whatever magnitude.
119             const real startingKineticEnergy = frame.at("Kinetic En.");
120             tolerance = relativeToleranceAsFloatingPoint(startingKineticEnergy, 1e-5);
121         }
122         else
123         {
124             EXPECT_REAL_EQ_TOL(totalEnergy, prevTotalEnergy, tolerance);
125         }
126     }
127 }
128
129 //! Integrators with energy conservation to test
130 const EnergyIntegratorType c_integratorsToTest[] = { "md", "md-vv", "md-vv-avek" };
131
132 INSTANTIATE_TEST_CASE_P(Checking, InitialConstraintsTest, ::testing::ValuesIn(c_integratorsToTest));
133
134 } // namespace
135 } // namespace test
136 } // namespace gmx