Modernize write_inpfile
[alexxy/gromacs.git] / src / gromacs / mdlib / sim_util.cpp
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,2015,2016,2017, 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 "sim_util.h"
40
41 #include "config.h"
42
43 #include <assert.h>
44 #include <math.h>
45 #include <stdio.h>
46 #include <string.h>
47
48 #include <cstdint>
49
50 #include <array>
51
52 #include "gromacs/domdec/domdec.h"
53 #include "gromacs/domdec/domdec_struct.h"
54 #include "gromacs/essentialdynamics/edsam.h"
55 #include "gromacs/ewald/pme.h"
56 #include "gromacs/gmxlib/chargegroup.h"
57 #include "gromacs/gmxlib/network.h"
58 #include "gromacs/gmxlib/nrnb.h"
59 #include "gromacs/gmxlib/nonbonded/nb_free_energy.h"
60 #include "gromacs/gmxlib/nonbonded/nb_kernel.h"
61 #include "gromacs/gmxlib/nonbonded/nonbonded.h"
62 #include "gromacs/imd/imd.h"
63 #include "gromacs/listed-forces/bonded.h"
64 #include "gromacs/listed-forces/disre.h"
65 #include "gromacs/listed-forces/orires.h"
66 #include "gromacs/math/functions.h"
67 #include "gromacs/math/units.h"
68 #include "gromacs/math/vec.h"
69 #include "gromacs/math/vecdump.h"
70 #include "gromacs/mdlib/calcmu.h"
71 #include "gromacs/mdlib/constr.h"
72 #include "gromacs/mdlib/force.h"
73 #include "gromacs/mdlib/forcerec.h"
74 #include "gromacs/mdlib/genborn.h"
75 #include "gromacs/mdlib/gmx_omp_nthreads.h"
76 #include "gromacs/mdlib/mdrun.h"
77 #include "gromacs/mdlib/nb_verlet.h"
78 #include "gromacs/mdlib/nbnxn_atomdata.h"
79 #include "gromacs/mdlib/nbnxn_gpu_data_mgmt.h"
80 #include "gromacs/mdlib/nbnxn_grid.h"
81 #include "gromacs/mdlib/nbnxn_search.h"
82 #include "gromacs/mdlib/qmmm.h"
83 #include "gromacs/mdlib/update.h"
84 #include "gromacs/mdlib/nbnxn_kernels/nbnxn_kernel_gpu_ref.h"
85 #include "gromacs/mdlib/nbnxn_kernels/nbnxn_kernel_ref.h"
86 #include "gromacs/mdlib/nbnxn_kernels/simd_2xnn/nbnxn_kernel_simd_2xnn.h"
87 #include "gromacs/mdlib/nbnxn_kernels/simd_4xn/nbnxn_kernel_simd_4xn.h"
88 #include "gromacs/mdtypes/commrec.h"
89 #include "gromacs/mdtypes/iforceprovider.h"
90 #include "gromacs/mdtypes/inputrec.h"
91 #include "gromacs/mdtypes/md_enums.h"
92 #include "gromacs/mdtypes/state.h"
93 #include "gromacs/pbcutil/ishift.h"
94 #include "gromacs/pbcutil/mshift.h"
95 #include "gromacs/pbcutil/pbc.h"
96 #include "gromacs/pulling/pull.h"
97 #include "gromacs/pulling/pull_rotation.h"
98 #include "gromacs/timing/cyclecounter.h"
99 #include "gromacs/timing/gpu_timing.h"
100 #include "gromacs/timing/wallcycle.h"
101 #include "gromacs/timing/wallcyclereporting.h"
102 #include "gromacs/timing/walltime_accounting.h"
103 #include "gromacs/utility/arrayref.h"
104 #include "gromacs/utility/basedefinitions.h"
105 #include "gromacs/utility/cstringutil.h"
106 #include "gromacs/utility/exceptions.h"
107 #include "gromacs/utility/fatalerror.h"
108 #include "gromacs/utility/gmxassert.h"
109 #include "gromacs/utility/gmxmpi.h"
110 #include "gromacs/utility/logger.h"
111 #include "gromacs/utility/pleasecite.h"
112 #include "gromacs/utility/smalloc.h"
113 #include "gromacs/utility/sysinfo.h"
114
115 #include "nbnxn_gpu.h"
116
117 void print_time(FILE                     *out,
118                 gmx_walltime_accounting_t walltime_accounting,
119                 gmx_int64_t               step,
120                 t_inputrec               *ir,
121                 t_commrec gmx_unused     *cr)
122 {
123     time_t finish;
124     char   timebuf[STRLEN];
125     double dt, elapsed_seconds, time_per_step;
126     char   buf[48];
127
128 #if !GMX_THREAD_MPI
129     if (!PAR(cr))
130 #endif
131     {
132         fprintf(out, "\r");
133     }
134     fprintf(out, "step %s", gmx_step_str(step, buf));
135     fflush(out);
136
137     if ((step >= ir->nstlist))
138     {
139         double seconds_since_epoch = gmx_gettime();
140         elapsed_seconds = seconds_since_epoch - walltime_accounting_get_start_time_stamp(walltime_accounting);
141         time_per_step   = elapsed_seconds/(step - ir->init_step + 1);
142         dt              = (ir->nsteps + ir->init_step - step) * time_per_step;
143
144         if (ir->nsteps >= 0)
145         {
146             if (dt >= 300)
147             {
148                 finish = (time_t) (seconds_since_epoch + dt);
149                 gmx_ctime_r(&finish, timebuf, STRLEN);
150                 sprintf(buf, "%s", timebuf);
151                 buf[strlen(buf)-1] = '\0';
152                 fprintf(out, ", will finish %s", buf);
153             }
154             else
155             {
156                 fprintf(out, ", remaining wall clock time: %5d s          ", (int)dt);
157             }
158         }
159         else
160         {
161             fprintf(out, " performance: %.1f ns/day    ",
162                     ir->delta_t/1000*24*60*60/time_per_step);
163         }
164     }
165 #if !GMX_THREAD_MPI
166     if (PAR(cr))
167     {
168         fprintf(out, "\n");
169     }
170 #endif
171
172     fflush(out);
173 }
174
175 void print_date_and_time(FILE *fplog, int nodeid, const char *title,
176                          double the_time)
177 {
178     char   time_string[STRLEN];
179
180     if (!fplog)
181     {
182         return;
183     }
184
185     {
186         int    i;
187         char   timebuf[STRLEN];
188         time_t temp_time = (time_t) the_time;
189
190         gmx_ctime_r(&temp_time, timebuf, STRLEN);
191         for (i = 0; timebuf[i] >= ' '; i++)
192         {
193             time_string[i] = timebuf[i];
194         }
195         time_string[i] = '\0';
196     }
197
198     fprintf(fplog, "%s on rank %d %s\n", title, nodeid, time_string);
199 }
200
201 void print_start(FILE *fplog, t_commrec *cr,
202                  gmx_walltime_accounting_t walltime_accounting,
203                  const char *name)
204 {
205     char buf[STRLEN];
206
207     sprintf(buf, "Started %s", name);
208     print_date_and_time(fplog, cr->nodeid, buf,
209                         walltime_accounting_get_start_time_stamp(walltime_accounting));
210 }
211
212 static void sum_forces(rvec f[], const PaddedRVecVector *forceToAdd)
213 {
214     /* TODO: remove this - 1 when padding is properly implemented */
215     int         end  = forceToAdd->size() - 1;
216     const rvec *fAdd = as_rvec_array(forceToAdd->data());
217
218     // cppcheck-suppress unreadVariable
219     int gmx_unused nt = gmx_omp_nthreads_get(emntDefault);
220 #pragma omp parallel for num_threads(nt) schedule(static)
221     for (int i = 0; i < end; i++)
222     {
223         rvec_inc(f[i], fAdd[i]);
224     }
225 }
226
227 static void calc_virial(int start, int homenr, rvec x[], rvec f[],
228                         tensor vir_part, t_graph *graph, matrix box,
229                         t_nrnb *nrnb, const t_forcerec *fr, int ePBC)
230 {
231     int    i;
232
233     /* The short-range virial from surrounding boxes */
234     clear_mat(vir_part);
235     calc_vir(SHIFTS, fr->shift_vec, fr->fshift, vir_part, ePBC == epbcSCREW, box);
236     inc_nrnb(nrnb, eNR_VIRIAL, SHIFTS);
237
238     /* Calculate partial virial, for local atoms only, based on short range.
239      * Total virial is computed in global_stat, called from do_md
240      */
241     f_calc_vir(start, start+homenr, x, f, vir_part, graph, box);
242     inc_nrnb(nrnb, eNR_VIRIAL, homenr);
243
244     /* Add position restraint contribution */
245     for (i = 0; i < DIM; i++)
246     {
247         vir_part[i][i] += fr->vir_diag_posres[i];
248     }
249
250     /* Add wall contribution */
251     for (i = 0; i < DIM; i++)
252     {
253         vir_part[i][ZZ] += fr->vir_wall_z[i];
254     }
255
256     if (debug)
257     {
258         pr_rvecs(debug, 0, "vir_part", vir_part, DIM);
259     }
260 }
261
262 static void pull_potential_wrapper(t_commrec *cr,
263                                    t_inputrec *ir,
264                                    matrix box, rvec x[],
265                                    rvec f[],
266                                    tensor vir_force,
267                                    t_mdatoms *mdatoms,
268                                    gmx_enerdata_t *enerd,
269                                    real *lambda,
270                                    double t,
271                                    gmx_wallcycle_t wcycle)
272 {
273     t_pbc  pbc;
274     real   dvdl;
275
276     /* Calculate the center of mass forces, this requires communication,
277      * which is why pull_potential is called close to other communication.
278      * The virial contribution is calculated directly,
279      * which is why we call pull_potential after calc_virial.
280      */
281     wallcycle_start(wcycle, ewcPULLPOT);
282     set_pbc(&pbc, ir->ePBC, box);
283     dvdl                     = 0;
284     enerd->term[F_COM_PULL] +=
285         pull_potential(ir->pull_work, mdatoms, &pbc,
286                        cr, t, lambda[efptRESTRAINT], x, f, vir_force, &dvdl);
287     enerd->dvdl_lin[efptRESTRAINT] += dvdl;
288     wallcycle_stop(wcycle, ewcPULLPOT);
289 }
290
291 static void pme_receive_force_ener(t_commrec      *cr,
292                                    gmx_wallcycle_t wcycle,
293                                    gmx_enerdata_t *enerd,
294                                    t_forcerec     *fr)
295 {
296     real   e_q, e_lj, dvdl_q, dvdl_lj;
297     float  cycles_ppdpme, cycles_seppme;
298
299     cycles_ppdpme = wallcycle_stop(wcycle, ewcPPDURINGPME);
300     dd_cycles_add(cr->dd, cycles_ppdpme, ddCyclPPduringPME);
301
302     /* In case of node-splitting, the PP nodes receive the long-range
303      * forces, virial and energy from the PME nodes here.
304      */
305     wallcycle_start(wcycle, ewcPP_PMEWAITRECVF);
306     dvdl_q  = 0;
307     dvdl_lj = 0;
308     gmx_pme_receive_f(cr, as_rvec_array(fr->f_novirsum->data()), fr->vir_el_recip, &e_q,
309                       fr->vir_lj_recip, &e_lj, &dvdl_q, &dvdl_lj,
310                       &cycles_seppme);
311     enerd->term[F_COUL_RECIP] += e_q;
312     enerd->term[F_LJ_RECIP]   += e_lj;
313     enerd->dvdl_lin[efptCOUL] += dvdl_q;
314     enerd->dvdl_lin[efptVDW]  += dvdl_lj;
315
316     if (wcycle)
317     {
318         dd_cycles_add(cr->dd, cycles_seppme, ddCyclPME);
319     }
320     wallcycle_stop(wcycle, ewcPP_PMEWAITRECVF);
321 }
322
323 static void print_large_forces(FILE *fp, t_mdatoms *md, t_commrec *cr,
324                                gmx_int64_t step, real forceTolerance,
325                                const rvec *x, const rvec *f)
326 {
327     real           force2Tolerance = gmx::square(forceTolerance);
328     std::uintmax_t numNonFinite    = 0;
329     for (int i = 0; i < md->homenr; i++)
330     {
331         real force2    = norm2(f[i]);
332         bool nonFinite = !std::isfinite(force2);
333         if (force2 >= force2Tolerance || nonFinite)
334         {
335             fprintf(fp, "step %" GMX_PRId64 " atom %6d  x %8.3f %8.3f %8.3f  force %12.5e\n",
336                     step,
337                     ddglatnr(cr->dd, i), x[i][XX], x[i][YY], x[i][ZZ], std::sqrt(force2));
338         }
339         if (nonFinite)
340         {
341             numNonFinite++;
342         }
343     }
344     if (numNonFinite > 0)
345     {
346         /* Note that with MPI this fatal call on one rank might interrupt
347          * the printing on other ranks. But we can only avoid that with
348          * an expensive MPI barrier that we would need at each step.
349          */
350         gmx_fatal(FARGS, "At step %" GMX_PRId64 " detected non-finite forces on %ju atoms", step, numNonFinite);
351     }
352 }
353
354 static void post_process_forces(t_commrec *cr,
355                                 gmx_int64_t step,
356                                 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
357                                 gmx_localtop_t *top,
358                                 matrix box, rvec x[],
359                                 rvec f[],
360                                 tensor vir_force,
361                                 t_mdatoms *mdatoms,
362                                 t_graph *graph,
363                                 t_forcerec *fr, gmx_vsite_t *vsite,
364                                 int flags)
365 {
366     if (fr->bF_NoVirSum)
367     {
368         if (vsite)
369         {
370             /* Spread the mesh force on virtual sites to the other particles...
371              * This is parallellized. MPI communication is performed
372              * if the constructing atoms aren't local.
373              */
374             wallcycle_start(wcycle, ewcVSITESPREAD);
375             spread_vsite_f(vsite, x, as_rvec_array(fr->f_novirsum->data()), nullptr,
376                            (flags & GMX_FORCE_VIRIAL), fr->vir_el_recip,
377                            nrnb,
378                            &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
379             wallcycle_stop(wcycle, ewcVSITESPREAD);
380         }
381         if (flags & GMX_FORCE_VIRIAL)
382         {
383             /* Now add the forces, this is local */
384             sum_forces(f, fr->f_novirsum);
385
386             if (EEL_FULL(fr->eeltype))
387             {
388                 /* Add the mesh contribution to the virial */
389                 m_add(vir_force, fr->vir_el_recip, vir_force);
390             }
391             if (EVDW_PME(fr->vdwtype))
392             {
393                 /* Add the mesh contribution to the virial */
394                 m_add(vir_force, fr->vir_lj_recip, vir_force);
395             }
396             if (debug)
397             {
398                 pr_rvecs(debug, 0, "vir_force", vir_force, DIM);
399             }
400         }
401     }
402
403     if (fr->print_force >= 0)
404     {
405         print_large_forces(stderr, mdatoms, cr, step, fr->print_force, x, f);
406     }
407 }
408
409 static void do_nb_verlet(t_forcerec *fr,
410                          interaction_const_t *ic,
411                          gmx_enerdata_t *enerd,
412                          int flags, int ilocality,
413                          int clearF,
414                          t_nrnb *nrnb,
415                          gmx_wallcycle_t wcycle)
416 {
417     int                        enr_nbnxn_kernel_ljc, enr_nbnxn_kernel_lj;
418     nonbonded_verlet_group_t  *nbvg;
419     gmx_bool                   bUsingGpuKernels;
420
421     if (!(flags & GMX_FORCE_NONBONDED))
422     {
423         /* skip non-bonded calculation */
424         return;
425     }
426
427     nbvg = &fr->nbv->grp[ilocality];
428
429     /* GPU kernel launch overhead is already timed separately */
430     if (fr->cutoff_scheme != ecutsVERLET)
431     {
432         gmx_incons("Invalid cut-off scheme passed!");
433     }
434
435     bUsingGpuKernels = (nbvg->kernel_type == nbnxnk8x8x8_GPU);
436
437     if (!bUsingGpuKernels)
438     {
439         wallcycle_sub_start(wcycle, ewcsNONBONDED);
440     }
441     switch (nbvg->kernel_type)
442     {
443         case nbnxnk4x4_PlainC:
444             nbnxn_kernel_ref(&nbvg->nbl_lists,
445                              nbvg->nbat, ic,
446                              fr->shift_vec,
447                              flags,
448                              clearF,
449                              fr->fshift[0],
450                              enerd->grpp.ener[egCOULSR],
451                              fr->bBHAM ?
452                              enerd->grpp.ener[egBHAMSR] :
453                              enerd->grpp.ener[egLJSR]);
454             break;
455
456         case nbnxnk4xN_SIMD_4xN:
457             nbnxn_kernel_simd_4xn(&nbvg->nbl_lists,
458                                   nbvg->nbat, ic,
459                                   nbvg->ewald_excl,
460                                   fr->shift_vec,
461                                   flags,
462                                   clearF,
463                                   fr->fshift[0],
464                                   enerd->grpp.ener[egCOULSR],
465                                   fr->bBHAM ?
466                                   enerd->grpp.ener[egBHAMSR] :
467                                   enerd->grpp.ener[egLJSR]);
468             break;
469         case nbnxnk4xN_SIMD_2xNN:
470             nbnxn_kernel_simd_2xnn(&nbvg->nbl_lists,
471                                    nbvg->nbat, ic,
472                                    nbvg->ewald_excl,
473                                    fr->shift_vec,
474                                    flags,
475                                    clearF,
476                                    fr->fshift[0],
477                                    enerd->grpp.ener[egCOULSR],
478                                    fr->bBHAM ?
479                                    enerd->grpp.ener[egBHAMSR] :
480                                    enerd->grpp.ener[egLJSR]);
481             break;
482
483         case nbnxnk8x8x8_GPU:
484             nbnxn_gpu_launch_kernel(fr->nbv->gpu_nbv, nbvg->nbat, flags, ilocality);
485             break;
486
487         case nbnxnk8x8x8_PlainC:
488             nbnxn_kernel_gpu_ref(nbvg->nbl_lists.nbl[0],
489                                  nbvg->nbat, ic,
490                                  fr->shift_vec,
491                                  flags,
492                                  clearF,
493                                  nbvg->nbat->out[0].f,
494                                  fr->fshift[0],
495                                  enerd->grpp.ener[egCOULSR],
496                                  fr->bBHAM ?
497                                  enerd->grpp.ener[egBHAMSR] :
498                                  enerd->grpp.ener[egLJSR]);
499             break;
500
501         default:
502             gmx_incons("Invalid nonbonded kernel type passed!");
503
504     }
505     if (!bUsingGpuKernels)
506     {
507         wallcycle_sub_stop(wcycle, ewcsNONBONDED);
508     }
509
510     if (EEL_RF(ic->eeltype) || ic->eeltype == eelCUT)
511     {
512         enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_RF;
513     }
514     else if ((!bUsingGpuKernels && nbvg->ewald_excl == ewaldexclAnalytical) ||
515              (bUsingGpuKernels && nbnxn_gpu_is_kernel_ewald_analytical(fr->nbv->gpu_nbv)))
516     {
517         enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_EWALD;
518     }
519     else
520     {
521         enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_TAB;
522     }
523     enr_nbnxn_kernel_lj = eNR_NBNXN_LJ;
524     if (flags & GMX_FORCE_ENERGY)
525     {
526         /* In eNR_??? the nbnxn F+E kernels are always the F kernel + 1 */
527         enr_nbnxn_kernel_ljc += 1;
528         enr_nbnxn_kernel_lj  += 1;
529     }
530
531     inc_nrnb(nrnb, enr_nbnxn_kernel_ljc,
532              nbvg->nbl_lists.natpair_ljq);
533     inc_nrnb(nrnb, enr_nbnxn_kernel_lj,
534              nbvg->nbl_lists.natpair_lj);
535     /* The Coulomb-only kernels are offset -eNR_NBNXN_LJ_RF+eNR_NBNXN_RF */
536     inc_nrnb(nrnb, enr_nbnxn_kernel_ljc-eNR_NBNXN_LJ_RF+eNR_NBNXN_RF,
537              nbvg->nbl_lists.natpair_q);
538
539     if (ic->vdw_modifier == eintmodFORCESWITCH)
540     {
541         /* We add up the switch cost separately */
542         inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_FSW+((flags & GMX_FORCE_ENERGY) ? 1 : 0),
543                  nbvg->nbl_lists.natpair_ljq + nbvg->nbl_lists.natpair_lj);
544     }
545     if (ic->vdw_modifier == eintmodPOTSWITCH)
546     {
547         /* We add up the switch cost separately */
548         inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_PSW+((flags & GMX_FORCE_ENERGY) ? 1 : 0),
549                  nbvg->nbl_lists.natpair_ljq + nbvg->nbl_lists.natpair_lj);
550     }
551     if (ic->vdwtype == evdwPME)
552     {
553         /* We add up the LJ Ewald cost separately */
554         inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_EWALD+((flags & GMX_FORCE_ENERGY) ? 1 : 0),
555                  nbvg->nbl_lists.natpair_ljq + nbvg->nbl_lists.natpair_lj);
556     }
557 }
558
559 static void do_nb_verlet_fep(nbnxn_pairlist_set_t *nbl_lists,
560                              t_forcerec           *fr,
561                              rvec                  x[],
562                              rvec                  f[],
563                              t_mdatoms            *mdatoms,
564                              t_lambda             *fepvals,
565                              real                 *lambda,
566                              gmx_enerdata_t       *enerd,
567                              int                   flags,
568                              t_nrnb               *nrnb,
569                              gmx_wallcycle_t       wcycle)
570 {
571     int              donb_flags;
572     nb_kernel_data_t kernel_data;
573     real             lam_i[efptNR];
574     real             dvdl_nb[efptNR];
575     int              th;
576     int              i, j;
577
578     donb_flags = 0;
579     /* Add short-range interactions */
580     donb_flags |= GMX_NONBONDED_DO_SR;
581
582     /* Currently all group scheme kernels always calculate (shift-)forces */
583     if (flags & GMX_FORCE_FORCES)
584     {
585         donb_flags |= GMX_NONBONDED_DO_FORCE;
586     }
587     if (flags & GMX_FORCE_VIRIAL)
588     {
589         donb_flags |= GMX_NONBONDED_DO_SHIFTFORCE;
590     }
591     if (flags & GMX_FORCE_ENERGY)
592     {
593         donb_flags |= GMX_NONBONDED_DO_POTENTIAL;
594     }
595
596     kernel_data.flags  = donb_flags;
597     kernel_data.lambda = lambda;
598     kernel_data.dvdl   = dvdl_nb;
599
600     kernel_data.energygrp_elec = enerd->grpp.ener[egCOULSR];
601     kernel_data.energygrp_vdw  = enerd->grpp.ener[egLJSR];
602
603     /* reset free energy components */
604     for (i = 0; i < efptNR; i++)
605     {
606         dvdl_nb[i]  = 0;
607     }
608
609     assert(gmx_omp_nthreads_get(emntNonbonded) == nbl_lists->nnbl);
610
611     wallcycle_sub_start(wcycle, ewcsNONBONDED);
612 #pragma omp parallel for schedule(static) num_threads(nbl_lists->nnbl)
613     for (th = 0; th < nbl_lists->nnbl; th++)
614     {
615         try
616         {
617             gmx_nb_free_energy_kernel(nbl_lists->nbl_fep[th],
618                                       x, f, fr, mdatoms, &kernel_data, nrnb);
619         }
620         GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
621     }
622
623     if (fepvals->sc_alpha != 0)
624     {
625         enerd->dvdl_nonlin[efptVDW]  += dvdl_nb[efptVDW];
626         enerd->dvdl_nonlin[efptCOUL] += dvdl_nb[efptCOUL];
627     }
628     else
629     {
630         enerd->dvdl_lin[efptVDW]  += dvdl_nb[efptVDW];
631         enerd->dvdl_lin[efptCOUL] += dvdl_nb[efptCOUL];
632     }
633
634     /* If we do foreign lambda and we have soft-core interactions
635      * we have to recalculate the (non-linear) energies contributions.
636      */
637     if (fepvals->n_lambda > 0 && (flags & GMX_FORCE_DHDL) && fepvals->sc_alpha != 0)
638     {
639         kernel_data.flags          = (donb_flags & ~(GMX_NONBONDED_DO_FORCE | GMX_NONBONDED_DO_SHIFTFORCE)) | GMX_NONBONDED_DO_FOREIGNLAMBDA;
640         kernel_data.lambda         = lam_i;
641         kernel_data.energygrp_elec = enerd->foreign_grpp.ener[egCOULSR];
642         kernel_data.energygrp_vdw  = enerd->foreign_grpp.ener[egLJSR];
643         /* Note that we add to kernel_data.dvdl, but ignore the result */
644
645         for (i = 0; i < enerd->n_lambda; i++)
646         {
647             for (j = 0; j < efptNR; j++)
648             {
649                 lam_i[j] = (i == 0 ? lambda[j] : fepvals->all_lambda[j][i-1]);
650             }
651             reset_foreign_enerdata(enerd);
652 #pragma omp parallel for schedule(static) num_threads(nbl_lists->nnbl)
653             for (th = 0; th < nbl_lists->nnbl; th++)
654             {
655                 try
656                 {
657                     gmx_nb_free_energy_kernel(nbl_lists->nbl_fep[th],
658                                               x, f, fr, mdatoms, &kernel_data, nrnb);
659                 }
660                 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
661             }
662
663             sum_epot(&(enerd->foreign_grpp), enerd->foreign_term);
664             enerd->enerpart_lambda[i] += enerd->foreign_term[F_EPOT];
665         }
666     }
667
668     wallcycle_sub_stop(wcycle, ewcsNONBONDED);
669 }
670
671 gmx_bool use_GPU(const nonbonded_verlet_t *nbv)
672 {
673     return nbv != nullptr && nbv->bUseGPU;
674 }
675
676 static gmx_inline void clear_rvecs_omp(int n, rvec v[])
677 {
678     int nth = gmx_omp_nthreads_get_simple_rvec_task(emntDefault, n);
679
680     /* Note that we would like to avoid this conditional by putting it
681      * into the omp pragma instead, but then we still take the full
682      * omp parallel for overhead (at least with gcc5).
683      */
684     if (nth == 1)
685     {
686         for (int i = 0; i < n; i++)
687         {
688             clear_rvec(v[i]);
689         }
690     }
691     else
692     {
693 #pragma omp parallel for num_threads(nth) schedule(static)
694         for (int i = 0; i < n; i++)
695         {
696             clear_rvec(v[i]);
697         }
698     }
699 }
700
701 /*! \brief  This routine checks if the potential energy is finite.
702  *
703  * Note that passing this check does not guarantee finite forces,
704  * since those use slightly different arithmetics. But in most cases
705  * there is just a narrow coordinate range where forces are not finite
706  * and energies are finite.
707  *
708  * \param[in] enerd  The energy data; the non-bonded group energies need to be added in here before calling this routine
709  */
710 static void checkPotentialEnergyValidity(const gmx_enerdata_t *enerd)
711 {
712     if (!std::isfinite(enerd->term[F_EPOT]))
713     {
714         gmx_fatal(FARGS, "The total potential energy is %g, which is not finite. The LJ and electrostatic contributions to the energy are %g and %g, respectively. A non-finite potential energy can be caused by overlapping interactions in bonded interactions or very large or NaN coordinate values. Usually this is caused by a badly or non-equilibrated initial configuration or incorrect interactions or parameters in the topology.",
715                   enerd->term[F_EPOT],
716                   enerd->term[F_LJ],
717                   enerd->term[F_COUL_SR]);
718     }
719 }
720
721 void do_force_cutsVERLET(FILE *fplog, t_commrec *cr,
722                          t_inputrec *inputrec,
723                          gmx_int64_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
724                          gmx_localtop_t *top,
725                          gmx_groups_t gmx_unused *groups,
726                          matrix box, rvec x[], history_t *hist,
727                          PaddedRVecVector *force,
728                          tensor vir_force,
729                          t_mdatoms *mdatoms,
730                          gmx_enerdata_t *enerd, t_fcdata *fcd,
731                          real *lambda, t_graph *graph,
732                          t_forcerec *fr, interaction_const_t *ic,
733                          gmx_vsite_t *vsite, rvec mu_tot,
734                          double t, gmx_edsam_t ed,
735                          gmx_bool bBornRadii,
736                          int flags)
737 {
738     int                 cg1, i, j;
739     double              mu[2*DIM];
740     gmx_bool            bStateChanged, bNS, bFillGrid, bCalcCGCM;
741     gmx_bool            bDoForces, bUseGPU, bUseOrEmulGPU;
742     gmx_bool            bDiffKernels = FALSE;
743     rvec                vzero, box_diag;
744     float               cycles_pme, cycles_force, cycles_wait_gpu;
745     /* TODO To avoid loss of precision, float can't be used for a
746      * cycle count. Build an object that can do this right and perhaps
747      * also be used by gmx_wallcycle_t */
748     gmx_cycles_t        cycleCountBeforeLocalWorkCompletes = 0;
749     nonbonded_verlet_t *nbv;
750
751     cycles_force    = 0;
752     cycles_wait_gpu = 0;
753     nbv             = fr->nbv;
754
755     const int start  = 0;
756     const int homenr = mdatoms->homenr;
757
758     clear_mat(vir_force);
759
760     if (DOMAINDECOMP(cr))
761     {
762         cg1 = cr->dd->ncg_tot;
763     }
764     else
765     {
766         cg1 = top->cgs.nr;
767     }
768     if (fr->n_tpi > 0)
769     {
770         cg1--;
771     }
772
773     bStateChanged = (flags & GMX_FORCE_STATECHANGED);
774     bNS           = (flags & GMX_FORCE_NS) && (fr->bAllvsAll == FALSE);
775     bFillGrid     = (bNS && bStateChanged);
776     bCalcCGCM     = (bFillGrid && !DOMAINDECOMP(cr));
777     bDoForces     = (flags & GMX_FORCE_FORCES);
778     bUseGPU       = fr->nbv->bUseGPU;
779     bUseOrEmulGPU = bUseGPU || (nbv->grp[0].kernel_type == nbnxnk8x8x8_PlainC);
780
781     if (bStateChanged)
782     {
783         update_forcerec(fr, box);
784
785         if (inputrecNeedMutot(inputrec))
786         {
787             /* Calculate total (local) dipole moment in a temporary common array.
788              * This makes it possible to sum them over nodes faster.
789              */
790             calc_mu(start, homenr,
791                     x, mdatoms->chargeA, mdatoms->chargeB, mdatoms->nChargePerturbed,
792                     mu, mu+DIM);
793         }
794     }
795
796     if (fr->ePBC != epbcNONE)
797     {
798         /* Compute shift vectors every step,
799          * because of pressure coupling or box deformation!
800          */
801         if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
802         {
803             calc_shifts(box, fr->shift_vec);
804         }
805
806         if (bCalcCGCM)
807         {
808             put_atoms_in_box_omp(fr->ePBC, box, homenr, x);
809             inc_nrnb(nrnb, eNR_SHIFTX, homenr);
810         }
811         else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph)
812         {
813             unshift_self(graph, box, x);
814         }
815     }
816
817     nbnxn_atomdata_copy_shiftvec(flags & GMX_FORCE_DYNAMICBOX,
818                                  fr->shift_vec, nbv->grp[0].nbat);
819
820 #if GMX_MPI
821     if (!(cr->duty & DUTY_PME))
822     {
823         gmx_bool bBS;
824         matrix   boxs;
825
826         /* Send particle coordinates to the pme nodes.
827          * Since this is only implemented for domain decomposition
828          * and domain decomposition does not use the graph,
829          * we do not need to worry about shifting.
830          */
831
832         wallcycle_start(wcycle, ewcPP_PMESENDX);
833
834         bBS = (inputrec->nwall == 2);
835         if (bBS)
836         {
837             copy_mat(box, boxs);
838             svmul(inputrec->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
839         }
840
841         gmx_pme_send_coordinates(cr, bBS ? boxs : box, x,
842                                  lambda[efptCOUL], lambda[efptVDW],
843                                  (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)),
844                                  step);
845
846         wallcycle_stop(wcycle, ewcPP_PMESENDX);
847     }
848 #endif /* GMX_MPI */
849
850     /* do gridding for pair search */
851     if (bNS)
852     {
853         if (graph && bStateChanged)
854         {
855             /* Calculate intramolecular shift vectors to make molecules whole */
856             mk_mshift(fplog, graph, fr->ePBC, box, x);
857         }
858
859         clear_rvec(vzero);
860         box_diag[XX] = box[XX][XX];
861         box_diag[YY] = box[YY][YY];
862         box_diag[ZZ] = box[ZZ][ZZ];
863
864         wallcycle_start(wcycle, ewcNS);
865         if (!fr->bDomDec)
866         {
867             wallcycle_sub_start(wcycle, ewcsNBS_GRID_LOCAL);
868             nbnxn_put_on_grid(nbv->nbs, fr->ePBC, box,
869                               0, vzero, box_diag,
870                               0, mdatoms->homenr, -1, fr->cginfo, x,
871                               0, nullptr,
872                               nbv->grp[eintLocal].kernel_type,
873                               nbv->grp[eintLocal].nbat);
874             wallcycle_sub_stop(wcycle, ewcsNBS_GRID_LOCAL);
875         }
876         else
877         {
878             wallcycle_sub_start(wcycle, ewcsNBS_GRID_NONLOCAL);
879             nbnxn_put_on_grid_nonlocal(nbv->nbs, domdec_zones(cr->dd),
880                                        fr->cginfo, x,
881                                        nbv->grp[eintNonlocal].kernel_type,
882                                        nbv->grp[eintNonlocal].nbat);
883             wallcycle_sub_stop(wcycle, ewcsNBS_GRID_NONLOCAL);
884         }
885
886         if (nbv->ngrp == 1 ||
887             nbv->grp[eintNonlocal].nbat == nbv->grp[eintLocal].nbat)
888         {
889             nbnxn_atomdata_set(nbv->grp[eintLocal].nbat, eatAll,
890                                nbv->nbs, mdatoms, fr->cginfo);
891         }
892         else
893         {
894             nbnxn_atomdata_set(nbv->grp[eintLocal].nbat, eatLocal,
895                                nbv->nbs, mdatoms, fr->cginfo);
896             nbnxn_atomdata_set(nbv->grp[eintNonlocal].nbat, eatAll,
897                                nbv->nbs, mdatoms, fr->cginfo);
898         }
899         wallcycle_stop(wcycle, ewcNS);
900     }
901
902     /* initialize the GPU atom data and copy shift vector */
903     if (bUseGPU)
904     {
905         if (bNS)
906         {
907             wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
908             nbnxn_gpu_init_atomdata(nbv->gpu_nbv, nbv->grp[eintLocal].nbat);
909             wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
910         }
911
912         wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
913         nbnxn_gpu_upload_shiftvec(nbv->gpu_nbv, nbv->grp[eintLocal].nbat);
914         wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
915     }
916
917     /* do local pair search */
918     if (bNS)
919     {
920         wallcycle_start_nocount(wcycle, ewcNS);
921         wallcycle_sub_start(wcycle, ewcsNBS_SEARCH_LOCAL);
922         nbnxn_make_pairlist(nbv->nbs, nbv->grp[eintLocal].nbat,
923                             &top->excls,
924                             ic->rlist,
925                             nbv->min_ci_balanced,
926                             &nbv->grp[eintLocal].nbl_lists,
927                             eintLocal,
928                             nbv->grp[eintLocal].kernel_type,
929                             nrnb);
930         wallcycle_sub_stop(wcycle, ewcsNBS_SEARCH_LOCAL);
931
932         if (bUseGPU)
933         {
934             /* initialize local pair-list on the GPU */
935             nbnxn_gpu_init_pairlist(nbv->gpu_nbv,
936                                     nbv->grp[eintLocal].nbl_lists.nbl[0],
937                                     eintLocal);
938         }
939         wallcycle_stop(wcycle, ewcNS);
940     }
941     else
942     {
943         wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
944         wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
945         nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatLocal, FALSE, x,
946                                         nbv->grp[eintLocal].nbat);
947         wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
948         wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
949     }
950
951     if (bUseGPU)
952     {
953         wallcycle_start(wcycle, ewcLAUNCH_GPU_NB);
954         /* launch local nonbonded F on GPU */
955         do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFNo,
956                      nrnb, wcycle);
957         wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
958     }
959
960     /* Communicate coordinates and sum dipole if necessary +
961        do non-local pair search */
962     if (DOMAINDECOMP(cr))
963     {
964         bDiffKernels = (nbv->grp[eintNonlocal].kernel_type !=
965                         nbv->grp[eintLocal].kernel_type);
966
967         if (bDiffKernels)
968         {
969             /* With GPU+CPU non-bonded calculations we need to copy
970              * the local coordinates to the non-local nbat struct
971              * (in CPU format) as the non-local kernel call also
972              * calculates the local - non-local interactions.
973              */
974             wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
975             wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
976             nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatLocal, TRUE, x,
977                                             nbv->grp[eintNonlocal].nbat);
978             wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
979             wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
980         }
981
982         if (bNS)
983         {
984             wallcycle_start_nocount(wcycle, ewcNS);
985             wallcycle_sub_start(wcycle, ewcsNBS_SEARCH_NONLOCAL);
986
987             if (bDiffKernels)
988             {
989                 nbnxn_grid_add_simple(nbv->nbs, nbv->grp[eintNonlocal].nbat);
990             }
991
992             nbnxn_make_pairlist(nbv->nbs, nbv->grp[eintNonlocal].nbat,
993                                 &top->excls,
994                                 ic->rlist,
995                                 nbv->min_ci_balanced,
996                                 &nbv->grp[eintNonlocal].nbl_lists,
997                                 eintNonlocal,
998                                 nbv->grp[eintNonlocal].kernel_type,
999                                 nrnb);
1000
1001             wallcycle_sub_stop(wcycle, ewcsNBS_SEARCH_NONLOCAL);
1002
1003             if (nbv->grp[eintNonlocal].kernel_type == nbnxnk8x8x8_GPU)
1004             {
1005                 /* initialize non-local pair-list on the GPU */
1006                 nbnxn_gpu_init_pairlist(nbv->gpu_nbv,
1007                                         nbv->grp[eintNonlocal].nbl_lists.nbl[0],
1008                                         eintNonlocal);
1009             }
1010             wallcycle_stop(wcycle, ewcNS);
1011         }
1012         else
1013         {
1014             wallcycle_start(wcycle, ewcMOVEX);
1015             dd_move_x(cr->dd, box, x);
1016             wallcycle_stop(wcycle, ewcMOVEX);
1017
1018             wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1019             wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
1020             nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatNonlocal, FALSE, x,
1021                                             nbv->grp[eintNonlocal].nbat);
1022             wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
1023             cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1024         }
1025
1026         if (bUseGPU && !bDiffKernels)
1027         {
1028             wallcycle_start(wcycle, ewcLAUNCH_GPU_NB);
1029             /* launch non-local nonbonded F on GPU */
1030             do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFNo,
1031                          nrnb, wcycle);
1032             cycles_force += wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1033         }
1034     }
1035
1036     if (bUseGPU)
1037     {
1038         /* launch D2H copy-back F */
1039         wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1040         if (DOMAINDECOMP(cr) && !bDiffKernels)
1041         {
1042             nbnxn_gpu_launch_cpyback(nbv->gpu_nbv, nbv->grp[eintNonlocal].nbat,
1043                                      flags, eatNonlocal);
1044         }
1045         nbnxn_gpu_launch_cpyback(nbv->gpu_nbv, nbv->grp[eintLocal].nbat,
1046                                  flags, eatLocal);
1047         cycles_force += wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1048     }
1049
1050     if (bStateChanged && inputrecNeedMutot(inputrec))
1051     {
1052         if (PAR(cr))
1053         {
1054             gmx_sumd(2*DIM, mu, cr);
1055         }
1056
1057         for (i = 0; i < 2; i++)
1058         {
1059             for (j = 0; j < DIM; j++)
1060             {
1061                 fr->mu_tot[i][j] = mu[i*DIM + j];
1062             }
1063         }
1064     }
1065     if (fr->efep == efepNO)
1066     {
1067         copy_rvec(fr->mu_tot[0], mu_tot);
1068     }
1069     else
1070     {
1071         for (j = 0; j < DIM; j++)
1072         {
1073             mu_tot[j] =
1074                 (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] +
1075                 lambda[efptCOUL]*fr->mu_tot[1][j];
1076         }
1077     }
1078
1079     /* Reset energies */
1080     reset_enerdata(enerd);
1081     clear_rvecs(SHIFTS, fr->fshift);
1082
1083     if (DOMAINDECOMP(cr) && !(cr->duty & DUTY_PME))
1084     {
1085         wallcycle_start(wcycle, ewcPPDURINGPME);
1086         dd_force_flop_start(cr->dd, nrnb);
1087     }
1088
1089     if (inputrec->bRot)
1090     {
1091         /* Enforced rotation has its own cycle counter that starts after the collective
1092          * coordinates have been communicated. It is added to ddCyclF to allow
1093          * for proper load-balancing */
1094         wallcycle_start(wcycle, ewcROT);
1095         do_rotation(cr, inputrec, box, x, t, step, wcycle, bNS);
1096         wallcycle_stop(wcycle, ewcROT);
1097     }
1098
1099     /* Temporary solution until all routines take PaddedRVecVector */
1100     rvec *f = as_rvec_array(force->data());
1101
1102     /* Start the force cycle counter.
1103      * This counter is stopped after do_force_lowlevel.
1104      * No parallel communication should occur while this counter is running,
1105      * since that will interfere with the dynamic load balancing.
1106      */
1107     wallcycle_start(wcycle, ewcFORCE);
1108     if (bDoForces)
1109     {
1110         /* Reset forces for which the virial is calculated separately:
1111          * PME/Ewald forces if necessary */
1112         if (fr->bF_NoVirSum)
1113         {
1114             if (flags & GMX_FORCE_VIRIAL)
1115             {
1116                 fr->f_novirsum = fr->forceBufferNoVirialSummation;
1117             }
1118             else
1119             {
1120                 /* We are not calculating the pressure so we do not need
1121                  * a separate array for forces that do not contribute
1122                  * to the pressure.
1123                  */
1124                 fr->f_novirsum = force;
1125             }
1126         }
1127
1128         if (fr->bF_NoVirSum)
1129         {
1130             if (flags & GMX_FORCE_VIRIAL)
1131             {
1132                 /* TODO: remove this - 1 when padding is properly implemented */
1133                 clear_rvecs_omp(fr->f_novirsum->size() - 1,
1134                                 as_rvec_array(fr->f_novirsum->data()));
1135             }
1136         }
1137         /* Clear the short- and long-range forces */
1138         clear_rvecs_omp(fr->natoms_force_constr, f);
1139
1140         clear_rvec(fr->vir_diag_posres);
1141     }
1142
1143     if (inputrec->bPull && pull_have_constraint(inputrec->pull_work))
1144     {
1145         clear_pull_forces(inputrec->pull_work);
1146     }
1147
1148     /* We calculate the non-bonded forces, when done on the CPU, here.
1149      * We do this before calling do_force_lowlevel, because in that
1150      * function, the listed forces are calculated before PME, which
1151      * does communication.  With this order, non-bonded and listed
1152      * force calculation imbalance can be balanced out by the domain
1153      * decomposition load balancing.
1154      */
1155
1156     if (!bUseOrEmulGPU)
1157     {
1158         /* Maybe we should move this into do_force_lowlevel */
1159         do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFYes,
1160                      nrnb, wcycle);
1161     }
1162
1163     if (fr->efep != efepNO)
1164     {
1165         /* Calculate the local and non-local free energy interactions here.
1166          * Happens here on the CPU both with and without GPU.
1167          */
1168         if (fr->nbv->grp[eintLocal].nbl_lists.nbl_fep[0]->nrj > 0)
1169         {
1170             do_nb_verlet_fep(&fr->nbv->grp[eintLocal].nbl_lists,
1171                              fr, x, f, mdatoms,
1172                              inputrec->fepvals, lambda,
1173                              enerd, flags, nrnb, wcycle);
1174         }
1175
1176         if (DOMAINDECOMP(cr) &&
1177             fr->nbv->grp[eintNonlocal].nbl_lists.nbl_fep[0]->nrj > 0)
1178         {
1179             do_nb_verlet_fep(&fr->nbv->grp[eintNonlocal].nbl_lists,
1180                              fr, x, f, mdatoms,
1181                              inputrec->fepvals, lambda,
1182                              enerd, flags, nrnb, wcycle);
1183         }
1184     }
1185
1186     if (!bUseOrEmulGPU || bDiffKernels)
1187     {
1188         int aloc;
1189
1190         if (DOMAINDECOMP(cr))
1191         {
1192             do_nb_verlet(fr, ic, enerd, flags, eintNonlocal,
1193                          bDiffKernels ? enbvClearFYes : enbvClearFNo,
1194                          nrnb, wcycle);
1195         }
1196
1197         if (!bUseOrEmulGPU)
1198         {
1199             aloc = eintLocal;
1200         }
1201         else
1202         {
1203             aloc = eintNonlocal;
1204         }
1205
1206         /* Add all the non-bonded force to the normal force array.
1207          * This can be split into a local and a non-local part when overlapping
1208          * communication with calculation with domain decomposition.
1209          */
1210         cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1211         wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1212         wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1213         nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatAll, nbv->grp[aloc].nbat, f);
1214         wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1215         cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1216         wallcycle_start_nocount(wcycle, ewcFORCE);
1217
1218         /* if there are multiple fshift output buffers reduce them */
1219         if ((flags & GMX_FORCE_VIRIAL) &&
1220             nbv->grp[aloc].nbl_lists.nnbl > 1)
1221         {
1222             /* This is not in a subcounter because it takes a
1223                negligible and constant-sized amount of time */
1224             nbnxn_atomdata_add_nbat_fshift_to_fshift(nbv->grp[aloc].nbat,
1225                                                      fr->fshift);
1226         }
1227     }
1228
1229     /* update QMMMrec, if necessary */
1230     if (fr->bQMMM)
1231     {
1232         update_QMMMrec(cr, fr, x, mdatoms, box, top);
1233     }
1234
1235     /* Compute the bonded and non-bonded energies and optionally forces */
1236     do_force_lowlevel(fr, inputrec, &(top->idef),
1237                       cr, nrnb, wcycle, mdatoms,
1238                       x, hist, f, enerd, fcd, top, fr->born,
1239                       bBornRadii, box,
1240                       inputrec->fepvals, lambda, graph, &(top->excls), fr->mu_tot,
1241                       flags, &cycles_pme);
1242
1243     cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1244
1245     if (ed)
1246     {
1247         do_flood(cr, inputrec, x, f, ed, box, step, bNS);
1248     }
1249
1250     if (bUseOrEmulGPU && !bDiffKernels)
1251     {
1252         /* wait for non-local forces (or calculate in emulation mode) */
1253         if (DOMAINDECOMP(cr))
1254         {
1255             if (bUseGPU)
1256             {
1257                 float cycles_tmp;
1258
1259                 wallcycle_start(wcycle, ewcWAIT_GPU_NB_NL);
1260                 nbnxn_gpu_wait_for_gpu(nbv->gpu_nbv,
1261                                        flags, eatNonlocal,
1262                                        enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1263                                        fr->fshift);
1264                 cycles_tmp       = wallcycle_stop(wcycle, ewcWAIT_GPU_NB_NL);
1265                 cycles_wait_gpu += cycles_tmp;
1266                 cycles_force    += cycles_tmp;
1267             }
1268             else
1269             {
1270                 wallcycle_start_nocount(wcycle, ewcFORCE);
1271                 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFYes,
1272                              nrnb, wcycle);
1273                 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1274             }
1275             wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1276             wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1277             /* skip the reduction if there was no non-local work to do */
1278             if (nbv->grp[eintNonlocal].nbl_lists.nbl[0]->nsci > 0)
1279             {
1280                 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatNonlocal,
1281                                                nbv->grp[eintNonlocal].nbat, f);
1282             }
1283             wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1284             cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1285         }
1286     }
1287
1288     if (bDoForces && DOMAINDECOMP(cr))
1289     {
1290         if (bUseGPU)
1291         {
1292             /* We are done with the CPU compute, but the GPU local non-bonded
1293              * kernel can still be running while we communicate the forces.
1294              * We start a counter here, so we can, hopefully, time the rest
1295              * of the GPU kernel execution and data transfer.
1296              */
1297             cycleCountBeforeLocalWorkCompletes = gmx_cycles_read();
1298         }
1299
1300         /* Communicate the forces */
1301         wallcycle_start(wcycle, ewcMOVEF);
1302         dd_move_f(cr->dd, f, fr->fshift);
1303         wallcycle_stop(wcycle, ewcMOVEF);
1304     }
1305
1306     if (bUseOrEmulGPU)
1307     {
1308         /* wait for local forces (or calculate in emulation mode) */
1309         if (bUseGPU)
1310         {
1311             float       cycles_tmp, cycles_wait_est;
1312             /* Measured overhead on CUDA and OpenCL with(out) GPU sharing
1313              * is between 0.5 and 1.5 Mcycles. So 2 MCycles is an overestimate,
1314              * but even with a step of 0.1 ms the difference is less than 1%
1315              * of the step time.
1316              */
1317             const float gpuWaitApiOverheadMargin = 2e6f; /* cycles */
1318
1319             wallcycle_start(wcycle, ewcWAIT_GPU_NB_L);
1320             nbnxn_gpu_wait_for_gpu(nbv->gpu_nbv,
1321                                    flags, eatLocal,
1322                                    enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1323                                    fr->fshift);
1324             cycles_tmp      = wallcycle_stop(wcycle, ewcWAIT_GPU_NB_L);
1325
1326             if (bDoForces && DOMAINDECOMP(cr))
1327             {
1328                 cycles_wait_est = gmx_cycles_read() - cycleCountBeforeLocalWorkCompletes;
1329
1330                 if (cycles_tmp < gpuWaitApiOverheadMargin)
1331                 {
1332                     /* We measured few cycles, it could be that the kernel
1333                      * and transfer finished earlier and there was no actual
1334                      * wait time, only API call overhead.
1335                      * Then the actual time could be anywhere between 0 and
1336                      * cycles_wait_est. As a compromise, we use half the time.
1337                      */
1338                     cycles_wait_est *= 0.5f;
1339                 }
1340             }
1341             else
1342             {
1343                 /* No force communication so we actually timed the wait */
1344                 cycles_wait_est = cycles_tmp;
1345             }
1346             /* Even though this is after dd_move_f, the actual task we are
1347              * waiting for runs asynchronously with dd_move_f and we usually
1348              * have nothing to balance it with, so we can and should add
1349              * the time to the force time for load balancing.
1350              */
1351             cycles_force    += cycles_wait_est;
1352             cycles_wait_gpu += cycles_wait_est;
1353
1354             /* now clear the GPU outputs while we finish the step on the CPU */
1355             wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1356             nbnxn_gpu_clear_outputs(nbv->gpu_nbv, flags);
1357             wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1358         }
1359         else
1360         {
1361             wallcycle_start_nocount(wcycle, ewcFORCE);
1362             do_nb_verlet(fr, ic, enerd, flags, eintLocal,
1363                          DOMAINDECOMP(cr) ? enbvClearFNo : enbvClearFYes,
1364                          nrnb, wcycle);
1365             wallcycle_stop(wcycle, ewcFORCE);
1366         }
1367         wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1368         wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1369         nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatLocal,
1370                                        nbv->grp[eintLocal].nbat, f);
1371         wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1372         wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1373     }
1374
1375     if (DOMAINDECOMP(cr))
1376     {
1377         dd_force_flop_stop(cr->dd, nrnb);
1378         if (wcycle)
1379         {
1380             dd_cycles_add(cr->dd, cycles_force-cycles_pme, ddCyclF);
1381             if (bUseGPU)
1382             {
1383                 dd_cycles_add(cr->dd, cycles_wait_gpu, ddCyclWaitGPU);
1384             }
1385         }
1386     }
1387
1388     if (bDoForces)
1389     {
1390         /* Compute forces due to electric field */
1391         if (fr->efield != nullptr)
1392         {
1393             fr->efield->calculateForces(cr, mdatoms, fr->f_novirsum, t);
1394         }
1395
1396         /* If we have NoVirSum forces, but we do not calculate the virial,
1397          * we sum fr->f_novirsum=f later.
1398          */
1399         if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1400         {
1401             wallcycle_start(wcycle, ewcVSITESPREAD);
1402             spread_vsite_f(vsite, x, f, fr->fshift, FALSE, nullptr, nrnb,
1403                            &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1404             wallcycle_stop(wcycle, ewcVSITESPREAD);
1405         }
1406
1407         if (flags & GMX_FORCE_VIRIAL)
1408         {
1409             /* Calculation of the virial must be done after vsites! */
1410             calc_virial(0, mdatoms->homenr, x, f,
1411                         vir_force, graph, box, nrnb, fr, inputrec->ePBC);
1412         }
1413     }
1414
1415     if (inputrec->bPull && pull_have_potential(inputrec->pull_work))
1416     {
1417         /* Since the COM pulling is always done mass-weighted, no forces are
1418          * applied to vsites and this call can be done after vsite spreading.
1419          */
1420         pull_potential_wrapper(cr, inputrec, box, x,
1421                                f, vir_force, mdatoms, enerd, lambda, t,
1422                                wcycle);
1423     }
1424
1425     /* Add the forces from enforced rotation potentials (if any) */
1426     if (inputrec->bRot)
1427     {
1428         wallcycle_start(wcycle, ewcROTadd);
1429         enerd->term[F_COM_PULL] += add_rot_forces(inputrec->rot, f, cr, step, t);
1430         wallcycle_stop(wcycle, ewcROTadd);
1431     }
1432
1433     /* Add forces from interactive molecular dynamics (IMD), if bIMD == TRUE. */
1434     IMD_apply_forces(inputrec->bIMD, inputrec->imd, cr, f, wcycle);
1435
1436     if (PAR(cr) && !(cr->duty & DUTY_PME))
1437     {
1438         /* In case of node-splitting, the PP nodes receive the long-range
1439          * forces, virial and energy from the PME nodes here.
1440          */
1441         pme_receive_force_ener(cr, wcycle, enerd, fr);
1442     }
1443
1444     if (bDoForces)
1445     {
1446         post_process_forces(cr, step, nrnb, wcycle,
1447                             top, box, x, f, vir_force, mdatoms, graph, fr, vsite,
1448                             flags);
1449     }
1450
1451     if (flags & GMX_FORCE_ENERGY)
1452     {
1453         /* Sum the potential energy terms from group contributions */
1454         sum_epot(&(enerd->grpp), enerd->term);
1455
1456         if (!EI_TPI(inputrec->eI))
1457         {
1458             checkPotentialEnergyValidity(enerd);
1459         }
1460     }
1461 }
1462
1463 void do_force_cutsGROUP(FILE *fplog, t_commrec *cr,
1464                         t_inputrec *inputrec,
1465                         gmx_int64_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
1466                         gmx_localtop_t *top,
1467                         gmx_groups_t *groups,
1468                         matrix box, rvec x[], history_t *hist,
1469                         PaddedRVecVector *force,
1470                         tensor vir_force,
1471                         t_mdatoms *mdatoms,
1472                         gmx_enerdata_t *enerd, t_fcdata *fcd,
1473                         real *lambda, t_graph *graph,
1474                         t_forcerec *fr, gmx_vsite_t *vsite, rvec mu_tot,
1475                         double t, gmx_edsam_t ed,
1476                         gmx_bool bBornRadii,
1477                         int flags)
1478 {
1479     int        cg0, cg1, i, j;
1480     double     mu[2*DIM];
1481     gmx_bool   bStateChanged, bNS, bFillGrid, bCalcCGCM;
1482     gmx_bool   bDoForces;
1483     float      cycles_pme, cycles_force;
1484
1485     const int  start  = 0;
1486     const int  homenr = mdatoms->homenr;
1487
1488     clear_mat(vir_force);
1489
1490     cg0 = 0;
1491     if (DOMAINDECOMP(cr))
1492     {
1493         cg1 = cr->dd->ncg_tot;
1494     }
1495     else
1496     {
1497         cg1 = top->cgs.nr;
1498     }
1499     if (fr->n_tpi > 0)
1500     {
1501         cg1--;
1502     }
1503
1504     bStateChanged  = (flags & GMX_FORCE_STATECHANGED);
1505     bNS            = (flags & GMX_FORCE_NS) && (fr->bAllvsAll == FALSE);
1506     /* Should we perform the long-range nonbonded evaluation inside the neighborsearching? */
1507     bFillGrid      = (bNS && bStateChanged);
1508     bCalcCGCM      = (bFillGrid && !DOMAINDECOMP(cr));
1509     bDoForces      = (flags & GMX_FORCE_FORCES);
1510
1511     if (bStateChanged)
1512     {
1513         update_forcerec(fr, box);
1514
1515         if (inputrecNeedMutot(inputrec))
1516         {
1517             /* Calculate total (local) dipole moment in a temporary common array.
1518              * This makes it possible to sum them over nodes faster.
1519              */
1520             calc_mu(start, homenr,
1521                     x, mdatoms->chargeA, mdatoms->chargeB, mdatoms->nChargePerturbed,
1522                     mu, mu+DIM);
1523         }
1524     }
1525
1526     if (fr->ePBC != epbcNONE)
1527     {
1528         /* Compute shift vectors every step,
1529          * because of pressure coupling or box deformation!
1530          */
1531         if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
1532         {
1533             calc_shifts(box, fr->shift_vec);
1534         }
1535
1536         if (bCalcCGCM)
1537         {
1538             put_charge_groups_in_box(fplog, cg0, cg1, fr->ePBC, box,
1539                                      &(top->cgs), x, fr->cg_cm);
1540             inc_nrnb(nrnb, eNR_CGCM, homenr);
1541             inc_nrnb(nrnb, eNR_RESETX, cg1-cg0);
1542         }
1543         else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph)
1544         {
1545             unshift_self(graph, box, x);
1546         }
1547     }
1548     else if (bCalcCGCM)
1549     {
1550         calc_cgcm(fplog, cg0, cg1, &(top->cgs), x, fr->cg_cm);
1551         inc_nrnb(nrnb, eNR_CGCM, homenr);
1552     }
1553
1554     if (bCalcCGCM && gmx_debug_at)
1555     {
1556         pr_rvecs(debug, 0, "cgcm", fr->cg_cm, top->cgs.nr);
1557     }
1558
1559 #if GMX_MPI
1560     if (!(cr->duty & DUTY_PME))
1561     {
1562         gmx_bool bBS;
1563         matrix   boxs;
1564
1565         /* Send particle coordinates to the pme nodes.
1566          * Since this is only implemented for domain decomposition
1567          * and domain decomposition does not use the graph,
1568          * we do not need to worry about shifting.
1569          */
1570
1571         wallcycle_start(wcycle, ewcPP_PMESENDX);
1572
1573         bBS = (inputrec->nwall == 2);
1574         if (bBS)
1575         {
1576             copy_mat(box, boxs);
1577             svmul(inputrec->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
1578         }
1579
1580         gmx_pme_send_coordinates(cr, bBS ? boxs : box, x,
1581                                  lambda[efptCOUL], lambda[efptVDW],
1582                                  (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)),
1583                                  step);
1584
1585         wallcycle_stop(wcycle, ewcPP_PMESENDX);
1586     }
1587 #endif /* GMX_MPI */
1588
1589     /* Communicate coordinates and sum dipole if necessary */
1590     if (DOMAINDECOMP(cr))
1591     {
1592         wallcycle_start(wcycle, ewcMOVEX);
1593         dd_move_x(cr->dd, box, x);
1594         wallcycle_stop(wcycle, ewcMOVEX);
1595     }
1596
1597     if (inputrecNeedMutot(inputrec))
1598     {
1599         if (bStateChanged)
1600         {
1601             if (PAR(cr))
1602             {
1603                 gmx_sumd(2*DIM, mu, cr);
1604             }
1605             for (i = 0; i < 2; i++)
1606             {
1607                 for (j = 0; j < DIM; j++)
1608                 {
1609                     fr->mu_tot[i][j] = mu[i*DIM + j];
1610                 }
1611             }
1612         }
1613         if (fr->efep == efepNO)
1614         {
1615             copy_rvec(fr->mu_tot[0], mu_tot);
1616         }
1617         else
1618         {
1619             for (j = 0; j < DIM; j++)
1620             {
1621                 mu_tot[j] =
1622                     (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] + lambda[efptCOUL]*fr->mu_tot[1][j];
1623             }
1624         }
1625     }
1626
1627     /* Reset energies */
1628     reset_enerdata(enerd);
1629     clear_rvecs(SHIFTS, fr->fshift);
1630
1631     if (bNS)
1632     {
1633         wallcycle_start(wcycle, ewcNS);
1634
1635         if (graph && bStateChanged)
1636         {
1637             /* Calculate intramolecular shift vectors to make molecules whole */
1638             mk_mshift(fplog, graph, fr->ePBC, box, x);
1639         }
1640
1641         /* Do the actual neighbour searching */
1642         ns(fplog, fr, box,
1643            groups, top, mdatoms,
1644            cr, nrnb, bFillGrid);
1645
1646         wallcycle_stop(wcycle, ewcNS);
1647     }
1648
1649     if (inputrec->implicit_solvent && bNS)
1650     {
1651         make_gb_nblist(cr, inputrec->gb_algorithm,
1652                        x, box, fr, &top->idef, graph, fr->born);
1653     }
1654
1655     if (DOMAINDECOMP(cr) && !(cr->duty & DUTY_PME))
1656     {
1657         wallcycle_start(wcycle, ewcPPDURINGPME);
1658         dd_force_flop_start(cr->dd, nrnb);
1659     }
1660
1661     if (inputrec->bRot)
1662     {
1663         /* Enforced rotation has its own cycle counter that starts after the collective
1664          * coordinates have been communicated. It is added to ddCyclF to allow
1665          * for proper load-balancing */
1666         wallcycle_start(wcycle, ewcROT);
1667         do_rotation(cr, inputrec, box, x, t, step, wcycle, bNS);
1668         wallcycle_stop(wcycle, ewcROT);
1669     }
1670
1671     /* Temporary solution until all routines take PaddedRVecVector */
1672     rvec *f = as_rvec_array(force->data());
1673
1674     /* Start the force cycle counter.
1675      * This counter is stopped after do_force_lowlevel.
1676      * No parallel communication should occur while this counter is running,
1677      * since that will interfere with the dynamic load balancing.
1678      */
1679     wallcycle_start(wcycle, ewcFORCE);
1680
1681     if (bDoForces)
1682     {
1683         /* Reset forces for which the virial is calculated separately:
1684          * PME/Ewald forces if necessary */
1685         if (fr->bF_NoVirSum)
1686         {
1687             if (flags & GMX_FORCE_VIRIAL)
1688             {
1689                 fr->f_novirsum = fr->forceBufferNoVirialSummation;
1690                 /* TODO: remove this - 1 when padding is properly implemented */
1691                 clear_rvecs(fr->f_novirsum->size() - 1,
1692                             as_rvec_array(fr->f_novirsum->data()));
1693             }
1694             else
1695             {
1696                 /* We are not calculating the pressure so we do not need
1697                  * a separate array for forces that do not contribute
1698                  * to the pressure.
1699                  */
1700                 fr->f_novirsum = force;
1701             }
1702         }
1703
1704         /* Clear the short- and long-range forces */
1705         clear_rvecs(fr->natoms_force_constr, f);
1706
1707         clear_rvec(fr->vir_diag_posres);
1708     }
1709     if (inputrec->bPull && pull_have_constraint(inputrec->pull_work))
1710     {
1711         clear_pull_forces(inputrec->pull_work);
1712     }
1713
1714     /* update QMMMrec, if necessary */
1715     if (fr->bQMMM)
1716     {
1717         update_QMMMrec(cr, fr, x, mdatoms, box, top);
1718     }
1719
1720     /* Compute the bonded and non-bonded energies and optionally forces */
1721     do_force_lowlevel(fr, inputrec, &(top->idef),
1722                       cr, nrnb, wcycle, mdatoms,
1723                       x, hist, f, enerd, fcd, top, fr->born,
1724                       bBornRadii, box,
1725                       inputrec->fepvals, lambda,
1726                       graph, &(top->excls), fr->mu_tot,
1727                       flags,
1728                       &cycles_pme);
1729
1730     cycles_force = wallcycle_stop(wcycle, ewcFORCE);
1731
1732     if (ed)
1733     {
1734         do_flood(cr, inputrec, x, f, ed, box, step, bNS);
1735     }
1736
1737     if (DOMAINDECOMP(cr))
1738     {
1739         dd_force_flop_stop(cr->dd, nrnb);
1740         if (wcycle)
1741         {
1742             dd_cycles_add(cr->dd, cycles_force-cycles_pme, ddCyclF);
1743         }
1744     }
1745
1746     if (bDoForces)
1747     {
1748         /* Compute forces due to electric field */
1749         if (fr->efield != nullptr)
1750         {
1751             fr->efield->calculateForces(cr, mdatoms, fr->f_novirsum, t);
1752         }
1753
1754         /* Communicate the forces */
1755         if (DOMAINDECOMP(cr))
1756         {
1757             wallcycle_start(wcycle, ewcMOVEF);
1758             dd_move_f(cr->dd, f, fr->fshift);
1759             /* Do we need to communicate the separate force array
1760              * for terms that do not contribute to the single sum virial?
1761              * Position restraints and electric fields do not introduce
1762              * inter-cg forces, only full electrostatics methods do.
1763              * When we do not calculate the virial, fr->f_novirsum = f,
1764              * so we have already communicated these forces.
1765              */
1766             if (EEL_FULL(fr->eeltype) && cr->dd->n_intercg_excl &&
1767                 (flags & GMX_FORCE_VIRIAL))
1768             {
1769                 dd_move_f(cr->dd, as_rvec_array(fr->f_novirsum->data()), nullptr);
1770             }
1771             wallcycle_stop(wcycle, ewcMOVEF);
1772         }
1773
1774         /* If we have NoVirSum forces, but we do not calculate the virial,
1775          * we sum fr->f_novirsum=f later.
1776          */
1777         if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1778         {
1779             wallcycle_start(wcycle, ewcVSITESPREAD);
1780             spread_vsite_f(vsite, x, f, fr->fshift, FALSE, nullptr, nrnb,
1781                            &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1782             wallcycle_stop(wcycle, ewcVSITESPREAD);
1783         }
1784
1785         if (flags & GMX_FORCE_VIRIAL)
1786         {
1787             /* Calculation of the virial must be done after vsites! */
1788             calc_virial(0, mdatoms->homenr, x, f,
1789                         vir_force, graph, box, nrnb, fr, inputrec->ePBC);
1790         }
1791     }
1792
1793     if (inputrec->bPull && pull_have_potential(inputrec->pull_work))
1794     {
1795         pull_potential_wrapper(cr, inputrec, box, x,
1796                                f, vir_force, mdatoms, enerd, lambda, t,
1797                                wcycle);
1798     }
1799
1800     /* Add the forces from enforced rotation potentials (if any) */
1801     if (inputrec->bRot)
1802     {
1803         wallcycle_start(wcycle, ewcROTadd);
1804         enerd->term[F_COM_PULL] += add_rot_forces(inputrec->rot, f, cr, step, t);
1805         wallcycle_stop(wcycle, ewcROTadd);
1806     }
1807
1808     /* Add forces from interactive molecular dynamics (IMD), if bIMD == TRUE. */
1809     IMD_apply_forces(inputrec->bIMD, inputrec->imd, cr, f, wcycle);
1810
1811     if (PAR(cr) && !(cr->duty & DUTY_PME))
1812     {
1813         /* In case of node-splitting, the PP nodes receive the long-range
1814          * forces, virial and energy from the PME nodes here.
1815          */
1816         pme_receive_force_ener(cr, wcycle, enerd, fr);
1817     }
1818
1819     if (bDoForces)
1820     {
1821         post_process_forces(cr, step, nrnb, wcycle,
1822                             top, box, x, f, vir_force, mdatoms, graph, fr, vsite,
1823                             flags);
1824     }
1825
1826     if (flags & GMX_FORCE_ENERGY)
1827     {
1828         /* Sum the potential energy terms from group contributions */
1829         sum_epot(&(enerd->grpp), enerd->term);
1830
1831         if (!EI_TPI(inputrec->eI))
1832         {
1833             checkPotentialEnergyValidity(enerd);
1834         }
1835     }
1836
1837 }
1838
1839 void do_force(FILE *fplog, t_commrec *cr,
1840               t_inputrec *inputrec,
1841               gmx_int64_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
1842               gmx_localtop_t *top,
1843               gmx_groups_t *groups,
1844               matrix box, PaddedRVecVector *coordinates, history_t *hist,
1845               PaddedRVecVector *force,
1846               tensor vir_force,
1847               t_mdatoms *mdatoms,
1848               gmx_enerdata_t *enerd, t_fcdata *fcd,
1849               gmx::ArrayRef<real> lambda, t_graph *graph,
1850               t_forcerec *fr,
1851               gmx_vsite_t *vsite, rvec mu_tot,
1852               double t, gmx_edsam_t ed,
1853               gmx_bool bBornRadii,
1854               int flags)
1855 {
1856     /* modify force flag if not doing nonbonded */
1857     if (!fr->bNonbonded)
1858     {
1859         flags &= ~GMX_FORCE_NONBONDED;
1860     }
1861
1862     GMX_ASSERT(coordinates->size() >= static_cast<unsigned int>(fr->natoms_force + 1) ||
1863                fr->natoms_force == 0, "We might need 1 element extra for SIMD");
1864     GMX_ASSERT(force->size() >= static_cast<unsigned int>(fr->natoms_force + 1) ||
1865                fr->natoms_force == 0, "We might need 1 element extra for SIMD");
1866
1867     rvec *x = as_rvec_array(coordinates->data());
1868
1869     switch (inputrec->cutoff_scheme)
1870     {
1871         case ecutsVERLET:
1872             do_force_cutsVERLET(fplog, cr, inputrec,
1873                                 step, nrnb, wcycle,
1874                                 top,
1875                                 groups,
1876                                 box, x, hist,
1877                                 force, vir_force,
1878                                 mdatoms,
1879                                 enerd, fcd,
1880                                 lambda.data(), graph,
1881                                 fr, fr->ic,
1882                                 vsite, mu_tot,
1883                                 t, ed,
1884                                 bBornRadii,
1885                                 flags);
1886             break;
1887         case ecutsGROUP:
1888             do_force_cutsGROUP(fplog, cr, inputrec,
1889                                step, nrnb, wcycle,
1890                                top,
1891                                groups,
1892                                box, x, hist,
1893                                force, vir_force,
1894                                mdatoms,
1895                                enerd, fcd,
1896                                lambda.data(), graph,
1897                                fr, vsite, mu_tot,
1898                                t, ed,
1899                                bBornRadii,
1900                                flags);
1901             break;
1902         default:
1903             gmx_incons("Invalid cut-off scheme passed!");
1904     }
1905 }
1906
1907
1908 void do_constrain_first(FILE *fplog, gmx_constr_t constr,
1909                         t_inputrec *ir, t_mdatoms *md,
1910                         t_state *state, t_commrec *cr, t_nrnb *nrnb,
1911                         t_forcerec *fr, gmx_localtop_t *top)
1912 {
1913     int             i, m, start, end;
1914     gmx_int64_t     step;
1915     real            dt = ir->delta_t;
1916     real            dvdl_dum;
1917     rvec           *savex;
1918
1919     /* We need to allocate one element extra, since we might use
1920      * (unaligned) 4-wide SIMD loads to access rvec entries.
1921      */
1922     snew(savex, state->natoms + 1);
1923
1924     start = 0;
1925     end   = md->homenr;
1926
1927     if (debug)
1928     {
1929         fprintf(debug, "vcm: start=%d, homenr=%d, end=%d\n",
1930                 start, md->homenr, end);
1931     }
1932     /* Do a first constrain to reset particles... */
1933     step = ir->init_step;
1934     if (fplog)
1935     {
1936         char buf[STEPSTRSIZE];
1937         fprintf(fplog, "\nConstraining the starting coordinates (step %s)\n",
1938                 gmx_step_str(step, buf));
1939     }
1940     dvdl_dum = 0;
1941
1942     /* constrain the current position */
1943     constrain(nullptr, TRUE, FALSE, constr, &(top->idef),
1944               ir, cr, step, 0, 1.0, md,
1945               as_rvec_array(state->x.data()), as_rvec_array(state->x.data()), nullptr,
1946               fr->bMolPBC, state->box,
1947               state->lambda[efptBONDED], &dvdl_dum,
1948               nullptr, nullptr, nrnb, econqCoord);
1949     if (EI_VV(ir->eI))
1950     {
1951         /* constrain the inital velocity, and save it */
1952         /* also may be useful if we need the ekin from the halfstep for velocity verlet */
1953         constrain(nullptr, TRUE, FALSE, constr, &(top->idef),
1954                   ir, cr, step, 0, 1.0, md,
1955                   as_rvec_array(state->x.data()), as_rvec_array(state->v.data()), as_rvec_array(state->v.data()),
1956                   fr->bMolPBC, state->box,
1957                   state->lambda[efptBONDED], &dvdl_dum,
1958                   nullptr, nullptr, nrnb, econqVeloc);
1959     }
1960     /* constrain the inital velocities at t-dt/2 */
1961     if (EI_STATE_VELOCITY(ir->eI) && ir->eI != eiVV)
1962     {
1963         for (i = start; (i < end); i++)
1964         {
1965             for (m = 0; (m < DIM); m++)
1966             {
1967                 /* Reverse the velocity */
1968                 state->v[i][m] = -state->v[i][m];
1969                 /* Store the position at t-dt in buf */
1970                 savex[i][m] = state->x[i][m] + dt*state->v[i][m];
1971             }
1972         }
1973         /* Shake the positions at t=-dt with the positions at t=0
1974          * as reference coordinates.
1975          */
1976         if (fplog)
1977         {
1978             char buf[STEPSTRSIZE];
1979             fprintf(fplog, "\nConstraining the coordinates at t0-dt (step %s)\n",
1980                     gmx_step_str(step, buf));
1981         }
1982         dvdl_dum = 0;
1983         constrain(nullptr, TRUE, FALSE, constr, &(top->idef),
1984                   ir, cr, step, -1, 1.0, md,
1985                   as_rvec_array(state->x.data()), savex, nullptr,
1986                   fr->bMolPBC, state->box,
1987                   state->lambda[efptBONDED], &dvdl_dum,
1988                   as_rvec_array(state->v.data()), nullptr, nrnb, econqCoord);
1989
1990         for (i = start; i < end; i++)
1991         {
1992             for (m = 0; m < DIM; m++)
1993             {
1994                 /* Re-reverse the velocities */
1995                 state->v[i][m] = -state->v[i][m];
1996             }
1997         }
1998     }
1999     sfree(savex);
2000 }
2001
2002
2003 static void
2004 integrate_table(real vdwtab[], real scale, int offstart, int rstart, int rend,
2005                 double *enerout, double *virout)
2006 {
2007     double enersum, virsum;
2008     double invscale, invscale2, invscale3;
2009     double r, ea, eb, ec, pa, pb, pc, pd;
2010     double y0, f, g, h;
2011     int    ri, offset;
2012     double tabfactor;
2013
2014     invscale  = 1.0/scale;
2015     invscale2 = invscale*invscale;
2016     invscale3 = invscale*invscale2;
2017
2018     /* Following summation derived from cubic spline definition,
2019      * Numerical Recipies in C, second edition, p. 113-116.  Exact for
2020      * the cubic spline.  We first calculate the negative of the
2021      * energy from rvdw to rvdw_switch, assuming that g(r)=1, and then
2022      * add the more standard, abrupt cutoff correction to that result,
2023      * yielding the long-range correction for a switched function.  We
2024      * perform both the pressure and energy loops at the same time for
2025      * simplicity, as the computational cost is low. */
2026
2027     if (offstart == 0)
2028     {
2029         /* Since the dispersion table has been scaled down a factor
2030          * 6.0 and the repulsion a factor 12.0 to compensate for the
2031          * c6/c12 parameters inside nbfp[] being scaled up (to save
2032          * flops in kernels), we need to correct for this.
2033          */
2034         tabfactor = 6.0;
2035     }
2036     else
2037     {
2038         tabfactor = 12.0;
2039     }
2040
2041     enersum = 0.0;
2042     virsum  = 0.0;
2043     for (ri = rstart; ri < rend; ++ri)
2044     {
2045         r  = ri*invscale;
2046         ea = invscale3;
2047         eb = 2.0*invscale2*r;
2048         ec = invscale*r*r;
2049
2050         pa = invscale3;
2051         pb = 3.0*invscale2*r;
2052         pc = 3.0*invscale*r*r;
2053         pd = r*r*r;
2054
2055         /* this "8" is from the packing in the vdwtab array - perhaps
2056            should be defined? */
2057
2058         offset = 8*ri + offstart;
2059         y0     = vdwtab[offset];
2060         f      = vdwtab[offset+1];
2061         g      = vdwtab[offset+2];
2062         h      = vdwtab[offset+3];
2063
2064         enersum += y0*(ea/3 + eb/2 + ec) + f*(ea/4 + eb/3 + ec/2) + g*(ea/5 + eb/4 + ec/3) + h*(ea/6 + eb/5 + ec/4);
2065         virsum  +=  f*(pa/4 + pb/3 + pc/2 + pd) + 2*g*(pa/5 + pb/4 + pc/3 + pd/2) + 3*h*(pa/6 + pb/5 + pc/4 + pd/3);
2066     }
2067     *enerout = 4.0*M_PI*enersum*tabfactor;
2068     *virout  = 4.0*M_PI*virsum*tabfactor;
2069 }
2070
2071 void calc_enervirdiff(FILE *fplog, int eDispCorr, t_forcerec *fr)
2072 {
2073     double   eners[2], virs[2], enersum, virsum;
2074     double   r0, rc3, rc9;
2075     int      ri0, ri1, i;
2076     real     scale, *vdwtab;
2077
2078     fr->enershiftsix    = 0;
2079     fr->enershifttwelve = 0;
2080     fr->enerdiffsix     = 0;
2081     fr->enerdifftwelve  = 0;
2082     fr->virdiffsix      = 0;
2083     fr->virdifftwelve   = 0;
2084
2085     if (eDispCorr != edispcNO)
2086     {
2087         for (i = 0; i < 2; i++)
2088         {
2089             eners[i] = 0;
2090             virs[i]  = 0;
2091         }
2092         if ((fr->vdw_modifier == eintmodPOTSHIFT) ||
2093             (fr->vdw_modifier == eintmodPOTSWITCH) ||
2094             (fr->vdw_modifier == eintmodFORCESWITCH) ||
2095             (fr->vdwtype == evdwSHIFT) ||
2096             (fr->vdwtype == evdwSWITCH))
2097         {
2098             if (((fr->vdw_modifier == eintmodPOTSWITCH) ||
2099                  (fr->vdw_modifier == eintmodFORCESWITCH) ||
2100                  (fr->vdwtype == evdwSWITCH)) && fr->rvdw_switch == 0)
2101             {
2102                 gmx_fatal(FARGS,
2103                           "With dispersion correction rvdw-switch can not be zero "
2104                           "for vdw-type = %s", evdw_names[fr->vdwtype]);
2105             }
2106
2107             /* TODO This code depends on the logic in tables.c that
2108                constructs the table layout, which should be made
2109                explicit in future cleanup. */
2110             GMX_ASSERT(fr->dispersionCorrectionTable->interaction == GMX_TABLE_INTERACTION_VDWREP_VDWDISP,
2111                        "Dispersion-correction code needs a table with both repulsion and dispersion terms");
2112             scale  = fr->dispersionCorrectionTable->scale;
2113             vdwtab = fr->dispersionCorrectionTable->data;
2114
2115             /* Round the cut-offs to exact table values for precision */
2116             ri0  = static_cast<int>(floor(fr->rvdw_switch*scale));
2117             ri1  = static_cast<int>(ceil(fr->rvdw*scale));
2118
2119             /* The code below has some support for handling force-switching, i.e.
2120              * when the force (instead of potential) is switched over a limited
2121              * region. This leads to a constant shift in the potential inside the
2122              * switching region, which we can handle by adding a constant energy
2123              * term in the force-switch case just like when we do potential-shift.
2124              *
2125              * For now this is not enabled, but to keep the functionality in the
2126              * code we check separately for switch and shift. When we do force-switch
2127              * the shifting point is rvdw_switch, while it is the cutoff when we
2128              * have a classical potential-shift.
2129              *
2130              * For a pure potential-shift the potential has a constant shift
2131              * all the way out to the cutoff, and that is it. For other forms
2132              * we need to calculate the constant shift up to the point where we
2133              * start modifying the potential.
2134              */
2135             ri0  = (fr->vdw_modifier == eintmodPOTSHIFT) ? ri1 : ri0;
2136
2137             r0   = ri0/scale;
2138             rc3  = r0*r0*r0;
2139             rc9  = rc3*rc3*rc3;
2140
2141             if ((fr->vdw_modifier == eintmodFORCESWITCH) ||
2142                 (fr->vdwtype == evdwSHIFT))
2143             {
2144                 /* Determine the constant energy shift below rvdw_switch.
2145                  * Table has a scale factor since we have scaled it down to compensate
2146                  * for scaling-up c6/c12 with the derivative factors to save flops in analytical kernels.
2147                  */
2148                 fr->enershiftsix    = (real)(-1.0/(rc3*rc3)) - 6.0*vdwtab[8*ri0];
2149                 fr->enershifttwelve = (real)( 1.0/(rc9*rc3)) - 12.0*vdwtab[8*ri0 + 4];
2150             }
2151             else if (fr->vdw_modifier == eintmodPOTSHIFT)
2152             {
2153                 fr->enershiftsix    = (real)(-1.0/(rc3*rc3));
2154                 fr->enershifttwelve = (real)( 1.0/(rc9*rc3));
2155             }
2156
2157             /* Add the constant part from 0 to rvdw_switch.
2158              * This integration from 0 to rvdw_switch overcounts the number
2159              * of interactions by 1, as it also counts the self interaction.
2160              * We will correct for this later.
2161              */
2162             eners[0] += 4.0*M_PI*fr->enershiftsix*rc3/3.0;
2163             eners[1] += 4.0*M_PI*fr->enershifttwelve*rc3/3.0;
2164
2165             /* Calculate the contribution in the range [r0,r1] where we
2166              * modify the potential. For a pure potential-shift modifier we will
2167              * have ri0==ri1, and there will not be any contribution here.
2168              */
2169             for (i = 0; i < 2; i++)
2170             {
2171                 enersum = 0;
2172                 virsum  = 0;
2173                 integrate_table(vdwtab, scale, (i == 0 ? 0 : 4), ri0, ri1, &enersum, &virsum);
2174                 eners[i] -= enersum;
2175                 virs[i]  -= virsum;
2176             }
2177
2178             /* Alright: Above we compensated by REMOVING the parts outside r0
2179              * corresponding to the ideal VdW 1/r6 and /r12 potentials.
2180              *
2181              * Regardless of whether r0 is the point where we start switching,
2182              * or the cutoff where we calculated the constant shift, we include
2183              * all the parts we are missing out to infinity from r0 by
2184              * calculating the analytical dispersion correction.
2185              */
2186             eners[0] += -4.0*M_PI/(3.0*rc3);
2187             eners[1] +=  4.0*M_PI/(9.0*rc9);
2188             virs[0]  +=  8.0*M_PI/rc3;
2189             virs[1]  += -16.0*M_PI/(3.0*rc9);
2190         }
2191         else if (fr->vdwtype == evdwCUT ||
2192                  EVDW_PME(fr->vdwtype) ||
2193                  fr->vdwtype == evdwUSER)
2194         {
2195             if (fr->vdwtype == evdwUSER && fplog)
2196             {
2197                 fprintf(fplog,
2198                         "WARNING: using dispersion correction with user tables\n");
2199             }
2200
2201             /* Note that with LJ-PME, the dispersion correction is multiplied
2202              * by the difference between the actual C6 and the value of C6
2203              * that would produce the combination rule.
2204              * This means the normal energy and virial difference formulas
2205              * can be used here.
2206              */
2207
2208             rc3  = fr->rvdw*fr->rvdw*fr->rvdw;
2209             rc9  = rc3*rc3*rc3;
2210             /* Contribution beyond the cut-off */
2211             eners[0] += -4.0*M_PI/(3.0*rc3);
2212             eners[1] +=  4.0*M_PI/(9.0*rc9);
2213             if (fr->vdw_modifier == eintmodPOTSHIFT)
2214             {
2215                 /* Contribution within the cut-off */
2216                 eners[0] += -4.0*M_PI/(3.0*rc3);
2217                 eners[1] +=  4.0*M_PI/(3.0*rc9);
2218             }
2219             /* Contribution beyond the cut-off */
2220             virs[0]  +=  8.0*M_PI/rc3;
2221             virs[1]  += -16.0*M_PI/(3.0*rc9);
2222         }
2223         else
2224         {
2225             gmx_fatal(FARGS,
2226                       "Dispersion correction is not implemented for vdw-type = %s",
2227                       evdw_names[fr->vdwtype]);
2228         }
2229
2230         /* When we deprecate the group kernels the code below can go too */
2231         if (fr->vdwtype == evdwPME && fr->cutoff_scheme == ecutsGROUP)
2232         {
2233             /* Calculate self-interaction coefficient (assuming that
2234              * the reciprocal-space contribution is constant in the
2235              * region that contributes to the self-interaction).
2236              */
2237             fr->enershiftsix = gmx::power6(fr->ewaldcoeff_lj) / 6.0;
2238
2239             eners[0] += -gmx::power3(std::sqrt(M_PI)*fr->ewaldcoeff_lj)/3.0;
2240             virs[0]  +=  gmx::power3(std::sqrt(M_PI)*fr->ewaldcoeff_lj);
2241         }
2242
2243         fr->enerdiffsix    = eners[0];
2244         fr->enerdifftwelve = eners[1];
2245         /* The 0.5 is due to the Gromacs definition of the virial */
2246         fr->virdiffsix     = 0.5*virs[0];
2247         fr->virdifftwelve  = 0.5*virs[1];
2248     }
2249 }
2250
2251 void calc_dispcorr(t_inputrec *ir, t_forcerec *fr,
2252                    matrix box, real lambda, tensor pres, tensor virial,
2253                    real *prescorr, real *enercorr, real *dvdlcorr)
2254 {
2255     gmx_bool bCorrAll, bCorrPres;
2256     real     dvdlambda, invvol, dens, ninter, avcsix, avctwelve, enerdiff, svir = 0, spres = 0;
2257     int      m;
2258
2259     *prescorr = 0;
2260     *enercorr = 0;
2261     *dvdlcorr = 0;
2262
2263     clear_mat(virial);
2264     clear_mat(pres);
2265
2266     if (ir->eDispCorr != edispcNO)
2267     {
2268         bCorrAll  = (ir->eDispCorr == edispcAllEner ||
2269                      ir->eDispCorr == edispcAllEnerPres);
2270         bCorrPres = (ir->eDispCorr == edispcEnerPres ||
2271                      ir->eDispCorr == edispcAllEnerPres);
2272
2273         invvol = 1/det(box);
2274         if (fr->n_tpi)
2275         {
2276             /* Only correct for the interactions with the inserted molecule */
2277             dens   = (fr->numAtomsForDispersionCorrection - fr->n_tpi)*invvol;
2278             ninter = fr->n_tpi;
2279         }
2280         else
2281         {
2282             dens   = fr->numAtomsForDispersionCorrection*invvol;
2283             ninter = 0.5*fr->numAtomsForDispersionCorrection;
2284         }
2285
2286         if (ir->efep == efepNO)
2287         {
2288             avcsix    = fr->avcsix[0];
2289             avctwelve = fr->avctwelve[0];
2290         }
2291         else
2292         {
2293             avcsix    = (1 - lambda)*fr->avcsix[0]    + lambda*fr->avcsix[1];
2294             avctwelve = (1 - lambda)*fr->avctwelve[0] + lambda*fr->avctwelve[1];
2295         }
2296
2297         enerdiff   = ninter*(dens*fr->enerdiffsix - fr->enershiftsix);
2298         *enercorr += avcsix*enerdiff;
2299         dvdlambda  = 0.0;
2300         if (ir->efep != efepNO)
2301         {
2302             dvdlambda += (fr->avcsix[1] - fr->avcsix[0])*enerdiff;
2303         }
2304         if (bCorrAll)
2305         {
2306             enerdiff   = ninter*(dens*fr->enerdifftwelve - fr->enershifttwelve);
2307             *enercorr += avctwelve*enerdiff;
2308             if (fr->efep != efepNO)
2309             {
2310                 dvdlambda += (fr->avctwelve[1] - fr->avctwelve[0])*enerdiff;
2311             }
2312         }
2313
2314         if (bCorrPres)
2315         {
2316             svir = ninter*dens*avcsix*fr->virdiffsix/3.0;
2317             if (ir->eDispCorr == edispcAllEnerPres)
2318             {
2319                 svir += ninter*dens*avctwelve*fr->virdifftwelve/3.0;
2320             }
2321             /* The factor 2 is because of the Gromacs virial definition */
2322             spres = -2.0*invvol*svir*PRESFAC;
2323
2324             for (m = 0; m < DIM; m++)
2325             {
2326                 virial[m][m] += svir;
2327                 pres[m][m]   += spres;
2328             }
2329             *prescorr += spres;
2330         }
2331
2332         /* Can't currently control when it prints, for now, just print when degugging */
2333         if (debug)
2334         {
2335             if (bCorrAll)
2336             {
2337                 fprintf(debug, "Long Range LJ corr.: <C6> %10.4e, <C12> %10.4e\n",
2338                         avcsix, avctwelve);
2339             }
2340             if (bCorrPres)
2341             {
2342                 fprintf(debug,
2343                         "Long Range LJ corr.: Epot %10g, Pres: %10g, Vir: %10g\n",
2344                         *enercorr, spres, svir);
2345             }
2346             else
2347             {
2348                 fprintf(debug, "Long Range LJ corr.: Epot %10g\n", *enercorr);
2349             }
2350         }
2351
2352         if (fr->efep != efepNO)
2353         {
2354             *dvdlcorr += dvdlambda;
2355         }
2356     }
2357 }
2358
2359 void do_pbc_first(FILE *fplog, matrix box, t_forcerec *fr,
2360                   t_graph *graph, rvec x[])
2361 {
2362     if (fplog)
2363     {
2364         fprintf(fplog, "Removing pbc first time\n");
2365     }
2366     calc_shifts(box, fr->shift_vec);
2367     if (graph)
2368     {
2369         mk_mshift(fplog, graph, fr->ePBC, box, x);
2370         if (gmx_debug_at)
2371         {
2372             p_graph(debug, "do_pbc_first 1", graph);
2373         }
2374         shift_self(graph, box, x);
2375         /* By doing an extra mk_mshift the molecules that are broken
2376          * because they were e.g. imported from another software
2377          * will be made whole again. Such are the healing powers
2378          * of GROMACS.
2379          */
2380         mk_mshift(fplog, graph, fr->ePBC, box, x);
2381         if (gmx_debug_at)
2382         {
2383             p_graph(debug, "do_pbc_first 2", graph);
2384         }
2385     }
2386     if (fplog)
2387     {
2388         fprintf(fplog, "Done rmpbc\n");
2389     }
2390 }
2391
2392 static void low_do_pbc_mtop(FILE *fplog, int ePBC, matrix box,
2393                             const gmx_mtop_t *mtop, rvec x[],
2394                             gmx_bool bFirst)
2395 {
2396     t_graph        *graph;
2397     int             mb, as, mol;
2398     gmx_molblock_t *molb;
2399
2400     if (bFirst && fplog)
2401     {
2402         fprintf(fplog, "Removing pbc first time\n");
2403     }
2404
2405     snew(graph, 1);
2406     as = 0;
2407     for (mb = 0; mb < mtop->nmolblock; mb++)
2408     {
2409         molb = &mtop->molblock[mb];
2410         if (molb->natoms_mol == 1 ||
2411             (!bFirst && mtop->moltype[molb->type].cgs.nr == 1))
2412         {
2413             /* Just one atom or charge group in the molecule, no PBC required */
2414             as += molb->nmol*molb->natoms_mol;
2415         }
2416         else
2417         {
2418             /* Pass NULL iso fplog to avoid graph prints for each molecule type */
2419             mk_graph_ilist(nullptr, mtop->moltype[molb->type].ilist,
2420                            0, molb->natoms_mol, FALSE, FALSE, graph);
2421
2422             for (mol = 0; mol < molb->nmol; mol++)
2423             {
2424                 mk_mshift(fplog, graph, ePBC, box, x+as);
2425
2426                 shift_self(graph, box, x+as);
2427                 /* The molecule is whole now.
2428                  * We don't need the second mk_mshift call as in do_pbc_first,
2429                  * since we no longer need this graph.
2430                  */
2431
2432                 as += molb->natoms_mol;
2433             }
2434             done_graph(graph);
2435         }
2436     }
2437     sfree(graph);
2438 }
2439
2440 void do_pbc_first_mtop(FILE *fplog, int ePBC, matrix box,
2441                        const gmx_mtop_t *mtop, rvec x[])
2442 {
2443     low_do_pbc_mtop(fplog, ePBC, box, mtop, x, TRUE);
2444 }
2445
2446 void do_pbc_mtop(FILE *fplog, int ePBC, matrix box,
2447                  gmx_mtop_t *mtop, rvec x[])
2448 {
2449     low_do_pbc_mtop(fplog, ePBC, box, mtop, x, FALSE);
2450 }
2451
2452 void put_atoms_in_box_omp(int ePBC, const matrix box, int natoms, rvec x[])
2453 {
2454     int t, nth;
2455     nth = gmx_omp_nthreads_get(emntDefault);
2456
2457 #pragma omp parallel for num_threads(nth) schedule(static)
2458     for (t = 0; t < nth; t++)
2459     {
2460         try
2461         {
2462             int offset, len;
2463
2464             offset = (natoms*t    )/nth;
2465             len    = (natoms*(t + 1))/nth - offset;
2466             put_atoms_in_box(ePBC, box, len, x + offset);
2467         }
2468         GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
2469     }
2470 }
2471
2472 // TODO This can be cleaned up a lot, and move back to runner.cpp
2473 void finish_run(FILE *fplog, const gmx::MDLogger &mdlog, t_commrec *cr,
2474                 t_inputrec *inputrec,
2475                 t_nrnb nrnb[], gmx_wallcycle_t wcycle,
2476                 gmx_walltime_accounting_t walltime_accounting,
2477                 nonbonded_verlet_t *nbv,
2478                 gmx_bool bWriteStat)
2479 {
2480     t_nrnb *nrnb_tot = nullptr;
2481     double  delta_t  = 0;
2482     double  nbfs     = 0, mflop = 0;
2483     double  elapsed_time,
2484             elapsed_time_over_all_ranks,
2485             elapsed_time_over_all_threads,
2486             elapsed_time_over_all_threads_over_all_ranks;
2487
2488     if (!walltime_accounting_get_valid_finish(walltime_accounting))
2489     {
2490         GMX_LOG(mdlog.warning).asParagraph().appendText("Simulation ended prematurely, no performance report will be written.");
2491         return;
2492     }
2493
2494     if (cr->nnodes > 1)
2495     {
2496         snew(nrnb_tot, 1);
2497 #if GMX_MPI
2498         MPI_Allreduce(nrnb->n, nrnb_tot->n, eNRNB, MPI_DOUBLE, MPI_SUM,
2499                       cr->mpi_comm_mysim);
2500 #endif
2501     }
2502     else
2503     {
2504         nrnb_tot = nrnb;
2505     }
2506
2507     elapsed_time                                 = walltime_accounting_get_elapsed_time(walltime_accounting);
2508     elapsed_time_over_all_ranks                  = elapsed_time;
2509     elapsed_time_over_all_threads                = walltime_accounting_get_elapsed_time_over_all_threads(walltime_accounting);
2510     elapsed_time_over_all_threads_over_all_ranks = elapsed_time_over_all_threads;
2511 #if GMX_MPI
2512     if (cr->nnodes > 1)
2513     {
2514         /* reduce elapsed_time over all MPI ranks in the current simulation */
2515         MPI_Allreduce(&elapsed_time,
2516                       &elapsed_time_over_all_ranks,
2517                       1, MPI_DOUBLE, MPI_SUM,
2518                       cr->mpi_comm_mysim);
2519         elapsed_time_over_all_ranks /= cr->nnodes;
2520         /* Reduce elapsed_time_over_all_threads over all MPI ranks in the
2521          * current simulation. */
2522         MPI_Allreduce(&elapsed_time_over_all_threads,
2523                       &elapsed_time_over_all_threads_over_all_ranks,
2524                       1, MPI_DOUBLE, MPI_SUM,
2525                       cr->mpi_comm_mysim);
2526     }
2527 #endif
2528
2529     if (SIMMASTER(cr))
2530     {
2531         print_flop(fplog, nrnb_tot, &nbfs, &mflop);
2532     }
2533     if (cr->nnodes > 1)
2534     {
2535         sfree(nrnb_tot);
2536     }
2537
2538     if ((cr->duty & DUTY_PP) && DOMAINDECOMP(cr))
2539     {
2540         print_dd_statistics(cr, inputrec, fplog);
2541     }
2542
2543     /* TODO Move the responsibility for any scaling by thread counts
2544      * to the code that handled the thread region, so that there's a
2545      * mechanism to keep cycle counting working during the transition
2546      * to task parallelism. */
2547     int nthreads_pp  = gmx_omp_nthreads_get(emntNonbonded);
2548     int nthreads_pme = gmx_omp_nthreads_get(emntPME);
2549     wallcycle_scale_by_num_threads(wcycle, cr->duty == DUTY_PME, nthreads_pp, nthreads_pme);
2550     auto cycle_sum(wallcycle_sum(cr, wcycle));
2551
2552     if (SIMMASTER(cr))
2553     {
2554         struct gmx_wallclock_gpu_t* gputimes = use_GPU(nbv) ? nbnxn_gpu_get_timings(nbv->gpu_nbv) : nullptr;
2555
2556         wallcycle_print(fplog, mdlog, cr->nnodes, cr->npmenodes, nthreads_pp, nthreads_pme,
2557                         elapsed_time_over_all_ranks,
2558                         wcycle, cycle_sum, gputimes);
2559
2560         if (EI_DYNAMICS(inputrec->eI))
2561         {
2562             delta_t = inputrec->delta_t;
2563         }
2564
2565         if (fplog)
2566         {
2567             print_perf(fplog, elapsed_time_over_all_threads_over_all_ranks,
2568                        elapsed_time_over_all_ranks,
2569                        walltime_accounting_get_nsteps_done(walltime_accounting),
2570                        delta_t, nbfs, mflop);
2571         }
2572         if (bWriteStat)
2573         {
2574             print_perf(stderr, elapsed_time_over_all_threads_over_all_ranks,
2575                        elapsed_time_over_all_ranks,
2576                        walltime_accounting_get_nsteps_done(walltime_accounting),
2577                        delta_t, nbfs, mflop);
2578         }
2579     }
2580 }
2581
2582 extern void initialize_lambdas(FILE *fplog, t_inputrec *ir, int *fep_state, gmx::ArrayRef<real> lambda, double *lam0)
2583 {
2584     /* this function works, but could probably use a logic rewrite to keep all the different
2585        types of efep straight. */
2586
2587     if ((ir->efep == efepNO) && (ir->bSimTemp == FALSE))
2588     {
2589         return;
2590     }
2591
2592     t_lambda *fep = ir->fepvals;
2593     *fep_state    = fep->init_fep_state; /* this might overwrite the checkpoint
2594                                             if checkpoint is set -- a kludge is in for now
2595                                             to prevent this.*/
2596
2597     for (int i = 0; i < efptNR; i++)
2598     {
2599         /* overwrite lambda state with init_lambda for now for backwards compatibility */
2600         if (fep->init_lambda >= 0) /* if it's -1, it was never initializd */
2601         {
2602             lambda[i] = fep->init_lambda;
2603             if (lam0)
2604             {
2605                 lam0[i] = lambda[i];
2606             }
2607         }
2608         else
2609         {
2610             lambda[i] = fep->all_lambda[i][*fep_state];
2611             if (lam0)
2612             {
2613                 lam0[i] = lambda[i];
2614             }
2615         }
2616     }
2617     if (ir->bSimTemp)
2618     {
2619         /* need to rescale control temperatures to match current state */
2620         for (int i = 0; i < ir->opts.ngtc; i++)
2621         {
2622             if (ir->opts.ref_t[i] > 0)
2623             {
2624                 ir->opts.ref_t[i] = ir->simtempvals->temperatures[*fep_state];
2625             }
2626         }
2627     }
2628
2629     /* Send to the log the information on the current lambdas */
2630     if (fplog != nullptr)
2631     {
2632         fprintf(fplog, "Initial vector of lambda components:[ ");
2633         for (const auto &l : lambda)
2634         {
2635             fprintf(fplog, "%10.4f ", l);
2636         }
2637         fprintf(fplog, "]\n");
2638     }
2639     return;
2640 }
2641
2642
2643 void init_md(FILE *fplog,
2644              t_commrec *cr, gmx::IMDOutputProvider *outputProvider,
2645              t_inputrec *ir, const gmx_output_env_t *oenv,
2646              double *t, double *t0,
2647              gmx::ArrayRef<real> lambda, int *fep_state, double *lam0,
2648              t_nrnb *nrnb, gmx_mtop_t *mtop,
2649              gmx_update_t **upd,
2650              int nfile, const t_filenm fnm[],
2651              gmx_mdoutf_t *outf, t_mdebin **mdebin,
2652              tensor force_vir, tensor shake_vir, rvec mu_tot,
2653              gmx_bool *bSimAnn, t_vcm **vcm, unsigned long Flags,
2654              gmx_wallcycle_t wcycle)
2655 {
2656     int  i;
2657
2658     /* Initial values */
2659     *t = *t0       = ir->init_t;
2660
2661     *bSimAnn = FALSE;
2662     for (i = 0; i < ir->opts.ngtc; i++)
2663     {
2664         /* set bSimAnn if any group is being annealed */
2665         if (ir->opts.annealing[i] != eannNO)
2666         {
2667             *bSimAnn = TRUE;
2668         }
2669     }
2670
2671     /* Initialize lambda variables */
2672     initialize_lambdas(fplog, ir, fep_state, lambda, lam0);
2673
2674     // TODO upd is never NULL in practice, but the analysers don't know that
2675     if (upd)
2676     {
2677         *upd = init_update(ir);
2678     }
2679     if (*bSimAnn)
2680     {
2681         update_annealing_target_temp(ir, ir->init_t, upd ? *upd : nullptr);
2682     }
2683
2684     if (vcm != nullptr)
2685     {
2686         *vcm = init_vcm(fplog, &mtop->groups, ir);
2687     }
2688
2689     if (EI_DYNAMICS(ir->eI) && !(Flags & MD_APPENDFILES))
2690     {
2691         if (ir->etc == etcBERENDSEN)
2692         {
2693             please_cite(fplog, "Berendsen84a");
2694         }
2695         if (ir->etc == etcVRESCALE)
2696         {
2697             please_cite(fplog, "Bussi2007a");
2698         }
2699         if (ir->eI == eiSD1)
2700         {
2701             please_cite(fplog, "Goga2012");
2702         }
2703     }
2704     init_nrnb(nrnb);
2705
2706     if (nfile != -1)
2707     {
2708         *outf = init_mdoutf(fplog, nfile, fnm, Flags, cr, outputProvider, ir, mtop, oenv, wcycle);
2709
2710         *mdebin = init_mdebin((Flags & MD_APPENDFILES) ? nullptr : mdoutf_get_fp_ene(*outf),
2711                               mtop, ir, mdoutf_get_fp_dhdl(*outf));
2712     }
2713
2714     /* Initiate variables */
2715     clear_mat(force_vir);
2716     clear_mat(shake_vir);
2717     clear_rvec(mu_tot);
2718 }