2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014, 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.
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.
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.
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.
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.
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.
45 #include "types/commrec.h"
47 #include "gromacs/utility/smalloc.h"
55 #include "gromacs/random/random.h"
65 #include "gmx_omp_nthreads.h"
67 #include "gromacs/fileio/confio.h"
68 #include "gromacs/fileio/futil.h"
69 #include "gromacs/timing/wallcycle.h"
70 #include "gromacs/utility/gmxomp.h"
71 #include "gromacs/pulling/pull.h"
73 /*For debugging, start at v(-dt/2) for velolcity verlet -- uncomment next line */
74 /*#define STARTFROMDT2*/
98 gmx_sd_sigma_t *sdsig;
101 /* andersen temperature control stuff */
102 gmx_bool *randomize_group;
106 typedef struct gmx_update
109 /* xprime for constraint algorithms */
113 /* Variables for the deform algorithm */
114 gmx_int64_t deformref_step;
115 matrix deformref_box;
119 static void do_update_md(int start, int nrend, double dt,
120 t_grp_tcstat *tcstat,
122 gmx_bool bNEMD, t_grp_acc *gstat, rvec accel[],
125 unsigned short ptype[], unsigned short cFREEZE[],
126 unsigned short cACC[], unsigned short cTC[],
127 rvec x[], rvec xprime[], rvec v[],
129 gmx_bool bNH, gmx_bool bPR)
132 int gf = 0, ga = 0, gt = 0;
134 real vn, vv, va, vb, vnrel;
140 /* Update with coupling to extended ensembles, used for
141 * Nose-Hoover and Parrinello-Rahman coupling
142 * Nose-Hoover uses the reversible leap-frog integrator from
143 * Holian et al. Phys Rev E 52(3) : 2338, 1995
145 for (n = start; n < nrend; n++)
160 lg = tcstat[gt].lambda;
165 rvec_sub(v[n], gstat[ga].u, vrel);
167 for (d = 0; d < DIM; d++)
169 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
171 vnrel = (lg*vrel[d] + dt*(imass*f[n][d] - 0.5*vxi*vrel[d]
172 - iprod(M[d], vrel)))/(1 + 0.5*vxi*dt);
173 /* do not scale the mean velocities u */
174 vn = gstat[ga].u[d] + accel[ga][d]*dt + vnrel;
176 xprime[n][d] = x[n][d]+vn*dt;
181 xprime[n][d] = x[n][d];
186 else if (cFREEZE != NULL ||
187 nFreeze[0][XX] || nFreeze[0][YY] || nFreeze[0][ZZ] ||
190 /* Update with Berendsen/v-rescale coupling and freeze or NEMD */
191 for (n = start; n < nrend; n++)
193 w_dt = invmass[n]*dt;
206 lg = tcstat[gt].lambda;
208 for (d = 0; d < DIM; d++)
211 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
213 vv = lg*vn + f[n][d]*w_dt;
215 /* do not scale the mean velocities u */
217 va = vv + accel[ga][d]*dt;
218 vb = va + (1.0-lg)*u;
220 xprime[n][d] = x[n][d]+vb*dt;
225 xprime[n][d] = x[n][d];
232 /* Plain update with Berendsen/v-rescale coupling */
233 for (n = start; n < nrend; n++)
235 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell))
237 w_dt = invmass[n]*dt;
242 lg = tcstat[gt].lambda;
244 for (d = 0; d < DIM; d++)
246 vn = lg*v[n][d] + f[n][d]*w_dt;
248 xprime[n][d] = x[n][d] + vn*dt;
253 for (d = 0; d < DIM; d++)
256 xprime[n][d] = x[n][d];
263 static void do_update_vv_vel(int start, int nrend, double dt,
264 rvec accel[], ivec nFreeze[], real invmass[],
265 unsigned short ptype[], unsigned short cFREEZE[],
266 unsigned short cACC[], rvec v[], rvec f[],
267 gmx_bool bExtended, real veta, real alpha)
272 real u, vn, vv, va, vb, vnrel;
278 g = 0.25*dt*veta*alpha;
280 mv2 = series_sinhx(g);
287 for (n = start; n < nrend; n++)
289 w_dt = invmass[n]*dt;
299 for (d = 0; d < DIM; d++)
301 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
303 v[n][d] = mv1*(mv1*v[n][d] + 0.5*(w_dt*mv2*f[n][d]))+0.5*accel[ga][d]*dt;
311 } /* do_update_vv_vel */
313 static void do_update_vv_pos(int start, int nrend, double dt,
315 unsigned short ptype[], unsigned short cFREEZE[],
316 rvec x[], rvec xprime[], rvec v[],
317 gmx_bool bExtended, real veta)
324 /* Would it make more sense if Parrinello-Rahman was put here? */
329 mr2 = series_sinhx(g);
337 for (n = start; n < nrend; n++)
345 for (d = 0; d < DIM; d++)
347 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
349 xprime[n][d] = mr1*(mr1*x[n][d]+mr2*dt*v[n][d]);
353 xprime[n][d] = x[n][d];
357 } /* do_update_vv_pos */
359 static void do_update_visc(int start, int nrend, double dt,
360 t_grp_tcstat *tcstat,
363 unsigned short ptype[], unsigned short cTC[],
364 rvec x[], rvec xprime[], rvec v[],
365 rvec f[], matrix M, matrix box, real
366 cos_accel, real vcos,
367 gmx_bool bNH, gmx_bool bPR)
372 real lg, vxi = 0, vv;
377 fac = 2*M_PI/(box[ZZ][ZZ]);
381 /* Update with coupling to extended ensembles, used for
382 * Nose-Hoover and Parrinello-Rahman coupling
384 for (n = start; n < nrend; n++)
391 lg = tcstat[gt].lambda;
392 cosz = cos(fac*x[n][ZZ]);
394 copy_rvec(v[n], vrel);
402 for (d = 0; d < DIM; d++)
406 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell))
408 vn = (lg*vrel[d] + dt*(imass*f[n][d] - 0.5*vxi*vrel[d]
409 - iprod(M[d], vrel)))/(1 + 0.5*vxi*dt);
412 vn += vc + dt*cosz*cos_accel;
415 xprime[n][d] = x[n][d]+vn*dt;
419 xprime[n][d] = x[n][d];
426 /* Classic version of update, used with berendsen coupling */
427 for (n = start; n < nrend; n++)
429 w_dt = invmass[n]*dt;
434 lg = tcstat[gt].lambda;
435 cosz = cos(fac*x[n][ZZ]);
437 for (d = 0; d < DIM; d++)
441 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell))
446 /* Do not scale the cosine velocity profile */
447 vv = vc + lg*(vn - vc + f[n][d]*w_dt);
448 /* Add the cosine accelaration profile */
449 vv += dt*cosz*cos_accel;
453 vv = lg*(vn + f[n][d]*w_dt);
456 xprime[n][d] = x[n][d]+vv*dt;
461 xprime[n][d] = x[n][d];
468 static gmx_stochd_t *init_stochd(t_inputrec *ir)
477 ngtc = ir->opts.ngtc;
481 snew(sd->bd_rf, ngtc);
483 else if (EI_SD(ir->eI))
486 snew(sd->sdsig, ngtc);
489 for (n = 0; n < ngtc; n++)
491 if (ir->opts.tau_t[n] > 0)
493 sdc[n].gdt = ir->delta_t/ir->opts.tau_t[n];
494 sdc[n].eph = exp(sdc[n].gdt/2);
495 sdc[n].emh = exp(-sdc[n].gdt/2);
496 sdc[n].em = exp(-sdc[n].gdt);
500 /* No friction and noise on this group */
506 if (sdc[n].gdt >= 0.05)
508 sdc[n].b = sdc[n].gdt*(sdc[n].eph*sdc[n].eph - 1)
509 - 4*(sdc[n].eph - 1)*(sdc[n].eph - 1);
510 sdc[n].c = sdc[n].gdt - 3 + 4*sdc[n].emh - sdc[n].em;
511 sdc[n].d = 2 - sdc[n].eph - sdc[n].emh;
516 /* Seventh order expansions for small y */
517 sdc[n].b = y*y*y*y*(1/3.0+y*(1/3.0+y*(17/90.0+y*7/9.0)));
518 sdc[n].c = y*y*y*(2/3.0+y*(-1/2.0+y*(7/30.0+y*(-1/12.0+y*31/1260.0))));
519 sdc[n].d = y*y*(-1+y*y*(-1/12.0-y*y/360.0));
523 fprintf(debug, "SD const tc-grp %d: b %g c %g d %g\n",
524 n, sdc[n].b, sdc[n].c, sdc[n].d);
528 else if (ETC_ANDERSEN(ir->etc))
537 snew(sd->randomize_group, ngtc);
538 snew(sd->boltzfac, ngtc);
540 /* for now, assume that all groups, if randomized, are randomized at the same rate, i.e. tau_t is the same. */
541 /* since constraint groups don't necessarily match up with temperature groups! This is checked in readir.c */
543 for (n = 0; n < ngtc; n++)
545 reft = max(0.0, opts->ref_t[n]);
546 if ((opts->tau_t[n] > 0) && (reft > 0)) /* tau_t or ref_t = 0 means that no randomization is done */
548 sd->randomize_group[n] = TRUE;
549 sd->boltzfac[n] = BOLTZ*opts->ref_t[n];
553 sd->randomize_group[n] = FALSE;
560 gmx_update_t init_update(t_inputrec *ir)
566 if (ir->eI == eiBD || EI_SD(ir->eI) || ir->etc == etcVRESCALE || ETC_ANDERSEN(ir->etc))
568 upd->sd = init_stochd(ir);
577 static void do_update_sd1(gmx_stochd_t *sd,
578 int start, int nrend, double dt,
579 rvec accel[], ivec nFreeze[],
580 real invmass[], unsigned short ptype[],
581 unsigned short cFREEZE[], unsigned short cACC[],
582 unsigned short cTC[],
583 rvec x[], rvec xprime[], rvec v[], rvec f[],
584 int ngtc, real tau_t[], real ref_t[],
585 gmx_int64_t step, int seed, int* gatindex)
590 int gf = 0, ga = 0, gt = 0;
597 for (n = 0; n < ngtc; n++)
600 /* The mass is encounted for later, since this differs per atom */
601 sig[n].V = sqrt(kT*(1 - sdc[n].em*sdc[n].em));
604 for (n = start; n < nrend; n++)
607 int ng = gatindex ? gatindex[n] : n;
608 ism = sqrt(invmass[n]);
622 gmx_rng_cycle_3gaussian_table(step, ng, seed, RND_SEED_UPDATE, rnd);
623 for (d = 0; d < DIM; d++)
625 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
627 sd_V = ism*sig[gt].V*rnd[d];
629 v[n][d] = v[n][d]*sdc[gt].em
630 + (invmass[n]*f[n][d] + accel[ga][d])*tau_t[gt]*(1 - sdc[gt].em)
633 xprime[n][d] = x[n][d] + v[n][d]*dt;
638 xprime[n][d] = x[n][d];
644 static void check_sd2_work_data_allocation(gmx_stochd_t *sd, int nrend)
646 if (nrend > sd->sd_V_nalloc)
648 sd->sd_V_nalloc = over_alloc_dd(nrend);
649 srenew(sd->sd_V, sd->sd_V_nalloc);
653 static void do_update_sd2_Tconsts(gmx_stochd_t *sd,
658 /* This is separated from the update below, because it is single threaded */
667 for (gt = 0; gt < ngtc; gt++)
669 kT = BOLTZ*ref_t[gt];
670 /* The mass is encounted for later, since this differs per atom */
671 sig[gt].V = sqrt(kT*(1-sdc[gt].em));
672 sig[gt].X = sqrt(kT*sqr(tau_t[gt])*sdc[gt].c);
673 sig[gt].Yv = sqrt(kT*sdc[gt].b/sdc[gt].c);
674 sig[gt].Yx = sqrt(kT*sqr(tau_t[gt])*sdc[gt].b/(1-sdc[gt].em));
678 static void do_update_sd2(gmx_stochd_t *sd,
680 int start, int nrend,
681 rvec accel[], ivec nFreeze[],
682 real invmass[], unsigned short ptype[],
683 unsigned short cFREEZE[], unsigned short cACC[],
684 unsigned short cTC[],
685 rvec x[], rvec xprime[], rvec v[], rvec f[],
688 gmx_bool bFirstHalf, gmx_int64_t step, int seed,
693 /* The random part of the velocity update, generated in the first
694 * half of the update, needs to be remembered for the second half.
698 int gf = 0, ga = 0, gt = 0;
699 real vn = 0, Vmh, Xmh;
707 for (n = start; n < nrend; n++)
709 real rnd[6], rndi[3];
710 ng = gatindex ? gatindex[n] : n;
711 ism = sqrt(invmass[n]);
725 gmx_rng_cycle_6gaussian_table(step*2+(bFirstHalf ? 1 : 2), ng, seed, RND_SEED_UPDATE, rnd);
728 gmx_rng_cycle_3gaussian_table(step*2, ng, seed, RND_SEED_UPDATE, rndi);
730 for (d = 0; d < DIM; d++)
736 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
742 sd_X[n][d] = ism*sig[gt].X*rndi[d];
744 Vmh = sd_X[n][d]*sdc[gt].d/(tau_t[gt]*sdc[gt].c)
745 + ism*sig[gt].Yv*rnd[d*2];
746 sd_V[n][d] = ism*sig[gt].V*rnd[d*2+1];
748 v[n][d] = vn*sdc[gt].em
749 + (invmass[n]*f[n][d] + accel[ga][d])*tau_t[gt]*(1 - sdc[gt].em)
750 + sd_V[n][d] - sdc[gt].em*Vmh;
752 xprime[n][d] = x[n][d] + v[n][d]*tau_t[gt]*(sdc[gt].eph - sdc[gt].emh);
756 /* Correct the velocities for the constraints.
757 * This operation introduces some inaccuracy,
758 * since the velocity is determined from differences in coordinates.
761 (xprime[n][d] - x[n][d])/(tau_t[gt]*(sdc[gt].eph - sdc[gt].emh));
763 Xmh = sd_V[n][d]*tau_t[gt]*sdc[gt].d/(sdc[gt].em-1)
764 + ism*sig[gt].Yx*rnd[d*2];
765 sd_X[n][d] = ism*sig[gt].X*rnd[d*2+1];
767 xprime[n][d] += sd_X[n][d] - Xmh;
776 xprime[n][d] = x[n][d];
783 static void do_update_bd_Tconsts(double dt, real friction_coefficient,
784 int ngtc, const real ref_t[],
787 /* This is separated from the update below, because it is single threaded */
790 if (friction_coefficient != 0)
792 for (gt = 0; gt < ngtc; gt++)
794 rf[gt] = sqrt(2.0*BOLTZ*ref_t[gt]/(friction_coefficient*dt));
799 for (gt = 0; gt < ngtc; gt++)
801 rf[gt] = sqrt(2.0*BOLTZ*ref_t[gt]);
806 static void do_update_bd(int start, int nrend, double dt,
808 real invmass[], unsigned short ptype[],
809 unsigned short cFREEZE[], unsigned short cTC[],
810 rvec x[], rvec xprime[], rvec v[],
811 rvec f[], real friction_coefficient,
812 real *rf, gmx_int64_t step, int seed,
815 /* note -- these appear to be full step velocities . . . */
821 if (friction_coefficient != 0)
823 invfr = 1.0/friction_coefficient;
826 for (n = start; (n < nrend); n++)
829 int ng = gatindex ? gatindex[n] : n;
839 gmx_rng_cycle_3gaussian_table(step, ng, seed, RND_SEED_UPDATE, rnd);
840 for (d = 0; (d < DIM); d++)
842 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
844 if (friction_coefficient != 0)
846 vn = invfr*f[n][d] + rf[gt]*rnd[d];
850 /* NOTE: invmass = 2/(mass*friction_constant*dt) */
851 vn = 0.5*invmass[n]*f[n][d]*dt
852 + sqrt(0.5*invmass[n])*rf[gt]*rnd[d];
856 xprime[n][d] = x[n][d]+vn*dt;
861 xprime[n][d] = x[n][d];
867 static void dump_it_all(FILE gmx_unused *fp, const char gmx_unused *title,
868 int gmx_unused natoms, rvec gmx_unused x[], rvec gmx_unused xp[],
869 rvec gmx_unused v[], rvec gmx_unused f[])
874 fprintf(fp, "%s\n", title);
875 pr_rvecs(fp, 0, "x", x, natoms);
876 pr_rvecs(fp, 0, "xp", xp, natoms);
877 pr_rvecs(fp, 0, "v", v, natoms);
878 pr_rvecs(fp, 0, "f", f, natoms);
883 static void calc_ke_part_normal(rvec v[], t_grpopts *opts, t_mdatoms *md,
884 gmx_ekindata_t *ekind, t_nrnb *nrnb, gmx_bool bEkinAveVel,
885 gmx_bool bSaveEkinOld)
888 t_grp_tcstat *tcstat = ekind->tcstat;
889 t_grp_acc *grpstat = ekind->grpstat;
892 /* three main: VV with AveVel, vv with AveEkin, leap with AveEkin. Leap with AveVel is also
893 an option, but not supported now. Additionally, if we are doing iterations.
894 bEkinAveVel: If TRUE, we sum into ekin, if FALSE, into ekinh.
895 bSavEkinOld: If TRUE (in the case of iteration = bIterate is TRUE), we don't copy over the ekinh_old.
896 If FALSE, we overrwrite it.
899 /* group velocities are calculated in update_ekindata and
900 * accumulated in acumulate_groups.
901 * Now the partial global and groups ekin.
903 for (g = 0; (g < opts->ngtc); g++)
908 copy_mat(tcstat[g].ekinh, tcstat[g].ekinh_old);
912 clear_mat(tcstat[g].ekinf);
916 clear_mat(tcstat[g].ekinh);
920 tcstat[g].ekinscalef_nhc = 1.0; /* need to clear this -- logic is complicated! */
923 ekind->dekindl_old = ekind->dekindl;
925 nthread = gmx_omp_nthreads_get(emntUpdate);
927 #pragma omp parallel for num_threads(nthread) schedule(static)
928 for (thread = 0; thread < nthread; thread++)
930 int start_t, end_t, n;
938 start_t = ((thread+0)*md->homenr)/nthread;
939 end_t = ((thread+1)*md->homenr)/nthread;
941 ekin_sum = ekind->ekin_work[thread];
942 dekindl_sum = ekind->dekindl_work[thread];
944 for (gt = 0; gt < opts->ngtc; gt++)
946 clear_mat(ekin_sum[gt]);
952 for (n = start_t; n < end_t; n++)
962 hm = 0.5*md->massT[n];
964 for (d = 0; (d < DIM); d++)
966 v_corrt[d] = v[n][d] - grpstat[ga].u[d];
968 for (d = 0; (d < DIM); d++)
970 for (m = 0; (m < DIM); m++)
972 /* if we're computing a full step velocity, v_corrt[d] has v(t). Otherwise, v(t+dt/2) */
973 ekin_sum[gt][m][d] += hm*v_corrt[m]*v_corrt[d];
976 if (md->nMassPerturbed && md->bPerturbed[n])
979 0.5*(md->massB[n] - md->massA[n])*iprod(v_corrt, v_corrt);
985 for (thread = 0; thread < nthread; thread++)
987 for (g = 0; g < opts->ngtc; g++)
991 m_add(tcstat[g].ekinf, ekind->ekin_work[thread][g],
996 m_add(tcstat[g].ekinh, ekind->ekin_work[thread][g],
1001 ekind->dekindl += *ekind->dekindl_work[thread];
1004 inc_nrnb(nrnb, eNR_EKIN, md->homenr);
1007 static void calc_ke_part_visc(matrix box, rvec x[], rvec v[],
1008 t_grpopts *opts, t_mdatoms *md,
1009 gmx_ekindata_t *ekind,
1010 t_nrnb *nrnb, gmx_bool bEkinAveVel)
1012 int start = 0, homenr = md->homenr;
1013 int g, d, n, m, gt = 0;
1016 t_grp_tcstat *tcstat = ekind->tcstat;
1017 t_cos_acc *cosacc = &(ekind->cosacc);
1022 for (g = 0; g < opts->ngtc; g++)
1024 copy_mat(ekind->tcstat[g].ekinh, ekind->tcstat[g].ekinh_old);
1025 clear_mat(ekind->tcstat[g].ekinh);
1027 ekind->dekindl_old = ekind->dekindl;
1029 fac = 2*M_PI/box[ZZ][ZZ];
1032 for (n = start; n < start+homenr; n++)
1038 hm = 0.5*md->massT[n];
1040 /* Note that the times of x and v differ by half a step */
1041 /* MRS -- would have to be changed for VV */
1042 cosz = cos(fac*x[n][ZZ]);
1043 /* Calculate the amplitude of the new velocity profile */
1044 mvcos += 2*cosz*md->massT[n]*v[n][XX];
1046 copy_rvec(v[n], v_corrt);
1047 /* Subtract the profile for the kinetic energy */
1048 v_corrt[XX] -= cosz*cosacc->vcos;
1049 for (d = 0; (d < DIM); d++)
1051 for (m = 0; (m < DIM); m++)
1053 /* if we're computing a full step velocity, v_corrt[d] has v(t). Otherwise, v(t+dt/2) */
1056 tcstat[gt].ekinf[m][d] += hm*v_corrt[m]*v_corrt[d];
1060 tcstat[gt].ekinh[m][d] += hm*v_corrt[m]*v_corrt[d];
1064 if (md->nPerturbed && md->bPerturbed[n])
1066 dekindl += 0.5*(md->massB[n] - md->massA[n])*iprod(v_corrt, v_corrt);
1069 ekind->dekindl = dekindl;
1070 cosacc->mvcos = mvcos;
1072 inc_nrnb(nrnb, eNR_EKIN, homenr);
1075 void calc_ke_part(t_state *state, t_grpopts *opts, t_mdatoms *md,
1076 gmx_ekindata_t *ekind, t_nrnb *nrnb, gmx_bool bEkinAveVel, gmx_bool bSaveEkinOld)
1078 if (ekind->cosacc.cos_accel == 0)
1080 calc_ke_part_normal(state->v, opts, md, ekind, nrnb, bEkinAveVel, bSaveEkinOld);
1084 calc_ke_part_visc(state->box, state->x, state->v, opts, md, ekind, nrnb, bEkinAveVel);
1088 extern void init_ekinstate(ekinstate_t *ekinstate, const t_inputrec *ir)
1090 ekinstate->ekin_n = ir->opts.ngtc;
1091 snew(ekinstate->ekinh, ekinstate->ekin_n);
1092 snew(ekinstate->ekinf, ekinstate->ekin_n);
1093 snew(ekinstate->ekinh_old, ekinstate->ekin_n);
1094 snew(ekinstate->ekinscalef_nhc, ekinstate->ekin_n);
1095 snew(ekinstate->ekinscaleh_nhc, ekinstate->ekin_n);
1096 snew(ekinstate->vscale_nhc, ekinstate->ekin_n);
1097 ekinstate->dekindl = 0;
1098 ekinstate->mvcos = 0;
1101 void update_ekinstate(ekinstate_t *ekinstate, gmx_ekindata_t *ekind)
1105 for (i = 0; i < ekinstate->ekin_n; i++)
1107 copy_mat(ekind->tcstat[i].ekinh, ekinstate->ekinh[i]);
1108 copy_mat(ekind->tcstat[i].ekinf, ekinstate->ekinf[i]);
1109 copy_mat(ekind->tcstat[i].ekinh_old, ekinstate->ekinh_old[i]);
1110 ekinstate->ekinscalef_nhc[i] = ekind->tcstat[i].ekinscalef_nhc;
1111 ekinstate->ekinscaleh_nhc[i] = ekind->tcstat[i].ekinscaleh_nhc;
1112 ekinstate->vscale_nhc[i] = ekind->tcstat[i].vscale_nhc;
1115 copy_mat(ekind->ekin, ekinstate->ekin_total);
1116 ekinstate->dekindl = ekind->dekindl;
1117 ekinstate->mvcos = ekind->cosacc.mvcos;
1121 void restore_ekinstate_from_state(t_commrec *cr,
1122 gmx_ekindata_t *ekind, ekinstate_t *ekinstate)
1128 for (i = 0; i < ekinstate->ekin_n; i++)
1130 copy_mat(ekinstate->ekinh[i], ekind->tcstat[i].ekinh);
1131 copy_mat(ekinstate->ekinf[i], ekind->tcstat[i].ekinf);
1132 copy_mat(ekinstate->ekinh_old[i], ekind->tcstat[i].ekinh_old);
1133 ekind->tcstat[i].ekinscalef_nhc = ekinstate->ekinscalef_nhc[i];
1134 ekind->tcstat[i].ekinscaleh_nhc = ekinstate->ekinscaleh_nhc[i];
1135 ekind->tcstat[i].vscale_nhc = ekinstate->vscale_nhc[i];
1138 copy_mat(ekinstate->ekin_total, ekind->ekin);
1140 ekind->dekindl = ekinstate->dekindl;
1141 ekind->cosacc.mvcos = ekinstate->mvcos;
1142 n = ekinstate->ekin_n;
1147 gmx_bcast(sizeof(n), &n, cr);
1148 for (i = 0; i < n; i++)
1150 gmx_bcast(DIM*DIM*sizeof(ekind->tcstat[i].ekinh[0][0]),
1151 ekind->tcstat[i].ekinh[0], cr);
1152 gmx_bcast(DIM*DIM*sizeof(ekind->tcstat[i].ekinf[0][0]),
1153 ekind->tcstat[i].ekinf[0], cr);
1154 gmx_bcast(DIM*DIM*sizeof(ekind->tcstat[i].ekinh_old[0][0]),
1155 ekind->tcstat[i].ekinh_old[0], cr);
1157 gmx_bcast(sizeof(ekind->tcstat[i].ekinscalef_nhc),
1158 &(ekind->tcstat[i].ekinscalef_nhc), cr);
1159 gmx_bcast(sizeof(ekind->tcstat[i].ekinscaleh_nhc),
1160 &(ekind->tcstat[i].ekinscaleh_nhc), cr);
1161 gmx_bcast(sizeof(ekind->tcstat[i].vscale_nhc),
1162 &(ekind->tcstat[i].vscale_nhc), cr);
1164 gmx_bcast(DIM*DIM*sizeof(ekind->ekin[0][0]),
1165 ekind->ekin[0], cr);
1167 gmx_bcast(sizeof(ekind->dekindl), &ekind->dekindl, cr);
1168 gmx_bcast(sizeof(ekind->cosacc.mvcos), &ekind->cosacc.mvcos, cr);
1172 void set_deform_reference_box(gmx_update_t upd, gmx_int64_t step, matrix box)
1174 upd->deformref_step = step;
1175 copy_mat(box, upd->deformref_box);
1178 static void deform(gmx_update_t upd,
1179 int start, int homenr, rvec x[], matrix box, matrix *scale_tot,
1180 const t_inputrec *ir, gmx_int64_t step)
1182 matrix bnew, invbox, mu;
1186 elapsed_time = (step + 1 - upd->deformref_step)*ir->delta_t;
1187 copy_mat(box, bnew);
1188 for (i = 0; i < DIM; i++)
1190 for (j = 0; j < DIM; j++)
1192 if (ir->deform[i][j] != 0)
1195 upd->deformref_box[i][j] + elapsed_time*ir->deform[i][j];
1199 /* We correct the off-diagonal elements,
1200 * which can grow indefinitely during shearing,
1201 * so the shifts do not get messed up.
1203 for (i = 1; i < DIM; i++)
1205 for (j = i-1; j >= 0; j--)
1207 while (bnew[i][j] - box[i][j] > 0.5*bnew[j][j])
1209 rvec_dec(bnew[i], bnew[j]);
1211 while (bnew[i][j] - box[i][j] < -0.5*bnew[j][j])
1213 rvec_inc(bnew[i], bnew[j]);
1217 m_inv_ur0(box, invbox);
1218 copy_mat(bnew, box);
1219 mmul_ur0(box, invbox, mu);
1221 for (i = start; i < start+homenr; i++)
1223 x[i][XX] = mu[XX][XX]*x[i][XX]+mu[YY][XX]*x[i][YY]+mu[ZZ][XX]*x[i][ZZ];
1224 x[i][YY] = mu[YY][YY]*x[i][YY]+mu[ZZ][YY]*x[i][ZZ];
1225 x[i][ZZ] = mu[ZZ][ZZ]*x[i][ZZ];
1229 /* The transposes of the scaling matrices are stored,
1230 * so we need to do matrix multiplication in the inverse order.
1232 mmul_ur0(*scale_tot, mu, *scale_tot);
1236 void update_tcouple(gmx_int64_t step,
1237 t_inputrec *inputrec,
1239 gmx_ekindata_t *ekind,
1244 gmx_bool bTCouple = FALSE;
1246 int i, start, end, homenr, offset;
1248 /* if using vv with trotter decomposition methods, we do this elsewhere in the code */
1249 if (inputrec->etc != etcNO &&
1250 !(IR_NVT_TROTTER(inputrec) || IR_NPT_TROTTER(inputrec) || IR_NPH_TROTTER(inputrec)))
1252 /* We should only couple after a step where energies were determined (for leapfrog versions)
1253 or the step energies are determined, for velocity verlet versions */
1255 if (EI_VV(inputrec->eI))
1263 bTCouple = (inputrec->nsttcouple == 1 ||
1264 do_per_step(step+inputrec->nsttcouple-offset,
1265 inputrec->nsttcouple));
1270 dttc = inputrec->nsttcouple*inputrec->delta_t;
1272 switch (inputrec->etc)
1277 berendsen_tcoupl(inputrec, ekind, dttc);
1280 nosehoover_tcoupl(&(inputrec->opts), ekind, dttc,
1281 state->nosehoover_xi, state->nosehoover_vxi, MassQ);
1284 vrescale_tcoupl(inputrec, step, ekind, dttc,
1285 state->therm_integral);
1288 /* rescale in place here */
1289 if (EI_VV(inputrec->eI))
1291 rescale_velocities(ekind, md, 0, md->homenr, state->v);
1296 /* Set the T scaling lambda to 1 to have no scaling */
1297 for (i = 0; (i < inputrec->opts.ngtc); i++)
1299 ekind->tcstat[i].lambda = 1.0;
1304 void update_pcouple(FILE *fplog,
1306 t_inputrec *inputrec,
1312 gmx_bool bPCouple = FALSE;
1316 /* if using Trotter pressure, we do this in coupling.c, so we leave it false. */
1317 if (inputrec->epc != epcNO && (!(IR_NPT_TROTTER(inputrec) || IR_NPH_TROTTER(inputrec))))
1319 /* We should only couple after a step where energies were determined */
1320 bPCouple = (inputrec->nstpcouple == 1 ||
1321 do_per_step(step+inputrec->nstpcouple-1,
1322 inputrec->nstpcouple));
1325 clear_mat(pcoupl_mu);
1326 for (i = 0; i < DIM; i++)
1328 pcoupl_mu[i][i] = 1.0;
1335 dtpc = inputrec->nstpcouple*inputrec->delta_t;
1337 switch (inputrec->epc)
1339 /* We can always pcoupl, even if we did not sum the energies
1340 * the previous step, since state->pres_prev is only updated
1341 * when the energies have been summed.
1345 case (epcBERENDSEN):
1348 berendsen_pcoupl(fplog, step, inputrec, dtpc, state->pres_prev, state->box,
1352 case (epcPARRINELLORAHMAN):
1353 parrinellorahman_pcoupl(fplog, step, inputrec, dtpc, state->pres_prev,
1354 state->box, state->box_rel, state->boxv,
1355 M, pcoupl_mu, bInitStep);
1363 static rvec *get_xprime(const t_state *state, gmx_update_t upd)
1365 if (state->nalloc > upd->xp_nalloc)
1367 upd->xp_nalloc = state->nalloc;
1368 srenew(upd->xp, upd->xp_nalloc);
1374 static void combine_forces(gmx_update_t upd,
1376 gmx_constr_t constr,
1377 t_inputrec *ir, t_mdatoms *md, t_idef *idef,
1380 t_state *state, gmx_bool bMolPBC,
1381 int start, int nrend,
1382 rvec f[], rvec f_lr[],
1383 tensor *vir_lr_constr,
1388 /* f contains the short-range forces + the long range forces
1389 * which are stored separately in f_lr.
1392 if (constr != NULL && vir_lr_constr != NULL &&
1393 !(ir->eConstrAlg == econtSHAKE && ir->epc == epcNO))
1395 /* We need to constrain the LR forces separately,
1396 * because due to the different pre-factor for the SR and LR
1397 * forces in the update algorithm, we have to correct
1398 * the constraint virial for the nstcalclr-1 extra f_lr.
1399 * Constrain only the additional LR part of the force.
1401 /* MRS -- need to make sure this works with trotter integration -- the constraint calls may not be right.*/
1406 xp = get_xprime(state, upd);
1408 fac = (nstcalclr - 1)*ir->delta_t*ir->delta_t;
1410 for (i = 0; i < md->homenr; i++)
1412 if (md->cFREEZE != NULL)
1414 gf = md->cFREEZE[i];
1416 for (d = 0; d < DIM; d++)
1418 if ((md->ptype[i] != eptVSite) &&
1419 (md->ptype[i] != eptShell) &&
1420 !ir->opts.nFreeze[gf][d])
1422 xp[i][d] = state->x[i][d] + fac*f_lr[i][d]*md->invmass[i];
1426 constrain(NULL, FALSE, FALSE, constr, idef, ir, NULL, cr, step, 0, md,
1427 state->x, xp, xp, bMolPBC, state->box, state->lambda[efptBONDED], NULL,
1428 NULL, vir_lr_constr, nrnb, econqCoord, ir->epc == epcMTTK, state->veta, state->veta);
1431 /* Add nstcalclr-1 times the LR force to the sum of both forces
1432 * and store the result in forces_lr.
1434 for (i = start; i < nrend; i++)
1436 for (d = 0; d < DIM; d++)
1438 f_lr[i][d] = f[i][d] + (nstcalclr - 1)*f_lr[i][d];
1443 void update_constraints(FILE *fplog,
1445 real *dvdlambda, /* the contribution to be added to the bonded interactions */
1446 t_inputrec *inputrec, /* input record and box stuff */
1447 gmx_ekindata_t *ekind,
1452 rvec force[], /* forces on home particles */
1457 gmx_wallcycle_t wcycle,
1459 gmx_constr_t constr,
1460 gmx_bool bFirstHalf,
1464 gmx_bool bExtended, bLastStep, bLog = FALSE, bEner = FALSE, bDoConstr = FALSE;
1467 int start, homenr, nrend, i, n, m, g, d;
1469 rvec *vbuf, *xprime = NULL;
1476 if (bFirstHalf && !EI_VV(inputrec->eI))
1481 /* for now, SD update is here -- though it really seems like it
1482 should be reformulated as a velocity verlet method, since it has two parts */
1485 homenr = md->homenr;
1486 nrend = start+homenr;
1488 dt = inputrec->delta_t;
1493 * APPLY CONSTRAINTS:
1496 * When doing PR pressure coupling we have to constrain the
1497 * bonds in each iteration. If we are only using Nose-Hoover tcoupling
1498 * it is enough to do this once though, since the relative velocities
1499 * after this will be normal to the bond vector
1504 /* clear out constraints before applying */
1505 clear_mat(vir_part);
1507 xprime = get_xprime(state, upd);
1509 bLastStep = (step == inputrec->init_step+inputrec->nsteps);
1510 bLog = (do_per_step(step, inputrec->nstlog) || bLastStep || (step < 0));
1511 bEner = (do_per_step(step, inputrec->nstenergy) || bLastStep);
1512 /* Constrain the coordinates xprime */
1513 wallcycle_start(wcycle, ewcCONSTR);
1514 if (EI_VV(inputrec->eI) && bFirstHalf)
1516 constrain(NULL, bLog, bEner, constr, idef,
1517 inputrec, ekind, cr, step, 1, md,
1518 state->x, state->v, state->v,
1519 bMolPBC, state->box,
1520 state->lambda[efptBONDED], dvdlambda,
1521 NULL, bCalcVir ? &vir_con : NULL, nrnb, econqVeloc,
1522 inputrec->epc == epcMTTK, state->veta, vetanew);
1526 constrain(NULL, bLog, bEner, constr, idef,
1527 inputrec, ekind, cr, step, 1, md,
1528 state->x, xprime, NULL,
1529 bMolPBC, state->box,
1530 state->lambda[efptBONDED], dvdlambda,
1531 state->v, bCalcVir ? &vir_con : NULL, nrnb, econqCoord,
1532 inputrec->epc == epcMTTK, state->veta, state->veta);
1534 wallcycle_stop(wcycle, ewcCONSTR);
1538 dump_it_all(fplog, "After Shake",
1539 state->natoms, state->x, xprime, state->v, force);
1543 if (inputrec->eI == eiSD2)
1545 /* A correction factor eph is needed for the SD constraint force */
1546 /* Here we can, unfortunately, not have proper corrections
1547 * for different friction constants, so we use the first one.
1549 for (i = 0; i < DIM; i++)
1551 for (m = 0; m < DIM; m++)
1553 vir_part[i][m] += upd->sd->sdc[0].eph*vir_con[i][m];
1559 m_add(vir_part, vir_con, vir_part);
1563 pr_rvecs(debug, 0, "constraint virial", vir_part, DIM);
1569 if ((inputrec->eI == eiSD2) && !(bFirstHalf))
1571 xprime = get_xprime(state, upd);
1573 nth = gmx_omp_nthreads_get(emntUpdate);
1575 #pragma omp parallel for num_threads(nth) schedule(static)
1576 for (th = 0; th < nth; th++)
1578 int start_th, end_th;
1580 start_th = start + ((nrend-start)* th )/nth;
1581 end_th = start + ((nrend-start)*(th+1))/nth;
1583 /* The second part of the SD integration */
1584 do_update_sd2(upd->sd,
1585 FALSE, start_th, end_th,
1586 inputrec->opts.acc, inputrec->opts.nFreeze,
1587 md->invmass, md->ptype,
1588 md->cFREEZE, md->cACC, md->cTC,
1589 state->x, xprime, state->v, force, state->sd_X,
1590 inputrec->opts.tau_t,
1591 FALSE, step, inputrec->ld_seed,
1592 DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL);
1594 inc_nrnb(nrnb, eNR_UPDATE, homenr);
1598 /* Constrain the coordinates xprime */
1599 wallcycle_start(wcycle, ewcCONSTR);
1600 constrain(NULL, bLog, bEner, constr, idef,
1601 inputrec, NULL, cr, step, 1, md,
1602 state->x, xprime, NULL,
1603 bMolPBC, state->box,
1604 state->lambda[efptBONDED], dvdlambda,
1605 NULL, NULL, nrnb, econqCoord, FALSE, 0, 0);
1606 wallcycle_stop(wcycle, ewcCONSTR);
1610 /* We must always unshift after updating coordinates; if we did not shake
1611 x was shifted in do_force */
1613 if (!(bFirstHalf)) /* in the first half of vv, no shift. */
1615 if (graph && (graph->nnodes > 0))
1617 unshift_x(graph, state->box, state->x, upd->xp);
1618 if (TRICLINIC(state->box))
1620 inc_nrnb(nrnb, eNR_SHIFTX, 2*graph->nnodes);
1624 inc_nrnb(nrnb, eNR_SHIFTX, graph->nnodes);
1629 #pragma omp parallel for num_threads(gmx_omp_nthreads_get(emntUpdate)) schedule(static)
1630 for (i = start; i < nrend; i++)
1632 copy_rvec(upd->xp[i], state->x[i]);
1636 dump_it_all(fplog, "After unshift",
1637 state->natoms, state->x, upd->xp, state->v, force);
1639 /* ############# END the update of velocities and positions ######### */
1642 void update_box(FILE *fplog,
1644 t_inputrec *inputrec, /* input record and box stuff */
1647 rvec force[], /* forces on home particles */
1653 gmx_bool bExtended, bLastStep, bLog = FALSE, bEner = FALSE;
1656 int start, homenr, nrend, i, n, m, g;
1660 homenr = md->homenr;
1661 nrend = start+homenr;
1664 (inputrec->etc == etcNOSEHOOVER) ||
1665 (inputrec->epc == epcPARRINELLORAHMAN) ||
1666 (inputrec->epc == epcMTTK);
1668 dt = inputrec->delta_t;
1672 /* now update boxes */
1673 switch (inputrec->epc)
1677 case (epcBERENDSEN):
1678 berendsen_pscale(inputrec, pcoupl_mu, state->box, state->box_rel,
1679 start, homenr, state->x, md->cFREEZE, nrnb);
1681 case (epcPARRINELLORAHMAN):
1682 /* The box velocities were updated in do_pr_pcoupl in the update
1683 * iteration, but we dont change the box vectors until we get here
1684 * since we need to be able to shift/unshift above.
1686 for (i = 0; i < DIM; i++)
1688 for (m = 0; m <= i; m++)
1690 state->box[i][m] += dt*state->boxv[i][m];
1693 preserve_box_shape(inputrec, state->box_rel, state->box);
1695 /* Scale the coordinates */
1696 for (n = start; (n < start+homenr); n++)
1698 tmvmul_ur0(pcoupl_mu, state->x[n], state->x[n]);
1702 switch (inputrec->epct)
1704 case (epctISOTROPIC):
1705 /* DIM * eta = ln V. so DIM*eta_new = DIM*eta_old + DIM*dt*veta =>
1706 ln V_new = ln V_old + 3*dt*veta => V_new = V_old*exp(3*dt*veta) =>
1707 Side length scales as exp(veta*dt) */
1709 msmul(state->box, exp(state->veta*dt), state->box);
1711 /* Relate veta to boxv. veta = d(eta)/dT = (1/DIM)*1/V dV/dT.
1712 o If we assume isotropic scaling, and box length scaling
1713 factor L, then V = L^DIM (det(M)). So dV/dt = DIM
1714 L^(DIM-1) dL/dt det(M), and veta = (1/L) dL/dt. The
1715 determinant of B is L^DIM det(M), and the determinant
1716 of dB/dt is (dL/dT)^DIM det (M). veta will be
1717 (det(dB/dT)/det(B))^(1/3). Then since M =
1718 B_new*(vol_new)^(1/3), dB/dT_new = (veta_new)*B(new). */
1720 msmul(state->box, state->veta, state->boxv);
1730 if ((!(IR_NPT_TROTTER(inputrec) || IR_NPH_TROTTER(inputrec))) && scale_tot)
1732 /* The transposes of the scaling matrices are stored,
1733 * therefore we need to reverse the order in the multiplication.
1735 mmul_ur0(*scale_tot, pcoupl_mu, *scale_tot);
1738 if (DEFORM(*inputrec))
1740 deform(upd, start, homenr, state->x, state->box, scale_tot, inputrec, step);
1743 dump_it_all(fplog, "After update",
1744 state->natoms, state->x, upd->xp, state->v, force);
1747 void update_coords(FILE *fplog,
1749 t_inputrec *inputrec, /* input record and box stuff */
1753 rvec *f, /* forces on home particles */
1756 tensor *vir_lr_constr,
1758 gmx_ekindata_t *ekind,
1763 t_commrec *cr, /* these shouldn't be here -- need to think about it */
1765 gmx_constr_t constr,
1768 gmx_bool bNH, bPR, bLastStep, bLog = FALSE, bEner = FALSE;
1770 real *imass, *imassin;
1773 int start, homenr, nrend, i, j, d, n, m, g;
1774 int blen0, blen1, iatom, jatom, nshake, nsettle, nconstr, nexpand;
1777 rvec *vcom, *xcom, *vall, *xall, *xin, *vin, *forcein, *fall, *xpall, *xprimein, *xprime;
1780 /* Running the velocity half does nothing except for velocity verlet */
1781 if ((UpdatePart == etrtVELOCITY1 || UpdatePart == etrtVELOCITY2) &&
1782 !EI_VV(inputrec->eI))
1784 gmx_incons("update_coords called for velocity without VV integrator");
1788 homenr = md->homenr;
1789 nrend = start+homenr;
1791 xprime = get_xprime(state, upd);
1793 dt = inputrec->delta_t;
1796 /* We need to update the NMR restraint history when time averaging is used */
1797 if (state->flags & (1<<estDISRE_RM3TAV))
1799 update_disres_history(fcd, &state->hist);
1801 if (state->flags & (1<<estORIRE_DTAV))
1803 update_orires_history(fcd, &state->hist);
1807 bNH = inputrec->etc == etcNOSEHOOVER;
1808 bPR = ((inputrec->epc == epcPARRINELLORAHMAN) || (inputrec->epc == epcMTTK));
1810 if (bDoLR && inputrec->nstcalclr > 1 && !EI_VV(inputrec->eI)) /* get this working with VV? */
1812 /* Store the total force + nstcalclr-1 times the LR force
1813 * in forces_lr, so it can be used in a normal update algorithm
1814 * to produce twin time stepping.
1816 /* is this correct in the new construction? MRS */
1818 inputrec->nstcalclr, constr, inputrec, md, idef, cr,
1819 step, state, bMolPBC,
1820 start, nrend, f, f_lr, vir_lr_constr, nrnb);
1828 /* ############# START The update of velocities and positions ######### */
1830 dump_it_all(fplog, "Before update",
1831 state->natoms, state->x, xprime, state->v, force);
1833 if (inputrec->eI == eiSD2)
1835 check_sd2_work_data_allocation(upd->sd, nrend);
1837 do_update_sd2_Tconsts(upd->sd,
1838 inputrec->opts.ngtc,
1839 inputrec->opts.tau_t,
1840 inputrec->opts.ref_t);
1842 if (inputrec->eI == eiBD)
1844 do_update_bd_Tconsts(dt, inputrec->bd_fric,
1845 inputrec->opts.ngtc, inputrec->opts.ref_t,
1849 nth = gmx_omp_nthreads_get(emntUpdate);
1851 #pragma omp parallel for num_threads(nth) schedule(static) private(alpha)
1852 for (th = 0; th < nth; th++)
1854 int start_th, end_th;
1856 start_th = start + ((nrend-start)* th )/nth;
1857 end_th = start + ((nrend-start)*(th+1))/nth;
1859 switch (inputrec->eI)
1862 if (ekind->cosacc.cos_accel == 0)
1864 do_update_md(start_th, end_th, dt,
1865 ekind->tcstat, state->nosehoover_vxi,
1866 ekind->bNEMD, ekind->grpstat, inputrec->opts.acc,
1867 inputrec->opts.nFreeze,
1868 md->invmass, md->ptype,
1869 md->cFREEZE, md->cACC, md->cTC,
1870 state->x, xprime, state->v, force, M,
1875 do_update_visc(start_th, end_th, dt,
1876 ekind->tcstat, state->nosehoover_vxi,
1877 md->invmass, md->ptype,
1878 md->cTC, state->x, xprime, state->v, force, M,
1880 ekind->cosacc.cos_accel,
1886 do_update_sd1(upd->sd,
1887 start_th, end_th, dt,
1888 inputrec->opts.acc, inputrec->opts.nFreeze,
1889 md->invmass, md->ptype,
1890 md->cFREEZE, md->cACC, md->cTC,
1891 state->x, xprime, state->v, force,
1892 inputrec->opts.ngtc, inputrec->opts.tau_t, inputrec->opts.ref_t,
1893 step, inputrec->ld_seed, DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL);
1896 /* The SD update is done in 2 parts, because an extra constraint step
1899 do_update_sd2(upd->sd,
1900 bInitStep, start_th, end_th,
1901 inputrec->opts.acc, inputrec->opts.nFreeze,
1902 md->invmass, md->ptype,
1903 md->cFREEZE, md->cACC, md->cTC,
1904 state->x, xprime, state->v, force, state->sd_X,
1905 inputrec->opts.tau_t,
1906 TRUE, step, inputrec->ld_seed,
1907 DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL);
1910 do_update_bd(start_th, end_th, dt,
1911 inputrec->opts.nFreeze, md->invmass, md->ptype,
1912 md->cFREEZE, md->cTC,
1913 state->x, xprime, state->v, force,
1916 step, inputrec->ld_seed, DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL);
1920 alpha = 1.0 + DIM/((double)inputrec->opts.nrdf[0]); /* assuming barostat coupled to group 0. */
1925 do_update_vv_vel(start_th, end_th, dt,
1926 inputrec->opts.acc, inputrec->opts.nFreeze,
1927 md->invmass, md->ptype,
1928 md->cFREEZE, md->cACC,
1930 (bNH || bPR), state->veta, alpha);
1933 do_update_vv_pos(start_th, end_th, dt,
1934 inputrec->opts.nFreeze,
1935 md->ptype, md->cFREEZE,
1936 state->x, xprime, state->v,
1937 (bNH || bPR), state->veta);
1942 gmx_fatal(FARGS, "Don't know how to update coordinates");
1950 void correct_ekin(FILE *log, int start, int end, rvec v[], rvec vcm, real mass[],
1951 real tmass, tensor ekin)
1954 * This is a debugging routine. It should not be called for production code
1956 * The kinetic energy should calculated according to:
1957 * Ekin = 1/2 m (v-vcm)^2
1958 * However the correction is not always applied, since vcm may not be
1959 * known in time and we compute
1960 * Ekin' = 1/2 m v^2 instead
1961 * This can be corrected afterwards by computing
1962 * Ekin = Ekin' + 1/2 m ( -2 v vcm + vcm^2)
1964 * Ekin = Ekin' - m v vcm + 1/2 m vcm^2
1971 /* Local particles */
1974 /* Processor dependent part. */
1976 for (i = start; (i < end); i++)
1980 for (j = 0; (j < DIM); j++)
1986 svmul(1/tmass, vcm, vcm);
1987 svmul(0.5, vcm, hvcm);
1989 for (j = 0; (j < DIM); j++)
1991 for (k = 0; (k < DIM); k++)
1993 dekin[j][k] += vcm[k]*(tm*hvcm[j]-mv[j]);
1996 pr_rvecs(log, 0, "dekin", dekin, DIM);
1997 pr_rvecs(log, 0, " ekin", ekin, DIM);
1998 fprintf(log, "dekin = %g, ekin = %g vcm = (%8.4f %8.4f %8.4f)\n",
1999 trace(dekin), trace(ekin), vcm[XX], vcm[YY], vcm[ZZ]);
2000 fprintf(log, "mv = (%8.4f %8.4f %8.4f)\n",
2001 mv[XX], mv[YY], mv[ZZ]);
2004 extern gmx_bool update_randomize_velocities(t_inputrec *ir, gmx_int64_t step, const t_commrec *cr,
2005 t_mdatoms *md, t_state *state, gmx_update_t upd, gmx_constr_t constr)
2009 real rate = (ir->delta_t)/ir->opts.tau_t[0];
2011 if (ir->etc == etcANDERSEN && constr != NULL)
2013 gmx_fatal(FARGS, "Normal Andersen is currently not supported with constraints, use massive Andersen instead");
2016 /* proceed with andersen if 1) it's fixed probability per
2017 particle andersen or 2) it's massive andersen and it's tau_t/dt */
2018 if ((ir->etc == etcANDERSEN) || do_per_step(step, (int)(1.0/rate)))
2020 andersen_tcoupl(ir, step, cr, md, state, rate,
2021 upd->sd->randomize_group, upd->sd->boltzfac);