2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019, 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 * Tests for the density fitting force.
39 * \author Christian Blau <blau@kth.se>
40 * \ingroup module_math
44 #include "gromacs/math/densityfittingforce.h"
46 #include <gmock/gmock.h>
47 #include <gtest/gtest.h>
49 #include "gromacs/math/multidimarray.h"
50 #include "gromacs/math/utilities.h"
51 #include "gromacs/math/vec.h"
52 #include "gromacs/mdspan/extensions.h"
54 #include "testutils/testasserts.h"
55 #include "testutils/testmatchers.h"
65 TEST(DensityFittingForce, isZeroWhenMatchingDensity)
67 const float sigma = 1;
68 const float nSigma = 5;
69 const RVec gridCenter = { 1, 1, 1 };
71 DensityFittingForce forceEvaluator({ { sigma, sigma, sigma }, nSigma });
72 MultiDimArray<std::vector<float>, dynamicExtents3D> densityDerivative(3, 3, 3);
73 std::fill(begin(densityDerivative), end(densityDerivative), 0);
76 forceEvaluator.evaluateForce({ gridCenter, 1.0 }, densityDerivative.asConstView());
77 const RVec expected = { 0, 0, 0 };
78 FloatingPointTolerance tolerance(defaultFloatTolerance());
80 EXPECT_FLOAT_EQ_TOL(expected[XX], result[XX], tolerance);
81 EXPECT_FLOAT_EQ_TOL(expected[YY], result[YY], tolerance);
82 EXPECT_FLOAT_EQ_TOL(expected[ZZ], result[ZZ], tolerance);
85 TEST(DensityFittingForce, isZeroWhenMismatchingSameAllDirections)
87 const BasicVector<double> sigma = { 1., 1., 1. };
88 const float nSigma = 5;
89 const RVec gridCenter = { 1, 1, 1 };
91 DensityFittingForce forceEvaluator({ { sigma }, nSigma });
92 MultiDimArray<std::vector<float>, dynamicExtents3D> densityDerivative(3, 3, 3);
93 std::fill(begin(densityDerivative), end(densityDerivative), 1);
96 forceEvaluator.evaluateForce({ gridCenter, 1. }, densityDerivative.asConstView());
97 const RVec expected = { 0, 0, 0 };
99 // need to increase the tolerance here, because the checked value is the
100 // result of a complex calculation (the sum of 27 3d Gaussians weighted with the distance to
101 // center) \todo increase tightness of this bound with better implementations
102 FloatingPointTolerance tolerance(absoluteTolerance(1e-8));
104 EXPECT_FLOAT_EQ_TOL(expected[XX], result[XX], tolerance);
105 EXPECT_FLOAT_EQ_TOL(expected[YY], result[YY], tolerance);
106 EXPECT_FLOAT_EQ_TOL(expected[ZZ], result[ZZ], tolerance);
109 TEST(DensityFittingForce, pullsTowardsDerivative)
111 const float sigma = 1;
112 const float nSigma = 5;
113 const RVec gridCenter = { 1, 1, 1 };
115 DensityFittingForce forceEvaluator({ { sigma, sigma, sigma }, nSigma });
116 MultiDimArray<std::vector<float>, dynamicExtents3D> densityDerivative(3, 3, 3);
117 std::fill(begin(densityDerivative), end(densityDerivative), 0);
118 densityDerivative(0, 0, 0) = 1;
121 forceEvaluator.evaluateForce({ gridCenter, 1. }, densityDerivative.asConstView());
122 real expected = -1 / sqrt(2 * 2 * 2 * M_PI * M_PI * M_PI) * exp(-0.5) * exp(-0.5) * exp(-0.5);
124 EXPECT_FLOAT_EQ(expected, result[XX]);
125 EXPECT_FLOAT_EQ(expected, result[YY]);
126 EXPECT_FLOAT_EQ(expected, result[ZZ]);