2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2020,2021, 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 * This implements basic nblib box tests
39 * \author Victor Holanda <victor.holanda@cscs.ch>
40 * \author Joe Jordan <ejjordan@kth.se>
41 * \author Prashanth Kanduri <kanduri@cscs.ch>
42 * \author Sebastian Keller <keller@cscs.ch>
44 #include "nblib/listed_forces/dataflow.hpp"
46 #include "testutils/refdata.h"
47 #include "testutils/testasserts.h"
52 //! Number of atoms used in these tests.
53 constexpr int c_numAtoms = 4;
55 //! Coordinates for testing
56 std::vector<std::vector<gmx::RVec>> c_coordinatesForTests = {
57 { { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.2 }, { 0.005, 0.0, 0.1 }, { -0.001, 0.1, 0.0 } },
58 { { 0.5, 0.0, 0.0 }, { 0.5, 0.0, 0.15 }, { 0.5, 0.07, 0.22 }, { 0.5, 0.18, 0.22 } },
59 { { -0.1143, -0.0282, 0.0 }, { 0.0, 0.0434, 0.0 }, { 0.1185, -0.0138, 0.0 }, { -0.0195, 0.1498, 0.0 } }
61 // Parameters for harmonic bonds
62 std::vector<InteractionIndex<HarmonicBondType>> c_HarmonicBondIndices{ { 0, 1, 0 }, { 1, 2, 0 }, { 2, 3, 0 } };
63 std::vector<std::vector<HarmonicBondType>> c_InputHarmonicBond = { { HarmonicBondType(500, 0.15) } };
65 // Parameters for harmonic angles
66 std::vector<InteractionIndex<HarmonicAngle>> c_HarmonicAngleIndices{ { 0, 1, 2, 0 }, { 1, 2, 3, 0 } };
67 std::vector<std::vector<HarmonicAngle>> c_InputHarmonicAngle = { { HarmonicAngle(Degrees(100), 50.0) } };
69 //! Function types for testing dihedrals. Add new terms at the end.
70 std::vector<std::vector<ProperDihedral>> c_InputDihs = { { { ProperDihedral(Degrees(-105.0), 15.0, 2) } } /*, { ImproperDihedral(100.0, 50.0) }*/ };
71 // Todo: update test setup to allow more than one interaction type and add the following to the inputs
72 // std::vector<std::vector<RyckaertBellemanDihedral>> c_InputDihs = { { RyckaertBellemanDihedral({ -7.35, 13.6, 8.4, -16.7, 1.3, 12.4 }) } };
74 template<class Interaction>
75 class ListedForcesBase
78 std::vector<Interaction> input_;
79 std::vector<gmx::RVec> x_;
80 std::vector<InteractionIndex<Interaction>> indices_;
82 gmx::test::TestReferenceData refData_;
83 gmx::test::TestReferenceChecker checker_;
84 std::vector<gmx::RVec> forces_;
87 ListedForcesBase(std::vector<Interaction> input,
88 std::vector<gmx::RVec> coordinates,
89 std::vector<InteractionIndex<Interaction>> indices) :
90 input_(std::move(input)),
91 x_(std::move(coordinates)),
92 indices_(std::move(indices)),
93 pbcHolder_(PbcType::Xyz, Box(1.5)),
94 checker_(refData_.rootChecker()),
95 forces_(c_numAtoms, gmx::RVec{ 0, 0, 0 })
97 energy_ = computeForces(gmx::ArrayRef<const InteractionIndex<Interaction>>(indices_),
98 gmx::ArrayRef<const Interaction>(input_),
104 void checkForcesAndEnergies()
106 // We need quite specific tolerances here since angle functions
107 // etc. are not very precise and reproducible.
108 auto tolerances = gmx::test::FloatingPointTolerance(
109 singleAbsoluteTolerance_, 1.0e-12, singleRelativeTolerance_, 1.0e-12, 1000, 100, false);
110 checker_.setDefaultTolerance(tolerances);
111 checker_.checkReal(energy_, "Epot");
112 checker_.checkSequence(std::begin(forces_), std::end(forces_), "forces");
115 void setSingleTolerance(float relative, float absolute)
117 singleRelativeTolerance_ = relative;
118 singleAbsoluteTolerance_ = absolute;
122 float singleRelativeTolerance_ = 1.0e-12;
123 float singleAbsoluteTolerance_ = 1.0e-12;
126 class ProperDihedralTest :
127 public ListedForcesBase<ProperDihedral>,
128 public testing::TestWithParam<std::tuple<std::vector<ProperDihedral>, std::vector<gmx::RVec>>>
130 using Base = ListedForcesBase<ProperDihedral>;
133 ProperDihedralTest() :
134 Base(std::get<0>(GetParam()), std::get<1>(GetParam()), { { 0, 1, 2, 3, 0 } })
139 TEST_P(ProperDihedralTest, CheckListed)
141 checkForcesAndEnergies();
144 INSTANTIATE_TEST_CASE_P(FourCenter,
146 ::testing::Combine(::testing::ValuesIn(c_InputDihs),
147 ::testing::ValuesIn(c_coordinatesForTests)));
149 class HarmonicBondTest :
150 public ListedForcesBase<HarmonicBondType>,
151 public testing::TestWithParam<std::tuple<std::vector<HarmonicBondType>, std::vector<gmx::RVec>>>
153 using Base = ListedForcesBase<HarmonicBondType>;
157 Base(std::get<0>(GetParam()), std::get<1>(GetParam()), c_HarmonicBondIndices)
162 TEST_P(HarmonicBondTest, CheckListed)
164 checkForcesAndEnergies();
167 INSTANTIATE_TEST_CASE_P(TwoCenter,
169 ::testing::Combine(::testing::ValuesIn(c_InputHarmonicBond),
170 ::testing::ValuesIn(c_coordinatesForTests)));
172 class HarmonicAngleTest :
173 public ListedForcesBase<HarmonicAngle>,
174 public testing::TestWithParam<std::tuple<std::vector<HarmonicAngle>, std::vector<gmx::RVec>>>
176 using Base = ListedForcesBase<HarmonicAngle>;
179 HarmonicAngleTest() :
180 Base(std::get<0>(GetParam()), std::get<1>(GetParam()), c_HarmonicAngleIndices)
185 TEST_P(HarmonicAngleTest, CheckListed)
187 setSingleTolerance(1e-12, 2e-3);
188 checkForcesAndEnergies();
191 INSTANTIATE_TEST_CASE_P(ThreeCenter,
193 ::testing::Combine(::testing::ValuesIn(c_InputHarmonicAngle),
194 ::testing::ValuesIn(c_coordinatesForTests)));