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