cb91699c8feb7f9c063e0efe7733064d92aa6b09
[alexxy/gromacs.git] / api / nblib / listed_forces / tests / helpers.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 utility 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 "gmxpre.h"
45
46 #include <gtest/gtest.h>
47
48 #include "nblib/listed_forces/helpers.hpp"
49 #include "nblib/listed_forces/traits.h"
50
51 #include "testutils/testasserts.h"
52
53
54 namespace nblib
55 {
56 namespace test
57 {
58 namespace
59 {
60
61 TEST(NBlibTest, CanSplitListedWork)
62 {
63     ListedInteractionData interactions;
64
65     HarmonicAngle    angle(1, Degrees(1));
66     HarmonicBondType bond(1, 1);
67
68     int largestIndex = 20;
69     int nSplits      = 3; // split ranges: [0,5], [6,11], [12, 19]
70
71     std::vector<InteractionIndex<HarmonicBondType>> bondIndices{
72         { 0, 1, 0 }, { 0, 6, 0 }, { 11, 12, 0 }, { 18, 19, 0 }
73     };
74     std::vector<InteractionIndex<HarmonicAngle>> angleIndices{
75         { 0, 1, 2, 0 }, { 0, 6, 7, 0 }, { 11, 12, 13, 0 }, { 17, 19, 18, 0 }
76     };
77
78     pickType<HarmonicBondType>(interactions).indices = bondIndices;
79     pickType<HarmonicAngle>(interactions).indices    = angleIndices;
80
81     std::vector<ListedInteractionData> splitInteractions =
82             splitListedWork(interactions, largestIndex, nSplits);
83
84     std::vector<InteractionIndex<HarmonicBondType>> refBondIndices0{ { 0, 1, 0 }, { 0, 6, 0 } };
85     std::vector<InteractionIndex<HarmonicAngle>> refAngleIndices0{ { 0, 1, 2, 0 }, { 0, 6, 7, 0 } };
86     std::vector<InteractionIndex<HarmonicBondType>> refBondIndices1{ { 11, 12, 0 } };
87     std::vector<InteractionIndex<HarmonicAngle>>    refAngleIndices1{ { 11, 12, 13, 0 } };
88     std::vector<InteractionIndex<HarmonicBondType>> refBondIndices2{ { 18, 19, 0 } };
89     std::vector<InteractionIndex<HarmonicAngle>>    refAngleIndices2{ { 17, 19, 18, 0 } };
90
91     EXPECT_EQ(refBondIndices0, pickType<HarmonicBondType>(splitInteractions[0]).indices);
92     EXPECT_EQ(refBondIndices1, pickType<HarmonicBondType>(splitInteractions[1]).indices);
93     EXPECT_EQ(refBondIndices2, pickType<HarmonicBondType>(splitInteractions[2]).indices);
94
95     EXPECT_EQ(refAngleIndices0, pickType<HarmonicAngle>(splitInteractions[0]).indices);
96     EXPECT_EQ(refAngleIndices1, pickType<HarmonicAngle>(splitInteractions[1]).indices);
97     EXPECT_EQ(refAngleIndices2, pickType<HarmonicAngle>(splitInteractions[2]).indices);
98 }
99
100
101 TEST(NBlibTest, ListedForceBuffer)
102 {
103     using T     = gmx::RVec;
104     int ncoords = 20;
105
106     T              vzero{ 0, 0, 0 };
107     std::vector<T> masterBuffer(ncoords, vzero);
108
109     // the ForceBuffer is going to access indices [10-15) through the masterBuffer
110     // and the outliers internally
111     int rangeStart = 10;
112     int rangeEnd   = 15;
113
114     ForceBuffer<T> forceBuffer(masterBuffer.data(), rangeStart, rangeEnd);
115
116     // in range
117     T internal1{ 1, 2, 3 };
118     T internal2{ 4, 5, 6 };
119     forceBuffer[10] = internal1;
120     forceBuffer[14] = internal2;
121
122     // outliers
123     T outlier{ 0, 1, 2 };
124     forceBuffer[5] = outlier;
125
126     std::vector<T> refMasterBuffer(ncoords, vzero);
127     refMasterBuffer[10] = internal1;
128     refMasterBuffer[14] = internal2;
129
130     for (size_t i = 0; i < masterBuffer.size(); ++i)
131     {
132         for (size_t m = 0; m < dimSize; ++m)
133         {
134             EXPECT_REAL_EQ_TOL(
135                     refMasterBuffer[i][m], masterBuffer[i][m], gmx::test::defaultRealTolerance());
136         }
137     }
138
139     for (size_t m = 0; m < dimSize; ++m)
140     {
141         EXPECT_REAL_EQ_TOL(outlier[m], forceBuffer[5][m], gmx::test::defaultRealTolerance());
142         EXPECT_REAL_EQ_TOL(vzero[m], forceBuffer[4][m], gmx::test::defaultRealTolerance());
143     }
144 }
145
146 } // namespace
147 } // namespace test
148 } // namespace nblib