a3c4844e2e06487f65b9bd6e33fd2c1e9f59a565
[alexxy/gromacs.git] / src / gromacs / mdlib / update.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_MDLIB_UPDATE_H
39 #define GMX_MDLIB_UPDATE_H
40
41 #include <memory>
42
43 #include "gromacs/math/paddedvector.h"
44 #include "gromacs/math/vectypes.h"
45 #include "gromacs/mdtypes/md_enums.h"
46 #include "gromacs/timing/wallcycle.h"
47 #include "gromacs/utility/arrayref.h"
48 #include "gromacs/utility/real.h"
49
50 class ekinstate_t;
51 class gmx_ekindata_t;
52 struct gmx_enerdata_t;
53 enum class PbcType;
54 struct t_fcdata;
55 struct t_graph;
56 struct t_grpopts;
57 struct t_inputrec;
58 struct t_nrnb;
59 class t_state;
60 enum class ParticleType;
61
62 namespace gmx
63 {
64 class BoxDeformation;
65 class Constraints;
66
67
68 /*! \libinternal
69  * \brief Contains data for update phase */
70 class Update
71 {
72 public:
73     /*! \brief Constructor
74      *
75      * \param[in] inputRecord     Input record, used to construct SD object.
76      * \param[in] boxDeformation  Periodic box deformation object.
77      */
78     Update(const t_inputrec& inputRecord, BoxDeformation* boxDeformation);
79     //! Destructor
80     ~Update();
81     /*! \brief Get the pointer to updated coordinates
82      *
83      * Update saves the updated coordinates into separate buffer, so that constraints will have
84      * access to both updated and not update coordinates. For that, update owns a separate buffer.
85      * See finish_update(...) for details.
86      *
87      * \returns The pointer to the intermediate coordinates buffer.
88      */
89     PaddedVector<gmx::RVec>* xp();
90     /*!\brief Getter to local copy of box deformation class.
91      *
92      * \returns handle to box deformation class
93      */
94     BoxDeformation* deform() const;
95     /*! \brief Sets data that changes only at domain decomposition time.
96      *
97      * \param[in] numAtoms  Updated number of atoms.
98      * \param[in] cFREEZE   Group index for freezing
99      * \param[in] cTC       Group index for center of mass motion removal
100      */
101     void updateAfterPartition(int                                 numAtoms,
102                               gmx::ArrayRef<const unsigned short> cFREEZE,
103                               gmx::ArrayRef<const unsigned short> cTC);
104
105     /*! \brief Perform numerical integration step.
106      *
107      * Selects the appropriate integrator, based on the input record and performs a numerical integration step.
108      *
109      * \param[in]  inputRecord               Input record.
110      * \param[in]  step                      Current timestep.
111      * \param[in]  homenr                    The number of atoms on this processor.
112      * \param[in]  havePartiallyFrozenAtoms  Whether atoms are frozen along 1 or 2 (not 3) dimensions?
113      * \param[in]  ptype                     The list of particle types.
114      * \param[in]  invMass                   Inverse atomic mass per atom, 0 for vsites and shells.
115      * \param[in]  invMassPerDim             Inverse atomic mass per atom and dimension, 0 for vsites, shells and frozen dimensions
116      * \param[in]  state                     System state object.
117      * \param[in]  f                         Buffer with atomic forces for home particles.
118      * \param[in]  fcdata                    Force calculation data to update distance and orientation restraints.
119      * \param[in]  ekind                     Kinetic energy data (for temperature coupling, energy groups, etc.).
120      * \param[in]  M                         Parrinello-Rahman velocity scaling matrix.
121      * \param[in]  updatePart                What should be updated, coordinates or velocities. This enum only used in VV integrator.
122      * \param[in]  cr                        Comunication record  (Old comment: these shouldn't be here -- need to think about it).
123      * \param[in]  haveConstraints           If the system has constraints.
124      */
125     void update_coords(const t_inputrec&                                inputRecord,
126                        int64_t                                          step,
127                        int                                              homenr,
128                        bool                                             havePartiallyFrozenAtoms,
129                        gmx::ArrayRef<const ParticleType>                ptype,
130                        gmx::ArrayRef<const real>                        invMass,
131                        gmx::ArrayRef<const rvec>                        invMassPerDim,
132                        t_state*                                         state,
133                        const gmx::ArrayRefWithPadding<const gmx::RVec>& f,
134                        t_fcdata*                                        fcdata,
135                        const gmx_ekindata_t*                            ekind,
136                        const matrix                                     M,
137                        int                                              updatePart,
138                        const t_commrec*                                 cr,
139                        bool                                             haveConstraints);
140
141     /*! \brief Finalize the coordinate update.
142      *
143      * Copy the updated coordinates to the main coordinates buffer for the atoms that are not frozen.
144      *
145      * \param[in]  inputRecord      Input record.
146      * \param[in]  havePartiallyFrozenAtoms  Whether atoms are frozen along 1 or 2 (not 3) dimensions?
147      * \param[in]  homenr                    The number of atoms on this processor.
148      * \param[in]  state            System state object.
149      * \param[in]  wcycle           Wall-clock cycle counter.
150      * \param[in]  haveConstraints  If the system has constraints.
151      */
152     void finish_update(const t_inputrec& inputRecord,
153                        bool              havePartiallyFrozenAtoms,
154                        int               homenr,
155                        t_state*          state,
156                        gmx_wallcycle*    wcycle,
157                        bool              haveConstraints);
158
159     /*! \brief Secong part of the SD integrator.
160      *
161      * The first part of integration is performed in the update_coords(...) method.
162      *
163      * \param[in]  inputRecord  Input record.
164      * \param[in]  step         Current timestep.
165      * \param[in]  dvdlambda    Free energy derivative. Contribution to be added to
166      *                          the bonded interactions.
167      * \param[in]  homenr       The number of atoms on this processor.
168      * \param[in]  ptype        The list of particle types.
169      * \param[in]  invMass      Inverse atomic mass per atom, 0 for vsites and shells.
170      * \param[in]  state        System state object.
171      * \param[in]  cr           Comunication record.
172      * \param[in]  nrnb         Cycle counters.
173      * \param[in]  wcycle       Wall-clock cycle counter.
174      * \param[in]  constr       Constraints object. The constraints are applied
175      *                          on coordinates after update.
176      * \param[in]  do_log       If this is logging step.
177      * \param[in]  do_ene       If this is an energy evaluation step.
178      */
179     void update_sd_second_half(const t_inputrec&                 inputRecord,
180                                int64_t                           step,
181                                real*                             dvdlambda,
182                                int                               homenr,
183                                gmx::ArrayRef<const ParticleType> ptype,
184                                gmx::ArrayRef<const real>         invMass,
185                                t_state*                          state,
186                                const t_commrec*                  cr,
187                                t_nrnb*                           nrnb,
188                                gmx_wallcycle*                    wcycle,
189                                gmx::Constraints*                 constr,
190                                bool                              do_log,
191                                bool                              do_ene);
192
193     /*! \brief Performs a leap-frog update without updating \p state so the constrain virial
194      * can be computed.
195      */
196     void update_for_constraint_virial(const t_inputrec&         inputRecord,
197                                       int                       homenr,
198                                       bool                      havePartiallyFrozenAtoms,
199                                       gmx::ArrayRef<const real> invmass,
200                                       gmx::ArrayRef<const rvec> invMassPerDim,
201                                       const t_state&            state,
202                                       const gmx::ArrayRefWithPadding<const gmx::RVec>& f,
203                                       const gmx_ekindata_t&                            ekind);
204
205     /*! \brief Update pre-computed constants that depend on the reference temperature for coupling.
206      *
207      * This could change e.g. in simulated annealing.
208      *
209      * \param[in]  inputRecord  Input record.
210      */
211     void update_temperature_constants(const t_inputrec& inputRecord);
212
213     /*!\brief Getter for the list of the randomize groups.
214      *
215      *  Needed for Andersen temperature control.
216      *
217      * \returns Reference to the groups from the SD data object.
218      */
219     const std::vector<bool>& getAndersenRandomizeGroup() const;
220     /*!\brief Getter for the list of the Boltzmann factors.
221      *
222      *  Needed for Andersen temperature control.
223      *
224      * \returns Reference to the Boltzmann factors from the SD data object.
225      */
226     const std::vector<real>& getBoltzmanFactor() const;
227
228 private:
229     //! Implementation type.
230     class Impl;
231     //! Implementation object.
232     std::unique_ptr<Impl> impl_;
233 };
234
235 }; // namespace gmx
236
237 /*
238  * Compute the partial kinetic energy for home particles;
239  * will be accumulated in the calling routine.
240  * The tensor is
241  *
242  * Ekin = SUM(i) 0.5 m[i] v[i] (x) v[i]
243  *
244  *     use v[i] = v[i] - u[i] when calculating temperature
245  *
246  * u must be accumulated already.
247  *
248  * Now also computes the contribution of the kinetic energy to the
249  * free energy
250  *
251  */
252
253
254 void init_ekinstate(ekinstate_t* ekinstate, const t_inputrec* ir);
255
256 void update_ekinstate(ekinstate_t* ekinstate, const gmx_ekindata_t* ekind);
257
258 /*! \brief Restores data from \p ekinstate to \p ekind, then broadcasts it
259    to the rest of the simulation */
260 void restore_ekinstate_from_state(const t_commrec* cr, gmx_ekindata_t* ekind, const ekinstate_t* ekinstate);
261
262 /*! \brief Computes the atom range for a thread to operate on, ensuring SIMD aligned ranges
263  *
264  * \param[in]  numThreads   The number of threads to divide atoms over
265  * \param[in]  threadIndex  The thread to get the range for
266  * \param[in]  numAtoms     The total number of atoms (on this rank)
267  * \param[out] startAtom    The start of the atom range
268  * \param[out] endAtom      The end of the atom range, note that this is in general not a multiple of the SIMD width
269  */
270 void getThreadAtomRange(int numThreads, int threadIndex, int numAtoms, int* startAtom, int* endAtom);
271
272 #endif