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 force gathering 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 /* Valid input instances */
66 //! A couple of valid inputs for boxes.
67 std::vector<Matrix3x3> const c_sampleBoxes
83 //! A couple of valid inputs for grid sizes
84 std::vector<IVec> const c_sampleGridSizes
94 std::vector<real> const c_sampleChargesFull
96 4.95F, 3.11F, 3.97F, 1.08F, 2.09F, 1.1F, 4.13F, 3.31F, 2.8F, 5.83F, 5.09F, 6.1F, 2.86F, 0.24F, 5.76F, 5.19F, 0.72F
99 //! All the input atom gridline indices
100 std::vector<IVec> const c_sampleGridLineIndicesFull
155 // Spline values/derivatives below are also generated randomly, so they are bogus,
156 // but that should not affect the reproducibility, which we're after
158 //! A lot of bogus input spline values - should have at list (max PME order = 5) * (DIM = 3) * (total unique atom number in all test cases = 16) values
159 std::vector<real> const c_sampleSplineValuesFull
161 0.12F, 0.81F, 0.29F, 0.22F, 0.13F, 0.19F, 0.12F, 0.8F, 0.44F, 0.38F, 0.32F, 0.36F, 0.27F, 0.11F, 0.17F, 0.94F, 0.07F, 0.9F, 0.98F, 0.96F, 0.07F, 0.94F, 0.77F, 0.24F, 0.84F, 0.16F, 0.77F, 0.57F, 0.52F, 0.27F, 0.39F, 0.45F, 0.6F, 0.59F, 0.44F, 0.91F, 0.97F, 0.43F, 0.24F, 0.52F, 0.73F, 0.55F, 0.99F, 0.39F, 0.97F, 0.35F, 0.1F, 0.68F, 0.19F, 0.1F, 0.77F, 0.2F, 0.43F, 0.69F, 0.76F, 0.32F, 0.31F, 0.94F, 0.53F, 0.6F, 0.93F, 0.57F, 0.94F, 0.88F, 0.75F, 0.77F, 0.91F, 0.72F, 0.07F, 0.78F, 0.09F, 0.02F, 0.48F, 0.97F, 0.89F, 0.39F, 0.48F, 0.19F, 0.02F, 0.92F, 0.8F, 0.41F, 0.53F, 0.32F, 0.38F, 0.58F, 0.36F, 0.46F, 0.92F, 0.91F, 0.01F, 0.86F, 0.54F, 0.86F, 0.94F, 0.37F, 0.35F, 0.81F, 0.89F, 0.48F,
162 0.34F, 0.18F, 0.11F, 0.02F, 0.87F, 0.95F, 0.66F, 0.67F, 0.38F, 0.45F, 0.04F, 0.94F, 0.54F, 0.76F, 0.58F, 0.83F, 0.31F, 0.73F, 0.71F, 0.06F, 0.35F, 0.32F, 0.35F, 0.61F, 0.27F, 0.98F, 0.83F, 0.11F, 0.3F, 0.42F, 0.95F, 0.69F, 0.58F, 0.29F, 0.1F, 0.68F, 0.94F, 0.62F, 0.51F, 0.47F, 0.04F, 0.47F, 0.34F, 0.71F, 0.52F, 0.19F, 0.69F, 0.5F, 0.59F, 0.05F, 0.74F, 0.11F, 0.4F, 0.81F, 0.24F, 0.53F, 0.71F, 0.07F, 0.17F, 0.41F, 0.23F, 0.78F, 0.27F, 0.1F, 0.71F, 0.36F, 0.67F, 0.6F, 0.94F, 0.69F, 0.19F, 0.58F, 0.68F, 0.5F, 0.62F, 0.38F, 0.29F, 0.44F, 0.04F, 0.89F, 0.0F, 0.76F, 0.22F, 0.16F, 0.08F, 0.62F, 0.51F, 0.62F, 0.83F, 0.72F, 0.96F, 0.99F, 0.4F, 0.79F, 0.83F, 0.21F, 0.43F, 0.32F, 0.44F, 0.72F,
163 0.21F, 0.4F, 0.93F, 0.07F, 0.11F, 0.41F, 0.24F, 0.04F, 0.36F, 0.15F, 0.92F, 0.08F, 0.99F, 0.35F, 0.42F, 0.7F, 0.17F, 0.39F, 0.69F, 0.0F, 0.86F, 0.89F, 0.59F, 0.81F, 0.77F, 0.15F, 0.89F, 0.17F, 0.76F, 0.67F, 0.58F, 0.78F, 0.26F, 0.19F, 0.69F, 0.18F, 0.46F, 0.6F, 0.69F, 0.23F, 0.34F, 0.3F, 0.64F, 0.34F, 0.6F, 0.99F, 0.69F, 0.57F, 0.75F, 0.07F, 0.36F, 0.75F, 0.81F, 0.8F, 0.42F, 0.09F, 0.94F, 0.66F, 0.35F, 0.67F, 0.34F, 0.66F, 0.02F, 0.47F, 0.78F, 0.21F, 0.02F, 0.18F, 0.42F, 0.2F, 0.46F, 0.34F, 0.4F, 0.46F, 0.96F, 0.86F, 0.25F, 0.25F, 0.22F, 0.37F, 0.59F, 0.19F, 0.45F, 0.61F, 0.04F, 0.71F, 0.77F, 0.51F, 0.77F, 0.15F, 0.78F, 0.36F, 0.62F, 0.24F, 0.86F, 0.2F, 0.77F, 0.08F, 0.09F, 0.3F,
164 0.0F, 0.6F, 0.99F, 0.69F,
167 //! A lot of bogus input spline derivatives - should have at list (max PME order = 5) * (DIM = 3) * (total unique atom number in all test cases = 16) values
168 std::vector<real> const c_sampleSplineDerivativesFull
170 0.82F, 0.88F, 0.83F, 0.11F, 0.93F, 0.32F, 0.71F, 0.37F, 0.69F, 0.88F, 0.11F, 0.38F, 0.25F, 0.5F, 0.36F, 0.81F, 0.78F, 0.31F, 0.66F, 0.32F, 0.27F, 0.35F, 0.53F, 0.83F, 0.08F, 0.08F, 0.94F, 0.71F, 0.65F, 0.24F, 0.13F, 0.01F, 0.33F, 0.65F, 0.24F, 0.53F, 0.45F, 0.84F, 0.33F, 0.97F, 0.31F, 0.7F, 0.03F, 0.31F, 0.41F, 0.76F, 0.12F, 0.3F, 0.57F, 0.65F, 0.87F, 0.99F, 0.42F, 0.97F, 0.32F, 0.39F, 0.73F, 0.23F, 0.03F, 0.67F, 0.97F, 0.57F, 0.42F, 0.38F, 0.54F, 0.17F, 0.53F, 0.54F, 0.18F, 0.8F, 0.76F, 0.13F, 0.29F, 0.83F, 0.77F, 0.56F, 0.4F, 0.87F, 0.36F, 0.18F, 0.59F, 0.04F, 0.05F, 0.61F, 0.26F, 0.91F, 0.62F, 0.16F, 0.89F, 0.23F, 0.26F, 0.59F, 0.33F, 0.2F, 0.49F, 0.41F, 0.25F, 0.4F, 0.16F, 0.83F,
171 0.44F, 0.82F, 0.21F, 0.95F, 0.14F, 0.8F, 0.37F, 0.31F, 0.41F, 0.53F, 0.15F, 0.85F, 0.78F, 0.17F, 0.92F, 0.03F, 0.13F, 0.2F, 0.03F, 0.33F, 0.87F, 0.38F, 0, 0.08F, 0.79F, 0.36F, 0.53F, 0.05F, 0.07F, 0.94F, 0.23F, 0.85F, 0.13F, 0.27F, 0.23F, 0.22F, 0.26F, 0.38F, 0.15F, 0.48F, 0.18F, 0.33F, 0.23F, 0.62F, 0.1F, 0.36F, 0.99F, 0.07F, 0.02F, 0.04F, 0.09F, 0.29F, 0.52F, 0.29F, 0.83F, 0.97F, 0.61F, 0.81F, 0.49F, 0.56F, 0.08F, 0.09F, 0.03F, 0.65F, 0.46F, 0.1F, 0.06F, 0.06F, 0.39F, 0.29F, 0.04F, 0.03F, 0.1F, 0.83F, 0.94F, 0.59F, 0.97F, 0.82F, 0.2F, 0.66F, 0.23F, 0.11F, 0.03F, 0.16F, 0.27F, 0.53F, 0.94F, 0.46F, 0.43F, 0.29F, 0.97F, 0.64F, 0.46F, 0.37F, 0.43F, 0.48F, 0.37F, 0.93F, 0.5F, 0.2F,
172 0.92F, 0.09F, 0.74F, 0.55F, 0.44F, 0.05F, 0.13F, 0.17F, 0.79F, 0.44F, 0.11F, 0.6F, 0.64F, 0.05F, 0.96F, 0.3F, 0.45F, 0.47F, 0.42F, 0.74F, 0.91F, 0.06F, 0.89F, 0.24F, 0.26F, 0.68F, 0.4F, 0.88F, 0.5F, 0.65F, 0.48F, 0.15F, 0.0F, 0.41F, 0.67F, 0.4F, 0.31F, 0.73F, 0.77F, 0.36F, 0.26F, 0.74F, 0.46F, 0.56F, 0.78F, 0.92F, 0.32F, 0.9F, 0.06F, 0.55F, 0.6F, 0.13F, 0.38F, 0.93F, 0.5F, 0.92F, 0.96F, 0.82F, 0.0F, 0.04F, 0.9F, 0.55F, 0.97F, 1.0F, 0.23F, 0.46F, 0.52F, 0.49F, 0.0F, 0.32F, 0.16F, 0.4F, 0.62F, 0.36F, 0.03F, 0.63F, 0.16F, 0.58F, 0.97F, 0.03F, 0.44F, 0.07F, 0.22F, 0.75F, 0.32F, 0.61F, 0.94F, 0.33F, 0.7F, 0.57F, 0.5F, 0.84F, 0.7F, 0.47F, 0.18F, 0.09F, 0.25F, 0.77F, 0.94F, 0.85F,
173 0.09F, 0.83F, 0.02F, 0.91F,
176 //! 2 c_sample grids - only non-zero values have to be listed
177 std::vector<SparseRealGridValuesInput> const c_sampleGrids
179 SparseRealGridValuesInput {{
213 SparseRealGridValuesInput {{
250 //! Input forces for reduction
251 std::vector<RVec> const c_sampleForcesFull {
286 //! PME orders to test
287 std::vector<int> const pmeOrders {
290 //! Atom counts to test
291 std::vector<size_t> const atomCounts {
295 /* Helper structures for test input */
297 //! A structure for all the spline data which depends in size both on the PME order and atom count
298 struct AtomAndPmeOrderSizedData
301 SplineParamsVector splineValues;
302 //! Spline derivatives
303 SplineParamsVector splineDerivatives;
306 //! A structure for all the input atom data which depends in size on atom count - including a range of spline data for different PME orders
310 GridLineIndicesVector gridLineIndices;
312 ChargesVector charges;
314 CoordinatesVector coordinates;
315 //! Spline data for different orders
316 std::map<int, AtomAndPmeOrderSizedData> splineDataByPmeOrder;
319 //! A range of the test input data sets, uniquely identified by the atom count
320 typedef std::map<size_t, AtomSizedData> InputDataByAtomCount;
322 /*! \brief Convenience typedef of the test input parameters - unit cell box, PME interpolation order, grid dimensions,
323 * grid values, overwriting/reducing the input forces, atom count.
325 * The rest of the atom-related input data - gridline indices, spline theta values, spline dtheta values, atom charges -
326 * is looked up in the inputAtomDataSets_ test fixture variable.
328 typedef std::tuple<Matrix3x3, int, IVec, SparseRealGridValuesInput, PmeForceOutputHandling, size_t> GatherInputParameters;
331 class PmeGatherTest : public ::testing::TestWithParam<GatherInputParameters>
334 //! Storage of all the input atom datasets
335 static InputDataByAtomCount s_inputAtomDataSets_;
338 PmeGatherTest() = default;
339 //! Sets the input atom data references once
340 static void SetUpTestCase()
343 for (auto atomCount : atomCounts)
345 AtomSizedData atomData;
346 atomData.gridLineIndices = GridLineIndicesVector(c_sampleGridLineIndicesFull).subArray(start, atomCount);
347 atomData.charges = ChargesVector(c_sampleChargesFull).subArray(start, atomCount);
349 atomData.coordinates.resize(atomCount, RVec {1e6, 1e7, -1e8});
350 /* The coordinates are intentionally bogus in this test - only the size matters; the gridline indices are fed directly as inputs */
351 for (auto pmeOrder : pmeOrders)
353 AtomAndPmeOrderSizedData splineData;
354 const size_t dimSize = atomCount * pmeOrder;
355 for (int dimIndex = 0; dimIndex < DIM; dimIndex++)
357 splineData.splineValues[dimIndex] = SplineParamsDimVector(c_sampleSplineValuesFull).subArray(dimIndex * dimSize, dimSize);
358 splineData.splineDerivatives[dimIndex] = SplineParamsDimVector(c_sampleSplineDerivativesFull).subArray(dimIndex * dimSize, dimSize);
360 atomData.splineDataByPmeOrder[pmeOrder] = splineData;
362 s_inputAtomDataSets_[atomCount] = atomData;
368 /* Getting the input */
373 SparseRealGridValuesInput nonZeroGridValues;
374 PmeForceOutputHandling inputForceTreatment;
375 std::tie(box, pmeOrder, gridSize, nonZeroGridValues, inputForceTreatment, atomCount) = GetParam();
376 auto inputAtomData = s_inputAtomDataSets_[atomCount];
377 auto inputAtomSplineData = inputAtomData.splineDataByPmeOrder[pmeOrder];
379 /* Storing the input where it's needed, running the test */
381 inputRec.nkx = gridSize[XX];
382 inputRec.nky = gridSize[YY];
383 inputRec.nkz = gridSize[ZZ];
384 inputRec.pme_order = pmeOrder;
385 inputRec.coulombtype = eelPME;
386 inputRec.epsilon_r = 1.0;
388 TestReferenceData refData;
389 for (const auto &context : getPmeTestEnv()->getHardwareContexts())
391 CodePath codePath = context->getCodePath();
392 const bool supportedInput = pmeSupportsInputForMode(*getPmeTestEnv()->hwinfo(), &inputRec, codePath);
395 /* Testing the failure for the unsupported input */
396 EXPECT_THROW(pmeInitWrapper(&inputRec, codePath, nullptr, nullptr, box), NotImplementedError);
400 /* Describing the test uniquely */
401 SCOPED_TRACE(formatString("Testing force gathering with %s %sfor PME grid size %d %d %d"
402 ", order %d, %zu atoms, %s",
403 codePathToString(codePath), context->getDescription().c_str(),
404 gridSize[XX], gridSize[YY], gridSize[ZZ],
407 (inputForceTreatment == PmeForceOutputHandling::ReduceWithInput) ? "with reduction" : "without reduction"
410 PmeSafePointer pmeSafe = pmeInitWrapper(&inputRec, codePath, context->getDeviceInfo(), context->getPmeGpuProgram(), box);
411 StatePropagatorDataGpu stateGpu = makeStatePropagatorDataGpu(*pmeSafe.get());
412 pmeInitAtoms(pmeSafe.get(), &stateGpu, codePath, inputAtomData.coordinates, inputAtomData.charges);
414 /* Setting some more inputs */
415 pmeSetRealGrid(pmeSafe.get(), codePath, nonZeroGridValues);
416 pmeSetGridLineIndices(pmeSafe.get(), codePath, inputAtomData.gridLineIndices);
417 for (int dimIndex = 0; dimIndex < DIM; dimIndex++)
419 pmeSetSplineData(pmeSafe.get(), codePath, inputAtomSplineData.splineValues[dimIndex], PmeSplineDataType::Values, dimIndex);
420 pmeSetSplineData(pmeSafe.get(), codePath, inputAtomSplineData.splineDerivatives[dimIndex], PmeSplineDataType::Derivatives, dimIndex);
423 /* Explicitly copying the sample forces to be able to modify them */
424 auto inputForcesFull(c_sampleForcesFull);
425 GMX_RELEASE_ASSERT(inputForcesFull.size() >= atomCount, "Bad input forces size");
426 auto forces = ForcesVector(inputForcesFull).subArray(0, atomCount);
428 /* Running the force gathering itself */
429 pmePerformGather(pmeSafe.get(), codePath, inputForceTreatment, forces);
430 pmeFinalizeTest(pmeSafe.get(), codePath);
432 /* Check the output forces correctness */
433 TestReferenceChecker forceChecker(refData.rootChecker());
434 const auto ulpTolerance = 3 * pmeOrder;
435 forceChecker.setDefaultTolerance(relativeToleranceAsUlp(1.0, ulpTolerance));
436 forceChecker.checkSequence(forces.begin(), forces.end(), "Forces");
441 // An instance of static atom data
442 InputDataByAtomCount PmeGatherTest::s_inputAtomDataSets_;
444 //! Test for PME force gathering
445 TEST_P(PmeGatherTest, ReproducesOutputs)
447 EXPECT_NO_THROW(runTest());
450 //! Instantiation of the PME gathering test
451 INSTANTIATE_TEST_CASE_P(SaneInput, PmeGatherTest, ::testing::Combine(::testing::ValuesIn(c_sampleBoxes),
452 ::testing::ValuesIn(pmeOrders),
453 ::testing::ValuesIn(c_sampleGridSizes),
454 ::testing::ValuesIn(c_sampleGrids),
455 ::testing::Values(PmeForceOutputHandling::Set, PmeForceOutputHandling::ReduceWithInput),
456 ::testing::ValuesIn(atomCounts)));