367d0abb427789d551e6928b69bcf9b1fd0bb880
[alexxy/gromacs.git] / src / programs / mdrun / tests / ewaldsurfaceterm.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020, 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  * Test for Ewald 3DC and epsilon-surface terms.
39  *
40  * \author Berk Hess <hess@kth.se>
41  * \ingroup module_mdrun_integration_tests
42  */
43 #include "gmxpre.h"
44
45 #include <memory>
46 #include <string>
47 #include <vector>
48
49 #include <gtest/gtest.h>
50
51 #include "gromacs/topology/idef.h"
52 #include "gromacs/topology/ifunc.h"
53 #include "gromacs/trajectory/trajectoryframe.h"
54 #include "gromacs/utility/basenetwork.h"
55 #include "gromacs/utility/filestream.h"
56 #include "gromacs/utility/strconvert.h"
57 #include "gromacs/utility/stringutil.h"
58
59 #include "testutils/mpitest.h"
60 #include "testutils/refdata.h"
61 #include "testutils/simulationdatabase.h"
62 #include "testutils/testasserts.h"
63
64 #include "energycomparison.h"
65 #include "moduletest.h"
66 #include "trajectoryreader.h"
67
68 namespace gmx
69 {
70 namespace test
71 {
72 namespace
73 {
74
75 //! Helper type
76 using MdpField = MdpFieldValues::value_type;
77
78 /*! \brief Test fixture base for simple mdrun systems
79  *
80  * This test ensures mdrun can run a simulation, reaching reproducible
81  * energies and forces.
82  * The starting coordinates are set up such that both molecules are
83  * broken over PBC and the PBC treatment is tested.
84  */
85 class EwaldSurfaceTermTest : public MdrunTestFixture, public ::testing::WithParamInterface<std::string>
86 {
87 };
88
89 TEST_P(EwaldSurfaceTermTest, WithinTolerances)
90 {
91     auto simulationName = GetParam();
92     SCOPED_TRACE(formatString("Comparing simple mdrun for '%s'", simulationName.c_str()));
93
94     int numRanksAvailable = getNumberOfTestMpiRanks();
95     /* For epsilon-surface we need whole molecules.
96      * Without constraints we can make molecules whole on a sinlge rank.
97      * With constraints molecules are whole with update groups with DD.
98      *
99      * TODO: Remove the rank=1 check when DD also runs single rank.
100      */
101     if ((simulationName == "epsilon-surface" && numRanksAvailable > 1)
102         || (simulationName == "epsilon-surface-constraint" && numRanksAvailable == 1))
103     {
104         fprintf(stdout,
105                 "Test system '%s' cannot run with %d ranks.\n"
106                 "The supported numbers are %s 1.\n",
107                 simulationName.c_str(), numRanksAvailable, numRanksAvailable == 1 ? ">" : "=");
108         return;
109     }
110
111     std::string theMdpFile =
112             "coulombtype     = PME\n"
113             "nstcalcenergy   = 1\n"
114             "nstenergy       = 4\n"
115             "nstfout         = 4\n"
116             "rcoulomb        = 1.5\n"
117             "rvdw            = 1.5\n"
118             "pme-order       = 4\n"
119             "fourier-spacing = 0.2\n"
120             "dt              = 0.0025\n"
121             "nsteps          = 20\n";
122
123     if (simulationName == "3DC")
124     {
125         theMdpFile +=
126                 "ewald-geometry = 3DC\n"
127                 "pbc             = xy\n"
128                 "nwall           = 2\n"
129                 "wall-type       = 12-6\n"
130                 "wall-atomtype   = C C\n"
131                 "wall-ewald-zfac = 2\n";
132     }
133     else
134     {
135         theMdpFile += "epsilon-surface = 1\n";
136         if (simulationName == "epsilon-surface-constraint")
137         {
138             theMdpFile += "constraints = all-bonds";
139         }
140     }
141
142     // Prepare the .tpr file
143     {
144         CommandLine caller;
145         runner_.useTopGroAndNdxFromDatabase("dipoles");
146         runner_.useStringAsMdpFile(theMdpFile);
147         EXPECT_EQ(0, runner_.callGrompp(caller));
148     }
149     // Do mdrun
150     {
151         CommandLine mdrunCaller;
152         ASSERT_EQ(0, runner_.callMdrun(mdrunCaller));
153         EnergyTermsToCompare energyTermsToCompare{
154             { { interaction_function[F_EPOT].longname, absoluteTolerance(1e-3) },
155               { interaction_function[F_ETOT].longname, absoluteTolerance(1e-3) } }
156         };
157         TestReferenceData refData;
158         auto checker = refData.rootChecker().checkCompound("Simulation", simulationName);
159         checkEnergiesAgainstReferenceData(runner_.edrFileName_, energyTermsToCompare, &checker);
160         // Now check the forces
161         TrajectoryFrameReader reader(runner_.fullPrecisionTrajectoryFileName_);
162         checker.setDefaultTolerance(relativeToleranceAsFloatingPoint(1, 1e-3));
163         do
164         {
165             auto frame = reader.frame();
166             auto force = frame.f();
167             int  atom  = 0;
168             for (auto& f : force)
169             {
170                 std::string forceName = frame.frameName() + " F[" + toString(atom) + "]";
171
172                 checker.checkVector(f, forceName.c_str());
173                 atom++;
174             }
175         } while (reader.readNextFrame());
176     }
177 }
178
179 //! Containers of systems to test.
180 //! \{
181 std::vector<std::string> surfaceTerm = { "3DC", "epsilon-surface-constraint", "epsilon-surface" };
182 //! \}
183
184 INSTANTIATE_TEST_CASE_P(EwaldSurfaceTerm, EwaldSurfaceTermTest, ::testing::ValuesIn(surfaceTerm));
185
186 } // namespace
187 } // namespace test
188 } // namespace gmx