1 /*
2 * This file is part of the GROMACS molecular simulation package.
3 *
4 * Copyright (c) 2019,2020, 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 * Implements density fitting forces.
38 *
39 * \author Christian Blau <blau@kth.se>
40 * \ingroup module_math
41 */
42 #include "gmxpre.h"
43
44 #include "densityfittingforce.h"
45
46 #include "gromacs/math/functions.h"
47 #include "gromacs/math/multidimarray.h"
48
49 namespace gmx
50 {
51
52 /********************************************************************
53 * DensityFittingForce::Impl
54 */
55
56 /*! \internal \brief
57 * Private implementation class for DensityFittingForce.
58 */
59 class DensityFittingForce::Impl
60 {
61 public:
62 /*! \brief Construct densityfitting force implementation from
63 * spread of function used to generate the density and spread range.
64 * \param[in] kernelShapeParameters determine the shape of the spreading kernel
65 */
66 explicit Impl(const GaussianSpreadKernelParameters::Shape& kernelShapeParameters);
67 //! \copydoc DensityFittingForce::evaluateForce
68 RVec evaluateForce(const GaussianSpreadKernelParameters::PositionAndAmplitude& localParameters,
69 basic_mdspan<const float, dynamicExtents3D> densityDerivative);
70 //! The width of the Gaussian in lattice spacing units
71 DVec sigma_;
72 //! The spread range in lattice points
73 IVec latticeSpreadRange_;
74 //! The three one-dimensional Gaussians that are used in the force calculation
75 std::array<GaussianOn1DLattice, DIM> gauss1d_;
76 //! The outer product of a Gaussian along the z and y dimension
77 OuterProductEvaluator outerProductZY_;
78 };
79
Impl(const GaussianSpreadKernelParameters::Shape & kernelShapeParameters)80 DensityFittingForce::Impl::Impl(const GaussianSpreadKernelParameters::Shape& kernelShapeParameters) :
81 sigma_{ kernelShapeParameters.sigma_ },
82 latticeSpreadRange_{ kernelShapeParameters.latticeSpreadRange()[XX],
83 kernelShapeParameters.latticeSpreadRange()[YY],
84 kernelShapeParameters.latticeSpreadRange()[ZZ] },
85 gauss1d_({ GaussianOn1DLattice(latticeSpreadRange_[XX], sigma_[XX]),
86 GaussianOn1DLattice(latticeSpreadRange_[YY], sigma_[YY]),
87 GaussianOn1DLattice(latticeSpreadRange_[ZZ], sigma_[ZZ]) })
88 {
89 }
90
evaluateForce(const GaussianSpreadKernelParameters::PositionAndAmplitude & localParameters,basic_mdspan<const float,dynamicExtents3D> densityDerivative)91 RVec DensityFittingForce::Impl::evaluateForce(const GaussianSpreadKernelParameters::PositionAndAmplitude& localParameters,
92 basic_mdspan<const float, dynamicExtents3D> densityDerivative)
93 {
94 const IVec closestLatticePoint(roundToInt(localParameters.coordinate_[XX]),
95 roundToInt(localParameters.coordinate_[YY]),
96 roundToInt(localParameters.coordinate_[ZZ]));
97 const auto spreadRange = spreadRangeWithinLattice(
98 closestLatticePoint, densityDerivative.extents(), latticeSpreadRange_);
99
100 // do nothing if the added Gaussian will never reach the lattice
101 if (spreadRange.empty())
102 {
103 return {};
104 }
105
106 for (int dimension = XX; dimension <= ZZ; ++dimension)
107 {
108 // multiply with amplitude so that Gauss3D = (amplitude * Gauss_x) * Gauss_y * Gauss_z
109 const float gauss1DAmplitude = dimension > XX ? 1.0 : localParameters.amplitude_;
110 gauss1d_[dimension].spread(gauss1DAmplitude, localParameters.coordinate_[dimension]
111 - closestLatticePoint[dimension]);
112 }
113
114 const auto spreadZY = outerProductZY_(gauss1d_[ZZ].view(), gauss1d_[YY].view());
115 const auto spreadX = gauss1d_[XX].view();
116 const IVec spreadGridOffset(latticeSpreadRange_[XX] - closestLatticePoint[XX],
117 latticeSpreadRange_[YY] - closestLatticePoint[YY],
118 latticeSpreadRange_[ZZ] - closestLatticePoint[ZZ]);
119
120 const DVec differenceVectorScale = { 1. / (square(sigma_[XX])), 1. / (square(sigma_[YY])),
121 1. / (square(sigma_[ZZ])) };
122 const DVec differenceVectorOffset = scaleByVector(
123 spreadRange.begin().toDVec() - localParameters.coordinate_.toDVec(), differenceVectorScale);
124
125 DVec differenceVector = differenceVectorOffset;
126
127 DVec force = { 0., 0., 0. };
128
129 for (int zLatticeIndex = spreadRange.begin()[ZZ]; zLatticeIndex < spreadRange.end()[ZZ];
130 ++zLatticeIndex, differenceVector[ZZ] += differenceVectorScale[ZZ])
131 {
132 auto zSliceOfDerivative = densityDerivative[zLatticeIndex];
133
134 differenceVector[YY] = differenceVectorOffset[YY];
135 for (int yLatticeIndex = spreadRange.begin()[YY]; yLatticeIndex < spreadRange.end()[YY];
136 ++yLatticeIndex, differenceVector[YY] += differenceVectorScale[YY])
137 {
138 auto ySliceOfDerivative = zSliceOfDerivative[yLatticeIndex];
139 const auto zyPrefactor = spreadZY(zLatticeIndex + spreadGridOffset[ZZ],
140 yLatticeIndex + spreadGridOffset[YY]);
141
142 differenceVector[XX] = differenceVectorOffset[XX];
143 for (int xLatticeIndex = spreadRange.begin()[XX]; xLatticeIndex < spreadRange.end()[XX];
144 ++xLatticeIndex, differenceVector[XX] += differenceVectorScale[XX])
145 {
146 const double preFactor = zyPrefactor * spreadX[xLatticeIndex + spreadGridOffset[XX]]
147 * ySliceOfDerivative[xLatticeIndex];
148 force += preFactor * differenceVector;
149 }
150 }
151 }
152 return localParameters.amplitude_ * force.toRVec();
153 }
154
155 /********************************************************************
156 * DensityFittingForce
157 */
158
DensityFittingForce(const GaussianSpreadKernelParameters::Shape & kernelShapeParameters)159 DensityFittingForce::DensityFittingForce(const GaussianSpreadKernelParameters::Shape& kernelShapeParameters) :
160 impl_(new Impl(kernelShapeParameters))
161 {
162 }
163
evaluateForce(const GaussianSpreadKernelParameters::PositionAndAmplitude & localParameters,basic_mdspan<const float,dynamicExtents3D> densityDerivative)164 RVec DensityFittingForce::evaluateForce(const GaussianSpreadKernelParameters::PositionAndAmplitude& localParameters,
165 basic_mdspan<const float, dynamicExtents3D> densityDerivative)
166 {
167 return impl_->evaluateForce(localParameters, densityDerivative);
168 }
169
~DensityFittingForce()170 DensityFittingForce::~DensityFittingForce() {}
171
DensityFittingForce(const DensityFittingForce & other)172 DensityFittingForce::DensityFittingForce(const DensityFittingForce& other) :
173 impl_(new Impl(*other.impl_))
174 {
175 }
176
operator =(const DensityFittingForce & other)177 DensityFittingForce& DensityFittingForce::operator=(const DensityFittingForce& other)
178 {
179 *impl_ = *other.impl_;
180 return *this;
181 }
182
183 DensityFittingForce::DensityFittingForce(DensityFittingForce&&) noexcept = default;
184
185 DensityFittingForce& DensityFittingForce::operator=(DensityFittingForce&&) noexcept = default;
186
187 } // namespace gmx
188