b3d69670a42f8923e301f05914a8c42993d2a9bd
[alexxy/gromacs.git] / src / gromacs / math / tests / densityfittingforce.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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  * Tests for the density fitting force.
38  *
39  * \author Christian Blau <blau@kth.se>
40  * \ingroup module_math
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/math/densityfittingforce.h"
45
46 #include <gmock/gmock.h>
47 #include <gtest/gtest.h>
48
49 #include "gromacs/math/multidimarray.h"
50 #include "gromacs/math/utilities.h"
51 #include "gromacs/math/vec.h"
52 #include "gromacs/mdspan/extensions.h"
53
54 #include "testutils/testasserts.h"
55 #include "testutils/testmatchers.h"
56 namespace gmx
57 {
58
59 namespace test
60 {
61
62 namespace
63 {
64
65 TEST(DensityFittingForce, isZeroWhenMatchingDensity)
66 {
67     const float sigma      = 1;
68     const float nSigma     = 5;
69     const RVec  gridCenter = { 1, 1, 1 };
70
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);
74
75     const RVec result =
76             forceEvaluator.evaluateForce({ gridCenter, 1.0 }, densityDerivative.asConstView());
77     const RVec             expected = { 0, 0, 0 };
78     FloatingPointTolerance tolerance(defaultFloatTolerance());
79
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);
83 }
84
85 TEST(DensityFittingForce, isZeroWhenMismatchingSameAllDirections)
86 {
87     const BasicVector<double> sigma      = { 1., 1., 1. };
88     const float               nSigma     = 5;
89     const RVec                gridCenter = { 1, 1, 1 };
90
91     DensityFittingForce                                 forceEvaluator({ { sigma }, nSigma });
92     MultiDimArray<std::vector<float>, dynamicExtents3D> densityDerivative(3, 3, 3);
93     std::fill(begin(densityDerivative), end(densityDerivative), 1);
94
95     const RVec result =
96             forceEvaluator.evaluateForce({ gridCenter, 1. }, densityDerivative.asConstView());
97     const RVec expected = { 0, 0, 0 };
98
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));
103
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);
107 }
108
109 TEST(DensityFittingForce, pullsTowardsDerivative)
110 {
111     const float sigma      = 1;
112     const float nSigma     = 5;
113     const RVec  gridCenter = { 1, 1, 1 };
114
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;
119
120     const RVec result =
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);
123
124     EXPECT_FLOAT_EQ(expected, result[XX]);
125     EXPECT_FLOAT_EQ(expected, result[YY]);
126     EXPECT_FLOAT_EQ(expected, result[ZZ]);
127 }
128
129 } // namespace
130
131 } // namespace test
132
133 } // namespace gmx