1cf9f3e93912a85d164ae5e1935031ea9fa953ce
[alexxy/gromacs.git] / src / gromacs / pulling / tests / pull.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2018,2019,2020,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 /*! \internal \file
36  * \brief
37  * Implements test of some pulling routines
38  *
39  * \author David van der Spoel <david.vanderspoel@icm.uu.se>
40  * \ingroup module_pulling
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/pulling/pull.h"
45
46 #include <cmath>
47
48 #include <algorithm>
49
50 #include <gtest/gtest.h>
51
52 #include "gromacs/math/vec.h"
53 #include "gromacs/pbcutil/pbc.h"
54 #include "gromacs/pulling/pull_internal.h"
55 #include "gromacs/utility/smalloc.h"
56
57 #include "testutils/refdata.h"
58 #include "testutils/testasserts.h"
59 #include "testutils/testfilemanager.h"
60
61 namespace gmx
62 {
63 namespace
64 {
65
66 using gmx::test::defaultRealTolerance;
67
68 class PullTest : public ::testing::Test
69 {
70 protected:
71     PullTest() {}
72
73     static void test(PbcType pbcType, matrix box)
74     {
75         t_pbc pbc;
76
77         // PBC stuff
78         set_pbc(&pbc, pbcType, box);
79
80         GMX_ASSERT(pbc.ndim_ePBC >= 1 && pbc.ndim_ePBC <= DIM,
81                    "Tests only support PBC along at least x and at most x, y, and z");
82
83         real boxSizeZSquared;
84         if (pbc.ndim_ePBC > ZZ)
85         {
86             boxSizeZSquared = gmx::square(box[ZZ][ZZ]);
87         }
88         else
89         {
90             boxSizeZSquared = GMX_REAL_MAX;
91         }
92
93         {
94             // Distance pulling in all 3 dimensions
95             t_pull_coord params;
96             params.eGeom   = PullGroupGeometry::Distance;
97             params.dim[XX] = 1;
98             params.dim[YY] = 1;
99             params.dim[ZZ] = 1;
100             pull_coord_work_t pcrd(params);
101             clear_dvec(pcrd.spatialData.vec);
102
103             real minBoxSize2 = GMX_REAL_MAX;
104             for (int d = 0; d < pbc.ndim_ePBC; d++)
105             {
106                 minBoxSize2 = std::min(minBoxSize2, norm2(box[d]));
107             }
108             EXPECT_REAL_EQ_TOL(
109                     0.25 * minBoxSize2, max_pull_distance2(&pcrd, &pbc), defaultRealTolerance());
110         }
111
112         {
113             // Distance pulling along Z
114             t_pull_coord params;
115             params.eGeom   = PullGroupGeometry::Distance;
116             params.dim[XX] = 0;
117             params.dim[YY] = 0;
118             params.dim[ZZ] = 1;
119             pull_coord_work_t pcrd(params);
120             clear_dvec(pcrd.spatialData.vec);
121             EXPECT_REAL_EQ_TOL(
122                     0.25 * boxSizeZSquared, max_pull_distance2(&pcrd, &pbc), defaultRealTolerance());
123         }
124
125         {
126             // Directional pulling along Z
127             t_pull_coord params;
128             params.eGeom   = PullGroupGeometry::Direction;
129             params.dim[XX] = 1;
130             params.dim[YY] = 1;
131             params.dim[ZZ] = 1;
132             pull_coord_work_t pcrd(params);
133             clear_dvec(pcrd.spatialData.vec);
134             pcrd.spatialData.vec[ZZ] = 1;
135             EXPECT_REAL_EQ_TOL(
136                     0.25 * boxSizeZSquared, max_pull_distance2(&pcrd, &pbc), defaultRealTolerance());
137         }
138
139         {
140             // Directional pulling along X
141             t_pull_coord params;
142             params.eGeom   = PullGroupGeometry::Direction;
143             params.dim[XX] = 1;
144             params.dim[YY] = 1;
145             params.dim[ZZ] = 1;
146             pull_coord_work_t pcrd(params);
147             clear_dvec(pcrd.spatialData.vec);
148             pcrd.spatialData.vec[XX] = 1;
149
150             real minDist2 = square(box[XX][XX]);
151             for (int d = XX + 1; d < DIM; d++)
152             {
153                 minDist2 -= square(box[d][XX]);
154             }
155             EXPECT_REAL_EQ_TOL(0.25 * minDist2, max_pull_distance2(&pcrd, &pbc), defaultRealTolerance());
156         }
157     }
158 };
159
160 TEST_F(PullTest, MaxPullDistanceXyzScrewBox)
161 {
162     matrix box = { { 10, 0, 0 }, { 0, 10, 0 }, { 0, 0, 10 } };
163
164     test(PbcType::Screw, box);
165 }
166
167 TEST_F(PullTest, MaxPullDistanceXyzCubicBox)
168 {
169     matrix box = { { 10, 0, 0 }, { 0, 10, 0 }, { 0, 0, 10 } };
170
171     test(PbcType::Xyz, box);
172 }
173
174 TEST_F(PullTest, MaxPullDistanceXyzTricBox)
175 {
176     matrix box = { { 10, 0, 0 }, { 3, 10, 0 }, { 3, 4, 10 } };
177
178     test(PbcType::Xyz, box);
179 }
180
181 TEST_F(PullTest, MaxPullDistanceXyzLongBox)
182 {
183     matrix box = { { 10, 0, 0 }, { 0, 10, 0 }, { 0, 0, 30 } };
184
185     test(PbcType::Xyz, box);
186 }
187
188 TEST_F(PullTest, MaxPullDistanceXySkewedBox)
189 {
190     matrix box = { { 10, 0, 0 }, { 5, 8, 0 }, { 0, 0, 0 } };
191
192     test(PbcType::XY, box);
193 }
194
195 } // namespace
196
197 } // namespace gmx