Apply re-formatting to C++ in src/ tree.
[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(),
108                 numRanksAvailable,
109                 numRanksAvailable == 1 ? ">" : "=");
110         return;
111     }
112
113     std::string theMdpFile =
114             "coulombtype     = PME\n"
115             "nstcalcenergy   = 1\n"
116             "nstenergy       = 4\n"
117             "nstfout         = 4\n"
118             "rcoulomb        = 1.5\n"
119             "rvdw            = 1.5\n"
120             "pme-order       = 4\n"
121             "fourier-spacing = 0.2\n"
122             "dt              = 0.0025\n"
123             "nsteps          = 20\n";
124
125     if (simulationName == "3DC")
126     {
127         theMdpFile +=
128                 "ewald-geometry = 3DC\n"
129                 "pbc             = xy\n"
130                 "nwall           = 2\n"
131                 "wall-type       = 12-6\n"
132                 "wall-atomtype   = C C\n"
133                 "wall-ewald-zfac = 2\n";
134     }
135     else
136     {
137         theMdpFile += "epsilon-surface = 1\n";
138         if (simulationName == "epsilon-surface-constraint")
139         {
140             theMdpFile += "constraints = all-bonds";
141         }
142     }
143
144     // Prepare the .tpr file
145     {
146         CommandLine caller;
147         runner_.useTopGroAndNdxFromDatabase("dipoles");
148         runner_.useStringAsMdpFile(theMdpFile);
149         EXPECT_EQ(0, runner_.callGrompp(caller));
150     }
151     // Do mdrun
152     {
153         CommandLine mdrunCaller;
154         ASSERT_EQ(0, runner_.callMdrun(mdrunCaller));
155         EnergyTermsToCompare energyTermsToCompare{
156             { { interaction_function[F_EPOT].longname, absoluteTolerance(1e-3) },
157               { interaction_function[F_ETOT].longname, absoluteTolerance(1e-3) } }
158         };
159         TestReferenceData refData;
160         auto checker = refData.rootChecker().checkCompound("Simulation", simulationName);
161         checkEnergiesAgainstReferenceData(runner_.edrFileName_, energyTermsToCompare, &checker);
162         // Now check the forces
163         TrajectoryFrameReader reader(runner_.fullPrecisionTrajectoryFileName_);
164         checker.setDefaultTolerance(relativeToleranceAsFloatingPoint(1, 1e-3));
165         do
166         {
167             auto frame = reader.frame();
168             auto force = frame.f();
169             int  atom  = 0;
170             for (auto& f : force)
171             {
172                 std::string forceName = frame.frameName() + " F[" + toString(atom) + "]";
173
174                 checker.checkVector(f, forceName.c_str());
175                 atom++;
176             }
177         } while (reader.readNextFrame());
178     }
179 }
180
181 //! Containers of systems to test.
182 //! \{
183 std::vector<std::string> surfaceTerm = { "3DC", "epsilon-surface-constraint", "epsilon-surface" };
184 //! \}
185
186 INSTANTIATE_TEST_CASE_P(EwaldSurfaceTerm, EwaldSurfaceTermTest, ::testing::ValuesIn(surfaceTerm));
187
188 } // namespace
189 } // namespace test
190 } // namespace gmx