Access the device status directly, remove the getter
[alexxy/gromacs.git] / src / gromacs / mdlib / tests / constr.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019,2020, 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 SHAKE and LINCS tests.
37  *
38  * \todo Better tests for virial are needed.
39  * \todo Tests for bigger systems to test threads synchronization,
40  *       reduction, etc. on the GPU.
41  * \todo Tests for algorithms for derivatives.
42  * \todo Free-energy perturbation tests
43  *
44  * \author Artem Zhmurov <zhmurov@gmail.com>
45  * \ingroup module_mdlib
46  */
47 #include "gmxpre.h"
48
49 #include "config.h"
50
51 #include <assert.h>
52
53 #include <unordered_map>
54 #include <vector>
55
56 #include <gtest/gtest.h>
57
58 #include "gromacs/math/vec.h"
59 #include "gromacs/math/vectypes.h"
60 #include "gromacs/pbcutil/pbc.h"
61 #include "gromacs/utility/stringutil.h"
62
63 #include "testutils/testasserts.h"
64
65 #include "constrtestdata.h"
66 #include "constrtestrunners.h"
67
68 namespace gmx
69 {
70 namespace test
71 {
72 namespace
73 {
74
75 /*! \brief The two-dimensional parameter space for test.
76  *
77  * The test will run for all possible combinations of accessible
78  * values of the:
79  * 1. PBC setup ("PBCNONE" or "PBCXYZ")
80  * 2. The algorithm ("SHAKE", "LINCS" or "LINCS_GPU").
81  */
82 typedef std::tuple<std::string, std::string> ConstraintsTestParameters;
83
84 //! Names of all availible runners
85 std::vector<std::string> runnersNames;
86
87 //! Method that fills and returns runnersNames to the test macros.
88 std::vector<std::string> getRunnersNames()
89 {
90     runnersNames.emplace_back("SHAKE");
91     runnersNames.emplace_back("LINCS");
92     if (GMX_GPU_CUDA && canComputeOnDevice())
93     {
94         runnersNames.emplace_back("LINCS_GPU");
95     }
96     return runnersNames;
97 }
98
99 /*! \brief Test fixture for constraints.
100  *
101  * The fixture uses following test systems:
102  * 1. Two atoms, connected with one constraint (e.g. NH).
103  * 2. Three atoms, connected consequently with two constraints (e.g. CH2).
104  * 3. Three atoms, constrained to the fourth atom (e.g. CH3).
105  * 4. Four atoms, connected by two independent constraints.
106  * 5. Three atoms, connected by three constraints in a triangle
107  *      (e.g. H2O with constrained H-O-H angle).
108  * 6. Four atoms, connected by three consequential constraints.
109  *
110  * For all systems, the final lengths of the constraints are tested against the
111  * reference values, the direction of each constraint is checked.
112  * Test also verifies that the center of mass has not been
113  * shifted by the constraints and that its velocity has not changed.
114  * For some systems, the value for scaled virial tensor is checked against
115  * pre-computed data.
116  */
117 class ConstraintsTest : public ::testing::TestWithParam<ConstraintsTestParameters>
118 {
119 public:
120     //! PBC setups
121     std::unordered_map<std::string, t_pbc> pbcs_;
122     //! Algorithms (SHAKE and LINCS)
123     std::unordered_map<std::string, void (*)(ConstraintsTestData* testData, t_pbc pbc)> algorithms_;
124
125     /*! \brief Test setup function.
126      *
127      * Setting up the pbcs and algorithms. Note, that corresponding string keywords
128      * have to be explicitly added at the end of this file when the tests are called.
129      *
130      */
131     void SetUp() override
132     {
133
134         //
135         // PBC initialization
136         //
137         t_pbc pbc;
138
139         // Infinitely small box
140         matrix boxNone = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
141         set_pbc(&pbc, PbcType::No, boxNone);
142         pbcs_["PBCNone"] = pbc;
143
144         // Rectangular box
145         matrix boxXyz = { { 10.0, 0.0, 0.0 }, { 0.0, 20.0, 0.0 }, { 0.0, 0.0, 15.0 } };
146         set_pbc(&pbc, PbcType::Xyz, boxXyz);
147         pbcs_["PBCXYZ"] = pbc;
148
149         //
150         // Algorithms
151         //
152         // SHAKE
153         algorithms_["SHAKE"] = applyShake;
154         // LINCS
155         algorithms_["LINCS"] = applyLincs;
156         // LINCS using GPU (will only be called if GPU is available)
157         algorithms_["LINCS_GPU"] = applyLincsGpu;
158     }
159
160     /*! \brief
161      * The test on the final length of constrained bonds.
162      *
163      * Goes through all the constraints and checks if the final length of all the constraints is
164      * equal to the target length with provided tolerance.
165      *
166      * \param[in] tolerance       Allowed tolerance in final lengths.
167      * \param[in] testData        Test data structure.
168      * \param[in] pbc             Periodic boundary data.
169      */
170     static void checkConstrainsLength(FloatingPointTolerance     tolerance,
171                                       const ConstraintsTestData& testData,
172                                       t_pbc                      pbc)
173     {
174
175         // Test if all the constraints are satisfied
176         for (index c = 0; c < ssize(testData.constraints_) / 3; c++)
177         {
178             real r0 = testData.constraintsR0_.at(testData.constraints_.at(3 * c));
179             int  i  = testData.constraints_.at(3 * c + 1);
180             int  j  = testData.constraints_.at(3 * c + 2);
181             RVec xij0, xij1;
182             real d0, d1;
183             if (pbc.pbcType == PbcType::Xyz)
184             {
185                 pbc_dx_aiuc(&pbc, testData.x_[i], testData.x_[j], xij0);
186                 pbc_dx_aiuc(&pbc, testData.xPrime_[i], testData.xPrime_[j], xij1);
187             }
188             else
189             {
190                 rvec_sub(testData.x_[i], testData.x_[j], xij0);
191                 rvec_sub(testData.xPrime_[i], testData.xPrime_[j], xij1);
192             }
193             d0 = norm(xij0);
194             d1 = norm(xij1);
195             EXPECT_REAL_EQ_TOL(r0, d1, tolerance) << gmx::formatString(
196                     "rij = %f, which is not equal to r0 = %f for constraint #%zd, between atoms %d "
197                     "and %d"
198                     " (before constraining rij was %f).",
199                     d1, r0, c, i, j, d0);
200         }
201     }
202
203     /*! \brief
204      * The test on the final length of constrained bonds.
205      *
206      * Goes through all the constraints and checks if the direction of constraint has not changed
207      * by the algorithm (i.e. the constraints algorithm arrived to the solution that is closest
208      * to the initial system conformation).
209      *
210      * \param[in] testData        Test data structure.
211      * \param[in] pbc             Periodic boundary data.
212      */
213     static void checkConstrainsDirection(const ConstraintsTestData& testData, t_pbc pbc)
214     {
215
216         for (index c = 0; c < ssize(testData.constraints_) / 3; c++)
217         {
218             int  i = testData.constraints_.at(3 * c + 1);
219             int  j = testData.constraints_.at(3 * c + 2);
220             RVec xij0, xij1;
221             if (pbc.pbcType == PbcType::Xyz)
222             {
223                 pbc_dx_aiuc(&pbc, testData.x_[i], testData.x_[j], xij0);
224                 pbc_dx_aiuc(&pbc, testData.xPrime_[i], testData.xPrime_[j], xij1);
225             }
226             else
227             {
228                 rvec_sub(testData.x_[i], testData.x_[j], xij0);
229                 rvec_sub(testData.xPrime_[i], testData.xPrime_[j], xij1);
230             }
231
232             real dot = xij0.dot(xij1);
233
234             EXPECT_GE(dot, 0.0) << gmx::formatString(
235                     "The constraint %zd changed direction. Constraining algorithm might have "
236                     "returned the wrong root "
237                     "of the constraints equation.",
238                     c);
239         }
240     }
241
242     /*! \brief
243      * The test on the coordinates of the center of the mass (COM) of the system.
244      *
245      * Checks if the center of mass has not been shifted by the constraints. Note,
246      * that this test does not take into account the periodic boundary conditions.
247      * Hence it will not work should the constraints decide to move atoms across
248      * PBC borders.
249      *
250      * \param[in] tolerance       Allowed tolerance in COM coordinates.
251      * \param[in] testData        Test data structure.
252      */
253     static void checkCOMCoordinates(FloatingPointTolerance tolerance, const ConstraintsTestData& testData)
254     {
255
256         RVec comPrime0({ 0.0, 0.0, 0.0 });
257         RVec comPrime({ 0.0, 0.0, 0.0 });
258         for (int i = 0; i < testData.numAtoms_; i++)
259         {
260             comPrime0 += testData.masses_[i] * testData.xPrime0_[i];
261             comPrime += testData.masses_[i] * testData.xPrime_[i];
262         }
263
264         comPrime0 /= testData.numAtoms_;
265         comPrime /= testData.numAtoms_;
266
267         EXPECT_REAL_EQ_TOL(comPrime[XX], comPrime0[XX], tolerance)
268                 << "Center of mass was shifted by constraints in x-direction.";
269         EXPECT_REAL_EQ_TOL(comPrime[YY], comPrime0[YY], tolerance)
270                 << "Center of mass was shifted by constraints in y-direction.";
271         EXPECT_REAL_EQ_TOL(comPrime[ZZ], comPrime0[ZZ], tolerance)
272                 << "Center of mass was shifted by constraints in z-direction.";
273     }
274
275     /*! \brief
276      * The test on the velocity of the center of the mass (COM) of the system.
277      *
278      * Checks if the velocity of the center of mass has not changed.
279      *
280      * \param[in] tolerance       Allowed tolerance in COM velocity components.
281      * \param[in] testData        Test data structure.
282      */
283     static void checkCOMVelocity(FloatingPointTolerance tolerance, const ConstraintsTestData& testData)
284     {
285
286         RVec comV0({ 0.0, 0.0, 0.0 });
287         RVec comV({ 0.0, 0.0, 0.0 });
288         for (int i = 0; i < testData.numAtoms_; i++)
289         {
290             comV0 += testData.masses_[i] * testData.v0_[i];
291             comV += testData.masses_[i] * testData.v_[i];
292         }
293         comV0 /= testData.numAtoms_;
294         comV /= testData.numAtoms_;
295
296         EXPECT_REAL_EQ_TOL(comV[XX], comV0[XX], tolerance)
297                 << "Velocity of the center of mass in x-direction has been changed by constraints.";
298         EXPECT_REAL_EQ_TOL(comV[YY], comV0[YY], tolerance)
299                 << "Velocity of the center of mass in y-direction has been changed by constraints.";
300         EXPECT_REAL_EQ_TOL(comV[ZZ], comV0[ZZ], tolerance)
301                 << "Velocity of the center of mass in z-direction has been changed by constraints.";
302     }
303
304     /*! \brief
305      * The test of virial tensor.
306      *
307      * Checks if the values in the scaled virial tensor are equal to pre-computed values.
308      *
309      * \param[in] tolerance       Tolerance for the tensor values.
310      * \param[in] testData        Test data structure.
311      */
312     static void checkVirialTensor(FloatingPointTolerance tolerance, const ConstraintsTestData& testData)
313     {
314         for (int i = 0; i < DIM; i++)
315         {
316             for (int j = 0; j < DIM; j++)
317             {
318                 EXPECT_REAL_EQ_TOL(testData.virialScaledRef_[i][j], testData.virialScaled_[i][j], tolerance)
319                         << gmx::formatString(
320                                    "Values in virial tensor at [%d][%d] are not within the "
321                                    "tolerance from reference value.",
322                                    i, j);
323             }
324         }
325     }
326 };
327
328 TEST_P(ConstraintsTest, SingleConstraint)
329 {
330     std::string title    = "one constraint (e.g. OH)";
331     int         numAtoms = 2;
332
333     std::vector<real> masses        = { 1.0, 12.0 };
334     std::vector<int>  constraints   = { 0, 0, 1 };
335     std::vector<real> constraintsR0 = { 0.1 };
336
337     real oneTenthOverSqrtTwo = 0.1_real / std::sqrt(2.0_real);
338
339     std::vector<RVec> x = { { 0.0, oneTenthOverSqrtTwo, 0.0 }, { oneTenthOverSqrtTwo, 0.0, 0.0 } };
340     std::vector<RVec> xPrime = { { 0.01, 0.08, 0.01 }, { 0.06, 0.01, -0.01 } };
341     std::vector<RVec> v      = { { 1.0, 2.0, 3.0 }, { 3.0, 2.0, 1.0 } };
342
343     tensor virialScaledRef = { { -5.58e-04, 5.58e-04, 0.00e+00 },
344                                { 5.58e-04, -5.58e-04, 0.00e+00 },
345                                { 0.00e+00, 0.00e+00, 0.00e+00 } };
346
347     real     shakeTolerance = 0.0001;
348     gmx_bool shakeUseSOR    = false;
349
350     int  lincsNIter               = 1;
351     int  lincslincsExpansionOrder = 4;
352     real lincsWarnAngle           = 30.0;
353
354     std::unique_ptr<ConstraintsTestData> testData = std::make_unique<ConstraintsTestData>(
355             title, numAtoms, masses, constraints, constraintsR0, true, virialScaledRef, false, 0,
356             real(0.0), real(0.001), x, xPrime, v, shakeTolerance, shakeUseSOR, lincsNIter,
357             lincslincsExpansionOrder, lincsWarnAngle);
358     std::string pbcName;
359     std::string algorithmName;
360     std::tie(pbcName, algorithmName) = GetParam();
361     t_pbc pbc                        = pbcs_.at(pbcName);
362
363     // Apply constraints
364     algorithms_.at(algorithmName)(testData.get(), pbc);
365
366     checkConstrainsLength(absoluteTolerance(0.0002), *testData, pbc);
367     checkConstrainsDirection(*testData, pbc);
368     checkCOMCoordinates(absoluteTolerance(0.0001), *testData);
369     checkCOMVelocity(absoluteTolerance(0.0001), *testData);
370
371     checkVirialTensor(absoluteTolerance(0.0001), *testData);
372 }
373
374 TEST_P(ConstraintsTest, TwoDisjointConstraints)
375 {
376
377     std::string       title         = "two disjoint constraints";
378     int               numAtoms      = 4;
379     std::vector<real> masses        = { 0.5, 1.0 / 3.0, 0.25, 1.0 };
380     std::vector<int>  constraints   = { 0, 0, 1, 1, 2, 3 };
381     std::vector<real> constraintsR0 = { 2.0, 1.0 };
382
383
384     std::vector<RVec> x = {
385         { 2.50, -3.10, 15.70 }, { 0.51, -3.02, 15.55 }, { -0.50, -3.00, 15.20 }, { -1.51, -2.95, 15.05 }
386     };
387
388     std::vector<RVec> xPrime = {
389         { 2.50, -3.10, 15.70 }, { 0.51, -3.02, 15.55 }, { -0.50, -3.00, 15.20 }, { -1.51, -2.95, 15.05 }
390     };
391
392     std::vector<RVec> v = { { 0.0, 1.0, 0.0 }, { 1.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0 }, { 0.0, 0.0, 0.0 } };
393
394     tensor virialScaledRef = { { 3.3e-03, -1.7e-04, 5.6e-04 },
395                                { -1.7e-04, 8.9e-06, -2.8e-05 },
396                                { 5.6e-04, -2.8e-05, 8.9e-05 } };
397
398     real     shakeTolerance = 0.0001;
399     gmx_bool shakeUseSOR    = false;
400
401     int  lincsNIter               = 1;
402     int  lincslincsExpansionOrder = 4;
403     real lincsWarnAngle           = 30.0;
404
405     std::unique_ptr<ConstraintsTestData> testData = std::make_unique<ConstraintsTestData>(
406             title, numAtoms, masses, constraints, constraintsR0, true, virialScaledRef, false, 0,
407             real(0.0), real(0.001), x, xPrime, v, shakeTolerance, shakeUseSOR, lincsNIter,
408             lincslincsExpansionOrder, lincsWarnAngle);
409
410     std::string pbcName;
411     std::string algorithmName;
412     std::tie(pbcName, algorithmName) = GetParam();
413     t_pbc pbc                        = pbcs_.at(pbcName);
414
415     // Apply constraints
416     algorithms_.at(algorithmName)(testData.get(), pbc);
417
418     checkConstrainsLength(absoluteTolerance(0.0002), *testData, pbc);
419     checkConstrainsDirection(*testData, pbc);
420     checkCOMCoordinates(absoluteTolerance(0.0001), *testData);
421     checkCOMVelocity(absoluteTolerance(0.0001), *testData);
422
423     checkVirialTensor(absoluteTolerance(0.0001), *testData);
424 }
425
426 TEST_P(ConstraintsTest, ThreeSequentialConstraints)
427 {
428
429     std::string       title         = "three atoms, connected longitudinally (e.g. CH2)";
430     int               numAtoms      = 3;
431     std::vector<real> masses        = { 1.0, 12.0, 16.0 };
432     std::vector<int>  constraints   = { 0, 0, 1, 1, 1, 2 };
433     std::vector<real> constraintsR0 = { 0.1, 0.2 };
434
435     real oneTenthOverSqrtTwo    = 0.1_real / std::sqrt(2.0_real);
436     real twoTenthsOverSqrtThree = 0.2_real / std::sqrt(3.0_real);
437
438     std::vector<RVec> x = { { oneTenthOverSqrtTwo, oneTenthOverSqrtTwo, 0.0 },
439                             { 0.0, 0.0, 0.0 },
440                             { twoTenthsOverSqrtThree, twoTenthsOverSqrtThree, twoTenthsOverSqrtThree } };
441
442     std::vector<RVec> xPrime = { { 0.08, 0.07, 0.01 }, { -0.02, 0.01, -0.02 }, { 0.10, 0.12, 0.11 } };
443
444     std::vector<RVec> v = { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } };
445
446     tensor virialScaledRef = { { 4.14e-03, 4.14e-03, 3.31e-03 },
447                                { 4.14e-03, 4.14e-03, 3.31e-03 },
448                                { 3.31e-03, 3.31e-03, 3.31e-03 } };
449
450     real     shakeTolerance = 0.0001;
451     gmx_bool shakeUseSOR    = false;
452
453     int  lincsNIter               = 1;
454     int  lincslincsExpansionOrder = 4;
455     real lincsWarnAngle           = 30.0;
456
457     std::unique_ptr<ConstraintsTestData> testData = std::make_unique<ConstraintsTestData>(
458             title, numAtoms, masses, constraints, constraintsR0, true, virialScaledRef, false, 0,
459             real(0.0), real(0.001), x, xPrime, v, shakeTolerance, shakeUseSOR, lincsNIter,
460             lincslincsExpansionOrder, lincsWarnAngle);
461
462     std::string pbcName;
463     std::string algorithmName;
464     std::tie(pbcName, algorithmName) = GetParam();
465     t_pbc pbc                        = pbcs_.at(pbcName);
466
467     // Apply constraints
468     algorithms_.at(algorithmName)(testData.get(), pbc);
469
470     checkConstrainsLength(absoluteTolerance(0.0002), *testData, pbc);
471     checkConstrainsDirection(*testData, pbc);
472     checkCOMCoordinates(absoluteTolerance(0.0001), *testData);
473     checkCOMVelocity(absoluteTolerance(0.0001), *testData);
474
475     checkVirialTensor(absoluteTolerance(0.0001), *testData);
476 }
477
478 TEST_P(ConstraintsTest, ThreeConstraintsWithCentralAtom)
479 {
480
481     std::string       title         = "three atoms, connected to the central atom (e.g. CH3)";
482     int               numAtoms      = 4;
483     std::vector<real> masses        = { 12.0, 1.0, 1.0, 1.0 };
484     std::vector<int>  constraints   = { 0, 0, 1, 0, 0, 2, 0, 0, 3 };
485     std::vector<real> constraintsR0 = { 0.1 };
486
487
488     std::vector<RVec> x = {
489         { 0.00, 0.00, 0.00 }, { 0.10, 0.00, 0.00 }, { 0.00, -0.10, 0.00 }, { 0.00, 0.00, 0.10 }
490     };
491
492     std::vector<RVec> xPrime = { { 0.004, 0.009, -0.010 },
493                                  { 0.110, -0.006, 0.003 },
494                                  { -0.007, -0.102, -0.007 },
495                                  { -0.005, 0.011, 0.102 } };
496
497     std::vector<RVec> v = { { 1.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0 } };
498
499     tensor virialScaledRef = { { 7.14e-04, 0.00e+00, 0.00e+00 },
500                                { 0.00e+00, 1.08e-03, 0.00e+00 },
501                                { 0.00e+00, 0.00e+00, 1.15e-03 } };
502
503     real     shakeTolerance = 0.0001;
504     gmx_bool shakeUseSOR    = false;
505
506     int  lincsNIter               = 1;
507     int  lincslincsExpansionOrder = 4;
508     real lincsWarnAngle           = 30.0;
509
510     std::unique_ptr<ConstraintsTestData> testData = std::make_unique<ConstraintsTestData>(
511             title, numAtoms, masses, constraints, constraintsR0, true, virialScaledRef, false, 0,
512             real(0.0), real(0.001), x, xPrime, v, shakeTolerance, shakeUseSOR, lincsNIter,
513             lincslincsExpansionOrder, lincsWarnAngle);
514
515     std::string pbcName;
516     std::string algorithmName;
517     std::tie(pbcName, algorithmName) = GetParam();
518     t_pbc pbc                        = pbcs_.at(pbcName);
519
520     // Apply constraints
521     algorithms_.at(algorithmName)(testData.get(), pbc);
522
523     checkConstrainsLength(absoluteTolerance(0.0002), *testData, pbc);
524     checkConstrainsDirection(*testData, pbc);
525     checkCOMCoordinates(absoluteTolerance(0.0001), *testData);
526     checkCOMVelocity(absoluteTolerance(0.0001), *testData);
527
528     checkVirialTensor(absoluteTolerance(0.0001), *testData);
529 }
530
531 TEST_P(ConstraintsTest, FourSequentialConstraints)
532 {
533
534     std::string       title         = "four atoms, connected longitudinally";
535     int               numAtoms      = 4;
536     std::vector<real> masses        = { 0.5, 1.0 / 3.0, 0.25, 1.0 };
537     std::vector<int>  constraints   = { 0, 0, 1, 1, 1, 2, 2, 2, 3 };
538     std::vector<real> constraintsR0 = { 2.0, 1.0, 1.0 };
539
540
541     std::vector<RVec> x = {
542         { 2.50, -3.10, 15.70 }, { 0.51, -3.02, 15.55 }, { -0.50, -3.00, 15.20 }, { -1.51, -2.95, 15.05 }
543     };
544
545     std::vector<RVec> xPrime = {
546         { 2.50, -3.10, 15.70 }, { 0.51, -3.02, 15.55 }, { -0.50, -3.00, 15.20 }, { -1.51, -2.95, 15.05 }
547     };
548
549     std::vector<RVec> v = { { 0.0, 0.0, 2.0 }, { 0.0, 0.0, 3.0 }, { 0.0, 0.0, -4.0 }, { 0.0, 0.0, -1.0 } };
550
551     tensor virialScaledRef = { { 1.15e-01, -4.20e-03, 2.12e-02 },
552                                { -4.20e-03, 1.70e-04, -6.41e-04 },
553                                { 2.12e-02, -6.41e-04, 5.45e-03 } };
554
555     real     shakeTolerance = 0.0001;
556     gmx_bool shakeUseSOR    = false;
557
558     int  lincsNIter               = 4;
559     int  lincslincsExpansionOrder = 8;
560     real lincsWarnAngle           = 30.0;
561
562     std::unique_ptr<ConstraintsTestData> testData = std::make_unique<ConstraintsTestData>(
563             title, numAtoms, masses, constraints, constraintsR0, true, virialScaledRef, false, 0,
564             real(0.0), real(0.001), x, xPrime, v, shakeTolerance, shakeUseSOR, lincsNIter,
565             lincslincsExpansionOrder, lincsWarnAngle);
566
567     std::string pbcName;
568     std::string algorithmName;
569     std::tie(pbcName, algorithmName) = GetParam();
570     t_pbc pbc                        = pbcs_.at(pbcName);
571
572     // Apply constraints
573     algorithms_.at(algorithmName)(testData.get(), pbc);
574
575     checkConstrainsLength(absoluteTolerance(0.0002), *testData, pbc);
576     checkConstrainsDirection(*testData, pbc);
577     checkCOMCoordinates(absoluteTolerance(0.0001), *testData);
578     checkCOMVelocity(absoluteTolerance(0.0001), *testData);
579
580     checkVirialTensor(absoluteTolerance(0.01), *testData);
581 }
582
583 TEST_P(ConstraintsTest, TriangleOfConstraints)
584 {
585
586     std::string       title         = "basic triangle (tree atoms, connected to each other)";
587     int               numAtoms      = 3;
588     std::vector<real> masses        = { 1.0, 1.0, 1.0 };
589     std::vector<int>  constraints   = { 0, 0, 1, 2, 0, 2, 1, 1, 2 };
590     std::vector<real> constraintsR0 = { 0.1, 0.1, 0.1 };
591
592     real oneTenthOverSqrtTwo = 0.1_real / std::sqrt(2.0_real);
593
594     std::vector<RVec> x = { { oneTenthOverSqrtTwo, 0.0, 0.0 },
595                             { 0.0, oneTenthOverSqrtTwo, 0.0 },
596                             { 0.0, 0.0, oneTenthOverSqrtTwo } };
597
598     std::vector<RVec> xPrime = { { 0.09, -0.02, 0.01 }, { -0.02, 0.10, -0.02 }, { 0.03, -0.01, 0.07 } };
599
600     std::vector<RVec> v = { { 1.0, 1.0, 1.0 }, { -2.0, -2.0, -2.0 }, { 1.0, 1.0, 1.0 } };
601
602     tensor virialScaledRef = { { 6.00e-04, -1.61e-03, 1.01e-03 },
603                                { -1.61e-03, 2.53e-03, -9.25e-04 },
604                                { 1.01e-03, -9.25e-04, -8.05e-05 } };
605
606     real     shakeTolerance = 0.0001;
607     gmx_bool shakeUseSOR    = false;
608
609     int  lincsNIter               = 1;
610     int  lincslincsExpansionOrder = 4;
611     real lincsWarnAngle           = 30.0;
612
613     std::unique_ptr<ConstraintsTestData> testData = std::make_unique<ConstraintsTestData>(
614             title, numAtoms, masses, constraints, constraintsR0, true, virialScaledRef, false, 0,
615             real(0.0), real(0.001), x, xPrime, v, shakeTolerance, shakeUseSOR, lincsNIter,
616             lincslincsExpansionOrder, lincsWarnAngle);
617
618     std::string pbcName;
619     std::string runnerName;
620     std::tie(pbcName, runnerName) = GetParam();
621     t_pbc pbc                     = pbcs_.at(pbcName);
622
623     // Apply constraints
624     algorithms_.at(runnerName)(testData.get(), pbc);
625
626     checkConstrainsLength(absoluteTolerance(0.0002), *testData, pbc);
627     checkConstrainsDirection(*testData, pbc);
628     checkCOMCoordinates(absoluteTolerance(0.0001), *testData);
629     checkCOMVelocity(absoluteTolerance(0.0001), *testData);
630
631     checkVirialTensor(absoluteTolerance(0.00001), *testData);
632 }
633
634
635 INSTANTIATE_TEST_CASE_P(WithParameters,
636                         ConstraintsTest,
637                         ::testing::Combine(::testing::Values("PBCNone", "PBCXYZ"),
638                                            ::testing::ValuesIn(getRunnersNames())));
639
640 } // namespace
641 } // namespace test
642 } // namespace gmx