Rename GpuBonded into ListedForcesGpu
[alexxy/gromacs.git] / src / gromacs / mdtypes / forcerec.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
7  * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #ifndef GMX_MDTYPES_TYPES_FORCEREC_H
39 #define GMX_MDTYPES_TYPES_FORCEREC_H
40
41 #include <array>
42 #include <memory>
43 #include <vector>
44
45 #include "gromacs/math/vectypes.h"
46 #include "gromacs/mdtypes/md_enums.h"
47 #include "gromacs/pbcutil/ishift.h"
48 #include "gromacs/pbcutil/pbc.h"
49 #include "gromacs/utility/arrayref.h"
50 #include "gromacs/utility/real.h"
51
52 #include "locality.h"
53
54 /* Abstract type for PME that is defined only in the routine that use them. */
55 struct gmx_pme_t;
56 struct nonbonded_verlet_t;
57 struct bonded_threading_t;
58 class DeviceContext;
59 class DispersionCorrection;
60 class ListedForces;
61 struct t_fcdata;
62 struct t_forcetable;
63 struct interaction_const_t;
64
65 namespace gmx
66 {
67 class DeviceStreamManager;
68 class ListedForcesGpu;
69 class GpuForceReduction;
70 class ForceProviders;
71 class StatePropagatorDataGpu;
72 class PmePpCommGpu;
73 class WholeMoleculeTransform;
74 } // namespace gmx
75
76 /* macros for the cginfo data in forcerec
77  *
78  * Since the tpx format support max 256 energy groups, we do the same here.
79  * Note that we thus have bits 8-14 still unused.
80  *
81  * The maximum cg size in cginfo is 63
82  * because we only have space for 6 bits in cginfo,
83  * this cg size entry is actually only read with domain decomposition.
84  */
85 #define SET_CGINFO_GID(cgi, gid) (cgi) = (((cgi) & ~255) | (gid))
86 #define GET_CGINFO_GID(cgi) ((cgi)&255)
87 #define SET_CGINFO_FEP(cgi) (cgi) = ((cgi) | (1 << 15))
88 #define GET_CGINFO_FEP(cgi) ((cgi) & (1 << 15))
89 #define SET_CGINFO_EXCL_INTER(cgi) (cgi) = ((cgi) | (1 << 17))
90 #define GET_CGINFO_EXCL_INTER(cgi) ((cgi) & (1 << 17))
91 #define SET_CGINFO_CONSTR(cgi) (cgi) = ((cgi) | (1 << 20))
92 #define GET_CGINFO_CONSTR(cgi) ((cgi) & (1 << 20))
93 #define SET_CGINFO_SETTLE(cgi) (cgi) = ((cgi) | (1 << 21))
94 #define GET_CGINFO_SETTLE(cgi) ((cgi) & (1 << 21))
95 /* This bit is only used with bBondComm in the domain decomposition */
96 #define SET_CGINFO_BOND_INTER(cgi) (cgi) = ((cgi) | (1 << 22))
97 #define GET_CGINFO_BOND_INTER(cgi) ((cgi) & (1 << 22))
98 #define SET_CGINFO_HAS_VDW(cgi) (cgi) = ((cgi) | (1 << 23))
99 #define GET_CGINFO_HAS_VDW(cgi) ((cgi) & (1 << 23))
100 #define SET_CGINFO_HAS_Q(cgi) (cgi) = ((cgi) | (1 << 24))
101 #define GET_CGINFO_HAS_Q(cgi) ((cgi) & (1 << 24))
102
103
104 /* Value to be used in mdrun for an infinite cut-off.
105  * Since we need to compare with the cut-off squared,
106  * this value should be slighlty smaller than sqrt(GMX_FLOAT_MAX).
107  */
108 #define GMX_CUTOFF_INF 1E+18
109 //! Check the cuttoff
110 real cutoff_inf(real cutoff);
111
112 struct cginfo_mb_t
113 {
114     int              cg_start = 0;
115     int              cg_end   = 0;
116     int              cg_mod   = 0;
117     std::vector<int> cginfo;
118 };
119
120
121 /* Forward declaration of type for managing Ewald tables */
122 struct gmx_ewald_tab_t;
123
124 struct ewald_corr_thread_t;
125
126 /*! \brief Helper force buffers for ForceOutputs
127  *
128  * This class stores intermediate force buffers that are used
129  * internally in the force calculation and which are reduced into
130  * the output force buffer passed to the force calculation.
131  */
132 class ForceHelperBuffers
133 {
134 public:
135     /*! \brief Constructs helper buffers
136      *
137      * When the forces that will be accumulated with help of these buffers
138      * have direct virial contributions, set the parameter to true, so
139      * an extra force buffer is available for these forces to enable
140      * correct virial computation.
141      */
142     ForceHelperBuffers(bool haveDirectVirialContributions);
143
144     //! Returns whether we have a direct virial contribution force buffer
145     bool haveDirectVirialContributions() const { return haveDirectVirialContributions_; }
146
147     //! Returns the buffer for direct virial contributions
148     gmx::ArrayRef<gmx::RVec> forceBufferForDirectVirialContributions()
149     {
150         GMX_ASSERT(haveDirectVirialContributions_, "Buffer can only be requested when present");
151         return forceBufferForDirectVirialContributions_;
152     }
153
154     //! Returns the buffer for shift forces, size c_numShiftVectors
155     gmx::ArrayRef<gmx::RVec> shiftForces() { return shiftForces_; }
156
157     //! Resizes the direct virial contribution buffer, when present
158     void resize(int numAtoms);
159
160 private:
161     //! True when we have contributions that are directly added to the virial
162     bool haveDirectVirialContributions_ = false;
163     //! Force buffer for force computation with direct virial contributions
164     std::vector<gmx::RVec> forceBufferForDirectVirialContributions_;
165     //! Shift force array for computing the virial, size c_numShiftVectors
166     std::vector<gmx::RVec> shiftForces_;
167 };
168 // NOLINTNEXTLINE (clang-analyzer-optin.performance.Padding)
169 struct t_forcerec
170 {
171     // Declare an explicit constructor and destructor, so they can be
172     // implemented in a single source file, so that not every source
173     // file that includes this one needs to understand how to find the
174     // destructors of the objects pointed to by unique_ptr members.
175     t_forcerec();
176     ~t_forcerec();
177
178     std::unique_ptr<interaction_const_t> ic;
179
180     /* PBC stuff */
181     PbcType pbcType = PbcType::Xyz;
182     //! Tells whether atoms inside a molecule can be in different periodic images,
183     //  i.e. whether we need to take into account PBC when computing distances inside molecules.
184     //  This determines whether PBC must be considered for e.g. bonded interactions.
185     bool            bMolPBC     = false;
186     RefCoordScaling rc_scaling  = RefCoordScaling::No;
187     gmx::RVec       posres_com  = { 0, 0, 0 };
188     gmx::RVec       posres_comB = { 0, 0, 0 };
189
190     bool use_simd_kernels = false;
191
192     /* Interaction for calculated in kernels. In many cases this is similar to
193      * the electrostatics settings in the inputrecord, but the difference is that
194      * these variables always specify the actual interaction in the kernel - if
195      * we are tabulating reaction-field the inputrec will say reaction-field, but
196      * the kernel interaction will say cubic-spline-table. To be safe we also
197      * have a kernel-specific setting for the modifiers - if the interaction is
198      * tabulated we already included the inputrec modification there, so the kernel
199      * modification setting will say 'none' in that case.
200      */
201     NbkernelElecType     nbkernel_elec_interaction = NbkernelElecType::None;
202     NbkernelVdwType      nbkernel_vdw_interaction  = NbkernelVdwType::None;
203     InteractionModifiers nbkernel_elec_modifier    = InteractionModifiers::None;
204     InteractionModifiers nbkernel_vdw_modifier     = InteractionModifiers::None;
205
206     /* Cut-Off stuff.
207      * Infinite cut-off's will be GMX_CUTOFF_INF (unlike in t_inputrec: 0).
208      */
209     real rlist = 0;
210
211     /* Charge sum for topology A/B ([0]/[1]) for Ewald corrections */
212     std::array<double, 2> qsum  = { 0 };
213     std::array<double, 2> q2sum = { 0 };
214     std::array<double, 2> c6sum = { 0 };
215
216     /* Dispersion correction stuff */
217     std::unique_ptr<DispersionCorrection> dispersionCorrection;
218
219     /* Fudge factors */
220     real fudgeQQ = 0;
221
222     std::unique_ptr<t_forcetable> pairsTable; /* for 1-4 interactions, [pairs] and [pairs_nb] */
223
224     /* Free energy */
225     FreeEnergyPerturbationType efep = FreeEnergyPerturbationType::No;
226
227     /* Information about atom properties for the molecule blocks in the system */
228     std::vector<cginfo_mb_t> cginfo_mb;
229     /* Information about atom properties for local and non-local atoms */
230     std::vector<int> cginfo;
231
232     std::vector<gmx::RVec> shift_vec;
233
234     std::unique_ptr<gmx::WholeMoleculeTransform> wholeMoleculeTransform;
235
236     /* The Nbnxm Verlet non-bonded machinery */
237     std::unique_ptr<nonbonded_verlet_t> nbv;
238
239     /* The wall tables (if used) */
240     int                                                     nwall = 0;
241     std::vector<std::vector<std::unique_ptr<t_forcetable>>> wall_tab;
242
243     /* The number of atoms participating in do_force_lowlevel */
244     int natoms_force = 0;
245     /* The number of atoms participating in force calculation and constraints */
246     int natoms_force_constr = 0;
247
248     /* List of helper buffers for ForceOutputs, one for each time step with MTS */
249     std::vector<ForceHelperBuffers> forceHelperBuffers;
250
251     /* Data for PPPM/PME/Ewald */
252     gmx_pme_t*   pmedata                = nullptr;
253     LongRangeVdW ljpme_combination_rule = LongRangeVdW::Geom;
254
255     /* PME/Ewald stuff */
256     std::unique_ptr<gmx_ewald_tab_t> ewald_table;
257
258     /* Non bonded Parameter lists */
259     int               ntype          = 0; /* Number of atom types */
260     bool              haveBuckingham = false;
261     std::vector<real> nbfp;
262     std::vector<real> ljpme_c6grid; /* C6-values used on grid in LJPME */
263
264     /* Energy group pair flags */
265     int* egp_flags = nullptr;
266
267     /* Shell molecular dynamics flexible constraints */
268     real fc_stepsize = 0;
269
270     /* If > 0 signals Test Particle Insertion,
271      * the value is the number of atoms of the molecule to insert
272      * Only the energy difference due to the addition of the last molecule
273      * should be calculated.
274      */
275     int n_tpi = 0;
276
277     /* Limit for printing large forces, negative is don't print */
278     real print_force = 0;
279
280     /* User determined parameters, copied from the inputrec */
281     int  userint1  = 0;
282     int  userint2  = 0;
283     int  userint3  = 0;
284     int  userint4  = 0;
285     real userreal1 = 0;
286     real userreal2 = 0;
287     real userreal3 = 0;
288     real userreal4 = 0;
289
290     /* Tells whether we use multiple time stepping, computing some forces less frequently */
291     bool useMts = false;
292
293     /* Data for special listed force calculations */
294     std::unique_ptr<t_fcdata> fcdata;
295
296     // The listed forces calculation data, 1 entry or multiple entries with multiple time stepping
297     std::vector<ListedForces> listedForces;
298
299     /* TODO: Replace the pointer by an object once we got rid of C */
300     gmx::ListedForcesGpu* listedForcesGpu = nullptr;
301
302     /* Ewald correction thread local virial and energy data */
303     int                              nthread_ewc = 0;
304     std::vector<ewald_corr_thread_t> ewc_t;
305
306     gmx::ForceProviders* forceProviders = nullptr;
307
308     // The stateGpu object is created in runner, forcerec just keeps the copy of the pointer.
309     // TODO: This is not supposed to be here. StatePropagatorDataGpu should be a part of
310     //       general StatePropagatorData object that is passed around
311     gmx::StatePropagatorDataGpu* stateGpu = nullptr;
312     // TODO: Should not be here. This is here only to pass the pointer around.
313     gmx::DeviceStreamManager* deviceStreamManager = nullptr;
314
315     //! GPU device context
316     DeviceContext* deviceContext = nullptr;
317
318     /* For PME-PP GPU communication */
319     std::unique_ptr<gmx::PmePpCommGpu> pmePpCommGpu;
320
321     /* For GPU force reduction (on both local and non-local atoms) */
322     gmx::EnumerationArray<gmx::AtomLocality, std::unique_ptr<gmx::GpuForceReduction>> gpuForceReduction;
323 };
324
325 /* Important: Starting with Gromacs-4.6, the values of c6 and c12 in the nbfp array have
326  * been scaled by 6.0 or 12.0 to save flops in the kernels. We have corrected this everywhere
327  * in the code, but beware if you are using these macros externally.
328  */
329 #define C6(nbfp, ntp, ai, aj) (nbfp)[2 * ((ntp) * (ai) + (aj))]
330 #define C12(nbfp, ntp, ai, aj) (nbfp)[2 * ((ntp) * (ai) + (aj)) + 1]
331 #define BHAMC(nbfp, ntp, ai, aj) (nbfp)[3 * ((ntp) * (ai) + (aj))]
332 #define BHAMA(nbfp, ntp, ai, aj) (nbfp)[3 * ((ntp) * (ai) + (aj)) + 1]
333 #define BHAMB(nbfp, ntp, ai, aj) (nbfp)[3 * ((ntp) * (ai) + (aj)) + 2]
334
335 #endif