4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
27 * Great Red Oystrich Makes All Chemists Sane
29 static char *SRCID_do_gct_c = "$Id$";
51 t_coupl_rec *init_coupling(FILE *log,int nfile,t_filenm fnm[],
52 t_commrec *cr,t_forcerec *fr,
53 t_mdatoms *md,t_idef *idef)
61 read_gct (opt2fn("-j",nfile,fnm), tcr);
62 write_gct(opt2fn("-jo",nfile,fnm),tcr,idef);
64 copy_ff(tcr,fr,md,idef);
66 /* Update all processors with coupling info */
68 comm_tcr(log,cr,&tcr);
73 static real Ecouple(t_coupl_rec *tcr,real ener[])
76 return ener[F_SR]+ener[F_LJ]+ener[F_LR]+ener[F_LJLR];
81 static char *mk_gct_nm(char *fn,int ftp,int ati,int atj)
87 sprintf(buf+strlen(fn)-4,"%d.%s",ati,ftp2ext(ftp));
89 sprintf(buf+strlen(fn)-4,"%d_%d.%s",ati,atj,ftp2ext(ftp));
94 static void pr_ff(t_coupl_rec *tcr,real time,t_idef *idef,
95 t_commrec *cr,int nfile,t_filenm fnm[])
98 static FILE **out=NULL;
99 static FILE **qq=NULL;
100 static FILE **ip=NULL;
104 char *leg[] = { "C12", "C6" };
105 char *bleg[] = { "A", "B", "C" };
109 if ((prop == NULL) && (out == NULL) && (qq == NULL) && (ip == NULL)) {
110 prop=xvgropen(opt2fn("-runav",nfile,fnm),
111 "Properties and Running Averages","Time (ps)","");
112 snew(raleg,2*eoObsNR);
113 for(i=j=0; (i<eoObsNR); i++) {
114 if (tcr->bObsUsed[i]) {
115 raleg[j++] = strdup(eoNames[i]);
116 sprintf(buf,"RA-%s",eoNames[i]);
117 raleg[j++] = strdup(buf);
120 xvgr_legend(prop,j,raleg);
127 for(i=0; (i<tcr->nLJ); i++) {
128 if (tcr->tcLJ[i].bPrint) {
129 tclj = &(tcr->tcLJ[i]);
131 xvgropen(mk_gct_nm(opt2fn("-ffout",nfile,fnm),
132 efXVG,tclj->at_i,tclj->at_j),
133 "General Coupling Lennard Jones","Time (ps)",
134 "Force constant (units)");
135 fprintf(out[i],"@ subtitle \"Interaction between types %d and %d\"\n",
136 tclj->at_i,tclj->at_j);
137 xvgr_legend(out[i],asize(leg),leg);
144 for(i=0; (i<tcr->nBU); i++) {
145 if (tcr->tcBU[i].bPrint) {
146 tcbu=&(tcr->tcBU[i]);
148 xvgropen(mk_gct_nm(opt2fn("-ffout",nfile,fnm),efXVG,
149 tcbu->at_i,tcbu->at_j),
150 "General Coupling Buckingham","Time (ps)",
151 "Force constant (units)");
152 fprintf(out[i],"@ subtitle \"Interaction between types %d and %d\"\n",
153 tcbu->at_i,tcbu->at_j);
154 xvgr_legend(out[i],asize(bleg),bleg);
160 for(i=0; (i<tcr->nQ); i++) {
161 if (tcr->tcQ[i].bPrint) {
162 qq[i] = xvgropen(mk_gct_nm(opt2fn("-ffout",nfile,fnm),efXVG,
163 tcr->tcQ[i].at_i,-1),
164 "General Coupling Charge","Time (ps)","Charge (e)");
165 fprintf(qq[i],"@ subtitle \"Type %d\"\n",tcr->tcQ[i].at_i);
170 for(i=0; (i<tcr->nIP); i++) {
171 sprintf(buf,"gctIP%d",tcr->tIP[i].type);
172 ip[i]=xvgropen(mk_gct_nm(opt2fn("-ffout",nfile,fnm),efXVG,0,-1),
173 "General Coupling iparams","Time (ps)","ip ()");
174 index=tcr->tIP[i].type;
175 fprintf(ip[i],"@ subtitle \"Coupling to %s\"\n",
176 interaction_function[idef->functype[index]].longname);
180 /* Write properties to file */
181 fprintf(prop,"%10.3f",time);
182 for(i=0; (i<eoObsNR); i++)
183 if (tcr->bObsUsed[i])
184 fprintf(prop," %10.3e %10.3e",tcr->act_value[i],tcr->av_value[i]);
188 for(i=0; (i<tcr->nLJ); i++) {
189 tclj=&(tcr->tcLJ[i]);
191 fprintf(out[i],"%14.7e %14.7e %14.7e\n",
192 time,tclj->c12,tclj->c6);
196 for(i=0; (i<tcr->nBU); i++) {
197 tcbu=&(tcr->tcBU[i]);
199 fprintf(out[i],"%14.7e %14.7e %14.7e %14.7e\n",
200 time,tcbu->a,tcbu->b,tcbu->c);
204 for(i=0; (i<tcr->nQ); i++) {
205 if (tcr->tcQ[i].bPrint) {
206 fprintf(qq[i],"%14.7e %14.7e\n",time,tcr->tcQ[i].Q);
210 for(i=0; (i<tcr->nIP); i++) {
211 fprintf(ip[i],"%10g ",time);
212 index=tcr->tIP[i].type;
213 switch(idef->functype[index]) {
215 fprintf(ip[i],"%10g %10g\n",tcr->tIP[i].iprint.harmonic.krA,
216 tcr->tIP[i].iprint.harmonic.rA);
225 static void pr_dev(t_coupl_rec *tcr,
226 real t,real dev[eoObsNR],t_commrec *cr,int nfile,t_filenm fnm[])
228 static FILE *fp=NULL;
233 fp=xvgropen(opt2fn("-devout",nfile,fnm),
234 "Deviations from target value","Time (ps)","");
236 for(i=j=0; (i<eoObsNR); i++)
237 if (tcr->bObsUsed[i])
238 ptr[j++] = eoNames[i];
239 xvgr_legend(fp,j,ptr);
242 fprintf(fp,"%10.3f",t);
243 for(i=0; (i<eoObsNR); i++)
244 if (tcr->bObsUsed[i])
245 fprintf(fp," %10.3e",dev[i]);
250 static void upd_nbfplj(FILE *log,real *nbfp,int atnr,real f6[],real f12[])
254 /* Update the nonbonded force parameters */
255 for(k=n=0; (n<atnr); n++) {
256 for(m=0; (m<atnr); m++,k++) {
257 C6 (nbfp,atnr,n,m) *= f6[k];
258 C12(nbfp,atnr,n,m) *= f12[k];
263 static void upd_nbfpbu(FILE *log,real *nbfp,int atnr,
264 real fa[],real fb[],real fc[])
268 /* Update the nonbonded force parameters */
269 for(k=n=0; (n<atnr); n++) {
270 for(m=0; (m<atnr); m++,k++) {
271 BHAMA(nbfp,atnr,n,m) *= fa[k];
272 BHAMB(nbfp,atnr,n,m) *= fb[k];
273 BHAMC(nbfp,atnr,n,m) *= fc[k];
278 void gprod(t_commrec *cr,int n,real f[])
280 /* Compute the global product of all elements in an array
281 * such that after gprod f[i] = PROD_j=1,nprocs f[i][j]
284 static real *buf[2] = { NULL, NULL };
290 srenew(buf[cur], nbuf);
291 srenew(buf[next],nbuf);
297 for(i=0; (i<cr->nnodes-1); i++) {
298 gmx_tx(cr->left, array(buf[cur],n));
299 gmx_rx(cr->right,array(buf[next],n));
300 gmx_wait(cr->left,cr->right);
301 /* Multiply f by factor read */
303 f[j] *= buf[next][j];
310 static void set_factor_matrix(int ntypes,real f[],real fmult,int ati,int atj)
316 fmult = min(FMAX,max(FMIN,fmult));
318 f[ntypes*ati+atj] *= fmult;
319 f[ntypes*atj+ati] *= fmult;
322 for(i=0; (i<ntypes); i++) {
323 f[ntypes*ati+i] *= fmult;
324 f[ntypes*i+ati] *= fmult;
331 static real calc_deviation(real xav,real xt,real x0)
335 /* This may prevent overshooting in GCT coupling... */
338 dev = /*max(x0-xav,x0-xt);*/ min(xav-x0,xt-x0);
344 dev = max(xav-x0,xt-x0);
351 static real calc_dist(FILE *log,rvec x[])
353 static bool bFirst=TRUE;
360 if ((buf = getenv("DISTGCT")) == NULL)
363 bDist = (sscanf(buf,"%d%d",&i1,&i2) == 2);
365 fprintf(log,"GCT: Will couple to distance between %d and %d\n",i1,i2);
367 fprintf(log,"GCT: Will not couple to distances\n");
372 rvec_sub(x[i1],x[i2],dx);
379 real run_aver(real old,real cur,int step,int nmem)
383 return ((nmem-1)*old+cur)/nmem;
386 static void set_act_value(t_coupl_rec *tcr,int index,real val,int step)
388 tcr->act_value[index] = val;
389 tcr->av_value[index] = run_aver(tcr->av_value[index],val,step,tcr->nmemory);
392 static void upd_f_value(FILE *log,int atnr,real xi,real dt,real factor,
393 real ff[],int ati,int atj)
398 fff = 1 + (dt/xi) * factor;
400 set_factor_matrix(atnr,ff,sqrt(fff),ati,atj);
404 static void dump_fm(FILE *fp,int n,real f[],char *s)
408 fprintf(fp,"Factor matrix (all numbers -1) %s\n",s);
409 for(i=0; (i<n); i++) {
411 fprintf(fp," %10.3e",f[n*i+j]-1.0);
416 void do_coupling(FILE *log,int nfile,t_filenm fnm[],
417 t_coupl_rec *tcr,real t,int step,real ener[],
418 t_forcerec *fr,t_inputrec *ir,bool bMaster,
419 t_mdatoms *md,t_idef *idef,real mu_aver,int nmols,
420 t_commrec *cr,matrix box,tensor virial,
421 tensor pres,rvec mu_tot,
422 rvec x[],rvec f[],bool bDoIt)
424 #define enm2Debye 48.0321
425 #define d2e(x) (x)/enm2Debye
426 #define enm2kjmol(x) (x)*0.0143952 /* = 2.0*4.0*M_PI*EPSILON0 */
428 static real *f6,*f12,*fa,*fb,*fc,*fq;
429 static bool bFirst = TRUE;
431 int i,j,ati,atj,atnr2,type,ftype;
432 real deviation[eoObsNR],prdev[eoObsNR],epot0,dist,rmsf;
433 real ff6,ff12,ffa,ffb,ffc,ffq,factor,dt,mu_ind;
434 real Epol,Eintern,Virial,muabs,xiH=-1,xiS=-1,xi6,xi12;
440 t_coupl_iparams *tip;
442 atnr2 = idef->atnr * idef->atnr;
445 fprintf(log,"GCT: this is parallel\n");
447 fprintf(log,"GCT: this is not parallel\n");
454 snew(fq, idef->atnr);
461 for(i=0; (i<ir->opts.ngtc); i++) {
462 nrdf += ir->opts.nrdf[i];
463 TTT += ir->opts.nrdf[i]*ir->opts.ref_t[i];
467 /* Calculate reference virial from reference temperature and pressure */
468 tcr->ref_value[eoVir] = 0.5*BOLTZ*nrdf*TTT - (3.0/2.0)*
469 Vol*tcr->ref_value[eoPres];
471 fprintf(log,"GCT: TTT = %g, nrdf = %d, vir0 = %g, Vol = %g\n",
472 TTT,nrdf,tcr->ref_value[eoVir],Vol);
478 bPrint = MASTER(cr) && do_per_step(step,ir->nstlog);
481 /* Initiate coupling to the reference pressure and temperature to start
485 for(i=0; (i<eoObsNR); i++)
486 tcr->av_value[i] = tcr->ref_value[i];
487 if ((tcr->ref_value[eoDipole]) != 0.0) {
488 mu_ind = mu_aver - d2e(tcr->ref_value[eoDipole]); /* in e nm */
489 Epol = mu_ind*mu_ind/(enm2kjmol(tcr->ref_value[eoPolarizability]));
490 tcr->av_value[eoEpot] -= Epol;
491 fprintf(log,"GCT: mu_aver = %g(D), mu_ind = %g(D), Epol = %g (kJ/mol)\n",
492 mu_aver*enm2Debye,mu_ind*enm2Debye,Epol);
496 /* We want to optimize the LJ params, usually to the Vaporization energy
497 * therefore we only count intermolecular degrees of freedom.
498 * Note that this is now optional. switch UseEinter to yes in your gct file
501 dist = calc_dist(log,x);
502 muabs = norm(mu_tot);
503 Eintern = Ecouple(tcr,ener)/nmols;
504 Virial = virial[XX][XX]+virial[YY][YY]+virial[ZZ][ZZ];
506 /*calc_force(md->nr,f,fmol);*/
509 /* Use a memory of tcr->nmemory steps, so we actually couple to the
510 * average observable over the last tcr->nmemory steps. This may help
511 * in avoiding local minima in parameter space.
513 set_act_value(tcr,eoPres, ener[F_PRES],step);
514 set_act_value(tcr,eoEpot, Eintern, step);
515 set_act_value(tcr,eoVir, Virial, step);
516 set_act_value(tcr,eoDist, dist, step);
517 set_act_value(tcr,eoMu, muabs, step);
518 set_act_value(tcr,eoFx, fmol[0][XX], step);
519 set_act_value(tcr,eoFy, fmol[0][YY], step);
520 set_act_value(tcr,eoFz, fmol[0][ZZ], step);
521 set_act_value(tcr,eoPx, pres[XX][XX],step);
522 set_act_value(tcr,eoPy, pres[YY][YY],step);
523 set_act_value(tcr,eoPz, pres[ZZ][ZZ],step);
525 epot0 = tcr->ref_value[eoEpot];
526 /* If dipole != 0.0 assume we want to use polarization corrected coupling */
527 if ((tcr->ref_value[eoDipole]) != 0.0) {
528 mu_ind = mu_aver - d2e(tcr->ref_value[eoDipole]); /* in e nm */
530 Epol = mu_ind*mu_ind/(enm2kjmol(tcr->ref_value[eoPolarizability]));
534 fprintf(debug,"mu_ind: %g (%g D) mu_aver: %g (%g D)\n",
535 mu_ind,mu_ind*enm2Debye,mu_aver,mu_aver*enm2Debye);
536 fprintf(debug,"Eref %g Epol %g Erunav %g Eact %g\n",
537 tcr->ref_value[eoEpot],Epol,tcr->av_value[eoEpot],
538 tcr->act_value[eoEpot]);
543 pr_ff(tcr,t,idef,cr,nfile,fnm);
545 /* Calculate the deviation of average value from the target value */
546 for(i=0; (i<eoObsNR); i++) {
547 deviation[i] = calc_deviation(tcr->av_value[i],tcr->act_value[i],
549 prdev[i] = tcr->ref_value[i] - tcr->act_value[i];
551 deviation[eoEpot] = calc_deviation(tcr->av_value[eoEpot],tcr->act_value[eoEpot],
553 prdev[eoEpot] = epot0 - tcr->act_value[eoEpot];
556 pr_dev(tcr,t,prdev,cr,nfile,fnm);
558 /* First set all factors to 1 */
559 for(i=0; (i<atnr2); i++) {
560 f6[i] = f12[i] = fa[i] = fb[i] = fc[i] = 1.0;
562 for(i=0; (i<idef->atnr); i++)
565 /* Now compute the actual coupling compononents */
568 for(i=0; (i<tcr->nLJ); i++) {
569 tclj=&(tcr->tcLJ[i]);
576 if (tclj->eObs == eoForce) {
577 fatal_error(0,"Hack code for this to work again ");
579 fprintf(debug,"Have computed derivatives: xiH = %g, xiS = %g\n",xiH,xiS);
589 fatal_error(0,"No H, no Shell, edit code at %s, line %d\n",
592 set_factor_matrix(idef->atnr,f6, sqrt(ff6), ati,atj);
594 set_factor_matrix(idef->atnr,f12,sqrt(ff12),ati,atj);
598 fprintf(debug,"tcr->tcLJ[%d].xi_6 = %g, xi_12 = %g deviation = %g\n",i,
599 tclj->xi_6,tclj->xi_12,deviation[tclj->eObs]);
600 factor=deviation[tclj->eObs];
602 upd_f_value(log,idef->atnr,tclj->xi_6, dt,factor,f6, ati,atj);
603 upd_f_value(log,idef->atnr,tclj->xi_12,dt,factor,f12,ati,atj);
611 dump_fm(log,idef->atnr,f6,"f6");
612 dump_fm(log,idef->atnr,f12,"f12");
615 upd_nbfplj(log,fr->nbfp,idef->atnr,f6,f12);
617 /* Copy for printing */
618 for(i=0; (i<tcr->nLJ); i++) {
619 tclj=&(tcr->tcLJ[i]);
624 tclj->c6 = C6(fr->nbfp,fr->ntype,ati,atj);
625 tclj->c12 = C12(fr->nbfp,fr->ntype,ati,atj);
630 for(i=0; (i<tcr->nBU); i++) {
631 tcbu = &(tcr->tcBU[i]);
632 factor = deviation[tcbu->eObs];
636 upd_f_value(log,idef->atnr,tcbu->xi_a,dt,factor,fa,ati,atj);
637 upd_f_value(log,idef->atnr,tcbu->xi_b,dt,factor,fb,ati,atj);
638 upd_f_value(log,idef->atnr,tcbu->xi_c,dt,factor,fc,ati,atj);
646 upd_nbfpbu(log,fr->nbfp,idef->atnr,fa,fb,fc);
647 /* Copy for printing */
648 for(i=0; (i<tcr->nBU); i++) {
649 tcbu=&(tcr->tcBU[i]);
654 tcbu->a = BHAMA(fr->nbfp,fr->ntype,ati,atj);
655 tcbu->b = BHAMB(fr->nbfp,fr->ntype,ati,atj);
656 tcbu->c = BHAMC(fr->nbfp,fr->ntype,ati,atj);
658 fprintf(debug,"buck (type=%d) = %e, %e, %e\n",
659 tcbu->at_i,tcbu->a,tcbu->b,tcbu->c);
663 for(i=0; (i<tcr->nQ); i++) {
666 ffq = 1.0 + (dt/tcq->xi_Q) * deviation[tcq->eObs];
669 fq[tcq->at_i] *= ffq;
673 gprod(cr,idef->atnr,fq);
675 for(j=0; (j<md->nr); j++) {
676 md->chargeA[j] *= fq[md->typeA[j]];
678 for(i=0; (i<tcr->nQ); i++) {
680 for(j=0; (j<md->nr); j++) {
681 if (md->typeA[j] == tcq->at_i) {
682 tcq->Q = md->chargeA[j];
687 fatal_error(0,"Coupling type %d not found",tcq->at_i);
689 for(i=0; (i<tcr->nIP); i++) {
690 tip = &(tcr->tIP[i]);
692 ftype = idef->functype[type];
693 factor = dt*deviation[tip->eObs];
695 /* Time for a killer macro */
696 #define DOIP(ip) if (tip->xi.##ip) idef->iparams[type].##ip *= (1+factor/tip->xi.##ip)
707 tip->iprint=idef->iparams[type];