Remove use of graph in do_force()
[alexxy/gromacs.git] / src / gromacs / mdlib / vsite.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 The GROMACS development team.
7  * Copyright (c) 2018,2019,2020, 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_MDLIB_VSITE_H
39 #define GMX_MDLIB_VSITE_H
40
41 #include <memory>
42
43 #include "gromacs/math/vectypes.h"
44 #include "gromacs/pbcutil/ishift.h"
45 #include "gromacs/topology/idef.h"
46 #include "gromacs/utility/arrayref.h"
47 #include "gromacs/utility/basedefinitions.h"
48 #include "gromacs/utility/real.h"
49
50 struct gmx_localtop_t;
51 struct gmx_mtop_t;
52 struct t_commrec;
53 struct InteractionList;
54 struct t_mdatoms;
55 struct t_nrnb;
56 struct gmx_wallcycle;
57 struct VsiteThread;
58 enum class PbcType : int;
59
60 namespace gmx
61 {
62 class RangePartitioning;
63 }
64
65 /* The start and end values of for the vsite indices in the ftype enum.
66  * The validity of these values is checked in init_vsite.
67  * This is used to avoid loops over all ftypes just to get the vsite entries.
68  * (We should replace the fixed ilist array by only the used entries.)
69  */
70 static constexpr int c_ftypeVsiteStart = F_VSITE2;
71 static constexpr int c_ftypeVsiteEnd   = F_VSITEN + 1;
72
73 /* Type for storing PBC atom information for all vsite types in the system */
74 typedef std::array<std::vector<int>, c_ftypeVsiteEnd - c_ftypeVsiteStart> VsitePbc;
75
76 /* Data for handling vsites, needed with OpenMP threading or with charge-groups and PBC */
77 struct gmx_vsite_t
78 {
79     gmx_vsite_t();
80
81     ~gmx_vsite_t();
82
83     /* The number of vsites that cross update groups, when =0 no PBC treatment is needed */
84     int numInterUpdategroupVsites;
85     int nthreads;                                    /* Number of threads used for vsites       */
86     std::vector<std::unique_ptr<VsiteThread>> tData; /* Thread local vsites and work structs    */
87     std::vector<int> taskIndex;                      /* Work array                              */
88     bool useDomdec; /* Tells whether we use domain decomposition with more than 1 DD rank */
89 };
90
91 /*! \brief Create positions of vsite atoms based for the local system
92  *
93  * \param[in]     vsite    The vsite struct, when nullptr is passed, no MPI and no multi-threading
94  *                         is used
95  * \param[in,out] x        The coordinates
96  * \param[in]     dt       The time step
97  * \param[in,out] v        When != nullptr, velocities for vsites are set as displacement/dt
98  * \param[in]     ip       Interaction parameters
99  * \param[in]     ilist    The interaction list
100  * \param[in]     pbcType  The type of periodic boundary conditions
101  * \param[in]     bMolPBC  When true, molecules are broken over PBC
102  * \param[in]     cr       The communication record
103  * \param[in]     box      The box
104  */
105 void construct_vsites(const gmx_vsite_t*                   vsite,
106                       rvec                                 x[],
107                       real                                 dt,
108                       rvec                                 v[],
109                       gmx::ArrayRef<const t_iparams>       ip,
110                       gmx::ArrayRef<const InteractionList> ilist,
111                       PbcType                              pbcType,
112                       gmx_bool                             bMolPBC,
113                       const t_commrec*                     cr,
114                       const matrix                         box);
115
116 /*! \brief Create positions of vsite atoms for the whole system assuming all molecules are wholex
117  *
118  * \param[in]     mtop  The global topology
119  * \param[in,out] x     The global coordinates
120  */
121 void constructVsitesGlobal(const gmx_mtop_t& mtop, gmx::ArrayRef<gmx::RVec> x);
122
123 void spread_vsite_f(const gmx_vsite_t*            vsite,
124                     const rvec                    x[],
125                     rvec                          f[],
126                     rvec*                         fshift,
127                     gmx_bool                      VirCorr,
128                     matrix                        vir,
129                     t_nrnb*                       nrnb,
130                     const InteractionDefinitions& idef,
131                     PbcType                       pbcType,
132                     gmx_bool                      bMolPBC,
133                     const matrix                  box,
134                     const t_commrec*              cr,
135                     gmx_wallcycle*                wcycle);
136 /* Spread the force operating on the vsite atoms on the surrounding atoms.
137  * If fshift!=NULL also update the shift forces.
138  * If VirCorr=TRUE add the virial correction for non-linear vsite constructs
139  * to vir. This correction is required when the virial is not calculated
140  * afterwards from the particle position and forces, but in a different way,
141  * as for instance for the PME mesh contribution.
142  */
143
144 /* Return the number of non-linear virtual site constructions in the system */
145 int countNonlinearVsites(const gmx_mtop_t& mtop);
146
147 /* Return the number of virtual sites that cross update groups
148  *
149  * \param[in] mtop                           The global topology
150  * \param[in] updateGroupingPerMoleculetype  Update grouping per molecule type, pass empty when not using update groups
151  */
152 int countInterUpdategroupVsites(const gmx_mtop_t&                           mtop,
153                                 gmx::ArrayRef<const gmx::RangePartitioning> updateGroupingPerMoleculetype);
154
155 /* Initialize the virtual site struct,
156  *
157  * \param[in] mtop  The global topology
158  * \param[in] cr    The communication record
159  * \returns A valid vsite struct or nullptr when there are no virtual sites
160  */
161 std::unique_ptr<gmx_vsite_t> initVsite(const gmx_mtop_t& mtop, const t_commrec* cr);
162
163 void split_vsites_over_threads(gmx::ArrayRef<const InteractionList> ilist,
164                                gmx::ArrayRef<const t_iparams>       ip,
165                                const t_mdatoms*                     mdatoms,
166                                gmx_vsite_t*                         vsite);
167 /* Divide the vsite work-load over the threads.
168  * Should be called at the end of the domain decomposition.
169  */
170
171 void set_vsite_top(gmx_vsite_t* vsite, const gmx_localtop_t* top, const t_mdatoms* md);
172 /* Set some vsite data for runs without domain decomposition.
173  * Should be called once after init_vsite, before calling other routines.
174  */
175
176 #endif