NBLIB listed forces API update
[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, 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
56 namespace gmx
57 {
58 template<typename T>
59 class ArrayRef;
60 } // namespace gmx
61
62 namespace nblib
63 {
64 class Box;
65 class PbcHolder;
66 template<class T>
67 class ForceBuffer;
68
69 /*! \internal \brief object to calculate listed forces
70  *
71  */
72 class ListedForceCalculator
73 {
74 public:
75     using EnergyType = std::array<real, std::tuple_size<ListedInteractionData>::value>;
76
77     ListedForceCalculator(const ListedInteractionData& interactions,
78                           size_t                       bufferSize,
79                           int                          numThreads,
80                           const Box&                   box);
81
82     /*! \brief Dispatch the listed force kernels and reduce the forces
83      *
84      * This function adds the computed listed forces to all values in the passed in forces buffer,
85      * so it can be regarded as an output only param. In case this is being used in a simulation
86      * that uses the same force buffer for both non-bonded and listed forces, this call should be
87      * made only after the compute() call from the non-bonded ForceCalculator
88      *
89      * This function also stores the forces and energies from listed interactions in the internal
90      * buffer of the ListedForceCalculator object
91      *
92      * \param[in] coordinates to be used for the force calculation
93      * \param[out] forces buffer to store the output forces
94      */
95     void compute(gmx::ArrayRef<const Vec3> coordinates, gmx::ArrayRef<Vec3> forces, bool usePbc = false);
96
97     //! Alternative overload with the energies in an output buffer
98     void compute(gmx::ArrayRef<const Vec3> coordinates,
99                  gmx::ArrayRef<Vec3>       forces,
100                  EnergyType&               energies,
101                  bool                      usePbc = false);
102
103     /*! \brief We need to declare the destructor here to move the (still default) implementation
104      *  to the .cpp file. Omitting this declaration would mean an inline destructor
105      *  which can't compile because the unique_ptr dtor needs ~ForceBuffer, which is not available
106      * here because it's incomplete.
107      */
108     ~ListedForceCalculator();
109
110 private:
111     int numThreads;
112
113     //! the main buffer to hold the final listed forces
114     std::vector<gmx::RVec> masterForceBuffer_;
115
116     //! holds the array of energies computed
117     EnergyType energyBuffer_;
118
119     //! holds the listed interactions split into groups for multithreading
120     std::vector<ListedInteractionData> threadedInteractions_;
121
122     //! reduction force buffers
123     std::vector<std::unique_ptr<ForceBuffer<gmx::RVec>>> threadedForceBuffers_;
124
125     //! PBC objects
126     std::unique_ptr<PbcHolder> pbcHolder_;
127
128     //! compute listed forces and energies, overwrites the internal buffers
129     void computeForcesAndEnergies(gmx::ArrayRef<const Vec3> x, bool usePbc = false);
130 };
131
132 } // namespace nblib
133
134 #endif // NBLIB_LISTEDFORCES_CALCULATOR_H