Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / mdlib / force.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 #include "gmxpre.h"
38
39 #include "config.h"
40
41 #include <assert.h>
42 #include <math.h>
43 #include <string.h>
44
45 #include "gromacs/legacyheaders/typedefs.h"
46 #include "gromacs/legacyheaders/macros.h"
47 #include "gromacs/legacyheaders/force.h"
48 #include "gromacs/legacyheaders/nonbonded.h"
49 #include "gromacs/legacyheaders/names.h"
50 #include "gromacs/legacyheaders/network.h"
51 #include "gromacs/legacyheaders/ns.h"
52 #include "gromacs/legacyheaders/nrnb.h"
53 #include "gromacs/legacyheaders/bondf.h"
54 #include "gromacs/legacyheaders/txtdump.h"
55 #include "gromacs/legacyheaders/coulomb.h"
56 #include "gromacs/legacyheaders/pme.h"
57 #include "gromacs/legacyheaders/mdrun.h"
58 #include "gromacs/legacyheaders/domdec.h"
59 #include "gromacs/legacyheaders/qmmm.h"
60 #include "gromacs/legacyheaders/gmx_omp_nthreads.h"
61
62 #include "gromacs/legacyheaders/types/commrec.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"
70
71 void ns(FILE              *fp,
72         t_forcerec        *fr,
73         matrix             box,
74         gmx_groups_t      *groups,
75         gmx_localtop_t    *top,
76         t_mdatoms         *md,
77         t_commrec         *cr,
78         t_nrnb            *nrnb,
79         gmx_bool           bFillGrid,
80         gmx_bool           bDoLongRangeNS)
81 {
82     char   *ptr;
83     int     nsearch;
84
85
86     if (!fr->ns.nblist_initialized)
87     {
88         init_neighbor_list(fp, fr, md->homenr);
89     }
90
91     if (fr->bTwinRange)
92     {
93         fr->nlr = 0;
94     }
95
96     nsearch = search_neighbours(fp, fr, box, top, groups, cr, nrnb, md,
97                                 bFillGrid, bDoLongRangeNS);
98     if (debug)
99     {
100         fprintf(debug, "nsearch = %d\n", nsearch);
101     }
102
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);
107      */
108     if (fr->ns.dump_nl > 0)
109     {
110         dump_nblist(fp, cr, fr, fr->ns.dump_nl);
111     }
112 }
113
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)
119 {
120     int t, i;
121
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++)
125     {
126         for (t = 1; t < nthreads; t++)
127         {
128             rvec_inc(f[i], f_t[t].f[i]);
129         }
130     }
131     for (t = 1; t < nthreads; t++)
132     {
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);
139     }
140 }
141
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,
145                        t_mdatoms  *md,
146                        rvec       x[],      history_t  *hist,
147                        rvec       f[],
148                        rvec       f_longrange[],
149                        gmx_enerdata_t *enerd,
150                        t_fcdata   *fcd,
151                        gmx_localtop_t *top,
152                        gmx_genborn_t *born,
153                        t_atomtypes *atype,
154                        gmx_bool       bBornRadii,
155                        matrix     box,
156                        t_lambda   *fepvals,
157                        real       *lambda,
158                        t_graph    *graph,
159                        t_blocka   *excl,
160                        rvec       mu_tot[],
161                        int        flags,
162                        float      *cycles_pme)
163 {
164     int         i, j;
165     int         donb_flags;
166     gmx_bool    bDoEpot, bSB;
167     int         pme_flags;
168     matrix      boxs;
169     rvec        box_size;
170     t_pbc       pbc;
171     char        buf[22];
172     double      clam_i, vlam_i;
173     real        dvdl_dum[efptNR], dvdl_nb[efptNR], lam_i[efptNR];
174     real        dvdl_q, dvdl_lj;
175
176 #ifdef GMX_MPI
177     double  t0 = 0.0, t1, t2, t3; /* time measurement for coarse load balancing */
178 #endif
179
180     set_pbc(&pbc, fr->ePBC, box);
181
182     /* reset free energy components */
183     for (i = 0; i < efptNR; i++)
184     {
185         dvdl_nb[i]  = 0;
186         dvdl_dum[i] = 0;
187     }
188
189     /* Reset box */
190     for (i = 0; (i < DIM); i++)
191     {
192         box_size[i] = box[i][i];
193     }
194
195     debug_gmx();
196
197     /* do QMMM first if requested */
198     if (fr->bQMMM)
199     {
200         enerd->term[F_EQM] = calculate_QMMM(cr, x, f, fr);
201     }
202
203     /* Call the short range functions all in one go. */
204
205 #ifdef GMX_MPI
206     /*#define TAKETIME ((cr->npmenodes) && (fr->timesteps < 12))*/
207 #define TAKETIME FALSE
208     if (TAKETIME)
209     {
210         MPI_Barrier(cr->mpi_comm_mygroup);
211         t0 = MPI_Wtime();
212     }
213 #endif
214
215     if (ir->nwall)
216     {
217         /* foreign lambda component for walls */
218         real dvdl_walls = do_walls(ir, fr, box, md, x, f, lambda[efptVDW],
219                                    enerd->grpp.ener[egLJSR], nrnb);
220         enerd->dvdl_lin[efptVDW] += dvdl_walls;
221     }
222
223     /* If doing GB, reset dvda and calculate the Born radii */
224     if (ir->implicit_solvent)
225     {
226         wallcycle_sub_start(wcycle, ewcsNONBONDED);
227
228         for (i = 0; i < born->nr; i++)
229         {
230             fr->dvda[i] = 0;
231         }
232
233         if (bBornRadii)
234         {
235             calc_gb_rad(cr, fr, ir, top, x, &(fr->gblist), born, md, nrnb);
236         }
237
238         wallcycle_sub_stop(wcycle, ewcsNONBONDED);
239     }
240
241     where();
242     /* We only do non-bonded calculation with group scheme here, the verlet
243      * calls are done from do_force_cutsVERLET(). */
244     if (fr->cutoff_scheme == ecutsGROUP && (flags & GMX_FORCE_NONBONDED))
245     {
246         donb_flags = 0;
247         /* Add short-range interactions */
248         donb_flags |= GMX_NONBONDED_DO_SR;
249
250         /* Currently all group scheme kernels always calculate (shift-)forces */
251         if (flags & GMX_FORCE_FORCES)
252         {
253             donb_flags |= GMX_NONBONDED_DO_FORCE;
254         }
255         if (flags & GMX_FORCE_VIRIAL)
256         {
257             donb_flags |= GMX_NONBONDED_DO_SHIFTFORCE;
258         }
259         if (flags & GMX_FORCE_ENERGY)
260         {
261             donb_flags |= GMX_NONBONDED_DO_POTENTIAL;
262         }
263         if (flags & GMX_FORCE_DO_LR)
264         {
265             donb_flags |= GMX_NONBONDED_DO_LR;
266         }
267
268         wallcycle_sub_start(wcycle, ewcsNONBONDED);
269         do_nonbonded(fr, x, f, f_longrange, md, excl,
270                      &enerd->grpp, nrnb,
271                      lambda, dvdl_nb, -1, -1, donb_flags);
272
273         /* If we do foreign lambda and we have soft-core interactions
274          * we have to recalculate the (non-linear) energies contributions.
275          */
276         if (fepvals->n_lambda > 0 && (flags & GMX_FORCE_DHDL) && fepvals->sc_alpha != 0)
277         {
278             for (i = 0; i < enerd->n_lambda; i++)
279             {
280                 for (j = 0; j < efptNR; j++)
281                 {
282                     lam_i[j] = (i == 0 ? lambda[j] : fepvals->all_lambda[j][i-1]);
283                 }
284                 reset_foreign_enerdata(enerd);
285                 do_nonbonded(fr, x, f, f_longrange, md, excl,
286                              &(enerd->foreign_grpp), nrnb,
287                              lam_i, dvdl_dum, -1, -1,
288                              (donb_flags & ~GMX_NONBONDED_DO_FORCE) | GMX_NONBONDED_DO_FOREIGNLAMBDA);
289                 sum_epot(&(enerd->foreign_grpp), enerd->foreign_term);
290                 enerd->enerpart_lambda[i] += enerd->foreign_term[F_EPOT];
291             }
292         }
293         wallcycle_sub_stop(wcycle, ewcsNONBONDED);
294         where();
295     }
296
297     /* If we are doing GB, calculate bonded forces and apply corrections
298      * to the solvation forces */
299     /* MRS: Eventually, many need to include free energy contribution here! */
300     if (ir->implicit_solvent)
301     {
302         wallcycle_sub_start(wcycle, ewcsBONDED);
303         calc_gb_forces(cr, md, born, top, x, f, fr, idef,
304                        ir->gb_algorithm, ir->sa_algorithm, nrnb, &pbc, graph, enerd);
305         wallcycle_sub_stop(wcycle, ewcsBONDED);
306     }
307
308 #ifdef GMX_MPI
309     if (TAKETIME)
310     {
311         t1          = MPI_Wtime();
312         fr->t_fnbf += t1-t0;
313     }
314 #endif
315
316     if (fepvals->sc_alpha != 0)
317     {
318         enerd->dvdl_nonlin[efptVDW] += dvdl_nb[efptVDW];
319     }
320     else
321     {
322         enerd->dvdl_lin[efptVDW] += dvdl_nb[efptVDW];
323     }
324
325     if (fepvals->sc_alpha != 0)
326
327     /* even though coulomb part is linear, we already added it, beacuse we
328        need to go through the vdw calculation anyway */
329     {
330         enerd->dvdl_nonlin[efptCOUL] += dvdl_nb[efptCOUL];
331     }
332     else
333     {
334         enerd->dvdl_lin[efptCOUL] += dvdl_nb[efptCOUL];
335     }
336
337     debug_gmx();
338
339
340     if (debug)
341     {
342         pr_rvecs(debug, 0, "fshift after SR", fr->fshift, SHIFTS);
343     }
344
345     /* Shift the coordinates. Must be done before bonded forces and PPPM,
346      * but is also necessary for SHAKE and update, therefore it can NOT
347      * go when no bonded forces have to be evaluated.
348      */
349
350     /* Here sometimes we would not need to shift with NBFonly,
351      * but we do so anyhow for consistency of the returned coordinates.
352      */
353     if (graph)
354     {
355         shift_self(graph, box, x);
356         if (TRICLINIC(box))
357         {
358             inc_nrnb(nrnb, eNR_SHIFTX, 2*graph->nnodes);
359         }
360         else
361         {
362             inc_nrnb(nrnb, eNR_SHIFTX, graph->nnodes);
363         }
364     }
365     /* Check whether we need to do bondeds or correct for exclusions */
366     if (fr->bMolPBC &&
367         ((flags & GMX_FORCE_BONDED)
368          || EEL_RF(fr->eeltype) || EEL_FULL(fr->eeltype) || EVDW_PME(fr->vdwtype)))
369     {
370         /* Since all atoms are in the rectangular or triclinic unit-cell,
371          * only single box vector shifts (2 in x) are required.
372          */
373         set_pbc_dd(&pbc, fr->ePBC, cr->dd, TRUE, box);
374     }
375     debug_gmx();
376
377     if (flags & GMX_FORCE_BONDED)
378     {
379         wallcycle_sub_start(wcycle, ewcsBONDED);
380         calc_bonds(cr->ms,
381                    idef, x, hist, f, fr, &pbc, graph, enerd, nrnb, lambda, md, fcd,
382                    DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL, atype, born,
383                    flags);
384
385         /* Check if we have to determine energy differences
386          * at foreign lambda's.
387          */
388         if (fepvals->n_lambda > 0 && (flags & GMX_FORCE_DHDL) &&
389             idef->ilsort != ilsortNO_FE)
390         {
391             if (idef->ilsort != ilsortFE_SORTED)
392             {
393                 gmx_incons("The bonded interactions are not sorted for free energy");
394             }
395             for (i = 0; i < enerd->n_lambda; i++)
396             {
397                 reset_foreign_enerdata(enerd);
398                 for (j = 0; j < efptNR; j++)
399                 {
400                     lam_i[j] = (i == 0 ? lambda[j] : fepvals->all_lambda[j][i-1]);
401                 }
402                 calc_bonds_lambda(idef, x, fr, &pbc, graph, &(enerd->foreign_grpp), enerd->foreign_term, nrnb, lam_i, md,
403                                   fcd, DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL);
404                 sum_epot(&(enerd->foreign_grpp), enerd->foreign_term);
405                 enerd->enerpart_lambda[i] += enerd->foreign_term[F_EPOT];
406             }
407         }
408         debug_gmx();
409
410         wallcycle_sub_stop(wcycle, ewcsBONDED);
411     }
412
413     where();
414
415     *cycles_pme = 0;
416     clear_mat(fr->vir_el_recip);
417     clear_mat(fr->vir_lj_recip);
418
419     /* Do long-range electrostatics and/or LJ-PME, including related short-range
420      * corrections.
421      */
422     if (EEL_FULL(fr->eeltype) || EVDW_PME(fr->vdwtype))
423     {
424         real Vlr               = 0, Vcorr = 0;
425         real dvdl_long_range   = 0;
426         int  status            = 0;
427         real Vlr_q             = 0, Vlr_lj = 0, Vcorr_q = 0, Vcorr_lj = 0;
428         real dvdl_long_range_q = 0, dvdl_long_range_lj = 0;
429
430         bSB = (ir->nwall == 2);
431         if (bSB)
432         {
433             copy_mat(box, boxs);
434             svmul(ir->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
435             box_size[ZZ] *= ir->wall_ewald_zfac;
436         }
437
438         if (EEL_PME_EWALD(fr->eeltype) || EVDW_PME(fr->vdwtype))
439         {
440             real dvdl_long_range_correction_q   = 0;
441             real dvdl_long_range_correction_lj  = 0;
442             /* With the Verlet scheme exclusion forces are calculated
443              * in the non-bonded kernel.
444              */
445             /* The TPI molecule does not have exclusions with the rest
446              * of the system and no intra-molecular PME grid
447              * contributions will be calculated in
448              * gmx_pme_calc_energy.
449              */
450             if ((ir->cutoff_scheme == ecutsGROUP && fr->n_tpi == 0) ||
451                 ir->ewald_geometry != eewg3D ||
452                 ir->epsilon_surface != 0)
453             {
454                 int nthreads, t;
455
456                 wallcycle_sub_start(wcycle, ewcsEWALD_CORRECTION);
457
458                 if (fr->n_tpi > 0)
459                 {
460                     gmx_fatal(FARGS, "TPI with PME currently only works in a 3D geometry with tin-foil boundary conditions");
461                 }
462
463                 nthreads = gmx_omp_nthreads_get(emntBonded);
464 #pragma omp parallel for num_threads(nthreads) schedule(static)
465                 for (t = 0; t < nthreads; t++)
466                 {
467                     int     s, e, i;
468                     rvec   *fnv;
469                     tensor *vir_q, *vir_lj;
470                     real   *Vcorrt_q, *Vcorrt_lj, *dvdlt_q, *dvdlt_lj;
471                     if (t == 0)
472                     {
473                         fnv       = fr->f_novirsum;
474                         vir_q     = &fr->vir_el_recip;
475                         vir_lj    = &fr->vir_lj_recip;
476                         Vcorrt_q  = &Vcorr_q;
477                         Vcorrt_lj = &Vcorr_lj;
478                         dvdlt_q   = &dvdl_long_range_correction_q;
479                         dvdlt_lj  = &dvdl_long_range_correction_lj;
480                     }
481                     else
482                     {
483                         fnv       = fr->f_t[t].f;
484                         vir_q     = &fr->f_t[t].vir_q;
485                         vir_lj    = &fr->f_t[t].vir_lj;
486                         Vcorrt_q  = &fr->f_t[t].Vcorr_q;
487                         Vcorrt_lj = &fr->f_t[t].Vcorr_lj;
488                         dvdlt_q   = &fr->f_t[t].dvdl[efptCOUL];
489                         dvdlt_lj  = &fr->f_t[t].dvdl[efptVDW];
490                         for (i = 0; i < fr->natoms_force; i++)
491                         {
492                             clear_rvec(fnv[i]);
493                         }
494                         clear_mat(*vir_q);
495                         clear_mat(*vir_lj);
496                     }
497                     *dvdlt_q  = 0;
498                     *dvdlt_lj = 0;
499
500                     ewald_LRcorrection(fr->excl_load[t], fr->excl_load[t+1],
501                                        cr, t, fr,
502                                        md->chargeA,
503                                        md->nChargePerturbed ? md->chargeB : NULL,
504                                        md->sqrt_c6A,
505                                        md->nTypePerturbed ? md->sqrt_c6B : NULL,
506                                        md->sigmaA,
507                                        md->nTypePerturbed ? md->sigmaB : NULL,
508                                        md->sigma3A,
509                                        md->nTypePerturbed ? md->sigma3B : NULL,
510                                        ir->cutoff_scheme != ecutsVERLET,
511                                        excl, x, bSB ? boxs : box, mu_tot,
512                                        ir->ewald_geometry,
513                                        ir->epsilon_surface,
514                                        fnv, *vir_q, *vir_lj,
515                                        Vcorrt_q, Vcorrt_lj,
516                                        lambda[efptCOUL], lambda[efptVDW],
517                                        dvdlt_q, dvdlt_lj);
518                 }
519                 if (nthreads > 1)
520                 {
521                     reduce_thread_forces(fr->natoms_force, fr->f_novirsum,
522                                          fr->vir_el_recip, fr->vir_lj_recip,
523                                          &Vcorr_q, &Vcorr_lj,
524                                          &dvdl_long_range_correction_q,
525                                          &dvdl_long_range_correction_lj,
526                                          nthreads, fr->f_t);
527                 }
528                 wallcycle_sub_stop(wcycle, ewcsEWALD_CORRECTION);
529             }
530
531             if (EEL_PME_EWALD(fr->eeltype) && fr->n_tpi == 0)
532             {
533                 Vcorr_q += ewald_charge_correction(cr, fr, lambda[efptCOUL], box,
534                                                    &dvdl_long_range_correction_q,
535                                                    fr->vir_el_recip);
536             }
537
538             enerd->dvdl_lin[efptCOUL] += dvdl_long_range_correction_q;
539             enerd->dvdl_lin[efptVDW]  += dvdl_long_range_correction_lj;
540
541             if ((EEL_PME(fr->eeltype) || EVDW_PME(fr->vdwtype)) && (cr->duty & DUTY_PME))
542             {
543                 /* Do reciprocal PME for Coulomb and/or LJ. */
544                 assert(fr->n_tpi >= 0);
545                 if (fr->n_tpi == 0 || (flags & GMX_FORCE_STATECHANGED))
546                 {
547                     pme_flags = GMX_PME_SPREAD | GMX_PME_SOLVE;
548                     if (EEL_PME(fr->eeltype))
549                     {
550                         pme_flags     |= GMX_PME_DO_COULOMB;
551                     }
552                     if (EVDW_PME(fr->vdwtype))
553                     {
554                         pme_flags |= GMX_PME_DO_LJ;
555                     }
556                     if (flags & GMX_FORCE_FORCES)
557                     {
558                         pme_flags |= GMX_PME_CALC_F;
559                     }
560                     if (flags & GMX_FORCE_VIRIAL)
561                     {
562                         pme_flags |= GMX_PME_CALC_ENER_VIR;
563                     }
564                     if (fr->n_tpi > 0)
565                     {
566                         /* We don't calculate f, but we do want the potential */
567                         pme_flags |= GMX_PME_CALC_POT;
568                     }
569                     wallcycle_start(wcycle, ewcPMEMESH);
570                     status = gmx_pme_do(fr->pmedata,
571                                         0, md->homenr - fr->n_tpi,
572                                         x, fr->f_novirsum,
573                                         md->chargeA, md->chargeB,
574                                         md->sqrt_c6A, md->sqrt_c6B,
575                                         md->sigmaA, md->sigmaB,
576                                         bSB ? boxs : box, cr,
577                                         DOMAINDECOMP(cr) ? dd_pme_maxshift_x(cr->dd) : 0,
578                                         DOMAINDECOMP(cr) ? dd_pme_maxshift_y(cr->dd) : 0,
579                                         nrnb, wcycle,
580                                         fr->vir_el_recip, fr->ewaldcoeff_q,
581                                         fr->vir_lj_recip, fr->ewaldcoeff_lj,
582                                         &Vlr_q, &Vlr_lj,
583                                         lambda[efptCOUL], lambda[efptVDW],
584                                         &dvdl_long_range_q, &dvdl_long_range_lj, pme_flags);
585                     *cycles_pme = wallcycle_stop(wcycle, ewcPMEMESH);
586                     if (status != 0)
587                     {
588                         gmx_fatal(FARGS, "Error %d in reciprocal PME routine", status);
589                     }
590                     /* We should try to do as little computation after
591                      * this as possible, because parallel PME synchronizes
592                      * the nodes, so we want all load imbalance of the
593                      * rest of the force calculation to be before the PME
594                      * call.  DD load balancing is done on the whole time
595                      * of the force call (without PME).
596                      */
597                 }
598                 if (fr->n_tpi > 0)
599                 {
600                     if (EVDW_PME(ir->vdwtype))
601                     {
602
603                         gmx_fatal(FARGS, "Test particle insertion not implemented with LJ-PME");
604                     }
605                     /* Determine the PME grid energy of the test molecule
606                      * with the PME grid potential of the other charges.
607                      */
608                     gmx_pme_calc_energy(fr->pmedata, fr->n_tpi,
609                                         x + md->homenr - fr->n_tpi,
610                                         md->chargeA + md->homenr - fr->n_tpi,
611                                         &Vlr_q);
612                 }
613             }
614         }
615
616         if (!EEL_PME(fr->eeltype) && EEL_PME_EWALD(fr->eeltype))
617         {
618             Vlr_q = do_ewald(ir, x, fr->f_novirsum,
619                              md->chargeA, md->chargeB,
620                              box_size, cr, md->homenr,
621                              fr->vir_el_recip, fr->ewaldcoeff_q,
622                              lambda[efptCOUL], &dvdl_long_range_q, fr->ewald_table);
623         }
624
625         /* Note that with separate PME nodes we get the real energies later */
626         enerd->dvdl_lin[efptCOUL] += dvdl_long_range_q;
627         enerd->dvdl_lin[efptVDW]  += dvdl_long_range_lj;
628         enerd->term[F_COUL_RECIP]  = Vlr_q + Vcorr_q;
629         enerd->term[F_LJ_RECIP]    = Vlr_lj + Vcorr_lj;
630         if (debug)
631         {
632             fprintf(debug, "Vlr_q = %g, Vcorr_q = %g, Vlr_corr_q = %g\n",
633                     Vlr_q, Vcorr_q, enerd->term[F_COUL_RECIP]);
634             pr_rvecs(debug, 0, "vir_el_recip after corr", fr->vir_el_recip, DIM);
635             pr_rvecs(debug, 0, "fshift after LR Corrections", fr->fshift, SHIFTS);
636             fprintf(debug, "Vlr_lj: %g, Vcorr_lj = %g, Vlr_corr_lj = %g\n",
637                     Vlr_lj, Vcorr_lj, enerd->term[F_LJ_RECIP]);
638             pr_rvecs(debug, 0, "vir_lj_recip after corr", fr->vir_lj_recip, DIM);
639         }
640     }
641     else
642     {
643         /* Is there a reaction-field exclusion correction needed? */
644         if (EEL_RF(fr->eeltype) && eelRF_NEC != fr->eeltype)
645         {
646             /* With the Verlet scheme, exclusion forces are calculated
647              * in the non-bonded kernel.
648              */
649             if (ir->cutoff_scheme != ecutsVERLET)
650             {
651                 real dvdl_rf_excl      = 0;
652                 enerd->term[F_RF_EXCL] =
653                     RF_excl_correction(fr, graph, md, excl, x, f,
654                                        fr->fshift, &pbc, lambda[efptCOUL], &dvdl_rf_excl);
655
656                 enerd->dvdl_lin[efptCOUL] += dvdl_rf_excl;
657             }
658         }
659     }
660     where();
661     debug_gmx();
662
663     if (debug)
664     {
665         print_nrnb(debug, nrnb);
666     }
667     debug_gmx();
668
669 #ifdef GMX_MPI
670     if (TAKETIME)
671     {
672         t2 = MPI_Wtime();
673         MPI_Barrier(cr->mpi_comm_mygroup);
674         t3          = MPI_Wtime();
675         fr->t_wait += t3-t2;
676         if (fr->timesteps == 11)
677         {
678             fprintf(stderr, "* PP load balancing info: rank %d, step %s, rel wait time=%3.0f%% , load string value: %7.2f\n",
679                     cr->nodeid, gmx_step_str(fr->timesteps, buf),
680                     100*fr->t_wait/(fr->t_wait+fr->t_fnbf),
681                     (fr->t_fnbf+fr->t_wait)/fr->t_fnbf);
682         }
683         fr->timesteps++;
684     }
685 #endif
686
687     if (debug)
688     {
689         pr_rvecs(debug, 0, "fshift after bondeds", fr->fshift, SHIFTS);
690     }
691
692 }
693
694 void init_enerdata(int ngener, int n_lambda, gmx_enerdata_t *enerd)
695 {
696     int i, n2;
697
698     for (i = 0; i < F_NRE; i++)
699     {
700         enerd->term[i]         = 0;
701         enerd->foreign_term[i] = 0;
702     }
703
704
705     for (i = 0; i < efptNR; i++)
706     {
707         enerd->dvdl_lin[i]     = 0;
708         enerd->dvdl_nonlin[i]  = 0;
709     }
710
711     n2 = ngener*ngener;
712     if (debug)
713     {
714         fprintf(debug, "Creating %d sized group matrix for energies\n", n2);
715     }
716     enerd->grpp.nener         = n2;
717     enerd->foreign_grpp.nener = n2;
718     for (i = 0; (i < egNR); i++)
719     {
720         snew(enerd->grpp.ener[i], n2);
721         snew(enerd->foreign_grpp.ener[i], n2);
722     }
723
724     if (n_lambda)
725     {
726         enerd->n_lambda = 1 + n_lambda;
727         snew(enerd->enerpart_lambda, enerd->n_lambda);
728     }
729     else
730     {
731         enerd->n_lambda = 0;
732     }
733 }
734
735 void destroy_enerdata(gmx_enerdata_t *enerd)
736 {
737     int i;
738
739     for (i = 0; (i < egNR); i++)
740     {
741         sfree(enerd->grpp.ener[i]);
742     }
743
744     for (i = 0; (i < egNR); i++)
745     {
746         sfree(enerd->foreign_grpp.ener[i]);
747     }
748
749     if (enerd->n_lambda)
750     {
751         sfree(enerd->enerpart_lambda);
752     }
753 }
754
755 static real sum_v(int n, real v[])
756 {
757     real t;
758     int  i;
759
760     t = 0.0;
761     for (i = 0; (i < n); i++)
762     {
763         t = t + v[i];
764     }
765
766     return t;
767 }
768
769 void sum_epot(gmx_grppairener_t *grpp, real *epot)
770 {
771     int i;
772
773     /* Accumulate energies */
774     epot[F_COUL_SR]  = sum_v(grpp->nener, grpp->ener[egCOULSR]);
775     epot[F_LJ]       = sum_v(grpp->nener, grpp->ener[egLJSR]);
776     epot[F_LJ14]     = sum_v(grpp->nener, grpp->ener[egLJ14]);
777     epot[F_COUL14]   = sum_v(grpp->nener, grpp->ener[egCOUL14]);
778     epot[F_COUL_LR]  = sum_v(grpp->nener, grpp->ener[egCOULLR]);
779     epot[F_LJ_LR]    = sum_v(grpp->nener, grpp->ener[egLJLR]);
780     /* We have already added 1-2,1-3, and 1-4 terms to F_GBPOL */
781     epot[F_GBPOL]   += sum_v(grpp->nener, grpp->ener[egGB]);
782
783 /* lattice part of LR doesnt belong to any group
784  * and has been added earlier
785  */
786     epot[F_BHAM]     = sum_v(grpp->nener, grpp->ener[egBHAMSR]);
787     epot[F_BHAM_LR]  = sum_v(grpp->nener, grpp->ener[egBHAMLR]);
788
789     epot[F_EPOT] = 0;
790     for (i = 0; (i < F_EPOT); i++)
791     {
792         if (i != F_DISRESVIOL && i != F_ORIRESDEV)
793         {
794             epot[F_EPOT] += epot[i];
795         }
796     }
797 }
798
799 void sum_dhdl(gmx_enerdata_t *enerd, real *lambda, t_lambda *fepvals)
800 {
801     int    i, j, index;
802     double dlam;
803
804     enerd->dvdl_lin[efptVDW] += enerd->term[F_DVDL_VDW];  /* include dispersion correction */
805     enerd->term[F_DVDL]       = 0.0;
806     for (i = 0; i < efptNR; i++)
807     {
808         if (fepvals->separate_dvdl[i])
809         {
810             /* could this be done more readably/compactly? */
811             switch (i)
812             {
813                 case (efptMASS):
814                     index = F_DKDL;
815                     break;
816                 case (efptCOUL):
817                     index = F_DVDL_COUL;
818                     break;
819                 case (efptVDW):
820                     index = F_DVDL_VDW;
821                     break;
822                 case (efptBONDED):
823                     index = F_DVDL_BONDED;
824                     break;
825                 case (efptRESTRAINT):
826                     index = F_DVDL_RESTRAINT;
827                     break;
828                 default:
829                     index = F_DVDL;
830                     break;
831             }
832             enerd->term[index] = enerd->dvdl_lin[i] + enerd->dvdl_nonlin[i];
833             if (debug)
834             {
835                 fprintf(debug, "dvdl-%s[%2d]: %f: non-linear %f + linear %f\n",
836                         efpt_names[i], i, enerd->term[index], enerd->dvdl_nonlin[i], enerd->dvdl_lin[i]);
837             }
838         }
839         else
840         {
841             enerd->term[F_DVDL] += enerd->dvdl_lin[i] + enerd->dvdl_nonlin[i];
842             if (debug)
843             {
844                 fprintf(debug, "dvd-%sl[%2d]: %f: non-linear %f + linear %f\n",
845                         efpt_names[0], i, enerd->term[F_DVDL], enerd->dvdl_nonlin[i], enerd->dvdl_lin[i]);
846             }
847         }
848     }
849
850     /* Notes on the foreign lambda free energy difference evaluation:
851      * Adding the potential and ekin terms that depend linearly on lambda
852      * as delta lam * dvdl to the energy differences is exact.
853      * For the constraints this is not exact, but we have no other option
854      * without literally changing the lengths and reevaluating the energies at each step.
855      * (try to remedy this post 4.6 - MRS)
856      * For the non-bonded LR term we assume that the soft-core (if present)
857      * no longer affects the energy beyond the short-range cut-off,
858      * which is a very good approximation (except for exotic settings).
859      * (investigate how to overcome this post 4.6 - MRS)
860      */
861     if (fepvals->separate_dvdl[efptBONDED])
862     {
863         enerd->term[F_DVDL_BONDED] += enerd->term[F_DVDL_CONSTR];
864     }
865     else
866     {
867         enerd->term[F_DVDL] += enerd->term[F_DVDL_CONSTR];
868     }
869     enerd->term[F_DVDL_CONSTR] = 0;
870
871     for (i = 0; i < fepvals->n_lambda; i++)
872     {
873         /* note we are iterating over fepvals here!
874            For the current lam, dlam = 0 automatically,
875            so we don't need to add anything to the
876            enerd->enerpart_lambda[0] */
877
878         /* we don't need to worry about dvdl_lin contributions to dE at
879            current lambda, because the contributions to the current
880            lambda are automatically zeroed */
881
882         for (j = 0; j < efptNR; j++)
883         {
884             /* Note that this loop is over all dhdl components, not just the separated ones */
885             dlam = (fepvals->all_lambda[j][i]-lambda[j]);
886             enerd->enerpart_lambda[i+1] += dlam*enerd->dvdl_lin[j];
887             if (debug)
888             {
889                 fprintf(debug, "enerdiff lam %g: (%15s), non-linear %f linear %f*%f\n",
890                         fepvals->all_lambda[j][i], efpt_names[j],
891                         (enerd->enerpart_lambda[i+1] - enerd->enerpart_lambda[0]),
892                         dlam, enerd->dvdl_lin[j]);
893             }
894         }
895     }
896 }
897
898
899 void reset_foreign_enerdata(gmx_enerdata_t *enerd)
900 {
901     int  i, j;
902
903     /* First reset all foreign energy components.  Foreign energies always called on
904        neighbor search steps */
905     for (i = 0; (i < egNR); i++)
906     {
907         for (j = 0; (j < enerd->grpp.nener); j++)
908         {
909             enerd->foreign_grpp.ener[i][j] = 0.0;
910         }
911     }
912
913     /* potential energy components */
914     for (i = 0; (i <= F_EPOT); i++)
915     {
916         enerd->foreign_term[i] = 0.0;
917     }
918 }
919
920 void reset_enerdata(t_forcerec *fr, gmx_bool bNS,
921                     gmx_enerdata_t *enerd,
922                     gmx_bool bMaster)
923 {
924     gmx_bool bKeepLR;
925     int      i, j;
926
927     /* First reset all energy components, except for the long range terms
928      * on the master at non neighbor search steps, since the long range
929      * terms have already been summed at the last neighbor search step.
930      */
931     bKeepLR = (fr->bTwinRange && !bNS);
932     for (i = 0; (i < egNR); i++)
933     {
934         if (!(bKeepLR && bMaster && (i == egCOULLR || i == egLJLR)))
935         {
936             for (j = 0; (j < enerd->grpp.nener); j++)
937             {
938                 enerd->grpp.ener[i][j] = 0.0;
939             }
940         }
941     }
942     for (i = 0; i < efptNR; i++)
943     {
944         enerd->dvdl_lin[i]    = 0.0;
945         enerd->dvdl_nonlin[i] = 0.0;
946     }
947
948     /* Normal potential energy components */
949     for (i = 0; (i <= F_EPOT); i++)
950     {
951         enerd->term[i] = 0.0;
952     }
953     /* Initialize the dVdlambda term with the long range contribution */
954     /* Initialize the dvdl term with the long range contribution */
955     enerd->term[F_DVDL]            = 0.0;
956     enerd->term[F_DVDL_COUL]       = 0.0;
957     enerd->term[F_DVDL_VDW]        = 0.0;
958     enerd->term[F_DVDL_BONDED]     = 0.0;
959     enerd->term[F_DVDL_RESTRAINT]  = 0.0;
960     enerd->term[F_DKDL]            = 0.0;
961     if (enerd->n_lambda > 0)
962     {
963         for (i = 0; i < enerd->n_lambda; i++)
964         {
965             enerd->enerpart_lambda[i] = 0.0;
966         }
967     }
968     /* reset foreign energy data - separate function since we also call it elsewhere */
969     reset_foreign_enerdata(enerd);
970 }