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 * Implements the data flow from ListedInteractionData and coordinates
38 * down to the individual interaction type kernels
40 * Intended for internal use inside ListedCalculator only.
42 * \author Victor Holanda <victor.holanda@cscs.ch>
43 * \author Joe Jordan <ejjordan@kth.se>
44 * \author Prashanth Kanduri <kanduri@cscs.ch>
45 * \author Sebastian Keller <keller@cscs.ch>
46 * \author Artem Zhmurov <zhmurov@gmail.com>
49 #ifndef NBLIB_LISTEDFORCES_DATAFLOW_HPP
50 #define NBLIB_LISTEDFORCES_DATAFLOW_HPP
55 #include "nblib/listed_forces/traits.h"
56 #include "nblib/listed_forces/kernels.hpp"
57 #include "nblib/util/util.hpp"
58 #include "nblib/pbc.hpp"
59 #include "nblib/vector.h"
60 #include "gromacs/utility/arrayref.h"
62 #define NBLIB_ALWAYS_INLINE __attribute((always_inline))
67 template<class TwoCenterType, class BasicVector>
68 inline NBLIB_ALWAYS_INLINE
69 auto computeTwoCenter(const TwoCenterType& parameters, const BasicVector& dx, BasicVector* fi, BasicVector* fj)
71 using ValueType = BasicVectorValueType_t<BasicVector>;
73 ValueType dr2 = dot(dx, dx);
74 ValueType dr = std::sqrt(dr2);
75 auto [force, energy] = bondKernel(dr, parameters);
77 // avoid division by 0
81 spreadTwoCenterForces(force, dx, fi, fj);
87 /*! \brief calculate two-center interactions
89 * \tparam Force buffer type
90 * \tparam TwoCenterType The bond type to compute; used for type deduction
91 * \tparam Cartesian vector type
93 * \param[in] index The atom and parameter indices used for computing the interaction
94 * \param[in] bondInstances The full type-specific interaction list
95 * \param[in] x The coordinates
96 * \param[in/out] forces The forces
97 * \param[in] pbc Object used for computing distances accounting for PBC's
98 * \return Computed kernel energies
100 template <class Buffer, class TwoCenterType, class BasicVector, class Pbc,
101 std::enable_if_t<Contains<TwoCenterType, SupportedTwoCenterTypes>{}>* = nullptr>
102 inline NBLIB_ALWAYS_INLINE
103 auto dispatchInteraction(InteractionIndex<TwoCenterType> index,
104 gmx::ArrayRef<const TwoCenterType> bondInstances,
105 gmx::ArrayRef<const BasicVector> x,
109 KernelEnergy<BasicVectorValueType_t<BasicVector>> energy;
111 int i = std::get<0>(index);
112 int j = std::get<1>(index);
113 BasicVector x1 = x[i];
114 BasicVector x2 = x[j];
115 TwoCenterType bond = bondInstances[std::get<2>(index)];
118 // calculate x1 - x2 modulo pbc
119 pbc.dxAiuc(x1, x2, dx);
121 energy.carrier() = computeTwoCenter(bond, dx, &(*forces)[i], &(*forces)[j]);
126 template<class ThreeCenterType, class BasicVector>
127 inline NBLIB_ALWAYS_INLINE
128 std::enable_if_t<HasTwoCenterAggregate<ThreeCenterType>::value, BasicVectorValueType_t<BasicVector>>
129 addTwoCenterAggregate(const ThreeCenterType& parameters, const BasicVector& rij, const BasicVector& rkj,
130 BasicVector* fi, BasicVector* fj, BasicVector* fk)
132 if (parameters.manifest == ThreeCenterType::Cargo::ij)
135 return computeTwoCenter(parameters.twoCenter(), rij, fi, fj);
137 if (parameters.manifest == ThreeCenterType::Cargo::jk)
140 return computeTwoCenter(parameters.twoCenter(), rkj, fk, fj);
143 // aggregate is empty
147 template<class ThreeCenterType, class BasicVector>
148 inline NBLIB_ALWAYS_INLINE
149 std::enable_if_t<!HasTwoCenterAggregate<ThreeCenterType>::value, BasicVectorValueType_t<BasicVector>>
150 addTwoCenterAggregate([[maybe_unused]] const ThreeCenterType& parameters,
151 [[maybe_unused]] const BasicVector& rij,
152 [[maybe_unused]] const BasicVector& rkj,
153 [[maybe_unused]] BasicVector* fi,
154 [[maybe_unused]] BasicVector* fj,
155 [[maybe_unused]] BasicVector* fk)
160 template<class ThreeCenterType, class BasicVector>
161 inline NBLIB_ALWAYS_INLINE
162 auto computeThreeCenter(const ThreeCenterType& parameters, const BasicVector& rij, const BasicVector& rkj,
163 [[maybe_unused]] const BasicVector& rik, BasicVector* fi, BasicVector* fj, BasicVector* fk)
165 using ValueType = BasicVectorValueType_t<BasicVector>;
166 // calculate 3-center common quantities: angle between x1-x2 and x2-x3
167 // Todo: after sufficient evaluation, switch over to atan2 based algorithm
168 ValueType costh = basicVectorCosAngle(rij, rkj); /* 25 */
169 ValueType theta = std::acos(costh); /* 10 */
171 // call type-specific angle kernel, e.g. harmonic, linear, quartic, etc.
172 auto [force, energy] = threeCenterKernel(theta, parameters);
174 spreadThreeCenterForces(costh, force, rij, rkj, fi, fj, fk);
179 /*! \brief Calculate three-center interactions
181 * \tparam Force buffer type
182 * \tparam Three centre interaction parameters
183 * \tparam Cartesian vector type
186 * \param[in] Bond parameters
187 * \param[in] x coordinate array
188 * \param[in/out] Force buffer
190 * \return Computed kernel energies
192 template <class Buffer, class ThreeCenterType, class BasicVector, class Pbc,
193 std::enable_if_t<Contains<ThreeCenterType, SupportedThreeCenterTypes>{}>* = nullptr>
194 inline NBLIB_ALWAYS_INLINE
195 auto dispatchInteraction(InteractionIndex<ThreeCenterType> index,
196 gmx::ArrayRef<const ThreeCenterType> parameters,
197 gmx::ArrayRef<const BasicVector> x,
201 KernelEnergy<BasicVectorValueType_t<BasicVector>> energy;
203 //! fetch input data: position vectors x1-x3 and interaction parameters
204 int i = std::get<0>(index);
205 int j = std::get<1>(index);
206 int k = std::get<2>(index);
207 BasicVector xi = x[i];
208 BasicVector xj = x[j];
209 BasicVector xk = x[k];
210 ThreeCenterType threeCenterParameters = parameters[std::get<3>(index)];
212 BasicVector fi{0,0,0}, fj{0,0,0}, fk{0,0,0};
214 BasicVector rij, rkj, rik;
215 pbc.dxAiuc(xi, xj, rij); /* 3 */
216 pbc.dxAiuc(xk, xj, rkj); /* 3 */
217 pbc.dxAiuc(xi, xk, rik); /* 3 */
219 energy.carrier() = computeThreeCenter(threeCenterParameters, rij, rkj, rik, &fi, &fj, &fk);
220 energy.twoCenterAggregate() = addTwoCenterAggregate(threeCenterParameters, rij, rkj, &fi, &fj, &fk);
229 template<class FourCenterType, class BasicVector>
230 inline NBLIB_ALWAYS_INLINE
231 std::enable_if_t<HasThreeCenterAggregate<FourCenterType>::value, BasicVectorValueType_t<BasicVector>>
232 addThreeCenterAggregate(const FourCenterType& parameters,
233 const BasicVector& rij, const BasicVector& rkj, const BasicVector& rkl,
234 BasicVector* fi, BasicVector* fj, BasicVector* fk, BasicVector* fl)
236 using ValueType = BasicVectorValueType_t<BasicVector>;
237 if (parameters.manifest == FourCenterType::Cargo::j)
239 return computeThreeCenter(parameters.threeCenter(), rij, rkj, fi, fj, fk);
241 if (parameters.manifest == FourCenterType::Cargo::k)
243 return computeThreeCenter(parameters.threeCenter(), ValueType(-1.0)*rkj, ValueType(-1.0)*rkl, fj, fk, fl);
244 //return computeThreeCenter(parameters.threeCenter(), rkj, rkl, fj, fk, fl);
247 // aggregate is empty
251 template<class FourCenterType, class BasicVector>
252 inline NBLIB_ALWAYS_INLINE
253 std::enable_if_t<!HasThreeCenterAggregate<FourCenterType>::value, BasicVectorValueType_t<BasicVector>>
254 addThreeCenterAggregate([[maybe_unused]] const FourCenterType& parameters,
255 [[maybe_unused]] const BasicVector& rij,
256 [[maybe_unused]] const BasicVector& rkj,
257 [[maybe_unused]] const BasicVector& rkl,
258 [[maybe_unused]] BasicVector* fi,
259 [[maybe_unused]] BasicVector* fj,
260 [[maybe_unused]] BasicVector* fk,
261 [[maybe_unused]] BasicVector* fl)
266 /*! \brief Calculate four-center interactions
268 * \tparam Force buffer type
269 * \tparam FourCenterType The bond type to compute; used for type deduction
270 * \tparam Cartesian vector type
272 * \param[in] index The atom and parameter indices used for computing the interaction
273 * \param[in] parameters The full type-specific interaction list
274 * \param[in] x The coordinates
275 * \param[in/out] forces The forces
276 * \param[in] pbc Object used for computing distances accounting for PBC's
277 * \return Computed kernel energies
279 template <class Buffer, class FourCenterType, class BasicVector, class Pbc,
280 std::enable_if_t<Contains<FourCenterType, SupportedFourCenterTypes>{}>* = nullptr>
281 inline NBLIB_ALWAYS_INLINE
282 auto dispatchInteraction(InteractionIndex<FourCenterType> index,
283 gmx::ArrayRef<const FourCenterType> parameters,
284 gmx::ArrayRef<const BasicVector> x,
288 using RealScalar = BasicVectorValueType_t<BasicVector>;
289 KernelEnergy<RealScalar> energy;
291 int i = std::get<0>(index);
292 int j = std::get<1>(index);
293 int k = std::get<2>(index);
294 int l = std::get<3>(index);
296 BasicVector xi = x[i];
297 BasicVector xj = x[j];
298 BasicVector xk = x[k];
299 BasicVector xl = x[l];
301 BasicVector fi{0,0,0}, fj{0,0,0}, fk{0,0,0}, fl{0,0,0};
303 BasicVector dxIJ, dxKJ, dxKL;
304 pbc.dxAiuc(xi, xj, dxIJ);
305 pbc.dxAiuc(xk, xj, dxKJ);
306 pbc.dxAiuc(xk, xl, dxKL);
308 FourCenterType fourCenterTypeParams = parameters[std::get<4>(index)];
311 RealScalar phi = dihedralPhi(dxIJ, dxKJ, dxKL, m, n);
313 auto [force, kernelEnergy] = fourCenterKernel(phi, fourCenterTypeParams);
315 energy.carrier() = kernelEnergy;
316 energy.threeCenterAggregate() = addThreeCenterAggregate(fourCenterTypeParams, dxIJ, dxKJ, dxKL, &fi, &fj, &fk, &fl);
318 spreadFourCenterForces(force, dxIJ, dxKJ, dxKL, m, n, &fi, &fj, &fk, &fl);
328 /*! \brief Calculate five-center interactions
330 * \tparam Force buffer type
331 * \tparam FiveCenterType The bond type to compute; used for type deduction
332 * \tparam Cartesian vector type
334 * \param[in] index The atom and parameter indices used for computing the interaction
335 * \param[in] parameters The full type-specific interaction list
336 * \param[in] x The coordinates
337 * \param[in/out] forces The forces
338 * \param[in] pbc Object used for computing distances accounting for PBC's
339 * \return Computed kernel energies
341 template <class Buffer, class FiveCenterType, class BasicVector, class Pbc,
342 std::enable_if_t<Contains<FiveCenterType, SupportedFiveCenterTypes>{}>* = nullptr>
343 inline NBLIB_ALWAYS_INLINE
344 auto dispatchInteraction(InteractionIndex<FiveCenterType> index,
345 gmx::ArrayRef<const FiveCenterType> parameters,
346 gmx::ArrayRef<const BasicVector> x,
350 KernelEnergy<BasicVectorValueType_t<BasicVector>> energy;
352 int i = std::get<0>(index);
353 int j = std::get<1>(index);
354 int k = std::get<2>(index);
355 int l = std::get<3>(index);
356 int m = std::get<4>(index);
358 BasicVector xi = x[i];
359 BasicVector xj = x[j];
360 BasicVector xk = x[k];
361 BasicVector xl = x[l];
362 BasicVector xm = x[m];
364 BasicVector dxIJ, dxJK, dxKL, dxLM;
365 pbc.dxAiuc(xi, xj, dxIJ);
366 pbc.dxAiuc(xj, xk, dxJK);
367 pbc.dxAiuc(xk, xl, dxKL);
368 pbc.dxAiuc(xl, xm, dxLM);
370 FiveCenterType fiveCenterTypeParams = parameters[std::get<5>(index)];
372 // this dispatch function is not in use yet, because CMap is not yet implemented
373 // we don't want to add [[maybe_unused]] in the signature
374 // and we also don't want compiler warnings, so we cast to void
375 (void)fiveCenterTypeParams;
382 /*! \brief implement a loop over bonds for a given BondType and Kernel
383 * corresponds to e.g. the "bonds" function at Gromacs:bonded.cpp@450
385 * \param[in] indices interaction atom pair indices + bond parameter index
386 * \param[in] interactionParameters bond/interaction parameters
387 * \param[in] x coordinate input
388 * \param[in/out] forces The forces
389 * \param[in] pbc Object used for computing distances accounting for PBC's
390 * \return Computed kernel energies
392 template <class Index, class InteractionType, class Buffer, class Pbc>
393 auto computeForces(gmx::ArrayRef<const Index> indices,
394 gmx::ArrayRef<const InteractionType> parameters,
395 gmx::ArrayRef<const Vec3> x,
399 KernelEnergy<BasicVectorValueType_t<Vec3>> energy;
401 for (const auto& index : indices)
403 energy += dispatchInteraction(index, parameters, x, forces, pbc);
409 /*! \brief implement a loop over bond types and accumulate their force contributions
411 * \param[in] interactions interaction pairs and bond parameters
412 * \param[in] x coordinate input
413 * \param[in/out] forces output force buffer
414 * \param[in] pbc Object used for computing distances accounting for PBC's
415 * \return Computed kernel energies
417 template<class Buffer, class Pbc>
418 auto reduceListedForces(const ListedInteractionData& interactions,
419 gmx::ArrayRef<const Vec3> x,
423 using ValueType = BasicVectorValueType_t<Vec3>;
424 std::array<ValueType, std::tuple_size<ListedInteractionData>::value> energies{0};
427 // calculate one bond type
428 auto computeForceType = [forces, &x, &energies, &pbc](const auto& interactionElement) {
429 using InteractionType = typename std::decay_t<decltype(interactionElement)>::type;
431 gmx::ArrayRef<const InteractionIndex<InteractionType>> indices(interactionElement.indices);
432 gmx::ArrayRef<const InteractionType> parameters(interactionElement.parameters);
433 KernelEnergy<ValueType> energy = computeForces(indices, parameters, x, forces, pbc);
435 energies[CarrierIndex<InteractionType>{}] += energy.carrier();
436 energies[TwoCenterAggregateIndex<InteractionType>{}] += energy.twoCenterAggregate();
437 energies[ThreeCenterAggregateIndex<InteractionType>{}] += energy.threeCenterAggregate();
438 // energies[FepIndex{}] += energy.freeEnergyDerivative();
441 // calculate all bond types, returns a tuple with the energies for each type
442 for_each_tuple(computeForceType, interactions);
449 #undef NBLIB_ALWAYS_INLINE
451 #endif // NBLIB_LISTEDFORCES_DATAFLOW_HPP