b57d2dd91bcd931ed3b7b9a1b33b35d6c01fec3f
[alexxy/gromacs.git] / src / gromacs / ewald / tests / pmebsplinetest.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2017,2018, 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 PME B-spline moduli computation tests.
38  *
39  * \author Aleksei Iupinov <a.yupinov@gmail.com>
40  * \ingroup module_ewald
41  */
42
43 #include "gmxpre.h"
44
45 #include <string>
46
47 #include <gmock/gmock.h>
48
49 #include "gromacs/ewald/pme-internal.h"
50 #include "gromacs/math/vectypes.h"
51 #include "gromacs/mdtypes/inputrec.h"
52 #include "gromacs/utility/stringutil.h"
53
54 #include "testutils/refdata.h"
55 #include "testutils/testasserts.h"
56
57 #include "pmetestcommon.h"
58
59 namespace gmx
60 {
61 namespace test
62 {
63 namespace
64 {
65 /*! \brief Moduli algorithm type */
66 enum class ModuliType
67 {
68     PME,
69     P3M
70 };
71
72 /*! \brief Convenience typedef of input parameters - grid dimensions, PME interpolation order, moduli type */
73 typedef std::tuple<IVec, int, ModuliType> BSplineModuliInputParameters;
74
75 /*! \brief Test fixture for testing PME B-spline moduli creation */
76 class PmeBSplineModuliTest : public ::testing::TestWithParam<BSplineModuliInputParameters>
77 {
78     public:
79         PmeBSplineModuliTest() = default;
80         //! The whole logic being tested is contained here
81         void runTest()
82         {
83             /* Getting the input */
84             const BSplineModuliInputParameters parameters = GetParam();
85             int         pmeOrder;
86             IVec        gridSize;
87             ModuliType  moduliType;
88             std::tie(gridSize, pmeOrder, moduliType) = parameters;
89
90             /* Describing the test in case it fails */
91             SCOPED_TRACE(formatString("Testing B-spline moduli creation (%s) for PME order %d, grid size %d %d %d",
92                                       (moduliType == ModuliType::P3M) ? "P3M" : "plain",
93                                       pmeOrder,
94                                       gridSize[XX], gridSize[YY], gridSize[ZZ]));
95
96             /* Storing the input where it's needed */
97             t_inputrec inputRec;
98             inputRec.nkx         = gridSize[XX];
99             inputRec.nky         = gridSize[YY];
100             inputRec.nkz         = gridSize[ZZ];
101             inputRec.coulombtype = (moduliType == ModuliType::P3M) ? eelP3M_AD : eelPME;
102             inputRec.pme_order   = pmeOrder;
103
104             /* PME initialization call which checks the inputs and computes the B-spline moduli according to the grid sizes. */
105             PmeSafePointer pme = pmeInitEmpty(&inputRec);
106
107             /* Setting up the checker */
108             TestReferenceData    refData;
109             TestReferenceChecker checker(refData.rootChecker());
110             checker.setDefaultTolerance(relativeToleranceAsPrecisionDependentUlp(1.0,
111                                                                                  c_splineModuliSinglePrecisionUlps,
112                                                                                  getSplineModuliDoublePrecisionUlps(pmeOrder+1)));
113
114             /* Perform a correctness check */
115             const char *dimString[] = { "X", "Y", "Z" };
116             for (int i = 0; i < DIM; i++)
117             {
118                 checker.checkSequenceArray(gridSize[i], pme->bsp_mod[i], dimString[i]);
119             }
120         }
121 };
122
123 // Different aliases are needed to reuse the test class without instantiating tests more than once
124 //! Failure test alias
125 using PmeBSplineModuliFailureTest = PmeBSplineModuliTest;
126 TEST_P(PmeBSplineModuliFailureTest, Throws)
127 {
128     EXPECT_THROW(runTest(), InconsistentInputError);
129 }
130 //! Correctness test alias
131 using PmeBSplineModuliCorrectnessTest = PmeBSplineModuliTest;
132 TEST_P(PmeBSplineModuliCorrectnessTest, ReproducesValues)
133 {
134     EXPECT_NO_THROW(runTest());
135 }
136
137 /* Invalid input instances */
138
139 //! Sane interpolation order
140 const int       sanePmeOrder = 4;
141 //! Sane grid size
142 const IVec      saneGridSize = {32, 25, 47};
143 /*! \brief Hand-picked invalid input for the exception tests */
144 std::vector<BSplineModuliInputParameters> const invalidInputs
145 {
146     /* Invalid grid sizes */
147     BSplineModuliInputParameters {
148         IVec {
149             -1, 10, 10
150         }, sanePmeOrder, ModuliType::P3M
151     },
152     BSplineModuliInputParameters {
153         IVec {
154             40, 0, 20
155         }, sanePmeOrder, ModuliType::P3M
156     },
157     BSplineModuliInputParameters {
158         IVec {
159             64, 2, 64
160         }, sanePmeOrder, ModuliType::PME
161     },
162     /* Invalid interpolation orders */
163     BSplineModuliInputParameters {
164         saneGridSize, 8 + 1, ModuliType::P3M  // P3M only supports orders up to 8
165     },
166     BSplineModuliInputParameters {
167         saneGridSize, PME_ORDER_MAX + 1, ModuliType::PME
168     },
169 };
170
171 /*! \brief Instantiation of the PME B-spline moduli creation test with invalid input */
172 INSTANTIATE_TEST_CASE_P(InsaneInput, PmeBSplineModuliFailureTest, ::testing::ValuesIn(invalidInputs));
173
174 /* Valid input instances */
175
176 //! A couple of valid inputs for grid sizes. It is good to test both even and odd dimensions.
177 std::vector<IVec> const sampleGridSizes
178 {
179     IVec {
180         64, 32, 64
181     },
182     IVec {
183         57, 84, 29
184     }
185 };
186
187 /*! \brief Instantiation of the PME B-spline moduli creation test with valid input - up to order of 8 */
188 INSTANTIATE_TEST_CASE_P(SaneInput1, PmeBSplineModuliCorrectnessTest, ::testing::Combine(
189                                     ::testing::ValuesIn(sampleGridSizes),
190                                     ::testing::Range(3, 8 + 1),
191                                     ::testing::Values(ModuliType::PME, ModuliType::P3M)
192                                 ));
193 }  // namespace
194 }  // namespace test
195 }  // namespace gmx