Rework -Weverything
[alexxy/gromacs.git] / api / nblib / listed_forces / tests / typetests.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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
37  * This implements basic nblib box tests
38  *
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>
43  */
44 #include "nblib/listed_forces/dataflow.hpp"
45
46 #include "testutils/refdata.h"
47 #include "testutils/testasserts.h"
48
49 namespace nblib
50 {
51
52 //! Number of atoms used in these tests.
53 constexpr int c_numAtoms = 4;
54
55 namespace
56 {
57
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 } }
63 };
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) } };
67
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(Degrees(100), 50.0) } };
71
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 }) } };
76
77 } // namespace
78
79 template<class Interaction>
80 class ListedForcesBase
81 {
82 public:
83     std::vector<Interaction>                   input_;
84     std::vector<gmx::RVec>                     x_;
85     std::vector<InteractionIndex<Interaction>> indices_;
86     PbcHolder                                  pbcHolder_;
87     gmx::test::TestReferenceData               refData_;
88     gmx::test::TestReferenceChecker            checker_;
89     std::vector<gmx::RVec>                     forces_;
90     real                                       energy_;
91
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 })
101     {
102         energy_ = computeForces(gmx::ArrayRef<const InteractionIndex<Interaction>>(indices_),
103                                 gmx::ArrayRef<const Interaction>(input_),
104                                 x_,
105                                 &forces_,
106                                 pbcHolder_);
107     }
108
109     void checkForcesAndEnergies()
110     {
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");
118     }
119
120     void setSingleTolerance(float relative, float absolute)
121     {
122         singleRelativeTolerance_ = relative;
123         singleAbsoluteTolerance_ = absolute;
124     }
125
126 private:
127     float singleRelativeTolerance_ = 1.0e-12;
128     float singleAbsoluteTolerance_ = 1.0e-12;
129 };
130
131 class ProperDihedralTest :
132     public ListedForcesBase<ProperDihedral>,
133     public testing::TestWithParam<std::tuple<std::vector<ProperDihedral>, std::vector<gmx::RVec>>>
134 {
135     using Base = ListedForcesBase<ProperDihedral>;
136
137 public:
138     ProperDihedralTest() :
139         Base(std::get<0>(GetParam()), std::get<1>(GetParam()), { { 0, 1, 2, 3, 0 } })
140     {
141     }
142 };
143
144 TEST_P(ProperDihedralTest, CheckListed)
145 {
146     checkForcesAndEnergies();
147 }
148
149 INSTANTIATE_TEST_CASE_P(FourCenter,
150                         ProperDihedralTest,
151                         ::testing::Combine(::testing::ValuesIn(c_InputDihs),
152                                            ::testing::ValuesIn(c_coordinatesForTests)));
153
154 class HarmonicBondTest :
155     public ListedForcesBase<HarmonicBondType>,
156     public testing::TestWithParam<std::tuple<std::vector<HarmonicBondType>, std::vector<gmx::RVec>>>
157 {
158     using Base = ListedForcesBase<HarmonicBondType>;
159
160 public:
161     HarmonicBondTest() :
162         Base(std::get<0>(GetParam()), std::get<1>(GetParam()), c_HarmonicBondIndices)
163     {
164     }
165 };
166
167 TEST_P(HarmonicBondTest, CheckListed)
168 {
169     checkForcesAndEnergies();
170 }
171
172 INSTANTIATE_TEST_CASE_P(TwoCenter,
173                         HarmonicBondTest,
174                         ::testing::Combine(::testing::ValuesIn(c_InputHarmonicBond),
175                                            ::testing::ValuesIn(c_coordinatesForTests)));
176
177 class HarmonicAngleTest :
178     public ListedForcesBase<HarmonicAngle>,
179     public testing::TestWithParam<std::tuple<std::vector<HarmonicAngle>, std::vector<gmx::RVec>>>
180 {
181     using Base = ListedForcesBase<HarmonicAngle>;
182
183 public:
184     HarmonicAngleTest() :
185         Base(std::get<0>(GetParam()), std::get<1>(GetParam()), c_HarmonicAngleIndices)
186     {
187     }
188 };
189
190 TEST_P(HarmonicAngleTest, CheckListed)
191 {
192     setSingleTolerance(1e-12, 2e-3);
193     checkForcesAndEnergies();
194 }
195
196 INSTANTIATE_TEST_CASE_P(ThreeCenter,
197                         HarmonicAngleTest,
198                         ::testing::Combine(::testing::ValuesIn(c_InputHarmonicAngle),
199                                            ::testing::ValuesIn(c_coordinatesForTests)));
200
201 } // namespace nblib