Add backend for nblib listed forces API
[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, 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
62 //! Function types for testing dihedrals. Add new terms at the end.
63 std::vector<std::vector<ProperDihedral>> c_InputDihs = { { { ProperDihedral(Degrees(-105.0), 15.0, 2) } } /*, { ImproperDihedral(100.0, 50.0) }*/ };
64 // Todo: update test setup to allow more than one interaction type and add the following to the inputs
65 // std::vector<std::vector<RyckaertBellemanDihedral>> c_InputDihs = { { RyckaertBellemanDihedral({ -7.35, 13.6, 8.4, -16.7, 1.3, 12.4 }) } };
66
67 template<class Interaction>
68 class ListedForcesBase
69 {
70 public:
71     std::vector<Interaction>                   input_;
72     std::vector<gmx::RVec>                     x_;
73     std::vector<InteractionIndex<Interaction>> indices_;
74     PbcHolder                                  pbcHolder_;
75     gmx::test::TestReferenceData               refData_;
76     gmx::test::TestReferenceChecker            checker_;
77     std::vector<gmx::RVec>                     forces_;
78     real                                       energy_;
79
80     ListedForcesBase(std::vector<Interaction>                   input,
81                      std::vector<gmx::RVec>                     coordinates,
82                      std::vector<InteractionIndex<Interaction>> indices) :
83         input_(std::move(input)),
84         x_(std::move(coordinates)),
85         indices_(std::move(indices)),
86         pbcHolder_(Box(1.5)),
87         checker_(refData_.rootChecker()),
88         forces_(c_numAtoms, gmx::RVec{ 0, 0, 0 })
89     {
90         energy_ = computeForces(indices_, input_, x_, &forces_, pbcHolder_);
91     }
92
93     void checkForcesAndEnergies()
94     {
95         checker_.checkReal(energy_, "Epot");
96         checker_.checkSequence(std::begin(forces_), std::end(forces_), "forces");
97     }
98 };
99
100 class ListedForcesProperDihedralTest :
101     public ListedForcesBase<ProperDihedral>,
102     public testing::TestWithParam<std::tuple<std::vector<ProperDihedral>, std::vector<gmx::RVec>>>
103 {
104     using Base = ListedForcesBase<ProperDihedral>;
105
106 public:
107     ListedForcesProperDihedralTest() :
108         Base(std::get<0>(GetParam()), std::get<1>(GetParam()), { { 0, 1, 2, 3, 0 } })
109     {
110     }
111 };
112
113 TEST_P(ListedForcesProperDihedralTest, CheckListed)
114 {
115     checkForcesAndEnergies();
116 }
117
118 INSTANTIATE_TEST_CASE_P(FourCenter,
119                         ListedForcesProperDihedralTest,
120                         ::testing::Combine(::testing::ValuesIn(c_InputDihs),
121                                            ::testing::ValuesIn(c_coordinatesForTests)));
122
123 } // namespace nblib