Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / mdlib / force.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.2.0
11  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13  * Copyright (c) 2001-2004, The GROMACS development team,
14  * check out http://www.gromacs.org for more information.
15
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * For more info, check our website at http://www.gromacs.org
32  * 
33  * And Hey:
34  * GROwing Monsters And Cloning Shrimps
35  */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include <math.h>
41 #include <string.h>
42 #include <assert.h>
43 #include "sysstuff.h"
44 #include "typedefs.h"
45 #include "macros.h"
46 #include "smalloc.h"
47 #include "macros.h"
48 #include "physics.h"
49 #include "force.h"
50 #include "nonbonded.h"
51 #include "names.h"
52 #include "network.h"
53 #include "pbc.h"
54 #include "ns.h"
55 #include "nrnb.h"
56 #include "bondf.h"
57 #include "mshift.h"
58 #include "txtdump.h"
59 #include "coulomb.h"
60 #include "pme.h"
61 #include "mdrun.h"
62 #include "domdec.h"
63 #include "partdec.h"
64 #include "qmmm.h"
65
66
67 void ns(FILE *fp,
68         t_forcerec *fr,
69         rvec       x[],
70         matrix     box,
71         gmx_groups_t *groups,
72         t_grpopts  *opts,
73         gmx_localtop_t *top,
74         t_mdatoms  *md,
75         t_commrec  *cr,
76         t_nrnb     *nrnb,
77         real       lambda,
78         real       *dvdlambda,
79         gmx_grppairener_t *grppener,
80         gmx_bool       bFillGrid,
81         gmx_bool       bDoLongRange,
82         gmx_bool       bDoForces,
83         rvec       *f)
84 {
85   char   *ptr;
86   int    nsearch;
87
88
89   if (!fr->ns.nblist_initialized)
90   {
91       init_neighbor_list(fp, fr, md->homenr);
92   }
93     
94   if (fr->bTwinRange) 
95     fr->nlr=0;
96
97     nsearch = search_neighbours(fp,fr,x,box,top,groups,cr,nrnb,md,
98                                 lambda,dvdlambda,grppener,
99                                 bFillGrid,bDoLongRange,
100                                 bDoForces,f);
101   if (debug)
102     fprintf(debug,"nsearch = %d\n",nsearch);
103     
104   /* Check whether we have to do dynamic load balancing */
105   /*if ((nsb->nstDlb > 0) && (mod(step,nsb->nstDlb) == 0))
106     count_nb(cr,nsb,&(top->blocks[ebCGS]),nns,fr->nlr,
107     &(top->idef),opts->ngener);
108   */
109   if (fr->ns.dump_nl > 0)
110     dump_nblist(fp,cr,fr,fr->ns.dump_nl);
111 }
112
113 void do_force_lowlevel(FILE       *fplog,   gmx_large_int_t step,
114                        t_forcerec *fr,      t_inputrec *ir,
115                        t_idef     *idef,    t_commrec  *cr,
116                        t_nrnb     *nrnb,    gmx_wallcycle_t wcycle,
117                        t_mdatoms  *md,
118                        t_grpopts  *opts,
119                        rvec       x[],      history_t  *hist,
120                        rvec       f[],
121                        gmx_enerdata_t *enerd,
122                        t_fcdata   *fcd,
123                        gmx_mtop_t     *mtop,
124                        gmx_localtop_t *top,
125                        gmx_genborn_t *born,
126                        t_atomtypes *atype,
127                        gmx_bool       bBornRadii,
128                        matrix     box,
129                        real       lambda,  
130                        t_graph    *graph,
131                        t_blocka   *excl,    
132                        rvec       mu_tot[],
133                        int        flags,
134                        float      *cycles_pme)
135 {
136     int     i,status;
137     int     donb_flags;
138     gmx_bool    bDoEpot,bSepDVDL,bSB;
139     int     pme_flags;
140     matrix  boxs;
141     rvec    box_size;
142     real    dvdlambda,Vsr,Vlr,Vcorr=0,vdip,vcharge;
143     t_pbc   pbc;
144     real    dvdgb;
145     char    buf[22];
146     gmx_enerdata_t ed_lam;
147     double  lam_i;
148     real    dvdl_dum;
149
150 #ifdef GMX_MPI
151     double  t0=0.0,t1,t2,t3; /* time measurement for coarse load balancing */
152 #endif
153     
154 #define PRINT_SEPDVDL(s,v,dvdl) if (bSepDVDL) fprintf(fplog,sepdvdlformat,s,v,dvdl);
155     
156
157     set_pbc(&pbc,fr->ePBC,box);
158     
159     /* Reset box */
160     for(i=0; (i<DIM); i++)
161     {
162         box_size[i]=box[i][i];
163     }
164     
165     bSepDVDL=(fr->bSepDVDL && do_per_step(step,ir->nstlog));
166     debug_gmx();
167     
168     /* do QMMM first if requested */
169     if(fr->bQMMM)
170     {
171         enerd->term[F_EQM] = calculate_QMMM(cr,x,f,fr,md);
172     }
173     
174     if (bSepDVDL)
175     {
176         fprintf(fplog,"Step %s: non-bonded V and dVdl for node %d:\n",
177                 gmx_step_str(step,buf),cr->nodeid);
178     }
179     
180     /* Call the short range functions all in one go. */
181     
182     dvdlambda = 0;
183     
184 #ifdef GMX_MPI
185     /*#define TAKETIME ((cr->npmenodes) && (fr->timesteps < 12))*/
186 #define TAKETIME FALSE
187     if (TAKETIME)
188     {
189         MPI_Barrier(cr->mpi_comm_mygroup);
190         t0=MPI_Wtime();
191     }
192 #endif
193     
194     if (ir->nwall)
195     {
196         dvdlambda = do_walls(ir,fr,box,md,x,f,lambda,
197                              enerd->grpp.ener[egLJSR],nrnb);
198         PRINT_SEPDVDL("Walls",0.0,dvdlambda);
199         enerd->dvdl_lin += dvdlambda;
200     }
201                 
202         /* If doing GB, reset dvda and calculate the Born radii */
203         if (ir->implicit_solvent)
204         {
205                 /* wallcycle_start(wcycle,ewcGB); */
206                 
207                 for(i=0;i<born->nr;i++)
208                 {
209                         fr->dvda[i]=0;
210                 }
211                 
212                 if(bBornRadii)
213                 {
214                         calc_gb_rad(cr,fr,ir,top,atype,x,&(fr->gblist),born,md,nrnb);
215                 }
216                 
217                 /* wallcycle_stop(wcycle, ewcGB); */
218         }
219         
220     where();
221     donb_flags = 0;
222     if (flags & GMX_FORCE_FORCES)
223     {
224         donb_flags |= GMX_DONB_FORCES;
225     }
226     do_nonbonded(cr,fr,x,f,md,excl,
227                  fr->bBHAM ?
228                  enerd->grpp.ener[egBHAMSR] :
229                  enerd->grpp.ener[egLJSR],
230                  enerd->grpp.ener[egCOULSR],
231                                  enerd->grpp.ener[egGB],box_size,nrnb,
232                  lambda,&dvdlambda,-1,-1,donb_flags);
233     /* If we do foreign lambda and we have soft-core interactions
234      * we have to recalculate the (non-linear) energies contributions.
235      */
236     if (ir->n_flambda > 0 && (flags & GMX_FORCE_DHDL) && ir->sc_alpha != 0)
237     {
238         init_enerdata(mtop->groups.grps[egcENER].nr,ir->n_flambda,&ed_lam);
239         
240         for(i=0; i<enerd->n_lambda; i++)
241         {
242             lam_i = (i==0 ? lambda : ir->flambda[i-1]);
243             dvdl_dum = 0;
244             reset_enerdata(&ir->opts,fr,TRUE,&ed_lam,FALSE);
245             do_nonbonded(cr,fr,x,f,md,excl,
246                          fr->bBHAM ?
247                          ed_lam.grpp.ener[egBHAMSR] :
248                          ed_lam.grpp.ener[egLJSR],
249                          ed_lam.grpp.ener[egCOULSR],
250                          enerd->grpp.ener[egGB], box_size,nrnb,
251                          lam_i,&dvdl_dum,-1,-1,
252                          GMX_DONB_FOREIGNLAMBDA);
253             sum_epot(&ir->opts,&ed_lam);
254             enerd->enerpart_lambda[i] += ed_lam.term[F_EPOT];
255         }
256         destroy_enerdata(&ed_lam);
257     }
258     where();
259         
260         /* If we are doing GB, calculate bonded forces and apply corrections 
261          * to the solvation forces */
262         if (ir->implicit_solvent)  {
263                 calc_gb_forces(cr,md,born,top,atype,x,f,fr,idef,
264                        ir->gb_algorithm,ir->sa_algorithm,nrnb,bBornRadii,&pbc,graph,enerd);
265     }
266
267 #ifdef GMX_MPI
268     if (TAKETIME)
269     {
270         t1=MPI_Wtime();
271         fr->t_fnbf += t1-t0;
272     }
273 #endif
274     
275     if (ir->sc_alpha != 0)
276     {
277         enerd->dvdl_nonlin += dvdlambda;
278     }
279     else
280     {
281         enerd->dvdl_lin    += dvdlambda;
282     }
283     Vsr = 0;
284     if (bSepDVDL)
285     {
286         for(i=0; i<enerd->grpp.nener; i++)
287         {
288             Vsr +=
289                 (fr->bBHAM ?
290                  enerd->grpp.ener[egBHAMSR][i] :
291                  enerd->grpp.ener[egLJSR][i])
292                 + enerd->grpp.ener[egCOULSR][i] + enerd->grpp.ener[egGB][i];
293         }
294     }
295     PRINT_SEPDVDL("VdW and Coulomb SR particle-p.",Vsr,dvdlambda);
296     debug_gmx();
297     
298     if (debug)
299     {
300         pr_rvecs(debug,0,"fshift after SR",fr->fshift,SHIFTS);
301     }
302     
303     /* Shift the coordinates. Must be done before bonded forces and PPPM, 
304      * but is also necessary for SHAKE and update, therefore it can NOT 
305      * go when no bonded forces have to be evaluated.
306      */
307     
308     /* Here sometimes we would not need to shift with NBFonly,
309      * but we do so anyhow for consistency of the returned coordinates.
310      */
311     if (graph)
312     {
313         shift_self(graph,box,x);
314         if (TRICLINIC(box))
315         {
316             inc_nrnb(nrnb,eNR_SHIFTX,2*graph->nnodes);
317         }
318         else
319         {
320             inc_nrnb(nrnb,eNR_SHIFTX,graph->nnodes);
321         }
322     }
323     /* Check whether we need to do bondeds or correct for exclusions */
324     if (fr->bMolPBC &&
325         ((flags & GMX_FORCE_BONDED)
326          || EEL_RF(fr->eeltype) || EEL_FULL(fr->eeltype)))
327     {
328         /* Since all atoms are in the rectangular or triclinic unit-cell,
329          * only single box vector shifts (2 in x) are required.
330          */
331         set_pbc_dd(&pbc,fr->ePBC,cr->dd,TRUE,box);
332     }
333     debug_gmx();
334     
335     if (flags & GMX_FORCE_BONDED)
336     {
337         calc_bonds(fplog,cr->ms,
338                    idef,x,hist,f,fr,&pbc,graph,enerd,nrnb,lambda,md,fcd,
339                    DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL, atype, born,
340                    fr->bSepDVDL && do_per_step(step,ir->nstlog),step);
341         
342         /* Check if we have to determine energy differences
343          * at foreign lambda's.
344          */
345         if (ir->n_flambda > 0 && (flags & GMX_FORCE_DHDL) &&
346             idef->ilsort != ilsortNO_FE)
347         {
348             if (idef->ilsort != ilsortFE_SORTED)
349             {
350                 gmx_incons("The bonded interactions are not sorted for free energy");
351             }
352             init_enerdata(mtop->groups.grps[egcENER].nr,ir->n_flambda,&ed_lam);
353             
354             for(i=0; i<enerd->n_lambda; i++)
355             {
356                 lam_i = (i==0 ? lambda : ir->flambda[i-1]);
357                 dvdl_dum = 0;
358                 reset_enerdata(&ir->opts,fr,TRUE,&ed_lam,FALSE);
359                 calc_bonds_lambda(fplog,
360                                   idef,x,fr,&pbc,graph,&ed_lam,nrnb,lam_i,md,
361                                   fcd,
362                                   DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL);
363                 sum_epot(&ir->opts,&ed_lam);
364                 enerd->enerpart_lambda[i] += ed_lam.term[F_EPOT];
365             }
366             destroy_enerdata(&ed_lam);
367         }
368         debug_gmx();
369     }
370
371     where();
372
373     *cycles_pme = 0;
374     if (EEL_FULL(fr->eeltype))
375     {
376         bSB = (ir->nwall == 2);
377         if (bSB)
378         {
379             copy_mat(box,boxs);
380             svmul(ir->wall_ewald_zfac,boxs[ZZ],boxs[ZZ]);
381             box_size[ZZ] *= ir->wall_ewald_zfac;
382         }
383         
384         clear_mat(fr->vir_el_recip);    
385         
386         if (fr->bEwald)
387         {
388             if (fr->n_tpi == 0)
389             {
390                 dvdlambda = 0;
391                 Vcorr = ewald_LRcorrection(fplog,md->start,md->start+md->homenr,
392                                            cr,fr,
393                                            md->chargeA,
394                                            md->nChargePerturbed ? md->chargeB : NULL,
395                                            excl,x,bSB ? boxs : box,mu_tot,
396                                            ir->ewald_geometry,
397                                            ir->epsilon_surface,
398                                            lambda,&dvdlambda,&vdip,&vcharge);
399                 PRINT_SEPDVDL("Ewald excl./charge/dip. corr.",Vcorr,dvdlambda);
400                 enerd->dvdl_lin += dvdlambda;
401             }
402             else
403             {
404                 if (ir->ewald_geometry != eewg3D || ir->epsilon_surface != 0)
405                 {
406                     gmx_fatal(FARGS,"TPI with PME currently only works in a 3D geometry with tin-foil boundary conditions");
407                 }
408                 /* The TPI molecule does not have exclusions with the rest
409                  * of the system and no intra-molecular PME grid contributions
410                  * will be calculated in gmx_pme_calc_energy.
411                  */
412                 Vcorr = 0;
413             }
414         }
415         
416         dvdlambda = 0;
417         status = 0;
418         switch (fr->eeltype)
419         {
420         case eelPME:
421         case eelPMESWITCH:
422         case eelPMEUSER:
423         case eelPMEUSERSWITCH:
424         case eelP3M_AD:
425             if (cr->duty & DUTY_PME)
426             {
427                 assert(fr->n_tpi >= 0);
428                 if (fr->n_tpi == 0 || (flags & GMX_FORCE_STATECHANGED))
429                 {
430                     pme_flags = GMX_PME_SPREAD_Q | GMX_PME_SOLVE;
431                     if (flags & GMX_FORCE_FORCES)
432                     {
433                         pme_flags |= GMX_PME_CALC_F;
434                     }
435                     if (flags & GMX_FORCE_VIRIAL)
436                     {
437                         pme_flags |= GMX_PME_CALC_ENER_VIR;
438                     }
439                     if (fr->n_tpi > 0)
440                     {
441                         /* We don't calculate f, but we do want the potential */
442                         pme_flags |= GMX_PME_CALC_POT;
443                     }
444                     wallcycle_start(wcycle,ewcPMEMESH);
445                     status = gmx_pme_do(fr->pmedata,
446                                         md->start,md->homenr - fr->n_tpi,
447                                         x,fr->f_novirsum,
448                                         md->chargeA,md->chargeB,
449                                         bSB ? boxs : box,cr,
450                                         DOMAINDECOMP(cr) ? dd_pme_maxshift_x(cr->dd) : 0,
451                                         DOMAINDECOMP(cr) ? dd_pme_maxshift_y(cr->dd) : 0,
452                                         nrnb,wcycle,
453                                         fr->vir_el_recip,fr->ewaldcoeff,
454                                         &Vlr,lambda,&dvdlambda,
455                                         pme_flags);
456                     *cycles_pme = wallcycle_stop(wcycle,ewcPMEMESH);
457
458                     /* We should try to do as little computation after
459                      * this as possible, because parallel PME synchronizes
460                      * the nodes, so we want all load imbalance of the rest
461                      * of the force calculation to be before the PME call.
462                      * DD load balancing is done on the whole time of
463                      * the force call (without PME).
464                      */
465                 }
466                 if (fr->n_tpi > 0)
467                 {
468                     /* Determine the PME grid energy of the test molecule
469                      * with the PME grid potential of the other charges.
470                      */
471                     gmx_pme_calc_energy(fr->pmedata,fr->n_tpi,
472                                         x + md->homenr - fr->n_tpi,
473                                         md->chargeA + md->homenr - fr->n_tpi,
474                                         &Vlr);
475                 }
476                 PRINT_SEPDVDL("PME mesh",Vlr,dvdlambda);
477             } 
478             else
479             {
480                 /* Energies and virial are obtained later from the PME nodes */
481                 /* but values have to be zeroed out here */
482                 Vlr=0.0;
483             }
484             break;
485         case eelEWALD:
486             Vlr = do_ewald(fplog,FALSE,ir,x,fr->f_novirsum,
487                            md->chargeA,md->chargeB,
488                            box_size,cr,md->homenr,
489                            fr->vir_el_recip,fr->ewaldcoeff,
490                            lambda,&dvdlambda,fr->ewald_table);
491             PRINT_SEPDVDL("Ewald long-range",Vlr,dvdlambda);
492             break;
493         default:
494             Vlr = 0;
495             gmx_fatal(FARGS,"No such electrostatics method implemented %s",
496                       eel_names[fr->eeltype]);
497         }
498         if (status != 0)
499         {
500             gmx_fatal(FARGS,"Error %d in long range electrostatics routine %s",
501                       status,EELTYPE(fr->eeltype));
502                 }
503         enerd->dvdl_lin += dvdlambda;
504         enerd->term[F_COUL_RECIP] = Vlr + Vcorr;
505         if (debug)
506         {
507             fprintf(debug,"Vlr = %g, Vcorr = %g, Vlr_corr = %g\n",
508                     Vlr,Vcorr,enerd->term[F_COUL_RECIP]);
509             pr_rvecs(debug,0,"vir_el_recip after corr",fr->vir_el_recip,DIM);
510             pr_rvecs(debug,0,"fshift after LR Corrections",fr->fshift,SHIFTS);
511         }
512     }
513     else
514     {
515         if (EEL_RF(fr->eeltype))
516         {
517             dvdlambda = 0;
518             
519             if (fr->eeltype != eelRF_NEC)
520             {
521                 enerd->term[F_RF_EXCL] =
522                     RF_excl_correction(fplog,fr,graph,md,excl,x,f,
523                                        fr->fshift,&pbc,lambda,&dvdlambda);
524             }
525             
526             enerd->dvdl_lin += dvdlambda;
527             PRINT_SEPDVDL("RF exclusion correction",
528                           enerd->term[F_RF_EXCL],dvdlambda);
529         }
530     }
531     where();
532     debug_gmx();
533         
534     if (debug)
535     {
536         print_nrnb(debug,nrnb); 
537     }
538     debug_gmx();
539     
540 #ifdef GMX_MPI
541     if (TAKETIME)
542     {
543         t2=MPI_Wtime();
544         MPI_Barrier(cr->mpi_comm_mygroup);
545         t3=MPI_Wtime();
546         fr->t_wait += t3-t2;
547         if (fr->timesteps == 11)
548         {
549             fprintf(stderr,"* PP load balancing info: node %d, step %s, rel wait time=%3.0f%% , load string value: %7.2f\n", 
550                     cr->nodeid, gmx_step_str(fr->timesteps,buf), 
551                     100*fr->t_wait/(fr->t_wait+fr->t_fnbf), 
552                     (fr->t_fnbf+fr->t_wait)/fr->t_fnbf);
553         }         
554         fr->timesteps++;
555     }
556 #endif
557     
558     if (debug)
559     {
560         pr_rvecs(debug,0,"fshift after bondeds",fr->fshift,SHIFTS);
561     }
562     
563 }
564
565 void init_enerdata(int ngener,int n_flambda,gmx_enerdata_t *enerd)
566 {
567     int i,n2;
568     
569     for(i=0; i<F_NRE; i++)
570     {
571         enerd->term[i] = 0;
572     }
573     
574     n2=ngener*ngener;
575     if (debug)
576     {
577         fprintf(debug,"Creating %d sized group matrix for energies\n",n2);
578     }
579     enerd->grpp.nener = n2;
580     for(i=0; (i<egNR); i++)
581     {
582         snew(enerd->grpp.ener[i],n2);
583     }
584
585     if (n_flambda)
586     {
587         enerd->n_lambda = 1 + n_flambda;
588         snew(enerd->enerpart_lambda,enerd->n_lambda);
589     }
590     else
591     {
592         enerd->n_lambda = 0;
593     }
594 }
595
596 void destroy_enerdata(gmx_enerdata_t *enerd)
597 {
598     int i;
599
600     for(i=0; (i<egNR); i++)
601     {
602         sfree(enerd->grpp.ener[i]);
603     }
604
605     if (enerd->n_lambda)
606     {
607         sfree(enerd->enerpart_lambda);
608     }
609 }
610
611 static real sum_v(int n,real v[])
612 {
613   real t;
614   int  i;
615   
616   t = 0.0;
617   for(i=0; (i<n); i++)
618     t = t + v[i];
619     
620   return t;
621 }
622
623 void sum_epot(t_grpopts *opts,gmx_enerdata_t *enerd)
624 {
625   gmx_grppairener_t *grpp;
626   real *epot;
627   int i;
628   
629   grpp = &enerd->grpp;
630   epot = enerd->term;
631
632   /* Accumulate energies */
633   epot[F_COUL_SR]  = sum_v(grpp->nener,grpp->ener[egCOULSR]);
634   epot[F_LJ]       = sum_v(grpp->nener,grpp->ener[egLJSR]);
635   epot[F_LJ14]     = sum_v(grpp->nener,grpp->ener[egLJ14]);
636   epot[F_COUL14]   = sum_v(grpp->nener,grpp->ener[egCOUL14]);
637   epot[F_COUL_LR]  = sum_v(grpp->nener,grpp->ener[egCOULLR]);
638   epot[F_LJ_LR]    = sum_v(grpp->nener,grpp->ener[egLJLR]);
639   /* We have already added 1-2,1-3, and 1-4 terms to F_GBPOL */
640   epot[F_GBPOL]   += sum_v(grpp->nener,grpp->ener[egGB]);
641     
642 /* lattice part of LR doesnt belong to any group
643  * and has been added earlier
644  */
645   epot[F_BHAM]     = sum_v(grpp->nener,grpp->ener[egBHAMSR]);
646   epot[F_BHAM_LR]  = sum_v(grpp->nener,grpp->ener[egBHAMLR]);
647
648   epot[F_EPOT] = 0;
649   for(i=0; (i<F_EPOT); i++)
650     if (i != F_DISRESVIOL && i != F_ORIRESDEV && i != F_DIHRESVIOL)
651       epot[F_EPOT] += epot[i];
652 }
653
654 void sum_dhdl(gmx_enerdata_t *enerd,double lambda,t_inputrec *ir)
655 {
656     int i;
657     double dlam,dhdl_lin;
658
659     enerd->term[F_DVDL] = enerd->dvdl_lin + enerd->dvdl_nonlin;
660
661     if (debug)
662     {
663         fprintf(debug,"dvdl: %f, non-linear %f + linear %f\n",
664                 enerd->term[F_DVDL],enerd->dvdl_nonlin,enerd->dvdl_lin);
665     }
666
667     /* Notes on the foreign lambda free energy difference evaluation:
668      * Adding the potential and ekin terms that depend linearly on lambda
669      * as delta lam * dvdl to the energy differences is exact.
670      * For the constraint dvdl this is not exact, but we have no other option.
671      * For the non-bonded LR term we assume that the soft-core (if present)
672      * no longer affects the energy beyond the short-range cut-off,
673      * which is a very good approximation (except for exotic settings).
674      */
675     for(i=1; i<enerd->n_lambda; i++)
676     {
677         dlam = (ir->flambda[i-1] - lambda);
678         dhdl_lin =
679             enerd->dvdl_lin + enerd->term[F_DKDL] + enerd->term[F_DHDL_CON];
680         if (debug)
681         {
682             fprintf(debug,"enerdiff lam %g: non-linear %f linear %f*%f\n",
683                     ir->flambda[i-1],
684                     enerd->enerpart_lambda[i] - enerd->enerpart_lambda[0],
685                     dlam,dhdl_lin);
686         }
687         enerd->enerpart_lambda[i] += dlam*dhdl_lin;
688
689     }
690 }
691
692 void reset_enerdata(t_grpopts *opts,
693                     t_forcerec *fr,gmx_bool bNS,
694                     gmx_enerdata_t *enerd,
695                     gmx_bool bMaster)
696 {
697   gmx_bool bKeepLR;
698   int  i,j;
699   
700   /* First reset all energy components, except for the long range terms
701    * on the master at non neighbor search steps, since the long range
702    * terms have already been summed at the last neighbor search step.
703    */
704   bKeepLR = (fr->bTwinRange && !bNS);
705   for(i=0; (i<egNR); i++) {
706     if (!(bKeepLR && bMaster && (i == egCOULLR || i == egLJLR))) {
707       for(j=0; (j<enerd->grpp.nener); j++)
708         enerd->grpp.ener[i][j] = 0.0;
709     }
710   }
711   enerd->dvdl_lin    = 0.0;
712   enerd->dvdl_nonlin = 0.0;
713
714   /* Normal potential energy components */
715   for(i=0; (i<=F_EPOT); i++) {
716     enerd->term[i] = 0.0;
717   }
718   /* Initialize the dVdlambda term with the long range contribution */
719   enerd->term[F_DVDL]     = 0.0;
720   enerd->term[F_DKDL]     = 0.0;
721   enerd->term[F_DHDL_CON] = 0.0;
722   if (enerd->n_lambda > 0)
723   {
724       for(i=0; i<enerd->n_lambda; i++)
725       {
726           enerd->enerpart_lambda[i] = 0.0;
727       }
728   }
729 }