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;
58 //! Coordinates for testing
59 std::vector<std::vector<gmx::RVec>> c_coordinatesForTests = {
60 { { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.2 }, { 0.005, 0.0, 0.1 }, { -0.001, 0.1, 0.0 } },
61 { { 0.5, 0.0, 0.0 }, { 0.5, 0.0, 0.15 }, { 0.5, 0.07, 0.22 }, { 0.5, 0.18, 0.22 } },
62 { { -0.1143, -0.0282, 0.0 }, { 0.0, 0.0434, 0.0 }, { 0.1185, -0.0138, 0.0 }, { -0.0195, 0.1498, 0.0 } }
64 // Parameters for harmonic bonds
65 std::vector<InteractionIndex<HarmonicBondType>> c_HarmonicBondIndices{ { 0, 1, 0 }, { 1, 2, 0 }, { 2, 3, 0 } };
66 std::vector<std::vector<HarmonicBondType>> c_InputHarmonicBond = { { HarmonicBondType(500, 0.15) } };
68 // Parameters for harmonic angles
69 std::vector<InteractionIndex<HarmonicAngle>> c_HarmonicAngleIndices{ { 0, 1, 2, 0 }, { 1, 2, 3, 0 } };
70 std::vector<std::vector<HarmonicAngle>> c_InputHarmonicAngle = { { HarmonicAngle(50.0, Degrees(100)) } };
72 //! Function types for testing dihedrals. Add new terms at the end.
73 std::vector<std::vector<ProperDihedral>> c_InputDihs = { { { ProperDihedral(Degrees(-105.0), 15.0, 2) } } /*, { ImproperDihedral(100.0, 50.0) }*/ };
74 // Todo: update test setup to allow more than one interaction type and add the following to the inputs
75 // std::vector<std::vector<RyckaertBellemanDihedral>> c_InputDihs = { { RyckaertBellemanDihedral({ -7.35, 13.6, 8.4, -16.7, 1.3, 12.4 }) } };
79 template<class Interaction>
80 class ListedForcesBase
83 std::vector<Interaction> input_;
84 std::vector<gmx::RVec> x_;
85 std::vector<InteractionIndex<Interaction>> indices_;
87 gmx::test::TestReferenceData refData_;
88 gmx::test::TestReferenceChecker checker_;
89 std::vector<gmx::RVec> forces_;
92 ListedForcesBase(std::vector<Interaction> input,
93 std::vector<gmx::RVec> coordinates,
94 std::vector<InteractionIndex<Interaction>> indices) :
95 input_(std::move(input)),
96 x_(std::move(coordinates)),
97 indices_(std::move(indices)),
98 pbcHolder_(PbcType::Xyz, Box(1.5)),
99 checker_(refData_.rootChecker()),
100 forces_(c_numAtoms, gmx::RVec{ 0, 0, 0 })
102 energy_ = computeForces(gmx::ArrayRef<const InteractionIndex<Interaction>>(indices_),
103 gmx::ArrayRef<const Interaction>(input_),
109 void checkForcesAndEnergies()
111 // We need quite specific tolerances here since angle functions
112 // etc. are not very precise and reproducible.
113 auto tolerances = gmx::test::FloatingPointTolerance(
114 singleAbsoluteTolerance_, 1.0e-12, singleRelativeTolerance_, 1.0e-12, 1000, 100, false);
115 checker_.setDefaultTolerance(tolerances);
116 checker_.checkReal(energy_, "Epot");
117 checker_.checkSequence(std::begin(forces_), std::end(forces_), "forces");
120 void setSingleTolerance(float relative, float absolute)
122 singleRelativeTolerance_ = relative;
123 singleAbsoluteTolerance_ = absolute;
127 float singleRelativeTolerance_ = 1.0e-12;
128 float singleAbsoluteTolerance_ = 1.0e-12;
131 class ProperDihedralTest :
132 public ListedForcesBase<ProperDihedral>,
133 public testing::TestWithParam<std::tuple<std::vector<ProperDihedral>, std::vector<gmx::RVec>>>
135 using Base = ListedForcesBase<ProperDihedral>;
138 ProperDihedralTest() :
139 Base(std::get<0>(GetParam()), std::get<1>(GetParam()), { { 0, 1, 2, 3, 0 } })
144 TEST_P(ProperDihedralTest, CheckListed)
146 checkForcesAndEnergies();
149 INSTANTIATE_TEST_CASE_P(FourCenter,
151 ::testing::Combine(::testing::ValuesIn(c_InputDihs),
152 ::testing::ValuesIn(c_coordinatesForTests)));
154 class HarmonicBondTest :
155 public ListedForcesBase<HarmonicBondType>,
156 public testing::TestWithParam<std::tuple<std::vector<HarmonicBondType>, std::vector<gmx::RVec>>>
158 using Base = ListedForcesBase<HarmonicBondType>;
162 Base(std::get<0>(GetParam()), std::get<1>(GetParam()), c_HarmonicBondIndices)
167 TEST_P(HarmonicBondTest, CheckListed)
169 checkForcesAndEnergies();
172 INSTANTIATE_TEST_CASE_P(TwoCenter,
174 ::testing::Combine(::testing::ValuesIn(c_InputHarmonicBond),
175 ::testing::ValuesIn(c_coordinatesForTests)));
177 class HarmonicAngleTest :
178 public ListedForcesBase<HarmonicAngle>,
179 public testing::TestWithParam<std::tuple<std::vector<HarmonicAngle>, std::vector<gmx::RVec>>>
181 using Base = ListedForcesBase<HarmonicAngle>;
184 HarmonicAngleTest() :
185 Base(std::get<0>(GetParam()), std::get<1>(GetParam()), c_HarmonicAngleIndices)
190 TEST_P(HarmonicAngleTest, CheckListed)
192 setSingleTolerance(1e-12, 2e-3);
193 checkForcesAndEnergies();
196 INSTANTIATE_TEST_CASE_P(ThreeCenter,
198 ::testing::Combine(::testing::ValuesIn(c_InputHarmonicAngle),
199 ::testing::ValuesIn(c_coordinatesForTests)));