Merge branch 'master' of git@git.gromacs.org:gromacs
[gromacs/rigid-bodies.git] / src / mdlib / force.c
blobb8fa968259f6602072577f7366ecb8febf874b1d
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
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.
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.
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.
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.
31 * For more info, check our website at http://www.gromacs.org
33 * And Hey:
34 * GROwing Monsters And Cloning Shrimps
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
40 #include <math.h>
41 #include <string.h>
42 #include "sysstuff.h"
43 #include "typedefs.h"
44 #include "macros.h"
45 #include "smalloc.h"
46 #include "macros.h"
47 #include "physics.h"
48 #include "force.h"
49 #include "nonbonded.h"
50 #include "names.h"
51 #include "network.h"
52 #include "pbc.h"
53 #include "ns.h"
54 #include "nrnb.h"
55 #include "bondf.h"
56 #include "mshift.h"
57 #include "txtdump.h"
58 #include "coulomb.h"
59 #include "pppm.h"
60 #include "pme.h"
61 #include "mdrun.h"
62 #include "domdec.h"
63 #include "partdec.h"
64 #include "qmmm.h"
65 #include "mpelogging.h"
68 void ns(FILE *fp,
69 t_forcerec *fr,
70 rvec x[],
71 matrix box,
72 gmx_groups_t *groups,
73 t_grpopts *opts,
74 gmx_localtop_t *top,
75 t_mdatoms *md,
76 t_commrec *cr,
77 t_nrnb *nrnb,
78 real *lambda,
79 real *dvdl,
80 gmx_grppairener_t *grppener,
81 bool bFillGrid,
82 bool bDoLongRange,
83 bool bDoForces,
84 rvec *f)
86 char *ptr;
87 int nsearch;
89 GMX_MPE_LOG(ev_ns_start);
90 if (!fr->ns.nblist_initialized)
92 init_neighbor_list(fp, fr, md->homenr);
95 if (fr->bTwinRange)
96 fr->nlr=0;
98 /* no FEP possibilities? Should this be noted in grompp? check. MRS */
99 nsearch = search_neighbours(fp,fr,x,box,top,groups,cr,nrnb,md,
100 lambda,dvdl,grppener,
101 bFillGrid,bDoLongRange,
102 bDoForces,f);
103 if (debug)
104 fprintf(debug,"nsearch = %d\n",nsearch);
106 /* Check whether we have to do dynamic load balancing */
107 /*if ((nsb->nstDlb > 0) && (mod(step,nsb->nstDlb) == 0))
108 count_nb(cr,nsb,&(top->blocks[ebCGS]),nns,fr->nlr,
109 &(top->idef),opts->ngener);
111 if (fr->ns.dump_nl > 0)
112 dump_nblist(fp,cr,fr,fr->ns.dump_nl);
114 GMX_MPE_LOG(ev_ns_finish);
117 void do_force_lowlevel(FILE *fplog, gmx_large_int_t step,
118 t_forcerec *fr, t_inputrec *ir,
119 t_idef *idef, t_commrec *cr,
120 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
121 t_mdatoms *md,
122 t_grpopts *opts,
123 rvec x[], history_t *hist,
124 rvec f[],
125 gmx_enerdata_t *enerd,
126 t_fcdata *fcd,
127 gmx_mtop_t *mtop,
128 gmx_localtop_t *top,
129 gmx_genborn_t *born,
130 t_atomtypes *atype,
131 bool bBornRadii,
132 matrix box,
133 t_lambda *fepvals,
134 real *lambda,
135 t_graph *graph,
136 t_blocka *excl,
137 rvec mu_tot[],
138 int flags,
139 float *cycles_pme)
141 int i,j,status;
142 int donb_flags;
143 bool bDoEpot,bSepDVDL,bSB;
144 int pme_flags;
145 matrix boxs;
146 rvec box_size;
147 real Vsr,Vlr,Vcorr=0,vdip,vcharge;
148 t_pbc pbc;
149 real dvdgb;
150 char buf[22];
151 gmx_enerdata_t ed_lam;
152 double clam_i,vlam_i;
153 real dvdl_dum[efptNR], dvdl[efptNR], lambda_dum[efptNR];
154 real dvdlsum;
156 #ifdef GMX_MPI
157 double t0=0.0,t1,t2,t3; /* time measurement for coarse load balancing */
158 #endif
160 #define PRINT_SEPDVDL(s,v,dvdl) if (bSepDVDL) fprintf(fplog,sepdvdlformat,s,v,dvdl);
162 GMX_MPE_LOG(ev_force_start);
164 /* reset free energy components */
165 for (i=0;i<efptNR;i++)
167 dvdl[i] = 0;
168 dvdl_dum[i] = 0;
171 /* Reset box */
172 for(i=0; (i<DIM); i++)
174 box_size[i]=box[i][i];
177 bSepDVDL=(fr->bSepDVDL && do_per_step(step,ir->nstlog));
178 debug_gmx();
180 /* do QMMM first if requested */
181 if(fr->bQMMM)
183 enerd->term[F_EQM] = calculate_QMMM(cr,x,f,fr,md);
186 if (bSepDVDL)
188 fprintf(fplog,"Step %s: non-bonded V and dVdl for node %d:\n",
189 gmx_step_str(step,buf),cr->nodeid);
192 /* Call the short range functions all in one go. */
193 GMX_MPE_LOG(ev_do_fnbf_start);
195 #ifdef GMX_MPI
196 /*#define TAKETIME ((cr->npmenodes) && (fr->timesteps < 12))*/
197 #define TAKETIME FALSE
198 if (TAKETIME)
200 MPI_Barrier(cr->mpi_comm_mygroup);
201 t0=MPI_Wtime();
203 #endif
205 if (ir->nwall)
207 /* add foreign lambda component to walls! */
208 do_walls(ir,fr,box,md,x,f,lambda[efptVDW],&dvdl[efptVDW],
209 enerd->grpp.ener[egLJSR],nrnb);
210 PRINT_SEPDVDL("Walls",0.0,dvdl[efptVDW]);
211 enerd->dvdl_lin[efptVDW] += dvdl[efptVDW];
214 /* If doing GB, reset dvda and calculate the Born radii */
215 if (ir->implicit_solvent)
217 /* wallcycle_start(wcycle,ewcGB); */
219 for(i=0;i<born->nr;i++)
221 fr->dvda[i]=0;
224 if(bBornRadii)
226 calc_gb_rad(cr,fr,ir,top,atype,x,&(fr->gblist),born,md,nrnb);
229 /* wallcycle_stop(wcycle, ewcGB); */
232 where();
233 donb_flags = 0;
234 if (flags & GMX_FORCE_FORCES)
236 donb_flags |= GMX_DONB_FORCES;
238 do_nonbonded(cr,fr,x,f,md,excl,
239 fr->bBHAM ?
240 enerd->grpp.ener[egBHAMSR] :
241 enerd->grpp.ener[egLJSR],
242 enerd->grpp.ener[egCOULSR],
243 enerd->grpp.ener[egGB],box_size,nrnb,
244 lambda,dvdl,-1,-1,donb_flags);
245 /* If we do foreign lambda and we have soft-core interactions
246 * we have to recalculate the (non-linear) energies contributions.
248 if (fepvals->n_lambda > 0 && (flags & GMX_FORCE_DHDL) && fepvals->sc_alpha != 0)
250 init_enerdata(mtop->groups.grps[egcENER].nr,fepvals->n_lambda,&ed_lam);
251 for(i=0; i<=enerd->n_lambda; i++)
253 for (j=0;j<efptNR;j++) {
254 if (i==0)
256 lambda_dum[j] = lambda[j];
258 else
260 lambda_dum[j] = fepvals->all_lambda[j][i-1];
263 /* currently, evaluates all energies. Eventually, can eliminate the call to the same lambda */
264 reset_enerdata(&ir->opts,fr,TRUE,&ed_lam,FALSE);
265 do_nonbonded(cr,fr,x,f,md,excl,
266 fr->bBHAM ?
267 ed_lam.grpp.ener[egBHAMSR] :
268 ed_lam.grpp.ener[egLJSR],
269 ed_lam.grpp.ener[egCOULSR],
270 enerd->grpp.ener[egGB], box_size,nrnb,
271 lambda_dum,dvdl_dum,-1,-1,
272 GMX_DONB_FOREIGNLAMBDA);
273 sum_epot(&ir->opts,&ed_lam);
274 enerd->enerpart_lambda[i] += ed_lam.term[F_EPOT];
276 destroy_enerdata(&ed_lam);
278 where();
280 /* If we are doing GB, calculate bonded forces and apply corrections
281 * to the solvation forces */
282 /* is there any free energy contribution here? */
283 if (ir->implicit_solvent) {
284 dvdgb = calc_gb_forces(cr,md,born,top,atype,x,f,fr,idef,ir->gb_algorithm,nrnb,bBornRadii);
285 enerd->term[F_GB12]+=dvdgb;
287 /* Also add the nonbonded GB potential energy (only from one energy group currently) */
288 enerd->term[F_GB12]+=enerd->grpp.ener[egGB][0];
291 #ifdef GMX_MPI
292 if (TAKETIME)
294 t1=MPI_Wtime();
295 fr->t_fnbf += t1-t0;
297 #endif
299 if (fepvals->sc_alpha != 0)
301 enerd->dvdl_nonlin[efptVDW] += dvdl[efptVDW];
303 else
305 enerd->dvdl_lin[efptVDW] += dvdl[efptVDW];
307 Vsr = 0;
308 if (bSepDVDL)
310 for(i=0; i<enerd->grpp.nener; i++)
312 Vsr +=
313 (fr->bBHAM ?
314 enerd->grpp.ener[egBHAMSR][i] :
315 enerd->grpp.ener[egLJSR][i])
316 + enerd->grpp.ener[egCOULSR][i];
318 dvdlsum = dvdl[efptVDW]+dvdl[efptCOUL];
319 PRINT_SEPDVDL("VdW and Coulomb SR particle-p.",Vsr,dvdlsum);
322 debug_gmx();
324 GMX_MPE_LOG(ev_do_fnbf_finish);
326 if (debug)
328 pr_rvecs(debug,0,"fshift after SR",fr->fshift,SHIFTS);
331 /* Shift the coordinates. Must be done before bonded forces and PPPM,
332 * but is also necessary for SHAKE and update, therefore it can NOT
333 * go when no bonded forces have to be evaluated.
336 /* Here sometimes we would not need to shift with NBFonly,
337 * but we do so anyhow for consistency of the returned coordinates.
339 if (graph)
341 shift_self(graph,box,x);
342 if (TRICLINIC(box))
344 inc_nrnb(nrnb,eNR_SHIFTX,2*graph->nnodes);
346 else
348 inc_nrnb(nrnb,eNR_SHIFTX,graph->nnodes);
351 /* Check whether we need to do bondeds or correct for exclusions */
352 if (fr->bMolPBC &&
353 ((flags & GMX_FORCE_BONDED)
354 || EEL_RF(fr->eeltype) || EEL_FULL(fr->eeltype)))
356 /* Since all atoms are in the rectangular or triclinic unit-cell,
357 * only single box vector shifts (2 in x) are required.
359 set_pbc_dd(&pbc,fr->ePBC,cr->dd,TRUE,box);
361 debug_gmx();
363 if (flags & GMX_FORCE_BONDED)
365 GMX_MPE_LOG(ev_calc_bonds_start);
366 calc_bonds(fplog,cr->ms,
367 idef,x,hist,f,fr,&pbc,graph,enerd,nrnb,lambda,md,fcd,
368 DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL, atype, born, &(mtop->cmap_grid),
369 fr->bSepDVDL && do_per_step(step,ir->nstlog),step);
371 /* Check if we have to determine energy differences
372 * at foreign lambda's.
374 if (fepvals->n_lambda > 0 && (flags & GMX_FORCE_DHDL) &&
375 idef->ilsort != ilsortNO_FE)
377 if (idef->ilsort != ilsortFE_SORTED)
379 gmx_incons("The bonded interactions are not sorted for free energy");
381 init_enerdata(mtop->groups.grps[egcENER].nr,fepvals->n_lambda,&ed_lam);
383 for(i=0; i<=enerd->n_lambda; i++)
385 reset_enerdata(&ir->opts,fr,TRUE,&ed_lam,FALSE);
386 for (j=0;j<efptNR;j++) {
387 if (i==0)
389 lambda_dum[j] = lambda[j];
391 else
393 lambda_dum[j] = fepvals->all_lambda[j][i-1];
396 calc_bonds_lambda(fplog,
397 idef,x,fr,&pbc,graph,&ed_lam,nrnb,lambda_dum,md,
398 fcd,DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL);
399 sum_epot(&ir->opts,&ed_lam);
400 enerd->enerpart_lambda[i] += ed_lam.term[F_EPOT];
402 destroy_enerdata(&ed_lam);
404 debug_gmx();
405 GMX_MPE_LOG(ev_calc_bonds_finish);
408 where();
410 *cycles_pme = 0;
411 if (EEL_FULL(fr->eeltype))
413 bSB = (ir->nwall == 2);
414 if (bSB)
416 copy_mat(box,boxs);
417 svmul(ir->wall_ewald_zfac,boxs[ZZ],boxs[ZZ]);
418 box_size[ZZ] *= ir->wall_ewald_zfac;
421 clear_mat(fr->vir_el_recip);
423 if (fr->bEwald)
425 if (fr->n_tpi == 0)
427 dvdl[efptCOUL] = 0;
428 Vcorr = ewald_LRcorrection(fplog,md->start,md->start+md->homenr,
429 cr,fr,
430 md->chargeA,
431 md->nChargePerturbed ? md->chargeB : NULL,
432 excl,x,bSB ? boxs : box,mu_tot,
433 ir->ewald_geometry,
434 ir->epsilon_surface,
435 lambda[efptCOUL],&dvdl[efptCOUL],&vdip,&vcharge);
436 PRINT_SEPDVDL("Ewald excl./charge/dip. corr.",Vcorr,dvdl);
437 enerd->dvdl_lin[efptCOUL] += dvdl[efptCOUL];
439 else
441 if (ir->ewald_geometry != eewg3D || ir->epsilon_surface != 0)
443 gmx_fatal(FARGS,"TPI with PME currently only works in a 3D geometry with tin-foil boundary conditions");
445 /* The TPI molecule does not have exclusions with the rest
446 * of the system and no intra-molecular PME grid contributions
447 * will be calculated in gmx_pme_calc_energy.
449 Vcorr = 0;
452 else
454 Vcorr = shift_LRcorrection(fplog,md->start,md->homenr,cr,fr,
455 md->chargeA,excl,x,TRUE,box,
456 fr->vir_el_recip);
459 dvdl[efptCOUL] = 0;
460 status = 0;
461 switch (fr->eeltype)
463 case eelPPPM:
464 status = gmx_pppm_do(fplog,fr->pmedata,FALSE,x,fr->f_novirsum,
465 md->chargeA,
466 box_size,fr->phi,cr,md->start,md->homenr,
467 nrnb,ir->pme_order,&Vlr);
468 break;
469 case eelPME:
470 case eelPMESWITCH:
471 case eelPMEUSER:
472 if (cr->duty & DUTY_PME)
474 if (fr->n_tpi == 0 || (flags & GMX_FORCE_STATECHANGED))
476 pme_flags = GMX_PME_SPREAD_Q | GMX_PME_SOLVE;
477 if (flags & GMX_FORCE_FORCES)
479 pme_flags |= GMX_PME_CALC_F;
481 wallcycle_start(wcycle,ewcPMEMESH);
482 status = gmx_pme_do(fr->pmedata,
483 md->start,md->homenr - fr->n_tpi,
484 x,fr->f_novirsum,
485 md->chargeA,md->chargeB,
486 bSB ? boxs : box,cr,
487 DOMAINDECOMP(cr) ? dd_pme_maxshift0(cr->dd) : 0,
488 DOMAINDECOMP(cr) ? dd_pme_maxshift1(cr->dd) : 0,
489 nrnb,wcycle,
490 fr->vir_el_recip,fr->ewaldcoeff,
491 &Vlr,lambda[efptCOUL],&dvdl[efptCOUL],
492 pme_flags);
493 *cycles_pme = wallcycle_stop(wcycle,ewcPMEMESH);
495 /* We should try to do as little computation after
496 * this as possible, because parallel PME synchronizes
497 * the nodes, so we want all load imbalance of the rest
498 * of the force calculation to be before the PME call.
499 * DD load balancing is done on the whole time of
500 * the force call (without PME).
503 if (fr->n_tpi > 0)
505 /* Determine the PME grid energy of the test molecule
506 * with the PME grid potential of the other charges.
508 gmx_pme_calc_energy(fr->pmedata,fr->n_tpi,
509 x + md->homenr - fr->n_tpi,
510 md->chargeA + md->homenr - fr->n_tpi,
511 &Vlr);
513 PRINT_SEPDVDL("PME mesh",Vlr,dvdl[efptCOUL]);
515 else
517 /* Energies and virial are obtained later from the PME nodes */
518 /* but values have to be zeroed out here */
519 Vlr=0.0;
521 break;
522 case eelEWALD:
523 Vlr = do_ewald(fplog,FALSE,ir,x,fr->f_novirsum,
524 md->chargeA,md->chargeB,
525 box_size,cr,md->homenr,
526 fr->vir_el_recip,fr->ewaldcoeff,
527 lambda[efptCOUL],&dvdl[efptCOUL],fr->ewald_table);
528 PRINT_SEPDVDL("Ewald long-range",Vlr,dvdl[efptCOUL]);
529 break;
530 default:
531 Vlr = 0;
532 gmx_fatal(FARGS,"No such electrostatics method implemented %s",
533 eel_names[fr->eeltype]);
535 if (status != 0)
537 gmx_fatal(FARGS,"Error %d in long range electrostatics routine %s",
538 status,EELTYPE(fr->eeltype));
540 enerd->dvdl_lin[efptCOUL] += dvdl[efptCOUL];
541 enerd->term[F_COUL_RECIP] = Vlr + Vcorr;
542 if (debug)
544 fprintf(debug,"Vlr = %g, Vcorr = %g, Vlr_corr = %g\n",
545 Vlr,Vcorr,enerd->term[F_COUL_RECIP]);
546 pr_rvecs(debug,0,"vir_el_recip after corr",fr->vir_el_recip,DIM);
547 pr_rvecs(debug,0,"fshift after LR Corrections",fr->fshift,SHIFTS);
550 else
552 if (EEL_RF(fr->eeltype))
554 dvdl[efptCOUL] = 0;
556 if (fr->eeltype != eelRF_NEC)
558 enerd->term[F_RF_EXCL] =
559 RF_excl_correction(fplog,fr,graph,md,excl,x,f,
560 fr->fshift,&pbc,lambda[efptCOUL],&dvdl[efptCOUL]);
563 enerd->dvdl_lin[efptCOUL] += dvdl[efptCOUL];
564 PRINT_SEPDVDL("RF exclusion correction",
565 enerd->term[F_RF_EXCL],dvdl[efptCOUL]);
568 where();
569 debug_gmx();
571 if (debug)
573 print_nrnb(debug,nrnb);
575 debug_gmx();
577 #ifdef GMX_MPI
578 if (TAKETIME)
580 t2=MPI_Wtime();
581 MPI_Barrier(cr->mpi_comm_mygroup);
582 t3=MPI_Wtime();
583 fr->t_wait += t3-t2;
584 if (fr->timesteps == 11)
586 fprintf(stderr,"* PP load balancing info: node %d, step %s, rel wait time=%3.0f%% , load string value: %7.2f\n",
587 cr->nodeid, gmx_step_str(fr->timesteps,buf),
588 100*fr->t_wait/(fr->t_wait+fr->t_fnbf),
589 (fr->t_fnbf+fr->t_wait)/fr->t_fnbf);
591 fr->timesteps++;
593 #endif
595 if (debug)
597 pr_rvecs(debug,0,"fshift after bondeds",fr->fshift,SHIFTS);
600 GMX_MPE_LOG(ev_force_finish);
604 void init_enerdata(int ngener,int n_lambda,gmx_enerdata_t *enerd)
606 int i,n2;
608 for(i=0; i<F_NRE; i++)
610 enerd->term[i] = 0;
613 n2=ngener*ngener;
614 if (debug)
616 fprintf(debug,"Creating %d sized group matrix for energies\n",n2);
618 enerd->grpp.nener = n2;
619 for(i=0; (i<egNR); i++)
621 snew(enerd->grpp.ener[i],n2);
624 if (n_lambda)
626 enerd->n_lambda = 1 + n_lambda;
627 snew(enerd->enerpart_lambda,enerd->n_lambda);
629 else
631 enerd->n_lambda = 0;
635 void destroy_enerdata(gmx_enerdata_t *enerd)
637 int i;
639 for(i=0; (i<egNR); i++)
641 sfree(enerd->grpp.ener[i]);
644 if (enerd->n_lambda)
646 sfree(enerd->enerpart_lambda);
650 static real sum_v(int n,real v[])
652 real t;
653 int i;
655 t = 0.0;
656 for(i=0; (i<n); i++)
657 t = t + v[i];
659 return t;
662 void sum_epot(t_grpopts *opts,gmx_enerdata_t *enerd)
664 gmx_grppairener_t *grpp;
665 real *epot;
666 int i;
668 grpp = &enerd->grpp;
669 epot = enerd->term;
671 /* Accumulate energies */
672 epot[F_COUL_SR] = sum_v(grpp->nener,grpp->ener[egCOULSR]);
673 epot[F_LJ] = sum_v(grpp->nener,grpp->ener[egLJSR]);
674 epot[F_LJ14] = sum_v(grpp->nener,grpp->ener[egLJ14]);
675 epot[F_COUL14] = sum_v(grpp->nener,grpp->ener[egCOUL14]);
676 epot[F_COUL_LR] = sum_v(grpp->nener,grpp->ener[egCOULLR]);
677 epot[F_LJ_LR] = sum_v(grpp->nener,grpp->ener[egLJLR]);
678 /* lattice part of LR doesnt belong to any group
679 * and has been added earlier
681 epot[F_BHAM] = sum_v(grpp->nener,grpp->ener[egBHAMSR]);
682 epot[F_BHAM_LR] = sum_v(grpp->nener,grpp->ener[egBHAMLR]);
684 epot[F_EPOT] = 0;
685 for(i=0; (i<F_EPOT); i++)
686 if (i != F_DISRESVIOL && i != F_ORIRESDEV && i != F_DIHRESVIOL)
687 epot[F_EPOT] += epot[i];
690 void sum_dhdl(gmx_enerdata_t *enerd, double *lambda, t_lambda *fepvals)
692 int i,j,index;
693 double dlam;
695 enerd->term[F_DVDL_REMAIN] = 0.0;
696 for (i=0;i<efptNR;i++)
698 if (fepvals->separate_dvdl[i])
700 /* could this be done more readably/compactly? */
701 switch (i) {
702 case (efptCOUL):
703 index = F_DVDL_COUL;
704 break;
705 case (efptVDW):
706 index = F_DVDL_VDW;
707 break;
708 case (efptBONDED):
709 index = F_DVDL_BONDED;
710 break;
711 case (efptRESTRAINT):
712 index = F_DVDL_RESTRAINT;
713 break;
714 case (efptMASS):
715 index = F_DKDL;
716 break;
717 default:
718 index = F_DVDL_REMAIN;
719 break;
721 enerd->term[index] = enerd->dvdl_lin[i] + enerd->dvdl_nonlin[i];
722 if (debug)
724 fprintf(debug,"dvdl-%s[%2d]: %f: non-linear %f + linear %f\n",
725 i,efpt_names[i],enerd->term[index],enerd->dvdl_nonlin[i],enerd->dvdl_lin[i]);
728 else
730 enerd->term[F_DVDL_REMAIN] += enerd->dvdl_lin[i] + enerd->dvdl_nonlin[i];
731 if (debug)
733 fprintf(debug,"dvd-%sl[%2d]: %f: non-linear %f + linear %f\n",
734 i,efpt_names[0],enerd->term[F_DVDL_REMAIN],enerd->dvdl_nonlin[i],enerd->dvdl_lin[i]);
739 /* Notes on the foreign lambda free energy difference evaluation:
740 * Adding the potential and ekin terms that depend linearly on lambda
741 * as delta lam * dvdl to the energy differences is exact.
742 * For the constraint dvdl this is not exact, but we have no other option
743 * (try to remedy this - MRS)
744 * For the non-bonded LR term we assume that the soft-core (if present)
745 * no longer affects the energy beyond the short-range cut-off,
746 * which is a very good approximation (except for exotic settings).
747 * (investigate how to overcome this - MRS)
750 for(i=1; i<=enerd->n_lambda; i++)
753 /* we don't need to worry about dvdl contributions to the currenta lambda, because
754 it's automatically zero */
756 /* first kinetic energy term */
757 dlam = (fepvals->all_lambda[efptMASS][i-1] - lambda[efptMASS]);
759 /* make sure constraint terms are added on correctly here or elsewhere! MRS */
760 enerd->enerpart_lambda[i] += enerd->term[F_DKDL]*dlam;
762 for (j=0;j<efptNR;j++)
764 dlam = (fepvals->all_lambda[j][i-1]-lambda[j]);
765 enerd->enerpart_lambda[i] += dlam*enerd->dvdl_lin[j];
766 if (debug)
768 fprintf(debug,"enerdiff lam %g: (%15s), non-linear %f linear %f*%f\n",
769 fepvals->all_lambda[j][i-1],efpt_names[j],
770 enerd->enerpart_lambda[i] - enerd->enerpart_lambda[0],
771 dlam,enerd->dvdl_lin[j]);
777 void reset_enerdata(t_grpopts *opts,
778 t_forcerec *fr,bool bNS,
779 gmx_enerdata_t *enerd,
780 bool bMaster)
782 bool bKeepLR;
783 int i,j;
785 /* First reset all energy components, except for the long range terms
786 * on the master at non neighbor search steps, since the long range
787 * terms have already been summed at the last neighbor search step.
789 bKeepLR = (fr->bTwinRange && !bNS);
790 for(i=0; (i<egNR); i++) {
791 if (!(bKeepLR && bMaster && (i == egCOULLR || i == egLJLR))) {
792 for(j=0; (j<enerd->grpp.nener); j++)
793 enerd->grpp.ener[i][j] = 0.0;
796 for (i=0;i<efptNR;i++) {
797 enerd->dvdl_lin[i] = 0.0;
798 enerd->dvdl_nonlin[i] = 0.0;
800 /* Normal potential energy components */
801 for(i=0; (i<=F_EPOT); i++) {
802 enerd->term[i] = 0.0;
804 /* Initialize the dvdl term with the long range contribution */
805 enerd->term[F_DVDL_COUL] = 0.0;
806 enerd->term[F_DVDL_VDW] = 0.0;
807 enerd->term[F_DVDL_BONDED] = 0.0;
808 enerd->term[F_DVDL_RESTRAINT] = 0.0;
809 enerd->term[F_DVDL_REMAIN] = 0.0;
810 enerd->term[F_DKDL] = 0.0;
811 if (enerd->n_lambda > 0)
813 for(i=0; i<enerd->n_lambda; i++)
815 enerd->enerpart_lambda[i] = 0.0;