2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
43 #ifdef HAVE_SYS_TIME_H
48 #include "gromacs/utility/cstringutil.h"
51 #include "gromacs/pbcutil/pbc.h"
52 #include "chargegroup.h"
53 #include "gromacs/math/vec.h"
58 #include "gromacs/math/units.h"
71 #include "nbnxn_atomdata.h"
72 #include "nbnxn_search.h"
73 #include "nbnxn_kernels/nbnxn_kernel_ref.h"
74 #include "nbnxn_kernels/simd_4xn/nbnxn_kernel_simd_4xn.h"
75 #include "nbnxn_kernels/simd_2xnn/nbnxn_kernel_simd_2xnn.h"
76 #include "nbnxn_kernels/nbnxn_kernel_gpu_ref.h"
77 #include "nonbonded.h"
78 #include "../gmxlib/nonbonded/nb_kernel.h"
79 #include "../gmxlib/nonbonded/nb_free_energy.h"
81 #include "gromacs/legacyheaders/types/commrec.h"
82 #include "gromacs/pbcutil/ishift.h"
83 #include "gromacs/pbcutil/mshift.h"
84 #include "gromacs/timing/wallcycle.h"
85 #include "gromacs/timing/walltime_accounting.h"
86 #include "gromacs/utility/gmxmpi.h"
87 #include "gromacs/utility/smalloc.h"
88 #include "gromacs/essentialdynamics/edsam.h"
89 #include "gromacs/pulling/pull.h"
90 #include "gromacs/pulling/pull_rotation.h"
91 #include "gromacs/imd/imd.h"
95 #include "gmx_omp_nthreads.h"
97 #include "nbnxn_cuda_data_mgmt.h"
98 #include "nbnxn_cuda/nbnxn_cuda.h"
100 void print_time(FILE *out,
101 gmx_walltime_accounting_t walltime_accounting,
104 t_commrec gmx_unused *cr)
107 char timebuf[STRLEN];
108 double dt, elapsed_seconds, time_per_step;
111 #ifndef GMX_THREAD_MPI
117 fprintf(out, "step %s", gmx_step_str(step, buf));
118 if ((step >= ir->nstlist))
120 double seconds_since_epoch = gmx_gettime();
121 elapsed_seconds = seconds_since_epoch - walltime_accounting_get_start_time_stamp(walltime_accounting);
122 time_per_step = elapsed_seconds/(step - ir->init_step + 1);
123 dt = (ir->nsteps + ir->init_step - step) * time_per_step;
129 finish = (time_t) (seconds_since_epoch + dt);
130 gmx_ctime_r(&finish, timebuf, STRLEN);
131 sprintf(buf, "%s", timebuf);
132 buf[strlen(buf)-1] = '\0';
133 fprintf(out, ", will finish %s", buf);
137 fprintf(out, ", remaining wall clock time: %5d s ", (int)dt);
142 fprintf(out, " performance: %.1f ns/day ",
143 ir->delta_t/1000*24*60*60/time_per_step);
146 #ifndef GMX_THREAD_MPI
156 void print_date_and_time(FILE *fplog, int nodeid, const char *title,
159 char time_string[STRLEN];
168 char timebuf[STRLEN];
169 time_t temp_time = (time_t) the_time;
171 gmx_ctime_r(&temp_time, timebuf, STRLEN);
172 for (i = 0; timebuf[i] >= ' '; i++)
174 time_string[i] = timebuf[i];
176 time_string[i] = '\0';
179 fprintf(fplog, "%s on rank %d %s\n", title, nodeid, time_string);
182 void print_start(FILE *fplog, t_commrec *cr,
183 gmx_walltime_accounting_t walltime_accounting,
188 sprintf(buf, "Started %s", name);
189 print_date_and_time(fplog, cr->nodeid, buf,
190 walltime_accounting_get_start_time_stamp(walltime_accounting));
193 static void sum_forces(int start, int end, rvec f[], rvec flr[])
199 pr_rvecs(debug, 0, "fsr", f+start, end-start);
200 pr_rvecs(debug, 0, "flr", flr+start, end-start);
202 for (i = start; (i < end); i++)
204 rvec_inc(f[i], flr[i]);
209 * calc_f_el calculates forces due to an electric field.
211 * force is kJ mol^-1 nm^-1 = e * kJ mol^-1 nm^-1 / e
213 * Et[] contains the parameters for the time dependent
214 * part of the field (not yet used).
215 * Ex[] contains the parameters for
216 * the spatial dependent part of the field. You can have cool periodic
217 * fields in principle, but only a constant field is supported
219 * The function should return the energy due to the electric field
220 * (if any) but for now returns 0.
223 * There can be problems with the virial.
224 * Since the field is not self-consistent this is unavoidable.
225 * For neutral molecules the virial is correct within this approximation.
226 * For neutral systems with many charged molecules the error is small.
227 * But for systems with a net charge or a few charged molecules
228 * the error can be significant when the field is high.
229 * Solution: implement a self-consitent electric field into PME.
231 static void calc_f_el(FILE *fp, int start, int homenr,
232 real charge[], rvec f[],
233 t_cosines Ex[], t_cosines Et[], double t)
239 for (m = 0; (m < DIM); m++)
246 Ext[m] = cos(Et[m].a[0]*(t-t0))*exp(-sqr(t-t0)/(2.0*sqr(Et[m].a[2])));
250 Ext[m] = cos(Et[m].a[0]*t);
259 /* Convert the field strength from V/nm to MD-units */
260 Ext[m] *= Ex[m].a[0]*FIELDFAC;
261 for (i = start; (i < start+homenr); i++)
263 f[i][m] += charge[i]*Ext[m];
273 fprintf(fp, "%10g %10g %10g %10g #FIELD\n", t,
274 Ext[XX]/FIELDFAC, Ext[YY]/FIELDFAC, Ext[ZZ]/FIELDFAC);
278 static void calc_virial(int start, int homenr, rvec x[], rvec f[],
279 tensor vir_part, t_graph *graph, matrix box,
280 t_nrnb *nrnb, const t_forcerec *fr, int ePBC)
285 /* The short-range virial from surrounding boxes */
287 calc_vir(SHIFTS, fr->shift_vec, fr->fshift, vir_part, ePBC == epbcSCREW, box);
288 inc_nrnb(nrnb, eNR_VIRIAL, SHIFTS);
290 /* Calculate partial virial, for local atoms only, based on short range.
291 * Total virial is computed in global_stat, called from do_md
293 f_calc_vir(start, start+homenr, x, f, vir_part, graph, box);
294 inc_nrnb(nrnb, eNR_VIRIAL, homenr);
296 /* Add position restraint contribution */
297 for (i = 0; i < DIM; i++)
299 vir_part[i][i] += fr->vir_diag_posres[i];
302 /* Add wall contribution */
303 for (i = 0; i < DIM; i++)
305 vir_part[i][ZZ] += fr->vir_wall_z[i];
310 pr_rvecs(debug, 0, "vir_part", vir_part, DIM);
314 static void posres_wrapper(FILE *fplog,
320 matrix box, rvec x[],
321 gmx_enerdata_t *enerd,
329 /* Position restraints always require full pbc */
330 set_pbc(&pbc, ir->ePBC, box);
332 v = posres(top->idef.il[F_POSRES].nr, top->idef.il[F_POSRES].iatoms,
333 top->idef.iparams_posres,
334 (const rvec*)x, fr->f_novirsum, fr->vir_diag_posres,
335 ir->ePBC == epbcNONE ? NULL : &pbc,
336 lambda[efptRESTRAINT], &dvdl,
337 fr->rc_scaling, fr->ePBC, fr->posres_com, fr->posres_comB);
340 gmx_print_sepdvdl(fplog, interaction_function[F_POSRES].longname, v, dvdl);
342 enerd->term[F_POSRES] += v;
343 /* If just the force constant changes, the FEP term is linear,
344 * but if k changes, it is not.
346 enerd->dvdl_nonlin[efptRESTRAINT] += dvdl;
347 inc_nrnb(nrnb, eNR_POSRES, top->idef.il[F_POSRES].nr/2);
349 if ((ir->fepvals->n_lambda > 0) && (flags & GMX_FORCE_DHDL))
351 for (i = 0; i < enerd->n_lambda; i++)
353 real dvdl_dum, lambda_dum;
355 lambda_dum = (i == 0 ? lambda[efptRESTRAINT] : ir->fepvals->all_lambda[efptRESTRAINT][i-1]);
356 v = posres(top->idef.il[F_POSRES].nr, top->idef.il[F_POSRES].iatoms,
357 top->idef.iparams_posres,
358 (const rvec*)x, NULL, NULL,
359 ir->ePBC == epbcNONE ? NULL : &pbc, lambda_dum, &dvdl,
360 fr->rc_scaling, fr->ePBC, fr->posres_com, fr->posres_comB);
361 enerd->enerpart_lambda[i] += v;
366 static void fbposres_wrapper(t_inputrec *ir,
369 matrix box, rvec x[],
370 gmx_enerdata_t *enerd,
376 /* Flat-bottomed position restraints always require full pbc */
377 set_pbc(&pbc, ir->ePBC, box);
378 v = fbposres(top->idef.il[F_FBPOSRES].nr, top->idef.il[F_FBPOSRES].iatoms,
379 top->idef.iparams_fbposres,
380 (const rvec*)x, fr->f_novirsum, fr->vir_diag_posres,
381 ir->ePBC == epbcNONE ? NULL : &pbc,
382 fr->rc_scaling, fr->ePBC, fr->posres_com);
383 enerd->term[F_FBPOSRES] += v;
384 inc_nrnb(nrnb, eNR_FBPOSRES, top->idef.il[F_FBPOSRES].nr/2);
387 static void pull_potential_wrapper(FILE *fplog,
391 matrix box, rvec x[],
395 gmx_enerdata_t *enerd,
402 /* Calculate the center of mass forces, this requires communication,
403 * which is why pull_potential is called close to other communication.
404 * The virial contribution is calculated directly,
405 * which is why we call pull_potential after calc_virial.
407 set_pbc(&pbc, ir->ePBC, box);
409 enerd->term[F_COM_PULL] +=
410 pull_potential(ir->ePull, ir->pull, mdatoms, &pbc,
411 cr, t, lambda[efptRESTRAINT], x, f, vir_force, &dvdl);
414 gmx_print_sepdvdl(fplog, "Com pull", enerd->term[F_COM_PULL], dvdl);
416 enerd->dvdl_lin[efptRESTRAINT] += dvdl;
419 static void pme_receive_force_ener(FILE *fplog,
422 gmx_wallcycle_t wcycle,
423 gmx_enerdata_t *enerd,
426 real e_q, e_lj, v, dvdl_q, dvdl_lj;
427 float cycles_ppdpme, cycles_seppme;
429 cycles_ppdpme = wallcycle_stop(wcycle, ewcPPDURINGPME);
430 dd_cycles_add(cr->dd, cycles_ppdpme, ddCyclPPduringPME);
432 /* In case of node-splitting, the PP nodes receive the long-range
433 * forces, virial and energy from the PME nodes here.
435 wallcycle_start(wcycle, ewcPP_PMEWAITRECVF);
438 gmx_pme_receive_f(cr, fr->f_novirsum, fr->vir_el_recip, &e_q,
439 fr->vir_lj_recip, &e_lj, &dvdl_q, &dvdl_lj,
443 gmx_print_sepdvdl(fplog, "Electrostatic PME mesh", e_q, dvdl_q);
444 gmx_print_sepdvdl(fplog, "Lennard-Jones PME mesh", e_lj, dvdl_lj);
446 enerd->term[F_COUL_RECIP] += e_q;
447 enerd->term[F_LJ_RECIP] += e_lj;
448 enerd->dvdl_lin[efptCOUL] += dvdl_q;
449 enerd->dvdl_lin[efptVDW] += dvdl_lj;
453 dd_cycles_add(cr->dd, cycles_seppme, ddCyclPME);
455 wallcycle_stop(wcycle, ewcPP_PMEWAITRECVF);
458 static void print_large_forces(FILE *fp, t_mdatoms *md, t_commrec *cr,
459 gmx_int64_t step, real pforce, rvec *x, rvec *f)
463 char buf[STEPSTRSIZE];
466 for (i = 0; i < md->homenr; i++)
469 /* We also catch NAN, if the compiler does not optimize this away. */
470 if (fn2 >= pf2 || fn2 != fn2)
472 fprintf(fp, "step %s atom %6d x %8.3f %8.3f %8.3f force %12.5e\n",
473 gmx_step_str(step, buf),
474 ddglatnr(cr->dd, i), x[i][XX], x[i][YY], x[i][ZZ], sqrt(fn2));
479 static void post_process_forces(t_commrec *cr,
481 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
483 matrix box, rvec x[],
488 t_forcerec *fr, gmx_vsite_t *vsite,
495 /* Spread the mesh force on virtual sites to the other particles...
496 * This is parallellized. MPI communication is performed
497 * if the constructing atoms aren't local.
499 wallcycle_start(wcycle, ewcVSITESPREAD);
500 spread_vsite_f(vsite, x, fr->f_novirsum, NULL,
501 (flags & GMX_FORCE_VIRIAL), fr->vir_el_recip,
503 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
504 wallcycle_stop(wcycle, ewcVSITESPREAD);
506 if (flags & GMX_FORCE_VIRIAL)
508 /* Now add the forces, this is local */
511 sum_forces(0, fr->f_novirsum_n, f, fr->f_novirsum);
515 sum_forces(0, mdatoms->homenr,
518 if (EEL_FULL(fr->eeltype))
520 /* Add the mesh contribution to the virial */
521 m_add(vir_force, fr->vir_el_recip, vir_force);
523 if (EVDW_PME(fr->vdwtype))
525 /* Add the mesh contribution to the virial */
526 m_add(vir_force, fr->vir_lj_recip, vir_force);
530 pr_rvecs(debug, 0, "vir_force", vir_force, DIM);
535 if (fr->print_force >= 0)
537 print_large_forces(stderr, mdatoms, cr, step, fr->print_force, x, f);
541 static void do_nb_verlet(t_forcerec *fr,
542 interaction_const_t *ic,
543 gmx_enerdata_t *enerd,
544 int flags, int ilocality,
547 gmx_wallcycle_t wcycle)
549 int nnbl, kernel_type, enr_nbnxn_kernel_ljc, enr_nbnxn_kernel_lj;
551 nonbonded_verlet_group_t *nbvg;
554 if (!(flags & GMX_FORCE_NONBONDED))
556 /* skip non-bonded calculation */
560 nbvg = &fr->nbv->grp[ilocality];
562 /* CUDA kernel launch overhead is already timed separately */
563 if (fr->cutoff_scheme != ecutsVERLET)
565 gmx_incons("Invalid cut-off scheme passed!");
568 bCUDA = (nbvg->kernel_type == nbnxnk8x8x8_CUDA);
572 wallcycle_sub_start(wcycle, ewcsNONBONDED);
574 switch (nbvg->kernel_type)
576 case nbnxnk4x4_PlainC:
577 nbnxn_kernel_ref(&nbvg->nbl_lists,
583 enerd->grpp.ener[egCOULSR],
585 enerd->grpp.ener[egBHAMSR] :
586 enerd->grpp.ener[egLJSR]);
589 case nbnxnk4xN_SIMD_4xN:
590 nbnxn_kernel_simd_4xn(&nbvg->nbl_lists,
597 enerd->grpp.ener[egCOULSR],
599 enerd->grpp.ener[egBHAMSR] :
600 enerd->grpp.ener[egLJSR]);
602 case nbnxnk4xN_SIMD_2xNN:
603 nbnxn_kernel_simd_2xnn(&nbvg->nbl_lists,
610 enerd->grpp.ener[egCOULSR],
612 enerd->grpp.ener[egBHAMSR] :
613 enerd->grpp.ener[egLJSR]);
616 case nbnxnk8x8x8_CUDA:
617 nbnxn_cuda_launch_kernel(fr->nbv->cu_nbv, nbvg->nbat, flags, ilocality);
620 case nbnxnk8x8x8_PlainC:
621 nbnxn_kernel_gpu_ref(nbvg->nbl_lists.nbl[0],
626 nbvg->nbat->out[0].f,
628 enerd->grpp.ener[egCOULSR],
630 enerd->grpp.ener[egBHAMSR] :
631 enerd->grpp.ener[egLJSR]);
635 gmx_incons("Invalid nonbonded kernel type passed!");
640 wallcycle_sub_stop(wcycle, ewcsNONBONDED);
643 if (EEL_RF(ic->eeltype) || ic->eeltype == eelCUT)
645 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_RF;
647 else if ((!bCUDA && nbvg->ewald_excl == ewaldexclAnalytical) ||
648 (bCUDA && nbnxn_cuda_is_kernel_ewald_analytical(fr->nbv->cu_nbv)))
650 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_EWALD;
654 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_TAB;
656 enr_nbnxn_kernel_lj = eNR_NBNXN_LJ;
657 if (flags & GMX_FORCE_ENERGY)
659 /* In eNR_??? the nbnxn F+E kernels are always the F kernel + 1 */
660 enr_nbnxn_kernel_ljc += 1;
661 enr_nbnxn_kernel_lj += 1;
664 inc_nrnb(nrnb, enr_nbnxn_kernel_ljc,
665 nbvg->nbl_lists.natpair_ljq);
666 inc_nrnb(nrnb, enr_nbnxn_kernel_lj,
667 nbvg->nbl_lists.natpair_lj);
668 /* The Coulomb-only kernels are offset -eNR_NBNXN_LJ_RF+eNR_NBNXN_RF */
669 inc_nrnb(nrnb, enr_nbnxn_kernel_ljc-eNR_NBNXN_LJ_RF+eNR_NBNXN_RF,
670 nbvg->nbl_lists.natpair_q);
672 if (ic->vdw_modifier == eintmodFORCESWITCH)
674 /* We add up the switch cost separately */
675 inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_FSW+((flags & GMX_FORCE_ENERGY) ? 1 : 0),
676 nbvg->nbl_lists.natpair_ljq + nbvg->nbl_lists.natpair_lj);
678 if (ic->vdw_modifier == eintmodPOTSWITCH)
680 /* We add up the switch cost separately */
681 inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_PSW+((flags & GMX_FORCE_ENERGY) ? 1 : 0),
682 nbvg->nbl_lists.natpair_ljq + nbvg->nbl_lists.natpair_lj);
684 if (ic->vdwtype == evdwPME)
686 /* We add up the LJ Ewald cost separately */
687 inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_EWALD+((flags & GMX_FORCE_ENERGY) ? 1 : 0),
688 nbvg->nbl_lists.natpair_ljq + nbvg->nbl_lists.natpair_lj);
692 static void do_nb_verlet_fep(nbnxn_pairlist_set_t *nbl_lists,
699 gmx_enerdata_t *enerd,
702 gmx_wallcycle_t wcycle)
705 nb_kernel_data_t kernel_data;
707 real dvdl_nb[efptNR];
712 /* Add short-range interactions */
713 donb_flags |= GMX_NONBONDED_DO_SR;
715 /* Currently all group scheme kernels always calculate (shift-)forces */
716 if (flags & GMX_FORCE_FORCES)
718 donb_flags |= GMX_NONBONDED_DO_FORCE;
720 if (flags & GMX_FORCE_VIRIAL)
722 donb_flags |= GMX_NONBONDED_DO_SHIFTFORCE;
724 if (flags & GMX_FORCE_ENERGY)
726 donb_flags |= GMX_NONBONDED_DO_POTENTIAL;
728 if (flags & GMX_FORCE_DO_LR)
730 donb_flags |= GMX_NONBONDED_DO_LR;
733 kernel_data.flags = donb_flags;
734 kernel_data.lambda = lambda;
735 kernel_data.dvdl = dvdl_nb;
737 kernel_data.energygrp_elec = enerd->grpp.ener[egCOULSR];
738 kernel_data.energygrp_vdw = enerd->grpp.ener[egLJSR];
740 /* reset free energy components */
741 for (i = 0; i < efptNR; i++)
746 assert(gmx_omp_nthreads_get(emntNonbonded) == nbl_lists->nnbl);
748 wallcycle_sub_start(wcycle, ewcsNONBONDED);
749 #pragma omp parallel for schedule(static) num_threads(nbl_lists->nnbl)
750 for (th = 0; th < nbl_lists->nnbl; th++)
752 gmx_nb_free_energy_kernel(nbl_lists->nbl_fep[th],
753 x, f, fr, mdatoms, &kernel_data, nrnb);
756 if (fepvals->sc_alpha != 0)
758 enerd->dvdl_nonlin[efptVDW] += dvdl_nb[efptVDW];
759 enerd->dvdl_nonlin[efptCOUL] += dvdl_nb[efptCOUL];
763 enerd->dvdl_lin[efptVDW] += dvdl_nb[efptVDW];
764 enerd->dvdl_lin[efptCOUL] += dvdl_nb[efptCOUL];
767 /* If we do foreign lambda and we have soft-core interactions
768 * we have to recalculate the (non-linear) energies contributions.
770 if (fepvals->n_lambda > 0 && (flags & GMX_FORCE_DHDL) && fepvals->sc_alpha != 0)
772 kernel_data.flags = (donb_flags & ~(GMX_NONBONDED_DO_FORCE | GMX_NONBONDED_DO_SHIFTFORCE)) | GMX_NONBONDED_DO_FOREIGNLAMBDA;
773 kernel_data.lambda = lam_i;
774 kernel_data.energygrp_elec = enerd->foreign_grpp.ener[egCOULSR];
775 kernel_data.energygrp_vdw = enerd->foreign_grpp.ener[egLJSR];
776 /* Note that we add to kernel_data.dvdl, but ignore the result */
778 for (i = 0; i < enerd->n_lambda; i++)
780 for (j = 0; j < efptNR; j++)
782 lam_i[j] = (i == 0 ? lambda[j] : fepvals->all_lambda[j][i-1]);
784 reset_foreign_enerdata(enerd);
785 #pragma omp parallel for schedule(static) num_threads(nbl_lists->nnbl)
786 for (th = 0; th < nbl_lists->nnbl; th++)
788 gmx_nb_free_energy_kernel(nbl_lists->nbl_fep[th],
789 x, f, fr, mdatoms, &kernel_data, nrnb);
792 sum_epot(&(enerd->foreign_grpp), enerd->foreign_term);
793 enerd->enerpart_lambda[i] += enerd->foreign_term[F_EPOT];
797 wallcycle_sub_stop(wcycle, ewcsNONBONDED);
800 void do_force_cutsVERLET(FILE *fplog, t_commrec *cr,
801 t_inputrec *inputrec,
802 gmx_int64_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
804 gmx_groups_t gmx_unused *groups,
805 matrix box, rvec x[], history_t *hist,
809 gmx_enerdata_t *enerd, t_fcdata *fcd,
810 real *lambda, t_graph *graph,
811 t_forcerec *fr, interaction_const_t *ic,
812 gmx_vsite_t *vsite, rvec mu_tot,
813 double t, FILE *field, gmx_edsam_t ed,
821 gmx_bool bSepDVDL, bStateChanged, bNS, bFillGrid, bCalcCGCM, bBS;
822 gmx_bool bDoLongRange, bDoForces, bSepLRF, bUseGPU, bUseOrEmulGPU;
823 gmx_bool bDiffKernels = FALSE;
825 rvec vzero, box_diag;
827 float cycles_pme, cycles_force, cycles_wait_gpu;
828 nonbonded_verlet_t *nbv;
833 nb_kernel_type = fr->nbv->grp[0].kernel_type;
836 homenr = mdatoms->homenr;
838 bSepDVDL = (fr->bSepDVDL && do_per_step(step, inputrec->nstlog));
840 clear_mat(vir_force);
843 if (DOMAINDECOMP(cr))
845 cg1 = cr->dd->ncg_tot;
856 bStateChanged = (flags & GMX_FORCE_STATECHANGED);
857 bNS = (flags & GMX_FORCE_NS) && (fr->bAllvsAll == FALSE);
858 bFillGrid = (bNS && bStateChanged);
859 bCalcCGCM = (bFillGrid && !DOMAINDECOMP(cr));
860 bDoLongRange = (fr->bTwinRange && bNS && (flags & GMX_FORCE_DO_LR));
861 bDoForces = (flags & GMX_FORCE_FORCES);
862 bSepLRF = (bDoLongRange && bDoForces && (flags & GMX_FORCE_SEPLRF));
863 bUseGPU = fr->nbv->bUseGPU;
864 bUseOrEmulGPU = bUseGPU || (nbv->grp[0].kernel_type == nbnxnk8x8x8_PlainC);
868 update_forcerec(fr, box);
870 if (NEED_MUTOT(*inputrec))
872 /* Calculate total (local) dipole moment in a temporary common array.
873 * This makes it possible to sum them over nodes faster.
875 calc_mu(start, homenr,
876 x, mdatoms->chargeA, mdatoms->chargeB, mdatoms->nChargePerturbed,
881 if (fr->ePBC != epbcNONE)
883 /* Compute shift vectors every step,
884 * because of pressure coupling or box deformation!
886 if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
888 calc_shifts(box, fr->shift_vec);
893 put_atoms_in_box_omp(fr->ePBC, box, homenr, x);
894 inc_nrnb(nrnb, eNR_SHIFTX, homenr);
896 else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph)
898 unshift_self(graph, box, x);
902 nbnxn_atomdata_copy_shiftvec(flags & GMX_FORCE_DYNAMICBOX,
903 fr->shift_vec, nbv->grp[0].nbat);
906 if (!(cr->duty & DUTY_PME))
908 /* Send particle coordinates to the pme nodes.
909 * Since this is only implemented for domain decomposition
910 * and domain decomposition does not use the graph,
911 * we do not need to worry about shifting.
916 wallcycle_start(wcycle, ewcPP_PMESENDX);
918 bBS = (inputrec->nwall == 2);
922 svmul(inputrec->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
925 if (EEL_PME(fr->eeltype))
927 pme_flags |= GMX_PME_DO_COULOMB;
930 if (EVDW_PME(fr->vdwtype))
932 pme_flags |= GMX_PME_DO_LJ;
935 gmx_pme_send_coordinates(cr, bBS ? boxs : box, x,
936 mdatoms->nChargePerturbed, mdatoms->nTypePerturbed, lambda[efptCOUL], lambda[efptVDW],
937 (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)),
940 wallcycle_stop(wcycle, ewcPP_PMESENDX);
944 /* do gridding for pair search */
947 if (graph && bStateChanged)
949 /* Calculate intramolecular shift vectors to make molecules whole */
950 mk_mshift(fplog, graph, fr->ePBC, box, x);
954 box_diag[XX] = box[XX][XX];
955 box_diag[YY] = box[YY][YY];
956 box_diag[ZZ] = box[ZZ][ZZ];
958 wallcycle_start(wcycle, ewcNS);
961 wallcycle_sub_start(wcycle, ewcsNBS_GRID_LOCAL);
962 nbnxn_put_on_grid(nbv->nbs, fr->ePBC, box,
964 0, mdatoms->homenr, -1, fr->cginfo, x,
966 nbv->grp[eintLocal].kernel_type,
967 nbv->grp[eintLocal].nbat);
968 wallcycle_sub_stop(wcycle, ewcsNBS_GRID_LOCAL);
972 wallcycle_sub_start(wcycle, ewcsNBS_GRID_NONLOCAL);
973 nbnxn_put_on_grid_nonlocal(nbv->nbs, domdec_zones(cr->dd),
975 nbv->grp[eintNonlocal].kernel_type,
976 nbv->grp[eintNonlocal].nbat);
977 wallcycle_sub_stop(wcycle, ewcsNBS_GRID_NONLOCAL);
980 if (nbv->ngrp == 1 ||
981 nbv->grp[eintNonlocal].nbat == nbv->grp[eintLocal].nbat)
983 nbnxn_atomdata_set(nbv->grp[eintLocal].nbat, eatAll,
984 nbv->nbs, mdatoms, fr->cginfo);
988 nbnxn_atomdata_set(nbv->grp[eintLocal].nbat, eatLocal,
989 nbv->nbs, mdatoms, fr->cginfo);
990 nbnxn_atomdata_set(nbv->grp[eintNonlocal].nbat, eatAll,
991 nbv->nbs, mdatoms, fr->cginfo);
993 wallcycle_stop(wcycle, ewcNS);
996 /* initialize the GPU atom data and copy shift vector */
1001 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1002 nbnxn_cuda_init_atomdata(nbv->cu_nbv, nbv->grp[eintLocal].nbat);
1003 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1006 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1007 nbnxn_cuda_upload_shiftvec(nbv->cu_nbv, nbv->grp[eintLocal].nbat);
1008 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1011 /* do local pair search */
1014 wallcycle_start_nocount(wcycle, ewcNS);
1015 wallcycle_sub_start(wcycle, ewcsNBS_SEARCH_LOCAL);
1016 nbnxn_make_pairlist(nbv->nbs, nbv->grp[eintLocal].nbat,
1019 nbv->min_ci_balanced,
1020 &nbv->grp[eintLocal].nbl_lists,
1022 nbv->grp[eintLocal].kernel_type,
1024 wallcycle_sub_stop(wcycle, ewcsNBS_SEARCH_LOCAL);
1028 /* initialize local pair-list on the GPU */
1029 nbnxn_cuda_init_pairlist(nbv->cu_nbv,
1030 nbv->grp[eintLocal].nbl_lists.nbl[0],
1033 wallcycle_stop(wcycle, ewcNS);
1037 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1038 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
1039 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatLocal, FALSE, x,
1040 nbv->grp[eintLocal].nbat);
1041 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
1042 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1047 wallcycle_start(wcycle, ewcLAUNCH_GPU_NB);
1048 /* launch local nonbonded F on GPU */
1049 do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFNo,
1051 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1054 /* Communicate coordinates and sum dipole if necessary +
1055 do non-local pair search */
1056 if (DOMAINDECOMP(cr))
1058 bDiffKernels = (nbv->grp[eintNonlocal].kernel_type !=
1059 nbv->grp[eintLocal].kernel_type);
1063 /* With GPU+CPU non-bonded calculations we need to copy
1064 * the local coordinates to the non-local nbat struct
1065 * (in CPU format) as the non-local kernel call also
1066 * calculates the local - non-local interactions.
1068 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1069 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
1070 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatLocal, TRUE, x,
1071 nbv->grp[eintNonlocal].nbat);
1072 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
1073 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1078 wallcycle_start_nocount(wcycle, ewcNS);
1079 wallcycle_sub_start(wcycle, ewcsNBS_SEARCH_NONLOCAL);
1083 nbnxn_grid_add_simple(nbv->nbs, nbv->grp[eintNonlocal].nbat);
1086 nbnxn_make_pairlist(nbv->nbs, nbv->grp[eintNonlocal].nbat,
1089 nbv->min_ci_balanced,
1090 &nbv->grp[eintNonlocal].nbl_lists,
1092 nbv->grp[eintNonlocal].kernel_type,
1095 wallcycle_sub_stop(wcycle, ewcsNBS_SEARCH_NONLOCAL);
1097 if (nbv->grp[eintNonlocal].kernel_type == nbnxnk8x8x8_CUDA)
1099 /* initialize non-local pair-list on the GPU */
1100 nbnxn_cuda_init_pairlist(nbv->cu_nbv,
1101 nbv->grp[eintNonlocal].nbl_lists.nbl[0],
1104 wallcycle_stop(wcycle, ewcNS);
1108 wallcycle_start(wcycle, ewcMOVEX);
1109 dd_move_x(cr->dd, box, x);
1111 /* When we don't need the total dipole we sum it in global_stat */
1112 if (bStateChanged && NEED_MUTOT(*inputrec))
1114 gmx_sumd(2*DIM, mu, cr);
1116 wallcycle_stop(wcycle, ewcMOVEX);
1118 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1119 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
1120 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatNonlocal, FALSE, x,
1121 nbv->grp[eintNonlocal].nbat);
1122 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
1123 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1126 if (bUseGPU && !bDiffKernels)
1128 wallcycle_start(wcycle, ewcLAUNCH_GPU_NB);
1129 /* launch non-local nonbonded F on GPU */
1130 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFNo,
1132 cycles_force += wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1138 /* launch D2H copy-back F */
1139 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1140 if (DOMAINDECOMP(cr) && !bDiffKernels)
1142 nbnxn_cuda_launch_cpyback(nbv->cu_nbv, nbv->grp[eintNonlocal].nbat,
1143 flags, eatNonlocal);
1145 nbnxn_cuda_launch_cpyback(nbv->cu_nbv, nbv->grp[eintLocal].nbat,
1147 cycles_force += wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1150 if (bStateChanged && NEED_MUTOT(*inputrec))
1154 gmx_sumd(2*DIM, mu, cr);
1157 for (i = 0; i < 2; i++)
1159 for (j = 0; j < DIM; j++)
1161 fr->mu_tot[i][j] = mu[i*DIM + j];
1165 if (fr->efep == efepNO)
1167 copy_rvec(fr->mu_tot[0], mu_tot);
1171 for (j = 0; j < DIM; j++)
1174 (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] +
1175 lambda[efptCOUL]*fr->mu_tot[1][j];
1179 /* Reset energies */
1180 reset_enerdata(fr, bNS, enerd, MASTER(cr));
1181 clear_rvecs(SHIFTS, fr->fshift);
1183 if (DOMAINDECOMP(cr) && !(cr->duty & DUTY_PME))
1185 wallcycle_start(wcycle, ewcPPDURINGPME);
1186 dd_force_flop_start(cr->dd, nrnb);
1191 /* Enforced rotation has its own cycle counter that starts after the collective
1192 * coordinates have been communicated. It is added to ddCyclF to allow
1193 * for proper load-balancing */
1194 wallcycle_start(wcycle, ewcROT);
1195 do_rotation(cr, inputrec, box, x, t, step, wcycle, bNS);
1196 wallcycle_stop(wcycle, ewcROT);
1199 /* Start the force cycle counter.
1200 * This counter is stopped in do_forcelow_level.
1201 * No parallel communication should occur while this counter is running,
1202 * since that will interfere with the dynamic load balancing.
1204 wallcycle_start(wcycle, ewcFORCE);
1207 /* Reset forces for which the virial is calculated separately:
1208 * PME/Ewald forces if necessary */
1209 if (fr->bF_NoVirSum)
1211 if (flags & GMX_FORCE_VIRIAL)
1213 fr->f_novirsum = fr->f_novirsum_alloc;
1216 clear_rvecs(fr->f_novirsum_n, fr->f_novirsum);
1220 clear_rvecs(homenr, fr->f_novirsum+start);
1225 /* We are not calculating the pressure so we do not need
1226 * a separate array for forces that do not contribute
1233 /* Clear the short- and long-range forces */
1234 clear_rvecs(fr->natoms_force_constr, f);
1235 if (bSepLRF && do_per_step(step, inputrec->nstcalclr))
1237 clear_rvecs(fr->natoms_force_constr, fr->f_twin);
1240 clear_rvec(fr->vir_diag_posres);
1243 if (inputrec->ePull == epullCONSTRAINT)
1245 clear_pull_forces(inputrec->pull);
1248 /* We calculate the non-bonded forces, when done on the CPU, here.
1249 * We do this before calling do_force_lowlevel, as in there bondeds
1250 * forces are calculated before PME, which does communication.
1251 * With this order, non-bonded and bonded force calculation imbalance
1252 * can be balanced out by the domain decomposition load balancing.
1257 /* Maybe we should move this into do_force_lowlevel */
1258 do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFYes,
1262 if (fr->efep != efepNO)
1264 /* Calculate the local and non-local free energy interactions here.
1265 * Happens here on the CPU both with and without GPU.
1267 if (fr->nbv->grp[eintLocal].nbl_lists.nbl_fep[0]->nrj > 0)
1269 do_nb_verlet_fep(&fr->nbv->grp[eintLocal].nbl_lists,
1271 inputrec->fepvals, lambda,
1272 enerd, flags, nrnb, wcycle);
1275 if (DOMAINDECOMP(cr) &&
1276 fr->nbv->grp[eintNonlocal].nbl_lists.nbl_fep[0]->nrj > 0)
1278 do_nb_verlet_fep(&fr->nbv->grp[eintNonlocal].nbl_lists,
1280 inputrec->fepvals, lambda,
1281 enerd, flags, nrnb, wcycle);
1285 if (!bUseOrEmulGPU || bDiffKernels)
1289 if (DOMAINDECOMP(cr))
1291 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal,
1292 bDiffKernels ? enbvClearFYes : enbvClearFNo,
1302 aloc = eintNonlocal;
1305 /* Add all the non-bonded force to the normal force array.
1306 * This can be split into a local a non-local part when overlapping
1307 * communication with calculation with domain decomposition.
1309 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1310 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1311 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1312 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatAll, nbv->grp[aloc].nbat, f);
1313 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1314 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1315 wallcycle_start_nocount(wcycle, ewcFORCE);
1317 /* if there are multiple fshift output buffers reduce them */
1318 if ((flags & GMX_FORCE_VIRIAL) &&
1319 nbv->grp[aloc].nbl_lists.nnbl > 1)
1321 nbnxn_atomdata_add_nbat_fshift_to_fshift(nbv->grp[aloc].nbat,
1326 /* update QMMMrec, if necessary */
1329 update_QMMMrec(cr, fr, x, mdatoms, box, top);
1332 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_POSRES].nr > 0)
1334 posres_wrapper(fplog, flags, bSepDVDL, inputrec, nrnb, top, box, x,
1338 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_FBPOSRES].nr > 0)
1340 fbposres_wrapper(inputrec, nrnb, top, box, x, enerd, fr);
1343 /* Compute the bonded and non-bonded energies and optionally forces */
1344 do_force_lowlevel(fplog, step, fr, inputrec, &(top->idef),
1345 cr, nrnb, wcycle, mdatoms,
1346 x, hist, f, bSepLRF ? fr->f_twin : f, enerd, fcd, top, fr->born,
1347 &(top->atomtypes), bBornRadii, box,
1348 inputrec->fepvals, lambda, graph, &(top->excls), fr->mu_tot,
1349 flags, &cycles_pme);
1353 if (do_per_step(step, inputrec->nstcalclr))
1355 /* Add the long range forces to the short range forces */
1356 for (i = 0; i < fr->natoms_force_constr; i++)
1358 rvec_add(fr->f_twin[i], f[i], f[i]);
1363 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1367 do_flood(cr, inputrec, x, f, ed, box, step, bNS);
1370 if (bUseOrEmulGPU && !bDiffKernels)
1372 /* wait for non-local forces (or calculate in emulation mode) */
1373 if (DOMAINDECOMP(cr))
1379 wallcycle_start(wcycle, ewcWAIT_GPU_NB_NL);
1380 nbnxn_cuda_wait_gpu(nbv->cu_nbv,
1381 nbv->grp[eintNonlocal].nbat,
1383 enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1385 cycles_tmp = wallcycle_stop(wcycle, ewcWAIT_GPU_NB_NL);
1386 cycles_wait_gpu += cycles_tmp;
1387 cycles_force += cycles_tmp;
1391 wallcycle_start_nocount(wcycle, ewcFORCE);
1392 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFYes,
1394 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1396 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1397 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1398 /* skip the reduction if there was no non-local work to do */
1399 if (nbv->grp[eintLocal].nbl_lists.nbl[0]->nsci > 0)
1401 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatNonlocal,
1402 nbv->grp[eintNonlocal].nbat, f);
1404 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1405 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1409 if (bDoForces && DOMAINDECOMP(cr))
1411 /* Communicate the forces */
1412 wallcycle_start(wcycle, ewcMOVEF);
1413 dd_move_f(cr->dd, f, fr->fshift);
1414 /* Do we need to communicate the separate force array
1415 * for terms that do not contribute to the single sum virial?
1416 * Position restraints and electric fields do not introduce
1417 * inter-cg forces, only full electrostatics methods do.
1418 * When we do not calculate the virial, fr->f_novirsum = f,
1419 * so we have already communicated these forces.
1421 if (EEL_FULL(fr->eeltype) && cr->dd->n_intercg_excl &&
1422 (flags & GMX_FORCE_VIRIAL))
1424 dd_move_f(cr->dd, fr->f_novirsum, NULL);
1428 /* We should not update the shift forces here,
1429 * since f_twin is already included in f.
1431 dd_move_f(cr->dd, fr->f_twin, NULL);
1433 wallcycle_stop(wcycle, ewcMOVEF);
1438 /* wait for local forces (or calculate in emulation mode) */
1441 wallcycle_start(wcycle, ewcWAIT_GPU_NB_L);
1442 nbnxn_cuda_wait_gpu(nbv->cu_nbv,
1443 nbv->grp[eintLocal].nbat,
1445 enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1447 cycles_wait_gpu += wallcycle_stop(wcycle, ewcWAIT_GPU_NB_L);
1449 /* now clear the GPU outputs while we finish the step on the CPU */
1451 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1452 nbnxn_cuda_clear_outputs(nbv->cu_nbv, flags);
1453 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1457 wallcycle_start_nocount(wcycle, ewcFORCE);
1458 do_nb_verlet(fr, ic, enerd, flags, eintLocal,
1459 DOMAINDECOMP(cr) ? enbvClearFNo : enbvClearFYes,
1461 wallcycle_stop(wcycle, ewcFORCE);
1463 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1464 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1465 if (nbv->grp[eintLocal].nbl_lists.nbl[0]->nsci > 0)
1467 /* skip the reduction if there was no non-local work to do */
1468 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatLocal,
1469 nbv->grp[eintLocal].nbat, f);
1471 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1472 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1475 if (DOMAINDECOMP(cr))
1477 dd_force_flop_stop(cr->dd, nrnb);
1480 dd_cycles_add(cr->dd, cycles_force-cycles_pme, ddCyclF);
1483 dd_cycles_add(cr->dd, cycles_wait_gpu, ddCyclWaitGPU);
1490 if (IR_ELEC_FIELD(*inputrec))
1492 /* Compute forces due to electric field */
1493 calc_f_el(MASTER(cr) ? field : NULL,
1494 start, homenr, mdatoms->chargeA, fr->f_novirsum,
1495 inputrec->ex, inputrec->et, t);
1498 /* If we have NoVirSum forces, but we do not calculate the virial,
1499 * we sum fr->f_novirum=f later.
1501 if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1503 wallcycle_start(wcycle, ewcVSITESPREAD);
1504 spread_vsite_f(vsite, x, f, fr->fshift, FALSE, NULL, nrnb,
1505 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1506 wallcycle_stop(wcycle, ewcVSITESPREAD);
1510 wallcycle_start(wcycle, ewcVSITESPREAD);
1511 spread_vsite_f(vsite, x, fr->f_twin, NULL, FALSE, NULL,
1513 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1514 wallcycle_stop(wcycle, ewcVSITESPREAD);
1518 if (flags & GMX_FORCE_VIRIAL)
1520 /* Calculation of the virial must be done after vsites! */
1521 calc_virial(0, mdatoms->homenr, x, f,
1522 vir_force, graph, box, nrnb, fr, inputrec->ePBC);
1526 if (inputrec->ePull == epullUMBRELLA || inputrec->ePull == epullCONST_F)
1528 pull_potential_wrapper(fplog, bSepDVDL, cr, inputrec, box, x,
1529 f, vir_force, mdatoms, enerd, lambda, t);
1532 /* Add the forces from enforced rotation potentials (if any) */
1535 wallcycle_start(wcycle, ewcROTadd);
1536 enerd->term[F_COM_PULL] += add_rot_forces(inputrec->rot, f, cr, step, t);
1537 wallcycle_stop(wcycle, ewcROTadd);
1540 /* Add forces from interactive molecular dynamics (IMD), if bIMD == TRUE. */
1541 IMD_apply_forces(inputrec->bIMD, inputrec->imd, cr, f, wcycle);
1543 if (PAR(cr) && !(cr->duty & DUTY_PME))
1545 /* In case of node-splitting, the PP nodes receive the long-range
1546 * forces, virial and energy from the PME nodes here.
1548 pme_receive_force_ener(fplog, bSepDVDL, cr, wcycle, enerd, fr);
1553 post_process_forces(cr, step, nrnb, wcycle,
1554 top, box, x, f, vir_force, mdatoms, graph, fr, vsite,
1558 /* Sum the potential energy terms from group contributions */
1559 sum_epot(&(enerd->grpp), enerd->term);
1562 void do_force_cutsGROUP(FILE *fplog, t_commrec *cr,
1563 t_inputrec *inputrec,
1564 gmx_int64_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
1565 gmx_localtop_t *top,
1566 gmx_groups_t *groups,
1567 matrix box, rvec x[], history_t *hist,
1571 gmx_enerdata_t *enerd, t_fcdata *fcd,
1572 real *lambda, t_graph *graph,
1573 t_forcerec *fr, gmx_vsite_t *vsite, rvec mu_tot,
1574 double t, FILE *field, gmx_edsam_t ed,
1575 gmx_bool bBornRadii,
1581 gmx_bool bSepDVDL, bStateChanged, bNS, bFillGrid, bCalcCGCM, bBS;
1582 gmx_bool bDoLongRangeNS, bDoForces, bDoPotential, bSepLRF;
1583 gmx_bool bDoAdressWF;
1585 rvec vzero, box_diag;
1586 real e, v, dvdlambda[efptNR];
1588 float cycles_pme, cycles_force;
1591 homenr = mdatoms->homenr;
1593 bSepDVDL = (fr->bSepDVDL && do_per_step(step, inputrec->nstlog));
1595 clear_mat(vir_force);
1598 if (DOMAINDECOMP(cr))
1600 cg1 = cr->dd->ncg_tot;
1611 bStateChanged = (flags & GMX_FORCE_STATECHANGED);
1612 bNS = (flags & GMX_FORCE_NS) && (fr->bAllvsAll == FALSE);
1613 /* Should we update the long-range neighborlists at this step? */
1614 bDoLongRangeNS = fr->bTwinRange && bNS;
1615 /* Should we perform the long-range nonbonded evaluation inside the neighborsearching? */
1616 bFillGrid = (bNS && bStateChanged);
1617 bCalcCGCM = (bFillGrid && !DOMAINDECOMP(cr));
1618 bDoForces = (flags & GMX_FORCE_FORCES);
1619 bDoPotential = (flags & GMX_FORCE_ENERGY);
1620 bSepLRF = ((inputrec->nstcalclr > 1) && bDoForces &&
1621 (flags & GMX_FORCE_SEPLRF) && (flags & GMX_FORCE_DO_LR));
1623 /* should probably move this to the forcerec since it doesn't change */
1624 bDoAdressWF = ((fr->adress_type != eAdressOff));
1628 update_forcerec(fr, box);
1630 if (NEED_MUTOT(*inputrec))
1632 /* Calculate total (local) dipole moment in a temporary common array.
1633 * This makes it possible to sum them over nodes faster.
1635 calc_mu(start, homenr,
1636 x, mdatoms->chargeA, mdatoms->chargeB, mdatoms->nChargePerturbed,
1641 if (fr->ePBC != epbcNONE)
1643 /* Compute shift vectors every step,
1644 * because of pressure coupling or box deformation!
1646 if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
1648 calc_shifts(box, fr->shift_vec);
1653 put_charge_groups_in_box(fplog, cg0, cg1, fr->ePBC, box,
1654 &(top->cgs), x, fr->cg_cm);
1655 inc_nrnb(nrnb, eNR_CGCM, homenr);
1656 inc_nrnb(nrnb, eNR_RESETX, cg1-cg0);
1658 else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph)
1660 unshift_self(graph, box, x);
1665 calc_cgcm(fplog, cg0, cg1, &(top->cgs), x, fr->cg_cm);
1666 inc_nrnb(nrnb, eNR_CGCM, homenr);
1669 if (bCalcCGCM && gmx_debug_at)
1671 pr_rvecs(debug, 0, "cgcm", fr->cg_cm, top->cgs.nr);
1675 if (!(cr->duty & DUTY_PME))
1677 /* Send particle coordinates to the pme nodes.
1678 * Since this is only implemented for domain decomposition
1679 * and domain decomposition does not use the graph,
1680 * we do not need to worry about shifting.
1685 wallcycle_start(wcycle, ewcPP_PMESENDX);
1687 bBS = (inputrec->nwall == 2);
1690 copy_mat(box, boxs);
1691 svmul(inputrec->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
1694 if (EEL_PME(fr->eeltype))
1696 pme_flags |= GMX_PME_DO_COULOMB;
1699 if (EVDW_PME(fr->vdwtype))
1701 pme_flags |= GMX_PME_DO_LJ;
1704 gmx_pme_send_coordinates(cr, bBS ? boxs : box, x,
1705 mdatoms->nChargePerturbed, mdatoms->nTypePerturbed, lambda[efptCOUL], lambda[efptVDW],
1706 (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)),
1709 wallcycle_stop(wcycle, ewcPP_PMESENDX);
1711 #endif /* GMX_MPI */
1713 /* Communicate coordinates and sum dipole if necessary */
1714 if (DOMAINDECOMP(cr))
1716 wallcycle_start(wcycle, ewcMOVEX);
1717 dd_move_x(cr->dd, box, x);
1718 wallcycle_stop(wcycle, ewcMOVEX);
1721 /* update adress weight beforehand */
1722 if (bStateChanged && bDoAdressWF)
1724 /* need pbc for adress weight calculation with pbc_dx */
1725 set_pbc(&pbc, inputrec->ePBC, box);
1726 if (fr->adress_site == eAdressSITEcog)
1728 update_adress_weights_cog(top->idef.iparams, top->idef.il, x, fr, mdatoms,
1729 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1731 else if (fr->adress_site == eAdressSITEcom)
1733 update_adress_weights_com(fplog, cg0, cg1, &(top->cgs), x, fr, mdatoms,
1734 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1736 else if (fr->adress_site == eAdressSITEatomatom)
1738 update_adress_weights_atom_per_atom(cg0, cg1, &(top->cgs), x, fr, mdatoms,
1739 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1743 update_adress_weights_atom(cg0, cg1, &(top->cgs), x, fr, mdatoms,
1744 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1748 if (NEED_MUTOT(*inputrec))
1755 gmx_sumd(2*DIM, mu, cr);
1757 for (i = 0; i < 2; i++)
1759 for (j = 0; j < DIM; j++)
1761 fr->mu_tot[i][j] = mu[i*DIM + j];
1765 if (fr->efep == efepNO)
1767 copy_rvec(fr->mu_tot[0], mu_tot);
1771 for (j = 0; j < DIM; j++)
1774 (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] + lambda[efptCOUL]*fr->mu_tot[1][j];
1779 /* Reset energies */
1780 reset_enerdata(fr, bNS, enerd, MASTER(cr));
1781 clear_rvecs(SHIFTS, fr->fshift);
1785 wallcycle_start(wcycle, ewcNS);
1787 if (graph && bStateChanged)
1789 /* Calculate intramolecular shift vectors to make molecules whole */
1790 mk_mshift(fplog, graph, fr->ePBC, box, x);
1793 /* Do the actual neighbour searching */
1795 groups, top, mdatoms,
1796 cr, nrnb, bFillGrid,
1799 wallcycle_stop(wcycle, ewcNS);
1802 if (inputrec->implicit_solvent && bNS)
1804 make_gb_nblist(cr, inputrec->gb_algorithm,
1805 x, box, fr, &top->idef, graph, fr->born);
1808 if (DOMAINDECOMP(cr) && !(cr->duty & DUTY_PME))
1810 wallcycle_start(wcycle, ewcPPDURINGPME);
1811 dd_force_flop_start(cr->dd, nrnb);
1816 /* Enforced rotation has its own cycle counter that starts after the collective
1817 * coordinates have been communicated. It is added to ddCyclF to allow
1818 * for proper load-balancing */
1819 wallcycle_start(wcycle, ewcROT);
1820 do_rotation(cr, inputrec, box, x, t, step, wcycle, bNS);
1821 wallcycle_stop(wcycle, ewcROT);
1824 /* Start the force cycle counter.
1825 * This counter is stopped in do_forcelow_level.
1826 * No parallel communication should occur while this counter is running,
1827 * since that will interfere with the dynamic load balancing.
1829 wallcycle_start(wcycle, ewcFORCE);
1833 /* Reset forces for which the virial is calculated separately:
1834 * PME/Ewald forces if necessary */
1835 if (fr->bF_NoVirSum)
1837 if (flags & GMX_FORCE_VIRIAL)
1839 fr->f_novirsum = fr->f_novirsum_alloc;
1842 clear_rvecs(fr->f_novirsum_n, fr->f_novirsum);
1846 clear_rvecs(homenr, fr->f_novirsum+start);
1851 /* We are not calculating the pressure so we do not need
1852 * a separate array for forces that do not contribute
1859 /* Clear the short- and long-range forces */
1860 clear_rvecs(fr->natoms_force_constr, f);
1861 if (bSepLRF && do_per_step(step, inputrec->nstcalclr))
1863 clear_rvecs(fr->natoms_force_constr, fr->f_twin);
1866 clear_rvec(fr->vir_diag_posres);
1868 if (inputrec->ePull == epullCONSTRAINT)
1870 clear_pull_forces(inputrec->pull);
1873 /* update QMMMrec, if necessary */
1876 update_QMMMrec(cr, fr, x, mdatoms, box, top);
1879 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_POSRES].nr > 0)
1881 posres_wrapper(fplog, flags, bSepDVDL, inputrec, nrnb, top, box, x,
1885 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_FBPOSRES].nr > 0)
1887 fbposres_wrapper(inputrec, nrnb, top, box, x, enerd, fr);
1890 /* Compute the bonded and non-bonded energies and optionally forces */
1891 do_force_lowlevel(fplog, step, fr, inputrec, &(top->idef),
1892 cr, nrnb, wcycle, mdatoms,
1893 x, hist, f, bSepLRF ? fr->f_twin : f, enerd, fcd, top, fr->born,
1894 &(top->atomtypes), bBornRadii, box,
1895 inputrec->fepvals, lambda,
1896 graph, &(top->excls), fr->mu_tot,
1902 if (do_per_step(step, inputrec->nstcalclr))
1904 /* Add the long range forces to the short range forces */
1905 for (i = 0; i < fr->natoms_force_constr; i++)
1907 rvec_add(fr->f_twin[i], f[i], f[i]);
1912 cycles_force = wallcycle_stop(wcycle, ewcFORCE);
1916 do_flood(cr, inputrec, x, f, ed, box, step, bNS);
1919 if (DOMAINDECOMP(cr))
1921 dd_force_flop_stop(cr->dd, nrnb);
1924 dd_cycles_add(cr->dd, cycles_force-cycles_pme, ddCyclF);
1930 if (IR_ELEC_FIELD(*inputrec))
1932 /* Compute forces due to electric field */
1933 calc_f_el(MASTER(cr) ? field : NULL,
1934 start, homenr, mdatoms->chargeA, fr->f_novirsum,
1935 inputrec->ex, inputrec->et, t);
1938 if (bDoAdressWF && fr->adress_icor == eAdressICThermoForce)
1940 /* Compute thermodynamic force in hybrid AdResS region */
1941 adress_thermo_force(start, homenr, &(top->cgs), x, fr->f_novirsum, fr, mdatoms,
1942 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1945 /* Communicate the forces */
1946 if (DOMAINDECOMP(cr))
1948 wallcycle_start(wcycle, ewcMOVEF);
1949 dd_move_f(cr->dd, f, fr->fshift);
1950 /* Do we need to communicate the separate force array
1951 * for terms that do not contribute to the single sum virial?
1952 * Position restraints and electric fields do not introduce
1953 * inter-cg forces, only full electrostatics methods do.
1954 * When we do not calculate the virial, fr->f_novirsum = f,
1955 * so we have already communicated these forces.
1957 if (EEL_FULL(fr->eeltype) && cr->dd->n_intercg_excl &&
1958 (flags & GMX_FORCE_VIRIAL))
1960 dd_move_f(cr->dd, fr->f_novirsum, NULL);
1964 /* We should not update the shift forces here,
1965 * since f_twin is already included in f.
1967 dd_move_f(cr->dd, fr->f_twin, NULL);
1969 wallcycle_stop(wcycle, ewcMOVEF);
1972 /* If we have NoVirSum forces, but we do not calculate the virial,
1973 * we sum fr->f_novirum=f later.
1975 if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1977 wallcycle_start(wcycle, ewcVSITESPREAD);
1978 spread_vsite_f(vsite, x, f, fr->fshift, FALSE, NULL, nrnb,
1979 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1980 wallcycle_stop(wcycle, ewcVSITESPREAD);
1984 wallcycle_start(wcycle, ewcVSITESPREAD);
1985 spread_vsite_f(vsite, x, fr->f_twin, NULL, FALSE, NULL,
1987 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1988 wallcycle_stop(wcycle, ewcVSITESPREAD);
1992 if (flags & GMX_FORCE_VIRIAL)
1994 /* Calculation of the virial must be done after vsites! */
1995 calc_virial(0, mdatoms->homenr, x, f,
1996 vir_force, graph, box, nrnb, fr, inputrec->ePBC);
2000 if (inputrec->ePull == epullUMBRELLA || inputrec->ePull == epullCONST_F)
2002 pull_potential_wrapper(fplog, bSepDVDL, cr, inputrec, box, x,
2003 f, vir_force, mdatoms, enerd, lambda, t);
2006 /* Add the forces from enforced rotation potentials (if any) */
2009 wallcycle_start(wcycle, ewcROTadd);
2010 enerd->term[F_COM_PULL] += add_rot_forces(inputrec->rot, f, cr, step, t);
2011 wallcycle_stop(wcycle, ewcROTadd);
2014 /* Add forces from interactive molecular dynamics (IMD), if bIMD == TRUE. */
2015 IMD_apply_forces(inputrec->bIMD, inputrec->imd, cr, f, wcycle);
2017 if (PAR(cr) && !(cr->duty & DUTY_PME))
2019 /* In case of node-splitting, the PP nodes receive the long-range
2020 * forces, virial and energy from the PME nodes here.
2022 pme_receive_force_ener(fplog, bSepDVDL, cr, wcycle, enerd, fr);
2027 post_process_forces(cr, step, nrnb, wcycle,
2028 top, box, x, f, vir_force, mdatoms, graph, fr, vsite,
2032 /* Sum the potential energy terms from group contributions */
2033 sum_epot(&(enerd->grpp), enerd->term);
2036 void do_force(FILE *fplog, t_commrec *cr,
2037 t_inputrec *inputrec,
2038 gmx_int64_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
2039 gmx_localtop_t *top,
2040 gmx_groups_t *groups,
2041 matrix box, rvec x[], history_t *hist,
2045 gmx_enerdata_t *enerd, t_fcdata *fcd,
2046 real *lambda, t_graph *graph,
2048 gmx_vsite_t *vsite, rvec mu_tot,
2049 double t, FILE *field, gmx_edsam_t ed,
2050 gmx_bool bBornRadii,
2053 /* modify force flag if not doing nonbonded */
2054 if (!fr->bNonbonded)
2056 flags &= ~GMX_FORCE_NONBONDED;
2059 switch (inputrec->cutoff_scheme)
2062 do_force_cutsVERLET(fplog, cr, inputrec,
2078 do_force_cutsGROUP(fplog, cr, inputrec,
2093 gmx_incons("Invalid cut-off scheme passed!");
2098 void do_constrain_first(FILE *fplog, gmx_constr_t constr,
2099 t_inputrec *ir, t_mdatoms *md,
2100 t_state *state, t_commrec *cr, t_nrnb *nrnb,
2101 t_forcerec *fr, gmx_localtop_t *top)
2103 int i, m, start, end;
2105 real dt = ir->delta_t;
2109 snew(savex, state->natoms);
2116 fprintf(debug, "vcm: start=%d, homenr=%d, end=%d\n",
2117 start, md->homenr, end);
2119 /* Do a first constrain to reset particles... */
2120 step = ir->init_step;
2123 char buf[STEPSTRSIZE];
2124 fprintf(fplog, "\nConstraining the starting coordinates (step %s)\n",
2125 gmx_step_str(step, buf));
2129 /* constrain the current position */
2130 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
2131 ir, NULL, cr, step, 0, 1.0, md,
2132 state->x, state->x, NULL,
2133 fr->bMolPBC, state->box,
2134 state->lambda[efptBONDED], &dvdl_dum,
2135 NULL, NULL, nrnb, econqCoord,
2136 ir->epc == epcMTTK, state->veta, state->veta);
2139 /* constrain the inital velocity, and save it */
2140 /* also may be useful if we need the ekin from the halfstep for velocity verlet */
2141 /* might not yet treat veta correctly */
2142 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
2143 ir, NULL, cr, step, 0, 1.0, md,
2144 state->x, state->v, state->v,
2145 fr->bMolPBC, state->box,
2146 state->lambda[efptBONDED], &dvdl_dum,
2147 NULL, NULL, nrnb, econqVeloc,
2148 ir->epc == epcMTTK, state->veta, state->veta);
2150 /* constrain the inital velocities at t-dt/2 */
2151 if (EI_STATE_VELOCITY(ir->eI) && ir->eI != eiVV)
2153 for (i = start; (i < end); i++)
2155 for (m = 0; (m < DIM); m++)
2157 /* Reverse the velocity */
2158 state->v[i][m] = -state->v[i][m];
2159 /* Store the position at t-dt in buf */
2160 savex[i][m] = state->x[i][m] + dt*state->v[i][m];
2163 /* Shake the positions at t=-dt with the positions at t=0
2164 * as reference coordinates.
2168 char buf[STEPSTRSIZE];
2169 fprintf(fplog, "\nConstraining the coordinates at t0-dt (step %s)\n",
2170 gmx_step_str(step, buf));
2173 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
2174 ir, NULL, cr, step, -1, 1.0, md,
2175 state->x, savex, NULL,
2176 fr->bMolPBC, state->box,
2177 state->lambda[efptBONDED], &dvdl_dum,
2178 state->v, NULL, nrnb, econqCoord,
2179 ir->epc == epcMTTK, state->veta, state->veta);
2181 for (i = start; i < end; i++)
2183 for (m = 0; m < DIM; m++)
2185 /* Re-reverse the velocities */
2186 state->v[i][m] = -state->v[i][m];
2195 integrate_table(real vdwtab[], real scale, int offstart, int rstart, int rend,
2196 double *enerout, double *virout)
2198 double enersum, virsum;
2199 double invscale, invscale2, invscale3;
2200 double r, ea, eb, ec, pa, pb, pc, pd;
2202 int ri, offset, tabfactor;
2204 invscale = 1.0/scale;
2205 invscale2 = invscale*invscale;
2206 invscale3 = invscale*invscale2;
2208 /* Following summation derived from cubic spline definition,
2209 * Numerical Recipies in C, second edition, p. 113-116. Exact for
2210 * the cubic spline. We first calculate the negative of the
2211 * energy from rvdw to rvdw_switch, assuming that g(r)=1, and then
2212 * add the more standard, abrupt cutoff correction to that result,
2213 * yielding the long-range correction for a switched function. We
2214 * perform both the pressure and energy loops at the same time for
2215 * simplicity, as the computational cost is low. */
2219 /* Since the dispersion table has been scaled down a factor
2220 * 6.0 and the repulsion a factor 12.0 to compensate for the
2221 * c6/c12 parameters inside nbfp[] being scaled up (to save
2222 * flops in kernels), we need to correct for this.
2233 for (ri = rstart; ri < rend; ++ri)
2237 eb = 2.0*invscale2*r;
2241 pb = 3.0*invscale2*r;
2242 pc = 3.0*invscale*r*r;
2245 /* this "8" is from the packing in the vdwtab array - perhaps
2246 should be defined? */
2248 offset = 8*ri + offstart;
2249 y0 = vdwtab[offset];
2250 f = vdwtab[offset+1];
2251 g = vdwtab[offset+2];
2252 h = vdwtab[offset+3];
2254 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);
2255 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);
2257 *enerout = 4.0*M_PI*enersum*tabfactor;
2258 *virout = 4.0*M_PI*virsum*tabfactor;
2261 void calc_enervirdiff(FILE *fplog, int eDispCorr, t_forcerec *fr)
2263 double eners[2], virs[2], enersum, virsum, y0, f, g, h;
2264 double r0, r1, r, rc3, rc9, ea, eb, ec, pa, pb, pc, pd;
2265 double invscale, invscale2, invscale3;
2266 int ri0, ri1, ri, i, offstart, offset;
2267 real scale, *vdwtab, tabfactor, tmp;
2269 fr->enershiftsix = 0;
2270 fr->enershifttwelve = 0;
2271 fr->enerdiffsix = 0;
2272 fr->enerdifftwelve = 0;
2274 fr->virdifftwelve = 0;
2276 if (eDispCorr != edispcNO)
2278 for (i = 0; i < 2; i++)
2283 if ((fr->vdw_modifier == eintmodPOTSHIFT) ||
2284 (fr->vdw_modifier == eintmodPOTSWITCH) ||
2285 (fr->vdw_modifier == eintmodFORCESWITCH) ||
2286 (fr->vdwtype == evdwSHIFT) ||
2287 (fr->vdwtype == evdwSWITCH))
2289 if (((fr->vdw_modifier == eintmodPOTSWITCH) ||
2290 (fr->vdw_modifier == eintmodFORCESWITCH) ||
2291 (fr->vdwtype == evdwSWITCH)) && fr->rvdw_switch == 0)
2294 "With dispersion correction rvdw-switch can not be zero "
2295 "for vdw-type = %s", evdw_names[fr->vdwtype]);
2298 scale = fr->nblists[0].table_vdw.scale;
2299 vdwtab = fr->nblists[0].table_vdw.data;
2301 /* Round the cut-offs to exact table values for precision */
2302 ri0 = floor(fr->rvdw_switch*scale);
2303 ri1 = ceil(fr->rvdw*scale);
2305 /* The code below has some support for handling force-switching, i.e.
2306 * when the force (instead of potential) is switched over a limited
2307 * region. This leads to a constant shift in the potential inside the
2308 * switching region, which we can handle by adding a constant energy
2309 * term in the force-switch case just like when we do potential-shift.
2311 * For now this is not enabled, but to keep the functionality in the
2312 * code we check separately for switch and shift. When we do force-switch
2313 * the shifting point is rvdw_switch, while it is the cutoff when we
2314 * have a classical potential-shift.
2316 * For a pure potential-shift the potential has a constant shift
2317 * all the way out to the cutoff, and that is it. For other forms
2318 * we need to calculate the constant shift up to the point where we
2319 * start modifying the potential.
2321 ri0 = (fr->vdw_modifier == eintmodPOTSHIFT) ? ri1 : ri0;
2328 if ((fr->vdw_modifier == eintmodFORCESWITCH) ||
2329 (fr->vdwtype == evdwSHIFT))
2331 /* Determine the constant energy shift below rvdw_switch.
2332 * Table has a scale factor since we have scaled it down to compensate
2333 * for scaling-up c6/c12 with the derivative factors to save flops in analytical kernels.
2335 fr->enershiftsix = (real)(-1.0/(rc3*rc3)) - 6.0*vdwtab[8*ri0];
2336 fr->enershifttwelve = (real)( 1.0/(rc9*rc3)) - 12.0*vdwtab[8*ri0 + 4];
2338 else if (fr->vdw_modifier == eintmodPOTSHIFT)
2340 fr->enershiftsix = (real)(-1.0/(rc3*rc3));
2341 fr->enershifttwelve = (real)( 1.0/(rc9*rc3));
2344 /* Add the constant part from 0 to rvdw_switch.
2345 * This integration from 0 to rvdw_switch overcounts the number
2346 * of interactions by 1, as it also counts the self interaction.
2347 * We will correct for this later.
2349 eners[0] += 4.0*M_PI*fr->enershiftsix*rc3/3.0;
2350 eners[1] += 4.0*M_PI*fr->enershifttwelve*rc3/3.0;
2352 /* Calculate the contribution in the range [r0,r1] where we
2353 * modify the potential. For a pure potential-shift modifier we will
2354 * have ri0==ri1, and there will not be any contribution here.
2356 for (i = 0; i < 2; i++)
2360 integrate_table(vdwtab, scale, (i == 0 ? 0 : 4), ri0, ri1, &enersum, &virsum);
2361 eners[i] -= enersum;
2365 /* Alright: Above we compensated by REMOVING the parts outside r0
2366 * corresponding to the ideal VdW 1/r6 and /r12 potentials.
2368 * Regardless of whether r0 is the point where we start switching,
2369 * or the cutoff where we calculated the constant shift, we include
2370 * all the parts we are missing out to infinity from r0 by
2371 * calculating the analytical dispersion correction.
2373 eners[0] += -4.0*M_PI/(3.0*rc3);
2374 eners[1] += 4.0*M_PI/(9.0*rc9);
2375 virs[0] += 8.0*M_PI/rc3;
2376 virs[1] += -16.0*M_PI/(3.0*rc9);
2378 else if (fr->vdwtype == evdwCUT ||
2379 EVDW_PME(fr->vdwtype) ||
2380 fr->vdwtype == evdwUSER)
2382 if (fr->vdwtype == evdwUSER && fplog)
2385 "WARNING: using dispersion correction with user tables\n");
2388 /* Note that with LJ-PME, the dispersion correction is multiplied
2389 * by the difference between the actual C6 and the value of C6
2390 * that would produce the combination rule.
2391 * This means the normal energy and virial difference formulas
2395 rc3 = fr->rvdw*fr->rvdw*fr->rvdw;
2397 /* Contribution beyond the cut-off */
2398 eners[0] += -4.0*M_PI/(3.0*rc3);
2399 eners[1] += 4.0*M_PI/(9.0*rc9);
2400 if (fr->vdw_modifier == eintmodPOTSHIFT)
2402 /* Contribution within the cut-off */
2403 eners[0] += -4.0*M_PI/(3.0*rc3);
2404 eners[1] += 4.0*M_PI/(3.0*rc9);
2406 /* Contribution beyond the cut-off */
2407 virs[0] += 8.0*M_PI/rc3;
2408 virs[1] += -16.0*M_PI/(3.0*rc9);
2413 "Dispersion correction is not implemented for vdw-type = %s",
2414 evdw_names[fr->vdwtype]);
2417 /* When we deprecate the group kernels the code below can go too */
2418 if (fr->vdwtype == evdwPME && fr->cutoff_scheme == ecutsGROUP)
2420 /* Calculate self-interaction coefficient (assuming that
2421 * the reciprocal-space contribution is constant in the
2422 * region that contributes to the self-interaction).
2424 fr->enershiftsix = pow(fr->ewaldcoeff_lj, 6) / 6.0;
2426 eners[0] += -pow(sqrt(M_PI)*fr->ewaldcoeff_lj, 3)/3.0;
2427 virs[0] += pow(sqrt(M_PI)*fr->ewaldcoeff_lj, 3);
2430 fr->enerdiffsix = eners[0];
2431 fr->enerdifftwelve = eners[1];
2432 /* The 0.5 is due to the Gromacs definition of the virial */
2433 fr->virdiffsix = 0.5*virs[0];
2434 fr->virdifftwelve = 0.5*virs[1];
2438 void calc_dispcorr(FILE *fplog, t_inputrec *ir, t_forcerec *fr,
2439 gmx_int64_t step, int natoms,
2440 matrix box, real lambda, tensor pres, tensor virial,
2441 real *prescorr, real *enercorr, real *dvdlcorr)
2443 gmx_bool bCorrAll, bCorrPres;
2444 real dvdlambda, invvol, dens, ninter, avcsix, avctwelve, enerdiff, svir = 0, spres = 0;
2454 if (ir->eDispCorr != edispcNO)
2456 bCorrAll = (ir->eDispCorr == edispcAllEner ||
2457 ir->eDispCorr == edispcAllEnerPres);
2458 bCorrPres = (ir->eDispCorr == edispcEnerPres ||
2459 ir->eDispCorr == edispcAllEnerPres);
2461 invvol = 1/det(box);
2464 /* Only correct for the interactions with the inserted molecule */
2465 dens = (natoms - fr->n_tpi)*invvol;
2470 dens = natoms*invvol;
2471 ninter = 0.5*natoms;
2474 if (ir->efep == efepNO)
2476 avcsix = fr->avcsix[0];
2477 avctwelve = fr->avctwelve[0];
2481 avcsix = (1 - lambda)*fr->avcsix[0] + lambda*fr->avcsix[1];
2482 avctwelve = (1 - lambda)*fr->avctwelve[0] + lambda*fr->avctwelve[1];
2485 enerdiff = ninter*(dens*fr->enerdiffsix - fr->enershiftsix);
2486 *enercorr += avcsix*enerdiff;
2488 if (ir->efep != efepNO)
2490 dvdlambda += (fr->avcsix[1] - fr->avcsix[0])*enerdiff;
2494 enerdiff = ninter*(dens*fr->enerdifftwelve - fr->enershifttwelve);
2495 *enercorr += avctwelve*enerdiff;
2496 if (fr->efep != efepNO)
2498 dvdlambda += (fr->avctwelve[1] - fr->avctwelve[0])*enerdiff;
2504 svir = ninter*dens*avcsix*fr->virdiffsix/3.0;
2505 if (ir->eDispCorr == edispcAllEnerPres)
2507 svir += ninter*dens*avctwelve*fr->virdifftwelve/3.0;
2509 /* The factor 2 is because of the Gromacs virial definition */
2510 spres = -2.0*invvol*svir*PRESFAC;
2512 for (m = 0; m < DIM; m++)
2514 virial[m][m] += svir;
2515 pres[m][m] += spres;
2520 /* Can't currently control when it prints, for now, just print when degugging */
2525 fprintf(debug, "Long Range LJ corr.: <C6> %10.4e, <C12> %10.4e\n",
2531 "Long Range LJ corr.: Epot %10g, Pres: %10g, Vir: %10g\n",
2532 *enercorr, spres, svir);
2536 fprintf(debug, "Long Range LJ corr.: Epot %10g\n", *enercorr);
2540 if (fr->bSepDVDL && do_per_step(step, ir->nstlog))
2542 gmx_print_sepdvdl(fplog, "Dispersion correction", *enercorr, dvdlambda);
2544 if (fr->efep != efepNO)
2546 *dvdlcorr += dvdlambda;
2551 void do_pbc_first(FILE *fplog, matrix box, t_forcerec *fr,
2552 t_graph *graph, rvec x[])
2556 fprintf(fplog, "Removing pbc first time\n");
2558 calc_shifts(box, fr->shift_vec);
2561 mk_mshift(fplog, graph, fr->ePBC, box, x);
2564 p_graph(debug, "do_pbc_first 1", graph);
2566 shift_self(graph, box, x);
2567 /* By doing an extra mk_mshift the molecules that are broken
2568 * because they were e.g. imported from another software
2569 * will be made whole again. Such are the healing powers
2572 mk_mshift(fplog, graph, fr->ePBC, box, x);
2575 p_graph(debug, "do_pbc_first 2", graph);
2580 fprintf(fplog, "Done rmpbc\n");
2584 static void low_do_pbc_mtop(FILE *fplog, int ePBC, matrix box,
2585 gmx_mtop_t *mtop, rvec x[],
2590 gmx_molblock_t *molb;
2592 if (bFirst && fplog)
2594 fprintf(fplog, "Removing pbc first time\n");
2599 for (mb = 0; mb < mtop->nmolblock; mb++)
2601 molb = &mtop->molblock[mb];
2602 if (molb->natoms_mol == 1 ||
2603 (!bFirst && mtop->moltype[molb->type].cgs.nr == 1))
2605 /* Just one atom or charge group in the molecule, no PBC required */
2606 as += molb->nmol*molb->natoms_mol;
2610 /* Pass NULL iso fplog to avoid graph prints for each molecule type */
2611 mk_graph_ilist(NULL, mtop->moltype[molb->type].ilist,
2612 0, molb->natoms_mol, FALSE, FALSE, graph);
2614 for (mol = 0; mol < molb->nmol; mol++)
2616 mk_mshift(fplog, graph, ePBC, box, x+as);
2618 shift_self(graph, box, x+as);
2619 /* The molecule is whole now.
2620 * We don't need the second mk_mshift call as in do_pbc_first,
2621 * since we no longer need this graph.
2624 as += molb->natoms_mol;
2632 void do_pbc_first_mtop(FILE *fplog, int ePBC, matrix box,
2633 gmx_mtop_t *mtop, rvec x[])
2635 low_do_pbc_mtop(fplog, ePBC, box, mtop, x, TRUE);
2638 void do_pbc_mtop(FILE *fplog, int ePBC, matrix box,
2639 gmx_mtop_t *mtop, rvec x[])
2641 low_do_pbc_mtop(fplog, ePBC, box, mtop, x, FALSE);
2644 void finish_run(FILE *fplog, t_commrec *cr,
2645 t_inputrec *inputrec,
2646 t_nrnb nrnb[], gmx_wallcycle_t wcycle,
2647 gmx_walltime_accounting_t walltime_accounting,
2648 wallclock_gpu_t *gputimes,
2649 gmx_bool bWriteStat)
2652 t_nrnb *nrnb_tot = NULL;
2655 double elapsed_time,
2656 elapsed_time_over_all_ranks,
2657 elapsed_time_over_all_threads,
2658 elapsed_time_over_all_threads_over_all_ranks;
2659 wallcycle_sum(cr, wcycle);
2665 MPI_Allreduce(nrnb->n, nrnb_tot->n, eNRNB, MPI_DOUBLE, MPI_SUM,
2666 cr->mpi_comm_mysim);
2674 elapsed_time = walltime_accounting_get_elapsed_time(walltime_accounting);
2675 elapsed_time_over_all_ranks = elapsed_time;
2676 elapsed_time_over_all_threads = walltime_accounting_get_elapsed_time_over_all_threads(walltime_accounting);
2677 elapsed_time_over_all_threads_over_all_ranks = elapsed_time_over_all_threads;
2681 /* reduce elapsed_time over all MPI ranks in the current simulation */
2682 MPI_Allreduce(&elapsed_time,
2683 &elapsed_time_over_all_ranks,
2684 1, MPI_DOUBLE, MPI_SUM,
2685 cr->mpi_comm_mysim);
2686 elapsed_time_over_all_ranks /= cr->nnodes;
2687 /* Reduce elapsed_time_over_all_threads over all MPI ranks in the
2688 * current simulation. */
2689 MPI_Allreduce(&elapsed_time_over_all_threads,
2690 &elapsed_time_over_all_threads_over_all_ranks,
2691 1, MPI_DOUBLE, MPI_SUM,
2692 cr->mpi_comm_mysim);
2698 print_flop(fplog, nrnb_tot, &nbfs, &mflop);
2705 if ((cr->duty & DUTY_PP) && DOMAINDECOMP(cr))
2707 print_dd_statistics(cr, inputrec, fplog);
2712 wallcycle_print(fplog, cr->nnodes, cr->npmenodes,
2713 elapsed_time_over_all_ranks,
2716 if (EI_DYNAMICS(inputrec->eI))
2718 delta_t = inputrec->delta_t;
2727 print_perf(fplog, elapsed_time_over_all_threads_over_all_ranks,
2728 elapsed_time_over_all_ranks,
2729 walltime_accounting_get_nsteps_done(walltime_accounting),
2730 delta_t, nbfs, mflop);
2734 print_perf(stderr, elapsed_time_over_all_threads_over_all_ranks,
2735 elapsed_time_over_all_ranks,
2736 walltime_accounting_get_nsteps_done(walltime_accounting),
2737 delta_t, nbfs, mflop);
2742 extern void initialize_lambdas(FILE *fplog, t_inputrec *ir, int *fep_state, real *lambda, double *lam0)
2744 /* this function works, but could probably use a logic rewrite to keep all the different
2745 types of efep straight. */
2748 t_lambda *fep = ir->fepvals;
2750 if ((ir->efep == efepNO) && (ir->bSimTemp == FALSE))
2752 for (i = 0; i < efptNR; i++)
2764 *fep_state = fep->init_fep_state; /* this might overwrite the checkpoint
2765 if checkpoint is set -- a kludge is in for now
2767 for (i = 0; i < efptNR; i++)
2769 /* overwrite lambda state with init_lambda for now for backwards compatibility */
2770 if (fep->init_lambda >= 0) /* if it's -1, it was never initializd */
2772 lambda[i] = fep->init_lambda;
2775 lam0[i] = lambda[i];
2780 lambda[i] = fep->all_lambda[i][*fep_state];
2783 lam0[i] = lambda[i];
2789 /* need to rescale control temperatures to match current state */
2790 for (i = 0; i < ir->opts.ngtc; i++)
2792 if (ir->opts.ref_t[i] > 0)
2794 ir->opts.ref_t[i] = ir->simtempvals->temperatures[*fep_state];
2800 /* Send to the log the information on the current lambdas */
2803 fprintf(fplog, "Initial vector of lambda components:[ ");
2804 for (i = 0; i < efptNR; i++)
2806 fprintf(fplog, "%10.4f ", lambda[i]);
2808 fprintf(fplog, "]\n");
2814 void init_md(FILE *fplog,
2815 t_commrec *cr, t_inputrec *ir, const output_env_t oenv,
2816 double *t, double *t0,
2817 real *lambda, int *fep_state, double *lam0,
2818 t_nrnb *nrnb, gmx_mtop_t *mtop,
2820 int nfile, const t_filenm fnm[],
2821 gmx_mdoutf_t *outf, t_mdebin **mdebin,
2822 tensor force_vir, tensor shake_vir, rvec mu_tot,
2823 gmx_bool *bSimAnn, t_vcm **vcm, unsigned long Flags)
2828 /* Initial values */
2829 *t = *t0 = ir->init_t;
2832 for (i = 0; i < ir->opts.ngtc; i++)
2834 /* set bSimAnn if any group is being annealed */
2835 if (ir->opts.annealing[i] != eannNO)
2842 update_annealing_target_temp(&(ir->opts), ir->init_t);
2845 /* Initialize lambda variables */
2846 initialize_lambdas(fplog, ir, fep_state, lambda, lam0);
2850 *upd = init_update(ir);
2856 *vcm = init_vcm(fplog, &mtop->groups, ir);
2859 if (EI_DYNAMICS(ir->eI) && !(Flags & MD_APPENDFILES))
2861 if (ir->etc == etcBERENDSEN)
2863 please_cite(fplog, "Berendsen84a");
2865 if (ir->etc == etcVRESCALE)
2867 please_cite(fplog, "Bussi2007a");
2869 if (ir->eI == eiSD1)
2871 please_cite(fplog, "Goga2012");
2879 *outf = init_mdoutf(fplog, nfile, fnm, Flags, cr, ir, mtop, oenv);
2881 *mdebin = init_mdebin((Flags & MD_APPENDFILES) ? NULL : mdoutf_get_fp_ene(*outf),
2882 mtop, ir, mdoutf_get_fp_dhdl(*outf));
2887 please_cite(fplog, "Fritsch12");
2888 please_cite(fplog, "Junghans10");
2890 /* Initiate variables */
2891 clear_mat(force_vir);
2892 clear_mat(shake_vir);