Make Constraints a proper class
[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,2018, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #ifndef GMX_MDLIB_UPDATE_H
38 #define GMX_MDLIB_UPDATE_H
39
40 #include "gromacs/math/paddedvector.h"
41 #include "gromacs/math/vectypes.h"
42 #include "gromacs/timing/wallcycle.h"
43 #include "gromacs/utility/arrayref.h"
44 #include "gromacs/utility/basedefinitions.h"
45 #include "gromacs/utility/real.h"
46
47 class ekinstate_t;
48 struct gmx_ekindata_t;
49 struct gmx_enerdata_t;
50 struct t_extmass;
51 struct t_fcdata;
52 struct t_graph;
53 struct t_grpopts;
54 struct t_inputrec;
55 struct t_mdatoms;
56 struct t_nrnb;
57 class t_state;
58
59 /* Abstract type for update */
60 struct gmx_update_t;
61
62 namespace gmx
63 {
64 class Constraints;
65 }
66
67 /* Initialize the stochastic dynamics struct */
68 gmx_update_t *init_update(const t_inputrec *ir);
69
70 /* Update pre-computed constants that depend on the reference
71  * temperature for coupling.
72  *
73  * This could change e.g. in simulated annealing. */
74 void update_temperature_constants(gmx_update_t *upd, const t_inputrec *ir);
75
76 /* Update the size of per-atom arrays (e.g. after DD re-partitioning,
77    which might increase the number of home atoms). */
78 void update_realloc(gmx_update_t *upd, int natoms);
79
80 /* Store the box at step step
81  * as a reference state for simulations with box deformation.
82  */
83 void set_deform_reference_box(gmx_update_t *upd,
84                               gmx_int64_t step, matrix box);
85
86 void update_tcouple(gmx_int64_t       step,
87                     t_inputrec       *inputrec,
88                     t_state          *state,
89                     gmx_ekindata_t   *ekind,
90                     t_extmass        *MassQ,
91                     t_mdatoms        *md
92                     );
93
94 /* Update Parrinello-Rahman, to be called before the coordinate update */
95 void update_pcouple_before_coordinates(FILE             *fplog,
96                                        gmx_int64_t       step,
97                                        const t_inputrec *inputrec,
98                                        t_state          *state,
99                                        matrix            parrinellorahmanMu,
100                                        matrix            M,
101                                        gmx_bool          bInitStep);
102
103 /* Update the box, to be called after the coordinate update.
104  * For Berendsen P-coupling, also calculates the scaling factor
105  * and scales the coordinates.
106  * When the deform option is used, scales coordinates and box here.
107  */
108 void update_pcouple_after_coordinates(FILE             *fplog,
109                                       gmx_int64_t       step,
110                                       const t_inputrec *inputrec,
111                                       const t_mdatoms  *md,
112                                       const matrix      pressure,
113                                       const matrix      forceVirial,
114                                       const matrix      constraintVirial,
115                                       const matrix      parrinellorahmanMu,
116                                       t_state          *state,
117                                       t_nrnb           *nrnb,
118                                       gmx_update_t     *upd);
119
120 void update_coords(gmx_int64_t                    step,
121                    t_inputrec                    *inputrec, /* input record and box stuff       */
122                    t_mdatoms                     *md,
123                    t_state                       *state,
124                    gmx::PaddedArrayRef<gmx::RVec> f, /* forces on home particles */
125                    t_fcdata                      *fcd,
126                    gmx_ekindata_t                *ekind,
127                    matrix                         M,
128                    gmx_update_t                  *upd,
129                    int                            bUpdatePart,
130                    const t_commrec               *cr, /* these shouldn't be here -- need to think about it */
131                    gmx::Constraints              *constr);
132
133 /* Return TRUE if OK, FALSE in case of Shake Error */
134
135 extern gmx_bool update_randomize_velocities(t_inputrec *ir, gmx_int64_t step, const t_commrec *cr, t_mdatoms *md, t_state *state, gmx_update_t *upd, gmx::Constraints *constr);
136
137 void constrain_velocities(gmx_int64_t                    step,
138                           real                          *dvdlambda, /* the contribution to be added to the bonded interactions */
139                           t_state                       *state,
140                           tensor                         vir_part,
141                           gmx_wallcycle_t                wcycle,
142                           gmx::Constraints              *constr,
143                           gmx_bool                       bCalcVir,
144                           bool                           do_log,
145                           bool                           do_ene);
146
147 void constrain_coordinates(gmx_int64_t                    step,
148                            real                          *dvdlambda, /* the contribution to be added to the bonded interactions */
149                            t_state                       *state,
150                            tensor                         vir_part,
151                            gmx_wallcycle_t                wcycle,
152                            gmx_update_t                  *upd,
153                            gmx::Constraints              *constr,
154                            gmx_bool                       bCalcVir,
155                            bool                           do_log,
156                            bool                           do_ene);
157
158 void update_sd_second_half(gmx_int64_t                    step,
159                            real                          *dvdlambda, /* the contribution to be added to the bonded interactions */
160                            const t_inputrec              *inputrec,  /* input record and box stuff */
161                            t_mdatoms                     *md,
162                            t_state                       *state,
163                            const t_commrec               *cr,
164                            t_nrnb                        *nrnb,
165                            gmx_wallcycle_t                wcycle,
166                            gmx_update_t                  *upd,
167                            gmx::Constraints              *constr,
168                            bool                           do_log,
169                            bool                           do_ene);
170
171 void finish_update(const t_inputrec              *inputrec,
172                    t_mdatoms                     *md,
173                    t_state                       *state,
174                    t_graph                       *graph,
175                    t_nrnb                        *nrnb,
176                    gmx_wallcycle_t                wcycle,
177                    gmx_update_t                  *upd,
178                    gmx::Constraints              *constr);
179
180 /* Return TRUE if OK, FALSE in case of Shake Error */
181
182 void calc_ke_part(t_state *state, t_grpopts *opts, t_mdatoms *md,
183                   gmx_ekindata_t *ekind, t_nrnb *nrnb, gmx_bool bEkinAveVel);
184 /*
185  * Compute the partial kinetic energy for home particles;
186  * will be accumulated in the calling routine.
187  * The tensor is
188  *
189  * Ekin = SUM(i) 0.5 m[i] v[i] (x) v[i]
190  *
191  *     use v[i] = v[i] - u[i] when calculating temperature
192  *
193  * u must be accumulated already.
194  *
195  * Now also computes the contribution of the kinetic energy to the
196  * free energy
197  *
198  */
199
200
201 void
202 init_ekinstate(ekinstate_t *ekinstate, const t_inputrec *ir);
203
204 void
205 update_ekinstate(ekinstate_t *ekinstate, gmx_ekindata_t *ekind);
206
207 /*! \brief Restores data from \p ekinstate to \p ekind, then broadcasts it
208    to the rest of the simulation */
209 void
210 restore_ekinstate_from_state(const t_commrec *cr,
211                              gmx_ekindata_t *ekind, const ekinstate_t *ekinstate);
212
213 void berendsen_tcoupl(const t_inputrec *ir, const gmx_ekindata_t *ekind, real dt,
214                       std::vector<double> &therm_integral);
215
216 void andersen_tcoupl(t_inputrec *ir, gmx_int64_t step,
217                      const t_commrec *cr, const t_mdatoms *md, t_state *state, real rate, const gmx_bool *randomize, const real *boltzfac);
218
219 void nosehoover_tcoupl(t_grpopts *opts, gmx_ekindata_t *ekind, real dt,
220                        double xi[], double vxi[], t_extmass *MassQ);
221
222 void trotter_update(t_inputrec *ir, gmx_int64_t step, gmx_ekindata_t *ekind,
223                     gmx_enerdata_t *enerd, t_state *state, tensor vir, t_mdatoms *md,
224                     t_extmass *MassQ, int **trotter_seqlist, int trotter_seqno);
225
226 int **init_npt_vars(t_inputrec *ir, t_state *state, t_extmass *Mass, gmx_bool bTrotter);
227
228 real NPT_energy(const t_inputrec *ir, const t_state *state, const t_extmass *MassQ);
229 /* computes all the pressure/tempertature control energy terms to get a conserved energy */
230
231 void NBaroT_trotter(t_grpopts *opts, real dt,
232                     double xi[], double vxi[], real *veta, t_extmass *MassQ);
233
234 void vrescale_tcoupl(t_inputrec *ir, gmx_int64_t step,
235                      gmx_ekindata_t *ekind, real dt,
236                      double therm_integral[]);
237 /* Compute temperature scaling. For V-rescale it is done in update. */
238
239 void rescale_velocities(gmx_ekindata_t *ekind, t_mdatoms *mdatoms,
240                         int start, int end, rvec v[]);
241 /* Rescale the velocities with the scaling factor in ekind */
242
243 void update_annealing_target_temp(t_inputrec *ir, real t, gmx_update_t *upd);
244 /* Set reference temp for simulated annealing at time t*/
245
246 real calc_temp(real ekin, real nrdf);
247 /* Calculate the temperature */
248
249 real calc_pres(int ePBC, int nwall, matrix box, tensor ekin, tensor vir,
250                tensor pres);
251 /* Calculate the pressure tensor, returns the scalar pressure.
252  * The unit of pressure is bar.
253  */
254
255 void parrinellorahman_pcoupl(FILE *fplog, gmx_int64_t step,
256                              const t_inputrec *ir, real dt, const tensor pres,
257                              tensor box, tensor box_rel, tensor boxv,
258                              tensor M, matrix mu,
259                              gmx_bool bFirstStep);
260
261 void berendsen_pcoupl(FILE *fplog, gmx_int64_t step,
262                       const t_inputrec *ir, real dt,
263                       const tensor pres, const matrix box,
264                       const matrix force_vir, const matrix constraint_vir,
265                       matrix mu, double *baros_integral);
266
267 void berendsen_pscale(const t_inputrec *ir, const matrix mu,
268                       matrix box, matrix box_rel,
269                       int start, int nr_atoms,
270                       rvec x[], const unsigned short cFREEZE[],
271                       t_nrnb *nrnb);
272
273 void correct_ekin(FILE *log, int start, int end, rvec v[],
274                   rvec vcm, real mass[], real tmass, tensor ekin);
275 /* Correct ekin for vcm */
276
277 #endif