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,2015,2016, 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.
46 #include "gromacs/domdec/domdec_struct.h"
47 #include "gromacs/fileio/confio.h"
48 #include "gromacs/gmxlib/network.h"
49 #include "gromacs/gmxlib/nrnb.h"
50 #include "gromacs/listed-forces/disre.h"
51 #include "gromacs/listed-forces/orires.h"
52 #include "gromacs/math/functions.h"
53 #include "gromacs/math/invertmatrix.h"
54 #include "gromacs/math/units.h"
55 #include "gromacs/math/vec.h"
56 #include "gromacs/math/vecdump.h"
57 #include "gromacs/mdlib/constr.h"
58 #include "gromacs/mdlib/force.h"
59 #include "gromacs/mdlib/gmx_omp_nthreads.h"
60 #include "gromacs/mdlib/mdrun.h"
61 #include "gromacs/mdlib/sim_util.h"
62 #include "gromacs/mdlib/tgroup.h"
63 #include "gromacs/mdtypes/commrec.h"
64 #include "gromacs/mdtypes/group.h"
65 #include "gromacs/mdtypes/inputrec.h"
66 #include "gromacs/mdtypes/md_enums.h"
67 #include "gromacs/pbcutil/boxutilities.h"
68 #include "gromacs/pbcutil/mshift.h"
69 #include "gromacs/pbcutil/pbc.h"
70 #include "gromacs/pulling/pull.h"
71 #include "gromacs/random/tabulatednormaldistribution.h"
72 #include "gromacs/random/threefry.h"
73 #include "gromacs/timing/wallcycle.h"
74 #include "gromacs/utility/exceptions.h"
75 #include "gromacs/utility/fatalerror.h"
76 #include "gromacs/utility/futil.h"
77 #include "gromacs/utility/gmxassert.h"
78 #include "gromacs/utility/gmxomp.h"
79 #include "gromacs/utility/smalloc.h"
81 /*For debugging, start at v(-dt/2) for velolcity verlet -- uncomment next line */
82 /*#define STARTFROMDT2*/
97 gmx_sd_sigma_t *sdsig;
98 /* andersen temperature control stuff */
99 gmx_bool *randomize_group;
106 /* xprime for constraint algorithms */
110 /* Variables for the deform algorithm */
111 gmx_int64_t deformref_step;
112 matrix deformref_box;
116 static void do_update_md(int start, int nrend, double dt,
117 t_grp_tcstat *tcstat,
119 gmx_bool bNEMD, t_grp_acc *gstat, rvec accel[],
122 unsigned short ptype[], unsigned short cFREEZE[],
123 unsigned short cACC[], unsigned short cTC[],
124 rvec x[], rvec xprime[], rvec v[],
126 gmx_bool bNH, gmx_bool bPR)
129 int gf = 0, ga = 0, gt = 0;
131 real vn, vv, va, vb, vnrel;
137 /* Update with coupling to extended ensembles, used for
138 * Nose-Hoover and Parrinello-Rahman coupling
139 * Nose-Hoover uses the reversible leap-frog integrator from
140 * Holian et al. Phys Rev E 52(3) : 2338, 1995
142 for (n = start; n < nrend; n++)
157 lg = tcstat[gt].lambda;
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;
173 xprime[n][d] = x[n][d]+vn*dt;
178 xprime[n][d] = x[n][d];
183 else if (cFREEZE != NULL ||
184 nFreeze[0][XX] || nFreeze[0][YY] || nFreeze[0][ZZ] ||
187 /* Update with Berendsen/v-rescale coupling and freeze or NEMD */
188 for (n = start; n < nrend; n++)
190 w_dt = invmass[n]*dt;
203 lg = tcstat[gt].lambda;
205 for (d = 0; d < DIM; d++)
208 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
210 vv = lg*vn + f[n][d]*w_dt;
212 /* do not scale the mean velocities u */
214 va = vv + accel[ga][d]*dt;
215 vb = va + (1.0-lg)*u;
217 xprime[n][d] = x[n][d]+vb*dt;
222 xprime[n][d] = x[n][d];
229 /* Plain update with Berendsen/v-rescale coupling */
230 for (n = start; n < nrend; n++)
232 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell))
234 w_dt = invmass[n]*dt;
239 lg = tcstat[gt].lambda;
241 for (d = 0; d < DIM; d++)
243 vn = lg*v[n][d] + f[n][d]*w_dt;
245 xprime[n][d] = x[n][d] + vn*dt;
250 for (d = 0; d < DIM; d++)
253 xprime[n][d] = x[n][d];
260 static void do_update_vv_vel(int start, int nrend, double dt,
261 rvec accel[], ivec nFreeze[], real invmass[],
262 unsigned short ptype[], unsigned short cFREEZE[],
263 unsigned short cACC[], rvec v[], rvec f[],
264 gmx_bool bExtended, real veta, real alpha)
273 g = 0.25*dt*veta*alpha;
275 mv2 = gmx::series_sinhx(g);
282 for (n = start; n < nrend; n++)
284 w_dt = invmass[n]*dt;
294 for (d = 0; d < DIM; d++)
296 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
298 v[n][d] = mv1*(mv1*v[n][d] + 0.5*(w_dt*mv2*f[n][d]))+0.5*accel[ga][d]*dt;
306 } /* do_update_vv_vel */
308 static void do_update_vv_pos(int start, int nrend, double dt,
310 unsigned short ptype[], unsigned short cFREEZE[],
311 rvec x[], rvec xprime[], rvec v[],
312 gmx_bool bExtended, real veta)
318 /* Would it make more sense if Parrinello-Rahman was put here? */
323 mr2 = gmx::series_sinhx(g);
331 for (n = start; n < nrend; n++)
339 for (d = 0; d < DIM; d++)
341 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
343 xprime[n][d] = mr1*(mr1*x[n][d]+mr2*dt*v[n][d]);
347 xprime[n][d] = x[n][d];
351 } /* do_update_vv_pos */
353 static void do_update_visc(int start, int nrend, double dt,
354 t_grp_tcstat *tcstat,
357 unsigned short ptype[], unsigned short cTC[],
358 rvec x[], rvec xprime[], rvec v[],
359 rvec f[], matrix M, matrix box, real
360 cos_accel, real vcos,
361 gmx_bool bNH, gmx_bool bPR)
366 real lg, vxi = 0, vv;
371 fac = 2*M_PI/(box[ZZ][ZZ]);
375 /* Update with coupling to extended ensembles, used for
376 * Nose-Hoover and Parrinello-Rahman coupling
378 for (n = start; n < nrend; n++)
385 lg = tcstat[gt].lambda;
386 cosz = cos(fac*x[n][ZZ]);
388 copy_rvec(v[n], vrel);
396 for (d = 0; d < DIM; d++)
398 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell))
400 vn = (lg*vrel[d] + dt*(imass*f[n][d] - 0.5*vxi*vrel[d]
401 - iprod(M[d], vrel)))/(1 + 0.5*vxi*dt);
404 vn += vc + dt*cosz*cos_accel;
407 xprime[n][d] = x[n][d]+vn*dt;
411 xprime[n][d] = x[n][d];
418 /* Classic version of update, used with berendsen coupling */
419 for (n = start; n < nrend; n++)
421 w_dt = invmass[n]*dt;
426 lg = tcstat[gt].lambda;
427 cosz = cos(fac*x[n][ZZ]);
429 for (d = 0; d < DIM; d++)
433 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell))
438 /* Do not scale the cosine velocity profile */
439 vv = vc + lg*(vn - vc + f[n][d]*w_dt);
440 /* Add the cosine accelaration profile */
441 vv += dt*cosz*cos_accel;
445 vv = lg*(vn + f[n][d]*w_dt);
448 xprime[n][d] = x[n][d]+vv*dt;
453 xprime[n][d] = x[n][d];
460 static gmx_stochd_t *init_stochd(const t_inputrec *ir)
466 const t_grpopts *opts = &ir->opts;
467 int ngtc = opts->ngtc;
471 snew(sd->bd_rf, ngtc);
473 else if (EI_SD(ir->eI))
476 snew(sd->sdsig, ngtc);
478 gmx_sd_const_t *sdc = sd->sdc;
480 for (int gt = 0; gt < ngtc; gt++)
482 if (opts->tau_t[gt] > 0)
484 sdc[gt].em = exp(-ir->delta_t/opts->tau_t[gt]);
488 /* No friction and noise on this group */
493 else if (ETC_ANDERSEN(ir->etc))
495 snew(sd->randomize_group, ngtc);
496 snew(sd->boltzfac, ngtc);
498 /* for now, assume that all groups, if randomized, are randomized at the same rate, i.e. tau_t is the same. */
499 /* since constraint groups don't necessarily match up with temperature groups! This is checked in readir.c */
501 for (int gt = 0; gt < ngtc; gt++)
503 real reft = std::max<real>(0, opts->ref_t[gt]);
504 if ((opts->tau_t[gt] > 0) && (reft > 0)) /* tau_t or ref_t = 0 means that no randomization is done */
506 sd->randomize_group[gt] = TRUE;
507 sd->boltzfac[gt] = BOLTZ*opts->ref_t[gt];
511 sd->randomize_group[gt] = FALSE;
519 void update_temperature_constants(gmx_update_t *upd, const t_inputrec *ir)
523 if (ir->bd_fric != 0)
525 for (int gt = 0; gt < ir->opts.ngtc; gt++)
527 upd->sd->bd_rf[gt] = std::sqrt(2.0*BOLTZ*ir->opts.ref_t[gt]/(ir->bd_fric*ir->delta_t));
532 for (int gt = 0; gt < ir->opts.ngtc; gt++)
534 upd->sd->bd_rf[gt] = std::sqrt(2.0*BOLTZ*ir->opts.ref_t[gt]);
540 for (int gt = 0; gt < ir->opts.ngtc; gt++)
542 real kT = BOLTZ*ir->opts.ref_t[gt];
543 /* The mass is accounted for later, since this differs per atom */
544 upd->sd->sdsig[gt].V = std::sqrt(kT*(1 - upd->sd->sdc[gt].em*upd->sd->sdc[gt].em));
549 gmx_update_t *init_update(const t_inputrec *ir)
555 if (ir->eI == eiBD || EI_SD(ir->eI) || ir->etc == etcVRESCALE || ETC_ANDERSEN(ir->etc))
557 upd->sd = init_stochd(ir);
560 update_temperature_constants(upd, ir);
568 void update_realloc(gmx_update_t *upd, int state_nalloc)
570 GMX_ASSERT(upd, "upd must be allocated before its fields can be reallocated");
571 if (state_nalloc > upd->xp_nalloc)
573 upd->xp_nalloc = state_nalloc;
574 /* We need to allocate one element extra, since we might use
575 * (unaligned) 4-wide SIMD loads to access rvec entries. */
576 srenew(upd->xp, upd->xp_nalloc + 1);
580 static void do_update_sd1(gmx_stochd_t *sd,
581 int start, int nrend, double dt,
582 rvec accel[], ivec nFreeze[],
583 real invmass[], unsigned short ptype[],
584 unsigned short cFREEZE[], unsigned short cACC[],
585 unsigned short cTC[],
586 rvec x[], rvec xprime[], rvec v[], rvec f[],
588 gmx_bool bFirstHalfConstr,
589 gmx_int64_t step, int seed, int* gatindex)
593 int gf = 0, ga = 0, gt = 0;
597 // Even 0 bits internal counter gives 2x64 ints (more than enough for three table lookups)
598 gmx::ThreeFry2x64<0> rng(seed, gmx::RandomDomain::UpdateCoordinates);
599 gmx::TabulatedNormalDistribution<real, 14> dist;
606 for (n = start; n < nrend; n++)
608 int ng = gatindex ? gatindex[n] : n;
610 rng.restart(step, ng);
613 ism = std::sqrt(invmass[n]);
628 for (d = 0; d < DIM; d++)
630 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
634 sd_V = ism*sig[gt].V*dist(rng);
635 vn = v[n][d] + (invmass[n]*f[n][d] + accel[ga][d])*dt;
636 v[n][d] = vn*sdc[gt].em + sd_V;
637 /* Here we include half of the friction+noise
638 * update of v into the integration of x.
640 xprime[n][d] = x[n][d] + 0.5*(vn + v[n][d])*dt;
645 xprime[n][d] = x[n][d];
652 /* We do have constraints */
653 if (bFirstHalfConstr)
655 /* First update without friction and noise */
658 for (n = start; n < nrend; n++)
671 for (d = 0; d < DIM; d++)
673 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
675 v[n][d] = v[n][d] + (im*f[n][d] + accel[ga][d])*dt;
676 xprime[n][d] = x[n][d] + v[n][d]*dt;
681 xprime[n][d] = x[n][d];
688 /* Update friction and noise only */
689 for (n = start; n < nrend; n++)
691 int ng = gatindex ? gatindex[n] : n;
693 rng.restart(step, ng);
696 ism = std::sqrt(invmass[n]);
707 for (d = 0; d < DIM; d++)
709 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
713 sd_V = ism*sig[gt].V*dist(rng);
715 v[n][d] = vn*sdc[gt].em + sd_V;
716 /* Add the friction and noise contribution only */
717 xprime[n][d] = xprime[n][d] + 0.5*(v[n][d] - vn)*dt;
725 static void do_update_bd(int start, int nrend, double dt,
727 real invmass[], unsigned short ptype[],
728 unsigned short cFREEZE[], unsigned short cTC[],
729 rvec x[], rvec xprime[], rvec v[],
730 rvec f[], real friction_coefficient,
731 real *rf, gmx_int64_t step, int seed,
734 /* note -- these appear to be full step velocities . . . */
739 // Use 1 bit of internal counters to give us 2*2 64-bits values per stream
740 // Each 64-bit value is enough for 4 normal distribution table numbers.
741 gmx::ThreeFry2x64<0> rng(seed, gmx::RandomDomain::UpdateCoordinates);
742 gmx::TabulatedNormalDistribution<real, 14> dist;
744 if (friction_coefficient != 0)
746 invfr = 1.0/friction_coefficient;
749 for (n = start; (n < nrend); n++)
751 int ng = gatindex ? gatindex[n] : n;
753 rng.restart(step, ng);
764 for (d = 0; (d < DIM); d++)
766 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
768 if (friction_coefficient != 0)
770 vn = invfr*f[n][d] + rf[gt]*dist(rng);
774 /* NOTE: invmass = 2/(mass*friction_constant*dt) */
775 vn = 0.5*invmass[n]*f[n][d]*dt
776 + std::sqrt(0.5*invmass[n])*rf[gt]*dist(rng);
780 xprime[n][d] = x[n][d]+vn*dt;
785 xprime[n][d] = x[n][d];
791 static void dump_it_all(FILE gmx_unused *fp, const char gmx_unused *title,
792 int gmx_unused natoms, rvec gmx_unused x[], rvec gmx_unused xp[],
793 rvec gmx_unused v[], rvec gmx_unused f[])
798 fprintf(fp, "%s\n", title);
799 pr_rvecs(fp, 0, "x", x, natoms);
800 pr_rvecs(fp, 0, "xp", xp, natoms);
801 pr_rvecs(fp, 0, "v", v, natoms);
802 pr_rvecs(fp, 0, "f", f, natoms);
807 static void calc_ke_part_normal(rvec v[], t_grpopts *opts, t_mdatoms *md,
808 gmx_ekindata_t *ekind, t_nrnb *nrnb, gmx_bool bEkinAveVel)
811 t_grp_tcstat *tcstat = ekind->tcstat;
812 t_grp_acc *grpstat = ekind->grpstat;
815 /* three main: VV with AveVel, vv with AveEkin, leap with AveEkin. Leap with AveVel is also
816 an option, but not supported now.
817 bEkinAveVel: If TRUE, we sum into ekin, if FALSE, into ekinh.
820 /* group velocities are calculated in update_ekindata and
821 * accumulated in acumulate_groups.
822 * Now the partial global and groups ekin.
824 for (g = 0; (g < opts->ngtc); g++)
826 copy_mat(tcstat[g].ekinh, tcstat[g].ekinh_old);
829 clear_mat(tcstat[g].ekinf);
830 tcstat[g].ekinscalef_nhc = 1.0; /* need to clear this -- logic is complicated! */
834 clear_mat(tcstat[g].ekinh);
837 ekind->dekindl_old = ekind->dekindl;
838 nthread = gmx_omp_nthreads_get(emntUpdate);
840 #pragma omp parallel for num_threads(nthread) schedule(static)
841 for (thread = 0; thread < nthread; thread++)
843 // This OpenMP only loops over arrays and does not call any functions
844 // or memory allocation. It should not be able to throw, so for now
845 // we do not need a try/catch wrapper.
846 int start_t, end_t, n;
854 start_t = ((thread+0)*md->homenr)/nthread;
855 end_t = ((thread+1)*md->homenr)/nthread;
857 ekin_sum = ekind->ekin_work[thread];
858 dekindl_sum = ekind->dekindl_work[thread];
860 for (gt = 0; gt < opts->ngtc; gt++)
862 clear_mat(ekin_sum[gt]);
868 for (n = start_t; n < end_t; n++)
878 hm = 0.5*md->massT[n];
880 for (d = 0; (d < DIM); d++)
882 v_corrt[d] = v[n][d] - grpstat[ga].u[d];
884 for (d = 0; (d < DIM); d++)
886 for (m = 0; (m < DIM); m++)
888 /* if we're computing a full step velocity, v_corrt[d] has v(t). Otherwise, v(t+dt/2) */
889 ekin_sum[gt][m][d] += hm*v_corrt[m]*v_corrt[d];
892 if (md->nMassPerturbed && md->bPerturbed[n])
895 0.5*(md->massB[n] - md->massA[n])*iprod(v_corrt, v_corrt);
901 for (thread = 0; thread < nthread; thread++)
903 for (g = 0; g < opts->ngtc; g++)
907 m_add(tcstat[g].ekinf, ekind->ekin_work[thread][g],
912 m_add(tcstat[g].ekinh, ekind->ekin_work[thread][g],
917 ekind->dekindl += *ekind->dekindl_work[thread];
920 inc_nrnb(nrnb, eNR_EKIN, md->homenr);
923 static void calc_ke_part_visc(matrix box, rvec x[], rvec v[],
924 t_grpopts *opts, t_mdatoms *md,
925 gmx_ekindata_t *ekind,
926 t_nrnb *nrnb, gmx_bool bEkinAveVel)
928 int start = 0, homenr = md->homenr;
929 int g, d, n, m, gt = 0;
932 t_grp_tcstat *tcstat = ekind->tcstat;
933 t_cos_acc *cosacc = &(ekind->cosacc);
938 for (g = 0; g < opts->ngtc; g++)
940 copy_mat(ekind->tcstat[g].ekinh, ekind->tcstat[g].ekinh_old);
941 clear_mat(ekind->tcstat[g].ekinh);
943 ekind->dekindl_old = ekind->dekindl;
945 fac = 2*M_PI/box[ZZ][ZZ];
948 for (n = start; n < start+homenr; n++)
954 hm = 0.5*md->massT[n];
956 /* Note that the times of x and v differ by half a step */
957 /* MRS -- would have to be changed for VV */
958 cosz = cos(fac*x[n][ZZ]);
959 /* Calculate the amplitude of the new velocity profile */
960 mvcos += 2*cosz*md->massT[n]*v[n][XX];
962 copy_rvec(v[n], v_corrt);
963 /* Subtract the profile for the kinetic energy */
964 v_corrt[XX] -= cosz*cosacc->vcos;
965 for (d = 0; (d < DIM); d++)
967 for (m = 0; (m < DIM); m++)
969 /* if we're computing a full step velocity, v_corrt[d] has v(t). Otherwise, v(t+dt/2) */
972 tcstat[gt].ekinf[m][d] += hm*v_corrt[m]*v_corrt[d];
976 tcstat[gt].ekinh[m][d] += hm*v_corrt[m]*v_corrt[d];
980 if (md->nPerturbed && md->bPerturbed[n])
982 /* The minus sign here might be confusing.
983 * The kinetic contribution from dH/dl doesn't come from
984 * d m(l)/2 v^2 / dl, but rather from d p^2/2m(l) / dl,
985 * where p are the momenta. The difference is only a minus sign.
987 dekindl -= 0.5*(md->massB[n] - md->massA[n])*iprod(v_corrt, v_corrt);
990 ekind->dekindl = dekindl;
991 cosacc->mvcos = mvcos;
993 inc_nrnb(nrnb, eNR_EKIN, homenr);
996 void calc_ke_part(t_state *state, t_grpopts *opts, t_mdatoms *md,
997 gmx_ekindata_t *ekind, t_nrnb *nrnb, gmx_bool bEkinAveVel)
999 if (ekind->cosacc.cos_accel == 0)
1001 calc_ke_part_normal(state->v, opts, md, ekind, nrnb, bEkinAveVel);
1005 calc_ke_part_visc(state->box, state->x, state->v, opts, md, ekind, nrnb, bEkinAveVel);
1009 extern void init_ekinstate(ekinstate_t *ekinstate, const t_inputrec *ir)
1011 ekinstate->ekin_n = ir->opts.ngtc;
1012 snew(ekinstate->ekinh, ekinstate->ekin_n);
1013 snew(ekinstate->ekinf, ekinstate->ekin_n);
1014 snew(ekinstate->ekinh_old, ekinstate->ekin_n);
1015 snew(ekinstate->ekinscalef_nhc, ekinstate->ekin_n);
1016 snew(ekinstate->ekinscaleh_nhc, ekinstate->ekin_n);
1017 snew(ekinstate->vscale_nhc, ekinstate->ekin_n);
1018 ekinstate->dekindl = 0;
1019 ekinstate->mvcos = 0;
1022 void update_ekinstate(ekinstate_t *ekinstate, gmx_ekindata_t *ekind)
1026 for (i = 0; i < ekinstate->ekin_n; i++)
1028 copy_mat(ekind->tcstat[i].ekinh, ekinstate->ekinh[i]);
1029 copy_mat(ekind->tcstat[i].ekinf, ekinstate->ekinf[i]);
1030 copy_mat(ekind->tcstat[i].ekinh_old, ekinstate->ekinh_old[i]);
1031 ekinstate->ekinscalef_nhc[i] = ekind->tcstat[i].ekinscalef_nhc;
1032 ekinstate->ekinscaleh_nhc[i] = ekind->tcstat[i].ekinscaleh_nhc;
1033 ekinstate->vscale_nhc[i] = ekind->tcstat[i].vscale_nhc;
1036 copy_mat(ekind->ekin, ekinstate->ekin_total);
1037 ekinstate->dekindl = ekind->dekindl;
1038 ekinstate->mvcos = ekind->cosacc.mvcos;
1042 void restore_ekinstate_from_state(t_commrec *cr,
1043 gmx_ekindata_t *ekind, const ekinstate_t *ekinstate)
1049 for (i = 0; i < ekinstate->ekin_n; i++)
1051 copy_mat(ekinstate->ekinh[i], ekind->tcstat[i].ekinh);
1052 copy_mat(ekinstate->ekinf[i], ekind->tcstat[i].ekinf);
1053 copy_mat(ekinstate->ekinh_old[i], ekind->tcstat[i].ekinh_old);
1054 ekind->tcstat[i].ekinscalef_nhc = ekinstate->ekinscalef_nhc[i];
1055 ekind->tcstat[i].ekinscaleh_nhc = ekinstate->ekinscaleh_nhc[i];
1056 ekind->tcstat[i].vscale_nhc = ekinstate->vscale_nhc[i];
1059 copy_mat(ekinstate->ekin_total, ekind->ekin);
1061 ekind->dekindl = ekinstate->dekindl;
1062 ekind->cosacc.mvcos = ekinstate->mvcos;
1063 n = ekinstate->ekin_n;
1068 gmx_bcast(sizeof(n), &n, cr);
1069 for (i = 0; i < n; i++)
1071 gmx_bcast(DIM*DIM*sizeof(ekind->tcstat[i].ekinh[0][0]),
1072 ekind->tcstat[i].ekinh[0], cr);
1073 gmx_bcast(DIM*DIM*sizeof(ekind->tcstat[i].ekinf[0][0]),
1074 ekind->tcstat[i].ekinf[0], cr);
1075 gmx_bcast(DIM*DIM*sizeof(ekind->tcstat[i].ekinh_old[0][0]),
1076 ekind->tcstat[i].ekinh_old[0], cr);
1078 gmx_bcast(sizeof(ekind->tcstat[i].ekinscalef_nhc),
1079 &(ekind->tcstat[i].ekinscalef_nhc), cr);
1080 gmx_bcast(sizeof(ekind->tcstat[i].ekinscaleh_nhc),
1081 &(ekind->tcstat[i].ekinscaleh_nhc), cr);
1082 gmx_bcast(sizeof(ekind->tcstat[i].vscale_nhc),
1083 &(ekind->tcstat[i].vscale_nhc), cr);
1085 gmx_bcast(DIM*DIM*sizeof(ekind->ekin[0][0]),
1086 ekind->ekin[0], cr);
1088 gmx_bcast(sizeof(ekind->dekindl), &ekind->dekindl, cr);
1089 gmx_bcast(sizeof(ekind->cosacc.mvcos), &ekind->cosacc.mvcos, cr);
1093 void set_deform_reference_box(gmx_update_t *upd, gmx_int64_t step, matrix box)
1095 upd->deformref_step = step;
1096 copy_mat(box, upd->deformref_box);
1099 static void deform(gmx_update_t *upd,
1100 int start, int homenr, rvec x[], matrix box,
1101 const t_inputrec *ir, gmx_int64_t step)
1103 matrix bnew, invbox, mu;
1107 elapsed_time = (step + 1 - upd->deformref_step)*ir->delta_t;
1108 copy_mat(box, bnew);
1109 for (i = 0; i < DIM; i++)
1111 for (j = 0; j < DIM; j++)
1113 if (ir->deform[i][j] != 0)
1116 upd->deformref_box[i][j] + elapsed_time*ir->deform[i][j];
1120 /* We correct the off-diagonal elements,
1121 * which can grow indefinitely during shearing,
1122 * so the shifts do not get messed up.
1124 for (i = 1; i < DIM; i++)
1126 for (j = i-1; j >= 0; j--)
1128 while (bnew[i][j] - box[i][j] > 0.5*bnew[j][j])
1130 rvec_dec(bnew[i], bnew[j]);
1132 while (bnew[i][j] - box[i][j] < -0.5*bnew[j][j])
1134 rvec_inc(bnew[i], bnew[j]);
1138 gmx::invertBoxMatrix(box, invbox);
1139 copy_mat(bnew, box);
1140 mmul_ur0(box, invbox, mu);
1142 for (i = start; i < start+homenr; i++)
1144 x[i][XX] = mu[XX][XX]*x[i][XX]+mu[YY][XX]*x[i][YY]+mu[ZZ][XX]*x[i][ZZ];
1145 x[i][YY] = mu[YY][YY]*x[i][YY]+mu[ZZ][YY]*x[i][ZZ];
1146 x[i][ZZ] = mu[ZZ][ZZ]*x[i][ZZ];
1150 void update_tcouple(gmx_int64_t step,
1151 t_inputrec *inputrec,
1153 gmx_ekindata_t *ekind,
1158 gmx_bool bTCouple = FALSE;
1162 /* if using vv with trotter decomposition methods, we do this elsewhere in the code */
1163 if (inputrec->etc != etcNO &&
1164 !(inputrecNvtTrotter(inputrec) || inputrecNptTrotter(inputrec) || inputrecNphTrotter(inputrec)))
1166 /* We should only couple after a step where energies were determined (for leapfrog versions)
1167 or the step energies are determined, for velocity verlet versions */
1169 if (EI_VV(inputrec->eI))
1177 bTCouple = (inputrec->nsttcouple == 1 ||
1178 do_per_step(step+inputrec->nsttcouple-offset,
1179 inputrec->nsttcouple));
1184 dttc = inputrec->nsttcouple*inputrec->delta_t;
1186 switch (inputrec->etc)
1191 berendsen_tcoupl(inputrec, ekind, dttc);
1194 nosehoover_tcoupl(&(inputrec->opts), ekind, dttc,
1195 state->nosehoover_xi, state->nosehoover_vxi, MassQ);
1198 vrescale_tcoupl(inputrec, step, ekind, dttc,
1199 state->therm_integral);
1202 /* rescale in place here */
1203 if (EI_VV(inputrec->eI))
1205 rescale_velocities(ekind, md, 0, md->homenr, state->v);
1210 /* Set the T scaling lambda to 1 to have no scaling */
1211 for (i = 0; (i < inputrec->opts.ngtc); i++)
1213 ekind->tcstat[i].lambda = 1.0;
1218 void update_pcouple(FILE *fplog,
1220 t_inputrec *inputrec,
1226 gmx_bool bPCouple = FALSE;
1230 /* if using Trotter pressure, we do this in coupling.c, so we leave it false. */
1231 if (inputrec->epc != epcNO && (!(inputrecNptTrotter(inputrec) || inputrecNphTrotter(inputrec))))
1233 /* We should only couple after a step where energies were determined */
1234 bPCouple = (inputrec->nstpcouple == 1 ||
1235 do_per_step(step+inputrec->nstpcouple-1,
1236 inputrec->nstpcouple));
1239 clear_mat(pcoupl_mu);
1240 for (i = 0; i < DIM; i++)
1242 pcoupl_mu[i][i] = 1.0;
1249 dtpc = inputrec->nstpcouple*inputrec->delta_t;
1251 switch (inputrec->epc)
1253 /* We can always pcoupl, even if we did not sum the energies
1254 * the previous step, since state->pres_prev is only updated
1255 * when the energies have been summed.
1259 case (epcBERENDSEN):
1262 berendsen_pcoupl(fplog, step, inputrec, dtpc, state->pres_prev, state->box,
1266 case (epcPARRINELLORAHMAN):
1267 parrinellorahman_pcoupl(fplog, step, inputrec, dtpc, state->pres_prev,
1268 state->box, state->box_rel, state->boxv,
1269 M, pcoupl_mu, bInitStep);
1277 void update_constraints(FILE *fplog,
1279 real *dvdlambda, /* the contribution to be added to the bonded interactions */
1280 t_inputrec *inputrec, /* input record and box stuff */
1285 rvec force[], /* forces on home particles */
1290 gmx_wallcycle_t wcycle,
1292 gmx_constr_t constr,
1293 gmx_bool bFirstHalf,
1296 gmx_bool bLastStep, bLog = FALSE, bEner = FALSE, bDoConstr = FALSE;
1298 int start, homenr, nrend, i;
1306 if (bFirstHalf && !EI_VV(inputrec->eI))
1311 /* for now, SD update is here -- though it really seems like it
1312 should be reformulated as a velocity verlet method, since it has two parts */
1315 homenr = md->homenr;
1316 nrend = start+homenr;
1318 dt = inputrec->delta_t;
1322 * APPLY CONSTRAINTS:
1325 * When doing PR pressure coupling we have to constrain the
1326 * bonds in each iteration. If we are only using Nose-Hoover tcoupling
1327 * it is enough to do this once though, since the relative velocities
1328 * after this will be normal to the bond vector
1333 /* clear out constraints before applying */
1334 clear_mat(vir_part);
1336 bLastStep = (step == inputrec->init_step+inputrec->nsteps);
1337 bLog = (do_per_step(step, inputrec->nstlog) || bLastStep || (step < 0));
1338 bEner = (do_per_step(step, inputrec->nstenergy) || bLastStep);
1339 /* Constrain the coordinates upd->xp */
1340 wallcycle_start(wcycle, ewcCONSTR);
1341 if (EI_VV(inputrec->eI) && bFirstHalf)
1343 constrain(NULL, bLog, bEner, constr, idef,
1344 inputrec, cr, step, 1, 1.0, md,
1345 state->x, state->v, state->v,
1346 bMolPBC, state->box,
1347 state->lambda[efptBONDED], dvdlambda,
1348 NULL, bCalcVir ? &vir_con : NULL, nrnb, econqVeloc);
1352 constrain(NULL, bLog, bEner, constr, idef,
1353 inputrec, cr, step, 1, 1.0, md,
1354 state->x, upd->xp, NULL,
1355 bMolPBC, state->box,
1356 state->lambda[efptBONDED], dvdlambda,
1357 state->v, bCalcVir ? &vir_con : NULL, nrnb, econqCoord);
1359 wallcycle_stop(wcycle, ewcCONSTR);
1363 dump_it_all(fplog, "After Shake",
1364 state->natoms, state->x, upd->xp, state->v, force);
1368 m_add(vir_part, vir_con, vir_part);
1371 pr_rvecs(debug, 0, "constraint virial", vir_part, DIM);
1378 if (inputrec->eI == eiSD1 && bDoConstr && !bFirstHalf)
1380 wallcycle_start(wcycle, ewcUPDATE);
1382 nth = gmx_omp_nthreads_get(emntUpdate);
1384 #pragma omp parallel for num_threads(nth) schedule(static)
1385 for (th = 0; th < nth; th++)
1389 int start_th, end_th;
1391 start_th = start + ((nrend-start)* th )/nth;
1392 end_th = start + ((nrend-start)*(th+1))/nth;
1394 /* The second part of the SD integration */
1395 do_update_sd1(upd->sd,
1396 start_th, end_th, dt,
1397 inputrec->opts.acc, inputrec->opts.nFreeze,
1398 md->invmass, md->ptype,
1399 md->cFREEZE, md->cACC, md->cTC,
1400 state->x, upd->xp, state->v, force,
1402 step, inputrec->ld_seed,
1403 DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL);
1405 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
1407 inc_nrnb(nrnb, eNR_UPDATE, homenr);
1408 wallcycle_stop(wcycle, ewcUPDATE);
1412 /* Constrain the coordinates upd->xp for half a time step */
1413 wallcycle_start(wcycle, ewcCONSTR);
1415 constrain(NULL, bLog, bEner, constr, idef,
1416 inputrec, cr, step, 1, 0.5, md,
1417 state->x, upd->xp, NULL,
1418 bMolPBC, state->box,
1419 state->lambda[efptBONDED], dvdlambda,
1420 state->v, NULL, nrnb, econqCoord);
1422 wallcycle_stop(wcycle, ewcCONSTR);
1426 /* We must always unshift after updating coordinates; if we did not shake
1427 x was shifted in do_force */
1429 if (!(bFirstHalf)) /* in the first half of vv, no shift. */
1431 /* NOTE This part of the update actually does not belong with
1432 * the constraints, since we also call it without constraints.
1433 * But currently we always integrate to a temporary buffer and
1434 * then copy the results back here.
1436 wallcycle_start_nocount(wcycle, ewcUPDATE);
1438 if (md->cFREEZE != NULL && constr != NULL)
1440 /* If we have atoms that are frozen along some, but not all
1441 * dimensions, the constraints will have moved them also along
1442 * the frozen dimensions. To freeze such degrees of freedom
1443 * we copy them back here to later copy them forward. It would
1444 * be more elegant and slightly more efficient to copies zero
1445 * times instead of twice, but the graph case below prevents this.
1447 const ivec *nFreeze = inputrec->opts.nFreeze;
1448 bool partialFreezeAndConstraints = false;
1449 for (int g = 0; g < inputrec->opts.ngfrz; g++)
1451 int numFreezeDim = nFreeze[g][XX] + nFreeze[g][YY] + nFreeze[g][ZZ];
1452 if (numFreezeDim > 0 && numFreezeDim < 3)
1454 partialFreezeAndConstraints = true;
1457 if (partialFreezeAndConstraints)
1459 for (int i = start; i < nrend; i++)
1461 int g = md->cFREEZE[i];
1463 for (int d = 0; d < DIM; d++)
1467 upd->xp[i][d] = state->x[i][d];
1474 if (graph && (graph->nnodes > 0))
1476 unshift_x(graph, state->box, state->x, upd->xp);
1477 if (TRICLINIC(state->box))
1479 inc_nrnb(nrnb, eNR_SHIFTX, 2*graph->nnodes);
1483 inc_nrnb(nrnb, eNR_SHIFTX, graph->nnodes);
1488 #ifndef __clang_analyzer__
1489 // cppcheck-suppress unreadVariable
1490 nth = gmx_omp_nthreads_get(emntUpdate);
1492 #pragma omp parallel for num_threads(nth) schedule(static)
1493 for (i = start; i < nrend; i++)
1495 // Trivial statement, does not throw
1496 copy_rvec(upd->xp[i], state->x[i]);
1499 wallcycle_stop(wcycle, ewcUPDATE);
1501 dump_it_all(fplog, "After unshift",
1502 state->natoms, state->x, upd->xp, state->v, force);
1504 /* ############# END the update of velocities and positions ######### */
1507 void update_box(FILE *fplog,
1509 t_inputrec *inputrec, /* input record and box stuff */
1512 rvec force[], /* forces on home particles */
1518 int start, homenr, i, n, m;
1521 homenr = md->homenr;
1523 dt = inputrec->delta_t;
1527 /* now update boxes */
1528 switch (inputrec->epc)
1532 case (epcBERENDSEN):
1533 /* We should only scale after a step where the pressure (kinetic
1534 * energy and virial) was calculated. This happens after the
1535 * coordinate update, whereas the current routine is called before
1536 * that, so we scale when step % nstpcouple = 1 instead of 0.
1538 if (inputrec->nstpcouple == 1 || (step % inputrec->nstpcouple == 1))
1540 berendsen_pscale(inputrec, pcoupl_mu, state->box, state->box_rel,
1541 start, homenr, state->x, md->cFREEZE, nrnb);
1544 case (epcPARRINELLORAHMAN):
1545 /* The box velocities were updated in do_pr_pcoupl in the update
1546 * iteration, but we dont change the box vectors until we get here
1547 * since we need to be able to shift/unshift above.
1549 for (i = 0; i < DIM; i++)
1551 for (m = 0; m <= i; m++)
1553 state->box[i][m] += dt*state->boxv[i][m];
1556 preserve_box_shape(inputrec, state->box_rel, state->box);
1558 /* Scale the coordinates */
1559 for (n = start; (n < start+homenr); n++)
1561 tmvmul_ur0(pcoupl_mu, state->x[n], state->x[n]);
1565 switch (inputrec->epct)
1567 case (epctISOTROPIC):
1568 /* DIM * eta = ln V. so DIM*eta_new = DIM*eta_old + DIM*dt*veta =>
1569 ln V_new = ln V_old + 3*dt*veta => V_new = V_old*exp(3*dt*veta) =>
1570 Side length scales as exp(veta*dt) */
1572 msmul(state->box, exp(state->veta*dt), state->box);
1574 /* Relate veta to boxv. veta = d(eta)/dT = (1/DIM)*1/V dV/dT.
1575 o If we assume isotropic scaling, and box length scaling
1576 factor L, then V = L^DIM (det(M)). So dV/dt = DIM
1577 L^(DIM-1) dL/dt det(M), and veta = (1/L) dL/dt. The
1578 determinant of B is L^DIM det(M), and the determinant
1579 of dB/dt is (dL/dT)^DIM det (M). veta will be
1580 (det(dB/dT)/det(B))^(1/3). Then since M =
1581 B_new*(vol_new)^(1/3), dB/dT_new = (veta_new)*B(new). */
1583 msmul(state->box, state->veta, state->boxv);
1593 if (inputrecDeform(inputrec))
1595 deform(upd, start, homenr, state->x, state->box, inputrec, step);
1598 dump_it_all(fplog, "After update",
1599 state->natoms, state->x, upd->xp, state->v, force);
1602 void update_coords(FILE *fplog,
1604 t_inputrec *inputrec, /* input record and box stuff */
1607 rvec *f, /* forces on home particles */
1609 gmx_ekindata_t *ekind,
1613 t_commrec *cr, /* these shouldn't be here -- need to think about it */
1614 gmx_constr_t constr)
1616 gmx_bool bNH, bPR, bDoConstr = FALSE;
1618 int start, homenr, nrend;
1621 bDoConstr = (NULL != constr);
1623 /* Running the velocity half does nothing except for velocity verlet */
1624 if ((UpdatePart == etrtVELOCITY1 || UpdatePart == etrtVELOCITY2) &&
1625 !EI_VV(inputrec->eI))
1627 gmx_incons("update_coords called for velocity without VV integrator");
1631 homenr = md->homenr;
1632 nrend = start+homenr;
1634 dt = inputrec->delta_t;
1636 /* We need to update the NMR restraint history when time averaging is used */
1637 if (state->flags & (1<<estDISRE_RM3TAV))
1639 update_disres_history(fcd, &state->hist);
1641 if (state->flags & (1<<estORIRE_DTAV))
1643 update_orires_history(fcd, &state->hist);
1647 bNH = inputrec->etc == etcNOSEHOOVER;
1648 bPR = ((inputrec->epc == epcPARRINELLORAHMAN) || (inputrec->epc == epcMTTK));
1650 /* ############# START The update of velocities and positions ######### */
1652 dump_it_all(fplog, "Before update",
1653 state->natoms, state->x, upd->xp, state->v, f);
1655 nth = gmx_omp_nthreads_get(emntUpdate);
1657 #pragma omp parallel for num_threads(nth) schedule(static) private(alpha)
1658 for (th = 0; th < nth; th++)
1662 int start_th, end_th;
1664 start_th = start + ((nrend-start)* th )/nth;
1665 end_th = start + ((nrend-start)*(th+1))/nth;
1667 switch (inputrec->eI)
1670 if (ekind->cosacc.cos_accel == 0)
1672 do_update_md(start_th, end_th, dt,
1673 ekind->tcstat, state->nosehoover_vxi,
1674 ekind->bNEMD, ekind->grpstat, inputrec->opts.acc,
1675 inputrec->opts.nFreeze,
1676 md->invmass, md->ptype,
1677 md->cFREEZE, md->cACC, md->cTC,
1678 state->x, upd->xp, state->v, f, M,
1683 do_update_visc(start_th, end_th, dt,
1684 ekind->tcstat, state->nosehoover_vxi,
1685 md->invmass, md->ptype,
1686 md->cTC, state->x, upd->xp, state->v, f, M,
1688 ekind->cosacc.cos_accel,
1694 /* With constraints, the SD1 update is done in 2 parts */
1695 do_update_sd1(upd->sd,
1696 start_th, end_th, dt,
1697 inputrec->opts.acc, inputrec->opts.nFreeze,
1698 md->invmass, md->ptype,
1699 md->cFREEZE, md->cACC, md->cTC,
1700 state->x, upd->xp, state->v, f,
1702 step, inputrec->ld_seed, DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL);
1705 do_update_bd(start_th, end_th, dt,
1706 inputrec->opts.nFreeze, md->invmass, md->ptype,
1707 md->cFREEZE, md->cTC,
1708 state->x, upd->xp, state->v, f,
1711 step, inputrec->ld_seed, DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL);
1715 alpha = 1.0 + DIM/((double)inputrec->opts.nrdf[0]); /* assuming barostat coupled to group 0. */
1720 do_update_vv_vel(start_th, end_th, dt,
1721 inputrec->opts.acc, inputrec->opts.nFreeze,
1722 md->invmass, md->ptype,
1723 md->cFREEZE, md->cACC,
1725 (bNH || bPR), state->veta, alpha);
1728 do_update_vv_pos(start_th, end_th, dt,
1729 inputrec->opts.nFreeze,
1730 md->ptype, md->cFREEZE,
1731 state->x, upd->xp, state->v,
1732 (bNH || bPR), state->veta);
1737 gmx_fatal(FARGS, "Don't know how to update coordinates");
1741 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
1747 void correct_ekin(FILE *log, int start, int end, rvec v[], rvec vcm, real mass[],
1748 real tmass, tensor ekin)
1751 * This is a debugging routine. It should not be called for production code
1753 * The kinetic energy should calculated according to:
1754 * Ekin = 1/2 m (v-vcm)^2
1755 * However the correction is not always applied, since vcm may not be
1756 * known in time and we compute
1757 * Ekin' = 1/2 m v^2 instead
1758 * This can be corrected afterwards by computing
1759 * Ekin = Ekin' + 1/2 m ( -2 v vcm + vcm^2)
1761 * Ekin = Ekin' - m v vcm + 1/2 m vcm^2
1768 /* Local particles */
1771 /* Processor dependent part. */
1773 for (i = start; (i < end); i++)
1777 for (j = 0; (j < DIM); j++)
1783 svmul(1/tmass, vcm, vcm);
1784 svmul(0.5, vcm, hvcm);
1786 for (j = 0; (j < DIM); j++)
1788 for (k = 0; (k < DIM); k++)
1790 dekin[j][k] += vcm[k]*(tm*hvcm[j]-mv[j]);
1793 pr_rvecs(log, 0, "dekin", dekin, DIM);
1794 pr_rvecs(log, 0, " ekin", ekin, DIM);
1795 fprintf(log, "dekin = %g, ekin = %g vcm = (%8.4f %8.4f %8.4f)\n",
1796 trace(dekin), trace(ekin), vcm[XX], vcm[YY], vcm[ZZ]);
1797 fprintf(log, "mv = (%8.4f %8.4f %8.4f)\n",
1798 mv[XX], mv[YY], mv[ZZ]);
1801 extern gmx_bool update_randomize_velocities(t_inputrec *ir, gmx_int64_t step, const t_commrec *cr,
1802 t_mdatoms *md, t_state *state, gmx_update_t *upd, gmx_constr_t constr)
1805 real rate = (ir->delta_t)/ir->opts.tau_t[0];
1807 if (ir->etc == etcANDERSEN && constr != NULL)
1809 /* Currently, Andersen thermostat does not support constrained
1810 systems. Functionality exists in the andersen_tcoupl
1811 function in GROMACS 4.5.7 to allow this combination. That
1812 code could be ported to the current random-number
1813 generation approach, but has not yet been done because of
1814 lack of time and resources. */
1815 gmx_fatal(FARGS, "Normal Andersen is currently not supported with constraints, use massive Andersen instead");
1818 /* proceed with andersen if 1) it's fixed probability per
1819 particle andersen or 2) it's massive andersen and it's tau_t/dt */
1820 if ((ir->etc == etcANDERSEN) || do_per_step(step, (int)(1.0/rate)))
1822 andersen_tcoupl(ir, step, cr, md, state, rate,
1823 upd->sd->randomize_group, upd->sd->boltzfac);