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)
68 read_gct (opt2fn("-j", nfile, fnm), tcr);
69 write_gct(opt2fn("-jo", nfile, fnm), tcr, idef);
71 /* Update all processors with coupling info */
74 comm_tcr(log, cr, &tcr);
77 /* Copy information from the coupling to the force field stuff */
78 copy_ff(tcr, fr, md, idef);
83 static real Ecouple(t_coupl_rec *tcr, real ener[])
87 return ener[F_COUL_SR]+ener[F_LJ]+ener[F_COUL_LR]+ener[F_LJ_LR]+
88 ener[F_RF_EXCL]+ener[F_COUL_RECIP];
96 static char *mk_gct_nm(const char *fn, int ftp, int ati, int atj)
103 sprintf(buf+strlen(fn)-4, "%d.%s", ati, ftp2ext(ftp));
107 sprintf(buf+strlen(fn)-4, "%d_%d.%s", ati, atj, ftp2ext(ftp));
113 static void pr_ff(t_coupl_rec *tcr, real time, t_idef *idef,
114 t_commrec *cr, int nfile, const t_filenm fnm[],
115 const output_env_t oenv)
118 static FILE **out = NULL;
119 static FILE **qq = NULL;
120 static FILE **ip = NULL;
124 const char *leg[] = { "C12", "C6" };
125 const char *eleg[] = { "Epsilon", "Sigma" };
126 const char *bleg[] = { "A", "B", "C" };
130 if ((prop == NULL) && (out == NULL) && (qq == NULL) && (ip == NULL))
132 prop = xvgropen(opt2fn("-runav", nfile, fnm),
133 "Properties and Running Averages", "Time (ps)", "", oenv);
134 snew(raleg, 2*eoObsNR);
135 for (i = j = 0; (i < eoObsNR); i++)
137 if (tcr->bObsUsed[i])
139 raleg[j++] = strdup(eoNames[i]);
140 sprintf(buf, "RA-%s", eoNames[i]);
141 raleg[j++] = strdup(buf);
144 xvgr_legend(prop, j, (const char**)raleg, oenv);
145 for (i = 0; (i < j); i++)
154 for (i = 0; (i < tcr->nLJ); i++)
156 if (tcr->tcLJ[i].bPrint)
158 tclj = &(tcr->tcLJ[i]);
160 xvgropen(mk_gct_nm(opt2fn("-ffout", nfile, fnm),
161 efXVG, tclj->at_i, tclj->at_j),
162 "General Coupling Lennard Jones", "Time (ps)",
163 "Force constant (units)", oenv);
164 fprintf(out[i], "@ subtitle \"Interaction between types %d and %d\"\n",
165 tclj->at_i, tclj->at_j);
166 if (tcr->combrule == 1)
168 xvgr_legend(out[i], asize(leg), leg, oenv);
172 xvgr_legend(out[i], asize(eleg), eleg, oenv);
181 for (i = 0; (i < tcr->nBU); i++)
183 if (tcr->tcBU[i].bPrint)
185 tcbu = &(tcr->tcBU[i]);
187 xvgropen(mk_gct_nm(opt2fn("-ffout", nfile, fnm), efXVG,
188 tcbu->at_i, tcbu->at_j),
189 "General Coupling Buckingham", "Time (ps)",
190 "Force constant (units)", oenv);
191 fprintf(out[i], "@ subtitle \"Interaction between types %d and %d\"\n",
192 tcbu->at_i, tcbu->at_j);
193 xvgr_legend(out[i], asize(bleg), bleg, oenv);
199 for (i = 0; (i < tcr->nQ); i++)
201 if (tcr->tcQ[i].bPrint)
203 qq[i] = xvgropen(mk_gct_nm(opt2fn("-ffout", nfile, fnm), efXVG,
204 tcr->tcQ[i].at_i, -1),
205 "General Coupling Charge", "Time (ps)", "Charge (e)",
207 fprintf(qq[i], "@ subtitle \"Type %d\"\n", tcr->tcQ[i].at_i);
212 for (i = 0; (i < tcr->nIP); i++)
214 sprintf(buf, "gctIP%d", tcr->tIP[i].type);
215 ip[i] = xvgropen(mk_gct_nm(opt2fn("-ffout", nfile, fnm), efXVG, 0, -1),
216 "General Coupling iparams", "Time (ps)", "ip ()", oenv);
217 index = tcr->tIP[i].type;
218 fprintf(ip[i], "@ subtitle \"Coupling to %s\"\n",
219 interaction_function[idef->functype[index]].longname);
223 /* Write properties to file */
224 fprintf(prop, "%10.3f", time);
225 for (i = 0; (i < eoObsNR); i++)
227 if (tcr->bObsUsed[i])
229 fprintf(prop, " %10.3e %10.3e", tcr->act_value[i], tcr->av_value[i]);
235 for (i = 0; (i < tcr->nLJ); i++)
237 tclj = &(tcr->tcLJ[i]);
240 if (tcr->combrule == 1)
242 fprintf(out[i], "%14.7e %14.7e %14.7e\n",
243 time, tclj->c12, tclj->c6);
247 double sigma = pow(tclj->c12/tclj->c6, 1.0/6.0);
248 double epsilon = 0.25*sqr(tclj->c6)/tclj->c12;
249 fprintf(out[i], "%14.7e %14.7e %14.7e\n",
250 time, epsilon, sigma);
255 for (i = 0; (i < tcr->nBU); i++)
257 tcbu = &(tcr->tcBU[i]);
260 fprintf(out[i], "%14.7e %14.7e %14.7e %14.7e\n",
261 time, tcbu->a, tcbu->b, tcbu->c);
265 for (i = 0; (i < tcr->nQ); i++)
267 if (tcr->tcQ[i].bPrint)
269 fprintf(qq[i], "%14.7e %14.7e\n", time, tcr->tcQ[i].Q);
273 for (i = 0; (i < tcr->nIP); i++)
275 fprintf(ip[i], "%10g ", time);
276 index = tcr->tIP[i].type;
277 switch (idef->functype[index])
280 fprintf(ip[i], "%10g %10g\n", tcr->tIP[i].iprint.harmonic.krA,
281 tcr->tIP[i].iprint.harmonic.rA);
290 static void pr_dev(t_coupl_rec *tcr,
291 real t, real dev[eoObsNR], t_commrec *cr, int nfile,
292 const t_filenm fnm[], const output_env_t oenv)
294 static FILE *fp = NULL;
300 fp = xvgropen(opt2fn("-devout", nfile, fnm),
301 "Deviations from target value", "Time (ps)", "", oenv);
303 for (i = j = 0; (i < eoObsNR); i++)
305 if (tcr->bObsUsed[i])
307 ptr[j++] = strdup(eoNames[i]);
310 xvgr_legend(fp, j, (const char**)ptr, oenv);
311 for (i = 0; i < j; i++)
317 fprintf(fp, "%10.3f", t);
318 for (i = 0; (i < eoObsNR); i++)
320 if (tcr->bObsUsed[i])
322 fprintf(fp, " %10.3e", dev[i]);
329 static void upd_nbfplj(FILE *log, real *nbfp, int atnr, real f6[], real f12[],
332 double *sigma, *epsilon, c6, c12, eps, sig, sig6;
335 /* Update the nonbonded force parameters */
339 for (k = n = 0; (n < atnr); n++)
341 for (m = 0; (m < atnr); m++, k++)
343 C6 (nbfp, atnr, n, m) *= f6[k];
344 C12(nbfp, atnr, n, m) *= f12[k];
350 /* Convert to sigma and epsilon */
353 for (n = 0; (n < atnr); n++)
356 c6 = C6 (nbfp, atnr, n, n) * f6[k];
357 c12 = C12(nbfp, atnr, n, n) * f12[k];
358 if ((c6 == 0) || (c12 == 0))
360 gmx_fatal(FARGS, "You can not use combination rule %d with zero C6 (%f) or C12 (%f)", combrule, c6, c12);
362 sigma[n] = pow(c12/c6, 1.0/6.0);
363 epsilon[n] = 0.25*(c6*c6/c12);
365 for (k = n = 0; (n < atnr); n++)
367 for (m = 0; (m < atnr); m++, k++)
369 eps = sqrt(epsilon[n]*epsilon[m]);
372 sig = 0.5*(sigma[n]+sigma[m]);
376 sig = sqrt(sigma[n]*sigma[m]);
378 sig6 = pow(sig, 6.0);
379 /* nbfp now includes the 6.0/12.0 derivative prefactors */
380 C6 (nbfp, atnr, n, m) = 4*eps*sig6/6.0;
381 C12(nbfp, atnr, n, m) = 4*eps*sig6*sig6/12.0;
388 gmx_fatal(FARGS, "Combination rule should be 1,2 or 3 instead of %d",
393 static void upd_nbfpbu(FILE *log, real *nbfp, int atnr,
394 real fa[], real fb[], real fc[])
398 /* Update the nonbonded force parameters */
399 for (k = n = 0; (n < atnr); n++)
401 for (m = 0; (m < atnr); m++, k++)
403 BHAMA(nbfp, atnr, n, m) *= fa[k];
404 BHAMB(nbfp, atnr, n, m) *= fb[k];
405 BHAMC(nbfp, atnr, n, m) *= fc[k];
410 void gprod(t_commrec *cr, int n, real f[])
412 /* Compute the global product of all elements in an array
413 * such that after gprod f[i] = PROD_j=1,nprocs f[i][j]
416 static real *buf = NULL;
427 MPI_Allreduce(f, buf, n, MPI_DOUBLE, MPI_PROD, cr->mpi_comm_mygroup);
429 MPI_Allreduce(f, buf, n, MPI_FLOAT, MPI_PROD, cr->mpi_comm_mygroup);
431 for (i = 0; (i < n); i++)
438 static void set_factor_matrix(int ntypes, real f[], real fmult, int ati, int atj)
444 fmult = min(FMAX, max(FMIN, fmult));
447 f[ntypes*ati+atj] *= fmult;
448 f[ntypes*atj+ati] *= fmult;
452 for (i = 0; (i < ntypes); i++)
454 f[ntypes*ati+i] *= fmult;
455 f[ntypes*i+ati] *= fmult;
462 static real calc_deviation(real xav, real xt, real x0)
464 /* This may prevent overshooting in GCT coupling... */
470 dev = min(xav-x0,xt-x0);
476 dev = max(xav-x0,xt-x0);
484 static real calc_dist(FILE *log, rvec x[])
486 static gmx_bool bFirst = TRUE;
487 static gmx_bool bDist;
494 if ((buf = getenv("DISTGCT")) == NULL)
500 bDist = (sscanf(buf, "%d%d", &i1, &i2) == 2);
503 fprintf(log, "GCT: Will couple to distance between %d and %d\n", i1, i2);
507 fprintf(log, "GCT: Will not couple to distances\n");
514 rvec_sub(x[i1], x[i2], dx);
523 real run_aver(real old, real cur, int step, int nmem)
527 return ((nmem-1)*old+cur)/nmem;
530 static void set_act_value(t_coupl_rec *tcr, int index, real val, int step)
532 tcr->act_value[index] = val;
533 tcr->av_value[index] = run_aver(tcr->av_value[index], val, step, tcr->nmemory);
536 static void upd_f_value(FILE *log, int atnr, real xi, real dt, real factor,
537 real ff[], int ati, int atj)
543 fff = 1 + (dt/xi) * factor;
546 set_factor_matrix(atnr, ff, sqrt(fff), ati, atj);
551 static void dump_fm(FILE *fp, int n, real f[], char *s)
555 fprintf(fp, "Factor matrix (all numbers -1) %s\n", s);
556 for (i = 0; (i < n); i++)
558 for (j = 0; (j < n); j++)
560 fprintf(fp, " %10.3e", f[n*i+j]-1.0);
566 void do_coupling(FILE *log, const output_env_t oenv, int nfile,
567 const t_filenm fnm[], t_coupl_rec *tcr, real t,
568 int step, real ener[], t_forcerec *fr, t_inputrec *ir,
569 gmx_bool bMaster, t_mdatoms *md, t_idef *idef, real mu_aver, int nmols,
570 t_commrec *cr, matrix box, tensor virial,
571 tensor pres, rvec mu_tot,
572 rvec x[], rvec f[], gmx_bool bDoIt)
574 #define enm2Debye 48.0321
575 #define d2e(x) (x)/enm2Debye
576 #define enm2kjmol(x) (x)*0.0143952 /* = 2.0*4.0*M_PI*EPSILON0 */
578 static real *f6, *f12, *fa, *fb, *fc, *fq;
579 static gmx_bool bFirst = TRUE;
581 int i, j, ati, atj, atnr2, type, ftype;
582 real deviation[eoObsNR], prdev[eoObsNR], epot0, dist, rmsf;
583 real ff6, ff12, ffa, ffb, ffc, ffq, factor, dt, mu_ind;
584 real Epol, Eintern, Virial, muabs, xiH = -1, xiS = -1, xi6, xi12;
586 gmx_bool bTest, bPrint;
590 t_coupl_iparams *tip;
592 atnr2 = idef->atnr * idef->atnr;
597 fprintf(log, "GCT: this is parallel\n");
601 fprintf(log, "GCT: this is not parallel\n");
609 snew(fq, idef->atnr);
617 for (i = 0; (i < ir->opts.ngtc); i++)
619 nrdf += ir->opts.nrdf[i];
620 TTT += ir->opts.nrdf[i]*ir->opts.ref_t[i];
624 /* Calculate reference virial from reference temperature and pressure */
625 tcr->ref_value[eoVir] = 0.5*BOLTZ*nrdf*TTT - (3.0/2.0)*
626 Vol*tcr->ref_value[eoPres];
628 fprintf(log, "GCT: TTT = %g, nrdf = %d, vir0 = %g, Vol = %g\n",
629 TTT, nrdf, tcr->ref_value[eoVir], Vol);
635 bPrint = MASTER(cr) && do_per_step(step, ir->nstlog);
638 /* Initiate coupling to the reference pressure and temperature to start
643 for (i = 0; (i < eoObsNR); i++)
645 tcr->av_value[i] = tcr->ref_value[i];
647 if ((tcr->ref_value[eoDipole]) != 0.0)
649 mu_ind = mu_aver - d2e(tcr->ref_value[eoDipole]); /* in e nm */
650 Epol = mu_ind*mu_ind/(enm2kjmol(tcr->ref_value[eoPolarizability]));
651 tcr->av_value[eoEpot] -= Epol;
652 fprintf(log, "GCT: mu_aver = %g(D), mu_ind = %g(D), Epol = %g (kJ/mol)\n",
653 mu_aver*enm2Debye, mu_ind*enm2Debye, Epol);
657 /* We want to optimize the LJ params, usually to the Vaporization energy
658 * therefore we only count intermolecular degrees of freedom.
659 * Note that this is now optional. switch UseEinter to yes in your gct file
662 dist = calc_dist(log, x);
663 muabs = norm(mu_tot);
664 Eintern = Ecouple(tcr, ener)/nmols;
665 Virial = virial[XX][XX]+virial[YY][YY]+virial[ZZ][ZZ];
667 /*calc_force(md->nr,f,fmol);*/
670 /* Use a memory of tcr->nmemory steps, so we actually couple to the
671 * average observable over the last tcr->nmemory steps. This may help
672 * in avoiding local minima in parameter space.
674 set_act_value(tcr, eoPres, ener[F_PRES], step);
675 set_act_value(tcr, eoEpot, Eintern, step);
676 set_act_value(tcr, eoVir, Virial, step);
677 set_act_value(tcr, eoDist, dist, step);
678 set_act_value(tcr, eoMu, muabs, step);
679 set_act_value(tcr, eoFx, fmol[0][XX], step);
680 set_act_value(tcr, eoFy, fmol[0][YY], step);
681 set_act_value(tcr, eoFz, fmol[0][ZZ], step);
682 set_act_value(tcr, eoPx, pres[XX][XX], step);
683 set_act_value(tcr, eoPy, pres[YY][YY], step);
684 set_act_value(tcr, eoPz, pres[ZZ][ZZ], step);
686 epot0 = tcr->ref_value[eoEpot];
687 /* If dipole != 0.0 assume we want to use polarization corrected coupling */
688 if ((tcr->ref_value[eoDipole]) != 0.0)
690 mu_ind = mu_aver - d2e(tcr->ref_value[eoDipole]); /* in e nm */
692 Epol = mu_ind*mu_ind/(enm2kjmol(tcr->ref_value[eoPolarizability]));
697 fprintf(debug, "mu_ind: %g (%g D) mu_aver: %g (%g D)\n",
698 mu_ind, mu_ind*enm2Debye, mu_aver, mu_aver*enm2Debye);
699 fprintf(debug, "Eref %g Epol %g Erunav %g Eact %g\n",
700 tcr->ref_value[eoEpot], Epol, tcr->av_value[eoEpot],
701 tcr->act_value[eoEpot]);
707 pr_ff(tcr, t, idef, cr, nfile, fnm, oenv);
709 /* Calculate the deviation of average value from the target value */
710 for (i = 0; (i < eoObsNR); i++)
712 deviation[i] = calc_deviation(tcr->av_value[i], tcr->act_value[i],
714 prdev[i] = tcr->ref_value[i] - tcr->act_value[i];
716 deviation[eoEpot] = calc_deviation(tcr->av_value[eoEpot], tcr->act_value[eoEpot],
718 prdev[eoEpot] = epot0 - tcr->act_value[eoEpot];
722 pr_dev(tcr, t, prdev, cr, nfile, fnm, oenv);
725 /* First set all factors to 1 */
726 for (i = 0; (i < atnr2); i++)
728 f6[i] = f12[i] = fa[i] = fb[i] = fc[i] = 1.0;
730 for (i = 0; (i < idef->atnr); i++)
735 /* Now compute the actual coupling compononents */
740 for (i = 0; (i < tcr->nLJ); i++)
742 tclj = &(tcr->tcLJ[i]);
749 if (tclj->eObs == eoForce)
751 gmx_fatal(FARGS, "Hack code for this to work again ");
754 fprintf(debug, "Have computed derivatives: xiH = %g, xiS = %g\n", xiH, xiS);
768 gmx_fatal(FARGS, "No H, no Shell, edit code at %s, line %d\n",
773 set_factor_matrix(idef->atnr, f6, sqrt(ff6), ati, atj);
777 set_factor_matrix(idef->atnr, f12, sqrt(ff12), ati, atj);
784 fprintf(debug, "tcr->tcLJ[%d].xi_6 = %g, xi_12 = %g deviation = %g\n", i,
785 tclj->xi_6, tclj->xi_12, deviation[tclj->eObs]);
787 factor = deviation[tclj->eObs];
789 upd_f_value(log, idef->atnr, tclj->xi_6, dt, factor, f6, ati, atj);
790 upd_f_value(log, idef->atnr, tclj->xi_12, dt, factor, f12, ati, atj);
796 gprod(cr, atnr2, f6);
797 gprod(cr, atnr2, f12);
799 dump_fm(log, idef->atnr, f6, "f6");
800 dump_fm(log, idef->atnr, f12, "f12");
803 upd_nbfplj(log, fr->nbfp, idef->atnr, f6, f12, tcr->combrule);
805 /* Copy for printing */
806 for (i = 0; (i < tcr->nLJ); i++)
808 tclj = &(tcr->tcLJ[i]);
815 /* nbfp now includes the 6.0/12.0 derivative prefactors */
816 tclj->c6 = C6(fr->nbfp, fr->ntype, ati, atj)/6.0;
817 tclj->c12 = C12(fr->nbfp, fr->ntype, ati, atj)/12.0;
824 for (i = 0; (i < tcr->nBU); i++)
826 tcbu = &(tcr->tcBU[i]);
827 factor = deviation[tcbu->eObs];
831 upd_f_value(log, idef->atnr, tcbu->xi_a, dt, factor, fa, ati, atj);
832 upd_f_value(log, idef->atnr, tcbu->xi_b, dt, factor, fb, ati, atj);
833 upd_f_value(log, idef->atnr, tcbu->xi_c, dt, factor, fc, ati, atj);
838 gprod(cr, atnr2, fa);
839 gprod(cr, atnr2, fb);
840 gprod(cr, atnr2, fc);
842 upd_nbfpbu(log, fr->nbfp, idef->atnr, fa, fb, fc);
843 /* Copy for printing */
844 for (i = 0; (i < tcr->nBU); i++)
846 tcbu = &(tcr->tcBU[i]);
853 /* nbfp now includes the 6.0 derivative prefactors */
854 tcbu->a = BHAMA(fr->nbfp, fr->ntype, ati, atj);
855 tcbu->b = BHAMB(fr->nbfp, fr->ntype, ati, atj);
856 tcbu->c = BHAMC(fr->nbfp, fr->ntype, ati, atj)/6.0;
859 fprintf(debug, "buck (type=%d) = %e, %e, %e\n",
860 tcbu->at_i, tcbu->a, tcbu->b, tcbu->c);
866 for (i = 0; (i < tcr->nQ); i++)
868 tcq = &(tcr->tcQ[i]);
871 ffq = 1.0 + (dt/tcq->xi_Q) * deviation[tcq->eObs];
877 fq[tcq->at_i] *= ffq;
882 gprod(cr, idef->atnr, fq);
885 for (j = 0; (j < md->nr); j++)
887 md->chargeA[j] *= fq[md->typeA[j]];
889 for (i = 0; (i < tcr->nQ); i++)
891 tcq = &(tcr->tcQ[i]);
892 for (j = 0; (j < md->nr); j++)
894 if (md->typeA[j] == tcq->at_i)
896 tcq->Q = md->chargeA[j];
902 gmx_fatal(FARGS, "Coupling type %d not found", tcq->at_i);
905 for (i = 0; (i < tcr->nIP); i++)
907 tip = &(tcr->tIP[i]);
909 ftype = idef->functype[type];
910 factor = dt*deviation[tip->eObs];
915 if (tip->xi.harmonic.krA)
917 idef->iparams[type].harmonic.krA *= (1+factor/tip->xi.harmonic.krA);
919 if (tip->xi.harmonic.rA)
921 idef->iparams[type].harmonic.rA *= (1+factor/tip->xi.harmonic.rA);
927 tip->iprint = idef->iparams[type];