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