15d7e35fdbe5386e55cdfcbb1f37806fb9917117
[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 //! 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 } }
60 };
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) } };
64
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) } };
68
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 }) } };
73
74 template<class Interaction>
75 class ListedForcesBase
76 {
77 public:
78     std::vector<Interaction>                   input_;
79     std::vector<gmx::RVec>                     x_;
80     std::vector<InteractionIndex<Interaction>> indices_;
81     PbcHolder                                  pbcHolder_;
82     gmx::test::TestReferenceData               refData_;
83     gmx::test::TestReferenceChecker            checker_;
84     std::vector<gmx::RVec>                     forces_;
85     real                                       energy_;
86
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 })
96     {
97         energy_ = computeForces(gmx::ArrayRef<const InteractionIndex<Interaction>>(indices_),
98                                 gmx::ArrayRef<const Interaction>(input_),
99                                 x_,
100                                 &forces_,
101                                 pbcHolder_);
102     }
103
104     void checkForcesAndEnergies()
105     {
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");
113     }
114
115     void setSingleTolerance(float relative, float absolute)
116     {
117         singleRelativeTolerance_ = relative;
118         singleAbsoluteTolerance_ = absolute;
119     }
120
121 private:
122     float singleRelativeTolerance_ = 1.0e-12;
123     float singleAbsoluteTolerance_ = 1.0e-12;
124 };
125
126 class ProperDihedralTest :
127     public ListedForcesBase<ProperDihedral>,
128     public testing::TestWithParam<std::tuple<std::vector<ProperDihedral>, std::vector<gmx::RVec>>>
129 {
130     using Base = ListedForcesBase<ProperDihedral>;
131
132 public:
133     ProperDihedralTest() :
134         Base(std::get<0>(GetParam()), std::get<1>(GetParam()), { { 0, 1, 2, 3, 0 } })
135     {
136     }
137 };
138
139 TEST_P(ProperDihedralTest, CheckListed)
140 {
141     checkForcesAndEnergies();
142 }
143
144 INSTANTIATE_TEST_CASE_P(FourCenter,
145                         ProperDihedralTest,
146                         ::testing::Combine(::testing::ValuesIn(c_InputDihs),
147                                            ::testing::ValuesIn(c_coordinatesForTests)));
148
149 class HarmonicBondTest :
150     public ListedForcesBase<HarmonicBondType>,
151     public testing::TestWithParam<std::tuple<std::vector<HarmonicBondType>, std::vector<gmx::RVec>>>
152 {
153     using Base = ListedForcesBase<HarmonicBondType>;
154
155 public:
156     HarmonicBondTest() :
157         Base(std::get<0>(GetParam()), std::get<1>(GetParam()), c_HarmonicBondIndices)
158     {
159     }
160 };
161
162 TEST_P(HarmonicBondTest, CheckListed)
163 {
164     checkForcesAndEnergies();
165 }
166
167 INSTANTIATE_TEST_CASE_P(TwoCenter,
168                         HarmonicBondTest,
169                         ::testing::Combine(::testing::ValuesIn(c_InputHarmonicBond),
170                                            ::testing::ValuesIn(c_coordinatesForTests)));
171
172 class HarmonicAngleTest :
173     public ListedForcesBase<HarmonicAngle>,
174     public testing::TestWithParam<std::tuple<std::vector<HarmonicAngle>, std::vector<gmx::RVec>>>
175 {
176     using Base = ListedForcesBase<HarmonicAngle>;
177
178 public:
179     HarmonicAngleTest() :
180         Base(std::get<0>(GetParam()), std::get<1>(GetParam()), c_HarmonicAngleIndices)
181     {
182     }
183 };
184
185 TEST_P(HarmonicAngleTest, CheckListed)
186 {
187     setSingleTolerance(1e-12, 2e-3);
188     checkForcesAndEnergies();
189 }
190
191 INSTANTIATE_TEST_CASE_P(ThreeCenter,
192                         HarmonicAngleTest,
193                         ::testing::Combine(::testing::ValuesIn(c_InputHarmonicAngle),
194                                            ::testing::ValuesIn(c_coordinatesForTests)));
195
196 } // namespace nblib