7017862aa690dcb98a09608e82dc3b9d53df2101
[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 ForceBuffer;
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 to be used for the force calculation
94      * \param[out] forces buffer to store the output forces
95      */
96     void compute(gmx::ArrayRef<const Vec3> coordinates, gmx::ArrayRef<Vec3> forces, bool usePbc = false);
97
98     //! \brief Alternative overload with the energies in an output buffer
99     void compute(gmx::ArrayRef<const Vec3> coordinates,
100                  gmx::ArrayRef<Vec3>       forces,
101                  EnergyType&               energies,
102                  bool                      usePbc = false);
103
104     //! \brief default, but moved to separate compilation unit
105     ~ListedForceCalculator();
106
107 private:
108     int numThreads;
109
110     //! the main buffer to hold the final listed forces
111     std::vector<gmx::RVec> masterForceBuffer_;
112
113     //! holds the array of energies computed
114     EnergyType energyBuffer_;
115
116     //! holds the listed interactions split into groups for multithreading
117     std::vector<ListedInteractionData> threadedInteractions_;
118
119     //! reduction force buffers
120     std::vector<std::unique_ptr<ForceBuffer<gmx::RVec>>> threadedForceBuffers_;
121
122     //! PBC objects
123     std::unique_ptr<PbcHolder> pbcHolder_;
124
125     //! compute listed forces and energies, overwrites the internal buffers
126     void computeForcesAndEnergies(gmx::ArrayRef<const Vec3> x, bool usePbc = false);
127 };
128
129 } // namespace nblib
130
131 #endif // NBLIB_LISTEDFORCES_CALCULATOR_H