Expand signatures for nblib listed forces calculator
[alexxy/gromacs.git] / api / nblib / listed_forces / calculator.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020,2021, 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 /*! \inpublicapi \file
36  * \brief
37  * Implements a force calculator based on GROMACS data structures.
38  *
39  * Intended for internal use inside the ForceCalculator.
40  *
41  * \author Victor Holanda <victor.holanda@cscs.ch>
42  * \author Joe Jordan <ejjordan@kth.se>
43  * \author Prashanth Kanduri <kanduri@cscs.ch>
44  * \author Sebastian Keller <keller@cscs.ch>
45  * \author Artem Zhmurov <zhmurov@gmail.com>
46  */
47
48 #ifndef NBLIB_LISTEDFORCES_CALCULATOR_H
49 #define NBLIB_LISTEDFORCES_CALCULATOR_H
50
51 #include <memory>
52 #include <unordered_map>
53
54 #include "nblib/listed_forces/definitions.h"
55 #include "nblib/vector.h"
56
57 namespace gmx
58 {
59 template<typename T>
60 class ArrayRef;
61 } // namespace gmx
62
63 namespace nblib
64 {
65 class Box;
66 class PbcHolder;
67 template<class T>
68 class ForceBufferProxy;
69
70 /*! \internal \brief Object to calculate forces and energies of listed forces
71  *
72  */
73 class ListedForceCalculator
74 {
75 public:
76     using EnergyType = std::array<real, std::tuple_size<ListedInteractionData>::value>;
77
78     ListedForceCalculator(const ListedInteractionData& interactions,
79                           size_t                       bufferSize,
80                           int                          numThreads,
81                           const Box&                   box);
82
83     /*! \brief Dispatch the listed force kernels and reduce the forces
84      *
85      * This function adds the computed listed forces to all values in the passed in forces buffer,
86      * so it can be regarded as an output only param. In case this is being used in a simulation
87      * that uses the same force buffer for both non-bonded and listed forces, this call should be
88      * made only after the compute() call from the non-bonded ForceCalculator
89      *
90      * This function also stores the forces and energies from listed interactions in the internal
91      * buffer of the ListedForceCalculator object
92      *
93      * \param[in]  coordinates     input coordinates for the force calculation
94      * \param[inout] forces        output for adding the forces
95      * \param[inout] shiftForces   output for adding shift forces
96      * \param[out] energies        output for potential energies
97      * \param[in]  usePbc          whether or not to consider periodic boundary conditions
98      */
99     void compute(gmx::ArrayRef<const Vec3> coordinates,
100                  gmx::ArrayRef<Vec3>       forces,
101                  gmx::ArrayRef<Vec3>       shiftForces,
102                  gmx::ArrayRef<real>       energies,
103                  bool                      usePbc = false);
104
105     //! \brief Alternative overload without shift forces
106     void compute(gmx::ArrayRef<const Vec3> coordinates,
107                  gmx::ArrayRef<Vec3>       forces,
108                  gmx::ArrayRef<real>       energies,
109                  bool                      usePbc = false);
110
111     //! \brief default, but moved to separate compilation unit
112     ~ListedForceCalculator();
113
114 private:
115     int numThreads;
116
117     //! holds the array of energies computed
118     EnergyType energyBuffer_;
119
120     //! holds the listed interactions split into groups for multithreading
121     std::vector<ListedInteractionData> threadedInteractions_;
122
123     //! reduction force buffers
124     std::vector<ForceBufferProxy<Vec3>> threadedForceBuffers_;
125
126     //! reduction shift force buffers
127     std::vector<std::vector<Vec3>> threadedShiftForceBuffers_;
128
129     //! PBC objects
130     std::unique_ptr<PbcHolder> pbcHolder_;
131
132     //! compute listed forces and energies, overwrites the internal buffers
133     template<class ShiftForce>
134     void computeForcesAndEnergies(gmx::ArrayRef<const Vec3>                  x,
135                                   gmx::ArrayRef<Vec3>                        forces,
136                                   [[maybe_unused]] gmx::ArrayRef<ShiftForce> shiftForces,
137                                   bool                                       usePbc = false);
138 };
139
140 } // namespace nblib
141
142 #endif // NBLIB_LISTEDFORCES_CALCULATOR_H