2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 * This implements topology setup tests
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>
45 #include <gtest/gtest.h>
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"
62 TEST(NBlibTest, SpcMethanolForcesAreCorrect)
64 auto options = NBKernelOptions();
65 options.nbnxmSimd = SimdKernels::SimdNo;
66 options.coulombType = CoulombType::Cutoff;
68 SpcMethanolSimulationStateBuilder spcMethanolSystemBuilder;
70 auto simState = spcMethanolSystemBuilder.setupSimulationState();
72 auto forceCalculator = setupGmxForceCalculatorCpu(simState.topology(), options);
74 forceCalculator->updatePairlist(simState.coordinates(), simState.box());
76 gmx::ArrayRef<Vec3> forces(simState.forces());
77 ASSERT_NO_THROW(forceCalculator->compute(simState.coordinates(), simState.box(), forces));
79 RefDataChecker forcesOutputTest(5e-5);
80 forcesOutputTest.testArrays<Vec3>(forces, "SPC-methanol forces");
83 TEST(NBlibTest, ExpectedNumberOfForces)
85 auto options = NBKernelOptions();
86 options.nbnxmSimd = SimdKernels::SimdNo;
88 SpcMethanolSimulationStateBuilder spcMethanolSystemBuilder;
90 auto simState = spcMethanolSystemBuilder.setupSimulationState();
92 auto forceCalculator = setupGmxForceCalculatorCpu(simState.topology(), options);
94 forceCalculator->updatePairlist(simState.coordinates(), simState.box());
96 gmx::ArrayRef<Vec3> forces(simState.forces());
97 forceCalculator->compute(simState.coordinates(), simState.box(), forces);
98 EXPECT_EQ(simState.topology().numParticles(), forces.size());
101 TEST(NBlibTest, CanIntegrateSystem)
103 auto options = NBKernelOptions();
104 options.nbnxmSimd = SimdKernels::SimdNo;
105 options.numIterations = 1;
107 SpcMethanolSimulationStateBuilder spcMethanolSystemBuilder;
109 auto simState = spcMethanolSystemBuilder.setupSimulationState();
111 auto forceCalculator = setupGmxForceCalculatorCpu(simState.topology(), options);
113 forceCalculator->updatePairlist(simState.coordinates(), simState.box());
115 LeapFrog integrator(simState.topology(), simState.box());
117 for (int iter = 0; iter < options.numIterations; iter++)
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));
126 * Check if the following aspects of the ForceCalculator and
127 * LeapFrog (integrator) work as expected:
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.
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.
138 TEST(NBlibTest, UpdateChangesForces)
140 auto options = NBKernelOptions();
141 options.nbnxmSimd = SimdKernels::SimdNo;
142 options.numIterations = 1;
144 SpcMethanolSimulationStateBuilder spcMethanolSystemBuilder;
146 auto simState = spcMethanolSystemBuilder.setupSimulationState();
148 auto forceCalculator = setupGmxForceCalculatorCpu(simState.topology(), options);
150 forceCalculator->updatePairlist(simState.coordinates(), simState.box());
152 LeapFrog integrator(simState.topology(), simState.box());
155 gmx::ArrayRef<Vec3> forces(simState.forces());
157 forceCalculator->compute(simState.coordinates(), simState.box(), simState.forces());
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));
163 // zero original force buffer
164 zeroCartesianArray(forces);
166 // check if forces change without update step
167 forceCalculator->compute(simState.coordinates(), simState.box(), forces);
169 // check if forces change without update
170 for (size_t i = 0; i < forces_1.size(); i++)
172 for (int j = 0; j < dimSize; j++)
174 EXPECT_EQ(forces[i][j], forces_1[i][j]);
179 integrator.integrate(1.0, simState.coordinates(), simState.velocities(), simState.forces());
181 // zero original force buffer
182 zeroCartesianArray(forces);
185 forceCalculator->compute(simState.coordinates(), simState.box(), forces);
187 std::vector<Vec3> forces_2(forces.size());
188 std::copy(forces.begin(), forces.end(), begin(forces_2));
190 // check if forces change after update
191 for (size_t i = 0; i < forces_1.size(); i++)
193 for (int j = 0; j < dimSize; j++)
195 EXPECT_NE(forces_1[i][j], forces_2[i][j]);
200 TEST(NBlibTest, ArgonOplsaForcesAreCorrect)
202 auto options = NBKernelOptions();
203 options.nbnxmSimd = SimdKernels::SimdNo;
204 options.coulombType = CoulombType::Cutoff;
206 ArgonSimulationStateBuilder argonSystemBuilder(fftypes::OPLSA);
208 auto simState = argonSystemBuilder.setupSimulationState();
210 auto forceCalculator = setupGmxForceCalculatorCpu(simState.topology(), options);
212 forceCalculator->updatePairlist(simState.coordinates(), simState.box());
214 gmx::ArrayRef<Vec3> testForces(simState.forces());
215 forceCalculator->compute(simState.coordinates(), simState.box(), simState.forces());
217 RefDataChecker forcesOutputTest(1e-7);
218 forcesOutputTest.testArrays<Vec3>(testForces, "Argon forces");
221 TEST(NBlibTest, ArgonGromos43A1ForcesAreCorrect)
223 auto options = NBKernelOptions();
224 options.nbnxmSimd = SimdKernels::SimdNo;
225 options.coulombType = CoulombType::Cutoff;
227 ArgonSimulationStateBuilder argonSystemBuilder(fftypes::GROMOS43A1);
229 auto simState = argonSystemBuilder.setupSimulationState();
231 auto forceCalculator = setupGmxForceCalculatorCpu(simState.topology(), options);
233 forceCalculator->updatePairlist(simState.coordinates(), simState.box());
235 gmx::ArrayRef<Vec3> testForces(simState.forces());
236 forceCalculator->compute(simState.coordinates(), simState.box(), simState.forces());
238 RefDataChecker forcesOutputTest;
239 forcesOutputTest.testArrays<Vec3>(testForces, "Argon forces");