Split lines with many copyright years
[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, 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 "gromacs/math/paddedvector.h"
42 #include "gromacs/math/vectypes.h"
43 #include "gromacs/mdtypes/md_enums.h"
44 #include "gromacs/timing/wallcycle.h"
45 #include "gromacs/utility/arrayref.h"
46 #include "gromacs/utility/basedefinitions.h"
47 #include "gromacs/utility/classhelpers.h"
48 #include "gromacs/utility/real.h"
49
50 class ekinstate_t;
51 struct gmx_ekindata_t;
52 struct gmx_enerdata_t;
53 struct t_extmass;
54 struct t_fcdata;
55 struct t_graph;
56 struct t_grpopts;
57 struct t_inputrec;
58 struct t_mdatoms;
59 struct t_nrnb;
60 class t_state;
61
62 /* Abstract type for update */
63 struct gmx_stochd_t;
64
65 namespace gmx
66 {
67 class BoxDeformation;
68 class Constraints;
69
70
71 /*! \libinternal
72  * \brief Contains data for update phase */
73 class Update
74 {
75 public:
76     //! Constructor
77     Update(const t_inputrec* ir, BoxDeformation* boxDeformation);
78     ~Update();
79     // TODO Get rid of getters when more free functions are incorporated as member methods
80     //! Returns handle to stochd_t struct
81     gmx_stochd_t* sd() const;
82     //! Returns pointer to PaddedVector xp
83     PaddedVector<gmx::RVec>* xp();
84     //! Returns handle to box deformation class
85     BoxDeformation* deform() const;
86     //! Resizes xp
87     void setNumAtoms(int nAtoms);
88
89 private:
90     //! Implementation type.
91     class Impl;
92     //! Implementation object.
93     PrivateImplPointer<Impl> impl_;
94 };
95
96 }; // namespace gmx
97
98 /* Update pre-computed constants that depend on the reference
99  * temperature for coupling.
100  *
101  * This could change e.g. in simulated annealing. */
102 void update_temperature_constants(gmx_stochd_t* sd, const t_inputrec* ir);
103
104 /* Update the size of per-atom arrays (e.g. after DD re-partitioning,
105    which might increase the number of home atoms). */
106
107 void update_tcouple(int64_t           step,
108                     const t_inputrec* inputrec,
109                     t_state*          state,
110                     gmx_ekindata_t*   ekind,
111                     const t_extmass*  MassQ,
112                     const t_mdatoms*  md);
113
114 /* Update Parrinello-Rahman, to be called before the coordinate update */
115 void update_pcouple_before_coordinates(FILE*             fplog,
116                                        int64_t           step,
117                                        const t_inputrec* inputrec,
118                                        t_state*          state,
119                                        matrix            parrinellorahmanMu,
120                                        matrix            M,
121                                        gmx_bool          bInitStep);
122
123 /* Update the box, to be called after the coordinate update.
124  * For Berendsen P-coupling, also calculates the scaling factor
125  * and scales the coordinates.
126  * When the deform option is used, scales coordinates and box here.
127  */
128 void update_pcouple_after_coordinates(FILE*             fplog,
129                                       int64_t           step,
130                                       const t_inputrec* inputrec,
131                                       const t_mdatoms*  md,
132                                       const matrix      pressure,
133                                       const matrix      forceVirial,
134                                       const matrix      constraintVirial,
135                                       matrix            pressureCouplingMu,
136                                       t_state*          state,
137                                       t_nrnb*           nrnb,
138                                       gmx::Update*      upd,
139                                       bool              scaleCoordinates);
140
141 void update_coords(int64_t           step,
142                    const t_inputrec* inputrec, /* input record and box stuff    */
143                    const t_mdatoms*  md,
144                    t_state*          state,
145                    gmx::ArrayRefWithPadding<const gmx::RVec> f, /* forces on home particles */
146                    const t_fcdata*                           fcd,
147                    const gmx_ekindata_t*                     ekind,
148                    const matrix                              M,
149                    gmx::Update*                              upd,
150                    int                                       bUpdatePart,
151                    const t_commrec* cr, /* these shouldn't be here -- need to think about it */
152                    const gmx::Constraints* constr);
153
154 /* Return TRUE if OK, FALSE in case of Shake Error */
155
156 extern gmx_bool update_randomize_velocities(const t_inputrec*        ir,
157                                             int64_t                  step,
158                                             const t_commrec*         cr,
159                                             const t_mdatoms*         md,
160                                             gmx::ArrayRef<gmx::RVec> v,
161                                             const gmx::Update*       upd,
162                                             const gmx::Constraints*  constr);
163
164 void constrain_velocities(int64_t step,
165                           real* dvdlambda, /* the contribution to be added to the bonded interactions */
166                           t_state*          state,
167                           tensor            vir_part,
168                           gmx::Constraints* constr,
169                           gmx_bool          bCalcVir,
170                           bool              do_log,
171                           bool              do_ene);
172
173 void constrain_coordinates(int64_t step,
174                            real* dvdlambda, /* the contribution to be added to the bonded interactions */
175                            t_state*          state,
176                            tensor            vir_part,
177                            gmx::Update*      upd,
178                            gmx::Constraints* constr,
179                            gmx_bool          bCalcVir,
180                            bool              do_log,
181                            bool              do_ene);
182
183 void update_sd_second_half(int64_t step,
184                            real* dvdlambda, /* the contribution to be added to the bonded interactions */
185                            const t_inputrec* inputrec, /* input record and box stuff */
186                            const t_mdatoms*  md,
187                            t_state*          state,
188                            const t_commrec*  cr,
189                            t_nrnb*           nrnb,
190                            gmx_wallcycle_t   wcycle,
191                            gmx::Update*      upd,
192                            gmx::Constraints* constr,
193                            bool              do_log,
194                            bool              do_ene);
195
196 void finish_update(const t_inputrec*       inputrec,
197                    const t_mdatoms*        md,
198                    t_state*                state,
199                    const t_graph*          graph,
200                    t_nrnb*                 nrnb,
201                    gmx_wallcycle_t         wcycle,
202                    gmx::Update*            upd,
203                    const gmx::Constraints* constr);
204
205 /* Return TRUE if OK, FALSE in case of Shake Error */
206
207 void calc_ke_part(const rvec*      x,
208                   const rvec*      v,
209                   const matrix     box,
210                   const t_grpopts* opts,
211                   const t_mdatoms* md,
212                   gmx_ekindata_t*  ekind,
213                   t_nrnb*          nrnb,
214                   gmx_bool         bEkinAveVel);
215 /*
216  * Compute the partial kinetic energy for home particles;
217  * will be accumulated in the calling routine.
218  * The tensor is
219  *
220  * Ekin = SUM(i) 0.5 m[i] v[i] (x) v[i]
221  *
222  *     use v[i] = v[i] - u[i] when calculating temperature
223  *
224  * u must be accumulated already.
225  *
226  * Now also computes the contribution of the kinetic energy to the
227  * free energy
228  *
229  */
230
231
232 void init_ekinstate(ekinstate_t* ekinstate, const t_inputrec* ir);
233
234 void update_ekinstate(ekinstate_t* ekinstate, const gmx_ekindata_t* ekind);
235
236 /*! \brief Restores data from \p ekinstate to \p ekind, then broadcasts it
237    to the rest of the simulation */
238 void restore_ekinstate_from_state(const t_commrec* cr, gmx_ekindata_t* ekind, const ekinstate_t* ekinstate);
239
240 void berendsen_tcoupl(const t_inputrec*    ir,
241                       gmx_ekindata_t*      ekind,
242                       real                 dt,
243                       std::vector<double>& therm_integral); //NOLINT(google-runtime-references)
244
245 void andersen_tcoupl(const t_inputrec*         ir,
246                      int64_t                   step,
247                      const t_commrec*          cr,
248                      const t_mdatoms*          md,
249                      gmx::ArrayRef<gmx::RVec>  v,
250                      real                      rate,
251                      const std::vector<bool>&  randomize,
252                      gmx::ArrayRef<const real> boltzfac);
253
254 void nosehoover_tcoupl(const t_grpopts*      opts,
255                        const gmx_ekindata_t* ekind,
256                        real                  dt,
257                        double                xi[],
258                        double                vxi[],
259                        const t_extmass*      MassQ);
260
261 void trotter_update(const t_inputrec*               ir,
262                     int64_t                         step,
263                     gmx_ekindata_t*                 ekind,
264                     const gmx_enerdata_t*           enerd,
265                     t_state*                        state,
266                     const tensor                    vir,
267                     const t_mdatoms*                md,
268                     const t_extmass*                MassQ,
269                     gmx::ArrayRef<std::vector<int>> trotter_seqlist,
270                     int                             trotter_seqno);
271
272 std::array<std::vector<int>, ettTSEQMAX>
273 init_npt_vars(const t_inputrec* ir, t_state* state, t_extmass* Mass, gmx_bool bTrotter);
274
275 real NPT_energy(const t_inputrec* ir, const t_state* state, const t_extmass* MassQ);
276 /* computes all the pressure/tempertature control energy terms to get a conserved energy */
277
278 void vrescale_tcoupl(const t_inputrec* ir, int64_t step, gmx_ekindata_t* ekind, real dt, double therm_integral[]);
279 /* Compute temperature scaling. For V-rescale it is done in update. */
280
281 void rescale_velocities(const gmx_ekindata_t* ekind, const t_mdatoms* mdatoms, int start, int end, rvec v[]);
282 /* Rescale the velocities with the scaling factor in ekind */
283
284 //! Check whether we do simulated annealing.
285 bool doSimulatedAnnealing(const t_inputrec* ir);
286
287 //! Initialize simulated annealing.
288 bool initSimulatedAnnealing(t_inputrec* ir, gmx::Update* upd);
289
290 // TODO: This is the only function in update.h altering the inputrec
291 void update_annealing_target_temp(t_inputrec* ir, real t, gmx::Update* upd);
292 /* Set reference temp for simulated annealing at time t*/
293
294 real calc_temp(real ekin, real nrdf);
295 /* Calculate the temperature */
296
297 real calc_pres(int ePBC, int nwall, const matrix box, const tensor ekin, const tensor vir, tensor pres);
298 /* Calculate the pressure tensor, returns the scalar pressure.
299  * The unit of pressure is bar.
300  */
301
302 void parrinellorahman_pcoupl(FILE*             fplog,
303                              int64_t           step,
304                              const t_inputrec* ir,
305                              real              dt,
306                              const tensor      pres,
307                              const tensor      box,
308                              tensor            box_rel,
309                              tensor            boxv,
310                              tensor            M,
311                              matrix            mu,
312                              gmx_bool          bFirstStep);
313
314 void berendsen_pcoupl(FILE*             fplog,
315                       int64_t           step,
316                       const t_inputrec* ir,
317                       real              dt,
318                       const tensor      pres,
319                       const matrix      box,
320                       const matrix      force_vir,
321                       const matrix      constraint_vir,
322                       matrix            mu,
323                       double*           baros_integral);
324
325 void berendsen_pscale(const t_inputrec*    ir,
326                       const matrix         mu,
327                       matrix               box,
328                       matrix               box_rel,
329                       int                  start,
330                       int                  nr_atoms,
331                       rvec                 x[],
332                       const unsigned short cFREEZE[],
333                       t_nrnb*              nrnb,
334                       bool                 scaleCoordinates);
335
336 void pleaseCiteCouplingAlgorithms(FILE* fplog, const t_inputrec& ir);
337
338 /*! \brief Computes the atom range for a thread to operate on, ensuring SIMD aligned ranges
339  *
340  * \param[in]  numThreads   The number of threads to divide atoms over
341  * \param[in]  threadIndex  The thread to get the range for
342  * \param[in]  numAtoms     The total number of atoms (on this rank)
343  * \param[out] startAtom    The start of the atom range
344  * \param[out] endAtom      The end of the atom range, note that this is in general not a multiple of the SIMD width
345  */
346 void getThreadAtomRange(int numThreads, int threadIndex, int numAtoms, int* startAtom, int* endAtom);
347
348 /*! \brief Generate a new kinetic energy for the v-rescale thermostat
349  *
350  * Generates a new value for the kinetic energy, according to
351  * Bussi et al JCP (2007), Eq. (A7)
352  *
353  * This is used by update_tcoupl(), and by the VRescaleThermostat of the modular
354  * simulator.
355  * TODO: Move this to the VRescaleThermostat once the modular simulator becomes
356  *       the default code path.
357  *
358  * @param kk     present value of the kinetic energy of the atoms to be thermalized (in arbitrary units)
359  * @param sigma  target average value of the kinetic energy (ndeg k_b T/2)  (in the same units as kk)
360  * @param ndeg   number of degrees of freedom of the atoms to be thermalized
361  * @param taut   relaxation time of the thermostat, in units of 'how often this routine is called'
362  * @param step   the time step this routine is called on
363  * @param seed   the random number generator seed
364  * @return  the new kinetic energy
365  */
366 real vrescale_resamplekin(real kk, real sigma, real ndeg, real taut, int64_t step, int64_t seed);
367
368 #endif