Redefine the default boolean type to gmx_bool.
[alexxy/gromacs.git] / src / kernel / do_gct.c
1 /*
2  * 
3  *                This source code is part of
4  * 
5  *                 G   R   O   M   A   C   S
6  * 
7  *          GROningen MAchine for Chemical Simulations
8  * 
9  *                        VERSION 3.2.0
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.
14
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.
19  * 
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.
26  * 
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.
29  * 
30  * For more info, check our website at http://www.gromacs.org
31  * 
32  * And Hey:
33  * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "typedefs.h"
40 #include "xmdrun.h"
41 #include "futil.h"
42 #include "xvgr.h"
43 #include "macros.h"
44 #include "physics.h"
45 #include "network.h"
46 #include "smalloc.h"
47 #include "mdrun.h"
48 #include "string2.h"
49 #include "readinp.h"
50 #include "filenm.h"
51 #include "update.h"
52 #include "vec.h"
53 #include "main.h"
54 #include "txtdump.h"
55
56 /*#define DEBUGGCT*/
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)
60 {
61   int         i,nc,index,j;
62   int         ati,atj;
63   t_coupl_rec *tcr;
64   
65   snew(tcr,1);
66   if (MASTER(cr)) {
67     read_gct (opt2fn("-j",nfile,fnm), tcr);
68     write_gct(opt2fn("-jo",nfile,fnm),tcr,idef);
69   }
70   /* Update all processors with coupling info */
71   if (PAR(cr))
72     comm_tcr(log,cr,&tcr);
73
74   /* Copy information from the coupling to the force field stuff */    
75   copy_ff(tcr,fr,md,idef);
76   
77   return tcr;
78 }
79
80 static real Ecouple(t_coupl_rec *tcr,real ener[])
81 {
82   if (tcr->bInter)
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];
85   else
86     return ener[F_EPOT];
87 }
88
89 static char *mk_gct_nm(const char *fn,int ftp,int ati,int atj)
90 {
91   static char buf[256];
92   
93   strcpy(buf,fn);
94   if (atj == -1)
95     sprintf(buf+strlen(fn)-4,"%d.%s",ati,ftp2ext(ftp));
96   else
97     sprintf(buf+strlen(fn)-4,"%d_%d.%s",ati,atj,ftp2ext(ftp));
98   
99   return buf;
100 }
101
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)
105 {
106   static FILE *prop;
107   static FILE **out=NULL;
108   static FILE **qq=NULL;
109   static FILE **ip=NULL;
110   t_coupl_LJ  *tclj;
111   t_coupl_BU  *tcbu;
112   char        buf[256];
113   const char *leg[]  =  { "C12", "C6" };
114   const char *eleg[] =  { "Epsilon", "Sigma" };
115   const char *bleg[] = { "A", "B", "C" };
116   char        **raleg;
117   int         i,j,index;
118   
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);
128       }
129     }
130     xvgr_legend(prop,j,(const char**)raleg,oenv);
131     for(i=0; (i<j); i++) 
132       sfree(raleg[i]);
133     sfree(raleg);
134       
135     if (tcr->nLJ) {
136       snew(out,tcr->nLJ);
137       for(i=0; (i<tcr->nLJ); i++) {
138         if (tcr->tcLJ[i].bPrint) {
139           tclj   = &(tcr->tcLJ[i]);
140           out[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);
149           else
150             xvgr_legend(out[i],asize(eleg),eleg,oenv);
151           fflush(out[i]);
152         }
153       }
154     }
155     else if (tcr->nBU) {
156       snew(out,tcr->nBU);
157       for(i=0; (i<tcr->nBU); i++) {
158         if (tcr->tcBU[i].bPrint) {
159           tcbu=&(tcr->tcBU[i]);
160           out[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);
168           fflush(out[i]);
169         }
170       }
171     }
172     snew(qq,tcr->nQ);
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)",
178                          oenv);
179         fprintf(qq[i],"@ subtitle \"Type %d\"\n",tcr->tcQ[i].at_i);
180         fflush(qq[i]);
181       }
182     }
183     snew(ip,tcr->nIP);
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);
191       fflush(ip[i]);
192     }
193   }
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]);
199   fprintf(prop,"\n");
200   fflush(prop);
201   
202   for(i=0; (i<tcr->nLJ); i++) {
203     tclj=&(tcr->tcLJ[i]);
204     if (tclj->bPrint) {
205       if (tcr->combrule == 1)
206         fprintf(out[i],"%14.7e  %14.7e  %14.7e\n",
207                 time,tclj->c12,tclj->c6);
208       else {
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",
212                 time,epsilon,sigma);
213       }
214       fflush(out[i]);
215     }
216   }
217   for(i=0; (i<tcr->nBU); i++) {
218     tcbu=&(tcr->tcBU[i]);
219     if (tcbu->bPrint) {
220       fprintf(out[i],"%14.7e  %14.7e  %14.7e  %14.7e\n",
221               time,tcbu->a,tcbu->b,tcbu->c);
222       fflush(out[i]);
223     }
224   }
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);
228       fflush(qq[i]);
229     }
230   }
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]) {
235     case F_BONDS:
236       fprintf(ip[i],"%10g  %10g\n",tcr->tIP[i].iprint.harmonic.krA,
237               tcr->tIP[i].iprint.harmonic.rA);
238       break;
239     default:
240       break;
241     }
242     fflush(ip[i]);
243   }
244 }
245
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)
249 {
250   static FILE *fp=NULL;
251   char   **ptr;
252   int    i,j;
253   
254   if (!fp) {
255     fp=xvgropen(opt2fn("-devout",nfile,fnm),
256                 "Deviations from target value","Time (ps)","",oenv);
257     snew(ptr,eoObsNR);
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);
262           for(i=0;i<j;i++)
263                   sfree(ptr[i]);
264     sfree(ptr);
265   }
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]);
270   fprintf(fp,"\n");
271   fflush(fp);
272 }
273
274 static void upd_nbfplj(FILE *log,real *nbfp,int atnr,real f6[],real f12[],
275                        int combrule)
276 {
277   double *sigma,*epsilon,c6,c12,eps,sig,sig6;
278   int n,m,k;
279   
280   /* Update the nonbonded force parameters */
281   switch (combrule) {
282   case 1:
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];
287       }
288     }
289     break;
290   case 2:
291   case 3:
292     /* Convert to sigma and epsilon */
293     snew(sigma,atnr);
294     snew(epsilon,atnr);
295     for(n=0; (n<atnr); n++) {
296       k = n*(atnr+1);
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);
303     }
304     for(k=n=0; (n<atnr); n++) {
305       for(m=0; (m<atnr); m++,k++) {
306         eps  = sqrt(epsilon[n]*epsilon[m]);
307         if (combrule == 2)
308           sig  = 0.5*(sigma[n]+sigma[m]);
309         else
310           sig  = sqrt(sigma[n]*sigma[m]);
311         sig6 = pow(sig,6.0);
312         C6 (nbfp,atnr,n,m) = 4*eps*sig6; 
313         C12(nbfp,atnr,n,m) = 4*eps*sig6*sig6;
314       }
315     }
316     sfree(sigma);
317     sfree(epsilon);
318     break;
319   default:
320     gmx_fatal(FARGS,"Combination rule should be 1,2 or 3 instead of %d",
321               combrule);
322   }
323 }
324
325 static void upd_nbfpbu(FILE *log,real *nbfp,int atnr,
326                        real fa[],real fb[],real fc[])
327 {
328   int n,m,k;
329   
330   /* Update the nonbonded force parameters */
331   for(k=n=0; (n<atnr); n++) {
332     for(m=0; (m<atnr); m++,k++) {
333       BHAMA(nbfp,atnr,n,m) *= fa[k];
334       BHAMB(nbfp,atnr,n,m) *= fb[k];
335       BHAMC(nbfp,atnr,n,m) *= fc[k];
336     }
337   }
338 }
339
340 void gprod(t_commrec *cr,int n,real f[])
341 {
342   /* Compute the global product of all elements in an array 
343    * such that after gprod f[i] = PROD_j=1,nprocs f[i][j]
344    */
345   static int  nbuf=0;
346   static real *buf=NULL;
347   int  i;
348
349   if (n > nbuf) {
350     nbuf = n;
351     srenew(buf, nbuf);
352   }
353
354 #ifdef GMX_MPI
355 #ifdef GMX_DOUBLE
356   MPI_Allreduce(f,buf,n,MPI_DOUBLE,MPI_PROD,cr->mpi_comm_mygroup);
357 #else
358   MPI_Allreduce(f,buf,n,MPI_FLOAT, MPI_PROD,cr->mpi_comm_mygroup);
359 #endif
360   for(i=0; (i<n); i++) 
361     f[i] = buf[i];
362 #endif
363 }
364
365 static void set_factor_matrix(int ntypes,real f[],real fmult,int ati,int atj)
366 {
367 #define FMIN 0.95
368 #define FMAX 1.05
369   int i;
370
371   fmult = min(FMAX,max(FMIN,fmult));  
372   if (atj != -1) {
373     f[ntypes*ati+atj] *= fmult;
374     f[ntypes*atj+ati] *= fmult;
375   }
376   else {
377     for(i=0; (i<ntypes); i++) {
378       f[ntypes*ati+i] *= fmult;
379       f[ntypes*i+ati] *= fmult;
380     }
381   }
382 #undef FMIN
383 #undef FMAX
384 }
385
386 static real calc_deviation(real xav,real xt,real x0)
387 {
388   /* This may prevent overshooting in GCT coupling... */
389
390   /* real dev;
391   
392   if (xav > x0) {
393     if (xt > x0)
394       dev = min(xav-x0,xt-x0);
395     else
396       dev = 0;
397   }
398   else {
399     if (xt < x0)
400       dev = max(xav-x0,xt-x0);
401     else
402       dev = 0;
403   } 
404 */
405   return x0-xav;
406 }
407
408 static real calc_dist(FILE *log,rvec x[])
409 {
410   static gmx_bool bFirst=TRUE;
411   static gmx_bool bDist;
412   static int i1,i2;
413   char *buf;
414   rvec   dx;
415   
416   if (bFirst) {
417     if ((buf = getenv("DISTGCT")) == NULL)
418       bDist = FALSE;
419     else {
420       bDist  = (sscanf(buf,"%d%d",&i1,&i2) == 2);
421       if (bDist)
422         fprintf(log,"GCT: Will couple to distance between %d and %d\n",i1,i2);
423       else
424         fprintf(log,"GCT: Will not couple to distances\n");
425     }
426     bFirst = FALSE;
427   }
428   if (bDist) {
429     rvec_sub(x[i1],x[i2],dx);
430     return norm(dx);
431   }
432   else
433     return 0.0;
434 }
435
436 real run_aver(real old,real cur,int step,int nmem)
437 {
438   nmem   = max(1,nmem);
439   
440   return ((nmem-1)*old+cur)/nmem;
441 }
442
443 static void set_act_value(t_coupl_rec *tcr,int index,real val,int step)
444 {
445   tcr->act_value[index] = val;
446   tcr->av_value[index]  = run_aver(tcr->av_value[index],val,step,tcr->nmemory);
447 }
448
449 static void upd_f_value(FILE *log,int atnr,real xi,real dt,real factor,
450                         real ff[],int ati,int atj)
451 {
452   real fff;
453   
454   if (xi != 0) {
455     fff = 1 + (dt/xi)  * factor;
456     if (fff > 0)
457       set_factor_matrix(atnr,ff,sqrt(fff),ati,atj);
458   }
459 }
460
461 static void dump_fm(FILE *fp,int n,real f[],char *s)
462 {
463   int i,j;
464   
465   fprintf(fp,"Factor matrix (all numbers -1) %s\n",s);
466   for(i=0; (i<n); i++) {
467     for(j=0; (j<n); j++) 
468       fprintf(fp,"  %10.3e",f[n*i+j]-1.0);
469     fprintf(fp,"\n");
470   }
471 }
472
473 void do_coupling(FILE *log,const output_env_t oenv,int nfile,
474                  const t_filenm fnm[], t_coupl_rec *tcr,real t,
475                  int step,real ener[], t_forcerec *fr,t_inputrec *ir,
476                  gmx_bool bMaster, t_mdatoms *md,t_idef *idef,real mu_aver,int nmols,
477                  t_commrec *cr,matrix box,tensor virial,
478                  tensor pres,rvec mu_tot,
479                  rvec x[],rvec f[],gmx_bool bDoIt)
480 {
481 #define enm2Debye 48.0321
482 #define d2e(x) (x)/enm2Debye
483 #define enm2kjmol(x) (x)*0.0143952 /* = 2.0*4.0*M_PI*EPSILON0 */
484
485   static real *f6,*f12,*fa,*fb,*fc,*fq;
486   static gmx_bool bFirst = TRUE;
487   
488   int         i,j,ati,atj,atnr2,type,ftype;
489   real        deviation[eoObsNR],prdev[eoObsNR],epot0,dist,rmsf;
490   real        ff6,ff12,ffa,ffb,ffc,ffq,factor,dt,mu_ind;
491   real        Epol,Eintern,Virial,muabs,xiH=-1,xiS=-1,xi6,xi12;
492   rvec        fmol[2];
493   gmx_bool        bTest,bPrint;
494   t_coupl_LJ  *tclj;
495   t_coupl_BU  *tcbu;
496   t_coupl_Q   *tcq;
497   t_coupl_iparams *tip;
498   
499   atnr2 = idef->atnr * idef->atnr;
500   if (bFirst) {
501     if (PAR(cr))
502       fprintf(log,"GCT: this is parallel\n");
503     else
504       fprintf(log,"GCT: this is not parallel\n");
505     fflush(log);
506     snew(f6, atnr2);
507     snew(f12,atnr2);
508     snew(fa, atnr2);
509     snew(fb, atnr2);
510     snew(fc, atnr2);
511     snew(fq, idef->atnr);
512     
513     if (tcr->bVirial) {
514       int  nrdf = 0;
515       real TTT  = 0;
516       real Vol  = det(box);
517       
518       for(i=0; (i<ir->opts.ngtc); i++) {
519         nrdf += ir->opts.nrdf[i];
520         TTT  += ir->opts.nrdf[i]*ir->opts.ref_t[i];
521       }
522       TTT /= nrdf;
523       
524       /* Calculate reference virial from reference temperature and pressure */
525       tcr->ref_value[eoVir] = 0.5*BOLTZ*nrdf*TTT - (3.0/2.0)*
526         Vol*tcr->ref_value[eoPres];
527       
528       fprintf(log,"GCT: TTT = %g, nrdf = %d, vir0 = %g,  Vol = %g\n",
529               TTT,nrdf,tcr->ref_value[eoVir],Vol);
530       fflush(log);
531     }
532     bFirst = FALSE;
533   }
534   
535   bPrint = MASTER(cr) && do_per_step(step,ir->nstlog);
536   dt     = ir->delta_t;
537
538   /* Initiate coupling to the reference pressure and temperature to start
539    * coupling slowly.
540    */
541   if (step == 0) {
542     for(i=0; (i<eoObsNR); i++)
543       tcr->av_value[i] = tcr->ref_value[i];
544     if ((tcr->ref_value[eoDipole]) != 0.0) {
545       mu_ind = mu_aver - d2e(tcr->ref_value[eoDipole]); /* in e nm */
546       Epol   = mu_ind*mu_ind/(enm2kjmol(tcr->ref_value[eoPolarizability]));
547       tcr->av_value[eoEpot] -= Epol;
548       fprintf(log,"GCT: mu_aver = %g(D), mu_ind = %g(D), Epol = %g (kJ/mol)\n",
549               mu_aver*enm2Debye,mu_ind*enm2Debye,Epol);
550     }
551   }
552
553   /* We want to optimize the LJ params, usually to the Vaporization energy 
554    * therefore we only count intermolecular degrees of freedom.
555    * Note that this is now optional. switch UseEinter to yes in your gct file
556    * if you want this.
557    */
558   dist      = calc_dist(log,x);
559   muabs     = norm(mu_tot);
560   Eintern   = Ecouple(tcr,ener)/nmols;
561   Virial    = virial[XX][XX]+virial[YY][YY]+virial[ZZ][ZZ];
562
563   /*calc_force(md->nr,f,fmol);*/
564   clear_rvec(fmol[0]);
565   
566   /* Use a memory of tcr->nmemory steps, so we actually couple to the
567    * average observable over the last tcr->nmemory steps. This may help
568    * in avoiding local minima in parameter space.
569    */
570   set_act_value(tcr,eoPres, ener[F_PRES],step);
571   set_act_value(tcr,eoEpot, Eintern,     step);
572   set_act_value(tcr,eoVir,  Virial,      step);
573   set_act_value(tcr,eoDist, dist,        step);
574   set_act_value(tcr,eoMu,   muabs,       step);
575   set_act_value(tcr,eoFx,   fmol[0][XX], step);
576   set_act_value(tcr,eoFy,   fmol[0][YY], step);
577   set_act_value(tcr,eoFz,   fmol[0][ZZ], step);
578   set_act_value(tcr,eoPx,   pres[XX][XX],step);
579   set_act_value(tcr,eoPy,   pres[YY][YY],step);
580   set_act_value(tcr,eoPz,   pres[ZZ][ZZ],step);
581   
582   epot0 = tcr->ref_value[eoEpot];
583   /* If dipole != 0.0 assume we want to use polarization corrected coupling */
584   if ((tcr->ref_value[eoDipole]) != 0.0) {
585     mu_ind = mu_aver - d2e(tcr->ref_value[eoDipole]); /* in e nm */
586     
587     Epol = mu_ind*mu_ind/(enm2kjmol(tcr->ref_value[eoPolarizability]));
588     
589     epot0 -= Epol;
590     if (debug) {
591       fprintf(debug,"mu_ind: %g (%g D) mu_aver: %g (%g D)\n",
592               mu_ind,mu_ind*enm2Debye,mu_aver,mu_aver*enm2Debye);
593       fprintf(debug,"Eref %g Epol %g Erunav %g Eact %g\n",
594               tcr->ref_value[eoEpot],Epol,tcr->av_value[eoEpot],
595               tcr->act_value[eoEpot]);
596     }
597   }
598
599   if (bPrint) {
600     pr_ff(tcr,t,idef,cr,nfile,fnm,oenv);
601   }
602   /* Calculate the deviation of average value from the target value */
603   for(i=0; (i<eoObsNR); i++) {
604     deviation[i] = calc_deviation(tcr->av_value[i],tcr->act_value[i],
605                                   tcr->ref_value[i]);
606     prdev[i]     = tcr->ref_value[i] - tcr->act_value[i];
607   }
608   deviation[eoEpot] = calc_deviation(tcr->av_value[eoEpot],tcr->act_value[eoEpot],
609                                      epot0);
610   prdev[eoEpot]     = epot0 - tcr->act_value[eoEpot];
611   
612   if (bPrint)
613     pr_dev(tcr,t,prdev,cr,nfile,fnm,oenv);
614   
615   /* First set all factors to 1 */
616   for(i=0; (i<atnr2); i++) {
617     f6[i] = f12[i] = fa[i] = fb[i] = fc[i] = 1.0;
618   }
619   for(i=0; (i<idef->atnr); i++) 
620     fq[i] = 1.0;
621
622   /* Now compute the actual coupling compononents */   
623   if (!fr->bBHAM) {
624     if (bDoIt) {
625       for(i=0; (i<tcr->nLJ); i++) {
626         tclj=&(tcr->tcLJ[i]);
627         
628         ati=tclj->at_i;
629         atj=tclj->at_j;
630         
631         ff6 = ff12 = 1.0;       
632         
633         if (tclj->eObs == eoForce) {
634           gmx_fatal(FARGS,"Hack code for this to work again ");
635           if (debug)
636             fprintf(debug,"Have computed derivatives: xiH = %g, xiS = %g\n",xiH,xiS);
637           if (ati == 1) {
638             /* Hydrogen */
639             ff12 += xiH; 
640           }
641           else if (ati == 2) {
642             /* Shell */
643             ff12 += xiS; 
644           }
645           else
646             gmx_fatal(FARGS,"No H, no Shell, edit code at %s, line %d\n",
647                         __FILE__,__LINE__);
648           if (ff6 > 0)
649             set_factor_matrix(idef->atnr,f6, sqrt(ff6), ati,atj);
650           if (ff12 > 0)
651             set_factor_matrix(idef->atnr,f12,sqrt(ff12),ati,atj);
652         }
653         else {
654           if (debug)
655             fprintf(debug,"tcr->tcLJ[%d].xi_6 = %g, xi_12 = %g deviation = %g\n",i,
656                     tclj->xi_6,tclj->xi_12,deviation[tclj->eObs]);
657           factor=deviation[tclj->eObs];
658           
659           upd_f_value(log,idef->atnr,tclj->xi_6, dt,factor,f6, ati,atj);
660           upd_f_value(log,idef->atnr,tclj->xi_12,dt,factor,f12,ati,atj);
661         }
662       }
663     }
664     if (PAR(cr)) {
665       gprod(cr,atnr2,f6);
666       gprod(cr,atnr2,f12);
667 #ifdef DEBUGGCT
668       dump_fm(log,idef->atnr,f6,"f6");
669       dump_fm(log,idef->atnr,f12,"f12");
670 #endif
671     }
672     upd_nbfplj(log,fr->nbfp,idef->atnr,f6,f12,tcr->combrule);
673     
674     /* Copy for printing */
675     for(i=0; (i<tcr->nLJ); i++) {
676       tclj=&(tcr->tcLJ[i]);
677       ati = tclj->at_i;
678       atj = tclj->at_j;
679       if (atj == -1) 
680         atj = ati;
681       tclj->c6  =  C6(fr->nbfp,fr->ntype,ati,atj);
682       tclj->c12 = C12(fr->nbfp,fr->ntype,ati,atj);
683     }
684   }
685   else {
686     if (bDoIt) {
687       for(i=0; (i<tcr->nBU); i++) {
688         tcbu   = &(tcr->tcBU[i]);
689         factor = deviation[tcbu->eObs];
690         ati    = tcbu->at_i;
691         atj    = tcbu->at_j;
692         
693         upd_f_value(log,idef->atnr,tcbu->xi_a,dt,factor,fa,ati,atj);
694         upd_f_value(log,idef->atnr,tcbu->xi_b,dt,factor,fb,ati,atj);
695         upd_f_value(log,idef->atnr,tcbu->xi_c,dt,factor,fc,ati,atj);
696       }
697     }
698     if (PAR(cr)) {
699       gprod(cr,atnr2,fa);
700       gprod(cr,atnr2,fb);
701       gprod(cr,atnr2,fc);
702     }
703     upd_nbfpbu(log,fr->nbfp,idef->atnr,fa,fb,fc);
704     /* Copy for printing */
705     for(i=0; (i<tcr->nBU); i++) {
706       tcbu=&(tcr->tcBU[i]);
707       ati = tcbu->at_i;
708       atj = tcbu->at_j;
709       if (atj == -1) 
710         atj = ati;
711       tcbu->a = BHAMA(fr->nbfp,fr->ntype,ati,atj);
712       tcbu->b = BHAMB(fr->nbfp,fr->ntype,ati,atj);
713       tcbu->c = BHAMC(fr->nbfp,fr->ntype,ati,atj);
714       if (debug)
715         fprintf(debug,"buck (type=%d) = %e, %e, %e\n",
716                 tcbu->at_i,tcbu->a,tcbu->b,tcbu->c);
717     }
718   }
719   if (bDoIt) {
720     for(i=0; (i<tcr->nQ); i++) {
721       tcq=&(tcr->tcQ[i]);
722       if (tcq->xi_Q)     
723         ffq = 1.0 + (dt/tcq->xi_Q) * deviation[tcq->eObs];
724       else
725         ffq = 1.0;
726       fq[tcq->at_i] *= ffq;
727     }
728   }
729   if (PAR(cr))
730     gprod(cr,idef->atnr,fq);
731   
732   for(j=0; (j<md->nr); j++) {
733     md->chargeA[j] *= fq[md->typeA[j]];
734   }
735   for(i=0; (i<tcr->nQ); i++) {
736     tcq=&(tcr->tcQ[i]);
737     for(j=0; (j<md->nr); j++) {
738       if (md->typeA[j] == tcq->at_i) {
739         tcq->Q = md->chargeA[j];
740         break;
741       }
742     }
743     if (j == md->nr)
744       gmx_fatal(FARGS,"Coupling type %d not found",tcq->at_i);
745   }  
746   for(i=0; (i<tcr->nIP); i++) {
747     tip    = &(tcr->tIP[i]);
748     type   = tip->type;
749     ftype  = idef->functype[type];
750     factor = dt*deviation[tip->eObs];
751       
752     switch(ftype) {
753     case F_BONDS:
754       if (tip->xi.harmonic.krA) idef->iparams[type].harmonic.krA *= (1+factor/tip->xi.harmonic.krA);
755       if (tip->xi.harmonic.rA) idef->iparams[type].harmonic.rA *= (1+factor/tip->xi.harmonic.rA);
756         break;
757     default:
758       break;
759     }
760     tip->iprint=idef->iparams[type];
761   }
762 }
763