2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018,2019, 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.
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.
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.
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.
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.
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.
37 * Implements PME spline computation and charge spreading tests.
39 * \author Aleksei Iupinov <a.yupinov@gmail.com>
40 * \ingroup module_ewald
47 #include <gmock/gmock.h>
49 #include "gromacs/mdtypes/inputrec.h"
50 #include "gromacs/utility/stringutil.h"
52 #include "testutils/refdata.h"
53 #include "testutils/testasserts.h"
55 #include "pmetestcommon.h"
64 //! PME spline and spread code path being tested
65 enum class PmeSplineAndSpreadOptions
69 SplineAndSpreadUnified
72 /*! \brief Convenience typedef of input parameters - unit cell box, PME interpolation order, grid
73 * dimensions, particle coordinates, particle charges
74 * TODO: consider inclusion of local grid offsets/sizes or PME nodes counts to test the PME DD
76 typedef std::tuple<Matrix3x3, int, IVec, CoordinatesVector, ChargesVector> SplineAndSpreadInputParameters;
78 /*! \brief Test fixture for testing both atom spline parameter computation and charge spreading.
79 * These 2 stages of PME are tightly coupled in the code.
81 class PmeSplineAndSpreadTest : public ::testing::TestWithParam<SplineAndSpreadInputParameters>
84 PmeSplineAndSpreadTest() = default;
88 /* Getting the input */
92 CoordinatesVector coordinates;
93 ChargesVector charges;
95 std::tie(box, pmeOrder, gridSize, coordinates, charges) = GetParam();
96 const size_t atomCount = coordinates.size();
98 /* Storing the input where it's needed */
100 inputRec.nkx = gridSize[XX];
101 inputRec.nky = gridSize[YY];
102 inputRec.nkz = gridSize[ZZ];
103 inputRec.pme_order = pmeOrder;
104 inputRec.coulombtype = eelPME;
105 inputRec.epsilon_r = 1.0;
107 TestReferenceData refData;
109 const std::map<PmeSplineAndSpreadOptions, std::string> optionsToTest = {
110 { PmeSplineAndSpreadOptions::SplineAndSpreadUnified,
111 "spline computation and charge spreading (fused)" },
112 { PmeSplineAndSpreadOptions::SplineOnly, "spline computation" },
113 { PmeSplineAndSpreadOptions::SpreadOnly, "charge spreading" }
116 // There is a subtle problem with multiple comparisons against same reference data:
117 // The subsequent (GPU) spreading runs at one point didn't actually copy the output grid
118 // into the proper buffer, but the reference data was already marked as checked
119 // (hasBeenChecked_) by the CPU run, so nothing failed. For now we will manually track that
120 // the count of the grid entries is the same on each run. This is just a hack for a single
121 // specific output though. What would be much better TODO is to split different codepaths
122 // into separate tests, while making them use the same reference files.
123 bool gridValuesSizeAssigned = false;
124 size_t previousGridValuesSize;
126 for (const auto& context : getPmeTestEnv()->getHardwareContexts())
128 CodePath codePath = context->getCodePath();
129 const bool supportedInput =
130 pmeSupportsInputForMode(*getPmeTestEnv()->hwinfo(), &inputRec, codePath);
133 /* Testing the failure for the unsupported input */
134 EXPECT_THROW(pmeInitWrapper(&inputRec, codePath, nullptr, nullptr, box), NotImplementedError);
138 for (const auto& option : optionsToTest)
140 /* Describing the test uniquely in case it fails */
143 formatString("Testing %s with %s %sfor PME grid size %d %d %d"
144 ", order %d, %zu atoms",
145 option.second.c_str(), codePathToString(codePath),
146 context->getDescription().c_str(), gridSize[XX], gridSize[YY],
147 gridSize[ZZ], pmeOrder, atomCount));
149 /* Running the test */
151 PmeSafePointer pmeSafe = pmeInitWrapper(&inputRec, codePath, context->getDeviceInfo(),
152 context->getPmeGpuProgram(), box);
153 std::unique_ptr<StatePropagatorDataGpu> stateGpu =
154 (codePath == CodePath::GPU) ? makeStatePropagatorDataGpu(*pmeSafe.get()) : nullptr;
156 pmeInitAtoms(pmeSafe.get(), stateGpu.get(), codePath, coordinates, charges);
158 const bool computeSplines =
159 (option.first == PmeSplineAndSpreadOptions::SplineOnly)
160 || (option.first == PmeSplineAndSpreadOptions::SplineAndSpreadUnified);
161 const bool spreadCharges =
162 (option.first == PmeSplineAndSpreadOptions::SpreadOnly)
163 || (option.first == PmeSplineAndSpreadOptions::SplineAndSpreadUnified);
167 // Here we should set up the results of the spline computation so that the spread can run.
168 // What is lazy and works is running the separate spline so that it will set it up for us:
169 pmePerformSplineAndSpread(pmeSafe.get(), codePath, true, false);
170 // We know that it is tested in another iteration.
171 // TODO: Clean alternative: read and set the reference gridline indices, spline params
174 pmePerformSplineAndSpread(pmeSafe.get(), codePath, computeSplines, spreadCharges);
175 pmeFinalizeTest(pmeSafe.get(), codePath);
177 /* Outputs correctness check */
178 /* All tolerances were picked empirically for single precision on CPU */
180 TestReferenceChecker rootChecker(refData.rootChecker());
182 const auto maxGridSize = std::max(std::max(gridSize[XX], gridSize[YY]), gridSize[ZZ]);
183 const auto ulpToleranceSplineValues = 4 * (pmeOrder - 2) * maxGridSize;
184 /* 4 is a modest estimate for amount of operations; (pmeOrder - 2) is a number of iterations;
185 * maxGridSize is inverse of the smallest positive fractional coordinate (which are interpolated by the splines).
190 const char* dimString[] = { "X", "Y", "Z" };
193 SCOPED_TRACE(formatString("Testing spline values with tolerance of %d",
194 ulpToleranceSplineValues));
195 TestReferenceChecker splineValuesChecker(
196 rootChecker.checkCompound("Splines", "Values"));
197 splineValuesChecker.setDefaultTolerance(
198 relativeToleranceAsUlp(1.0, ulpToleranceSplineValues));
199 for (int i = 0; i < DIM; i++)
201 auto splineValuesDim =
202 pmeGetSplineData(pmeSafe.get(), codePath, PmeSplineDataType::Values, i);
203 splineValuesChecker.checkSequence(splineValuesDim.begin(),
204 splineValuesDim.end(), dimString[i]);
207 /* Spline derivatives */
208 const auto ulpToleranceSplineDerivatives = 4 * ulpToleranceSplineValues;
209 /* 4 is just a wild guess since the derivatives are deltas of neighbor spline values which could differ greatly */
210 SCOPED_TRACE(formatString("Testing spline derivatives with tolerance of %d",
211 ulpToleranceSplineDerivatives));
212 TestReferenceChecker splineDerivativesChecker(
213 rootChecker.checkCompound("Splines", "Derivatives"));
214 splineDerivativesChecker.setDefaultTolerance(
215 relativeToleranceAsUlp(1.0, ulpToleranceSplineDerivatives));
216 for (int i = 0; i < DIM; i++)
218 auto splineDerivativesDim = pmeGetSplineData(
219 pmeSafe.get(), codePath, PmeSplineDataType::Derivatives, i);
220 splineDerivativesChecker.checkSequence(
221 splineDerivativesDim.begin(), splineDerivativesDim.end(), dimString[i]);
224 /* Particle gridline indices */
225 auto gridLineIndices = pmeGetGridlineIndices(pmeSafe.get(), codePath);
226 rootChecker.checkSequence(gridLineIndices.begin(), gridLineIndices.end(),
232 /* The wrapped grid */
233 SparseRealGridValuesOutput nonZeroGridValues = pmeGetRealGrid(pmeSafe.get(), codePath);
234 TestReferenceChecker gridValuesChecker(
235 rootChecker.checkCompound("NonZeroGridValues", "RealSpaceGrid"));
236 const auto ulpToleranceGrid =
237 2 * ulpToleranceSplineValues
238 * static_cast<int>(ceil(sqrt(static_cast<real>(atomCount))));
239 /* 2 is empiric; sqrt(atomCount) assumes all the input charges may spread onto the same cell */
240 SCOPED_TRACE(formatString("Testing grid values with tolerance of %d", ulpToleranceGrid));
241 if (!gridValuesSizeAssigned)
243 previousGridValuesSize = nonZeroGridValues.size();
244 gridValuesSizeAssigned = true;
248 EXPECT_EQ(previousGridValuesSize, nonZeroGridValues.size());
251 gridValuesChecker.setDefaultTolerance(relativeToleranceAsUlp(1.0, ulpToleranceGrid));
252 for (const auto& point : nonZeroGridValues)
254 gridValuesChecker.checkReal(point.second, point.first.c_str());
263 /*! \brief Test for spline parameter computation and charge spreading. */
264 TEST_P(PmeSplineAndSpreadTest, ReproducesOutputs)
266 EXPECT_NO_THROW(runTest());
269 /* Valid input instances */
271 //! A couple of valid inputs for boxes.
272 std::vector<Matrix3x3> const c_sampleBoxes{
274 Matrix3x3{ { 8.0F, 0.0F, 0.0F, 0.0F, 3.4F, 0.0F, 0.0F, 0.0F, 2.0F } },
276 Matrix3x3{ { 7.0F, 0.0F, 0.0F, 0.0F, 4.1F, 0.0F, 3.5F, 2.0F, 12.2F } },
279 //! A couple of valid inputs for grid sizes.
280 std::vector<IVec> const c_sampleGridSizes{ IVec{ 16, 12, 14 }, IVec{ 19, 17, 11 } };
283 std::vector<real> const c_sampleChargesFull{ 4.95F, 3.11F, 3.97F, 1.08F, 2.09F, 1.1F,
284 4.13F, 3.31F, 2.8F, 5.83F, 5.09F, 6.1F,
285 2.86F, 0.24F, 5.76F, 5.19F, 0.72F };
287 auto const c_sampleCharges1 = ChargesVector(c_sampleChargesFull).subArray(0, 1);
289 auto const c_sampleCharges2 = ChargesVector(c_sampleChargesFull).subArray(1, 2);
291 auto const c_sampleCharges13 = ChargesVector(c_sampleChargesFull).subArray(3, 13);
293 //! Random coordinate vectors
294 CoordinatesVector const c_sampleCoordinatesFull{ { 5.59F, 1.37F, 0.95F },
296 16.0F, 1.02F, 0.22F // 2 box lengths in x
298 { 0.034F, 1.65F, 0.22F },
299 { 0.33F, 0.92F, 1.56F },
300 { 1.16F, 0.75F, 0.39F },
301 { 0.5F, 1.63F, 1.14F },
303 16.0001F, 1.52F, 1.19F // > 2 box lengths in x
306 1.43F, 1.1F, 4.1F // > 2 box lengths in z
309 -1.08F, 1.19F, 0.08F // negative x
311 { 1.6F, 0.93F, 0.53F },
313 1.32F, -1.48F, 0.16F // negative y
315 { 0.87F, 0.0F, 0.33F },
317 0.95F, 7.7F, -0.48F // > 2 box lengths in y, negative z
319 { 1.23F, 0.91F, 0.68F },
320 { 0.19F, 1.45F, 0.94F },
321 { 1.28F, 0.46F, 0.38F },
322 { 1.21F, 0.23F, 1.0F } };
323 //! 1 coordinate vector
324 CoordinatesVector const c_sampleCoordinates1(c_sampleCoordinatesFull.begin(),
325 c_sampleCoordinatesFull.begin() + 1);
326 //! 2 coordinate vectors
327 CoordinatesVector const c_sampleCoordinates2(c_sampleCoordinatesFull.begin() + 1,
328 c_sampleCoordinatesFull.begin() + 3);
329 //! 13 coordinate vectors
330 CoordinatesVector const c_sampleCoordinates13(c_sampleCoordinatesFull.begin() + 3,
331 c_sampleCoordinatesFull.begin() + 16);
333 //! moved out from instantiantions for readability
334 auto c_inputBoxes = ::testing::ValuesIn(c_sampleBoxes);
335 //! moved out from instantiantions for readability
336 auto c_inputPmeOrders = ::testing::Range(3, 5 + 1);
337 //! moved out from instantiantions for readability
338 auto c_inputGridSizes = ::testing::ValuesIn(c_sampleGridSizes);
340 /*! \brief Instantiation of the test with valid input and 1 atom */
341 INSTANTIATE_TEST_CASE_P(SaneInput1,
342 PmeSplineAndSpreadTest,
343 ::testing::Combine(c_inputBoxes,
346 ::testing::Values(c_sampleCoordinates1),
347 ::testing::Values(c_sampleCharges1)));
349 /*! \brief Instantiation of the test with valid input and 2 atoms */
350 INSTANTIATE_TEST_CASE_P(SaneInput2,
351 PmeSplineAndSpreadTest,
352 ::testing::Combine(c_inputBoxes,
355 ::testing::Values(c_sampleCoordinates2),
356 ::testing::Values(c_sampleCharges2)));
357 /*! \brief Instantiation of the test with valid input and 13 atoms */
358 INSTANTIATE_TEST_CASE_P(SaneInput13,
359 PmeSplineAndSpreadTest,
360 ::testing::Combine(c_inputBoxes,
363 ::testing::Values(c_sampleCoordinates13),
364 ::testing::Values(c_sampleCharges13)));