2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
39 #include "gromacs/legacyheaders/sim_util.h"
48 #include "gromacs/domdec/domdec.h"
49 #include "gromacs/essentialdynamics/edsam.h"
50 #include "gromacs/ewald/pme.h"
51 #include "gromacs/gmxlib/nonbonded/nb_free_energy.h"
52 #include "gromacs/gmxlib/nonbonded/nb_kernel.h"
53 #include "gromacs/imd/imd.h"
54 #include "gromacs/legacyheaders/calcmu.h"
55 #include "gromacs/legacyheaders/chargegroup.h"
56 #include "gromacs/legacyheaders/constr.h"
57 #include "gromacs/legacyheaders/copyrite.h"
58 #include "gromacs/legacyheaders/disre.h"
59 #include "gromacs/legacyheaders/force.h"
60 #include "gromacs/legacyheaders/genborn.h"
61 #include "gromacs/legacyheaders/gmx_omp_nthreads.h"
62 #include "gromacs/legacyheaders/mdatoms.h"
63 #include "gromacs/legacyheaders/mdrun.h"
64 #include "gromacs/legacyheaders/names.h"
65 #include "gromacs/legacyheaders/network.h"
66 #include "gromacs/legacyheaders/nonbonded.h"
67 #include "gromacs/legacyheaders/nrnb.h"
68 #include "gromacs/legacyheaders/orires.h"
69 #include "gromacs/legacyheaders/qmmm.h"
70 #include "gromacs/legacyheaders/txtdump.h"
71 #include "gromacs/legacyheaders/typedefs.h"
72 #include "gromacs/legacyheaders/update.h"
73 #include "gromacs/legacyheaders/types/commrec.h"
74 #include "gromacs/listed-forces/bonded.h"
75 #include "gromacs/math/units.h"
76 #include "gromacs/math/vec.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_search.h"
81 #include "gromacs/mdlib/nbnxn_kernels/nbnxn_kernel_gpu_ref.h"
82 #include "gromacs/mdlib/nbnxn_kernels/nbnxn_kernel_ref.h"
83 #include "gromacs/mdlib/nbnxn_kernels/simd_2xnn/nbnxn_kernel_simd_2xnn.h"
84 #include "gromacs/mdlib/nbnxn_kernels/simd_4xn/nbnxn_kernel_simd_4xn.h"
85 #include "gromacs/pbcutil/ishift.h"
86 #include "gromacs/pbcutil/mshift.h"
87 #include "gromacs/pbcutil/pbc.h"
88 #include "gromacs/pulling/pull.h"
89 #include "gromacs/pulling/pull_rotation.h"
90 #include "gromacs/timing/gpu_timing.h"
91 #include "gromacs/timing/wallcycle.h"
92 #include "gromacs/timing/walltime_accounting.h"
93 #include "gromacs/utility/cstringutil.h"
94 #include "gromacs/utility/gmxmpi.h"
95 #include "gromacs/utility/smalloc.h"
96 #include "gromacs/utility/sysinfo.h"
99 #include "nbnxn_gpu.h"
101 void print_time(FILE *out,
102 gmx_walltime_accounting_t walltime_accounting,
105 t_commrec gmx_unused *cr)
108 char timebuf[STRLEN];
109 double dt, elapsed_seconds, time_per_step;
112 #ifndef GMX_THREAD_MPI
118 fprintf(out, "step %s", gmx_step_str(step, buf));
119 if ((step >= ir->nstlist))
121 double seconds_since_epoch = gmx_gettime();
122 elapsed_seconds = seconds_since_epoch - walltime_accounting_get_start_time_stamp(walltime_accounting);
123 time_per_step = elapsed_seconds/(step - ir->init_step + 1);
124 dt = (ir->nsteps + ir->init_step - step) * time_per_step;
130 finish = (time_t) (seconds_since_epoch + dt);
131 gmx_ctime_r(&finish, timebuf, STRLEN);
132 sprintf(buf, "%s", timebuf);
133 buf[strlen(buf)-1] = '\0';
134 fprintf(out, ", will finish %s", buf);
138 fprintf(out, ", remaining wall clock time: %5d s ", (int)dt);
143 fprintf(out, " performance: %.1f ns/day ",
144 ir->delta_t/1000*24*60*60/time_per_step);
147 #ifndef GMX_THREAD_MPI
157 void print_date_and_time(FILE *fplog, int nodeid, const char *title,
160 char time_string[STRLEN];
169 char timebuf[STRLEN];
170 time_t temp_time = (time_t) the_time;
172 gmx_ctime_r(&temp_time, timebuf, STRLEN);
173 for (i = 0; timebuf[i] >= ' '; i++)
175 time_string[i] = timebuf[i];
177 time_string[i] = '\0';
180 fprintf(fplog, "%s on rank %d %s\n", title, nodeid, time_string);
183 void print_start(FILE *fplog, t_commrec *cr,
184 gmx_walltime_accounting_t walltime_accounting,
189 sprintf(buf, "Started %s", name);
190 print_date_and_time(fplog, cr->nodeid, buf,
191 walltime_accounting_get_start_time_stamp(walltime_accounting));
194 static void sum_forces(int start, int end, rvec f[], rvec flr[])
200 pr_rvecs(debug, 0, "fsr", f+start, end-start);
201 pr_rvecs(debug, 0, "flr", flr+start, end-start);
203 for (i = start; (i < end); i++)
205 rvec_inc(f[i], flr[i]);
210 * calc_f_el calculates forces due to an electric field.
212 * force is kJ mol^-1 nm^-1 = e * kJ mol^-1 nm^-1 / e
214 * Et[] contains the parameters for the time dependent
216 * Ex[] contains the parameters for
217 * the spatial dependent part of the field.
218 * The function should return the energy due to the electric field
219 * (if any) but for now returns 0.
222 * There can be problems with the virial.
223 * Since the field is not self-consistent this is unavoidable.
224 * For neutral molecules the virial is correct within this approximation.
225 * For neutral systems with many charged molecules the error is small.
226 * But for systems with a net charge or a few charged molecules
227 * the error can be significant when the field is high.
228 * Solution: implement a self-consistent electric field into PME.
230 static void calc_f_el(FILE *fp, int start, int homenr,
231 real charge[], rvec f[],
232 t_cosines Ex[], t_cosines Et[], double t)
238 for (m = 0; (m < DIM); m++)
245 Ext[m] = cos(Et[m].a[0]*(t-t0))*exp(-sqr(t-t0)/(2.0*sqr(Et[m].a[2])));
249 Ext[m] = cos(Et[m].a[0]*t);
258 /* Convert the field strength from V/nm to MD-units */
259 Ext[m] *= Ex[m].a[0]*FIELDFAC;
260 for (i = start; (i < start+homenr); i++)
262 f[i][m] += charge[i]*Ext[m];
272 fprintf(fp, "%10g %10g %10g %10g #FIELD\n", t,
273 Ext[XX]/FIELDFAC, Ext[YY]/FIELDFAC, Ext[ZZ]/FIELDFAC);
277 static void calc_virial(int start, int homenr, rvec x[], rvec f[],
278 tensor vir_part, t_graph *graph, matrix box,
279 t_nrnb *nrnb, const t_forcerec *fr, int ePBC)
283 /* The short-range virial from surrounding boxes */
285 calc_vir(SHIFTS, fr->shift_vec, fr->fshift, vir_part, ePBC == epbcSCREW, box);
286 inc_nrnb(nrnb, eNR_VIRIAL, SHIFTS);
288 /* Calculate partial virial, for local atoms only, based on short range.
289 * Total virial is computed in global_stat, called from do_md
291 f_calc_vir(start, start+homenr, x, f, vir_part, graph, box);
292 inc_nrnb(nrnb, eNR_VIRIAL, homenr);
294 /* Add position restraint contribution */
295 for (i = 0; i < DIM; i++)
297 vir_part[i][i] += fr->vir_diag_posres[i];
300 /* Add wall contribution */
301 for (i = 0; i < DIM; i++)
303 vir_part[i][ZZ] += fr->vir_wall_z[i];
308 pr_rvecs(debug, 0, "vir_part", vir_part, DIM);
312 static void pull_potential_wrapper(t_commrec *cr,
314 matrix box, rvec x[],
318 gmx_enerdata_t *enerd,
321 gmx_wallcycle_t wcycle)
326 /* Calculate the center of mass forces, this requires communication,
327 * which is why pull_potential is called close to other communication.
328 * The virial contribution is calculated directly,
329 * which is why we call pull_potential after calc_virial.
331 wallcycle_start(wcycle, ewcPULLPOT);
332 set_pbc(&pbc, ir->ePBC, box);
334 enerd->term[F_COM_PULL] +=
335 pull_potential(ir->pull, mdatoms, &pbc,
336 cr, t, lambda[efptRESTRAINT], x, f, vir_force, &dvdl);
337 enerd->dvdl_lin[efptRESTRAINT] += dvdl;
338 wallcycle_stop(wcycle, ewcPULLPOT);
341 static void pme_receive_force_ener(t_commrec *cr,
342 gmx_wallcycle_t wcycle,
343 gmx_enerdata_t *enerd,
346 real e_q, e_lj, dvdl_q, dvdl_lj;
347 float cycles_ppdpme, cycles_seppme;
349 cycles_ppdpme = wallcycle_stop(wcycle, ewcPPDURINGPME);
350 dd_cycles_add(cr->dd, cycles_ppdpme, ddCyclPPduringPME);
352 /* In case of node-splitting, the PP nodes receive the long-range
353 * forces, virial and energy from the PME nodes here.
355 wallcycle_start(wcycle, ewcPP_PMEWAITRECVF);
358 gmx_pme_receive_f(cr, fr->f_novirsum, fr->vir_el_recip, &e_q,
359 fr->vir_lj_recip, &e_lj, &dvdl_q, &dvdl_lj,
361 enerd->term[F_COUL_RECIP] += e_q;
362 enerd->term[F_LJ_RECIP] += e_lj;
363 enerd->dvdl_lin[efptCOUL] += dvdl_q;
364 enerd->dvdl_lin[efptVDW] += dvdl_lj;
368 dd_cycles_add(cr->dd, cycles_seppme, ddCyclPME);
370 wallcycle_stop(wcycle, ewcPP_PMEWAITRECVF);
373 static void print_large_forces(FILE *fp, t_mdatoms *md, t_commrec *cr,
374 gmx_int64_t step, real pforce, rvec *x, rvec *f)
378 char buf[STEPSTRSIZE];
381 for (i = 0; i < md->homenr; i++)
384 /* We also catch NAN, if the compiler does not optimize this away. */
385 if (fn2 >= pf2 || fn2 != fn2)
387 fprintf(fp, "step %s atom %6d x %8.3f %8.3f %8.3f force %12.5e\n",
388 gmx_step_str(step, buf),
389 ddglatnr(cr->dd, i), x[i][XX], x[i][YY], x[i][ZZ], sqrt(fn2));
394 static void post_process_forces(t_commrec *cr,
396 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
398 matrix box, rvec x[],
403 t_forcerec *fr, gmx_vsite_t *vsite,
410 /* Spread the mesh force on virtual sites to the other particles...
411 * This is parallellized. MPI communication is performed
412 * if the constructing atoms aren't local.
414 wallcycle_start(wcycle, ewcVSITESPREAD);
415 spread_vsite_f(vsite, x, fr->f_novirsum, NULL,
416 (flags & GMX_FORCE_VIRIAL), fr->vir_el_recip,
418 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
419 wallcycle_stop(wcycle, ewcVSITESPREAD);
421 if (flags & GMX_FORCE_VIRIAL)
423 /* Now add the forces, this is local */
426 sum_forces(0, fr->f_novirsum_n, f, fr->f_novirsum);
430 sum_forces(0, mdatoms->homenr,
433 if (EEL_FULL(fr->eeltype))
435 /* Add the mesh contribution to the virial */
436 m_add(vir_force, fr->vir_el_recip, vir_force);
438 if (EVDW_PME(fr->vdwtype))
440 /* Add the mesh contribution to the virial */
441 m_add(vir_force, fr->vir_lj_recip, vir_force);
445 pr_rvecs(debug, 0, "vir_force", vir_force, DIM);
450 if (fr->print_force >= 0)
452 print_large_forces(stderr, mdatoms, cr, step, fr->print_force, x, f);
456 static void do_nb_verlet(t_forcerec *fr,
457 interaction_const_t *ic,
458 gmx_enerdata_t *enerd,
459 int flags, int ilocality,
462 gmx_wallcycle_t wcycle)
464 int enr_nbnxn_kernel_ljc, enr_nbnxn_kernel_lj;
465 nonbonded_verlet_group_t *nbvg;
466 gmx_bool bUsingGpuKernels;
468 if (!(flags & GMX_FORCE_NONBONDED))
470 /* skip non-bonded calculation */
474 nbvg = &fr->nbv->grp[ilocality];
476 /* GPU kernel launch overhead is already timed separately */
477 if (fr->cutoff_scheme != ecutsVERLET)
479 gmx_incons("Invalid cut-off scheme passed!");
482 bUsingGpuKernels = (nbvg->kernel_type == nbnxnk8x8x8_GPU);
484 if (!bUsingGpuKernels)
486 wallcycle_sub_start(wcycle, ewcsNONBONDED);
488 switch (nbvg->kernel_type)
490 case nbnxnk4x4_PlainC:
491 nbnxn_kernel_ref(&nbvg->nbl_lists,
497 enerd->grpp.ener[egCOULSR],
499 enerd->grpp.ener[egBHAMSR] :
500 enerd->grpp.ener[egLJSR]);
503 case nbnxnk4xN_SIMD_4xN:
504 nbnxn_kernel_simd_4xn(&nbvg->nbl_lists,
511 enerd->grpp.ener[egCOULSR],
513 enerd->grpp.ener[egBHAMSR] :
514 enerd->grpp.ener[egLJSR]);
516 case nbnxnk4xN_SIMD_2xNN:
517 nbnxn_kernel_simd_2xnn(&nbvg->nbl_lists,
524 enerd->grpp.ener[egCOULSR],
526 enerd->grpp.ener[egBHAMSR] :
527 enerd->grpp.ener[egLJSR]);
530 case nbnxnk8x8x8_GPU:
531 nbnxn_gpu_launch_kernel(fr->nbv->gpu_nbv, nbvg->nbat, flags, ilocality);
534 case nbnxnk8x8x8_PlainC:
535 nbnxn_kernel_gpu_ref(nbvg->nbl_lists.nbl[0],
540 nbvg->nbat->out[0].f,
542 enerd->grpp.ener[egCOULSR],
544 enerd->grpp.ener[egBHAMSR] :
545 enerd->grpp.ener[egLJSR]);
549 gmx_incons("Invalid nonbonded kernel type passed!");
552 if (!bUsingGpuKernels)
554 wallcycle_sub_stop(wcycle, ewcsNONBONDED);
557 if (EEL_RF(ic->eeltype) || ic->eeltype == eelCUT)
559 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_RF;
561 else if ((!bUsingGpuKernels && nbvg->ewald_excl == ewaldexclAnalytical) ||
562 (bUsingGpuKernels && nbnxn_gpu_is_kernel_ewald_analytical(fr->nbv->gpu_nbv)))
564 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_EWALD;
568 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_TAB;
570 enr_nbnxn_kernel_lj = eNR_NBNXN_LJ;
571 if (flags & GMX_FORCE_ENERGY)
573 /* In eNR_??? the nbnxn F+E kernels are always the F kernel + 1 */
574 enr_nbnxn_kernel_ljc += 1;
575 enr_nbnxn_kernel_lj += 1;
578 inc_nrnb(nrnb, enr_nbnxn_kernel_ljc,
579 nbvg->nbl_lists.natpair_ljq);
580 inc_nrnb(nrnb, enr_nbnxn_kernel_lj,
581 nbvg->nbl_lists.natpair_lj);
582 /* The Coulomb-only kernels are offset -eNR_NBNXN_LJ_RF+eNR_NBNXN_RF */
583 inc_nrnb(nrnb, enr_nbnxn_kernel_ljc-eNR_NBNXN_LJ_RF+eNR_NBNXN_RF,
584 nbvg->nbl_lists.natpair_q);
586 if (ic->vdw_modifier == eintmodFORCESWITCH)
588 /* We add up the switch cost separately */
589 inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_FSW+((flags & GMX_FORCE_ENERGY) ? 1 : 0),
590 nbvg->nbl_lists.natpair_ljq + nbvg->nbl_lists.natpair_lj);
592 if (ic->vdw_modifier == eintmodPOTSWITCH)
594 /* We add up the switch cost separately */
595 inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_PSW+((flags & GMX_FORCE_ENERGY) ? 1 : 0),
596 nbvg->nbl_lists.natpair_ljq + nbvg->nbl_lists.natpair_lj);
598 if (ic->vdwtype == evdwPME)
600 /* We add up the LJ Ewald cost separately */
601 inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_EWALD+((flags & GMX_FORCE_ENERGY) ? 1 : 0),
602 nbvg->nbl_lists.natpair_ljq + nbvg->nbl_lists.natpair_lj);
606 static void do_nb_verlet_fep(nbnxn_pairlist_set_t *nbl_lists,
613 gmx_enerdata_t *enerd,
616 gmx_wallcycle_t wcycle)
619 nb_kernel_data_t kernel_data;
621 real dvdl_nb[efptNR];
626 /* Add short-range interactions */
627 donb_flags |= GMX_NONBONDED_DO_SR;
629 /* Currently all group scheme kernels always calculate (shift-)forces */
630 if (flags & GMX_FORCE_FORCES)
632 donb_flags |= GMX_NONBONDED_DO_FORCE;
634 if (flags & GMX_FORCE_VIRIAL)
636 donb_flags |= GMX_NONBONDED_DO_SHIFTFORCE;
638 if (flags & GMX_FORCE_ENERGY)
640 donb_flags |= GMX_NONBONDED_DO_POTENTIAL;
642 if (flags & GMX_FORCE_DO_LR)
644 donb_flags |= GMX_NONBONDED_DO_LR;
647 kernel_data.flags = donb_flags;
648 kernel_data.lambda = lambda;
649 kernel_data.dvdl = dvdl_nb;
651 kernel_data.energygrp_elec = enerd->grpp.ener[egCOULSR];
652 kernel_data.energygrp_vdw = enerd->grpp.ener[egLJSR];
654 /* reset free energy components */
655 for (i = 0; i < efptNR; i++)
660 assert(gmx_omp_nthreads_get(emntNonbonded) == nbl_lists->nnbl);
662 wallcycle_sub_start(wcycle, ewcsNONBONDED);
663 #pragma omp parallel for schedule(static) num_threads(nbl_lists->nnbl)
664 for (th = 0; th < nbl_lists->nnbl; th++)
666 gmx_nb_free_energy_kernel(nbl_lists->nbl_fep[th],
667 x, f, fr, mdatoms, &kernel_data, nrnb);
670 if (fepvals->sc_alpha != 0)
672 enerd->dvdl_nonlin[efptVDW] += dvdl_nb[efptVDW];
673 enerd->dvdl_nonlin[efptCOUL] += dvdl_nb[efptCOUL];
677 enerd->dvdl_lin[efptVDW] += dvdl_nb[efptVDW];
678 enerd->dvdl_lin[efptCOUL] += dvdl_nb[efptCOUL];
681 /* If we do foreign lambda and we have soft-core interactions
682 * we have to recalculate the (non-linear) energies contributions.
684 if (fepvals->n_lambda > 0 && (flags & GMX_FORCE_DHDL) && fepvals->sc_alpha != 0)
686 kernel_data.flags = (donb_flags & ~(GMX_NONBONDED_DO_FORCE | GMX_NONBONDED_DO_SHIFTFORCE)) | GMX_NONBONDED_DO_FOREIGNLAMBDA;
687 kernel_data.lambda = lam_i;
688 kernel_data.energygrp_elec = enerd->foreign_grpp.ener[egCOULSR];
689 kernel_data.energygrp_vdw = enerd->foreign_grpp.ener[egLJSR];
690 /* Note that we add to kernel_data.dvdl, but ignore the result */
692 for (i = 0; i < enerd->n_lambda; i++)
694 for (j = 0; j < efptNR; j++)
696 lam_i[j] = (i == 0 ? lambda[j] : fepvals->all_lambda[j][i-1]);
698 reset_foreign_enerdata(enerd);
699 #pragma omp parallel for schedule(static) num_threads(nbl_lists->nnbl)
700 for (th = 0; th < nbl_lists->nnbl; th++)
702 gmx_nb_free_energy_kernel(nbl_lists->nbl_fep[th],
703 x, f, fr, mdatoms, &kernel_data, nrnb);
706 sum_epot(&(enerd->foreign_grpp), enerd->foreign_term);
707 enerd->enerpart_lambda[i] += enerd->foreign_term[F_EPOT];
711 wallcycle_sub_stop(wcycle, ewcsNONBONDED);
714 gmx_bool use_GPU(const nonbonded_verlet_t *nbv)
716 return nbv != NULL && nbv->bUseGPU;
719 void do_force_cutsVERLET(FILE *fplog, t_commrec *cr,
720 t_inputrec *inputrec,
721 gmx_int64_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
723 gmx_groups_t gmx_unused *groups,
724 matrix box, rvec x[], history_t *hist,
728 gmx_enerdata_t *enerd, t_fcdata *fcd,
729 real *lambda, t_graph *graph,
730 t_forcerec *fr, interaction_const_t *ic,
731 gmx_vsite_t *vsite, rvec mu_tot,
732 double t, FILE *field, gmx_edsam_t ed,
739 gmx_bool bStateChanged, bNS, bFillGrid, bCalcCGCM;
740 gmx_bool bDoLongRange, bDoForces, bSepLRF, bUseGPU, bUseOrEmulGPU;
741 gmx_bool bDiffKernels = FALSE;
742 rvec vzero, box_diag;
743 float cycles_pme, cycles_force, cycles_wait_gpu;
744 nonbonded_verlet_t *nbv;
751 homenr = mdatoms->homenr;
753 clear_mat(vir_force);
755 if (DOMAINDECOMP(cr))
757 cg1 = cr->dd->ncg_tot;
768 bStateChanged = (flags & GMX_FORCE_STATECHANGED);
769 bNS = (flags & GMX_FORCE_NS) && (fr->bAllvsAll == FALSE);
770 bFillGrid = (bNS && bStateChanged);
771 bCalcCGCM = (bFillGrid && !DOMAINDECOMP(cr));
772 bDoLongRange = (fr->bTwinRange && bNS && (flags & GMX_FORCE_DO_LR));
773 bDoForces = (flags & GMX_FORCE_FORCES);
774 bSepLRF = (bDoLongRange && bDoForces && (flags & GMX_FORCE_SEPLRF));
775 bUseGPU = fr->nbv->bUseGPU;
776 bUseOrEmulGPU = bUseGPU || (nbv->grp[0].kernel_type == nbnxnk8x8x8_PlainC);
780 update_forcerec(fr, box);
782 if (NEED_MUTOT(*inputrec))
784 /* Calculate total (local) dipole moment in a temporary common array.
785 * This makes it possible to sum them over nodes faster.
787 calc_mu(start, homenr,
788 x, mdatoms->chargeA, mdatoms->chargeB, mdatoms->nChargePerturbed,
793 if (fr->ePBC != epbcNONE)
795 /* Compute shift vectors every step,
796 * because of pressure coupling or box deformation!
798 if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
800 calc_shifts(box, fr->shift_vec);
805 put_atoms_in_box_omp(fr->ePBC, box, homenr, x);
806 inc_nrnb(nrnb, eNR_SHIFTX, homenr);
808 else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph)
810 unshift_self(graph, box, x);
814 nbnxn_atomdata_copy_shiftvec(flags & GMX_FORCE_DYNAMICBOX,
815 fr->shift_vec, nbv->grp[0].nbat);
818 if (!(cr->duty & DUTY_PME))
823 /* Send particle coordinates to the pme nodes.
824 * Since this is only implemented for domain decomposition
825 * and domain decomposition does not use the graph,
826 * we do not need to worry about shifting.
831 wallcycle_start(wcycle, ewcPP_PMESENDX);
833 bBS = (inputrec->nwall == 2);
837 svmul(inputrec->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
840 if (EEL_PME(fr->eeltype))
842 pme_flags |= GMX_PME_DO_COULOMB;
845 if (EVDW_PME(fr->vdwtype))
847 pme_flags |= GMX_PME_DO_LJ;
850 gmx_pme_send_coordinates(cr, bBS ? boxs : box, x,
851 mdatoms->nChargePerturbed, mdatoms->nTypePerturbed, lambda[efptCOUL], lambda[efptVDW],
852 (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)),
855 wallcycle_stop(wcycle, ewcPP_PMESENDX);
859 /* do gridding for pair search */
862 if (graph && bStateChanged)
864 /* Calculate intramolecular shift vectors to make molecules whole */
865 mk_mshift(fplog, graph, fr->ePBC, box, x);
869 box_diag[XX] = box[XX][XX];
870 box_diag[YY] = box[YY][YY];
871 box_diag[ZZ] = box[ZZ][ZZ];
873 wallcycle_start(wcycle, ewcNS);
876 wallcycle_sub_start(wcycle, ewcsNBS_GRID_LOCAL);
877 nbnxn_put_on_grid(nbv->nbs, fr->ePBC, box,
879 0, mdatoms->homenr, -1, fr->cginfo, x,
881 nbv->grp[eintLocal].kernel_type,
882 nbv->grp[eintLocal].nbat);
883 wallcycle_sub_stop(wcycle, ewcsNBS_GRID_LOCAL);
887 wallcycle_sub_start(wcycle, ewcsNBS_GRID_NONLOCAL);
888 nbnxn_put_on_grid_nonlocal(nbv->nbs, domdec_zones(cr->dd),
890 nbv->grp[eintNonlocal].kernel_type,
891 nbv->grp[eintNonlocal].nbat);
892 wallcycle_sub_stop(wcycle, ewcsNBS_GRID_NONLOCAL);
895 if (nbv->ngrp == 1 ||
896 nbv->grp[eintNonlocal].nbat == nbv->grp[eintLocal].nbat)
898 nbnxn_atomdata_set(nbv->grp[eintLocal].nbat, eatAll,
899 nbv->nbs, mdatoms, fr->cginfo);
903 nbnxn_atomdata_set(nbv->grp[eintLocal].nbat, eatLocal,
904 nbv->nbs, mdatoms, fr->cginfo);
905 nbnxn_atomdata_set(nbv->grp[eintNonlocal].nbat, eatAll,
906 nbv->nbs, mdatoms, fr->cginfo);
908 wallcycle_stop(wcycle, ewcNS);
911 /* initialize the GPU atom data and copy shift vector */
916 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
917 nbnxn_gpu_init_atomdata(nbv->gpu_nbv, nbv->grp[eintLocal].nbat);
918 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
921 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
922 nbnxn_gpu_upload_shiftvec(nbv->gpu_nbv, nbv->grp[eintLocal].nbat);
923 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
926 /* do local pair search */
929 wallcycle_start_nocount(wcycle, ewcNS);
930 wallcycle_sub_start(wcycle, ewcsNBS_SEARCH_LOCAL);
931 nbnxn_make_pairlist(nbv->nbs, nbv->grp[eintLocal].nbat,
934 nbv->min_ci_balanced,
935 &nbv->grp[eintLocal].nbl_lists,
937 nbv->grp[eintLocal].kernel_type,
939 wallcycle_sub_stop(wcycle, ewcsNBS_SEARCH_LOCAL);
943 /* initialize local pair-list on the GPU */
944 nbnxn_gpu_init_pairlist(nbv->gpu_nbv,
945 nbv->grp[eintLocal].nbl_lists.nbl[0],
948 wallcycle_stop(wcycle, ewcNS);
952 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
953 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
954 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatLocal, FALSE, x,
955 nbv->grp[eintLocal].nbat);
956 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
957 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
962 wallcycle_start(wcycle, ewcLAUNCH_GPU_NB);
963 /* launch local nonbonded F on GPU */
964 do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFNo,
966 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
969 /* Communicate coordinates and sum dipole if necessary +
970 do non-local pair search */
971 if (DOMAINDECOMP(cr))
973 bDiffKernels = (nbv->grp[eintNonlocal].kernel_type !=
974 nbv->grp[eintLocal].kernel_type);
978 /* With GPU+CPU non-bonded calculations we need to copy
979 * the local coordinates to the non-local nbat struct
980 * (in CPU format) as the non-local kernel call also
981 * calculates the local - non-local interactions.
983 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
984 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
985 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatLocal, TRUE, x,
986 nbv->grp[eintNonlocal].nbat);
987 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
988 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
993 wallcycle_start_nocount(wcycle, ewcNS);
994 wallcycle_sub_start(wcycle, ewcsNBS_SEARCH_NONLOCAL);
998 nbnxn_grid_add_simple(nbv->nbs, nbv->grp[eintNonlocal].nbat);
1001 nbnxn_make_pairlist(nbv->nbs, nbv->grp[eintNonlocal].nbat,
1004 nbv->min_ci_balanced,
1005 &nbv->grp[eintNonlocal].nbl_lists,
1007 nbv->grp[eintNonlocal].kernel_type,
1010 wallcycle_sub_stop(wcycle, ewcsNBS_SEARCH_NONLOCAL);
1012 if (nbv->grp[eintNonlocal].kernel_type == nbnxnk8x8x8_GPU)
1014 /* initialize non-local pair-list on the GPU */
1015 nbnxn_gpu_init_pairlist(nbv->gpu_nbv,
1016 nbv->grp[eintNonlocal].nbl_lists.nbl[0],
1019 wallcycle_stop(wcycle, ewcNS);
1023 wallcycle_start(wcycle, ewcMOVEX);
1024 dd_move_x(cr->dd, box, x);
1026 /* When we don't need the total dipole we sum it in global_stat */
1027 if (bStateChanged && NEED_MUTOT(*inputrec))
1029 gmx_sumd(2*DIM, mu, cr);
1031 wallcycle_stop(wcycle, ewcMOVEX);
1033 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1034 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
1035 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatNonlocal, FALSE, x,
1036 nbv->grp[eintNonlocal].nbat);
1037 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
1038 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1041 if (bUseGPU && !bDiffKernels)
1043 wallcycle_start(wcycle, ewcLAUNCH_GPU_NB);
1044 /* launch non-local nonbonded F on GPU */
1045 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFNo,
1047 cycles_force += wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1053 /* launch D2H copy-back F */
1054 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1055 if (DOMAINDECOMP(cr) && !bDiffKernels)
1057 nbnxn_gpu_launch_cpyback(nbv->gpu_nbv, nbv->grp[eintNonlocal].nbat,
1058 flags, eatNonlocal);
1060 nbnxn_gpu_launch_cpyback(nbv->gpu_nbv, nbv->grp[eintLocal].nbat,
1062 cycles_force += wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1065 if (bStateChanged && NEED_MUTOT(*inputrec))
1069 gmx_sumd(2*DIM, mu, cr);
1072 for (i = 0; i < 2; i++)
1074 for (j = 0; j < DIM; j++)
1076 fr->mu_tot[i][j] = mu[i*DIM + j];
1080 if (fr->efep == efepNO)
1082 copy_rvec(fr->mu_tot[0], mu_tot);
1086 for (j = 0; j < DIM; j++)
1089 (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] +
1090 lambda[efptCOUL]*fr->mu_tot[1][j];
1094 /* Reset energies */
1095 reset_enerdata(fr, bNS, enerd, MASTER(cr));
1096 clear_rvecs(SHIFTS, fr->fshift);
1098 if (DOMAINDECOMP(cr) && !(cr->duty & DUTY_PME))
1100 wallcycle_start(wcycle, ewcPPDURINGPME);
1101 dd_force_flop_start(cr->dd, nrnb);
1106 /* Enforced rotation has its own cycle counter that starts after the collective
1107 * coordinates have been communicated. It is added to ddCyclF to allow
1108 * for proper load-balancing */
1109 wallcycle_start(wcycle, ewcROT);
1110 do_rotation(cr, inputrec, box, x, t, step, wcycle, bNS);
1111 wallcycle_stop(wcycle, ewcROT);
1114 /* Start the force cycle counter.
1115 * This counter is stopped after do_force_lowlevel.
1116 * No parallel communication should occur while this counter is running,
1117 * since that will interfere with the dynamic load balancing.
1119 wallcycle_start(wcycle, ewcFORCE);
1122 /* Reset forces for which the virial is calculated separately:
1123 * PME/Ewald forces if necessary */
1124 if (fr->bF_NoVirSum)
1126 if (flags & GMX_FORCE_VIRIAL)
1128 fr->f_novirsum = fr->f_novirsum_alloc;
1131 clear_rvecs(fr->f_novirsum_n, fr->f_novirsum);
1135 clear_rvecs(homenr, fr->f_novirsum+start);
1140 /* We are not calculating the pressure so we do not need
1141 * a separate array for forces that do not contribute
1148 /* Clear the short- and long-range forces */
1149 clear_rvecs(fr->natoms_force_constr, f);
1150 if (bSepLRF && do_per_step(step, inputrec->nstcalclr))
1152 clear_rvecs(fr->natoms_force_constr, fr->f_twin);
1155 clear_rvec(fr->vir_diag_posres);
1158 if (inputrec->bPull && inputrec->pull->bConstraint)
1160 clear_pull_forces(inputrec->pull);
1163 /* We calculate the non-bonded forces, when done on the CPU, here.
1164 * We do this before calling do_force_lowlevel, because in that
1165 * function, the listed forces are calculated before PME, which
1166 * does communication. With this order, non-bonded and listed
1167 * force calculation imbalance can be balanced out by the domain
1168 * decomposition load balancing.
1173 /* Maybe we should move this into do_force_lowlevel */
1174 do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFYes,
1178 if (fr->efep != efepNO)
1180 /* Calculate the local and non-local free energy interactions here.
1181 * Happens here on the CPU both with and without GPU.
1183 if (fr->nbv->grp[eintLocal].nbl_lists.nbl_fep[0]->nrj > 0)
1185 do_nb_verlet_fep(&fr->nbv->grp[eintLocal].nbl_lists,
1187 inputrec->fepvals, lambda,
1188 enerd, flags, nrnb, wcycle);
1191 if (DOMAINDECOMP(cr) &&
1192 fr->nbv->grp[eintNonlocal].nbl_lists.nbl_fep[0]->nrj > 0)
1194 do_nb_verlet_fep(&fr->nbv->grp[eintNonlocal].nbl_lists,
1196 inputrec->fepvals, lambda,
1197 enerd, flags, nrnb, wcycle);
1201 if (!bUseOrEmulGPU || bDiffKernels)
1205 if (DOMAINDECOMP(cr))
1207 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal,
1208 bDiffKernels ? enbvClearFYes : enbvClearFNo,
1218 aloc = eintNonlocal;
1221 /* Add all the non-bonded force to the normal force array.
1222 * This can be split into a local and a non-local part when overlapping
1223 * communication with calculation with domain decomposition.
1225 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1226 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1227 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1228 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatAll, nbv->grp[aloc].nbat, f);
1229 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1230 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1231 wallcycle_start_nocount(wcycle, ewcFORCE);
1233 /* if there are multiple fshift output buffers reduce them */
1234 if ((flags & GMX_FORCE_VIRIAL) &&
1235 nbv->grp[aloc].nbl_lists.nnbl > 1)
1237 /* This is not in a subcounter because it takes a
1238 negligible and constant-sized amount of time */
1239 nbnxn_atomdata_add_nbat_fshift_to_fshift(nbv->grp[aloc].nbat,
1244 /* update QMMMrec, if necessary */
1247 update_QMMMrec(cr, fr, x, mdatoms, box, top);
1250 /* Compute the bonded and non-bonded energies and optionally forces */
1251 do_force_lowlevel(fr, inputrec, &(top->idef),
1252 cr, nrnb, wcycle, mdatoms,
1253 x, hist, f, bSepLRF ? fr->f_twin : f, enerd, fcd, top, fr->born,
1255 inputrec->fepvals, lambda, graph, &(top->excls), fr->mu_tot,
1256 flags, &cycles_pme);
1260 if (do_per_step(step, inputrec->nstcalclr))
1262 /* Add the long range forces to the short range forces */
1263 for (i = 0; i < fr->natoms_force_constr; i++)
1265 rvec_add(fr->f_twin[i], f[i], f[i]);
1270 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1274 do_flood(cr, inputrec, x, f, ed, box, step, bNS);
1277 if (bUseOrEmulGPU && !bDiffKernels)
1279 /* wait for non-local forces (or calculate in emulation mode) */
1280 if (DOMAINDECOMP(cr))
1286 wallcycle_start(wcycle, ewcWAIT_GPU_NB_NL);
1287 nbnxn_gpu_wait_for_gpu(nbv->gpu_nbv,
1288 nbv->grp[eintNonlocal].nbat,
1290 enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1292 cycles_tmp = wallcycle_stop(wcycle, ewcWAIT_GPU_NB_NL);
1293 cycles_wait_gpu += cycles_tmp;
1294 cycles_force += cycles_tmp;
1298 wallcycle_start_nocount(wcycle, ewcFORCE);
1299 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFYes,
1301 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1303 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1304 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1305 /* skip the reduction if there was no non-local work to do */
1306 if (nbv->grp[eintNonlocal].nbl_lists.nbl[0]->nsci > 0)
1308 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatNonlocal,
1309 nbv->grp[eintNonlocal].nbat, f);
1311 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1312 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1316 if (bDoForces && DOMAINDECOMP(cr))
1320 /* We are done with the CPU compute, but the GPU local non-bonded
1321 * kernel can still be running while we communicate the forces.
1322 * We start a counter here, so we can, hopefully, time the rest
1323 * of the GPU kernel execution and data transfer.
1325 wallcycle_start(wcycle, ewcWAIT_GPU_NB_L_EST);
1328 /* Communicate the forces */
1329 wallcycle_start(wcycle, ewcMOVEF);
1330 dd_move_f(cr->dd, f, fr->fshift);
1333 /* We should not update the shift forces here,
1334 * since f_twin is already included in f.
1336 dd_move_f(cr->dd, fr->f_twin, NULL);
1338 wallcycle_stop(wcycle, ewcMOVEF);
1343 /* wait for local forces (or calculate in emulation mode) */
1346 float cycles_tmp, cycles_wait_est;
1347 const float cuda_api_overhead_margin = 50000.0f; /* cycles */
1349 wallcycle_start(wcycle, ewcWAIT_GPU_NB_L);
1350 nbnxn_gpu_wait_for_gpu(nbv->gpu_nbv,
1351 nbv->grp[eintLocal].nbat,
1353 enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1355 cycles_tmp = wallcycle_stop(wcycle, ewcWAIT_GPU_NB_L);
1357 if (bDoForces && DOMAINDECOMP(cr))
1359 cycles_wait_est = wallcycle_stop(wcycle, ewcWAIT_GPU_NB_L_EST);
1361 if (cycles_tmp < cuda_api_overhead_margin)
1363 /* We measured few cycles, it could be that the kernel
1364 * and transfer finished earlier and there was no actual
1365 * wait time, only API call overhead.
1366 * Then the actual time could be anywhere between 0 and
1367 * cycles_wait_est. As a compromise, we use half the time.
1369 cycles_wait_est *= 0.5f;
1374 /* No force communication so we actually timed the wait */
1375 cycles_wait_est = cycles_tmp;
1377 /* Even though this is after dd_move_f, the actual task we are
1378 * waiting for runs asynchronously with dd_move_f and we usually
1379 * have nothing to balance it with, so we can and should add
1380 * the time to the force time for load balancing.
1382 cycles_force += cycles_wait_est;
1383 cycles_wait_gpu += cycles_wait_est;
1385 /* now clear the GPU outputs while we finish the step on the CPU */
1387 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1388 nbnxn_gpu_clear_outputs(nbv->gpu_nbv, flags);
1389 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1393 wallcycle_start_nocount(wcycle, ewcFORCE);
1394 do_nb_verlet(fr, ic, enerd, flags, eintLocal,
1395 DOMAINDECOMP(cr) ? enbvClearFNo : enbvClearFYes,
1397 wallcycle_stop(wcycle, ewcFORCE);
1399 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1400 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1401 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatLocal,
1402 nbv->grp[eintLocal].nbat, f);
1403 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1404 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1407 if (DOMAINDECOMP(cr))
1409 dd_force_flop_stop(cr->dd, nrnb);
1412 dd_cycles_add(cr->dd, cycles_force-cycles_pme, ddCyclF);
1415 dd_cycles_add(cr->dd, cycles_wait_gpu, ddCyclWaitGPU);
1422 if (IR_ELEC_FIELD(*inputrec))
1424 /* Compute forces due to electric field */
1425 calc_f_el(MASTER(cr) ? field : NULL,
1426 start, homenr, mdatoms->chargeA, fr->f_novirsum,
1427 inputrec->ex, inputrec->et, t);
1430 /* If we have NoVirSum forces, but we do not calculate the virial,
1431 * we sum fr->f_novirsum=f later.
1433 if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1435 wallcycle_start(wcycle, ewcVSITESPREAD);
1436 spread_vsite_f(vsite, x, f, fr->fshift, FALSE, NULL, nrnb,
1437 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1438 wallcycle_stop(wcycle, ewcVSITESPREAD);
1442 wallcycle_start(wcycle, ewcVSITESPREAD);
1443 spread_vsite_f(vsite, x, fr->f_twin, NULL, FALSE, NULL,
1445 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1446 wallcycle_stop(wcycle, ewcVSITESPREAD);
1450 if (flags & GMX_FORCE_VIRIAL)
1452 /* Calculation of the virial must be done after vsites! */
1453 calc_virial(0, mdatoms->homenr, x, f,
1454 vir_force, graph, box, nrnb, fr, inputrec->ePBC);
1458 if (inputrec->bPull && inputrec->pull->bPotential)
1460 /* Since the COM pulling is always done mass-weighted, no forces are
1461 * applied to vsites and this call can be done after vsite spreading.
1463 pull_potential_wrapper(cr, inputrec, box, x,
1464 f, vir_force, mdatoms, enerd, lambda, t,
1468 /* Add the forces from enforced rotation potentials (if any) */
1471 wallcycle_start(wcycle, ewcROTadd);
1472 enerd->term[F_COM_PULL] += add_rot_forces(inputrec->rot, f, cr, step, t);
1473 wallcycle_stop(wcycle, ewcROTadd);
1476 /* Add forces from interactive molecular dynamics (IMD), if bIMD == TRUE. */
1477 IMD_apply_forces(inputrec->bIMD, inputrec->imd, cr, f, wcycle);
1479 if (PAR(cr) && !(cr->duty & DUTY_PME))
1481 /* In case of node-splitting, the PP nodes receive the long-range
1482 * forces, virial and energy from the PME nodes here.
1484 pme_receive_force_ener(cr, wcycle, enerd, fr);
1489 post_process_forces(cr, step, nrnb, wcycle,
1490 top, box, x, f, vir_force, mdatoms, graph, fr, vsite,
1494 /* Sum the potential energy terms from group contributions */
1495 sum_epot(&(enerd->grpp), enerd->term);
1498 void do_force_cutsGROUP(FILE *fplog, t_commrec *cr,
1499 t_inputrec *inputrec,
1500 gmx_int64_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
1501 gmx_localtop_t *top,
1502 gmx_groups_t *groups,
1503 matrix box, rvec x[], history_t *hist,
1507 gmx_enerdata_t *enerd, t_fcdata *fcd,
1508 real *lambda, t_graph *graph,
1509 t_forcerec *fr, gmx_vsite_t *vsite, rvec mu_tot,
1510 double t, FILE *field, gmx_edsam_t ed,
1511 gmx_bool bBornRadii,
1517 gmx_bool bStateChanged, bNS, bFillGrid, bCalcCGCM;
1518 gmx_bool bDoLongRangeNS, bDoForces, bSepLRF;
1519 gmx_bool bDoAdressWF;
1521 float cycles_pme, cycles_force;
1524 homenr = mdatoms->homenr;
1526 clear_mat(vir_force);
1529 if (DOMAINDECOMP(cr))
1531 cg1 = cr->dd->ncg_tot;
1542 bStateChanged = (flags & GMX_FORCE_STATECHANGED);
1543 bNS = (flags & GMX_FORCE_NS) && (fr->bAllvsAll == FALSE);
1544 /* Should we update the long-range neighborlists at this step? */
1545 bDoLongRangeNS = fr->bTwinRange && bNS;
1546 /* Should we perform the long-range nonbonded evaluation inside the neighborsearching? */
1547 bFillGrid = (bNS && bStateChanged);
1548 bCalcCGCM = (bFillGrid && !DOMAINDECOMP(cr));
1549 bDoForces = (flags & GMX_FORCE_FORCES);
1550 bSepLRF = ((inputrec->nstcalclr > 1) && bDoForces &&
1551 (flags & GMX_FORCE_SEPLRF) && (flags & GMX_FORCE_DO_LR));
1553 /* should probably move this to the forcerec since it doesn't change */
1554 bDoAdressWF = ((fr->adress_type != eAdressOff));
1558 update_forcerec(fr, box);
1560 if (NEED_MUTOT(*inputrec))
1562 /* Calculate total (local) dipole moment in a temporary common array.
1563 * This makes it possible to sum them over nodes faster.
1565 calc_mu(start, homenr,
1566 x, mdatoms->chargeA, mdatoms->chargeB, mdatoms->nChargePerturbed,
1571 if (fr->ePBC != epbcNONE)
1573 /* Compute shift vectors every step,
1574 * because of pressure coupling or box deformation!
1576 if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
1578 calc_shifts(box, fr->shift_vec);
1583 put_charge_groups_in_box(fplog, cg0, cg1, fr->ePBC, box,
1584 &(top->cgs), x, fr->cg_cm);
1585 inc_nrnb(nrnb, eNR_CGCM, homenr);
1586 inc_nrnb(nrnb, eNR_RESETX, cg1-cg0);
1588 else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph)
1590 unshift_self(graph, box, x);
1595 calc_cgcm(fplog, cg0, cg1, &(top->cgs), x, fr->cg_cm);
1596 inc_nrnb(nrnb, eNR_CGCM, homenr);
1599 if (bCalcCGCM && gmx_debug_at)
1601 pr_rvecs(debug, 0, "cgcm", fr->cg_cm, top->cgs.nr);
1605 if (!(cr->duty & DUTY_PME))
1610 /* Send particle coordinates to the pme nodes.
1611 * Since this is only implemented for domain decomposition
1612 * and domain decomposition does not use the graph,
1613 * we do not need to worry about shifting.
1618 wallcycle_start(wcycle, ewcPP_PMESENDX);
1620 bBS = (inputrec->nwall == 2);
1623 copy_mat(box, boxs);
1624 svmul(inputrec->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
1627 if (EEL_PME(fr->eeltype))
1629 pme_flags |= GMX_PME_DO_COULOMB;
1632 if (EVDW_PME(fr->vdwtype))
1634 pme_flags |= GMX_PME_DO_LJ;
1637 gmx_pme_send_coordinates(cr, bBS ? boxs : box, x,
1638 mdatoms->nChargePerturbed, mdatoms->nTypePerturbed, lambda[efptCOUL], lambda[efptVDW],
1639 (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)),
1642 wallcycle_stop(wcycle, ewcPP_PMESENDX);
1644 #endif /* GMX_MPI */
1646 /* Communicate coordinates and sum dipole if necessary */
1647 if (DOMAINDECOMP(cr))
1649 wallcycle_start(wcycle, ewcMOVEX);
1650 dd_move_x(cr->dd, box, x);
1651 wallcycle_stop(wcycle, ewcMOVEX);
1654 /* update adress weight beforehand */
1655 if (bStateChanged && bDoAdressWF)
1657 /* need pbc for adress weight calculation with pbc_dx */
1658 set_pbc(&pbc, inputrec->ePBC, box);
1659 if (fr->adress_site == eAdressSITEcog)
1661 update_adress_weights_cog(top->idef.iparams, top->idef.il, x, fr, mdatoms,
1662 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1664 else if (fr->adress_site == eAdressSITEcom)
1666 update_adress_weights_com(fplog, cg0, cg1, &(top->cgs), x, fr, mdatoms,
1667 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1669 else if (fr->adress_site == eAdressSITEatomatom)
1671 update_adress_weights_atom_per_atom(cg0, cg1, &(top->cgs), x, fr, mdatoms,
1672 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1676 update_adress_weights_atom(cg0, cg1, &(top->cgs), x, fr, mdatoms,
1677 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1681 if (NEED_MUTOT(*inputrec))
1688 gmx_sumd(2*DIM, mu, cr);
1690 for (i = 0; i < 2; i++)
1692 for (j = 0; j < DIM; j++)
1694 fr->mu_tot[i][j] = mu[i*DIM + j];
1698 if (fr->efep == efepNO)
1700 copy_rvec(fr->mu_tot[0], mu_tot);
1704 for (j = 0; j < DIM; j++)
1707 (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] + lambda[efptCOUL]*fr->mu_tot[1][j];
1712 /* Reset energies */
1713 reset_enerdata(fr, bNS, enerd, MASTER(cr));
1714 clear_rvecs(SHIFTS, fr->fshift);
1718 wallcycle_start(wcycle, ewcNS);
1720 if (graph && bStateChanged)
1722 /* Calculate intramolecular shift vectors to make molecules whole */
1723 mk_mshift(fplog, graph, fr->ePBC, box, x);
1726 /* Do the actual neighbour searching */
1728 groups, top, mdatoms,
1729 cr, nrnb, bFillGrid,
1732 wallcycle_stop(wcycle, ewcNS);
1735 if (inputrec->implicit_solvent && bNS)
1737 make_gb_nblist(cr, inputrec->gb_algorithm,
1738 x, box, fr, &top->idef, graph, fr->born);
1741 if (DOMAINDECOMP(cr) && !(cr->duty & DUTY_PME))
1743 wallcycle_start(wcycle, ewcPPDURINGPME);
1744 dd_force_flop_start(cr->dd, nrnb);
1749 /* Enforced rotation has its own cycle counter that starts after the collective
1750 * coordinates have been communicated. It is added to ddCyclF to allow
1751 * for proper load-balancing */
1752 wallcycle_start(wcycle, ewcROT);
1753 do_rotation(cr, inputrec, box, x, t, step, wcycle, bNS);
1754 wallcycle_stop(wcycle, ewcROT);
1757 /* Start the force cycle counter.
1758 * This counter is stopped after do_force_lowlevel.
1759 * No parallel communication should occur while this counter is running,
1760 * since that will interfere with the dynamic load balancing.
1762 wallcycle_start(wcycle, ewcFORCE);
1766 /* Reset forces for which the virial is calculated separately:
1767 * PME/Ewald forces if necessary */
1768 if (fr->bF_NoVirSum)
1770 if (flags & GMX_FORCE_VIRIAL)
1772 fr->f_novirsum = fr->f_novirsum_alloc;
1775 clear_rvecs(fr->f_novirsum_n, fr->f_novirsum);
1779 clear_rvecs(homenr, fr->f_novirsum+start);
1784 /* We are not calculating the pressure so we do not need
1785 * a separate array for forces that do not contribute
1792 /* Clear the short- and long-range forces */
1793 clear_rvecs(fr->natoms_force_constr, f);
1794 if (bSepLRF && do_per_step(step, inputrec->nstcalclr))
1796 clear_rvecs(fr->natoms_force_constr, fr->f_twin);
1799 clear_rvec(fr->vir_diag_posres);
1801 if (inputrec->bPull && inputrec->pull->bConstraint)
1803 clear_pull_forces(inputrec->pull);
1806 /* update QMMMrec, if necessary */
1809 update_QMMMrec(cr, fr, x, mdatoms, box, top);
1812 /* Compute the bonded and non-bonded energies and optionally forces */
1813 do_force_lowlevel(fr, inputrec, &(top->idef),
1814 cr, nrnb, wcycle, mdatoms,
1815 x, hist, f, bSepLRF ? fr->f_twin : f, enerd, fcd, top, fr->born,
1817 inputrec->fepvals, lambda,
1818 graph, &(top->excls), fr->mu_tot,
1824 if (do_per_step(step, inputrec->nstcalclr))
1826 /* Add the long range forces to the short range forces */
1827 for (i = 0; i < fr->natoms_force_constr; i++)
1829 rvec_add(fr->f_twin[i], f[i], f[i]);
1834 cycles_force = wallcycle_stop(wcycle, ewcFORCE);
1838 do_flood(cr, inputrec, x, f, ed, box, step, bNS);
1841 if (DOMAINDECOMP(cr))
1843 dd_force_flop_stop(cr->dd, nrnb);
1846 dd_cycles_add(cr->dd, cycles_force-cycles_pme, ddCyclF);
1852 if (IR_ELEC_FIELD(*inputrec))
1854 /* Compute forces due to electric field */
1855 calc_f_el(MASTER(cr) ? field : NULL,
1856 start, homenr, mdatoms->chargeA, fr->f_novirsum,
1857 inputrec->ex, inputrec->et, t);
1860 if (bDoAdressWF && fr->adress_icor == eAdressICThermoForce)
1862 /* Compute thermodynamic force in hybrid AdResS region */
1863 adress_thermo_force(start, homenr, &(top->cgs), x, fr->f_novirsum, fr, mdatoms,
1864 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1867 /* Communicate the forces */
1868 if (DOMAINDECOMP(cr))
1870 wallcycle_start(wcycle, ewcMOVEF);
1871 dd_move_f(cr->dd, f, fr->fshift);
1872 /* Do we need to communicate the separate force array
1873 * for terms that do not contribute to the single sum virial?
1874 * Position restraints and electric fields do not introduce
1875 * inter-cg forces, only full electrostatics methods do.
1876 * When we do not calculate the virial, fr->f_novirsum = f,
1877 * so we have already communicated these forces.
1879 if (EEL_FULL(fr->eeltype) && cr->dd->n_intercg_excl &&
1880 (flags & GMX_FORCE_VIRIAL))
1882 dd_move_f(cr->dd, fr->f_novirsum, NULL);
1886 /* We should not update the shift forces here,
1887 * since f_twin is already included in f.
1889 dd_move_f(cr->dd, fr->f_twin, NULL);
1891 wallcycle_stop(wcycle, ewcMOVEF);
1894 /* If we have NoVirSum forces, but we do not calculate the virial,
1895 * we sum fr->f_novirsum=f later.
1897 if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1899 wallcycle_start(wcycle, ewcVSITESPREAD);
1900 spread_vsite_f(vsite, x, f, fr->fshift, FALSE, NULL, nrnb,
1901 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1902 wallcycle_stop(wcycle, ewcVSITESPREAD);
1906 wallcycle_start(wcycle, ewcVSITESPREAD);
1907 spread_vsite_f(vsite, x, fr->f_twin, NULL, FALSE, NULL,
1909 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1910 wallcycle_stop(wcycle, ewcVSITESPREAD);
1914 if (flags & GMX_FORCE_VIRIAL)
1916 /* Calculation of the virial must be done after vsites! */
1917 calc_virial(0, mdatoms->homenr, x, f,
1918 vir_force, graph, box, nrnb, fr, inputrec->ePBC);
1922 if (inputrec->bPull && inputrec->pull->bPotential)
1924 pull_potential_wrapper(cr, inputrec, box, x,
1925 f, vir_force, mdatoms, enerd, lambda, t,
1929 /* Add the forces from enforced rotation potentials (if any) */
1932 wallcycle_start(wcycle, ewcROTadd);
1933 enerd->term[F_COM_PULL] += add_rot_forces(inputrec->rot, f, cr, step, t);
1934 wallcycle_stop(wcycle, ewcROTadd);
1937 /* Add forces from interactive molecular dynamics (IMD), if bIMD == TRUE. */
1938 IMD_apply_forces(inputrec->bIMD, inputrec->imd, cr, f, wcycle);
1940 if (PAR(cr) && !(cr->duty & DUTY_PME))
1942 /* In case of node-splitting, the PP nodes receive the long-range
1943 * forces, virial and energy from the PME nodes here.
1945 pme_receive_force_ener(cr, wcycle, enerd, fr);
1950 post_process_forces(cr, step, nrnb, wcycle,
1951 top, box, x, f, vir_force, mdatoms, graph, fr, vsite,
1955 /* Sum the potential energy terms from group contributions */
1956 sum_epot(&(enerd->grpp), enerd->term);
1959 void do_force(FILE *fplog, t_commrec *cr,
1960 t_inputrec *inputrec,
1961 gmx_int64_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
1962 gmx_localtop_t *top,
1963 gmx_groups_t *groups,
1964 matrix box, rvec x[], history_t *hist,
1968 gmx_enerdata_t *enerd, t_fcdata *fcd,
1969 real *lambda, t_graph *graph,
1971 gmx_vsite_t *vsite, rvec mu_tot,
1972 double t, FILE *field, gmx_edsam_t ed,
1973 gmx_bool bBornRadii,
1976 /* modify force flag if not doing nonbonded */
1977 if (!fr->bNonbonded)
1979 flags &= ~GMX_FORCE_NONBONDED;
1982 switch (inputrec->cutoff_scheme)
1985 do_force_cutsVERLET(fplog, cr, inputrec,
2001 do_force_cutsGROUP(fplog, cr, inputrec,
2016 gmx_incons("Invalid cut-off scheme passed!");
2021 void do_constrain_first(FILE *fplog, gmx_constr_t constr,
2022 t_inputrec *ir, t_mdatoms *md,
2023 t_state *state, t_commrec *cr, t_nrnb *nrnb,
2024 t_forcerec *fr, gmx_localtop_t *top)
2026 int i, m, start, end;
2028 real dt = ir->delta_t;
2032 snew(savex, state->natoms);
2039 fprintf(debug, "vcm: start=%d, homenr=%d, end=%d\n",
2040 start, md->homenr, end);
2042 /* Do a first constrain to reset particles... */
2043 step = ir->init_step;
2046 char buf[STEPSTRSIZE];
2047 fprintf(fplog, "\nConstraining the starting coordinates (step %s)\n",
2048 gmx_step_str(step, buf));
2052 /* constrain the current position */
2053 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
2054 ir, cr, step, 0, 1.0, md,
2055 state->x, state->x, NULL,
2056 fr->bMolPBC, state->box,
2057 state->lambda[efptBONDED], &dvdl_dum,
2058 NULL, NULL, nrnb, econqCoord);
2061 /* constrain the inital velocity, and save it */
2062 /* also may be useful if we need the ekin from the halfstep for velocity verlet */
2063 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
2064 ir, cr, step, 0, 1.0, md,
2065 state->x, state->v, state->v,
2066 fr->bMolPBC, state->box,
2067 state->lambda[efptBONDED], &dvdl_dum,
2068 NULL, NULL, nrnb, econqVeloc);
2070 /* constrain the inital velocities at t-dt/2 */
2071 if (EI_STATE_VELOCITY(ir->eI) && ir->eI != eiVV)
2073 for (i = start; (i < end); i++)
2075 for (m = 0; (m < DIM); m++)
2077 /* Reverse the velocity */
2078 state->v[i][m] = -state->v[i][m];
2079 /* Store the position at t-dt in buf */
2080 savex[i][m] = state->x[i][m] + dt*state->v[i][m];
2083 /* Shake the positions at t=-dt with the positions at t=0
2084 * as reference coordinates.
2088 char buf[STEPSTRSIZE];
2089 fprintf(fplog, "\nConstraining the coordinates at t0-dt (step %s)\n",
2090 gmx_step_str(step, buf));
2093 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
2094 ir, cr, step, -1, 1.0, md,
2095 state->x, savex, NULL,
2096 fr->bMolPBC, state->box,
2097 state->lambda[efptBONDED], &dvdl_dum,
2098 state->v, NULL, nrnb, econqCoord);
2100 for (i = start; i < end; i++)
2102 for (m = 0; m < DIM; m++)
2104 /* Re-reverse the velocities */
2105 state->v[i][m] = -state->v[i][m];
2114 integrate_table(real vdwtab[], real scale, int offstart, int rstart, int rend,
2115 double *enerout, double *virout)
2117 double enersum, virsum;
2118 double invscale, invscale2, invscale3;
2119 double r, ea, eb, ec, pa, pb, pc, pd;
2124 invscale = 1.0/scale;
2125 invscale2 = invscale*invscale;
2126 invscale3 = invscale*invscale2;
2128 /* Following summation derived from cubic spline definition,
2129 * Numerical Recipies in C, second edition, p. 113-116. Exact for
2130 * the cubic spline. We first calculate the negative of the
2131 * energy from rvdw to rvdw_switch, assuming that g(r)=1, and then
2132 * add the more standard, abrupt cutoff correction to that result,
2133 * yielding the long-range correction for a switched function. We
2134 * perform both the pressure and energy loops at the same time for
2135 * simplicity, as the computational cost is low. */
2139 /* Since the dispersion table has been scaled down a factor
2140 * 6.0 and the repulsion a factor 12.0 to compensate for the
2141 * c6/c12 parameters inside nbfp[] being scaled up (to save
2142 * flops in kernels), we need to correct for this.
2153 for (ri = rstart; ri < rend; ++ri)
2157 eb = 2.0*invscale2*r;
2161 pb = 3.0*invscale2*r;
2162 pc = 3.0*invscale*r*r;
2165 /* this "8" is from the packing in the vdwtab array - perhaps
2166 should be defined? */
2168 offset = 8*ri + offstart;
2169 y0 = vdwtab[offset];
2170 f = vdwtab[offset+1];
2171 g = vdwtab[offset+2];
2172 h = vdwtab[offset+3];
2174 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);
2175 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);
2177 *enerout = 4.0*M_PI*enersum*tabfactor;
2178 *virout = 4.0*M_PI*virsum*tabfactor;
2181 void calc_enervirdiff(FILE *fplog, int eDispCorr, t_forcerec *fr)
2183 double eners[2], virs[2], enersum, virsum;
2184 double r0, rc3, rc9;
2186 real scale, *vdwtab;
2188 fr->enershiftsix = 0;
2189 fr->enershifttwelve = 0;
2190 fr->enerdiffsix = 0;
2191 fr->enerdifftwelve = 0;
2193 fr->virdifftwelve = 0;
2195 if (eDispCorr != edispcNO)
2197 for (i = 0; i < 2; i++)
2202 if ((fr->vdw_modifier == eintmodPOTSHIFT) ||
2203 (fr->vdw_modifier == eintmodPOTSWITCH) ||
2204 (fr->vdw_modifier == eintmodFORCESWITCH) ||
2205 (fr->vdwtype == evdwSHIFT) ||
2206 (fr->vdwtype == evdwSWITCH))
2208 if (((fr->vdw_modifier == eintmodPOTSWITCH) ||
2209 (fr->vdw_modifier == eintmodFORCESWITCH) ||
2210 (fr->vdwtype == evdwSWITCH)) && fr->rvdw_switch == 0)
2213 "With dispersion correction rvdw-switch can not be zero "
2214 "for vdw-type = %s", evdw_names[fr->vdwtype]);
2217 scale = fr->nblists[0].table_vdw.scale;
2218 vdwtab = fr->nblists[0].table_vdw.data;
2220 /* Round the cut-offs to exact table values for precision */
2221 ri0 = static_cast<int>(floor(fr->rvdw_switch*scale));
2222 ri1 = static_cast<int>(ceil(fr->rvdw*scale));
2224 /* The code below has some support for handling force-switching, i.e.
2225 * when the force (instead of potential) is switched over a limited
2226 * region. This leads to a constant shift in the potential inside the
2227 * switching region, which we can handle by adding a constant energy
2228 * term in the force-switch case just like when we do potential-shift.
2230 * For now this is not enabled, but to keep the functionality in the
2231 * code we check separately for switch and shift. When we do force-switch
2232 * the shifting point is rvdw_switch, while it is the cutoff when we
2233 * have a classical potential-shift.
2235 * For a pure potential-shift the potential has a constant shift
2236 * all the way out to the cutoff, and that is it. For other forms
2237 * we need to calculate the constant shift up to the point where we
2238 * start modifying the potential.
2240 ri0 = (fr->vdw_modifier == eintmodPOTSHIFT) ? ri1 : ri0;
2246 if ((fr->vdw_modifier == eintmodFORCESWITCH) ||
2247 (fr->vdwtype == evdwSHIFT))
2249 /* Determine the constant energy shift below rvdw_switch.
2250 * Table has a scale factor since we have scaled it down to compensate
2251 * for scaling-up c6/c12 with the derivative factors to save flops in analytical kernels.
2253 fr->enershiftsix = (real)(-1.0/(rc3*rc3)) - 6.0*vdwtab[8*ri0];
2254 fr->enershifttwelve = (real)( 1.0/(rc9*rc3)) - 12.0*vdwtab[8*ri0 + 4];
2256 else if (fr->vdw_modifier == eintmodPOTSHIFT)
2258 fr->enershiftsix = (real)(-1.0/(rc3*rc3));
2259 fr->enershifttwelve = (real)( 1.0/(rc9*rc3));
2262 /* Add the constant part from 0 to rvdw_switch.
2263 * This integration from 0 to rvdw_switch overcounts the number
2264 * of interactions by 1, as it also counts the self interaction.
2265 * We will correct for this later.
2267 eners[0] += 4.0*M_PI*fr->enershiftsix*rc3/3.0;
2268 eners[1] += 4.0*M_PI*fr->enershifttwelve*rc3/3.0;
2270 /* Calculate the contribution in the range [r0,r1] where we
2271 * modify the potential. For a pure potential-shift modifier we will
2272 * have ri0==ri1, and there will not be any contribution here.
2274 for (i = 0; i < 2; i++)
2278 integrate_table(vdwtab, scale, (i == 0 ? 0 : 4), ri0, ri1, &enersum, &virsum);
2279 eners[i] -= enersum;
2283 /* Alright: Above we compensated by REMOVING the parts outside r0
2284 * corresponding to the ideal VdW 1/r6 and /r12 potentials.
2286 * Regardless of whether r0 is the point where we start switching,
2287 * or the cutoff where we calculated the constant shift, we include
2288 * all the parts we are missing out to infinity from r0 by
2289 * calculating the analytical dispersion correction.
2291 eners[0] += -4.0*M_PI/(3.0*rc3);
2292 eners[1] += 4.0*M_PI/(9.0*rc9);
2293 virs[0] += 8.0*M_PI/rc3;
2294 virs[1] += -16.0*M_PI/(3.0*rc9);
2296 else if (fr->vdwtype == evdwCUT ||
2297 EVDW_PME(fr->vdwtype) ||
2298 fr->vdwtype == evdwUSER)
2300 if (fr->vdwtype == evdwUSER && fplog)
2303 "WARNING: using dispersion correction with user tables\n");
2306 /* Note that with LJ-PME, the dispersion correction is multiplied
2307 * by the difference between the actual C6 and the value of C6
2308 * that would produce the combination rule.
2309 * This means the normal energy and virial difference formulas
2313 rc3 = fr->rvdw*fr->rvdw*fr->rvdw;
2315 /* Contribution beyond the cut-off */
2316 eners[0] += -4.0*M_PI/(3.0*rc3);
2317 eners[1] += 4.0*M_PI/(9.0*rc9);
2318 if (fr->vdw_modifier == eintmodPOTSHIFT)
2320 /* Contribution within the cut-off */
2321 eners[0] += -4.0*M_PI/(3.0*rc3);
2322 eners[1] += 4.0*M_PI/(3.0*rc9);
2324 /* Contribution beyond the cut-off */
2325 virs[0] += 8.0*M_PI/rc3;
2326 virs[1] += -16.0*M_PI/(3.0*rc9);
2331 "Dispersion correction is not implemented for vdw-type = %s",
2332 evdw_names[fr->vdwtype]);
2335 /* When we deprecate the group kernels the code below can go too */
2336 if (fr->vdwtype == evdwPME && fr->cutoff_scheme == ecutsGROUP)
2338 /* Calculate self-interaction coefficient (assuming that
2339 * the reciprocal-space contribution is constant in the
2340 * region that contributes to the self-interaction).
2342 fr->enershiftsix = pow(fr->ewaldcoeff_lj, 6) / 6.0;
2344 eners[0] += -pow(sqrt(M_PI)*fr->ewaldcoeff_lj, 3)/3.0;
2345 virs[0] += pow(sqrt(M_PI)*fr->ewaldcoeff_lj, 3);
2348 fr->enerdiffsix = eners[0];
2349 fr->enerdifftwelve = eners[1];
2350 /* The 0.5 is due to the Gromacs definition of the virial */
2351 fr->virdiffsix = 0.5*virs[0];
2352 fr->virdifftwelve = 0.5*virs[1];
2356 void calc_dispcorr(t_inputrec *ir, t_forcerec *fr,
2358 matrix box, real lambda, tensor pres, tensor virial,
2359 real *prescorr, real *enercorr, real *dvdlcorr)
2361 gmx_bool bCorrAll, bCorrPres;
2362 real dvdlambda, invvol, dens, ninter, avcsix, avctwelve, enerdiff, svir = 0, spres = 0;
2372 if (ir->eDispCorr != edispcNO)
2374 bCorrAll = (ir->eDispCorr == edispcAllEner ||
2375 ir->eDispCorr == edispcAllEnerPres);
2376 bCorrPres = (ir->eDispCorr == edispcEnerPres ||
2377 ir->eDispCorr == edispcAllEnerPres);
2379 invvol = 1/det(box);
2382 /* Only correct for the interactions with the inserted molecule */
2383 dens = (natoms - fr->n_tpi)*invvol;
2388 dens = natoms*invvol;
2389 ninter = 0.5*natoms;
2392 if (ir->efep == efepNO)
2394 avcsix = fr->avcsix[0];
2395 avctwelve = fr->avctwelve[0];
2399 avcsix = (1 - lambda)*fr->avcsix[0] + lambda*fr->avcsix[1];
2400 avctwelve = (1 - lambda)*fr->avctwelve[0] + lambda*fr->avctwelve[1];
2403 enerdiff = ninter*(dens*fr->enerdiffsix - fr->enershiftsix);
2404 *enercorr += avcsix*enerdiff;
2406 if (ir->efep != efepNO)
2408 dvdlambda += (fr->avcsix[1] - fr->avcsix[0])*enerdiff;
2412 enerdiff = ninter*(dens*fr->enerdifftwelve - fr->enershifttwelve);
2413 *enercorr += avctwelve*enerdiff;
2414 if (fr->efep != efepNO)
2416 dvdlambda += (fr->avctwelve[1] - fr->avctwelve[0])*enerdiff;
2422 svir = ninter*dens*avcsix*fr->virdiffsix/3.0;
2423 if (ir->eDispCorr == edispcAllEnerPres)
2425 svir += ninter*dens*avctwelve*fr->virdifftwelve/3.0;
2427 /* The factor 2 is because of the Gromacs virial definition */
2428 spres = -2.0*invvol*svir*PRESFAC;
2430 for (m = 0; m < DIM; m++)
2432 virial[m][m] += svir;
2433 pres[m][m] += spres;
2438 /* Can't currently control when it prints, for now, just print when degugging */
2443 fprintf(debug, "Long Range LJ corr.: <C6> %10.4e, <C12> %10.4e\n",
2449 "Long Range LJ corr.: Epot %10g, Pres: %10g, Vir: %10g\n",
2450 *enercorr, spres, svir);
2454 fprintf(debug, "Long Range LJ corr.: Epot %10g\n", *enercorr);
2458 if (fr->efep != efepNO)
2460 *dvdlcorr += dvdlambda;
2465 void do_pbc_first(FILE *fplog, matrix box, t_forcerec *fr,
2466 t_graph *graph, rvec x[])
2470 fprintf(fplog, "Removing pbc first time\n");
2472 calc_shifts(box, fr->shift_vec);
2475 mk_mshift(fplog, graph, fr->ePBC, box, x);
2478 p_graph(debug, "do_pbc_first 1", graph);
2480 shift_self(graph, box, x);
2481 /* By doing an extra mk_mshift the molecules that are broken
2482 * because they were e.g. imported from another software
2483 * will be made whole again. Such are the healing powers
2486 mk_mshift(fplog, graph, fr->ePBC, box, x);
2489 p_graph(debug, "do_pbc_first 2", graph);
2494 fprintf(fplog, "Done rmpbc\n");
2498 static void low_do_pbc_mtop(FILE *fplog, int ePBC, matrix box,
2499 gmx_mtop_t *mtop, rvec x[],
2504 gmx_molblock_t *molb;
2506 if (bFirst && fplog)
2508 fprintf(fplog, "Removing pbc first time\n");
2513 for (mb = 0; mb < mtop->nmolblock; mb++)
2515 molb = &mtop->molblock[mb];
2516 if (molb->natoms_mol == 1 ||
2517 (!bFirst && mtop->moltype[molb->type].cgs.nr == 1))
2519 /* Just one atom or charge group in the molecule, no PBC required */
2520 as += molb->nmol*molb->natoms_mol;
2524 /* Pass NULL iso fplog to avoid graph prints for each molecule type */
2525 mk_graph_ilist(NULL, mtop->moltype[molb->type].ilist,
2526 0, molb->natoms_mol, FALSE, FALSE, graph);
2528 for (mol = 0; mol < molb->nmol; mol++)
2530 mk_mshift(fplog, graph, ePBC, box, x+as);
2532 shift_self(graph, box, x+as);
2533 /* The molecule is whole now.
2534 * We don't need the second mk_mshift call as in do_pbc_first,
2535 * since we no longer need this graph.
2538 as += molb->natoms_mol;
2546 void do_pbc_first_mtop(FILE *fplog, int ePBC, matrix box,
2547 gmx_mtop_t *mtop, rvec x[])
2549 low_do_pbc_mtop(fplog, ePBC, box, mtop, x, TRUE);
2552 void do_pbc_mtop(FILE *fplog, int ePBC, matrix box,
2553 gmx_mtop_t *mtop, rvec x[])
2555 low_do_pbc_mtop(fplog, ePBC, box, mtop, x, FALSE);
2558 void finish_run(FILE *fplog, t_commrec *cr,
2559 t_inputrec *inputrec,
2560 t_nrnb nrnb[], gmx_wallcycle_t wcycle,
2561 gmx_walltime_accounting_t walltime_accounting,
2562 nonbonded_verlet_t *nbv,
2563 gmx_bool bWriteStat)
2565 t_nrnb *nrnb_tot = NULL;
2567 double nbfs = 0, mflop = 0;
2568 double elapsed_time,
2569 elapsed_time_over_all_ranks,
2570 elapsed_time_over_all_threads,
2571 elapsed_time_over_all_threads_over_all_ranks;
2572 wallcycle_sum(cr, wcycle);
2578 MPI_Allreduce(nrnb->n, nrnb_tot->n, eNRNB, MPI_DOUBLE, MPI_SUM,
2579 cr->mpi_comm_mysim);
2587 elapsed_time = walltime_accounting_get_elapsed_time(walltime_accounting);
2588 elapsed_time_over_all_ranks = elapsed_time;
2589 elapsed_time_over_all_threads = walltime_accounting_get_elapsed_time_over_all_threads(walltime_accounting);
2590 elapsed_time_over_all_threads_over_all_ranks = elapsed_time_over_all_threads;
2594 /* reduce elapsed_time over all MPI ranks in the current simulation */
2595 MPI_Allreduce(&elapsed_time,
2596 &elapsed_time_over_all_ranks,
2597 1, MPI_DOUBLE, MPI_SUM,
2598 cr->mpi_comm_mysim);
2599 elapsed_time_over_all_ranks /= cr->nnodes;
2600 /* Reduce elapsed_time_over_all_threads over all MPI ranks in the
2601 * current simulation. */
2602 MPI_Allreduce(&elapsed_time_over_all_threads,
2603 &elapsed_time_over_all_threads_over_all_ranks,
2604 1, MPI_DOUBLE, MPI_SUM,
2605 cr->mpi_comm_mysim);
2611 print_flop(fplog, nrnb_tot, &nbfs, &mflop);
2618 if ((cr->duty & DUTY_PP) && DOMAINDECOMP(cr))
2620 print_dd_statistics(cr, inputrec, fplog);
2625 struct gmx_wallclock_gpu_t* gputimes = use_GPU(nbv) ? nbnxn_gpu_get_timings(nbv->gpu_nbv) : NULL;
2627 wallcycle_print(fplog, cr->nnodes, cr->npmenodes,
2628 elapsed_time_over_all_ranks,
2631 if (EI_DYNAMICS(inputrec->eI))
2633 delta_t = inputrec->delta_t;
2638 print_perf(fplog, elapsed_time_over_all_threads_over_all_ranks,
2639 elapsed_time_over_all_ranks,
2640 walltime_accounting_get_nsteps_done(walltime_accounting),
2641 delta_t, nbfs, mflop);
2645 print_perf(stderr, elapsed_time_over_all_threads_over_all_ranks,
2646 elapsed_time_over_all_ranks,
2647 walltime_accounting_get_nsteps_done(walltime_accounting),
2648 delta_t, nbfs, mflop);
2653 extern void initialize_lambdas(FILE *fplog, t_inputrec *ir, int *fep_state, real *lambda, double *lam0)
2655 /* this function works, but could probably use a logic rewrite to keep all the different
2656 types of efep straight. */
2659 t_lambda *fep = ir->fepvals;
2661 if ((ir->efep == efepNO) && (ir->bSimTemp == FALSE))
2663 for (i = 0; i < efptNR; i++)
2675 *fep_state = fep->init_fep_state; /* this might overwrite the checkpoint
2676 if checkpoint is set -- a kludge is in for now
2678 for (i = 0; i < efptNR; i++)
2680 /* overwrite lambda state with init_lambda for now for backwards compatibility */
2681 if (fep->init_lambda >= 0) /* if it's -1, it was never initializd */
2683 lambda[i] = fep->init_lambda;
2686 lam0[i] = lambda[i];
2691 lambda[i] = fep->all_lambda[i][*fep_state];
2694 lam0[i] = lambda[i];
2700 /* need to rescale control temperatures to match current state */
2701 for (i = 0; i < ir->opts.ngtc; i++)
2703 if (ir->opts.ref_t[i] > 0)
2705 ir->opts.ref_t[i] = ir->simtempvals->temperatures[*fep_state];
2711 /* Send to the log the information on the current lambdas */
2714 fprintf(fplog, "Initial vector of lambda components:[ ");
2715 for (i = 0; i < efptNR; i++)
2717 fprintf(fplog, "%10.4f ", lambda[i]);
2719 fprintf(fplog, "]\n");
2725 void init_md(FILE *fplog,
2726 t_commrec *cr, t_inputrec *ir, const output_env_t oenv,
2727 double *t, double *t0,
2728 real *lambda, int *fep_state, double *lam0,
2729 t_nrnb *nrnb, gmx_mtop_t *mtop,
2731 int nfile, const t_filenm fnm[],
2732 gmx_mdoutf_t *outf, t_mdebin **mdebin,
2733 tensor force_vir, tensor shake_vir, rvec mu_tot,
2734 gmx_bool *bSimAnn, t_vcm **vcm, unsigned long Flags,
2735 gmx_wallcycle_t wcycle)
2739 /* Initial values */
2740 *t = *t0 = ir->init_t;
2743 for (i = 0; i < ir->opts.ngtc; i++)
2745 /* set bSimAnn if any group is being annealed */
2746 if (ir->opts.annealing[i] != eannNO)
2753 update_annealing_target_temp(&(ir->opts), ir->init_t);
2756 /* Initialize lambda variables */
2757 initialize_lambdas(fplog, ir, fep_state, lambda, lam0);
2761 *upd = init_update(ir);
2767 *vcm = init_vcm(fplog, &mtop->groups, ir);
2770 if (EI_DYNAMICS(ir->eI) && !(Flags & MD_APPENDFILES))
2772 if (ir->etc == etcBERENDSEN)
2774 please_cite(fplog, "Berendsen84a");
2776 if (ir->etc == etcVRESCALE)
2778 please_cite(fplog, "Bussi2007a");
2780 if (ir->eI == eiSD1)
2782 please_cite(fplog, "Goga2012");
2785 if ((ir->et[XX].n > 0) || (ir->et[YY].n > 0) || (ir->et[ZZ].n > 0))
2787 please_cite(fplog, "Caleman2008a");
2793 *outf = init_mdoutf(fplog, nfile, fnm, Flags, cr, ir, mtop, oenv, wcycle);
2795 *mdebin = init_mdebin((Flags & MD_APPENDFILES) ? NULL : mdoutf_get_fp_ene(*outf),
2796 mtop, ir, mdoutf_get_fp_dhdl(*outf));
2801 please_cite(fplog, "Fritsch12");
2802 please_cite(fplog, "Junghans10");
2804 /* Initiate variables */
2805 clear_mat(force_vir);
2806 clear_mat(shake_vir);