2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
39 #include "gromacs/legacyheaders/force.h"
47 #include "gromacs/legacyheaders/coulomb.h"
48 #include "gromacs/legacyheaders/domdec.h"
49 #include "gromacs/legacyheaders/gmx_omp_nthreads.h"
50 #include "gromacs/legacyheaders/macros.h"
51 #include "gromacs/legacyheaders/mdrun.h"
52 #include "gromacs/legacyheaders/names.h"
53 #include "gromacs/legacyheaders/network.h"
54 #include "gromacs/legacyheaders/nonbonded.h"
55 #include "gromacs/legacyheaders/nrnb.h"
56 #include "gromacs/legacyheaders/ns.h"
57 #include "gromacs/legacyheaders/pme.h"
58 #include "gromacs/legacyheaders/qmmm.h"
59 #include "gromacs/legacyheaders/txtdump.h"
60 #include "gromacs/legacyheaders/typedefs.h"
61 #include "gromacs/legacyheaders/types/commrec.h"
62 #include "gromacs/listed-forces/bonded.h"
63 #include "gromacs/math/vec.h"
64 #include "gromacs/pbcutil/ishift.h"
65 #include "gromacs/pbcutil/mshift.h"
66 #include "gromacs/pbcutil/pbc.h"
67 #include "gromacs/timing/wallcycle.h"
68 #include "gromacs/utility/fatalerror.h"
69 #include "gromacs/utility/smalloc.h"
80 gmx_bool bDoLongRangeNS)
86 if (!fr->ns.nblist_initialized)
88 init_neighbor_list(fp, fr, md->homenr);
96 nsearch = search_neighbours(fp, fr, box, top, groups, cr, nrnb, md,
97 bFillGrid, bDoLongRangeNS);
100 fprintf(debug, "nsearch = %d\n", nsearch);
103 /* Check whether we have to do dynamic load balancing */
104 /*if ((nsb->nstDlb > 0) && (mod(step,nsb->nstDlb) == 0))
105 count_nb(cr,nsb,&(top->blocks[ebCGS]),nns,fr->nlr,
106 &(top->idef),opts->ngener);
108 if (fr->ns.dump_nl > 0)
110 dump_nblist(fp, cr, fr, fr->ns.dump_nl);
114 static void reduce_thread_forces(int n, rvec *f,
115 tensor vir_q, tensor vir_lj,
116 real *Vcorr_q, real *Vcorr_lj,
117 real *dvdl_q, real *dvdl_lj,
118 int nthreads, f_thread_t *f_t)
122 /* This reduction can run over any number of threads */
123 #pragma omp parallel for num_threads(gmx_omp_nthreads_get(emntBonded)) private(t) schedule(static)
124 for (i = 0; i < n; i++)
126 for (t = 1; t < nthreads; t++)
128 rvec_inc(f[i], f_t[t].f[i]);
131 for (t = 1; t < nthreads; t++)
133 *Vcorr_q += f_t[t].Vcorr_q;
134 *Vcorr_lj += f_t[t].Vcorr_lj;
135 *dvdl_q += f_t[t].dvdl[efptCOUL];
136 *dvdl_lj += f_t[t].dvdl[efptVDW];
137 m_add(vir_q, f_t[t].vir_q, vir_q);
138 m_add(vir_lj, f_t[t].vir_lj, vir_lj);
142 void do_force_lowlevel(t_forcerec *fr, t_inputrec *ir,
143 t_idef *idef, t_commrec *cr,
144 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
146 rvec x[], history_t *hist,
149 gmx_enerdata_t *enerd,
165 gmx_bool bDoEpot, bSB;
171 double clam_i, vlam_i;
172 real dvdl_dum[efptNR], dvdl_nb[efptNR], lam_i[efptNR];
173 real dvdl_q, dvdl_lj;
176 double t0 = 0.0, t1, t2, t3; /* time measurement for coarse load balancing */
179 set_pbc(&pbc, fr->ePBC, box);
181 /* reset free energy components */
182 for (i = 0; i < efptNR; i++)
189 for (i = 0; (i < DIM); i++)
191 box_size[i] = box[i][i];
196 /* do QMMM first if requested */
199 enerd->term[F_EQM] = calculate_QMMM(cr, x, f, fr);
202 /* Call the short range functions all in one go. */
205 /*#define TAKETIME ((cr->npmenodes) && (fr->timesteps < 12))*/
206 #define TAKETIME FALSE
209 MPI_Barrier(cr->mpi_comm_mygroup);
216 /* foreign lambda component for walls */
217 real dvdl_walls = do_walls(ir, fr, box, md, x, f, lambda[efptVDW],
218 enerd->grpp.ener[egLJSR], nrnb);
219 enerd->dvdl_lin[efptVDW] += dvdl_walls;
222 /* If doing GB, reset dvda and calculate the Born radii */
223 if (ir->implicit_solvent)
225 wallcycle_sub_start(wcycle, ewcsNONBONDED);
227 for (i = 0; i < born->nr; i++)
234 calc_gb_rad(cr, fr, ir, top, x, &(fr->gblist), born, md, nrnb);
237 wallcycle_sub_stop(wcycle, ewcsNONBONDED);
241 /* We only do non-bonded calculation with group scheme here, the verlet
242 * calls are done from do_force_cutsVERLET(). */
243 if (fr->cutoff_scheme == ecutsGROUP && (flags & GMX_FORCE_NONBONDED))
246 /* Add short-range interactions */
247 donb_flags |= GMX_NONBONDED_DO_SR;
249 /* Currently all group scheme kernels always calculate (shift-)forces */
250 if (flags & GMX_FORCE_FORCES)
252 donb_flags |= GMX_NONBONDED_DO_FORCE;
254 if (flags & GMX_FORCE_VIRIAL)
256 donb_flags |= GMX_NONBONDED_DO_SHIFTFORCE;
258 if (flags & GMX_FORCE_ENERGY)
260 donb_flags |= GMX_NONBONDED_DO_POTENTIAL;
262 if (flags & GMX_FORCE_DO_LR)
264 donb_flags |= GMX_NONBONDED_DO_LR;
267 wallcycle_sub_start(wcycle, ewcsNONBONDED);
268 do_nonbonded(fr, x, f, f_longrange, md, excl,
270 lambda, dvdl_nb, -1, -1, donb_flags);
272 /* If we do foreign lambda and we have soft-core interactions
273 * we have to recalculate the (non-linear) energies contributions.
275 if (fepvals->n_lambda > 0 && (flags & GMX_FORCE_DHDL) && fepvals->sc_alpha != 0)
277 for (i = 0; i < enerd->n_lambda; i++)
279 for (j = 0; j < efptNR; j++)
281 lam_i[j] = (i == 0 ? lambda[j] : fepvals->all_lambda[j][i-1]);
283 reset_foreign_enerdata(enerd);
284 do_nonbonded(fr, x, f, f_longrange, md, excl,
285 &(enerd->foreign_grpp), nrnb,
286 lam_i, dvdl_dum, -1, -1,
287 (donb_flags & ~GMX_NONBONDED_DO_FORCE) | GMX_NONBONDED_DO_FOREIGNLAMBDA);
288 sum_epot(&(enerd->foreign_grpp), enerd->foreign_term);
289 enerd->enerpart_lambda[i] += enerd->foreign_term[F_EPOT];
292 wallcycle_sub_stop(wcycle, ewcsNONBONDED);
296 /* If we are doing GB, calculate bonded forces and apply corrections
297 * to the solvation forces */
298 /* MRS: Eventually, many need to include free energy contribution here! */
299 if (ir->implicit_solvent)
301 wallcycle_sub_start(wcycle, ewcsLISTED);
302 calc_gb_forces(cr, md, born, top, x, f, fr, idef,
303 ir->gb_algorithm, ir->sa_algorithm, nrnb, &pbc, graph, enerd);
304 wallcycle_sub_stop(wcycle, ewcsLISTED);
315 if (fepvals->sc_alpha != 0)
317 enerd->dvdl_nonlin[efptVDW] += dvdl_nb[efptVDW];
321 enerd->dvdl_lin[efptVDW] += dvdl_nb[efptVDW];
324 if (fepvals->sc_alpha != 0)
326 /* even though coulomb part is linear, we already added it, beacuse we
327 need to go through the vdw calculation anyway */
329 enerd->dvdl_nonlin[efptCOUL] += dvdl_nb[efptCOUL];
333 enerd->dvdl_lin[efptCOUL] += dvdl_nb[efptCOUL];
341 pr_rvecs(debug, 0, "fshift after SR", fr->fshift, SHIFTS);
344 /* Shift the coordinates. Must be done before listed forces and PPPM,
345 * but is also necessary for SHAKE and update, therefore it can NOT
346 * go when no listed forces have to be evaluated.
349 /* Here sometimes we would not need to shift with NBFonly,
350 * but we do so anyhow for consistency of the returned coordinates.
354 shift_self(graph, box, x);
357 inc_nrnb(nrnb, eNR_SHIFTX, 2*graph->nnodes);
361 inc_nrnb(nrnb, eNR_SHIFTX, graph->nnodes);
364 /* Check whether we need to do listed interactions or correct for exclusions */
366 ((flags & GMX_FORCE_LISTED)
367 || EEL_RF(fr->eeltype) || EEL_FULL(fr->eeltype) || EVDW_PME(fr->vdwtype)))
369 /* Since all atoms are in the rectangular or triclinic unit-cell,
370 * only single box vector shifts (2 in x) are required.
372 set_pbc_dd(&pbc, fr->ePBC, cr->dd, TRUE, box);
376 if (flags & GMX_FORCE_LISTED)
378 wallcycle_sub_start(wcycle, ewcsLISTED);
380 idef, (const rvec *) x, hist, f, fr, &pbc, graph, enerd, nrnb, lambda, md, fcd,
381 DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL,
384 /* Check if we have to determine energy differences
385 * at foreign lambda's.
387 if (fepvals->n_lambda > 0 && (flags & GMX_FORCE_DHDL) &&
388 idef->ilsort != ilsortNO_FE)
390 if (idef->ilsort != ilsortFE_SORTED)
392 gmx_incons("The bonded interactions are not sorted for free energy");
394 for (i = 0; i < enerd->n_lambda; i++)
396 reset_foreign_enerdata(enerd);
397 for (j = 0; j < efptNR; j++)
399 lam_i[j] = (i == 0 ? lambda[j] : fepvals->all_lambda[j][i-1]);
401 calc_bonds_lambda(idef, (const rvec *) x, fr, &pbc, graph, &(enerd->foreign_grpp), enerd->foreign_term, nrnb, lam_i, md,
402 fcd, DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL);
403 sum_epot(&(enerd->foreign_grpp), enerd->foreign_term);
404 enerd->enerpart_lambda[i] += enerd->foreign_term[F_EPOT];
409 wallcycle_sub_stop(wcycle, ewcsLISTED);
415 clear_mat(fr->vir_el_recip);
416 clear_mat(fr->vir_lj_recip);
418 /* Do long-range electrostatics and/or LJ-PME, including related short-range
421 if (EEL_FULL(fr->eeltype) || EVDW_PME(fr->vdwtype))
423 real Vlr = 0, Vcorr = 0;
424 real dvdl_long_range = 0;
426 real Vlr_q = 0, Vlr_lj = 0, Vcorr_q = 0, Vcorr_lj = 0;
427 real dvdl_long_range_q = 0, dvdl_long_range_lj = 0;
429 bSB = (ir->nwall == 2);
433 svmul(ir->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
434 box_size[ZZ] *= ir->wall_ewald_zfac;
437 if (EEL_PME_EWALD(fr->eeltype) || EVDW_PME(fr->vdwtype))
439 real dvdl_long_range_correction_q = 0;
440 real dvdl_long_range_correction_lj = 0;
441 /* With the Verlet scheme exclusion forces are calculated
442 * in the non-bonded kernel.
444 /* The TPI molecule does not have exclusions with the rest
445 * of the system and no intra-molecular PME grid
446 * contributions will be calculated in
447 * gmx_pme_calc_energy.
449 if ((ir->cutoff_scheme == ecutsGROUP && fr->n_tpi == 0) ||
450 ir->ewald_geometry != eewg3D ||
451 ir->epsilon_surface != 0)
455 wallcycle_sub_start(wcycle, ewcsEWALD_CORRECTION);
459 gmx_fatal(FARGS, "TPI with PME currently only works in a 3D geometry with tin-foil boundary conditions");
462 nthreads = gmx_omp_nthreads_get(emntBonded);
463 #pragma omp parallel for num_threads(nthreads) schedule(static)
464 for (t = 0; t < nthreads; t++)
468 tensor *vir_q, *vir_lj;
469 real *Vcorrt_q, *Vcorrt_lj, *dvdlt_q, *dvdlt_lj;
472 fnv = fr->f_novirsum;
473 vir_q = &fr->vir_el_recip;
474 vir_lj = &fr->vir_lj_recip;
476 Vcorrt_lj = &Vcorr_lj;
477 dvdlt_q = &dvdl_long_range_correction_q;
478 dvdlt_lj = &dvdl_long_range_correction_lj;
483 vir_q = &fr->f_t[t].vir_q;
484 vir_lj = &fr->f_t[t].vir_lj;
485 Vcorrt_q = &fr->f_t[t].Vcorr_q;
486 Vcorrt_lj = &fr->f_t[t].Vcorr_lj;
487 dvdlt_q = &fr->f_t[t].dvdl[efptCOUL];
488 dvdlt_lj = &fr->f_t[t].dvdl[efptVDW];
489 for (i = 0; i < fr->natoms_force; i++)
499 ewald_LRcorrection(fr->excl_load[t], fr->excl_load[t+1],
502 md->nChargePerturbed ? md->chargeB : NULL,
504 md->nTypePerturbed ? md->sqrt_c6B : NULL,
506 md->nTypePerturbed ? md->sigmaB : NULL,
508 md->nTypePerturbed ? md->sigma3B : NULL,
509 ir->cutoff_scheme != ecutsVERLET,
510 excl, x, bSB ? boxs : box, mu_tot,
513 fnv, *vir_q, *vir_lj,
515 lambda[efptCOUL], lambda[efptVDW],
520 reduce_thread_forces(fr->natoms_force, fr->f_novirsum,
521 fr->vir_el_recip, fr->vir_lj_recip,
523 &dvdl_long_range_correction_q,
524 &dvdl_long_range_correction_lj,
527 wallcycle_sub_stop(wcycle, ewcsEWALD_CORRECTION);
530 if (EEL_PME_EWALD(fr->eeltype) && fr->n_tpi == 0)
532 Vcorr_q += ewald_charge_correction(cr, fr, lambda[efptCOUL], box,
533 &dvdl_long_range_correction_q,
537 enerd->dvdl_lin[efptCOUL] += dvdl_long_range_correction_q;
538 enerd->dvdl_lin[efptVDW] += dvdl_long_range_correction_lj;
540 if ((EEL_PME(fr->eeltype) || EVDW_PME(fr->vdwtype)) && (cr->duty & DUTY_PME))
542 /* Do reciprocal PME for Coulomb and/or LJ. */
543 assert(fr->n_tpi >= 0);
544 if (fr->n_tpi == 0 || (flags & GMX_FORCE_STATECHANGED))
546 pme_flags = GMX_PME_SPREAD | GMX_PME_SOLVE;
547 if (EEL_PME(fr->eeltype))
549 pme_flags |= GMX_PME_DO_COULOMB;
551 if (EVDW_PME(fr->vdwtype))
553 pme_flags |= GMX_PME_DO_LJ;
555 if (flags & GMX_FORCE_FORCES)
557 pme_flags |= GMX_PME_CALC_F;
559 if (flags & GMX_FORCE_VIRIAL)
561 pme_flags |= GMX_PME_CALC_ENER_VIR;
565 /* We don't calculate f, but we do want the potential */
566 pme_flags |= GMX_PME_CALC_POT;
568 wallcycle_start(wcycle, ewcPMEMESH);
569 status = gmx_pme_do(fr->pmedata,
570 0, md->homenr - fr->n_tpi,
572 md->chargeA, md->chargeB,
573 md->sqrt_c6A, md->sqrt_c6B,
574 md->sigmaA, md->sigmaB,
575 bSB ? boxs : box, cr,
576 DOMAINDECOMP(cr) ? dd_pme_maxshift_x(cr->dd) : 0,
577 DOMAINDECOMP(cr) ? dd_pme_maxshift_y(cr->dd) : 0,
579 fr->vir_el_recip, fr->ewaldcoeff_q,
580 fr->vir_lj_recip, fr->ewaldcoeff_lj,
582 lambda[efptCOUL], lambda[efptVDW],
583 &dvdl_long_range_q, &dvdl_long_range_lj, pme_flags);
584 *cycles_pme = wallcycle_stop(wcycle, ewcPMEMESH);
587 gmx_fatal(FARGS, "Error %d in reciprocal PME routine", status);
589 /* We should try to do as little computation after
590 * this as possible, because parallel PME synchronizes
591 * the nodes, so we want all load imbalance of the
592 * rest of the force calculation to be before the PME
593 * call. DD load balancing is done on the whole time
594 * of the force call (without PME).
599 if (EVDW_PME(ir->vdwtype))
602 gmx_fatal(FARGS, "Test particle insertion not implemented with LJ-PME");
604 /* Determine the PME grid energy of the test molecule
605 * with the PME grid potential of the other charges.
607 gmx_pme_calc_energy(fr->pmedata, fr->n_tpi,
608 x + md->homenr - fr->n_tpi,
609 md->chargeA + md->homenr - fr->n_tpi,
615 if (!EEL_PME(fr->eeltype) && EEL_PME_EWALD(fr->eeltype))
617 Vlr_q = do_ewald(ir, x, fr->f_novirsum,
618 md->chargeA, md->chargeB,
619 box_size, cr, md->homenr,
620 fr->vir_el_recip, fr->ewaldcoeff_q,
621 lambda[efptCOUL], &dvdl_long_range_q, fr->ewald_table);
624 /* Note that with separate PME nodes we get the real energies later */
625 enerd->dvdl_lin[efptCOUL] += dvdl_long_range_q;
626 enerd->dvdl_lin[efptVDW] += dvdl_long_range_lj;
627 enerd->term[F_COUL_RECIP] = Vlr_q + Vcorr_q;
628 enerd->term[F_LJ_RECIP] = Vlr_lj + Vcorr_lj;
631 fprintf(debug, "Vlr_q = %g, Vcorr_q = %g, Vlr_corr_q = %g\n",
632 Vlr_q, Vcorr_q, enerd->term[F_COUL_RECIP]);
633 pr_rvecs(debug, 0, "vir_el_recip after corr", fr->vir_el_recip, DIM);
634 pr_rvecs(debug, 0, "fshift after LR Corrections", fr->fshift, SHIFTS);
635 fprintf(debug, "Vlr_lj: %g, Vcorr_lj = %g, Vlr_corr_lj = %g\n",
636 Vlr_lj, Vcorr_lj, enerd->term[F_LJ_RECIP]);
637 pr_rvecs(debug, 0, "vir_lj_recip after corr", fr->vir_lj_recip, DIM);
642 /* Is there a reaction-field exclusion correction needed? */
643 if (EEL_RF(fr->eeltype) && eelRF_NEC != fr->eeltype)
645 /* With the Verlet scheme, exclusion forces are calculated
646 * in the non-bonded kernel.
648 if (ir->cutoff_scheme != ecutsVERLET)
650 real dvdl_rf_excl = 0;
651 enerd->term[F_RF_EXCL] =
652 RF_excl_correction(fr, graph, md, excl, x, f,
653 fr->fshift, &pbc, lambda[efptCOUL], &dvdl_rf_excl);
655 enerd->dvdl_lin[efptCOUL] += dvdl_rf_excl;
664 print_nrnb(debug, nrnb);
672 MPI_Barrier(cr->mpi_comm_mygroup);
675 if (fr->timesteps == 11)
677 fprintf(stderr, "* PP load balancing info: rank %d, step %s, rel wait time=%3.0f%% , load string value: %7.2f\n",
678 cr->nodeid, gmx_step_str(fr->timesteps, buf),
679 100*fr->t_wait/(fr->t_wait+fr->t_fnbf),
680 (fr->t_fnbf+fr->t_wait)/fr->t_fnbf);
688 pr_rvecs(debug, 0, "fshift after bondeds", fr->fshift, SHIFTS);
693 void init_enerdata(int ngener, int n_lambda, gmx_enerdata_t *enerd)
697 for (i = 0; i < F_NRE; i++)
700 enerd->foreign_term[i] = 0;
704 for (i = 0; i < efptNR; i++)
706 enerd->dvdl_lin[i] = 0;
707 enerd->dvdl_nonlin[i] = 0;
713 fprintf(debug, "Creating %d sized group matrix for energies\n", n2);
715 enerd->grpp.nener = n2;
716 enerd->foreign_grpp.nener = n2;
717 for (i = 0; (i < egNR); i++)
719 snew(enerd->grpp.ener[i], n2);
720 snew(enerd->foreign_grpp.ener[i], n2);
725 enerd->n_lambda = 1 + n_lambda;
726 snew(enerd->enerpart_lambda, enerd->n_lambda);
734 void destroy_enerdata(gmx_enerdata_t *enerd)
738 for (i = 0; (i < egNR); i++)
740 sfree(enerd->grpp.ener[i]);
743 for (i = 0; (i < egNR); i++)
745 sfree(enerd->foreign_grpp.ener[i]);
750 sfree(enerd->enerpart_lambda);
754 static real sum_v(int n, real v[])
760 for (i = 0; (i < n); i++)
768 void sum_epot(gmx_grppairener_t *grpp, real *epot)
772 /* Accumulate energies */
773 epot[F_COUL_SR] = sum_v(grpp->nener, grpp->ener[egCOULSR]);
774 epot[F_LJ] = sum_v(grpp->nener, grpp->ener[egLJSR]);
775 epot[F_LJ14] = sum_v(grpp->nener, grpp->ener[egLJ14]);
776 epot[F_COUL14] = sum_v(grpp->nener, grpp->ener[egCOUL14]);
777 epot[F_COUL_LR] = sum_v(grpp->nener, grpp->ener[egCOULLR]);
778 epot[F_LJ_LR] = sum_v(grpp->nener, grpp->ener[egLJLR]);
779 /* We have already added 1-2,1-3, and 1-4 terms to F_GBPOL */
780 epot[F_GBPOL] += sum_v(grpp->nener, grpp->ener[egGB]);
782 /* lattice part of LR doesnt belong to any group
783 * and has been added earlier
785 epot[F_BHAM] = sum_v(grpp->nener, grpp->ener[egBHAMSR]);
786 epot[F_BHAM_LR] = sum_v(grpp->nener, grpp->ener[egBHAMLR]);
789 for (i = 0; (i < F_EPOT); i++)
791 if (i != F_DISRESVIOL && i != F_ORIRESDEV)
793 epot[F_EPOT] += epot[i];
798 void sum_dhdl(gmx_enerdata_t *enerd, real *lambda, t_lambda *fepvals)
803 enerd->dvdl_lin[efptVDW] += enerd->term[F_DVDL_VDW]; /* include dispersion correction */
804 enerd->term[F_DVDL] = 0.0;
805 for (i = 0; i < efptNR; i++)
807 if (fepvals->separate_dvdl[i])
809 /* could this be done more readably/compactly? */
822 index = F_DVDL_BONDED;
824 case (efptRESTRAINT):
825 index = F_DVDL_RESTRAINT;
831 enerd->term[index] = enerd->dvdl_lin[i] + enerd->dvdl_nonlin[i];
834 fprintf(debug, "dvdl-%s[%2d]: %f: non-linear %f + linear %f\n",
835 efpt_names[i], i, enerd->term[index], enerd->dvdl_nonlin[i], enerd->dvdl_lin[i]);
840 enerd->term[F_DVDL] += enerd->dvdl_lin[i] + enerd->dvdl_nonlin[i];
843 fprintf(debug, "dvd-%sl[%2d]: %f: non-linear %f + linear %f\n",
844 efpt_names[0], i, enerd->term[F_DVDL], enerd->dvdl_nonlin[i], enerd->dvdl_lin[i]);
849 /* Notes on the foreign lambda free energy difference evaluation:
850 * Adding the potential and ekin terms that depend linearly on lambda
851 * as delta lam * dvdl to the energy differences is exact.
852 * For the constraints this is not exact, but we have no other option
853 * without literally changing the lengths and reevaluating the energies at each step.
854 * (try to remedy this post 4.6 - MRS)
855 * For the non-bonded LR term we assume that the soft-core (if present)
856 * no longer affects the energy beyond the short-range cut-off,
857 * which is a very good approximation (except for exotic settings).
858 * (investigate how to overcome this post 4.6 - MRS)
860 if (fepvals->separate_dvdl[efptBONDED])
862 enerd->term[F_DVDL_BONDED] += enerd->term[F_DVDL_CONSTR];
866 enerd->term[F_DVDL] += enerd->term[F_DVDL_CONSTR];
868 enerd->term[F_DVDL_CONSTR] = 0;
870 for (i = 0; i < fepvals->n_lambda; i++)
872 /* note we are iterating over fepvals here!
873 For the current lam, dlam = 0 automatically,
874 so we don't need to add anything to the
875 enerd->enerpart_lambda[0] */
877 /* we don't need to worry about dvdl_lin contributions to dE at
878 current lambda, because the contributions to the current
879 lambda are automatically zeroed */
881 for (j = 0; j < efptNR; j++)
883 /* Note that this loop is over all dhdl components, not just the separated ones */
884 dlam = (fepvals->all_lambda[j][i]-lambda[j]);
885 enerd->enerpart_lambda[i+1] += dlam*enerd->dvdl_lin[j];
888 fprintf(debug, "enerdiff lam %g: (%15s), non-linear %f linear %f*%f\n",
889 fepvals->all_lambda[j][i], efpt_names[j],
890 (enerd->enerpart_lambda[i+1] - enerd->enerpart_lambda[0]),
891 dlam, enerd->dvdl_lin[j]);
898 void reset_foreign_enerdata(gmx_enerdata_t *enerd)
902 /* First reset all foreign energy components. Foreign energies always called on
903 neighbor search steps */
904 for (i = 0; (i < egNR); i++)
906 for (j = 0; (j < enerd->grpp.nener); j++)
908 enerd->foreign_grpp.ener[i][j] = 0.0;
912 /* potential energy components */
913 for (i = 0; (i <= F_EPOT); i++)
915 enerd->foreign_term[i] = 0.0;
919 void reset_enerdata(t_forcerec *fr, gmx_bool bNS,
920 gmx_enerdata_t *enerd,
926 /* First reset all energy components, except for the long range terms
927 * on the master at non neighbor search steps, since the long range
928 * terms have already been summed at the last neighbor search step.
930 bKeepLR = (fr->bTwinRange && !bNS);
931 for (i = 0; (i < egNR); i++)
933 if (!(bKeepLR && bMaster && (i == egCOULLR || i == egLJLR)))
935 for (j = 0; (j < enerd->grpp.nener); j++)
937 enerd->grpp.ener[i][j] = 0.0;
941 for (i = 0; i < efptNR; i++)
943 enerd->dvdl_lin[i] = 0.0;
944 enerd->dvdl_nonlin[i] = 0.0;
947 /* Normal potential energy components */
948 for (i = 0; (i <= F_EPOT); i++)
950 enerd->term[i] = 0.0;
952 /* Initialize the dVdlambda term with the long range contribution */
953 /* Initialize the dvdl term with the long range contribution */
954 enerd->term[F_DVDL] = 0.0;
955 enerd->term[F_DVDL_COUL] = 0.0;
956 enerd->term[F_DVDL_VDW] = 0.0;
957 enerd->term[F_DVDL_BONDED] = 0.0;
958 enerd->term[F_DVDL_RESTRAINT] = 0.0;
959 enerd->term[F_DKDL] = 0.0;
960 if (enerd->n_lambda > 0)
962 for (i = 0; i < enerd->n_lambda; i++)
964 enerd->enerpart_lambda[i] = 0.0;
967 /* reset foreign energy data - separate function since we also call it elsewhere */
968 reset_foreign_enerdata(enerd);