Merge branch 'master' into rotation
[gromacs/adressmacs.git] / src / mdlib / mdebin.c
blob36b83e8da53211e5f17ea8499dbb9074faa2f1b2
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
40 #include <string.h>
41 #include "typedefs.h"
42 #include "string2.h"
43 #include "mdebin.h"
44 #include "smalloc.h"
45 #include "physics.h"
46 #include "enxio.h"
47 #include "vec.h"
48 #include "disre.h"
49 #include "main.h"
50 #include "network.h"
51 #include "names.h"
52 #include "orires.h"
53 #include "constr.h"
54 #include "mtop_util.h"
55 #include "xvgr.h"
56 #include "gmxfio.h"
58 static const char *conrmsd_nm[] = { "Constr. rmsd", "Constr.2 rmsd" };
60 static const char *boxs_nm[] = { "Box-X", "Box-Y", "Box-Z" };
62 static const char *tricl_boxs_nm[] = { "Box-XX", "Box-YX", "Box-YY",
63 "Box-ZX", "Box-ZY", "Box-ZZ" };
65 static const char *vol_nm[] = { "Volume" };
67 static const char *dens_nm[] = {"Density" };
69 static const char *pv_nm[] = {"pV" };
71 static const char *boxvel_nm[] = {
72 "Box-Vel-XX", "Box-Vel-YY", "Box-Vel-ZZ",
73 "Box-Vel-YX", "Box-Vel-ZX", "Box-Vel-ZY"
76 #define NBOXS asize(boxs_nm)
77 #define NTRICLBOXS asize(tricl_boxs_nm)
79 static bool bTricl,bDynBox;
80 static int f_nre=0,epc,etc,nCrmsd;
82 t_mdebin *init_mdebin(ener_file_t fp_ene,
83 const gmx_mtop_t *mtop,
84 const t_inputrec *ir)
86 const char *ener_nm[F_NRE];
87 static const char *vir_nm[] = {
88 "Vir-XX", "Vir-XY", "Vir-XZ",
89 "Vir-YX", "Vir-YY", "Vir-YZ",
90 "Vir-ZX", "Vir-ZY", "Vir-ZZ"
92 static const char *sv_nm[] = {
93 "ShakeVir-XX", "ShakeVir-XY", "ShakeVir-XZ",
94 "ShakeVir-YX", "ShakeVir-YY", "ShakeVir-YZ",
95 "ShakeVir-ZX", "ShakeVir-ZY", "ShakeVir-ZZ"
97 static const char *fv_nm[] = {
98 "ForceVir-XX", "ForceVir-XY", "ForceVir-XZ",
99 "ForceVir-YX", "ForceVir-YY", "ForceVir-YZ",
100 "ForceVir-ZX", "ForceVir-ZY", "ForceVir-ZZ"
102 static const char *pres_nm[] = {
103 "Pres-XX","Pres-XY","Pres-XZ",
104 "Pres-YX","Pres-YY","Pres-YZ",
105 "Pres-ZX","Pres-ZY","Pres-ZZ"
107 static const char *surft_nm[] = {
108 "#Surf*SurfTen"
110 static const char *mu_nm[] = {
111 "Mu-X", "Mu-Y", "Mu-Z"
113 static const char *vcos_nm[] = {
114 "2CosZ*Vel-X"
116 static const char *visc_nm[] = {
117 "1/Viscosity"
119 static char *bufbaro[] = {
120 "barostat"
123 char **grpnms;
124 const gmx_groups_t *groups;
125 char **gnm;
126 char buf[256];
127 char *bufi;
128 t_mdebin *md;
129 int i,j,ni,nj,n,nh,k,kk,ncon,nset;
130 bool bBHAM,bNoseHoover,b14;
132 snew(md,1);
134 groups = &mtop->groups;
136 bBHAM = (mtop->ffparams.functype[0] == F_BHAM);
137 b14 = (gmx_mtop_ftype_count(mtop,F_LJ14) > 0 ||
138 gmx_mtop_ftype_count(mtop,F_LJC14_Q) > 0);
140 ncon = gmx_mtop_ftype_count(mtop,F_CONSTR);
141 nset = gmx_mtop_ftype_count(mtop,F_SETTLE);
142 md->bConstr = (ncon > 0 || nset > 0);
143 md->bConstrVir = FALSE;
144 if (md->bConstr) {
145 if (ncon > 0 && ir->eConstrAlg == econtLINCS) {
146 if (ir->eI == eiSD2)
147 md->nCrmsd = 2;
148 else
149 md->nCrmsd = 1;
151 md->bConstrVir = (getenv("GMX_CONSTRAINTVIR") != NULL);
152 } else {
153 md->nCrmsd = 0;
156 /* Energy monitoring */
157 for(i=0;i<egNR;i++)
159 md->bEInd[i]=FALSE;
162 for(i=0; i<F_NRE; i++) {
163 md->bEner[i] = FALSE;
164 if (i == F_LJ)
165 md->bEner[i] = !bBHAM;
166 else if (i == F_BHAM)
167 md->bEner[i] = bBHAM;
168 else if (i == F_EQM)
169 md->bEner[i] = ir->bQMMM;
170 else if (i == F_COUL_LR)
171 md->bEner[i] = (ir->rcoulomb > ir->rlist);
172 else if (i == F_LJ_LR)
173 md->bEner[i] = (!bBHAM && ir->rvdw > ir->rlist);
174 else if (i == F_BHAM_LR)
175 md->bEner[i] = (bBHAM && ir->rvdw > ir->rlist);
176 else if (i == F_RF_EXCL)
177 md->bEner[i] = (EEL_RF(ir->coulombtype) && ir->coulombtype != eelRF_NEC);
178 else if (i == F_COUL_RECIP)
179 md->bEner[i] = EEL_FULL(ir->coulombtype);
180 else if (i == F_LJ14)
181 md->bEner[i] = b14;
182 else if (i == F_COUL14)
183 md->bEner[i] = b14;
184 else if (i == F_LJC14_Q || i == F_LJC_PAIRS_NB)
185 md->bEner[i] = FALSE;
186 else if ((i == F_DVDL) || (i == F_DKDL))
187 md->bEner[i] = (ir->efep != efepNO);
188 else if (i == F_DHDL_CON)
189 md->bEner[i] = (ir->efep != efepNO && md->bConstr);
190 else if ((interaction_function[i].flags & IF_VSITE) ||
191 (i == F_CONSTR) || (i == F_CONSTRNC) || (i == F_SETTLE))
192 md->bEner[i] = FALSE;
193 else if ((i == F_COUL_SR) || (i == F_EPOT) || (i == F_PRES) || (i==F_EQM))
194 md->bEner[i] = TRUE;
195 else if ((i == F_ETOT) || (i == F_EKIN) || (i == F_TEMP))
196 md->bEner[i] = EI_DYNAMICS(ir->eI);
197 else if (i==F_VTEMP)
198 md->bEner[i] = (EI_DYNAMICS(ir->eI) && getenv("GMX_VIRIAL_TEMPERATURE"));
199 else if (i == F_DISPCORR || i == F_PDISPCORR)
200 md->bEner[i] = (ir->eDispCorr != edispcNO);
201 else if (i == F_DISRESVIOL)
202 md->bEner[i] = (gmx_mtop_ftype_count(mtop,F_DISRES) > 0);
203 else if (i == F_ORIRESDEV)
204 md->bEner[i] = (gmx_mtop_ftype_count(mtop,F_ORIRES) > 0);
205 else if (i == F_CONNBONDS)
206 md->bEner[i] = FALSE;
207 else if (i == F_COM_PULL)
208 md->bEner[i] = (ir->ePull == epullUMBRELLA || ir->ePull == epullCONST_F || ir->bRot);
209 else if (i == F_ECONSERVED)
210 md->bEner[i] = ((ir->etc == etcNOSEHOOVER || ir->etc == etcVRESCALE) &&
211 (ir->epc == epcNO || ir->epc==epcMTTK));
212 else
213 md->bEner[i] = (gmx_mtop_ftype_count(mtop,i) > 0);
216 md->f_nre=0;
217 for(i=0; i<F_NRE; i++)
219 if (md->bEner[i])
221 /* FIXME: The constness should not be cast away */
222 /*ener_nm[f_nre]=(char *)interaction_function[i].longname;*/
223 ener_nm[md->f_nre]=interaction_function[i].longname;
224 md->f_nre++;
228 md->epc = ir->epc;
229 for (i=0;i<DIM;i++)
231 for (j=0;j<DIM;j++)
233 md->ref_p[i][j] = ir->ref_p[i][j];
236 md->bTricl = TRICLINIC(ir->compress) || TRICLINIC(ir->deform);
237 md->bDynBox = DYNAMIC_BOX(*ir);
238 md->etc = ir->etc;
239 md->bNHC_trotter = IR_NVT_TROTTER(ir);
241 md->ebin = mk_ebin();
242 /* Pass NULL for unit to let get_ebin_space determine the units
243 * for interaction_function[i].longname
245 md->ie = get_ebin_space(md->ebin,md->f_nre,ener_nm,NULL);
246 if (md->nCrmsd)
248 /* This should be called directly after the call for md->ie,
249 * such that md->iconrmsd follows directly in the list.
251 md->iconrmsd = get_ebin_space(md->ebin,md->nCrmsd,conrmsd_nm,"");
253 if (md->bDynBox)
255 md->ib = get_ebin_space(md->ebin, md->bTricl ? NTRICLBOXS :
256 NBOXS, md->bTricl ? tricl_boxs_nm : boxs_nm,
257 unit_length);
258 md->ivol = get_ebin_space(md->ebin, 1, vol_nm, unit_volume);
259 md->idens = get_ebin_space(md->ebin, 1, dens_nm, unit_density_SI);
260 md->ipv = get_ebin_space(md->ebin, 1, pv_nm, unit_energy);
262 if (md->bConstrVir)
264 md->isvir = get_ebin_space(md->ebin,asize(sv_nm),sv_nm,unit_energy);
265 md->ifvir = get_ebin_space(md->ebin,asize(fv_nm),fv_nm,unit_energy);
267 md->ivir = get_ebin_space(md->ebin,asize(vir_nm),vir_nm,unit_energy);
268 md->ipres = get_ebin_space(md->ebin,asize(pres_nm),pres_nm,unit_pres_bar);
269 md->isurft = get_ebin_space(md->ebin,asize(surft_nm),surft_nm,
270 unit_surft_bar);
271 if (md->epc == epcPARRINELLORAHMAN || md->epc == epcMTTK)
273 md->ipc = get_ebin_space(md->ebin,md->bTricl ? 6 : 3,
274 boxvel_nm,unit_vel);
276 md->imu = get_ebin_space(md->ebin,asize(mu_nm),mu_nm,unit_dipole_D);
277 if (ir->cos_accel != 0)
279 md->ivcos = get_ebin_space(md->ebin,asize(vcos_nm),vcos_nm,unit_vel);
280 md->ivisc = get_ebin_space(md->ebin,asize(visc_nm),visc_nm,
281 unit_invvisc_SI);
284 /* Energy monitoring */
285 for(i=0;i<egNR;i++)
287 md->bEInd[i] = FALSE;
289 md->bEInd[egCOULSR] = TRUE;
290 md->bEInd[egLJSR ] = TRUE;
292 if (ir->rcoulomb > ir->rlist)
294 md->bEInd[egCOULLR] = TRUE;
296 if (!bBHAM)
298 if (ir->rvdw > ir->rlist)
300 md->bEInd[egLJLR] = TRUE;
303 else
305 md->bEInd[egLJSR] = FALSE;
306 md->bEInd[egBHAMSR] = TRUE;
307 if (ir->rvdw > ir->rlist)
309 md->bEInd[egBHAMLR] = TRUE;
312 if (b14)
314 md->bEInd[egLJ14] = TRUE;
315 md->bEInd[egCOUL14] = TRUE;
317 md->nEc=0;
318 for(i=0; (i<egNR); i++)
320 if (md->bEInd[i])
322 md->nEc++;
326 n=groups->grps[egcENER].nr;
327 md->nEg=n;
328 md->nE=(n*(n+1))/2;
329 snew(md->igrp,md->nE);
330 if (md->nE > 1)
332 n=0;
333 snew(gnm,md->nEc);
334 for(k=0; (k<md->nEc); k++)
336 snew(gnm[k],STRLEN);
338 for(i=0; (i<groups->grps[egcENER].nr); i++)
340 ni=groups->grps[egcENER].nm_ind[i];
341 for(j=i; (j<groups->grps[egcENER].nr); j++)
343 nj=groups->grps[egcENER].nm_ind[j];
344 for(k=kk=0; (k<egNR); k++)
346 if (md->bEInd[k])
348 sprintf(gnm[kk],"%s:%s-%s",egrp_nm[k],
349 *(groups->grpname[ni]),*(groups->grpname[nj]));
350 kk++;
353 md->igrp[n]=get_ebin_space(md->ebin,md->nEc,
354 (const char **)gnm,unit_energy);
355 n++;
358 for(k=0; (k<md->nEc); k++)
360 sfree(gnm[k]);
362 sfree(gnm);
364 if (n != md->nE)
366 gmx_incons("Number of energy terms wrong");
371 md->nTC=groups->grps[egcTC].nr;
372 md->nNHC = ir->opts.nhchainlength; /* shorthand for number of NH chains */
373 if (md->epc == epcMTTK)
375 md->nTCB = md->nTC + 1; /* for barostat temperature group */
377 else
379 md->nTCB = md->nTC;
383 if (md->etc == etcNOSEHOOVER) {
384 if (md->bNHC_trotter) {
385 md->mde_n = 2*md->nNHC*md->nTCB;
387 else
389 md->mde_n = 2*md->nTCB;
391 } else {
392 md->mde_n = md->nTCB;
395 snew(md->tmp_r,md->mde_n);
396 snew(md->tmp_v,md->mde_n);
397 snew(md->grpnms,md->mde_n);
398 grpnms = md->grpnms;
400 for(i=0; (i<md->nTC); i++)
402 ni=groups->grps[egcTC].nm_ind[i];
403 sprintf(buf,"T-%s",*(groups->grpname[ni]));
404 grpnms[i]=strdup(buf);
406 md->itemp=get_ebin_space(md->ebin,md->nTC,(const char **)grpnms,
407 unit_temp_K);
409 bNoseHoover = (getenv("GMX_NOSEHOOVER_CHAINS") != NULL); /* whether to print Nose-Hoover chains */
411 if (md->etc == etcNOSEHOOVER)
413 if (bNoseHoover)
415 if (md->bNHC_trotter)
417 for(i=0; (i<md->nTCB); i++)
419 ni=groups->grps[egcTC].nm_ind[i];
420 /* this one is a barostat thermostat */
421 if ((i==md->nTCB-1) && (md->nTCB > md->nTC)) {bufi = bufbaro[0];} else {bufi = *(groups->grpname[ni]);}
422 for(j=0; (j<md->nNHC); j++)
424 sprintf(buf,"Xi-%d-%s",j,bufi);
425 grpnms[2*(i*md->nNHC+j)]=strdup(buf);
426 sprintf(buf,"vXi-%d-%s",j,bufi);
427 grpnms[2*(i*md->nNHC+j)+1]=strdup(buf);
430 md->itc=get_ebin_space(md->ebin,md->mde_n,(const char **)grpnms,unit_invtime);
432 else
434 for(i=0; (i<md->nTCB); i++)
436 ni=groups->grps[egcTC].nm_ind[i];
437 bufi = *(groups->grpname[ni]);
438 sprintf(buf,"Xi-%s",bufi);
439 grpnms[2*i]=strdup(buf);
440 sprintf(buf,"vXi-%s",bufi);
441 grpnms[2*i+1]=strdup(buf);
443 md->itc=get_ebin_space(md->ebin,md->mde_n,(const char **)grpnms,unit_invtime);
447 else if (md->etc == etcBERENDSEN || md->etc == etcYES ||
448 md->etc == etcVRESCALE)
450 for(i=0; (i<md->nTC); i++)
452 ni=groups->grps[egcTC].nm_ind[i];
453 sprintf(buf,"Lamb-%s",*(groups->grpname[ni]));
454 grpnms[i]=strdup(buf);
456 md->itc=get_ebin_space(md->ebin,md->mde_n,(const char **)grpnms,"");
459 sfree(grpnms);
462 md->nU=groups->grps[egcACC].nr;
463 if (md->nU > 1)
465 snew(grpnms,3*md->nU);
466 for(i=0; (i<md->nU); i++)
468 ni=groups->grps[egcACC].nm_ind[i];
469 sprintf(buf,"Ux-%s",*(groups->grpname[ni]));
470 grpnms[3*i+XX]=strdup(buf);
471 sprintf(buf,"Uy-%s",*(groups->grpname[ni]));
472 grpnms[3*i+YY]=strdup(buf);
473 sprintf(buf,"Uz-%s",*(groups->grpname[ni]));
474 grpnms[3*i+ZZ]=strdup(buf);
476 md->iu=get_ebin_space(md->ebin,3*md->nU,(const char **)grpnms,unit_vel);
477 sfree(grpnms);
480 if ( fp_ene )
482 do_enxnms(fp_ene,&md->ebin->nener,&md->ebin->enm);
485 md->print_grpnms=NULL;
487 return md;
490 FILE *open_dhdl(const char *filename,t_inputrec *ir,const output_env_t oenv)
492 FILE *fp;
493 const char *dhdl="dH/d\\8l\\4",*deltag="\\8D\\4H",*lambda="\\8l\\4";
494 char title[STRLEN],label_x[STRLEN],label_y[STRLEN];
495 int nsets,s;
496 char **setname,buf[STRLEN];
498 sprintf(label_x,"%s (%s)","Time",unit_time);
499 if (ir->n_flambda == 0)
501 sprintf(title,"%s",dhdl,deltag);
502 sprintf(label_y,"%s (%s %s)",
503 dhdl,unit_energy,"[\\8l\\4]\\S-1\\N");
505 else
507 sprintf(title,"%s, %s",dhdl,deltag);
508 sprintf(label_y,"(%s)",unit_energy);
510 fp = xvgropen(filename,title,label_x,label_y,oenv);
512 if (ir->n_flambda > 0)
514 /* g_bar has to determine the lambda values used in this simulation
515 * from this xvg legend.
517 nsets = 1 + ir->n_flambda;
518 snew(setname,nsets);
519 sprintf(buf,"%s %s %g",dhdl,lambda,ir->init_lambda);
520 setname[0] = strdup(buf);
521 for(s=1; s<nsets; s++)
523 sprintf(buf,"%s %s %g",deltag,lambda,ir->flambda[s-1]);
524 setname[s] = strdup(buf);
526 xvgr_legend(fp,nsets,setname,oenv);
528 for(s=0; s<nsets; s++)
530 sfree(setname[s]);
532 sfree(setname);
535 return fp;
538 static void copy_energy(t_mdebin *md, real e[],real ecpy[])
540 int i,j;
542 for(i=j=0; (i<F_NRE); i++)
543 if (md->bEner[i])
544 ecpy[j++] = e[i];
545 if (j != md->f_nre)
546 gmx_incons("Number of energy terms wrong");
549 void upd_mdebin(t_mdebin *md,FILE *fp_dhdl,
550 bool bSum,
551 double time,
552 real tmass,
553 gmx_enerdata_t *enerd,
554 t_state *state,
555 matrix box,
556 tensor svir,
557 tensor fvir,
558 tensor vir,
559 tensor pres,
560 gmx_ekindata_t *ekind,
561 rvec mu_tot,
562 gmx_constr_t constr)
564 int i,j,k,kk,m,n,gid;
565 real crmsd[2],tmp6[6];
566 real bs[NTRICLBOXS],vol,dens,pv;
567 real eee[egNR];
568 real ecopy[F_NRE];
569 real tmp;
570 bool bNoseHoover;
572 /* Do NOT use the box in the state variable, but the separate box provided
573 * as an argument. This is because we sometimes need to write the box from
574 * the last timestep to match the trajectory frames.
576 copy_energy(md, enerd->term,ecopy);
577 add_ebin(md->ebin,md->ie,md->f_nre,ecopy,bSum);
578 if (md->nCrmsd)
580 crmsd[0] = constr_rmsd(constr,FALSE);
581 if (md->nCrmsd > 1)
583 crmsd[1] = constr_rmsd(constr,TRUE);
585 add_ebin(md->ebin,md->iconrmsd,md->nCrmsd,crmsd,FALSE);
587 if (md->bDynBox)
589 if(md->bTricl)
591 bs[0] = box[XX][XX];
592 bs[1] = box[YY][XX];
593 bs[2] = box[YY][YY];
594 bs[3] = box[ZZ][XX];
595 bs[4] = box[ZZ][YY];
596 bs[5] = box[ZZ][ZZ];
598 else
600 bs[0] = box[XX][XX];
601 bs[1] = box[YY][YY];
602 bs[2] = box[ZZ][ZZ];
604 vol = box[XX][XX]*box[YY][YY]*box[ZZ][ZZ];
605 dens = (tmass*AMU)/(vol*NANO*NANO*NANO);
607 /* This is pV (in kJ/mol). The pressure is the reference pressure,
608 not the instantaneous pressure */
609 pv = 0;
610 for (i=0;i<DIM;i++)
612 for (j=0;j<DIM;j++)
614 if (i>j)
616 pv += box[i][j]*md->ref_p[i][j]/PRESFAC;
618 else
620 pv += box[j][i]*md->ref_p[j][i]/PRESFAC;
624 add_ebin(md->ebin,md->ib ,NBOXS,bs ,bSum);
625 add_ebin(md->ebin,md->ivol ,1 ,&vol ,bSum);
626 add_ebin(md->ebin,md->idens,1 ,&dens,bSum);
627 add_ebin(md->ebin,md->ipv ,1 ,&pv ,bSum);
629 if (md->bConstrVir)
631 add_ebin(md->ebin,md->isvir,9,svir[0],bSum);
632 add_ebin(md->ebin,md->ifvir,9,fvir[0],bSum);
634 add_ebin(md->ebin,md->ivir,9,vir[0],bSum);
635 add_ebin(md->ebin,md->ipres,9,pres[0],bSum);
636 tmp = (pres[ZZ][ZZ]-(pres[XX][XX]+pres[YY][YY])*0.5)*box[ZZ][ZZ];
637 add_ebin(md->ebin,md->isurft,1,&tmp,bSum);
638 if (md->epc == epcPARRINELLORAHMAN || md->epc == epcMTTK)
640 tmp6[0] = state->boxv[XX][XX];
641 tmp6[1] = state->boxv[YY][YY];
642 tmp6[2] = state->boxv[ZZ][ZZ];
643 tmp6[3] = state->boxv[YY][XX];
644 tmp6[4] = state->boxv[ZZ][XX];
645 tmp6[5] = state->boxv[ZZ][YY];
646 add_ebin(md->ebin,md->ipc,md->bTricl ? 6 : 3,tmp6,bSum);
648 add_ebin(md->ebin,md->imu,3,mu_tot,bSum);
649 if (ekind && ekind->cosacc.cos_accel != 0)
651 vol = box[XX][XX]*box[YY][YY]*box[ZZ][ZZ];
652 dens = (tmass*AMU)/(vol*NANO*NANO*NANO);
653 add_ebin(md->ebin,md->ivcos,1,&(ekind->cosacc.vcos),bSum);
654 /* 1/viscosity, unit 1/(kg m^-1 s^-1) */
655 tmp = 1/(ekind->cosacc.cos_accel/(ekind->cosacc.vcos*PICO)
656 *vol*sqr(box[ZZ][ZZ]*NANO/(2*M_PI)));
657 add_ebin(md->ebin,md->ivisc,1,&tmp,bSum);
659 if (md->nE > 1)
661 n=0;
662 for(i=0; (i<md->nEg); i++)
664 for(j=i; (j<md->nEg); j++)
666 gid=GID(i,j,md->nEg);
667 for(k=kk=0; (k<egNR); k++)
669 if (md->bEInd[k])
671 eee[kk++] = enerd->grpp.ener[k][gid];
674 add_ebin(md->ebin,md->igrp[n],md->nEc,eee,bSum);
675 n++;
680 if (ekind)
682 for(i=0; (i<md->nTC); i++)
684 md->tmp_r[i] = ekind->tcstat[i].T;
686 add_ebin(md->ebin,md->itemp,md->nTC,md->tmp_r,bSum);
688 bNoseHoover = (getenv("GMX_NOSEHOOVER_CHAINS") != NULL); /* whether to print Nose-Hoover chains */
690 if (md->etc == etcNOSEHOOVER)
692 if (bNoseHoover)
694 if (md->bNHC_trotter)
697 for(i=0; (i<md->nTCB); i++)
699 for (j=0;j<md->nNHC;j++)
701 k = i*md->nNHC+j;
702 md->tmp_r[2*k] = state->nosehoover_xi[k];
703 md->tmp_r[2*k+1] = state->nosehoover_vxi[k];
706 add_ebin(md->ebin,md->itc,md->mde_n,md->tmp_r,bSum);
708 else
710 for(i=0; (i<md->nTC); i++)
712 md->tmp_r[2*i] = state->nosehoover_xi[i];
713 md->tmp_r[2*i+1] = state->nosehoover_vxi[i];
715 add_ebin(md->ebin,md->itc,md->mde_n,md->tmp_r,bSum);
719 else if (md->etc == etcBERENDSEN || md->etc == etcYES || md->etc == etcVRESCALE)
721 for(i=0; (i<md->nTC); i++)
723 md->tmp_r[i] = ekind->tcstat[i].lambda;
725 add_ebin(md->ebin,md->itc,md->nTC,md->tmp_r,bSum);
729 if (ekind && md->nU > 1)
731 for(i=0; (i<md->nU); i++)
733 copy_rvec(ekind->grpstat[i].u,md->tmp_v[i]);
735 add_ebin(md->ebin,md->iu,3*md->nU,md->tmp_v[0],bSum);
738 ebin_increase_count(md->ebin,bSum);
740 if (fp_dhdl)
742 fprintf(fp_dhdl,"%.4f %g",
743 time,
744 enerd->term[F_DVDL]+enerd->term[F_DKDL]+enerd->term[F_DHDL_CON]);
745 for(i=1; i<enerd->n_lambda; i++)
747 fprintf(fp_dhdl," %g",
748 enerd->enerpart_lambda[i]-enerd->enerpart_lambda[0]);
750 fprintf(fp_dhdl,"\n");
754 void upd_mdebin_step(t_mdebin *md)
756 ebin_increase_count(md->ebin,FALSE);
759 static void npr(FILE *log,int n,char c)
761 for(; (n>0); n--) fprintf(log,"%c",c);
764 static void pprint(FILE *log,const char *s,t_mdebin *md)
766 char CHAR='#';
767 int slen;
768 char buf1[22],buf2[22];
770 slen = strlen(s);
771 fprintf(log,"\t<====== ");
772 npr(log,slen,CHAR);
773 fprintf(log," ==>\n");
774 fprintf(log,"\t<==== %s ====>\n",s);
775 fprintf(log,"\t<== ");
776 npr(log,slen,CHAR);
777 fprintf(log," ======>\n\n");
779 fprintf(log,"\tStatistics over %s steps using %s frames\n",
780 gmx_step_str(md->ebin->nsteps_sim,buf1),
781 gmx_step_str(md->ebin->nsum_sim,buf2));
782 fprintf(log,"\n");
785 void print_ebin_header(FILE *log,gmx_large_int_t steps,double time,real lamb)
787 char buf[22];
789 fprintf(log," %12s %12s %12s\n"
790 " %12s %12.5f %12.5f\n\n",
791 "Step","Time","Lambda",gmx_step_str(steps,buf),time,lamb);
794 void print_ebin(ener_file_t fp_ene,bool bEne,bool bDR,bool bOR,
795 FILE *log,
796 gmx_large_int_t step,double time,
797 int mode,bool bCompact,
798 t_mdebin *md,t_fcdata *fcd,
799 gmx_groups_t *groups,t_grpopts *opts)
801 /*static char **grpnms=NULL;*/
802 char buf[246];
803 int i,j,n,ni,nj,ndr,nor;
804 int nr[enxNR];
805 real *block[enxNR];
806 t_enxframe fr;
808 switch (mode)
810 case eprNORMAL:
811 fr.t = time;
812 fr.step = step;
813 fr.nsteps = md->ebin->nsteps;
814 fr.nsum = md->ebin->nsum;
815 fr.nre = (bEne) ? md->ebin->nener : 0;
816 fr.ener = md->ebin->e;
817 fr.ndisre = bDR ? fcd->disres.npair : 0;
818 fr.disre_rm3tav = fcd->disres.rm3tav;
819 fr.disre_rt = fcd->disres.rt;
820 /* Optional additional blocks */
821 for(i=0; i<enxNR; i++)
823 nr[i] = 0;
825 if (fcd->orires.nr > 0 && bOR)
827 diagonalize_orires_tensors(&(fcd->orires));
828 nr[enxOR] = fcd->orires.nr;
829 block[enxOR] = fcd->orires.otav;
830 nr[enxORI] = (fcd->orires.oinsl != fcd->orires.otav) ?
831 fcd->orires.nr : 0;
832 block[enxORI] = fcd->orires.oinsl;
833 nr[enxORT] = fcd->orires.nex*12;
834 block[enxORT] = fcd->orires.eig;
836 fr.nblock = 0;
837 for(i=0; i<enxNR; i++)
839 if (nr[i] > 0)
841 fr.nblock = i + 1;
844 fr.nr = nr;
845 fr.block = block;
846 if (fr.nre || fr.ndisre || fr.nr[enxOR] || fr.nr[enxORI])
848 do_enx(fp_ene,&fr);
849 gmx_fio_check_file_position(enx_file_pointer(fp_ene));
850 if (fr.nre)
852 /* We have stored the sums, so reset the sum history */
853 reset_ebin_sums(md->ebin);
856 break;
857 case eprAVER:
858 if (log)
860 pprint(log,"A V E R A G E S",md);
862 break;
863 case eprRMS:
864 if (log)
866 pprint(log,"R M S - F L U C T U A T I O N S",md);
868 break;
869 default:
870 gmx_fatal(FARGS,"Invalid print mode (%d)",mode);
873 if (log)
875 for(i=0;i<opts->ngtc;i++)
877 if(opts->annealing[i]!=eannNO)
879 fprintf(log,"Current ref_t for group %s: %8.1f\n",
880 *(groups->grpname[groups->grps[egcTC].nm_ind[i]]),
881 opts->ref_t[i]);
884 if (mode==eprNORMAL && fcd->orires.nr>0)
886 print_orires_log(log,&(fcd->orires));
888 fprintf(log," Energies (%s)\n",unit_energy);
889 pr_ebin(log,md->ebin,md->ie,md->f_nre+md->nCrmsd,5,mode,TRUE);
890 fprintf(log,"\n");
892 if (!bCompact)
894 if (md->bDynBox)
896 pr_ebin(log,md->ebin,md->ib, md->bTricl ? NTRICLBOXS : NBOXS,5,
897 mode,TRUE);
898 fprintf(log,"\n");
900 if (md->bConstrVir)
902 fprintf(log," Constraint Virial (%s)\n",unit_energy);
903 pr_ebin(log,md->ebin,md->isvir,9,3,mode,FALSE);
904 fprintf(log,"\n");
905 fprintf(log," Force Virial (%s)\n",unit_energy);
906 pr_ebin(log,md->ebin,md->ifvir,9,3,mode,FALSE);
907 fprintf(log,"\n");
909 fprintf(log," Total Virial (%s)\n",unit_energy);
910 pr_ebin(log,md->ebin,md->ivir,9,3,mode,FALSE);
911 fprintf(log,"\n");
912 fprintf(log," Pressure (%s)\n",unit_pres_bar);
913 pr_ebin(log,md->ebin,md->ipres,9,3,mode,FALSE);
914 fprintf(log,"\n");
915 fprintf(log," Total Dipole (%s)\n",unit_dipole_D);
916 pr_ebin(log,md->ebin,md->imu,3,3,mode,FALSE);
917 fprintf(log,"\n");
919 if (md->nE > 1)
921 if (md->print_grpnms==NULL)
923 snew(md->print_grpnms,md->nE);
924 n=0;
925 for(i=0; (i<md->nEg); i++)
927 ni=groups->grps[egcENER].nm_ind[i];
928 for(j=i; (j<md->nEg); j++)
930 nj=groups->grps[egcENER].nm_ind[j];
931 sprintf(buf,"%s-%s",*(groups->grpname[ni]),
932 *(groups->grpname[nj]));
933 md->print_grpnms[n++]=strdup(buf);
937 sprintf(buf,"Epot (%s)",unit_energy);
938 fprintf(log,"%15s ",buf);
939 for(i=0; (i<egNR); i++)
941 if (md->bEInd[i])
943 fprintf(log,"%12s ",egrp_nm[i]);
946 fprintf(log,"\n");
947 for(i=0; (i<md->nE); i++)
949 fprintf(log,"%15s",md->print_grpnms[i]);
950 pr_ebin(log,md->ebin,md->igrp[i],md->nEc,md->nEc,mode,
951 FALSE);
953 fprintf(log,"\n");
955 if (md->nTC > 1)
957 pr_ebin(log,md->ebin,md->itemp,md->nTC,4,mode,TRUE);
958 fprintf(log,"\n");
960 if (md->nU > 1)
962 fprintf(log,"%15s %12s %12s %12s\n",
963 "Group","Ux","Uy","Uz");
964 for(i=0; (i<md->nU); i++)
966 ni=groups->grps[egcACC].nm_ind[i];
967 fprintf(log,"%15s",*groups->grpname[ni]);
968 pr_ebin(log,md->ebin,md->iu+3*i,3,3,mode,FALSE);
970 fprintf(log,"\n");
977 void
978 init_energyhistory(energyhistory_t * enerhist)
980 enerhist->nener = 0;
982 enerhist->ener_ave = NULL;
983 enerhist->ener_sum = NULL;
984 enerhist->ener_sum_sim = NULL;
986 enerhist->nsteps = 0;
987 enerhist->nsum = 0;
988 enerhist->nsteps_sim = 0;
989 enerhist->nsum_sim = 0;
992 void
993 update_energyhistory(energyhistory_t * enerhist,t_mdebin * mdebin)
995 int i;
997 enerhist->nsteps = mdebin->ebin->nsteps;
998 enerhist->nsum = mdebin->ebin->nsum;
999 enerhist->nsteps_sim = mdebin->ebin->nsteps_sim;
1000 enerhist->nsum_sim = mdebin->ebin->nsum_sim;
1001 enerhist->nener = mdebin->ebin->nener;
1003 if (mdebin->ebin->nsum > 0)
1005 /* Check if we need to allocate first */
1006 if(enerhist->ener_ave == NULL)
1008 snew(enerhist->ener_ave,enerhist->nener);
1009 snew(enerhist->ener_sum,enerhist->nener);
1012 for(i=0;i<enerhist->nener;i++)
1014 enerhist->ener_ave[i] = mdebin->ebin->e[i].eav;
1015 enerhist->ener_sum[i] = mdebin->ebin->e[i].esum;
1019 if (mdebin->ebin->nsum_sim > 0)
1021 /* Check if we need to allocate first */
1022 if(enerhist->ener_sum_sim == NULL)
1024 snew(enerhist->ener_sum_sim,enerhist->nener);
1027 for(i=0;i<enerhist->nener;i++)
1029 enerhist->ener_sum_sim[i] = mdebin->ebin->e_sim[i].esum;
1034 void
1035 restore_energyhistory_from_state(t_mdebin * mdebin,energyhistory_t * enerhist)
1037 int i;
1039 if ((enerhist->nsum > 0 || enerhist->nsum_sim > 0) &&
1040 mdebin->ebin->nener != enerhist->nener)
1042 gmx_fatal(FARGS,"Mismatch between number of energies in run input (%d) and checkpoint file (%d).",
1043 mdebin->ebin->nener,enerhist->nener);
1046 mdebin->ebin->nsteps = enerhist->nsteps;
1047 mdebin->ebin->nsum = enerhist->nsum;
1048 mdebin->ebin->nsteps_sim = enerhist->nsteps_sim;
1049 mdebin->ebin->nsum_sim = enerhist->nsum_sim;
1051 for(i=0; i<mdebin->ebin->nener; i++)
1053 mdebin->ebin->e[i].eav =
1054 (enerhist->nsum > 0 ? enerhist->ener_ave[i] : 0);
1055 mdebin->ebin->e[i].esum =
1056 (enerhist->nsum > 0 ? enerhist->ener_sum[i] : 0);
1057 mdebin->ebin->e_sim[i].esum =
1058 (enerhist->nsum_sim > 0 ? enerhist->ener_sum_sim[i] : 0);