Quiet warning
[gromacs.git] / src / gromacs / mdlib / tests / settle.cpp
blob542f3d1f278a412cf7091bef3c4c3e91675fc8d2
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016,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.
35 /*! \internal \file
36 * \brief Tests for SETTLE constraints
38 * The test runs on several small systems, containing 1 to 17 water molecules,
39 * with and without periodic boundary conditions, with and without velocity
40 * and virial updates. The CPU and GPU versions are tested, if the code was
41 * compiled with CUDA support and there is a CUDA-capable GPU in the system.
43 * The tests check:
44 * 1. If the final distances between constrained atoms are within tolerance
45 * from the target distance.
46 * 2. If the velocities were updated when needed.
47 * 3. If the virial was computed.
49 * The test also compares the results from the CPU and GPU versions of the
50 * algorithm: final coordinates, velocities and virial should be within
51 * tolerance to one another.
53 * \todo This also tests that if the calling code requires velocities
54 * and virial updates, that those outputs do change, but does not
55 * test that those changes are correct.
57 * \todo Only no-PBC and cubic-PBC are tested here, but the correct
58 * function of the SIMD version of set_pbx_auic in all cases
59 * should be tested elsewhere.
61 * \todo The CPU and GPU versions are tested against each other. This
62 * should be changed to a proper test against pre-computed
63 * reference values. Also, these test will dry-run on a CUDA
64 * build if no CUDA-capable GPU is available.
66 * \author Mark Abraham <mark.j.abraham@gmail.com>
67 * \author Artem Zhmurov <zhmurov@gmail.com>
69 * \ingroup module_mdlib
71 #include "gmxpre.h"
73 #include "gromacs/mdlib/settle.h"
75 #include "config.h"
77 #include <algorithm>
78 #include <unordered_map>
79 #include <vector>
81 #include <gtest/gtest.h>
83 #include "gromacs/gpu_utils/gpu_testutils.h"
84 #include "gromacs/math/vec.h"
85 #include "gromacs/math/vectypes.h"
86 #include "gromacs/mdtypes/mdatom.h"
87 #include "gromacs/pbcutil/pbc.h"
88 #include "gromacs/topology/idef.h"
89 #include "gromacs/topology/ifunc.h"
90 #include "gromacs/topology/topology.h"
91 #include "gromacs/utility/stringutil.h"
92 #include "gromacs/utility/unique_cptr.h"
94 #include "gromacs/mdlib/tests/watersystem.h"
95 #include "testutils/testasserts.h"
97 #include "settletestdata.h"
98 #include "settletestrunners.h"
100 namespace gmx
102 namespace test
104 namespace
107 /*! \brief Parameters that will vary from test to test.
109 * The test will run in the following parameter space:
110 * 1. Number of water molecules (SETTLEs) [1, 2, 4, 5, 7, 10, 12, 15, 17]
111 * 2. If the velocities should be updated while constraining [true/false]
112 * 3. If the virial should be computed [true/false]
113 * 4. Periodic boundary conditions [XYZ/None]
114 * 5. Subroutine to use [CPU/GPU version of SETTLE]
116 typedef std::tuple<int, bool, bool, std::string, std::string> SettleTestParameters;
119 //! List of all availible algortihms/implementations.
120 std::vector<std::string> algorithmsNames;
122 //! Method that fills and returns algorithmNames to the test macros.
123 std::vector<std::string> getAlgorithmsNames()
125 algorithmsNames.emplace_back("SETTLE");
126 // TODO: Here we should check that at least 1 suitable GPU is available
127 if (GMX_GPU == GMX_GPU_CUDA && canComputeOnGpu())
129 algorithmsNames.emplace_back("SETTLE_GPU");
131 return algorithmsNames;
134 /*! \brief Test fixture for testing SETTLE position updates.
137 class SettleTest : public ::testing::TestWithParam<SettleTestParameters>
139 public:
140 //! PBC setups
141 std::unordered_map <std::string, t_pbc> pbcs_;
142 //! Algorithms (CPU and GPU versions of SETTLE)
143 std::unordered_map <std::string, void(*)(SettleTestData *testData,
144 const t_pbc pbc,
145 const bool updateVelocities,
146 const bool calcVirial,
147 const std::string &testDescription)> algorithms_;
149 /*! \brief Test setup function.
151 * Setting up the PBCs and algorithms. Note, that corresponding string keywords
152 * have to be explicitly added at the end of this file when the tests are called.
155 void SetUp() override
159 // PBC initialization
161 t_pbc pbc;
163 // Infinitely small box
164 matrix boxNone = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };
165 set_pbc(&pbc, epbcNONE, boxNone);
166 pbcs_["PBCNone"] = pbc;
168 // Rectangular box
169 matrix boxXyz = {{real(1.86206), 0, 0}, {0, real(1.86206), 0}, {0, 0, real(1.86206)}};
170 set_pbc(&pbc, epbcXYZ, boxXyz);
171 pbcs_["PBCXYZ"] = pbc;
174 // Algorithms
176 algorithms_["SETTLE"] = applySettle;
177 algorithms_["SETTLE_GPU"] = applySettleGpu;
180 /*! \brief Check if the positions and velocities were constrained correctly.
182 * The final distance between constrained atoms is compared to the target distance. The velocities are
183 * not properly tested, only the fact that they were updated is checked.
185 * \todo Make a proper test for both coordinates and velocities. They should be tested against known
186 * the final values.
188 * \param[in] numSettles Number of water molecules in the tested system.
189 * \param[in] updateVelocities If the velocities were updated.
190 * \param[in] tolerance Tolerance to compare floating point numbers.
191 * \param[in] testDescription Brief description that will be printed in case of test failure.
192 * \param[in] testData An object, containing all the data structures needed by SETTLE.
194 void checkFinalPositionsAndVelocities(const int numSettles,
195 const bool updateVelocities,
196 const FloatingPointTolerance tolerance,
197 const std::string &testDescription,
198 const SettleTestData &testData)
200 for (int i = 0; i < numSettles; ++i)
202 const gmx::RVec &positionO = testData.xPrime_[i*3 + 0];
203 const gmx::RVec &positionH1 = testData.xPrime_[i*3 + 1];
204 const gmx::RVec &positionH2 = testData.xPrime_[i*3 + 2];
206 real dOH = testData.dOH_;
207 real dHH = testData.dHH_;
209 EXPECT_REAL_EQ_TOL(dOH*dOH, distance2(positionO, positionH1), tolerance) << formatString("for water %d ", i) << testDescription;
210 EXPECT_REAL_EQ_TOL(dOH*dOH, distance2(positionO, positionH2), tolerance) << formatString("for water %d ", i) << testDescription;
211 EXPECT_REAL_EQ_TOL(dHH*dHH, distance2(positionH1, positionH2), tolerance) << formatString("for water %d ", i) << testDescription;
213 // This merely tests whether the velocities were
214 // updated from the starting values of zero (or not),
215 // but not whether the update was correct.
216 for (int j = 0; j < testData.atomsPerSettle_; ++j)
218 for (int d = 0; d < DIM; ++d)
220 EXPECT_TRUE(updateVelocities == (0. != testData.v_[i*3 + j][d]))
221 << formatString("for water %d velocity atom %d dim %d", i, j, d)
222 << testDescription;
228 /*! \brief Check if the virial was updated.
230 * The two tests on virial are:
231 * 1. If it was updated in case calcVirial is true.
232 * 2. If it is symmetrical.
234 * \todo Test against pre-computed values should be added.
236 * \param[in] calcVirial If the virial is computed.
237 * \param[in] tolerance Tolerance to compare floating point numbers.
238 * \param[in] testDescription Brief description that will be printed in case of test failure.
239 * \param[in] testData An object, containing all the data structures needed by SETTLE.
241 void checkVirial(const bool calcVirial,
242 const FloatingPointTolerance tolerance,
243 const std::string &testDescription,
244 const SettleTestData &testData)
246 // This merely tests whether the viral was updated from
247 // the starting values of zero (or not), but not whether
248 // any update was correct. The symmetry of the tensor is
249 // also tested.
250 for (int d = 0; d < DIM; ++d)
252 for (int dd = 0; dd < DIM; ++dd)
255 EXPECT_TRUE(calcVirial == (0. != testData.virial_[d][dd]))
256 << formatString("for virial component[%d][%d] ", d, dd)
257 << testDescription;
259 if (calcVirial)
261 EXPECT_REAL_EQ_TOL(testData.virial_[d][dd], testData.virial_[dd][d], tolerance)
262 << formatString("Virial is not symmetrical for [%d][%d] ", d, dd)
263 << testDescription;
269 /*! \brief Check if two sets of coordinates are equal up to the provided tolerance.
271 * This is used to compare the positions and velocities, obtained on the CPU and GPU between
272 * each other.
274 * \todo Values should be compared to pre-computed reference values instead.
276 * \param[in] coordinates Coordinates to check.
277 * \param[in] coordinatesRef Reference values for the constrained coordinates.
278 * \param[in] tolerance Tolerance to compare floating point numbers
279 * \param[in] testDescription Brief description that will be printed in case of test failure.
281 void checkCoordinatesEqual(const gmx::PaddedVector<gmx::RVec> &coordinates,
282 const gmx::PaddedVector<gmx::RVec> &coordinatesRef,
283 const FloatingPointTolerance tolerance,
284 const std::string &testDescription)
286 EXPECT_EQ(coordinates.size(), coordinatesRef.size());
287 for (index i = 0; i < ssize(coordinates); ++i)
289 for (int d = 0; d < DIM; ++d)
291 EXPECT_REAL_EQ_TOL(coordinates[i][d], coordinatesRef[i][d], tolerance)
292 << formatString("Different coordinates in GPU and CPU codes for particle %zd component %d ", i, d)
293 << testDescription;
298 /*! \brief Check if two tensors are equal up to the tolerance.
300 * This is used to check if the computation of virial works similarly on CPU and GPU.
302 * \todo Values should be compared to pre-computed reference values instead.
304 * \param[in] virial Tensor to check.
305 * \param[in] virialRef Reference values for the tensor.
306 * \param[in] tolerance Tolerance to compare floating point numbers.
307 * \param[in] testDescription Brief description that will be printed in case of test failure.
309 void checkTensorsEqual(const tensor virial,
310 const tensor virialRef,
311 const FloatingPointTolerance tolerance,
312 const std::string &testDescription)
314 for (int d1 = 0; d1 < DIM; ++d1)
316 for (int d2 = 0; d2 < DIM; ++d2)
318 EXPECT_REAL_EQ_TOL(virial[d1][d2], virialRef[d1][d2], tolerance)
319 << formatString("Different tensor components in GPU and CPU codes for components %d and %d ", d1, d2)
320 << testDescription;
325 //! Store whether any compatible GPUs exist.
326 static bool s_hasCompatibleGpus;
327 //! Before any test is run, work out whether any compatible GPUs exist.
328 static void SetUpTestCase()
330 s_hasCompatibleGpus = canComputeOnGpu();
334 bool SettleTest::s_hasCompatibleGpus = false;
336 TEST_P(SettleTest, SatisfiesConstraints)
338 int numSettles;
339 bool updateVelocities, calcVirial;
340 std::string pbcName;
341 std::string algorithmName;
342 // Make some symbolic names for the parameter combination.
343 std::tie(numSettles, updateVelocities, calcVirial, pbcName, algorithmName) = GetParam();
345 // Make a string that describes which parameter combination is
346 // being tested, to help make failing tests comprehensible.
347 std::string testDescription = formatString("while testing %s with %d SETTLEs, %s, %svelocities and %scalculating the virial",
348 algorithmName.c_str(),
349 numSettles,
350 pbcName.c_str(),
351 updateVelocities ? "with " : "without ",
352 calcVirial ? "" : "not ");
354 auto testData = std::make_unique<SettleTestData>(numSettles);
356 ASSERT_LE(numSettles, testData->xPrime_.size() / testData->atomsPerSettle_) << "cannot test that many SETTLEs " << testDescription;
358 t_pbc pbc = pbcs_.at(pbcName);
360 algorithms_.at(algorithmName)(testData.get(),
361 pbc,
362 updateVelocities,
363 calcVirial,
364 testDescription);
366 // The necessary tolerances for the test to pass were determined
367 // empirically. This isn't nice, but the required behavior that
368 // SETTLE produces constrained coordinates consistent with
369 // sensible sampling needs to be tested at a much higher level.
370 real dOH = testData->dOH_;
371 FloatingPointTolerance tolerance = relativeToleranceAsPrecisionDependentUlp(dOH*dOH, 80, 380);
372 FloatingPointTolerance toleranceVirial = absoluteTolerance(0.000001);
374 checkFinalPositionsAndVelocities(numSettles, updateVelocities, tolerance, testDescription, *testData);
375 checkVirial(calcVirial, toleranceVirial, testDescription, *testData);
378 #if GMX_GPU == GMX_GPU_CUDA
380 TEST_P(SettleTest, CompareCpuAndGpu)
382 // CUDA version will be tested only if:
383 // 1. The code was compiled with CUDA
384 // 2. There is a CUDA-capable GPU in a system
385 // 3. This GPU is detectable
386 // 4. GPU detection was not disabled by GMX_DISABLE_GPU_DETECTION environment variable
387 if (s_hasCompatibleGpus)
389 int numSettles;
390 bool updateVelocities, calcVirial;
391 std::string pbcName;
392 std::string algorithmName;
393 // Make some symbolic names for the parameter combination.
394 std::tie(numSettles, updateVelocities, calcVirial, pbcName, algorithmName) = GetParam();
396 // Make a string that describes which parameter combination is
397 // being tested, to help make failing tests comprehensible.
398 std::string testDescription = formatString("while comparing CPU and GPU implementations with %d SETTLEs, %s, %svelocities and %scalculating the virial",
399 numSettles,
400 pbcName.c_str(),
401 updateVelocities ? "with " : "without ",
402 calcVirial ? "" : "not ");
404 auto testDataCpu = std::make_unique<SettleTestData>(numSettles);
405 auto testDataGpu = std::make_unique<SettleTestData>(numSettles);
407 t_pbc pbc = pbcs_.at(pbcName);
409 applySettle(testDataCpu.get(),
410 pbc,
411 updateVelocities,
412 calcVirial,
413 testDescription);
415 applySettleGpu(testDataGpu.get(),
416 pbc,
417 updateVelocities,
418 calcVirial,
419 testDescription);
421 FloatingPointTolerance tolerance = absoluteTolerance(0.0001);
423 checkCoordinatesEqual(testDataGpu->xPrime_,
424 testDataCpu->xPrime_,
425 tolerance,
426 testDescription);
428 if (updateVelocities)
430 checkCoordinatesEqual(testDataGpu->v_,
431 testDataCpu->v_,
432 tolerance,
433 testDescription);
435 if (calcVirial)
437 checkTensorsEqual(testDataGpu->virial_,
438 testDataCpu->virial_,
439 tolerance,
440 testDescription);
444 #endif // GMX_GPU == GMX_GPU_CUDA
446 // Scan the full Cartesian product of numbers of SETTLE interactions, whether or not update velocities
447 // or calculate the virial contribution, use or not to use PBC, use CPU or GPU-based version of SETTLE.
448 INSTANTIATE_TEST_CASE_P(WithParameters, SettleTest,
449 ::testing::Combine(::testing::Values(1, 2, 4, 5, 7, 10, 12, 15, 17),
450 ::testing::Bool(),
451 ::testing::Bool(),
452 ::testing::Values("PBCNone", "PBCXYZ"),
453 ::testing::ValuesIn(getAlgorithmsNames())
455 } // namespace
456 } // namespace test
457 } // namespace gmx