4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Copyright (c) 1991-2001, University of Groningen, The Netherlands
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * If you want to redistribute modifications, please consider that
18 * scientific software is very special. Version control is crucial -
19 * bugs must be traceable. We will be happy to consider code for
20 * inclusion in the official distribution, but derived work must not
21 * be called official GROMACS. Details are found in the README & COPYING
22 * files - if they are missing, get the official version at www.gromacs.org.
24 * To help us fund GROMACS development, we humbly ask that you cite
25 * the papers on the package - you can find them in the top README file.
27 * For more info, check our website at http://www.gromacs.org
30 * GROningen Mixture of Alchemy and Childrens' Stories
53 t_coupl_rec
*init_coupling(FILE *log
,int nfile
,t_filenm fnm
[],
54 t_commrec
*cr
,t_forcerec
*fr
,
55 t_mdatoms
*md
,t_idef
*idef
)
63 read_gct (opt2fn("-j",nfile
,fnm
), tcr
);
64 write_gct(opt2fn("-jo",nfile
,fnm
),tcr
,idef
);
66 copy_ff(tcr
,fr
,md
,idef
);
68 /* Update all processors with coupling info */
70 comm_tcr(log
,cr
,&tcr
);
75 static real
Ecouple(t_coupl_rec
*tcr
,real ener
[])
78 return ener
[F_SR
]+ener
[F_LJ
]+ener
[F_LR
]+ener
[F_LJLR
];
83 static char *mk_gct_nm(char *fn
,int ftp
,int ati
,int atj
)
89 sprintf(buf
+strlen(fn
)-4,"%d.%s",ati
,ftp2ext(ftp
));
91 sprintf(buf
+strlen(fn
)-4,"%d_%d.%s",ati
,atj
,ftp2ext(ftp
));
96 static void pr_ff(t_coupl_rec
*tcr
,real time
,t_idef
*idef
,
97 t_commrec
*cr
,int nfile
,t_filenm fnm
[])
100 static FILE **out
=NULL
;
101 static FILE **qq
=NULL
;
102 static FILE **ip
=NULL
;
106 char *leg
[] = { "C12", "C6" };
107 char *bleg
[] = { "A", "B", "C" };
111 if ((prop
== NULL
) && (out
== NULL
) && (qq
== NULL
) && (ip
== NULL
)) {
112 prop
=xvgropen(opt2fn("-runav",nfile
,fnm
),
113 "Properties and Running Averages","Time (ps)","");
114 snew(raleg
,2*eoObsNR
);
115 for(i
=j
=0; (i
<eoObsNR
); i
++) {
116 if (tcr
->bObsUsed
[i
]) {
117 raleg
[j
++] = strdup(eoNames
[i
]);
118 sprintf(buf
,"RA-%s",eoNames
[i
]);
119 raleg
[j
++] = strdup(buf
);
122 xvgr_legend(prop
,j
,raleg
);
129 for(i
=0; (i
<tcr
->nLJ
); i
++) {
130 if (tcr
->tcLJ
[i
].bPrint
) {
131 tclj
= &(tcr
->tcLJ
[i
]);
133 xvgropen(mk_gct_nm(opt2fn("-ffout",nfile
,fnm
),
134 efXVG
,tclj
->at_i
,tclj
->at_j
),
135 "General Coupling Lennard Jones","Time (ps)",
136 "Force constant (units)");
137 fprintf(out
[i
],"@ subtitle \"Interaction between types %d and %d\"\n",
138 tclj
->at_i
,tclj
->at_j
);
139 xvgr_legend(out
[i
],asize(leg
),leg
);
146 for(i
=0; (i
<tcr
->nBU
); i
++) {
147 if (tcr
->tcBU
[i
].bPrint
) {
148 tcbu
=&(tcr
->tcBU
[i
]);
150 xvgropen(mk_gct_nm(opt2fn("-ffout",nfile
,fnm
),efXVG
,
151 tcbu
->at_i
,tcbu
->at_j
),
152 "General Coupling Buckingham","Time (ps)",
153 "Force constant (units)");
154 fprintf(out
[i
],"@ subtitle \"Interaction between types %d and %d\"\n",
155 tcbu
->at_i
,tcbu
->at_j
);
156 xvgr_legend(out
[i
],asize(bleg
),bleg
);
162 for(i
=0; (i
<tcr
->nQ
); i
++) {
163 if (tcr
->tcQ
[i
].bPrint
) {
164 qq
[i
] = xvgropen(mk_gct_nm(opt2fn("-ffout",nfile
,fnm
),efXVG
,
165 tcr
->tcQ
[i
].at_i
,-1),
166 "General Coupling Charge","Time (ps)","Charge (e)");
167 fprintf(qq
[i
],"@ subtitle \"Type %d\"\n",tcr
->tcQ
[i
].at_i
);
172 for(i
=0; (i
<tcr
->nIP
); i
++) {
173 sprintf(buf
,"gctIP%d",tcr
->tIP
[i
].type
);
174 ip
[i
]=xvgropen(mk_gct_nm(opt2fn("-ffout",nfile
,fnm
),efXVG
,0,-1),
175 "General Coupling iparams","Time (ps)","ip ()");
176 index
=tcr
->tIP
[i
].type
;
177 fprintf(ip
[i
],"@ subtitle \"Coupling to %s\"\n",
178 interaction_function
[idef
->functype
[index
]].longname
);
182 /* Write properties to file */
183 fprintf(prop
,"%10.3f",time
);
184 for(i
=0; (i
<eoObsNR
); i
++)
185 if (tcr
->bObsUsed
[i
])
186 fprintf(prop
," %10.3e %10.3e",tcr
->act_value
[i
],tcr
->av_value
[i
]);
190 for(i
=0; (i
<tcr
->nLJ
); i
++) {
191 tclj
=&(tcr
->tcLJ
[i
]);
193 fprintf(out
[i
],"%14.7e %14.7e %14.7e\n",
194 time
,tclj
->c12
,tclj
->c6
);
198 for(i
=0; (i
<tcr
->nBU
); i
++) {
199 tcbu
=&(tcr
->tcBU
[i
]);
201 fprintf(out
[i
],"%14.7e %14.7e %14.7e %14.7e\n",
202 time
,tcbu
->a
,tcbu
->b
,tcbu
->c
);
206 for(i
=0; (i
<tcr
->nQ
); i
++) {
207 if (tcr
->tcQ
[i
].bPrint
) {
208 fprintf(qq
[i
],"%14.7e %14.7e\n",time
,tcr
->tcQ
[i
].Q
);
212 for(i
=0; (i
<tcr
->nIP
); i
++) {
213 fprintf(ip
[i
],"%10g ",time
);
214 index
=tcr
->tIP
[i
].type
;
215 switch(idef
->functype
[index
]) {
217 fprintf(ip
[i
],"%10g %10g\n",tcr
->tIP
[i
].iprint
.harmonic
.krA
,
218 tcr
->tIP
[i
].iprint
.harmonic
.rA
);
227 static void pr_dev(t_coupl_rec
*tcr
,
228 real t
,real dev
[eoObsNR
],t_commrec
*cr
,int nfile
,t_filenm fnm
[])
230 static FILE *fp
=NULL
;
235 fp
=xvgropen(opt2fn("-devout",nfile
,fnm
),
236 "Deviations from target value","Time (ps)","");
238 for(i
=j
=0; (i
<eoObsNR
); i
++)
239 if (tcr
->bObsUsed
[i
])
240 ptr
[j
++] = eoNames
[i
];
241 xvgr_legend(fp
,j
,ptr
);
244 fprintf(fp
,"%10.3f",t
);
245 for(i
=0; (i
<eoObsNR
); i
++)
246 if (tcr
->bObsUsed
[i
])
247 fprintf(fp
," %10.3e",dev
[i
]);
252 static void upd_nbfplj(FILE *log
,real
*nbfp
,int atnr
,real f6
[],real f12
[])
256 /* Update the nonbonded force parameters */
257 for(k
=n
=0; (n
<atnr
); n
++) {
258 for(m
=0; (m
<atnr
); m
++,k
++) {
259 C6 (nbfp
,atnr
,n
,m
) *= f6
[k
];
260 C12(nbfp
,atnr
,n
,m
) *= f12
[k
];
265 static void upd_nbfpbu(FILE *log
,real
*nbfp
,int atnr
,
266 real fa
[],real fb
[],real fc
[])
270 /* Update the nonbonded force parameters */
271 for(k
=n
=0; (n
<atnr
); n
++) {
272 for(m
=0; (m
<atnr
); m
++,k
++) {
273 BHAMA(nbfp
,atnr
,n
,m
) *= fa
[k
];
274 BHAMB(nbfp
,atnr
,n
,m
) *= fb
[k
];
275 BHAMC(nbfp
,atnr
,n
,m
) *= fc
[k
];
280 void gprod(t_commrec
*cr
,int n
,real f
[])
282 /* Compute the global product of all elements in an array
283 * such that after gprod f[i] = PROD_j=1,nprocs f[i][j]
286 static real
*buf
[2] = { NULL
, NULL
};
292 srenew(buf
[cur
], nbuf
);
293 srenew(buf
[next
],nbuf
);
299 for(i
=0; (i
<cr
->nnodes
-1); i
++) {
300 gmx_tx(cr
->left
, array(buf
[cur
],n
));
301 gmx_rx(cr
->right
,array(buf
[next
],n
));
302 gmx_wait(cr
->left
,cr
->right
);
303 /* Multiply f by factor read */
305 f
[j
] *= buf
[next
][j
];
312 static void set_factor_matrix(int ntypes
,real f
[],real fmult
,int ati
,int atj
)
318 fmult
= min(FMAX
,max(FMIN
,fmult
));
320 f
[ntypes
*ati
+atj
] *= fmult
;
321 f
[ntypes
*atj
+ati
] *= fmult
;
324 for(i
=0; (i
<ntypes
); i
++) {
325 f
[ntypes
*ati
+i
] *= fmult
;
326 f
[ntypes
*i
+ati
] *= fmult
;
333 static real
calc_deviation(real xav
,real xt
,real x0
)
335 /* This may prevent overshooting in GCT coupling... */
341 dev = min(xav-x0,xt-x0);
347 dev = max(xav-x0,xt-x0);
355 static real
calc_dist(FILE *log
,rvec x
[])
357 static bool bFirst
=TRUE
;
364 if ((buf
= getenv("DISTGCT")) == NULL
)
367 bDist
= (sscanf(buf
,"%d%d",&i1
,&i2
) == 2);
369 fprintf(log
,"GCT: Will couple to distance between %d and %d\n",i1
,i2
);
371 fprintf(log
,"GCT: Will not couple to distances\n");
376 rvec_sub(x
[i1
],x
[i2
],dx
);
383 real
run_aver(real old
,real cur
,int step
,int nmem
)
387 return ((nmem
-1)*old
+cur
)/nmem
;
390 static void set_act_value(t_coupl_rec
*tcr
,int index
,real val
,int step
)
392 tcr
->act_value
[index
] = val
;
393 tcr
->av_value
[index
] = run_aver(tcr
->av_value
[index
],val
,step
,tcr
->nmemory
);
396 static void upd_f_value(FILE *log
,int atnr
,real xi
,real dt
,real factor
,
397 real ff
[],int ati
,int atj
)
402 fff
= 1 + (dt
/xi
) * factor
;
404 set_factor_matrix(atnr
,ff
,sqrt(fff
),ati
,atj
);
408 static void dump_fm(FILE *fp
,int n
,real f
[],char *s
)
412 fprintf(fp
,"Factor matrix (all numbers -1) %s\n",s
);
413 for(i
=0; (i
<n
); i
++) {
415 fprintf(fp
," %10.3e",f
[n
*i
+j
]-1.0);
420 void do_coupling(FILE *log
,int nfile
,t_filenm fnm
[],
421 t_coupl_rec
*tcr
,real t
,int step
,real ener
[],
422 t_forcerec
*fr
,t_inputrec
*ir
,bool bMaster
,
423 t_mdatoms
*md
,t_idef
*idef
,real mu_aver
,int nmols
,
424 t_commrec
*cr
,matrix box
,tensor virial
,
425 tensor pres
,rvec mu_tot
,
426 rvec x
[],rvec f
[],bool bDoIt
)
428 #define enm2Debye 48.0321
429 #define d2e(x) (x)/enm2Debye
430 #define enm2kjmol(x) (x)*0.0143952 /* = 2.0*4.0*M_PI*EPSILON0 */
432 static real
*f6
,*f12
,*fa
,*fb
,*fc
,*fq
;
433 static bool bFirst
= TRUE
;
435 int i
,j
,ati
,atj
,atnr2
,type
,ftype
;
436 real deviation
[eoObsNR
],prdev
[eoObsNR
],epot0
,dist
,rmsf
;
437 real ff6
,ff12
,ffa
,ffb
,ffc
,ffq
,factor
,dt
,mu_ind
;
438 real Epol
,Eintern
,Virial
,muabs
,xiH
=-1,xiS
=-1,xi6
,xi12
;
444 t_coupl_iparams
*tip
;
446 atnr2
= idef
->atnr
* idef
->atnr
;
449 fprintf(log
,"GCT: this is parallel\n");
451 fprintf(log
,"GCT: this is not parallel\n");
458 snew(fq
, idef
->atnr
);
465 for(i
=0; (i
<ir
->opts
.ngtc
); i
++) {
466 nrdf
+= ir
->opts
.nrdf
[i
];
467 TTT
+= ir
->opts
.nrdf
[i
]*ir
->opts
.ref_t
[i
];
471 /* Calculate reference virial from reference temperature and pressure */
472 tcr
->ref_value
[eoVir
] = 0.5*BOLTZ
*nrdf
*TTT
- (3.0/2.0)*
473 Vol
*tcr
->ref_value
[eoPres
];
475 fprintf(log
,"GCT: TTT = %g, nrdf = %d, vir0 = %g, Vol = %g\n",
476 TTT
,nrdf
,tcr
->ref_value
[eoVir
],Vol
);
482 bPrint
= MASTER(cr
) && do_per_step(step
,ir
->nstlog
);
485 /* Initiate coupling to the reference pressure and temperature to start
489 for(i
=0; (i
<eoObsNR
); i
++)
490 tcr
->av_value
[i
] = tcr
->ref_value
[i
];
491 if ((tcr
->ref_value
[eoDipole
]) != 0.0) {
492 mu_ind
= mu_aver
- d2e(tcr
->ref_value
[eoDipole
]); /* in e nm */
493 Epol
= mu_ind
*mu_ind
/(enm2kjmol(tcr
->ref_value
[eoPolarizability
]));
494 tcr
->av_value
[eoEpot
] -= Epol
;
495 fprintf(log
,"GCT: mu_aver = %g(D), mu_ind = %g(D), Epol = %g (kJ/mol)\n",
496 mu_aver
*enm2Debye
,mu_ind
*enm2Debye
,Epol
);
500 /* We want to optimize the LJ params, usually to the Vaporization energy
501 * therefore we only count intermolecular degrees of freedom.
502 * Note that this is now optional. switch UseEinter to yes in your gct file
505 dist
= calc_dist(log
,x
);
506 muabs
= norm(mu_tot
);
507 Eintern
= Ecouple(tcr
,ener
)/nmols
;
508 Virial
= virial
[XX
][XX
]+virial
[YY
][YY
]+virial
[ZZ
][ZZ
];
510 /*calc_force(md->nr,f,fmol);*/
513 /* Use a memory of tcr->nmemory steps, so we actually couple to the
514 * average observable over the last tcr->nmemory steps. This may help
515 * in avoiding local minima in parameter space.
517 set_act_value(tcr
,eoPres
, ener
[F_PRES
],step
);
518 set_act_value(tcr
,eoEpot
, Eintern
, step
);
519 set_act_value(tcr
,eoVir
, Virial
, step
);
520 set_act_value(tcr
,eoDist
, dist
, step
);
521 set_act_value(tcr
,eoMu
, muabs
, step
);
522 set_act_value(tcr
,eoFx
, fmol
[0][XX
], step
);
523 set_act_value(tcr
,eoFy
, fmol
[0][YY
], step
);
524 set_act_value(tcr
,eoFz
, fmol
[0][ZZ
], step
);
525 set_act_value(tcr
,eoPx
, pres
[XX
][XX
],step
);
526 set_act_value(tcr
,eoPy
, pres
[YY
][YY
],step
);
527 set_act_value(tcr
,eoPz
, pres
[ZZ
][ZZ
],step
);
529 epot0
= tcr
->ref_value
[eoEpot
];
530 /* If dipole != 0.0 assume we want to use polarization corrected coupling */
531 if ((tcr
->ref_value
[eoDipole
]) != 0.0) {
532 mu_ind
= mu_aver
- d2e(tcr
->ref_value
[eoDipole
]); /* in e nm */
534 Epol
= mu_ind
*mu_ind
/(enm2kjmol(tcr
->ref_value
[eoPolarizability
]));
538 fprintf(debug
,"mu_ind: %g (%g D) mu_aver: %g (%g D)\n",
539 mu_ind
,mu_ind
*enm2Debye
,mu_aver
,mu_aver
*enm2Debye
);
540 fprintf(debug
,"Eref %g Epol %g Erunav %g Eact %g\n",
541 tcr
->ref_value
[eoEpot
],Epol
,tcr
->av_value
[eoEpot
],
542 tcr
->act_value
[eoEpot
]);
547 pr_ff(tcr
,t
,idef
,cr
,nfile
,fnm
);
549 /* Calculate the deviation of average value from the target value */
550 for(i
=0; (i
<eoObsNR
); i
++) {
551 deviation
[i
] = calc_deviation(tcr
->av_value
[i
],tcr
->act_value
[i
],
553 prdev
[i
] = tcr
->ref_value
[i
] - tcr
->act_value
[i
];
555 deviation
[eoEpot
] = calc_deviation(tcr
->av_value
[eoEpot
],tcr
->act_value
[eoEpot
],
557 prdev
[eoEpot
] = epot0
- tcr
->act_value
[eoEpot
];
560 pr_dev(tcr
,t
,prdev
,cr
,nfile
,fnm
);
562 /* First set all factors to 1 */
563 for(i
=0; (i
<atnr2
); i
++) {
564 f6
[i
] = f12
[i
] = fa
[i
] = fb
[i
] = fc
[i
] = 1.0;
566 for(i
=0; (i
<idef
->atnr
); i
++)
569 /* Now compute the actual coupling compononents */
572 for(i
=0; (i
<tcr
->nLJ
); i
++) {
573 tclj
=&(tcr
->tcLJ
[i
]);
580 if (tclj
->eObs
== eoForce
) {
581 fatal_error(0,"Hack code for this to work again ");
583 fprintf(debug
,"Have computed derivatives: xiH = %g, xiS = %g\n",xiH
,xiS
);
593 fatal_error(0,"No H, no Shell, edit code at %s, line %d\n",
596 set_factor_matrix(idef
->atnr
,f6
, sqrt(ff6
), ati
,atj
);
598 set_factor_matrix(idef
->atnr
,f12
,sqrt(ff12
),ati
,atj
);
602 fprintf(debug
,"tcr->tcLJ[%d].xi_6 = %g, xi_12 = %g deviation = %g\n",i
,
603 tclj
->xi_6
,tclj
->xi_12
,deviation
[tclj
->eObs
]);
604 factor
=deviation
[tclj
->eObs
];
606 upd_f_value(log
,idef
->atnr
,tclj
->xi_6
, dt
,factor
,f6
, ati
,atj
);
607 upd_f_value(log
,idef
->atnr
,tclj
->xi_12
,dt
,factor
,f12
,ati
,atj
);
615 dump_fm(log
,idef
->atnr
,f6
,"f6");
616 dump_fm(log
,idef
->atnr
,f12
,"f12");
619 upd_nbfplj(log
,fr
->nbfp
,idef
->atnr
,f6
,f12
);
621 /* Copy for printing */
622 for(i
=0; (i
<tcr
->nLJ
); i
++) {
623 tclj
=&(tcr
->tcLJ
[i
]);
628 tclj
->c6
= C6(fr
->nbfp
,fr
->ntype
,ati
,atj
);
629 tclj
->c12
= C12(fr
->nbfp
,fr
->ntype
,ati
,atj
);
634 for(i
=0; (i
<tcr
->nBU
); i
++) {
635 tcbu
= &(tcr
->tcBU
[i
]);
636 factor
= deviation
[tcbu
->eObs
];
640 upd_f_value(log
,idef
->atnr
,tcbu
->xi_a
,dt
,factor
,fa
,ati
,atj
);
641 upd_f_value(log
,idef
->atnr
,tcbu
->xi_b
,dt
,factor
,fb
,ati
,atj
);
642 upd_f_value(log
,idef
->atnr
,tcbu
->xi_c
,dt
,factor
,fc
,ati
,atj
);
650 upd_nbfpbu(log
,fr
->nbfp
,idef
->atnr
,fa
,fb
,fc
);
651 /* Copy for printing */
652 for(i
=0; (i
<tcr
->nBU
); i
++) {
653 tcbu
=&(tcr
->tcBU
[i
]);
658 tcbu
->a
= BHAMA(fr
->nbfp
,fr
->ntype
,ati
,atj
);
659 tcbu
->b
= BHAMB(fr
->nbfp
,fr
->ntype
,ati
,atj
);
660 tcbu
->c
= BHAMC(fr
->nbfp
,fr
->ntype
,ati
,atj
);
662 fprintf(debug
,"buck (type=%d) = %e, %e, %e\n",
663 tcbu
->at_i
,tcbu
->a
,tcbu
->b
,tcbu
->c
);
667 for(i
=0; (i
<tcr
->nQ
); i
++) {
670 ffq
= 1.0 + (dt
/tcq
->xi_Q
) * deviation
[tcq
->eObs
];
673 fq
[tcq
->at_i
] *= ffq
;
677 gprod(cr
,idef
->atnr
,fq
);
679 for(j
=0; (j
<md
->nr
); j
++) {
680 md
->chargeA
[j
] *= fq
[md
->typeA
[j
]];
682 for(i
=0; (i
<tcr
->nQ
); i
++) {
684 for(j
=0; (j
<md
->nr
); j
++) {
685 if (md
->typeA
[j
] == tcq
->at_i
) {
686 tcq
->Q
= md
->chargeA
[j
];
691 fatal_error(0,"Coupling type %d not found",tcq
->at_i
);
693 for(i
=0; (i
<tcr
->nIP
); i
++) {
694 tip
= &(tcr
->tIP
[i
]);
696 ftype
= idef
->functype
[type
];
697 factor
= dt
*deviation
[tip
->eObs
];
701 if (tip
->xi
.harmonic
.krA
) idef
->iparams
[type
].harmonic
.krA
*= (1+factor
/tip
->xi
.harmonic
.krA
);
702 if (tip
->xi
.harmonic
.rA
) idef
->iparams
[type
].harmonic
.rA
*= (1+factor
/tip
->xi
.harmonic
.rA
);
707 tip
->iprint
=idef
->iparams
[type
];