Add replacements for pbc enumerations
[gromacs.git] / src / gromacs / math / tests / densityfittingforce.cpp
blob1d84933638e402cacba362fc54b50f8bb07fda14
1 /*
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.
35 /*! \internal \file
36 * \brief
37 * Tests for the density fitting force.
39 * \author Christian Blau <blau@kth.se>
40 * \ingroup module_math
42 #include "gmxpre.h"
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"
56 namespace gmx
59 namespace test
62 namespace
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);
75 const RVec result = forceEvaluator.evaluateForce({gridCenter, 1.0}, densityDerivative.asConstView());
76 const RVec expected = {0, 0, 0};
77 FloatingPointTolerance tolerance(defaultFloatTolerance());
79 EXPECT_FLOAT_EQ_TOL(expected[XX], result[XX], tolerance);
80 EXPECT_FLOAT_EQ_TOL(expected[YY], result[YY], tolerance);
81 EXPECT_FLOAT_EQ_TOL(expected[ZZ], result[ZZ], tolerance);
84 TEST(DensityFittingForce, isZeroWhenMismatchingSameAllDirections)
86 const BasicVector<double> sigma = {1., 1., 1.};
87 const float nSigma = 5;
88 const RVec gridCenter = {1, 1, 1};
90 DensityFittingForce forceEvaluator({{sigma}, nSigma});
91 MultiDimArray<std::vector<float>, dynamicExtents3D> densityDerivative(3, 3, 3);
92 std::fill(begin(densityDerivative), end(densityDerivative), 1);
94 const RVec result = forceEvaluator.evaluateForce({gridCenter, 1.}, densityDerivative.asConstView());
95 const RVec expected = {0, 0, 0};
97 // need to increase the tolerance here, because the checked value is the
98 // result of a complex calculation (the sum of 27 3d Gaussians weighted with the distance to center)
99 // \todo increase tightness of this bound with better implementations
100 FloatingPointTolerance tolerance(absoluteTolerance(1e-8));
102 EXPECT_FLOAT_EQ_TOL(expected[XX], result[XX], tolerance);
103 EXPECT_FLOAT_EQ_TOL(expected[YY], result[YY], tolerance);
104 EXPECT_FLOAT_EQ_TOL(expected[ZZ], result[ZZ], tolerance);
107 TEST(DensityFittingForce, pullsTowardsDerivative)
109 const float sigma = 1;
110 const float nSigma = 5;
111 const RVec gridCenter = {1, 1, 1};
113 DensityFittingForce forceEvaluator({{sigma, sigma, sigma}, nSigma});
114 MultiDimArray<std::vector<float>, dynamicExtents3D> densityDerivative(3, 3, 3);
115 std::fill(begin(densityDerivative), end(densityDerivative), 0);
116 densityDerivative(0, 0, 0) = 1;
118 const RVec result = forceEvaluator.evaluateForce({gridCenter, 1.}, densityDerivative.asConstView());
119 real expected = -1/sqrt(2*2*2*M_PI*M_PI*M_PI)*exp(-0.5)*exp(-0.5)*exp(-0.5);
121 EXPECT_FLOAT_EQ(expected, result[XX]);
122 EXPECT_FLOAT_EQ(expected, result[YY]);
123 EXPECT_FLOAT_EQ(expected, result[ZZ]);
126 } // namespace
128 } // namespace test
130 } // namespace gmx