84249c46b1b2dafd7e59281f071ef3c6bdad4e44
[alexxy/gromacs.git] / src / gromacs / pbcutil / tests / com.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 /*! \internal \file
36  * \brief
37  * Tests COM handling code.
38  *
39  * \author Paul Bauer <paul.bauer.q@gmail.com>
40  * \ingroup module_pbcutil
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/pbcutil/com.h"
45
46 #include <vector>
47
48 #include <gtest/gtest.h>
49
50 #include "gromacs/math/vectypes.h"
51 #include "gromacs/pbcutil/pbc.h"
52 #include "gromacs/topology/mtop_util.h"
53 #include "gromacs/topology/topology.h"
54
55 #include "testutils/refdata.h"
56 #include "testutils/testasserts.h"
57
58 namespace gmx
59 {
60
61 namespace test
62 {
63
64 namespace
65 {
66
67 /*! \brief Populates a molectype for generate a graph
68  *
69  * This moleculetype has the first and last atom not connected to the other atoms
70  * so we test optimizations in the shift code that generates the actual graph only
71  * for the atom range from the first to the last connected atoms.
72  *
73  * Defines three residues to test residue and molecule COM handling.
74  *
75  * \todo Return moltype once t_atoms is a proper class.
76  */
77 void populateMoleculeType(gmx_moltype_t* moltype)
78 {
79     constexpr int atomNumber        = 5;
80     constexpr int residueNumber     = 3;
81     moltype->atoms.nr               = atomNumber;
82     moltype->ilist[F_CONSTR].iatoms = { 0, 1, 2 };
83     moltype->ilist[F_ANGLES].iatoms = { 1, 2, 1, 3 };
84
85     snew(moltype->atoms.atom, atomNumber);
86     snew(moltype->atoms.resinfo, residueNumber);
87     moltype->atoms.atom[0].resind = 0;
88     moltype->atoms.atom[1].resind = 1;
89     moltype->atoms.atom[2].resind = 1;
90     moltype->atoms.atom[3].resind = 1;
91     moltype->atoms.atom[4].resind = 2;
92
93     moltype->atoms.atom[0].m = 42;
94     moltype->atoms.atom[1].m = 13;
95     moltype->atoms.atom[2].m = 7;
96     moltype->atoms.atom[3].m = 23;
97     moltype->atoms.atom[4].m = 2;
98 }
99
100 //! Set up initial coordinates.
101 std::vector<RVec> initialCoordinates()
102 {
103     std::vector<RVec> coordinates;
104     coordinates.emplace_back(-1, 0, 3);
105     coordinates.emplace_back(1.5, 1.5, 4.5);
106     coordinates.emplace_back(1.6, 1.5, 4.5);
107     coordinates.emplace_back(1.6, 1.7, 4.5);
108     coordinates.emplace_back(1, 4, 2);
109
110     coordinates.emplace_back(1, 0, -3);
111     coordinates.emplace_back(-1.5, -1.5, -4.5);
112     coordinates.emplace_back(-1.6, -1.5, -4.5);
113     coordinates.emplace_back(-1.6, -1.7, -4.5);
114     coordinates.emplace_back(-1, -4, -2);
115
116     return coordinates;
117 }
118
119 using COMInPlaceTestParams = std::tuple<UnitCellType, CenteringType, PbcType>;
120 using test::FloatingPointTolerance;
121
122 /*! \brief
123  * Test fixture for checking correct molecule COM treatment.
124  */
125 class COMInPlaceTest : public ::testing::Test, public ::testing::WithParamInterface<COMInPlaceTestParams>
126 {
127 public:
128     COMInPlaceTest();
129
130     //! Run the test with the given input for molecule COM.
131     void runTestMolecule(const matrix box);
132     //! Run the test with the given input for residue COM.
133     void runTestResidue(const matrix box);
134
135 private:
136     //! Coordinates to use.
137     std::vector<RVec> testCoordinates_;
138     //! Dummy topology to use.
139     gmx_mtop_t testTopology_;
140     //! Reference data holder.
141     TestReferenceData data_;
142     //! Checker for data.
143     TestReferenceChecker checker_;
144 };
145
146 COMInPlaceTest::COMInPlaceTest() :
147     testCoordinates_(initialCoordinates()),
148     checker_(data_.rootChecker())
149 {
150     auto& moltype = testTopology_.moltype.emplace_back();
151     populateMoleculeType(&moltype);
152     auto& molblock       = testTopology_.molblock.emplace_back();
153     molblock.nmol        = 2;
154     molblock.type        = 0;
155     testTopology_.natoms = moltype.atoms.nr * molblock.nmol;
156     testTopology_.finalize();
157     FloatingPointTolerance tolerance(
158             FloatingPointTolerance(1.0e-6, 1.0e-6, 1.0e-8, 1.0e-12, 10000, 100, false));
159     checker_.setDefaultTolerance(tolerance);
160 }
161
162 void COMInPlaceTest::runTestMolecule(const matrix box)
163 {
164     auto params   = GetParam();
165     auto unitcell = std::get<0>(params);
166     auto center   = std::get<1>(params);
167     auto pbcType  = std::get<2>(params);
168     placeCoordinatesWithCOMInBox(pbcType, unitcell, center, box, testCoordinates_, testTopology_,
169                                  COMShiftType::Molecule);
170     std::string testString = "Molecule " + std::string(unitCellTypeNames(unitcell))
171                              + std::string(centerTypeNames(center)) + c_pbcTypeNames[pbcType]
172                              + c_pbcTypeNames[guessPbcType(box)];
173     checker_.checkSequence(std::begin(testCoordinates_), std::end(testCoordinates_), testString.c_str());
174 }
175
176 void COMInPlaceTest::runTestResidue(const matrix box)
177 {
178     auto params   = GetParam();
179     auto unitcell = std::get<0>(params);
180     auto center   = std::get<1>(params);
181     auto pbcType  = std::get<2>(params);
182     placeCoordinatesWithCOMInBox(pbcType, unitcell, center, box, testCoordinates_, testTopology_,
183                                  COMShiftType::Residue);
184     std::string testString = "Residue " + std::string(unitCellTypeNames(unitcell))
185                              + std::string(centerTypeNames(center)) + c_pbcTypeNames[pbcType]
186                              + c_pbcTypeNames[guessPbcType(box)];
187     checker_.checkSequence(std::begin(testCoordinates_), std::end(testCoordinates_), testString.c_str());
188 }
189
190 TEST(ShiftTest, CoordinateShiftWorks)
191 {
192     auto       coords = initialCoordinates();
193     const RVec shift(1.5, -2.2, 0);
194
195     shiftAtoms(shift, coords);
196     EXPECT_FLOAT_EQ(coords[4][0], 2.5);
197     EXPECT_FLOAT_EQ(coords[3][1], -0.5);
198     EXPECT_FLOAT_EQ(coords[9][2], -2);
199 }
200
201
202 TEST_P(COMInPlaceTest, MatrixDefault)
203 {
204     const matrix box = { { 3, 0, 0 }, { 0, 3, 0 }, { 0, 0, 3 } };
205
206     runTestMolecule(box);
207     runTestResidue(box);
208 }
209
210 INSTANTIATE_TEST_CASE_P(CorrectCoordinates,
211                         COMInPlaceTest,
212                         testing::Combine(::testing::Values(UnitCellType::Compact,
213                                                            UnitCellType::Rectangular,
214                                                            UnitCellType::Triclinic),
215                                          ::testing::Values(CenteringType::Rectangular,
216                                                            CenteringType::Triclinic,
217                                                            CenteringType::Zero),
218                                          ::testing::Values(PbcType::No, PbcType::Xyz, PbcType::XY)));
219
220 // TODO add PbcType::Screw once it is fully supported.
221
222 } // namespace
223
224 } // namespace test
225
226 } // namespace gmx