3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
57 t_coupl_rec *init_coupling(FILE *log,int nfile, const t_filenm fnm[],
58 t_commrec *cr,t_forcerec *fr,
59 t_mdatoms *md,t_idef *idef)
67 read_gct (opt2fn("-j",nfile,fnm), tcr);
68 write_gct(opt2fn("-jo",nfile,fnm),tcr,idef);
70 /* Update all processors with coupling info */
72 comm_tcr(log,cr,&tcr);
74 /* Copy information from the coupling to the force field stuff */
75 copy_ff(tcr,fr,md,idef);
80 static real Ecouple(t_coupl_rec *tcr,real ener[])
83 return ener[F_COUL_SR]+ener[F_LJ]+ener[F_COUL_LR]+ener[F_LJ_LR]+
84 ener[F_RF_EXCL]+ener[F_COUL_RECIP];
89 static char *mk_gct_nm(const char *fn,int ftp,int ati,int atj)
95 sprintf(buf+strlen(fn)-4,"%d.%s",ati,ftp2ext(ftp));
97 sprintf(buf+strlen(fn)-4,"%d_%d.%s",ati,atj,ftp2ext(ftp));
102 static void pr_ff(t_coupl_rec *tcr,real time,t_idef *idef,
103 t_commrec *cr,int nfile,const t_filenm fnm[],
104 const output_env_t oenv)
107 static FILE **out=NULL;
108 static FILE **qq=NULL;
109 static FILE **ip=NULL;
113 const char *leg[] = { "C12", "C6" };
114 const char *eleg[] = { "Epsilon", "Sigma" };
115 const char *bleg[] = { "A", "B", "C" };
119 if ((prop == NULL) && (out == NULL) && (qq == NULL) && (ip == NULL)) {
120 prop=xvgropen(opt2fn("-runav",nfile,fnm),
121 "Properties and Running Averages","Time (ps)","",oenv);
122 snew(raleg,2*eoObsNR);
123 for(i=j=0; (i<eoObsNR); i++) {
124 if (tcr->bObsUsed[i]) {
125 raleg[j++] = strdup(eoNames[i]);
126 sprintf(buf,"RA-%s",eoNames[i]);
127 raleg[j++] = strdup(buf);
130 xvgr_legend(prop,j,(const char**)raleg,oenv);
137 for(i=0; (i<tcr->nLJ); i++) {
138 if (tcr->tcLJ[i].bPrint) {
139 tclj = &(tcr->tcLJ[i]);
141 xvgropen(mk_gct_nm(opt2fn("-ffout",nfile,fnm),
142 efXVG,tclj->at_i,tclj->at_j),
143 "General Coupling Lennard Jones","Time (ps)",
144 "Force constant (units)",oenv);
145 fprintf(out[i],"@ subtitle \"Interaction between types %d and %d\"\n",
146 tclj->at_i,tclj->at_j);
147 if (tcr->combrule == 1)
148 xvgr_legend(out[i],asize(leg),leg,oenv);
150 xvgr_legend(out[i],asize(eleg),eleg,oenv);
157 for(i=0; (i<tcr->nBU); i++) {
158 if (tcr->tcBU[i].bPrint) {
159 tcbu=&(tcr->tcBU[i]);
161 xvgropen(mk_gct_nm(opt2fn("-ffout",nfile,fnm),efXVG,
162 tcbu->at_i,tcbu->at_j),
163 "General Coupling Buckingham","Time (ps)",
164 "Force constant (units)",oenv);
165 fprintf(out[i],"@ subtitle \"Interaction between types %d and %d\"\n",
166 tcbu->at_i,tcbu->at_j);
167 xvgr_legend(out[i],asize(bleg),bleg,oenv);
173 for(i=0; (i<tcr->nQ); i++) {
174 if (tcr->tcQ[i].bPrint) {
175 qq[i] = xvgropen(mk_gct_nm(opt2fn("-ffout",nfile,fnm),efXVG,
176 tcr->tcQ[i].at_i,-1),
177 "General Coupling Charge","Time (ps)","Charge (e)",
179 fprintf(qq[i],"@ subtitle \"Type %d\"\n",tcr->tcQ[i].at_i);
184 for(i=0; (i<tcr->nIP); i++) {
185 sprintf(buf,"gctIP%d",tcr->tIP[i].type);
186 ip[i]=xvgropen(mk_gct_nm(opt2fn("-ffout",nfile,fnm),efXVG,0,-1),
187 "General Coupling iparams","Time (ps)","ip ()",oenv);
188 index=tcr->tIP[i].type;
189 fprintf(ip[i],"@ subtitle \"Coupling to %s\"\n",
190 interaction_function[idef->functype[index]].longname);
194 /* Write properties to file */
195 fprintf(prop,"%10.3f",time);
196 for(i=0; (i<eoObsNR); i++)
197 if (tcr->bObsUsed[i])
198 fprintf(prop," %10.3e %10.3e",tcr->act_value[i],tcr->av_value[i]);
202 for(i=0; (i<tcr->nLJ); i++) {
203 tclj=&(tcr->tcLJ[i]);
205 if (tcr->combrule == 1)
206 fprintf(out[i],"%14.7e %14.7e %14.7e\n",
207 time,tclj->c12,tclj->c6);
209 double sigma = pow(tclj->c12/tclj->c6,1.0/6.0);
210 double epsilon = 0.25*sqr(tclj->c6)/tclj->c12;
211 fprintf(out[i],"%14.7e %14.7e %14.7e\n",
217 for(i=0; (i<tcr->nBU); i++) {
218 tcbu=&(tcr->tcBU[i]);
220 fprintf(out[i],"%14.7e %14.7e %14.7e %14.7e\n",
221 time,tcbu->a,tcbu->b,tcbu->c);
225 for(i=0; (i<tcr->nQ); i++) {
226 if (tcr->tcQ[i].bPrint) {
227 fprintf(qq[i],"%14.7e %14.7e\n",time,tcr->tcQ[i].Q);
231 for(i=0; (i<tcr->nIP); i++) {
232 fprintf(ip[i],"%10g ",time);
233 index=tcr->tIP[i].type;
234 switch(idef->functype[index]) {
236 fprintf(ip[i],"%10g %10g\n",tcr->tIP[i].iprint.harmonic.krA,
237 tcr->tIP[i].iprint.harmonic.rA);
246 static void pr_dev(t_coupl_rec *tcr,
247 real t,real dev[eoObsNR],t_commrec *cr,int nfile,
248 const t_filenm fnm[],const output_env_t oenv)
250 static FILE *fp=NULL;
255 fp=xvgropen(opt2fn("-devout",nfile,fnm),
256 "Deviations from target value","Time (ps)","",oenv);
258 for(i=j=0; (i<eoObsNR); i++)
259 if (tcr->bObsUsed[i])
260 ptr[j++] = strdup(eoNames[i]);
261 xvgr_legend(fp,j,(const char**)ptr,oenv);
266 fprintf(fp,"%10.3f",t);
267 for(i=0; (i<eoObsNR); i++)
268 if (tcr->bObsUsed[i])
269 fprintf(fp," %10.3e",dev[i]);
274 static void upd_nbfplj(FILE *log,real *nbfp,int atnr,real f6[],real f12[],
277 double *sigma,*epsilon,c6,c12,eps,sig,sig6;
280 /* Update the nonbonded force parameters */
283 for(k=n=0; (n<atnr); n++) {
284 for(m=0; (m<atnr); m++,k++) {
285 C6 (nbfp,atnr,n,m) *= f6[k];
286 C12(nbfp,atnr,n,m) *= f12[k];
292 /* Convert to sigma and epsilon */
295 for(n=0; (n<atnr); n++) {
297 c6 = C6 (nbfp,atnr,n,n) * f6[k];
298 c12 = C12(nbfp,atnr,n,n) * f12[k];
299 if ((c6 == 0) || (c12 == 0))
300 gmx_fatal(FARGS,"You can not use combination rule %d with zero C6 (%f) or C12 (%f)",combrule,c6,c12);
301 sigma[n] = pow(c12/c6,1.0/6.0);
302 epsilon[n] = 0.25*(c6*c6/c12);
304 for(k=n=0; (n<atnr); n++) {
305 for(m=0; (m<atnr); m++,k++) {
306 eps = sqrt(epsilon[n]*epsilon[m]);
308 sig = 0.5*(sigma[n]+sigma[m]);
310 sig = sqrt(sigma[n]*sigma[m]);
312 /* nbfp now includes the 6.0/12.0 derivative prefactors */
313 C6 (nbfp,atnr,n,m) = 4*eps*sig6/6.0;
314 C12(nbfp,atnr,n,m) = 4*eps*sig6*sig6/12.0;
321 gmx_fatal(FARGS,"Combination rule should be 1,2 or 3 instead of %d",
326 static void upd_nbfpbu(FILE *log,real *nbfp,int atnr,
327 real fa[],real fb[],real fc[])
331 /* Update the nonbonded force parameters */
332 for(k=n=0; (n<atnr); n++) {
333 for(m=0; (m<atnr); m++,k++) {
334 BHAMA(nbfp,atnr,n,m) *= fa[k];
335 BHAMB(nbfp,atnr,n,m) *= fb[k];
336 BHAMC(nbfp,atnr,n,m) *= fc[k];
341 void gprod(t_commrec *cr,int n,real f[])
343 /* Compute the global product of all elements in an array
344 * such that after gprod f[i] = PROD_j=1,nprocs f[i][j]
347 static real *buf=NULL;
357 MPI_Allreduce(f,buf,n,MPI_DOUBLE,MPI_PROD,cr->mpi_comm_mygroup);
359 MPI_Allreduce(f,buf,n,MPI_FLOAT, MPI_PROD,cr->mpi_comm_mygroup);
366 static void set_factor_matrix(int ntypes,real f[],real fmult,int ati,int atj)
372 fmult = min(FMAX,max(FMIN,fmult));
374 f[ntypes*ati+atj] *= fmult;
375 f[ntypes*atj+ati] *= fmult;
378 for(i=0; (i<ntypes); i++) {
379 f[ntypes*ati+i] *= fmult;
380 f[ntypes*i+ati] *= fmult;
387 static real calc_deviation(real xav,real xt,real x0)
389 /* This may prevent overshooting in GCT coupling... */
395 dev = min(xav-x0,xt-x0);
401 dev = max(xav-x0,xt-x0);
409 static real calc_dist(FILE *log,rvec x[])
411 static gmx_bool bFirst=TRUE;
412 static gmx_bool bDist;
418 if ((buf = getenv("DISTGCT")) == NULL)
421 bDist = (sscanf(buf,"%d%d",&i1,&i2) == 2);
423 fprintf(log,"GCT: Will couple to distance between %d and %d\n",i1,i2);
425 fprintf(log,"GCT: Will not couple to distances\n");
430 rvec_sub(x[i1],x[i2],dx);
437 real run_aver(real old,real cur,int step,int nmem)
441 return ((nmem-1)*old+cur)/nmem;
444 static void set_act_value(t_coupl_rec *tcr,int index,real val,int step)
446 tcr->act_value[index] = val;
447 tcr->av_value[index] = run_aver(tcr->av_value[index],val,step,tcr->nmemory);
450 static void upd_f_value(FILE *log,int atnr,real xi,real dt,real factor,
451 real ff[],int ati,int atj)
456 fff = 1 + (dt/xi) * factor;
458 set_factor_matrix(atnr,ff,sqrt(fff),ati,atj);
462 static void dump_fm(FILE *fp,int n,real f[],char *s)
466 fprintf(fp,"Factor matrix (all numbers -1) %s\n",s);
467 for(i=0; (i<n); i++) {
469 fprintf(fp," %10.3e",f[n*i+j]-1.0);
474 void do_coupling(FILE *log,const output_env_t oenv,int nfile,
475 const t_filenm fnm[], t_coupl_rec *tcr,real t,
476 int step,real ener[], t_forcerec *fr,t_inputrec *ir,
477 gmx_bool bMaster, t_mdatoms *md,t_idef *idef,real mu_aver,int nmols,
478 t_commrec *cr,matrix box,tensor virial,
479 tensor pres,rvec mu_tot,
480 rvec x[],rvec f[],gmx_bool bDoIt)
482 #define enm2Debye 48.0321
483 #define d2e(x) (x)/enm2Debye
484 #define enm2kjmol(x) (x)*0.0143952 /* = 2.0*4.0*M_PI*EPSILON0 */
486 static real *f6,*f12,*fa,*fb,*fc,*fq;
487 static gmx_bool bFirst = TRUE;
489 int i,j,ati,atj,atnr2,type,ftype;
490 real deviation[eoObsNR],prdev[eoObsNR],epot0,dist,rmsf;
491 real ff6,ff12,ffa,ffb,ffc,ffq,factor,dt,mu_ind;
492 real Epol,Eintern,Virial,muabs,xiH=-1,xiS=-1,xi6,xi12;
494 gmx_bool bTest,bPrint;
498 t_coupl_iparams *tip;
500 atnr2 = idef->atnr * idef->atnr;
503 fprintf(log,"GCT: this is parallel\n");
505 fprintf(log,"GCT: this is not parallel\n");
512 snew(fq, idef->atnr);
519 for(i=0; (i<ir->opts.ngtc); i++) {
520 nrdf += ir->opts.nrdf[i];
521 TTT += ir->opts.nrdf[i]*ir->opts.ref_t[i];
525 /* Calculate reference virial from reference temperature and pressure */
526 tcr->ref_value[eoVir] = 0.5*BOLTZ*nrdf*TTT - (3.0/2.0)*
527 Vol*tcr->ref_value[eoPres];
529 fprintf(log,"GCT: TTT = %g, nrdf = %d, vir0 = %g, Vol = %g\n",
530 TTT,nrdf,tcr->ref_value[eoVir],Vol);
536 bPrint = MASTER(cr) && do_per_step(step,ir->nstlog);
539 /* Initiate coupling to the reference pressure and temperature to start
543 for(i=0; (i<eoObsNR); i++)
544 tcr->av_value[i] = tcr->ref_value[i];
545 if ((tcr->ref_value[eoDipole]) != 0.0) {
546 mu_ind = mu_aver - d2e(tcr->ref_value[eoDipole]); /* in e nm */
547 Epol = mu_ind*mu_ind/(enm2kjmol(tcr->ref_value[eoPolarizability]));
548 tcr->av_value[eoEpot] -= Epol;
549 fprintf(log,"GCT: mu_aver = %g(D), mu_ind = %g(D), Epol = %g (kJ/mol)\n",
550 mu_aver*enm2Debye,mu_ind*enm2Debye,Epol);
554 /* We want to optimize the LJ params, usually to the Vaporization energy
555 * therefore we only count intermolecular degrees of freedom.
556 * Note that this is now optional. switch UseEinter to yes in your gct file
559 dist = calc_dist(log,x);
560 muabs = norm(mu_tot);
561 Eintern = Ecouple(tcr,ener)/nmols;
562 Virial = virial[XX][XX]+virial[YY][YY]+virial[ZZ][ZZ];
564 /*calc_force(md->nr,f,fmol);*/
567 /* Use a memory of tcr->nmemory steps, so we actually couple to the
568 * average observable over the last tcr->nmemory steps. This may help
569 * in avoiding local minima in parameter space.
571 set_act_value(tcr,eoPres, ener[F_PRES],step);
572 set_act_value(tcr,eoEpot, Eintern, step);
573 set_act_value(tcr,eoVir, Virial, step);
574 set_act_value(tcr,eoDist, dist, step);
575 set_act_value(tcr,eoMu, muabs, step);
576 set_act_value(tcr,eoFx, fmol[0][XX], step);
577 set_act_value(tcr,eoFy, fmol[0][YY], step);
578 set_act_value(tcr,eoFz, fmol[0][ZZ], step);
579 set_act_value(tcr,eoPx, pres[XX][XX],step);
580 set_act_value(tcr,eoPy, pres[YY][YY],step);
581 set_act_value(tcr,eoPz, pres[ZZ][ZZ],step);
583 epot0 = tcr->ref_value[eoEpot];
584 /* If dipole != 0.0 assume we want to use polarization corrected coupling */
585 if ((tcr->ref_value[eoDipole]) != 0.0) {
586 mu_ind = mu_aver - d2e(tcr->ref_value[eoDipole]); /* in e nm */
588 Epol = mu_ind*mu_ind/(enm2kjmol(tcr->ref_value[eoPolarizability]));
592 fprintf(debug,"mu_ind: %g (%g D) mu_aver: %g (%g D)\n",
593 mu_ind,mu_ind*enm2Debye,mu_aver,mu_aver*enm2Debye);
594 fprintf(debug,"Eref %g Epol %g Erunav %g Eact %g\n",
595 tcr->ref_value[eoEpot],Epol,tcr->av_value[eoEpot],
596 tcr->act_value[eoEpot]);
601 pr_ff(tcr,t,idef,cr,nfile,fnm,oenv);
603 /* Calculate the deviation of average value from the target value */
604 for(i=0; (i<eoObsNR); i++) {
605 deviation[i] = calc_deviation(tcr->av_value[i],tcr->act_value[i],
607 prdev[i] = tcr->ref_value[i] - tcr->act_value[i];
609 deviation[eoEpot] = calc_deviation(tcr->av_value[eoEpot],tcr->act_value[eoEpot],
611 prdev[eoEpot] = epot0 - tcr->act_value[eoEpot];
614 pr_dev(tcr,t,prdev,cr,nfile,fnm,oenv);
616 /* First set all factors to 1 */
617 for(i=0; (i<atnr2); i++) {
618 f6[i] = f12[i] = fa[i] = fb[i] = fc[i] = 1.0;
620 for(i=0; (i<idef->atnr); i++)
623 /* Now compute the actual coupling compononents */
626 for(i=0; (i<tcr->nLJ); i++) {
627 tclj=&(tcr->tcLJ[i]);
634 if (tclj->eObs == eoForce) {
635 gmx_fatal(FARGS,"Hack code for this to work again ");
637 fprintf(debug,"Have computed derivatives: xiH = %g, xiS = %g\n",xiH,xiS);
647 gmx_fatal(FARGS,"No H, no Shell, edit code at %s, line %d\n",
650 set_factor_matrix(idef->atnr,f6, sqrt(ff6), ati,atj);
652 set_factor_matrix(idef->atnr,f12,sqrt(ff12),ati,atj);
656 fprintf(debug,"tcr->tcLJ[%d].xi_6 = %g, xi_12 = %g deviation = %g\n",i,
657 tclj->xi_6,tclj->xi_12,deviation[tclj->eObs]);
658 factor=deviation[tclj->eObs];
660 upd_f_value(log,idef->atnr,tclj->xi_6, dt,factor,f6, ati,atj);
661 upd_f_value(log,idef->atnr,tclj->xi_12,dt,factor,f12,ati,atj);
669 dump_fm(log,idef->atnr,f6,"f6");
670 dump_fm(log,idef->atnr,f12,"f12");
673 upd_nbfplj(log,fr->nbfp,idef->atnr,f6,f12,tcr->combrule);
675 /* Copy for printing */
676 for(i=0; (i<tcr->nLJ); i++) {
677 tclj=&(tcr->tcLJ[i]);
684 /* nbfp now includes the 6.0/12.0 derivative prefactors */
685 tclj->c6 = C6(fr->nbfp,fr->ntype,ati,atj)/6.0;
686 tclj->c12 = C12(fr->nbfp,fr->ntype,ati,atj)/12.0;
691 for(i=0; (i<tcr->nBU); i++) {
692 tcbu = &(tcr->tcBU[i]);
693 factor = deviation[tcbu->eObs];
697 upd_f_value(log,idef->atnr,tcbu->xi_a,dt,factor,fa,ati,atj);
698 upd_f_value(log,idef->atnr,tcbu->xi_b,dt,factor,fb,ati,atj);
699 upd_f_value(log,idef->atnr,tcbu->xi_c,dt,factor,fc,ati,atj);
707 upd_nbfpbu(log,fr->nbfp,idef->atnr,fa,fb,fc);
708 /* Copy for printing */
709 for(i=0; (i<tcr->nBU); i++) {
710 tcbu=&(tcr->tcBU[i]);
717 /* nbfp now includes the 6.0 derivative prefactors */
718 tcbu->a = BHAMA(fr->nbfp,fr->ntype,ati,atj);
719 tcbu->b = BHAMB(fr->nbfp,fr->ntype,ati,atj);
720 tcbu->c = BHAMC(fr->nbfp,fr->ntype,ati,atj)/6.0;
722 fprintf(debug,"buck (type=%d) = %e, %e, %e\n",
723 tcbu->at_i,tcbu->a,tcbu->b,tcbu->c);
727 for(i=0; (i<tcr->nQ); i++) {
730 ffq = 1.0 + (dt/tcq->xi_Q) * deviation[tcq->eObs];
733 fq[tcq->at_i] *= ffq;
737 gprod(cr,idef->atnr,fq);
739 for(j=0; (j<md->nr); j++) {
740 md->chargeA[j] *= fq[md->typeA[j]];
742 for(i=0; (i<tcr->nQ); i++) {
744 for(j=0; (j<md->nr); j++) {
745 if (md->typeA[j] == tcq->at_i) {
746 tcq->Q = md->chargeA[j];
751 gmx_fatal(FARGS,"Coupling type %d not found",tcq->at_i);
753 for(i=0; (i<tcr->nIP); i++) {
754 tip = &(tcr->tIP[i]);
756 ftype = idef->functype[type];
757 factor = dt*deviation[tip->eObs];
761 if (tip->xi.harmonic.krA) idef->iparams[type].harmonic.krA *= (1+factor/tip->xi.harmonic.krA);
762 if (tip->xi.harmonic.rA) idef->iparams[type].harmonic.rA *= (1+factor/tip->xi.harmonic.rA);
767 tip->iprint=idef->iparams[type];