Fixed typos in GB inner dielectric commit for some kernels.
[gromacs.git] / src / mdlib / update.c
blob5074712095fb61f31a8b3e6f0c7b3a2d7ffa614c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 3.2.0
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
33 * And Hey:
34 * GROwing Monsters And Cloning Shrimps
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
41 #include <stdio.h>
42 #include <math.h>
44 #include "sysstuff.h"
45 #include "smalloc.h"
46 #include "typedefs.h"
47 #include "nrnb.h"
48 #include "physics.h"
49 #include "macros.h"
50 #include "vec.h"
51 #include "main.h"
52 #include "confio.h"
53 #include "update.h"
54 #include "gmx_random.h"
55 #include "futil.h"
56 #include "mshift.h"
57 #include "tgroup.h"
58 #include "force.h"
59 #include "names.h"
60 #include "txtdump.h"
61 #include "mdrun.h"
62 #include "copyrite.h"
63 #include "constr.h"
64 #include "edsam.h"
65 #include "pull.h"
66 #include "disre.h"
67 #include "orires.h"
68 #include "gmx_wallcycle.h"
70 /*For debugging, start at v(-dt/2) for velolcity verlet -- uncomment next line */
71 /*#define STARTFROMDT2*/
73 typedef struct {
74 double gdt;
75 double eph;
76 double emh;
77 double em;
78 double b;
79 double c;
80 double d;
81 } gmx_sd_const_t;
83 typedef struct {
84 real V;
85 real X;
86 real Yv;
87 real Yx;
88 } gmx_sd_sigma_t;
90 typedef struct {
91 /* The random state */
92 gmx_rng_t gaussrand;
93 /* BD stuff */
94 real *bd_rf;
95 /* SD stuff */
96 gmx_sd_const_t *sdc;
97 gmx_sd_sigma_t *sdsig;
98 rvec *sd_V;
99 int sd_V_nalloc;
100 } gmx_stochd_t;
102 typedef struct gmx_update
104 gmx_stochd_t *sd;
105 rvec *xp;
106 int xp_nalloc;
107 /* Variables for the deform algorithm */
108 gmx_large_int_t deformref_step;
109 matrix deformref_box;
110 } t_gmx_update;
113 void store_rvec(rvec *from, rvec *to, int n) {
114 int i;
115 for (i=0;i<n;i++) {
116 copy_rvec(from[i],to[i]);
120 static void do_update_md(int start,int nrend,double dt,
121 t_grp_tcstat *tcstat,t_grp_acc *gstat,double nh_vxi[],
122 rvec accel[],ivec nFreeze[],real invmass[],
123 unsigned short ptype[],unsigned short cFREEZE[],
124 unsigned short cACC[],unsigned short cTC[],
125 rvec x[],rvec xprime[],rvec v[],
126 rvec f[],matrix M,
127 bool bNH,bool bPR)
129 double imass,w_dt;
130 int gf=0,ga=0,gt=0;
131 rvec vrel;
132 real vn,vv,va,vb,vnrel;
133 real lg,vxi=0,u;
134 int n,d;
136 if (bNH || bPR)
138 /* Update with coupling to extended ensembles, used for
139 * Nose-Hoover and Parrinello-Rahman coupling
140 * Nose-Hoover uses the reversible leap-frog integrator from
141 * Holian et al. Phys Rev E 52(3) : 2338, 1995
143 for(n=start; n<nrend; n++)
145 imass = invmass[n];
146 if (cFREEZE)
148 gf = cFREEZE[n];
150 if (cACC)
152 ga = cACC[n];
154 if (cTC)
156 gt = cTC[n];
158 lg = tcstat[gt].lambda;
159 if (bNH) {
160 vxi = nh_vxi[gt];
162 rvec_sub(v[n],gstat[ga].u,vrel);
164 for(d=0; d<DIM; d++)
166 if((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
168 vnrel = (lg*vrel[d] + dt*(imass*f[n][d] - 0.5*vxi*vrel[d]
169 - iprod(M[d],vrel)))/(1 + 0.5*vxi*dt);
170 /* do not scale the mean velocities u */
171 vn = gstat[ga].u[d] + accel[ga][d]*dt + vnrel;
172 v[n][d] = vn;
173 xprime[n][d] = x[n][d]+vn*dt;
175 else
177 v[n][d] = 0.0;
178 xprime[n][d] = x[n][d];
183 else
185 /* Classic version of update, used with berendsen coupling */
186 for(n=start; n<nrend; n++)
188 w_dt = invmass[n]*dt;
189 if (cFREEZE)
191 gf = cFREEZE[n];
193 if (cACC)
195 ga = cACC[n];
197 if (cTC)
199 gt = cTC[n];
201 lg = tcstat[gt].lambda;
203 for(d=0; d<DIM; d++)
205 vn = v[n][d];
206 if((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
208 vv = lg*vn + f[n][d]*w_dt;
210 /* do not scale the mean velocities u */
211 u = gstat[ga].u[d];
212 va = vv + accel[ga][d]*dt;
213 vb = va + (1.0-lg)*u;
214 v[n][d] = vb;
215 xprime[n][d] = x[n][d]+vb*dt;
217 else
219 v[n][d] = 0.0;
220 xprime[n][d] = x[n][d];
227 static void do_update_vv_vel(int start,int nrend,double dt,
228 t_grp_tcstat *tcstat,t_grp_acc *gstat,
229 rvec accel[],ivec nFreeze[],real invmass[],
230 unsigned short ptype[],
231 unsigned short cFREEZE[],
232 unsigned short cACC[],unsigned short cTC[],
233 rvec v[],rvec f[],bool bExtended, real veta, real alpha)
235 double imass,w_dt;
236 int gf=0,ga=0,gt=0;
237 rvec vrel,sumf;
238 real vn,vv,va,vb,vnrel;
239 real lg,u;
240 int n,d;
241 double g,mv1,mv2;
243 g = 0.25*dt*veta*alpha;
244 mv1 = exp(-g);
245 mv2 = series_sinhx(g);
247 for(n=start; n<nrend; n++)
249 w_dt = invmass[n]*dt;
250 if (cFREEZE)
252 gf = cFREEZE[n];
254 if (cACC)
256 ga = cACC[n];
258 if (cTC)
260 gt = cTC[n];
262 lg = tcstat[gt].lambda;
263 rvec_sub(v[n],gstat[ga].u,vrel);
265 for(d=0; d<DIM; d++)
267 if((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
269 if (bExtended)
271 vnrel = mv1*(mv1*vrel[d] + 0.5*w_dt*mv2*f[n][d]);
272 /* do not scale the mean velocities u: MRS: may not be done correctly currently? */
273 vn = gstat[ga].u[d] + accel[ga][d]*dt + vnrel;
274 v[n][d] = vn;
276 else
278 vv = lg*v[n][d] + 0.5*f[n][d]*w_dt;
279 /* do not scale the mean velocities u MRS: may not be done correctly currently? */
280 u = gstat[ga].u[d];
281 va = vv + 0.5*accel[ga][d]*dt;
282 vb = va + (1.0-lg)*u;
283 v[n][d] = vb;
286 else
288 v[n][d] = 0.0;
292 } /* do_update_vv_vel */
294 static void do_update_vv_pos(int start,int nrend,double dt,
295 t_grp_tcstat *tcstat,t_grp_acc *gstat,
296 rvec accel[],ivec nFreeze[],real invmass[],
297 unsigned short ptype[],
298 unsigned short cFREEZE[],
299 unsigned short cACC[],unsigned short cTC[],
300 rvec x[],rvec xprime[],rvec v[],
301 rvec f[],bool bExtended, real veta, real alpha)
303 double imass,w_dt;
304 int gf=0,ga=0,gt=0;
305 rvec vrel;
306 real vn,vv,va,vb,vnrel;
307 real lg,u,scale;
308 int n,d;
309 double g,mr1,mr2,mv1,mv2;
311 if (bExtended) {
312 g = 0.5*dt*veta;
313 mr1 = exp(g);
314 mr2 = series_sinhx(g);
316 else
318 mr1 = 1.0;
319 mr2 = 1.0;
322 for(n=start; n<nrend; n++) {
323 if (cFREEZE)
325 gf = cFREEZE[n];
328 for(d=0; d<DIM; d++)
330 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
332 xprime[n][d] = mr1*(mr1*x[n][d]+mr2*dt*v[n][d]);
334 else
336 xprime[n][d] = x[n][d];
340 }/* do_update_vv_pos */
342 static void do_update_visc(int start,int nrend,double dt,
343 t_grp_tcstat *tcstat,real invmass[],double nh_vxi[],
344 unsigned short ptype[],unsigned short cTC[],
345 rvec x[],rvec xprime[],rvec v[],
346 rvec f[],matrix M,matrix box,real
347 cos_accel,real vcos,
348 bool bNH,bool bPR)
350 double imass,w_dt;
351 int gt=0;
352 real vn,vc;
353 real lg,vxi=0,vv;
354 real fac,cosz;
355 rvec vrel;
356 int n,d;
358 fac = 2*M_PI/(box[ZZ][ZZ]);
360 if (bNH || bPR) {
361 /* Update with coupling to extended ensembles, used for
362 * Nose-Hoover and Parrinello-Rahman coupling
364 for(n=start; n<nrend; n++) {
365 imass = invmass[n];
366 if (cTC)
368 gt = cTC[n];
370 lg = tcstat[gt].lambda;
371 cosz = cos(fac*x[n][ZZ]);
373 copy_rvec(v[n],vrel);
375 vc = cosz*vcos;
376 vrel[XX] -= vc;
377 if (bNH)
379 vxi = nh_vxi[gt];
381 for(d=0; d<DIM; d++)
383 vn = v[n][d];
385 if((ptype[n] != eptVSite) && (ptype[n] != eptShell))
387 vn = (lg*vrel[d] + dt*(imass*f[n][d] - 0.5*vxi*vrel[d]
388 - iprod(M[d],vrel)))/(1 + 0.5*vxi*dt);
389 if(d == XX)
391 vn += vc + dt*cosz*cos_accel;
393 v[n][d] = vn;
394 xprime[n][d] = x[n][d]+vn*dt;
396 else
398 xprime[n][d] = x[n][d];
403 else
405 /* Classic version of update, used with berendsen coupling */
406 for(n=start; n<nrend; n++)
408 w_dt = invmass[n]*dt;
409 if (cTC)
411 gt = cTC[n];
413 lg = tcstat[gt].lambda;
414 cosz = cos(fac*x[n][ZZ]);
416 for(d=0; d<DIM; d++)
418 vn = v[n][d];
420 if((ptype[n] != eptVSite) && (ptype[n] != eptShell))
422 if(d == XX)
424 vc = cosz*vcos;
425 /* Do not scale the cosine velocity profile */
426 vv = vc + lg*(vn - vc + f[n][d]*w_dt);
427 /* Add the cosine accelaration profile */
428 vv += dt*cosz*cos_accel;
430 else
432 vv = lg*(vn + f[n][d]*w_dt);
434 v[n][d] = vv;
435 xprime[n][d] = x[n][d]+vv*dt;
437 else
439 v[n][d] = 0.0;
440 xprime[n][d] = x[n][d];
447 static gmx_stochd_t *init_stochd(FILE *fplog,t_inputrec *ir)
449 gmx_stochd_t *sd;
450 gmx_sd_const_t *sdc;
451 int ngtc,n;
452 real y;
454 snew(sd,1);
456 /* Initiate random number generator for langevin type dynamics,
457 * for BD, SD or velocity rescaling temperature coupling.
459 sd->gaussrand = gmx_rng_init(ir->ld_seed);
461 ngtc = ir->opts.ngtc;
463 if (ir->eI == eiBD) {
464 snew(sd->bd_rf,ngtc);
465 } else if (EI_SD(ir->eI)) {
466 snew(sd->sdc,ngtc);
467 snew(sd->sdsig,ngtc);
469 sdc = sd->sdc;
470 for(n=0; n<ngtc; n++) {
471 sdc[n].gdt = ir->delta_t/ir->opts.tau_t[n];
472 sdc[n].eph = exp(sdc[n].gdt/2);
473 sdc[n].emh = exp(-sdc[n].gdt/2);
474 sdc[n].em = exp(-sdc[n].gdt);
475 if (sdc[n].gdt >= 0.05) {
476 sdc[n].b = sdc[n].gdt*(sdc[n].eph*sdc[n].eph - 1)
477 - 4*(sdc[n].eph - 1)*(sdc[n].eph - 1);
478 sdc[n].c = sdc[n].gdt - 3 + 4*sdc[n].emh - sdc[n].em;
479 sdc[n].d = 2 - sdc[n].eph - sdc[n].emh;
480 } else {
481 y = sdc[n].gdt/2;
482 /* Seventh order expansions for small y */
483 sdc[n].b = y*y*y*y*(1/3.0+y*(1/3.0+y*(17/90.0+y*7/9.0)));
484 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))));
485 sdc[n].d = y*y*(-1+y*y*(-1/12.0-y*y/360.0));
487 if(debug)
488 fprintf(debug,"SD const tc-grp %d: b %g c %g d %g\n",
489 n,sdc[n].b,sdc[n].c,sdc[n].d);
493 return sd;
496 void get_stochd_state(gmx_update_t upd,t_state *state)
498 gmx_rng_get_state(upd->sd->gaussrand,state->ld_rng,state->ld_rngi);
501 void set_stochd_state(gmx_update_t upd,t_state *state)
503 gmx_rng_set_state(upd->sd->gaussrand,state->ld_rng,state->ld_rngi[0]);
506 gmx_update_t init_update(FILE *fplog,t_inputrec *ir)
508 t_gmx_update *upd;
510 snew(upd,1);
512 if (ir->eI == eiBD || EI_SD(ir->eI) || ir->etc == etcVRESCALE)
514 upd->sd = init_stochd(fplog,ir);
517 upd->xp = NULL;
518 upd->xp_nalloc = 0;
520 return upd;
523 static void do_update_sd1(gmx_stochd_t *sd,
524 int start,int homenr,double dt,
525 rvec accel[],ivec nFreeze[],
526 real invmass[],unsigned short ptype[],
527 unsigned short cFREEZE[],unsigned short cACC[],
528 unsigned short cTC[],
529 rvec x[],rvec xprime[],rvec v[],rvec f[],
530 rvec sd_X[],
531 int ngtc,real tau_t[],real ref_t[])
533 gmx_sd_const_t *sdc;
534 gmx_sd_sigma_t *sig;
535 gmx_rng_t gaussrand;
536 real kT;
537 int gf=0,ga=0,gt=0;
538 real ism,sd_V;
539 int n,d;
541 sdc = sd->sdc;
542 sig = sd->sdsig;
543 if (homenr > sd->sd_V_nalloc)
545 sd->sd_V_nalloc = over_alloc_dd(homenr);
546 srenew(sd->sd_V,sd->sd_V_nalloc);
548 gaussrand = sd->gaussrand;
550 for(n=0; n<ngtc; n++)
552 kT = BOLTZ*ref_t[n];
553 /* The mass is encounted for later, since this differs per atom */
554 sig[n].V = sqrt(2*kT*(1 - sdc[n].em));
557 for(n=start; n<start+homenr; n++)
559 ism = sqrt(invmass[n]);
560 if (cFREEZE)
562 gf = cFREEZE[n];
564 if (cACC)
566 ga = cACC[n];
568 if (cTC)
570 gt = cTC[n];
573 for(d=0; d<DIM; d++)
575 if((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
577 sd_V = ism*sig[gt].V*gmx_rng_gaussian_table(gaussrand);
579 v[n][d] = v[n][d]*sdc[gt].em
580 + (invmass[n]*f[n][d] + accel[ga][d])*tau_t[gt]*(1 - sdc[gt].em)
581 + sd_V;
583 xprime[n][d] = x[n][d] + v[n][d]*dt;
585 else
587 v[n][d] = 0.0;
588 xprime[n][d] = x[n][d];
594 static void do_update_sd2(gmx_stochd_t *sd,bool bInitStep,
595 int start,int homenr,
596 rvec accel[],ivec nFreeze[],
597 real invmass[],unsigned short ptype[],
598 unsigned short cFREEZE[],unsigned short cACC[],
599 unsigned short cTC[],
600 rvec x[],rvec xprime[],rvec v[],rvec f[],
601 rvec sd_X[],
602 int ngtc,real tau_t[],real ref_t[],
603 bool bFirstHalf)
605 gmx_sd_const_t *sdc;
606 gmx_sd_sigma_t *sig;
607 /* The random part of the velocity update, generated in the first
608 * half of the update, needs to be remembered for the second half.
610 rvec *sd_V;
611 gmx_rng_t gaussrand;
612 real kT;
613 int gf=0,ga=0,gt=0;
614 real vn=0,Vmh,Xmh;
615 real ism;
616 int n,d;
618 sdc = sd->sdc;
619 sig = sd->sdsig;
620 if (homenr > sd->sd_V_nalloc)
622 sd->sd_V_nalloc = over_alloc_dd(homenr);
623 srenew(sd->sd_V,sd->sd_V_nalloc);
625 sd_V = sd->sd_V;
626 gaussrand = sd->gaussrand;
628 if (bFirstHalf)
630 for (n=0; n<ngtc; n++)
632 kT = BOLTZ*ref_t[n];
633 /* The mass is encounted for later, since this differs per atom */
634 sig[n].V = sqrt(kT*(1-sdc[n].em));
635 sig[n].X = sqrt(kT*sqr(tau_t[n])*sdc[n].c);
636 sig[n].Yv = sqrt(kT*sdc[n].b/sdc[n].c);
637 sig[n].Yx = sqrt(kT*sqr(tau_t[n])*sdc[n].b/(1-sdc[n].em));
641 for (n=start; n<start+homenr; n++)
643 ism = sqrt(invmass[n]);
644 if (cFREEZE)
646 gf = cFREEZE[n];
648 if (cACC)
650 ga = cACC[n];
652 if (cTC)
654 gt = cTC[n];
657 for(d=0; d<DIM; d++)
659 if (bFirstHalf)
661 vn = v[n][d];
663 if((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
665 if (bFirstHalf)
667 if (bInitStep)
669 sd_X[n][d] = ism*sig[gt].X*gmx_rng_gaussian_table(gaussrand);
671 Vmh = sd_X[n][d]*sdc[gt].d/(tau_t[gt]*sdc[gt].c)
672 + ism*sig[gt].Yv*gmx_rng_gaussian_table(gaussrand);
673 sd_V[n-start][d] = ism*sig[gt].V*gmx_rng_gaussian_table(gaussrand);
675 v[n][d] = vn*sdc[gt].em
676 + (invmass[n]*f[n][d] + accel[ga][d])*tau_t[gt]*(1 - sdc[gt].em)
677 + sd_V[n-start][d] - sdc[gt].em*Vmh;
679 xprime[n][d] = x[n][d] + v[n][d]*tau_t[gt]*(sdc[gt].eph - sdc[gt].emh);
681 else
684 /* Correct the velocities for the constraints.
685 * This operation introduces some inaccuracy,
686 * since the velocity is determined from differences in coordinates.
688 v[n][d] =
689 (xprime[n][d] - x[n][d])/(tau_t[gt]*(sdc[gt].eph - sdc[gt].emh));
691 Xmh = sd_V[n-start][d]*tau_t[gt]*sdc[gt].d/(sdc[gt].em-1)
692 + ism*sig[gt].Yx*gmx_rng_gaussian_table(gaussrand);
693 sd_X[n][d] = ism*sig[gt].X*gmx_rng_gaussian_table(gaussrand);
695 xprime[n][d] += sd_X[n][d] - Xmh;
699 else
701 if (bFirstHalf)
703 v[n][d] = 0.0;
704 xprime[n][d] = x[n][d];
711 static void do_update_bd(int start,int nrend,double dt,
712 ivec nFreeze[],
713 real invmass[],unsigned short ptype[],
714 unsigned short cFREEZE[],unsigned short cTC[],
715 rvec x[],rvec xprime[],rvec v[],
716 rvec f[],real friction_coefficient,
717 int ngtc,real tau_t[],real ref_t[],
718 real *rf,gmx_rng_t gaussrand)
720 /* note -- these appear to be full step velocities . . . */
721 int gf=0,gt=0;
722 real vn;
723 real invfr=0;
724 int n,d;
726 if (friction_coefficient != 0)
728 invfr = 1.0/friction_coefficient;
729 for(n=0; n<ngtc; n++)
731 rf[n] = sqrt(2.0*BOLTZ*ref_t[n]/(friction_coefficient*dt));
734 else
736 for(n=0; n<ngtc; n++)
738 rf[n] = sqrt(2.0*BOLTZ*ref_t[n]);
741 for(n=start; (n<nrend); n++)
743 if (cFREEZE)
745 gf = cFREEZE[n];
747 if (cTC)
749 gt = cTC[n];
751 for(d=0; (d<DIM); d++)
753 if((ptype[n]!=eptVSite) && (ptype[n]!=eptShell) && !nFreeze[gf][d])
755 if (friction_coefficient != 0) {
756 vn = invfr*f[n][d] + rf[gt]*gmx_rng_gaussian_table(gaussrand);
758 else
760 /* NOTE: invmass = 1/(mass*friction_constant*dt) */
761 vn = invmass[n]*f[n][d]*dt
762 + sqrt(invmass[n])*rf[gt]*gmx_rng_gaussian_table(gaussrand);
764 v[n][d] = vn;
765 xprime[n][d] = x[n][d]+vn*dt;
768 else
770 v[n][d] = 0.0;
771 xprime[n][d] = x[n][d];
777 static void dump_it_all(FILE *fp,const char *title,
778 int natoms,rvec x[],rvec xp[],rvec v[],rvec f[])
780 #ifdef DEBUG
781 if (fp)
783 fprintf(fp,"%s\n",title);
784 pr_rvecs(fp,0,"x",x,natoms);
785 pr_rvecs(fp,0,"xp",xp,natoms);
786 pr_rvecs(fp,0,"v",v,natoms);
787 pr_rvecs(fp,0,"f",f,natoms);
789 #endif
792 static void calc_ke_part_normal(rvec v[], t_grpopts *opts,t_mdatoms *md,
793 gmx_ekindata_t *ekind,t_nrnb *nrnb,bool bEkinAveVel,
794 bool bSaveEkinOld)
796 int start=md->start,homenr=md->homenr;
797 int g,d,n,m,ga=0,gt=0;
798 rvec v_corrt;
799 real hm;
800 t_grp_tcstat *tcstat=ekind->tcstat;
801 t_grp_acc *grpstat=ekind->grpstat;
802 real dekindl;
804 /* three main: VV with AveVel, vv with AveEkin, leap with AveEkin. Leap with AveVel is also
805 an option, but not supported now. Additionally, if we are doing iterations.
806 bEkinAveVel: If TRUE, we sum into ekin, if FALSE, into ekinh.
807 bSavEkinOld: If TRUE (in the case of iteration = bIterate is TRUE), we don't copy over the ekinh_old.
808 If FALSE, we overrwrite it.
811 /* group velocities are calculated in update_ekindata and
812 * accumulated in acumulate_groups.
813 * Now the partial global and groups ekin.
815 for(g=0; (g<opts->ngtc); g++)
818 if (!bSaveEkinOld) {
819 copy_mat(tcstat[g].ekinh,tcstat[g].ekinh_old);
821 if(bEkinAveVel) {
822 clear_mat(tcstat[g].ekinf);
823 } else {
824 clear_mat(tcstat[g].ekinh);
826 if (bEkinAveVel) {
827 tcstat[g].ekinscalef_nhc = 1.0; /* need to clear this -- logic is complicated! */
830 ekind->dekindl_old = ekind->dekindl;
832 dekindl = 0;
833 for(n=start; (n<start+homenr); n++)
835 if (md->cACC)
837 ga = md->cACC[n];
839 if (md->cTC)
841 gt = md->cTC[n];
843 hm = 0.5*md->massT[n];
845 for(d=0; (d<DIM); d++)
847 v_corrt[d] = v[n][d] - grpstat[ga].u[d];
849 for(d=0; (d<DIM); d++)
851 for (m=0;(m<DIM); m++)
853 /* if we're computing a full step velocity, v_corrt[d] has v(t). Otherwise, v(t+dt/2) */
854 if (bEkinAveVel)
856 tcstat[gt].ekinf[m][d]+=hm*v_corrt[m]*v_corrt[d];
858 else
860 tcstat[gt].ekinh[m][d]+=hm*v_corrt[m]*v_corrt[d];
864 if (md->nMassPerturbed && md->bPerturbed[n])
866 dekindl -= 0.5*(md->massB[n] - md->massA[n])*iprod(v_corrt,v_corrt);
869 ekind->dekindl = dekindl;
870 inc_nrnb(nrnb,eNR_EKIN,homenr);
873 static void calc_ke_part_visc(matrix box,rvec x[],rvec v[],
874 t_grpopts *opts,t_mdatoms *md,
875 gmx_ekindata_t *ekind,
876 t_nrnb *nrnb, bool bEkinAveVel, bool bSaveEkinOld)
878 int start=md->start,homenr=md->homenr;
879 int g,d,n,m,gt=0;
880 rvec v_corrt;
881 real hm;
882 t_grp_tcstat *tcstat=ekind->tcstat;
883 t_cos_acc *cosacc=&(ekind->cosacc);
884 real dekindl;
885 real fac,cosz;
886 double mvcos;
888 for(g=0; g<opts->ngtc; g++)
890 copy_mat(ekind->tcstat[g].ekinh,ekind->tcstat[g].ekinh_old);
891 clear_mat(ekind->tcstat[g].ekinh);
893 ekind->dekindl_old = ekind->dekindl;
895 fac = 2*M_PI/box[ZZ][ZZ];
896 mvcos = 0;
897 dekindl = 0;
898 for(n=start; n<start+homenr; n++)
900 if (md->cTC)
902 gt = md->cTC[n];
904 hm = 0.5*md->massT[n];
906 /* Note that the times of x and v differ by half a step */
907 /* MRS -- would have to be changed for VV */
908 cosz = cos(fac*x[n][ZZ]);
909 /* Calculate the amplitude of the new velocity profile */
910 mvcos += 2*cosz*md->massT[n]*v[n][XX];
912 copy_rvec(v[n],v_corrt);
913 /* Subtract the profile for the kinetic energy */
914 v_corrt[XX] -= cosz*cosacc->vcos;
915 for (d=0; (d<DIM); d++)
917 for (m=0; (m<DIM); m++)
919 /* if we're computing a full step velocity, v_corrt[d] has v(t). Otherwise, v(t+dt/2) */
920 if (bEkinAveVel)
922 tcstat[gt].ekinf[m][d]+=hm*v_corrt[m]*v_corrt[d];
924 else
926 tcstat[gt].ekinh[m][d]+=hm*v_corrt[m]*v_corrt[d];
930 if(md->nPerturbed && md->bPerturbed[n])
932 dekindl -= 0.5*(md->massB[n] - md->massA[n])*iprod(v_corrt,v_corrt);
935 ekind->dekindl = dekindl;
936 cosacc->mvcos = mvcos;
938 inc_nrnb(nrnb,eNR_EKIN,homenr);
941 void calc_ke_part(t_state *state,t_grpopts *opts,t_mdatoms *md,
942 gmx_ekindata_t *ekind,t_nrnb *nrnb, bool bEkinAveVel, bool bSaveEkinOld)
944 if (ekind->cosacc.cos_accel == 0)
946 calc_ke_part_normal(state->v,opts,md,ekind,nrnb,bEkinAveVel,bSaveEkinOld);
948 else
950 calc_ke_part_visc(state->box,state->x,state->v,opts,md,ekind,nrnb,bEkinAveVel,bSaveEkinOld);
954 void init_ekinstate(ekinstate_t *ekinstate,t_inputrec *ir)
956 ekinstate->ekin_n = ir->opts.ngtc;
957 snew(ekinstate->ekinh,ekinstate->ekin_n);
958 snew(ekinstate->ekinf,ekinstate->ekin_n);
959 snew(ekinstate->ekinh_old,ekinstate->ekin_n);
960 snew(ekinstate->ekinscalef_nhc,ekinstate->ekin_n);
961 snew(ekinstate->ekinscaleh_nhc,ekinstate->ekin_n);
962 snew(ekinstate->vscale_nhc,ekinstate->ekin_n);
963 ekinstate->dekindl = 0;
964 ekinstate->mvcos = 0;
967 void update_ekinstate(ekinstate_t *ekinstate,gmx_ekindata_t *ekind)
969 int i;
971 for(i=0;i<ekinstate->ekin_n;i++)
973 copy_mat(ekind->tcstat[i].ekinh,ekinstate->ekinh[i]);
974 copy_mat(ekind->tcstat[i].ekinf,ekinstate->ekinf[i]);
975 copy_mat(ekind->tcstat[i].ekinh_old,ekinstate->ekinh_old[i]);
976 ekinstate->ekinscalef_nhc[i] = ekind->tcstat[i].ekinscalef_nhc;
977 ekinstate->ekinscaleh_nhc[i] = ekind->tcstat[i].ekinscaleh_nhc;
978 ekinstate->vscale_nhc[i] = ekind->tcstat[i].vscale_nhc;
981 copy_mat(ekind->ekin,ekinstate->ekin_total);
982 ekinstate->dekindl = ekind->dekindl;
983 ekinstate->mvcos = ekind->cosacc.mvcos;
987 void restore_ekinstate_from_state(t_commrec *cr,
988 gmx_ekindata_t *ekind,ekinstate_t *ekinstate)
990 int i,n;
992 if (MASTER(cr))
994 for(i=0;i<ekinstate->ekin_n;i++)
996 copy_mat(ekinstate->ekinh[i],ekind->tcstat[i].ekinh);
997 copy_mat(ekinstate->ekinf[i],ekind->tcstat[i].ekinf);
998 copy_mat(ekinstate->ekinh_old[i],ekind->tcstat[i].ekinh_old);
999 ekind->tcstat[i].ekinscalef_nhc = ekinstate->ekinscalef_nhc[i];
1000 ekind->tcstat[i].ekinscaleh_nhc = ekinstate->ekinscaleh_nhc[i];
1001 ekind->tcstat[i].vscale_nhc = ekinstate->vscale_nhc[i];
1004 copy_mat(ekinstate->ekin_total,ekind->ekin);
1006 ekind->dekindl = ekinstate->dekindl;
1007 ekind->cosacc.mvcos = ekinstate->mvcos;
1008 n = ekinstate->ekin_n;
1011 if (PAR(cr))
1013 gmx_bcast(sizeof(n),&n,cr);
1014 for(i=0;i<n;i++)
1016 gmx_bcast(DIM*DIM*sizeof(ekind->tcstat[i].ekinh[0][0]),
1017 ekind->tcstat[i].ekinh[0],cr);
1018 gmx_bcast(DIM*DIM*sizeof(ekind->tcstat[i].ekinf[0][0]),
1019 ekind->tcstat[i].ekinf[0],cr);
1020 gmx_bcast(DIM*DIM*sizeof(ekind->tcstat[i].ekinh_old[0][0]),
1021 ekind->tcstat[i].ekinh_old[0],cr);
1023 gmx_bcast(sizeof(ekind->tcstat[i].ekinscalef_nhc),
1024 &(ekind->tcstat[i].ekinscalef_nhc),cr);
1025 gmx_bcast(sizeof(ekind->tcstat[i].ekinscaleh_nhc),
1026 &(ekind->tcstat[i].ekinscaleh_nhc),cr);
1027 gmx_bcast(sizeof(ekind->tcstat[i].vscale_nhc),
1028 &(ekind->tcstat[i].vscale_nhc),cr);
1030 gmx_bcast(DIM*DIM*sizeof(ekind->ekin[0][0]),
1031 ekind->ekin[0],cr);
1033 gmx_bcast(sizeof(ekind->dekindl),&ekind->dekindl,cr);
1034 gmx_bcast(sizeof(ekind->cosacc.mvcos),&ekind->cosacc.mvcos,cr);
1038 void set_deform_reference_box(gmx_update_t upd,gmx_large_int_t step,matrix box)
1040 upd->deformref_step = step;
1041 copy_mat(box,upd->deformref_box);
1044 static void deform(gmx_update_t upd,
1045 int start,int homenr,rvec x[],matrix box,matrix *scale_tot,
1046 const t_inputrec *ir,gmx_large_int_t step)
1048 matrix bnew,invbox,mu;
1049 real elapsed_time;
1050 int i,j;
1052 elapsed_time = (step + 1 - upd->deformref_step)*ir->delta_t;
1053 copy_mat(box,bnew);
1054 for(i=0; i<DIM; i++)
1056 for(j=0; j<DIM; j++)
1058 if (ir->deform[i][j] != 0)
1060 bnew[i][j] =
1061 upd->deformref_box[i][j] + elapsed_time*ir->deform[i][j];
1065 /* We correct the off-diagonal elements,
1066 * which can grow indefinitely during shearing,
1067 * so the shifts do not get messed up.
1069 for(i=1; i<DIM; i++)
1071 for(j=i-1; j>=0; j--)
1073 while (bnew[i][j] - box[i][j] > 0.5*bnew[j][j])
1075 rvec_dec(bnew[i],bnew[j]);
1077 while (bnew[i][j] - box[i][j] < -0.5*bnew[j][j])
1079 rvec_inc(bnew[i],bnew[j]);
1083 m_inv_ur0(box,invbox);
1084 copy_mat(bnew,box);
1085 mmul_ur0(box,invbox,mu);
1087 for(i=start; i<start+homenr; i++)
1089 x[i][XX] = mu[XX][XX]*x[i][XX]+mu[YY][XX]*x[i][YY]+mu[ZZ][XX]*x[i][ZZ];
1090 x[i][YY] = mu[YY][YY]*x[i][YY]+mu[ZZ][YY]*x[i][ZZ];
1091 x[i][ZZ] = mu[ZZ][ZZ]*x[i][ZZ];
1093 if (*scale_tot)
1095 /* The transposes of the scaling matrices are stored,
1096 * so we need to do matrix multiplication in the inverse order.
1098 mmul_ur0(*scale_tot,mu,*scale_tot);
1102 static void combine_forces(int nstlist,
1103 gmx_constr_t constr,
1104 t_inputrec *ir,t_mdatoms *md,t_idef *idef,
1105 t_commrec *cr,gmx_large_int_t step,t_state *state,
1106 int start,int nrend,
1107 rvec f[],rvec f_lr[],
1108 t_nrnb *nrnb)
1110 int i,d,nm1;
1112 /* f contains the short-range forces + the long range forces
1113 * which are stored separately in f_lr.
1116 if (constr != NULL && !(ir->eConstrAlg == econtSHAKE && ir->epc == epcNO))
1118 /* We need to constrain the LR forces separately,
1119 * because due to the different pre-factor for the SR and LR
1120 * forces in the update algorithm, we can not determine
1121 * the constraint force for the coordinate constraining.
1122 * Constrain only the additional LR part of the force.
1124 /* MRS -- need to make sure this works with trotter integration -- the constraint calls may not be right.*/
1125 constrain(NULL,FALSE,FALSE,constr,idef,ir,NULL,cr,step,0,md,
1126 state->x,f_lr,f_lr,state->box,state->lambda,NULL,
1127 NULL,NULL,nrnb,econqForce,ir->epc==epcMTTK,state->veta,state->veta);
1130 /* Add nstlist-1 times the LR force to the sum of both forces
1131 * and store the result in forces_lr.
1133 nm1 = nstlist - 1;
1134 for(i=start; i<nrend; i++)
1136 for(d=0; d<DIM; d++)
1138 f_lr[i][d] = f[i][d] + nm1*f_lr[i][d];
1143 void update_extended(FILE *fplog,
1144 gmx_large_int_t step,
1145 t_inputrec *inputrec,
1146 t_mdatoms *md,
1147 t_state *state,
1148 gmx_ekindata_t *ekind,
1149 matrix pcoupl_mu, /* pressure coupling */
1150 matrix M,
1151 gmx_wallcycle_t wcycle,
1152 gmx_update_t upd,
1153 bool bInitStep,
1154 bool bFirstHalf,
1155 t_extmass *MassQ)
1157 bool bExtended,bNH,bPR,bTrotter,bLastStep,bLog=FALSE,bEner=FALSE,bCouple;
1158 double dt;
1159 real dt_1,dtc;
1160 int start,homenr,nrend,i,n,m,g;
1162 start = md->start;
1163 homenr = md->homenr;
1164 nrend = start+homenr;
1166 /* if using vv, we do this elsewhere in the code */
1167 if ((IR_NVT_TROTTER(inputrec)) || (IR_NPT_TROTTER(inputrec)))
1169 return;
1172 /* We should only couple after a step where energies were determined */
1174 bCouple = (inputrec->nstcalcenergy == 1 ||
1175 do_per_step(step+inputrec->nstcalcenergy-1,
1176 inputrec->nstcalcenergy));
1177 dtc = inputrec->nstcalcenergy*inputrec->delta_t;
1179 bNH = (inputrec->etc == etcNOSEHOOVER);
1180 bPR = (inputrec->epc == epcPARRINELLORAHMAN);
1181 bExtended = (bNH || bPR);
1183 dt = inputrec->delta_t;
1184 dt_1 = 1.0/dt;
1185 clear_mat(pcoupl_mu);
1186 for(i=0; i<DIM; i++)
1188 pcoupl_mu[i][i] = 1.0;
1191 clear_mat(M);
1193 if (bCouple)
1195 switch (inputrec->etc)
1197 case etcNO:
1198 break;
1199 case etcBERENDSEN:
1200 berendsen_tcoupl(&(inputrec->opts),ekind,dtc);
1201 break;
1202 case etcNOSEHOOVER:
1203 nosehoover_tcoupl(&(inputrec->opts),ekind,dtc,
1204 state->nosehoover_xi,state->nosehoover_vxi,MassQ);
1205 break;
1206 case etcVRESCALE:
1207 vrescale_tcoupl(&(inputrec->opts),ekind,dtc,
1208 state->therm_integral,upd->sd->gaussrand);
1209 break;
1212 switch (inputrec->epc)
1214 /* We can always pcoupl, even if we did not sum the energies
1215 * the previous step, since state->pres_prev is only updated
1216 * when the energies have been summed.
1218 case (epcNO):
1219 break;
1220 case (epcBERENDSEN):
1221 if (!bInitStep)
1223 berendsen_pcoupl(fplog,step,inputrec,dtc,state->pres_prev,state->box,
1224 pcoupl_mu);
1226 break;
1227 case (epcPARRINELLORAHMAN):
1228 parrinellorahman_pcoupl(fplog,step,inputrec,dtc,state->pres_prev,
1229 state->box,state->box_rel,state->boxv,
1230 M,pcoupl_mu,bInitStep);
1231 break;
1232 default:
1233 break;
1236 else
1238 /* Set the T scaling lambda to 1 to have no scaling */
1239 for(i=0; (i<inputrec->opts.ngtc); i++)
1241 ekind->tcstat[i].lambda = 1.0;
1246 static rvec *get_xprime(const t_state *state,gmx_update_t upd)
1248 if (state->nalloc > upd->xp_nalloc)
1250 upd->xp_nalloc = state->nalloc;
1251 srenew(upd->xp,upd->xp_nalloc);
1254 return upd->xp;
1257 void update_constraints(FILE *fplog,
1258 gmx_large_int_t step,
1259 real *dvdlambda, /* FEP stuff */
1260 t_inputrec *inputrec, /* input record and box stuff */
1261 gmx_ekindata_t *ekind,
1262 t_mdatoms *md,
1263 t_state *state,
1264 t_graph *graph,
1265 rvec force[], /* forces on home particles */
1266 t_idef *idef,
1267 tensor vir_part,
1268 tensor vir, /* tensors for virial and ekin, needed for computing */
1269 t_commrec *cr,
1270 t_nrnb *nrnb,
1271 gmx_wallcycle_t wcycle,
1272 gmx_update_t upd,
1273 gmx_constr_t constr,
1274 bool bInitStep,
1275 bool bFirstHalf,
1276 bool bCalcVir,
1277 real vetanew)
1279 bool bExtended,bTrotter,bLastStep,bLog=FALSE,bEner=FALSE,bDoConstr=FALSE;
1280 double dt;
1281 real dt_1;
1282 int start,homenr,nrend,i,n,m,g,d;
1283 tensor vir_con;
1284 rvec *vbuf,*xprime=NULL;
1286 if (constr) {bDoConstr=TRUE;}
1287 if (bFirstHalf && !EI_VV(inputrec->eI)) {bDoConstr=FALSE;}
1289 /* for now, SD update is here -- though it really seems like it
1290 should be reformulated as a velocity verlet method, since it has two parts */
1292 start = md->start;
1293 homenr = md->homenr;
1294 nrend = start+homenr;
1296 dt = inputrec->delta_t;
1297 dt_1 = 1.0/dt;
1300 * Steps (7C, 8C)
1301 * APPLY CONSTRAINTS:
1302 * BLOCK SHAKE
1304 * When doing PR pressure coupling we have to constrain the
1305 * bonds in each iteration. If we are only using Nose-Hoover tcoupling
1306 * it is enough to do this once though, since the relative velocities
1307 * after this will be normal to the bond vector
1310 if (bDoConstr)
1312 /* clear out constraints before applying */
1313 clear_mat(vir_part);
1315 xprime = get_xprime(state,upd);
1317 bLastStep = (step == inputrec->init_step+inputrec->nsteps);
1318 bLog = (do_per_step(step,inputrec->nstlog) || bLastStep || (step < 0));
1319 bEner = (do_per_step(step,inputrec->nstenergy) || bLastStep);
1320 /* Constrain the coordinates xprime */
1321 wallcycle_start(wcycle,ewcCONSTR);
1322 if (EI_VV(inputrec->eI) && bFirstHalf)
1324 constrain(NULL,bLog,bEner,constr,idef,
1325 inputrec,ekind,cr,step,1,md,
1326 state->x,state->v,state->v,
1327 state->box,state->lambda,dvdlambda,
1328 NULL,bCalcVir ? &vir_con : NULL,nrnb,econqVeloc,
1329 inputrec->epc==epcMTTK,state->veta,vetanew);
1331 else
1333 constrain(NULL,bLog,bEner,constr,idef,
1334 inputrec,ekind,cr,step,1,md,
1335 state->x,xprime,NULL,
1336 state->box,state->lambda,dvdlambda,
1337 state->v,bCalcVir ? &vir_con : NULL ,nrnb,econqCoord,
1338 inputrec->epc==epcMTTK,state->veta,state->veta);
1340 wallcycle_stop(wcycle,ewcCONSTR);
1342 where();
1344 dump_it_all(fplog,"After Shake",
1345 state->natoms,state->x,xprime,state->v,force);
1347 if (bCalcVir)
1349 if (inputrec->eI == eiSD2)
1351 /* A correction factor eph is needed for the SD constraint force */
1352 /* Here we can, unfortunately, not have proper corrections
1353 * for different friction constants, so we use the first one.
1355 for(i=0; i<DIM; i++)
1357 for(m=0; m<DIM; m++)
1359 vir_part[i][m] += upd->sd->sdc[0].eph*vir_con[i][m];
1363 else
1365 m_add(vir_part,vir_con,vir_part);
1367 if (debug)
1369 pr_rvecs(debug,0,"constraint virial",vir_part,DIM);
1374 where();
1375 if ((inputrec->eI == eiSD2) && !(bFirstHalf))
1377 xprime = get_xprime(state,upd);
1379 /* The second part of the SD integration */
1380 do_update_sd2(upd->sd,FALSE,start,homenr,
1381 inputrec->opts.acc,inputrec->opts.nFreeze,
1382 md->invmass,md->ptype,
1383 md->cFREEZE,md->cACC,md->cTC,
1384 state->x,xprime,state->v,force,state->sd_X,
1385 inputrec->opts.ngtc,inputrec->opts.tau_t,
1386 inputrec->opts.ref_t,FALSE);
1387 inc_nrnb(nrnb, eNR_UPDATE, homenr);
1389 if (bDoConstr)
1391 /* Constrain the coordinates xprime */
1392 wallcycle_start(wcycle,ewcCONSTR);
1393 constrain(NULL,bLog,bEner,constr,idef,
1394 inputrec,NULL,cr,step,1,md,
1395 state->x,xprime,NULL,
1396 state->box,state->lambda,dvdlambda,
1397 NULL,NULL,nrnb,econqCoord,FALSE,0,0);
1398 wallcycle_stop(wcycle,ewcCONSTR);
1402 /* We must always unshift after updating coordinates; if we did not shake
1403 x was shifted in do_force */
1405 if (!(bFirstHalf)) /* in the first half of vv, no shift. */
1407 if (graph && (graph->nnodes > 0))
1409 unshift_x(graph,state->box,state->x,upd->xp);
1410 if (TRICLINIC(state->box))
1412 inc_nrnb(nrnb,eNR_SHIFTX,2*graph->nnodes);
1414 else
1416 inc_nrnb(nrnb,eNR_SHIFTX,graph->nnodes);
1418 copy_rvecn(upd->xp,state->x,start,graph->start);
1419 copy_rvecn(upd->xp,state->x,graph->start+graph->nnodes,nrend);
1421 else
1423 copy_rvecn(upd->xp,state->x,start,nrend);
1426 dump_it_all(fplog,"After unshift",
1427 state->natoms,state->x,upd->xp,state->v,force);
1429 /* ############# END the update of velocities and positions ######### */
1432 void update_box(FILE *fplog,
1433 gmx_large_int_t step,
1434 t_inputrec *inputrec, /* input record and box stuff */
1435 t_mdatoms *md,
1436 t_state *state,
1437 t_graph *graph,
1438 rvec force[], /* forces on home particles */
1439 matrix *scale_tot,
1440 matrix pcoupl_mu,
1441 t_nrnb *nrnb,
1442 gmx_wallcycle_t wcycle,
1443 gmx_update_t upd,
1444 bool bInitStep,
1445 bool bFirstHalf)
1447 bool bExtended,bTrotter,bLastStep,bLog=FALSE,bEner=FALSE;
1448 double dt;
1449 real dt_1;
1450 int start,homenr,nrend,i,n,m,g;
1451 tensor vir_con;
1453 start = md->start;
1454 homenr = md->homenr;
1455 nrend = start+homenr;
1457 bExtended =
1458 (inputrec->etc == etcNOSEHOOVER) ||
1459 (inputrec->epc == epcPARRINELLORAHMAN) ||
1460 (inputrec->epc == epcMTTK);
1462 dt = inputrec->delta_t;
1464 where();
1466 /* now update boxes */
1467 switch (inputrec->epc) {
1468 case (epcNO):
1469 break;
1470 case (epcBERENDSEN):
1471 berendsen_pscale(inputrec,pcoupl_mu,state->box,state->box_rel,
1472 start,homenr,state->x,md->cFREEZE,nrnb);
1473 break;
1474 case (epcPARRINELLORAHMAN):
1475 /* The box velocities were updated in do_pr_pcoupl in the update
1476 * iteration, but we dont change the box vectors until we get here
1477 * since we need to be able to shift/unshift above.
1479 for(i=0;i<DIM;i++)
1481 for(m=0;m<=i;m++)
1483 state->box[i][m] += dt*state->boxv[i][m];
1486 preserve_box_shape(inputrec,state->box_rel,state->box);
1488 /* Scale the coordinates */
1489 for(n=start; (n<start+homenr); n++)
1491 tmvmul_ur0(pcoupl_mu,state->x[n],state->x[n]);
1493 break;
1494 case (epcMTTK):
1495 switch (inputrec->epct)
1497 case (epctISOTROPIC):
1498 /* DIM * eta = ln V. so DIM*eta_new = DIM*eta_old + DIM*dt*veta =>
1499 ln V_new = ln V_old + 3*dt*veta => V_new = V_old*exp(3*dt*veta) =>
1500 Side length scales as exp(veta*dt) */
1502 msmul(state->box,exp(state->veta*dt),state->box);
1504 /* Relate veta to boxv. veta = d(eta)/dT = (1/DIM)*1/V dV/dT.
1505 o If we assume isotropic scaling, and box length scaling
1506 factor L, then V = L^DIM (det(M)). So dV/dt = DIM
1507 L^(DIM-1) dL/dt det(M), and veta = (1/L) dL/dt. The
1508 determinant of B is L^DIM det(M), and the determinant
1509 of dB/dt is (dL/dT)^DIM det (M). veta will be
1510 (det(dB/dT)/det(B))^(1/3). Then since M =
1511 B_new*(vol_new)^(1/3), dB/dT_new = (veta_new)*B(new). */
1513 msmul(state->box,state->veta,state->boxv);
1514 break;
1515 default:
1516 break;
1518 break;
1519 default:
1520 break;
1523 if ((!(IR_NPT_TROTTER(inputrec))) && scale_tot)
1525 /* The transposes of the scaling matrices are stored,
1526 * therefore we need to reverse the order in the multiplication.
1528 mmul_ur0(*scale_tot,pcoupl_mu,*scale_tot);
1531 if (DEFORM(*inputrec))
1533 deform(upd,start,homenr,state->x,state->box,scale_tot,inputrec,step);
1535 where();
1536 dump_it_all(fplog,"After update",
1537 state->natoms,state->x,upd->xp,state->v,force);
1540 void update_coords(FILE *fplog,
1541 gmx_large_int_t step,
1542 t_inputrec *inputrec, /* input record and box stuff */
1543 t_mdatoms *md,
1544 t_state *state,
1545 rvec *f, /* forces on home particles */
1546 bool bDoLR,
1547 rvec *f_lr,
1548 t_fcdata *fcd,
1549 gmx_ekindata_t *ekind,
1550 matrix M,
1551 gmx_wallcycle_t wcycle,
1552 gmx_update_t upd,
1553 bool bInitStep,
1554 int UpdatePart,
1555 t_commrec *cr, /* these shouldn't be here -- need to think about it */
1556 t_nrnb *nrnb,
1557 gmx_constr_t constr,
1558 t_idef *idef)
1560 bool bExtended,bNH,bPR,bTrotter,bLastStep,bLog=FALSE,bEner=FALSE;
1561 double dt,alpha;
1562 real *imass,*imassin;
1563 rvec *force;
1564 real dt_1;
1565 int start,homenr,nrend,i,j,d,n,m,g;
1566 int blen0,blen1,iatom,jatom,nshake,nsettle,nconstr,nexpand;
1567 int *icom = NULL;
1568 tensor vir_con;
1569 rvec *vcom,*xcom,*vall,*xall,*xin,*vin,*forcein,*fall,*xpall,*xprimein,*xprime;
1572 /* Running the velocity half does nothing except for velocity verlet */
1573 if (UpdatePart == etrtVELOCITY)
1575 if (!EI_VV(inputrec->eI)) {return;} /*THIS IS FOR STARTING AT v(t=0)*/
1578 start = md->start;
1579 homenr = md->homenr;
1580 nrend = start+homenr;
1582 xprime = get_xprime(state,upd);
1584 dt = inputrec->delta_t;
1585 dt_1 = 1.0/dt;
1587 /* We need to update the NMR restraint history when time averaging is used */
1588 if (state->flags & (1<<estDISRE_RM3TAV))
1590 update_disres_history(fcd,&state->hist);
1592 if (state->flags & (1<<estORIRE_DTAV))
1594 update_orires_history(fcd,&state->hist);
1597 bNH = inputrec->etc == etcNOSEHOOVER;
1598 bPR = ((inputrec->epc == epcPARRINELLORAHMAN) || (inputrec->epc == epcMTTK));
1600 bExtended = bNH || bPR;
1602 if (bDoLR && inputrec->nstlist > 1 && !EI_VV(inputrec->eI)) /* get this working with VV? */
1604 /* Store the total force + nstlist-1 times the LR force
1605 * in forces_lr, so it can be used in a normal update algorithm
1606 * to produce twin time stepping.
1608 /* is this correct in the new construction? MRS */
1609 combine_forces(inputrec->nstlist,constr,inputrec,md,idef,cr,step,state,
1610 start,nrend,f,f_lr,nrnb);
1611 force = f_lr;
1613 else
1615 force = f;
1618 /* ############# START The update of velocities and positions ######### */
1619 where();
1620 dump_it_all(fplog,"Before update",
1621 state->natoms,state->x,xprime,state->v,force);
1623 switch (inputrec->eI) {
1624 case (eiMD):
1625 if (ekind->cosacc.cos_accel == 0) {
1626 /* use normal version of update */
1627 do_update_md(start,nrend,dt,
1628 ekind->tcstat,ekind->grpstat,state->nosehoover_vxi,
1629 inputrec->opts.acc,inputrec->opts.nFreeze,md->invmass,md->ptype,
1630 md->cFREEZE,md->cACC,md->cTC,
1631 state->x,xprime,state->v,force,M,
1632 bNH,bPR);
1634 else
1636 do_update_visc(start,nrend,dt,
1637 ekind->tcstat,md->invmass,state->nosehoover_vxi,
1638 md->ptype,md->cTC,state->x,xprime,state->v,force,M,
1639 state->box,ekind->cosacc.cos_accel,ekind->cosacc.vcos,bNH,bPR);
1641 break;
1642 case (eiSD1):
1643 do_update_sd1(upd->sd,start,homenr,dt,
1644 inputrec->opts.acc,inputrec->opts.nFreeze,
1645 md->invmass,md->ptype,
1646 md->cFREEZE,md->cACC,md->cTC,
1647 state->x,xprime,state->v,force,state->sd_X,
1648 inputrec->opts.ngtc,inputrec->opts.tau_t,inputrec->opts.ref_t);
1649 break;
1650 case (eiSD2):
1651 /* The SD update is done in 2 parts, because an extra constraint step
1652 * is needed
1654 do_update_sd2(upd->sd,bInitStep,start,homenr,
1655 inputrec->opts.acc,inputrec->opts.nFreeze,
1656 md->invmass,md->ptype,
1657 md->cFREEZE,md->cACC,md->cTC,
1658 state->x,xprime,state->v,force,state->sd_X,
1659 inputrec->opts.ngtc,inputrec->opts.tau_t,inputrec->opts.ref_t,
1660 TRUE);
1661 break;
1662 case (eiBD):
1663 do_update_bd(start,nrend,dt,
1664 inputrec->opts.nFreeze,md->invmass,md->ptype,
1665 md->cFREEZE,md->cTC,
1666 state->x,xprime,state->v,force,
1667 inputrec->bd_fric,
1668 inputrec->opts.ngtc,inputrec->opts.tau_t,inputrec->opts.ref_t,
1669 upd->sd->bd_rf,upd->sd->gaussrand);
1670 break;
1671 case (eiVV):
1672 case (eiVVAK):
1673 alpha = 1.0 + DIM/((double)inputrec->opts.nrdf[0]); /* assuming barostat coupled to group 0. */
1674 switch (UpdatePart) {
1675 case (etrtVELOCITY):
1676 do_update_vv_vel(start,nrend,dt,
1677 ekind->tcstat,ekind->grpstat,
1678 inputrec->opts.acc,inputrec->opts.nFreeze,
1679 md->invmass,md->ptype,
1680 md->cFREEZE,md->cACC,md->cTC,
1681 state->v,force,bExtended,state->veta,alpha);
1682 break;
1683 case (etrtPOSITION):
1684 do_update_vv_pos(start,nrend,dt,
1685 ekind->tcstat,ekind->grpstat,
1686 inputrec->opts.acc,inputrec->opts.nFreeze,
1687 md->invmass,md->ptype,
1688 md->cFREEZE,md->cACC,md->cTC,
1689 state->x,xprime,state->v,force,
1690 bExtended,state->veta,alpha);
1691 break;
1693 break;
1694 default:
1695 gmx_fatal(FARGS,"Don't know how to update coordinates");
1696 break;
1701 void correct_ekin(FILE *log,int start,int end,rvec v[],rvec vcm,real mass[],
1702 real tmass,tensor ekin)
1705 * This is a debugging routine. It should not be called for production code
1707 * The kinetic energy should calculated according to:
1708 * Ekin = 1/2 m (v-vcm)^2
1709 * However the correction is not always applied, since vcm may not be
1710 * known in time and we compute
1711 * Ekin' = 1/2 m v^2 instead
1712 * This can be corrected afterwards by computing
1713 * Ekin = Ekin' + 1/2 m ( -2 v vcm + vcm^2)
1714 * or in hsorthand:
1715 * Ekin = Ekin' - m v vcm + 1/2 m vcm^2
1717 int i,j,k;
1718 real m,tm;
1719 rvec hvcm,mv;
1720 tensor dekin;
1722 /* Local particles */
1723 clear_rvec(mv);
1725 /* Processor dependent part. */
1726 tm = 0;
1727 for(i=start; (i<end); i++)
1729 m = mass[i];
1730 tm += m;
1731 for(j=0; (j<DIM); j++)
1733 mv[j] += m*v[i][j];
1736 /* Shortcut */
1737 svmul(1/tmass,vcm,vcm);
1738 svmul(0.5,vcm,hvcm);
1739 clear_mat(dekin);
1740 for(j=0; (j<DIM); j++)
1742 for(k=0; (k<DIM); k++)
1744 dekin[j][k] += vcm[k]*(tm*hvcm[j]-mv[j]);
1747 pr_rvecs(log,0,"dekin",dekin,DIM);
1748 pr_rvecs(log,0," ekin", ekin,DIM);
1749 fprintf(log,"dekin = %g, ekin = %g vcm = (%8.4f %8.4f %8.4f)\n",
1750 trace(dekin),trace(ekin),vcm[XX],vcm[YY],vcm[ZZ]);
1751 fprintf(log,"mv = (%8.4f %8.4f %8.4f)\n",
1752 mv[XX],mv[YY],mv[ZZ]);