Introduce nblib nonbonded force calculator impl class
[alexxy/gromacs.git] / api / nblib / tests / nbkernelsystem.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 topology setup 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  * \author Artem Zhmurov <zhmurov@gmail.com>
44  */
45 #include <gtest/gtest.h>
46
47 #include "gromacs/topology/exclusionblocks.h"
48 #include "nblib/gmxcalculatorcpu.h"
49 #include "nblib/integrator.h"
50 #include "nblib/tests/testhelpers.h"
51 #include "nblib/tests/testsystems.h"
52 #include "nblib/topology.h"
53 #include "nblib/util/setup.h"
54
55 namespace nblib
56 {
57 namespace test
58 {
59 namespace
60 {
61
62 TEST(NBlibTest, SpcMethanolForcesAreCorrect)
63 {
64     auto options        = NBKernelOptions();
65     options.nbnxmSimd   = SimdKernels::SimdNo;
66     options.coulombType = CoulombType::Cutoff;
67
68     SpcMethanolSimulationStateBuilder spcMethanolSystemBuilder;
69
70     auto simState = spcMethanolSystemBuilder.setupSimulationState();
71
72     auto forceCalculator = setupGmxForceCalculatorCpu(simState.topology(), options);
73
74     forceCalculator->updatePairlist(simState.coordinates(), simState.box());
75
76     gmx::ArrayRef<Vec3> forces(simState.forces());
77     ASSERT_NO_THROW(forceCalculator->compute(simState.coordinates(), simState.box(), forces));
78
79     RefDataChecker forcesOutputTest(5e-5);
80     forcesOutputTest.testArrays<Vec3>(forces, "SPC-methanol forces");
81 }
82
83 TEST(NBlibTest, ExpectedNumberOfForces)
84 {
85     auto options      = NBKernelOptions();
86     options.nbnxmSimd = SimdKernels::SimdNo;
87
88     SpcMethanolSimulationStateBuilder spcMethanolSystemBuilder;
89
90     auto simState = spcMethanolSystemBuilder.setupSimulationState();
91
92     auto forceCalculator = setupGmxForceCalculatorCpu(simState.topology(), options);
93
94     forceCalculator->updatePairlist(simState.coordinates(), simState.box());
95
96     gmx::ArrayRef<Vec3> forces(simState.forces());
97     forceCalculator->compute(simState.coordinates(), simState.box(), forces);
98     EXPECT_EQ(simState.topology().numParticles(), forces.size());
99 }
100
101 TEST(NBlibTest, CanIntegrateSystem)
102 {
103     auto options          = NBKernelOptions();
104     options.nbnxmSimd     = SimdKernels::SimdNo;
105     options.numIterations = 1;
106
107     SpcMethanolSimulationStateBuilder spcMethanolSystemBuilder;
108
109     auto simState = spcMethanolSystemBuilder.setupSimulationState();
110
111     auto forceCalculator = setupGmxForceCalculatorCpu(simState.topology(), options);
112
113     forceCalculator->updatePairlist(simState.coordinates(), simState.box());
114
115     LeapFrog integrator(simState.topology(), simState.box());
116
117     for (int iter = 0; iter < options.numIterations; iter++)
118     {
119         gmx::ArrayRef<Vec3> forces(simState.forces());
120         forceCalculator->compute(simState.coordinates(), simState.box(), forces);
121         EXPECT_NO_THROW(integrator.integrate(1.0, simState.coordinates(), simState.velocities(), forces));
122     }
123 }
124
125 /*!
126  * Check if the following aspects of the ForceCalculator and
127  * LeapFrog (integrator) work as expected:
128  *
129  * 1. Calling the ForceCalculator::compute() function makes no change
130  *    to the internal representation of the system. Calling it repeatedly
131  *    without update should return the same vector of forces.
132  *
133  * 2. Once the LeapFrog objects integrates for the given time using the
134  *    forces, there the coordinates in SimulationState must change.
135  *    Calling the compute() function must now generate a new set of forces.
136  *
137  */
138 TEST(NBlibTest, UpdateChangesForces)
139 {
140     auto options          = NBKernelOptions();
141     options.nbnxmSimd     = SimdKernels::SimdNo;
142     options.numIterations = 1;
143
144     SpcMethanolSimulationStateBuilder spcMethanolSystemBuilder;
145
146     auto simState = spcMethanolSystemBuilder.setupSimulationState();
147
148     auto forceCalculator = setupGmxForceCalculatorCpu(simState.topology(), options);
149
150     forceCalculator->updatePairlist(simState.coordinates(), simState.box());
151
152     LeapFrog integrator(simState.topology(), simState.box());
153
154     // step 1
155     gmx::ArrayRef<Vec3> forces(simState.forces());
156
157     forceCalculator->compute(simState.coordinates(), simState.box(), simState.forces());
158
159     // copy computed forces to another array
160     std::vector<Vec3> forces_1(forces.size());
161     std::copy(forces.begin(), forces.end(), begin(forces_1));
162
163     // zero original force buffer
164     zeroCartesianArray(forces);
165
166     // check if forces change without update step
167     forceCalculator->compute(simState.coordinates(), simState.box(), forces);
168
169     // check if forces change without update
170     for (size_t i = 0; i < forces_1.size(); i++)
171     {
172         for (int j = 0; j < dimSize; j++)
173         {
174             EXPECT_EQ(forces[i][j], forces_1[i][j]);
175         }
176     }
177
178     // update
179     integrator.integrate(1.0, simState.coordinates(), simState.velocities(), simState.forces());
180
181     // zero original force buffer
182     zeroCartesianArray(forces);
183
184     // step 2
185     forceCalculator->compute(simState.coordinates(), simState.box(), forces);
186
187     std::vector<Vec3> forces_2(forces.size());
188     std::copy(forces.begin(), forces.end(), begin(forces_2));
189
190     // check if forces change after update
191     for (size_t i = 0; i < forces_1.size(); i++)
192     {
193         for (int j = 0; j < dimSize; j++)
194         {
195             EXPECT_NE(forces_1[i][j], forces_2[i][j]);
196         }
197     }
198 }
199
200 TEST(NBlibTest, ArgonOplsaForcesAreCorrect)
201 {
202     auto options        = NBKernelOptions();
203     options.nbnxmSimd   = SimdKernels::SimdNo;
204     options.coulombType = CoulombType::Cutoff;
205
206     ArgonSimulationStateBuilder argonSystemBuilder(fftypes::OPLSA);
207
208     auto simState = argonSystemBuilder.setupSimulationState();
209
210     auto forceCalculator = setupGmxForceCalculatorCpu(simState.topology(), options);
211
212     forceCalculator->updatePairlist(simState.coordinates(), simState.box());
213
214     gmx::ArrayRef<Vec3> testForces(simState.forces());
215     forceCalculator->compute(simState.coordinates(), simState.box(), simState.forces());
216
217     RefDataChecker forcesOutputTest(1e-7);
218     forcesOutputTest.testArrays<Vec3>(testForces, "Argon forces");
219 }
220
221 TEST(NBlibTest, ArgonGromos43A1ForcesAreCorrect)
222 {
223     auto options        = NBKernelOptions();
224     options.nbnxmSimd   = SimdKernels::SimdNo;
225     options.coulombType = CoulombType::Cutoff;
226
227     ArgonSimulationStateBuilder argonSystemBuilder(fftypes::GROMOS43A1);
228
229     auto simState = argonSystemBuilder.setupSimulationState();
230
231     auto forceCalculator = setupGmxForceCalculatorCpu(simState.topology(), options);
232
233     forceCalculator->updatePairlist(simState.coordinates(), simState.box());
234
235     gmx::ArrayRef<Vec3> testForces(simState.forces());
236     forceCalculator->compute(simState.coordinates(), simState.box(), simState.forces());
237
238     RefDataChecker forcesOutputTest;
239     forcesOutputTest.testArrays<Vec3>(testForces, "Argon forces");
240 }
241
242 } // namespace
243 } // namespace test
244 } // namespace nblib