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.
45 #ifdef HAVE_SYS_TIME_H
50 #include "gromacs/utility/cstringutil.h"
51 #include "gromacs/utility/smalloc.h"
55 #include "chargegroup.h"
56 #include "gromacs/math/vec.h"
62 #include "gromacs/math/units.h"
75 #include "nbnxn_atomdata.h"
76 #include "nbnxn_search.h"
77 #include "nbnxn_kernels/nbnxn_kernel_ref.h"
78 #include "nbnxn_kernels/simd_4xn/nbnxn_kernel_simd_4xn.h"
79 #include "nbnxn_kernels/simd_2xnn/nbnxn_kernel_simd_2xnn.h"
80 #include "nbnxn_kernels/nbnxn_kernel_gpu_ref.h"
81 #include "nonbonded.h"
82 #include "../gmxlib/nonbonded/nb_kernel.h"
83 #include "../gmxlib/nonbonded/nb_free_energy.h"
85 #include "gromacs/legacyheaders/types/commrec.h"
86 #include "gromacs/timing/wallcycle.h"
87 #include "gromacs/timing/walltime_accounting.h"
88 #include "gromacs/utility/gmxmpi.h"
89 #include "gromacs/essentialdynamics/edsam.h"
90 #include "gromacs/pulling/pull.h"
91 #include "gromacs/pulling/pull_rotation.h"
92 #include "gromacs/imd/imd.h"
96 #include "gmx_omp_nthreads.h"
98 #include "nbnxn_cuda_data_mgmt.h"
99 #include "nbnxn_cuda/nbnxn_cuda.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 node %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
215 * part of the field (not yet used).
216 * Ex[] contains the parameters for
217 * the spatial dependent part of the field. You can have cool periodic
218 * fields in principle, but only a constant field is supported
220 * The function should return the energy due to the electric field
221 * (if any) but for now returns 0.
224 * There can be problems with the virial.
225 * Since the field is not self-consistent this is unavoidable.
226 * For neutral molecules the virial is correct within this approximation.
227 * For neutral systems with many charged molecules the error is small.
228 * But for systems with a net charge or a few charged molecules
229 * the error can be significant when the field is high.
230 * Solution: implement a self-consitent electric field into PME.
232 static void calc_f_el(FILE *fp, int start, int homenr,
233 real charge[], rvec f[],
234 t_cosines Ex[], t_cosines Et[], double t)
240 for (m = 0; (m < DIM); m++)
247 Ext[m] = cos(Et[m].a[0]*(t-t0))*exp(-sqr(t-t0)/(2.0*sqr(Et[m].a[2])));
251 Ext[m] = cos(Et[m].a[0]*t);
260 /* Convert the field strength from V/nm to MD-units */
261 Ext[m] *= Ex[m].a[0]*FIELDFAC;
262 for (i = start; (i < start+homenr); i++)
264 f[i][m] += charge[i]*Ext[m];
274 fprintf(fp, "%10g %10g %10g %10g #FIELD\n", t,
275 Ext[XX]/FIELDFAC, Ext[YY]/FIELDFAC, Ext[ZZ]/FIELDFAC);
279 static void calc_virial(int start, int homenr, rvec x[], rvec f[],
280 tensor vir_part, t_graph *graph, matrix box,
281 t_nrnb *nrnb, const t_forcerec *fr, int ePBC)
286 /* The short-range virial from surrounding boxes */
288 calc_vir(SHIFTS, fr->shift_vec, fr->fshift, vir_part, ePBC == epbcSCREW, box);
289 inc_nrnb(nrnb, eNR_VIRIAL, SHIFTS);
291 /* Calculate partial virial, for local atoms only, based on short range.
292 * Total virial is computed in global_stat, called from do_md
294 f_calc_vir(start, start+homenr, x, f, vir_part, graph, box);
295 inc_nrnb(nrnb, eNR_VIRIAL, homenr);
297 /* Add position restraint contribution */
298 for (i = 0; i < DIM; i++)
300 vir_part[i][i] += fr->vir_diag_posres[i];
303 /* Add wall contribution */
304 for (i = 0; i < DIM; i++)
306 vir_part[i][ZZ] += fr->vir_wall_z[i];
311 pr_rvecs(debug, 0, "vir_part", vir_part, DIM);
315 static void posres_wrapper(FILE *fplog,
321 matrix box, rvec x[],
322 gmx_enerdata_t *enerd,
330 /* Position restraints always require full pbc */
331 set_pbc(&pbc, ir->ePBC, box);
333 v = posres(top->idef.il[F_POSRES].nr, top->idef.il[F_POSRES].iatoms,
334 top->idef.iparams_posres,
335 (const rvec*)x, fr->f_novirsum, fr->vir_diag_posres,
336 ir->ePBC == epbcNONE ? NULL : &pbc,
337 lambda[efptRESTRAINT], &dvdl,
338 fr->rc_scaling, fr->ePBC, fr->posres_com, fr->posres_comB);
341 gmx_print_sepdvdl(fplog, interaction_function[F_POSRES].longname, v, dvdl);
343 enerd->term[F_POSRES] += v;
344 /* If just the force constant changes, the FEP term is linear,
345 * but if k changes, it is not.
347 enerd->dvdl_nonlin[efptRESTRAINT] += dvdl;
348 inc_nrnb(nrnb, eNR_POSRES, top->idef.il[F_POSRES].nr/2);
350 if ((ir->fepvals->n_lambda > 0) && (flags & GMX_FORCE_DHDL))
352 for (i = 0; i < enerd->n_lambda; i++)
354 real dvdl_dum, lambda_dum;
356 lambda_dum = (i == 0 ? lambda[efptRESTRAINT] : ir->fepvals->all_lambda[efptRESTRAINT][i-1]);
357 v = posres(top->idef.il[F_POSRES].nr, top->idef.il[F_POSRES].iatoms,
358 top->idef.iparams_posres,
359 (const rvec*)x, NULL, NULL,
360 ir->ePBC == epbcNONE ? NULL : &pbc, lambda_dum, &dvdl,
361 fr->rc_scaling, fr->ePBC, fr->posres_com, fr->posres_comB);
362 enerd->enerpart_lambda[i] += v;
367 static void fbposres_wrapper(t_inputrec *ir,
370 matrix box, rvec x[],
371 gmx_enerdata_t *enerd,
377 /* Flat-bottomed position restraints always require full pbc */
378 set_pbc(&pbc, ir->ePBC, box);
379 v = fbposres(top->idef.il[F_FBPOSRES].nr, top->idef.il[F_FBPOSRES].iatoms,
380 top->idef.iparams_fbposres,
381 (const rvec*)x, fr->f_novirsum, fr->vir_diag_posres,
382 ir->ePBC == epbcNONE ? NULL : &pbc,
383 fr->rc_scaling, fr->ePBC, fr->posres_com);
384 enerd->term[F_FBPOSRES] += v;
385 inc_nrnb(nrnb, eNR_FBPOSRES, top->idef.il[F_FBPOSRES].nr/2);
388 static void pull_potential_wrapper(FILE *fplog,
392 matrix box, rvec x[],
396 gmx_enerdata_t *enerd,
403 /* Calculate the center of mass forces, this requires communication,
404 * which is why pull_potential is called close to other communication.
405 * The virial contribution is calculated directly,
406 * which is why we call pull_potential after calc_virial.
408 set_pbc(&pbc, ir->ePBC, box);
410 enerd->term[F_COM_PULL] +=
411 pull_potential(ir->ePull, ir->pull, mdatoms, &pbc,
412 cr, t, lambda[efptRESTRAINT], x, f, vir_force, &dvdl);
415 gmx_print_sepdvdl(fplog, "Com pull", enerd->term[F_COM_PULL], dvdl);
417 enerd->dvdl_lin[efptRESTRAINT] += dvdl;
420 static void pme_receive_force_ener(FILE *fplog,
423 gmx_wallcycle_t wcycle,
424 gmx_enerdata_t *enerd,
427 real e_q, e_lj, v, dvdl_q, dvdl_lj;
428 float cycles_ppdpme, cycles_seppme;
430 cycles_ppdpme = wallcycle_stop(wcycle, ewcPPDURINGPME);
431 dd_cycles_add(cr->dd, cycles_ppdpme, ddCyclPPduringPME);
433 /* In case of node-splitting, the PP nodes receive the long-range
434 * forces, virial and energy from the PME nodes here.
436 wallcycle_start(wcycle, ewcPP_PMEWAITRECVF);
439 gmx_pme_receive_f(cr, fr->f_novirsum, fr->vir_el_recip, &e_q,
440 fr->vir_lj_recip, &e_lj, &dvdl_q, &dvdl_lj,
444 gmx_print_sepdvdl(fplog, "Electrostatic PME mesh", e_q, dvdl_q);
445 gmx_print_sepdvdl(fplog, "Lennard-Jones PME mesh", e_lj, dvdl_lj);
447 enerd->term[F_COUL_RECIP] += e_q;
448 enerd->term[F_LJ_RECIP] += e_lj;
449 enerd->dvdl_lin[efptCOUL] += dvdl_q;
450 enerd->dvdl_lin[efptVDW] += dvdl_lj;
454 dd_cycles_add(cr->dd, cycles_seppme, ddCyclPME);
456 wallcycle_stop(wcycle, ewcPP_PMEWAITRECVF);
459 static void print_large_forces(FILE *fp, t_mdatoms *md, t_commrec *cr,
460 gmx_int64_t step, real pforce, rvec *x, rvec *f)
464 char buf[STEPSTRSIZE];
467 for (i = 0; i < md->homenr; i++)
470 /* We also catch NAN, if the compiler does not optimize this away. */
471 if (fn2 >= pf2 || fn2 != fn2)
473 fprintf(fp, "step %s atom %6d x %8.3f %8.3f %8.3f force %12.5e\n",
474 gmx_step_str(step, buf),
475 ddglatnr(cr->dd, i), x[i][XX], x[i][YY], x[i][ZZ], sqrt(fn2));
480 static void post_process_forces(t_commrec *cr,
482 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
484 matrix box, rvec x[],
489 t_forcerec *fr, gmx_vsite_t *vsite,
496 /* Spread the mesh force on virtual sites to the other particles...
497 * This is parallellized. MPI communication is performed
498 * if the constructing atoms aren't local.
500 wallcycle_start(wcycle, ewcVSITESPREAD);
501 spread_vsite_f(vsite, x, fr->f_novirsum, NULL,
502 (flags & GMX_FORCE_VIRIAL), fr->vir_el_recip,
504 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
505 wallcycle_stop(wcycle, ewcVSITESPREAD);
507 if (flags & GMX_FORCE_VIRIAL)
509 /* Now add the forces, this is local */
512 sum_forces(0, fr->f_novirsum_n, f, fr->f_novirsum);
516 sum_forces(0, mdatoms->homenr,
519 if (EEL_FULL(fr->eeltype))
521 /* Add the mesh contribution to the virial */
522 m_add(vir_force, fr->vir_el_recip, vir_force);
524 if (EVDW_PME(fr->vdwtype))
526 /* Add the mesh contribution to the virial */
527 m_add(vir_force, fr->vir_lj_recip, vir_force);
531 pr_rvecs(debug, 0, "vir_force", vir_force, DIM);
536 if (fr->print_force >= 0)
538 print_large_forces(stderr, mdatoms, cr, step, fr->print_force, x, f);
542 static void do_nb_verlet(t_forcerec *fr,
543 interaction_const_t *ic,
544 gmx_enerdata_t *enerd,
545 int flags, int ilocality,
548 gmx_wallcycle_t wcycle)
550 int nnbl, kernel_type, enr_nbnxn_kernel_ljc, enr_nbnxn_kernel_lj;
552 nonbonded_verlet_group_t *nbvg;
555 if (!(flags & GMX_FORCE_NONBONDED))
557 /* skip non-bonded calculation */
561 nbvg = &fr->nbv->grp[ilocality];
563 /* CUDA kernel launch overhead is already timed separately */
564 if (fr->cutoff_scheme != ecutsVERLET)
566 gmx_incons("Invalid cut-off scheme passed!");
569 bCUDA = (nbvg->kernel_type == nbnxnk8x8x8_CUDA);
573 wallcycle_sub_start(wcycle, ewcsNONBONDED);
575 switch (nbvg->kernel_type)
577 case nbnxnk4x4_PlainC:
578 nbnxn_kernel_ref(&nbvg->nbl_lists,
584 enerd->grpp.ener[egCOULSR],
586 enerd->grpp.ener[egBHAMSR] :
587 enerd->grpp.ener[egLJSR]);
590 case nbnxnk4xN_SIMD_4xN:
591 nbnxn_kernel_simd_4xn(&nbvg->nbl_lists,
598 enerd->grpp.ener[egCOULSR],
600 enerd->grpp.ener[egBHAMSR] :
601 enerd->grpp.ener[egLJSR]);
603 case nbnxnk4xN_SIMD_2xNN:
604 nbnxn_kernel_simd_2xnn(&nbvg->nbl_lists,
611 enerd->grpp.ener[egCOULSR],
613 enerd->grpp.ener[egBHAMSR] :
614 enerd->grpp.ener[egLJSR]);
617 case nbnxnk8x8x8_CUDA:
618 nbnxn_cuda_launch_kernel(fr->nbv->cu_nbv, nbvg->nbat, flags, ilocality);
621 case nbnxnk8x8x8_PlainC:
622 nbnxn_kernel_gpu_ref(nbvg->nbl_lists.nbl[0],
627 nbvg->nbat->out[0].f,
629 enerd->grpp.ener[egCOULSR],
631 enerd->grpp.ener[egBHAMSR] :
632 enerd->grpp.ener[egLJSR]);
636 gmx_incons("Invalid nonbonded kernel type passed!");
641 wallcycle_sub_stop(wcycle, ewcsNONBONDED);
644 if (EEL_RF(ic->eeltype) || ic->eeltype == eelCUT)
646 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_RF;
648 else if ((!bCUDA && nbvg->ewald_excl == ewaldexclAnalytical) ||
649 (bCUDA && nbnxn_cuda_is_kernel_ewald_analytical(fr->nbv->cu_nbv)))
651 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_EWALD;
655 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_TAB;
657 enr_nbnxn_kernel_lj = eNR_NBNXN_LJ;
658 if (flags & GMX_FORCE_ENERGY)
660 /* In eNR_??? the nbnxn F+E kernels are always the F kernel + 1 */
661 enr_nbnxn_kernel_ljc += 1;
662 enr_nbnxn_kernel_lj += 1;
665 inc_nrnb(nrnb, enr_nbnxn_kernel_ljc,
666 nbvg->nbl_lists.natpair_ljq);
667 inc_nrnb(nrnb, enr_nbnxn_kernel_lj,
668 nbvg->nbl_lists.natpair_lj);
669 /* The Coulomb-only kernels are offset -eNR_NBNXN_LJ_RF+eNR_NBNXN_RF */
670 inc_nrnb(nrnb, enr_nbnxn_kernel_ljc-eNR_NBNXN_LJ_RF+eNR_NBNXN_RF,
671 nbvg->nbl_lists.natpair_q);
673 if (ic->vdw_modifier == eintmodFORCESWITCH)
675 /* We add up the switch cost separately */
676 inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_FSW+((flags & GMX_FORCE_ENERGY) ? 1 : 0),
677 nbvg->nbl_lists.natpair_ljq + nbvg->nbl_lists.natpair_lj);
679 if (ic->vdw_modifier == eintmodPOTSWITCH)
681 /* We add up the switch cost separately */
682 inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_PSW+((flags & GMX_FORCE_ENERGY) ? 1 : 0),
683 nbvg->nbl_lists.natpair_ljq + nbvg->nbl_lists.natpair_lj);
685 if (ic->vdwtype == evdwPME)
687 /* We add up the LJ Ewald cost separately */
688 inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_EWALD+((flags & GMX_FORCE_ENERGY) ? 1 : 0),
689 nbvg->nbl_lists.natpair_ljq + nbvg->nbl_lists.natpair_lj);
693 static void do_nb_verlet_fep(nbnxn_pairlist_set_t *nbl_lists,
700 gmx_enerdata_t *enerd,
703 gmx_wallcycle_t wcycle)
706 nb_kernel_data_t kernel_data;
708 real dvdl_nb[efptNR];
713 /* Add short-range interactions */
714 donb_flags |= GMX_NONBONDED_DO_SR;
716 /* Currently all group scheme kernels always calculate (shift-)forces */
717 if (flags & GMX_FORCE_FORCES)
719 donb_flags |= GMX_NONBONDED_DO_FORCE;
721 if (flags & GMX_FORCE_VIRIAL)
723 donb_flags |= GMX_NONBONDED_DO_SHIFTFORCE;
725 if (flags & GMX_FORCE_ENERGY)
727 donb_flags |= GMX_NONBONDED_DO_POTENTIAL;
729 if (flags & GMX_FORCE_DO_LR)
731 donb_flags |= GMX_NONBONDED_DO_LR;
734 kernel_data.flags = donb_flags;
735 kernel_data.lambda = lambda;
736 kernel_data.dvdl = dvdl_nb;
738 kernel_data.energygrp_elec = enerd->grpp.ener[egCOULSR];
739 kernel_data.energygrp_vdw = enerd->grpp.ener[egLJSR];
741 /* reset free energy components */
742 for (i = 0; i < efptNR; i++)
747 assert(gmx_omp_nthreads_get(emntNonbonded) == nbl_lists->nnbl);
749 wallcycle_sub_start(wcycle, ewcsNONBONDED);
750 #pragma omp parallel for schedule(static) num_threads(nbl_lists->nnbl)
751 for (th = 0; th < nbl_lists->nnbl; th++)
753 gmx_nb_free_energy_kernel(nbl_lists->nbl_fep[th],
754 x, f, fr, mdatoms, &kernel_data, nrnb);
757 if (fepvals->sc_alpha != 0)
759 enerd->dvdl_nonlin[efptVDW] += dvdl_nb[efptVDW];
760 enerd->dvdl_nonlin[efptCOUL] += dvdl_nb[efptCOUL];
764 enerd->dvdl_lin[efptVDW] += dvdl_nb[efptVDW];
765 enerd->dvdl_lin[efptCOUL] += dvdl_nb[efptCOUL];
768 /* If we do foreign lambda and we have soft-core interactions
769 * we have to recalculate the (non-linear) energies contributions.
771 if (fepvals->n_lambda > 0 && (flags & GMX_FORCE_DHDL) && fepvals->sc_alpha != 0)
773 kernel_data.flags = (donb_flags & ~(GMX_NONBONDED_DO_FORCE | GMX_NONBONDED_DO_SHIFTFORCE)) | GMX_NONBONDED_DO_FOREIGNLAMBDA;
774 kernel_data.lambda = lam_i;
775 kernel_data.energygrp_elec = enerd->foreign_grpp.ener[egCOULSR];
776 kernel_data.energygrp_vdw = enerd->foreign_grpp.ener[egLJSR];
777 /* Note that we add to kernel_data.dvdl, but ignore the result */
779 for (i = 0; i < enerd->n_lambda; i++)
781 for (j = 0; j < efptNR; j++)
783 lam_i[j] = (i == 0 ? lambda[j] : fepvals->all_lambda[j][i-1]);
785 reset_foreign_enerdata(enerd);
786 #pragma omp parallel for schedule(static) num_threads(nbl_lists->nnbl)
787 for (th = 0; th < nbl_lists->nnbl; th++)
789 gmx_nb_free_energy_kernel(nbl_lists->nbl_fep[th],
790 x, f, fr, mdatoms, &kernel_data, nrnb);
793 sum_epot(&(enerd->foreign_grpp), enerd->foreign_term);
794 enerd->enerpart_lambda[i] += enerd->foreign_term[F_EPOT];
798 wallcycle_sub_stop(wcycle, ewcsNONBONDED);
801 void do_force_cutsVERLET(FILE *fplog, t_commrec *cr,
802 t_inputrec *inputrec,
803 gmx_int64_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
805 gmx_groups_t gmx_unused *groups,
806 matrix box, rvec x[], history_t *hist,
810 gmx_enerdata_t *enerd, t_fcdata *fcd,
811 real *lambda, t_graph *graph,
812 t_forcerec *fr, interaction_const_t *ic,
813 gmx_vsite_t *vsite, rvec mu_tot,
814 double t, FILE *field, gmx_edsam_t ed,
822 gmx_bool bSepDVDL, bStateChanged, bNS, bFillGrid, bCalcCGCM, bBS;
823 gmx_bool bDoLongRange, bDoForces, bSepLRF, bUseGPU, bUseOrEmulGPU;
824 gmx_bool bDiffKernels = FALSE;
826 rvec vzero, box_diag;
828 float cycles_pme, cycles_force, cycles_wait_gpu;
829 nonbonded_verlet_t *nbv;
834 nb_kernel_type = fr->nbv->grp[0].kernel_type;
837 homenr = mdatoms->homenr;
839 bSepDVDL = (fr->bSepDVDL && do_per_step(step, inputrec->nstlog));
841 clear_mat(vir_force);
844 if (DOMAINDECOMP(cr))
846 cg1 = cr->dd->ncg_tot;
857 bStateChanged = (flags & GMX_FORCE_STATECHANGED);
858 bNS = (flags & GMX_FORCE_NS) && (fr->bAllvsAll == FALSE);
859 bFillGrid = (bNS && bStateChanged);
860 bCalcCGCM = (bFillGrid && !DOMAINDECOMP(cr));
861 bDoLongRange = (fr->bTwinRange && bNS && (flags & GMX_FORCE_DO_LR));
862 bDoForces = (flags & GMX_FORCE_FORCES);
863 bSepLRF = (bDoLongRange && bDoForces && (flags & GMX_FORCE_SEPLRF));
864 bUseGPU = fr->nbv->bUseGPU;
865 bUseOrEmulGPU = bUseGPU || (nbv->grp[0].kernel_type == nbnxnk8x8x8_PlainC);
869 update_forcerec(fr, box);
871 if (NEED_MUTOT(*inputrec))
873 /* Calculate total (local) dipole moment in a temporary common array.
874 * This makes it possible to sum them over nodes faster.
876 calc_mu(start, homenr,
877 x, mdatoms->chargeA, mdatoms->chargeB, mdatoms->nChargePerturbed,
882 if (fr->ePBC != epbcNONE)
884 /* Compute shift vectors every step,
885 * because of pressure coupling or box deformation!
887 if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
889 calc_shifts(box, fr->shift_vec);
894 put_atoms_in_box_omp(fr->ePBC, box, homenr, x);
895 inc_nrnb(nrnb, eNR_SHIFTX, homenr);
897 else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph)
899 unshift_self(graph, box, x);
903 nbnxn_atomdata_copy_shiftvec(flags & GMX_FORCE_DYNAMICBOX,
904 fr->shift_vec, nbv->grp[0].nbat);
907 if (!(cr->duty & DUTY_PME))
909 /* Send particle coordinates to the pme nodes.
910 * Since this is only implemented for domain decomposition
911 * and domain decomposition does not use the graph,
912 * we do not need to worry about shifting.
917 wallcycle_start(wcycle, ewcPP_PMESENDX);
919 bBS = (inputrec->nwall == 2);
923 svmul(inputrec->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
926 if (EEL_PME(fr->eeltype))
928 pme_flags |= GMX_PME_DO_COULOMB;
931 if (EVDW_PME(fr->vdwtype))
933 pme_flags |= GMX_PME_DO_LJ;
936 gmx_pme_send_coordinates(cr, bBS ? boxs : box, x,
937 mdatoms->nChargePerturbed, mdatoms->nTypePerturbed, lambda[efptCOUL], lambda[efptVDW],
938 (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)),
941 wallcycle_stop(wcycle, ewcPP_PMESENDX);
945 /* do gridding for pair search */
948 if (graph && bStateChanged)
950 /* Calculate intramolecular shift vectors to make molecules whole */
951 mk_mshift(fplog, graph, fr->ePBC, box, x);
955 box_diag[XX] = box[XX][XX];
956 box_diag[YY] = box[YY][YY];
957 box_diag[ZZ] = box[ZZ][ZZ];
959 wallcycle_start(wcycle, ewcNS);
962 wallcycle_sub_start(wcycle, ewcsNBS_GRID_LOCAL);
963 nbnxn_put_on_grid(nbv->nbs, fr->ePBC, box,
965 0, mdatoms->homenr, -1, fr->cginfo, x,
967 nbv->grp[eintLocal].kernel_type,
968 nbv->grp[eintLocal].nbat);
969 wallcycle_sub_stop(wcycle, ewcsNBS_GRID_LOCAL);
973 wallcycle_sub_start(wcycle, ewcsNBS_GRID_NONLOCAL);
974 nbnxn_put_on_grid_nonlocal(nbv->nbs, domdec_zones(cr->dd),
976 nbv->grp[eintNonlocal].kernel_type,
977 nbv->grp[eintNonlocal].nbat);
978 wallcycle_sub_stop(wcycle, ewcsNBS_GRID_NONLOCAL);
981 if (nbv->ngrp == 1 ||
982 nbv->grp[eintNonlocal].nbat == nbv->grp[eintLocal].nbat)
984 nbnxn_atomdata_set(nbv->grp[eintLocal].nbat, eatAll,
985 nbv->nbs, mdatoms, fr->cginfo);
989 nbnxn_atomdata_set(nbv->grp[eintLocal].nbat, eatLocal,
990 nbv->nbs, mdatoms, fr->cginfo);
991 nbnxn_atomdata_set(nbv->grp[eintNonlocal].nbat, eatAll,
992 nbv->nbs, mdatoms, fr->cginfo);
994 wallcycle_stop(wcycle, ewcNS);
997 /* initialize the GPU atom data and copy shift vector */
1002 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1003 nbnxn_cuda_init_atomdata(nbv->cu_nbv, nbv->grp[eintLocal].nbat);
1004 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1007 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1008 nbnxn_cuda_upload_shiftvec(nbv->cu_nbv, nbv->grp[eintLocal].nbat);
1009 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1012 /* do local pair search */
1015 wallcycle_start_nocount(wcycle, ewcNS);
1016 wallcycle_sub_start(wcycle, ewcsNBS_SEARCH_LOCAL);
1017 nbnxn_make_pairlist(nbv->nbs, nbv->grp[eintLocal].nbat,
1020 nbv->min_ci_balanced,
1021 &nbv->grp[eintLocal].nbl_lists,
1023 nbv->grp[eintLocal].kernel_type,
1025 wallcycle_sub_stop(wcycle, ewcsNBS_SEARCH_LOCAL);
1029 /* initialize local pair-list on the GPU */
1030 nbnxn_cuda_init_pairlist(nbv->cu_nbv,
1031 nbv->grp[eintLocal].nbl_lists.nbl[0],
1034 wallcycle_stop(wcycle, ewcNS);
1038 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1039 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
1040 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatLocal, FALSE, x,
1041 nbv->grp[eintLocal].nbat);
1042 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
1043 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1048 wallcycle_start(wcycle, ewcLAUNCH_GPU_NB);
1049 /* launch local nonbonded F on GPU */
1050 do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFNo,
1052 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1055 /* Communicate coordinates and sum dipole if necessary +
1056 do non-local pair search */
1057 if (DOMAINDECOMP(cr))
1059 bDiffKernels = (nbv->grp[eintNonlocal].kernel_type !=
1060 nbv->grp[eintLocal].kernel_type);
1064 /* With GPU+CPU non-bonded calculations we need to copy
1065 * the local coordinates to the non-local nbat struct
1066 * (in CPU format) as the non-local kernel call also
1067 * calculates the local - non-local interactions.
1069 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1070 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
1071 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatLocal, TRUE, x,
1072 nbv->grp[eintNonlocal].nbat);
1073 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
1074 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1079 wallcycle_start_nocount(wcycle, ewcNS);
1080 wallcycle_sub_start(wcycle, ewcsNBS_SEARCH_NONLOCAL);
1084 nbnxn_grid_add_simple(nbv->nbs, nbv->grp[eintNonlocal].nbat);
1087 nbnxn_make_pairlist(nbv->nbs, nbv->grp[eintNonlocal].nbat,
1090 nbv->min_ci_balanced,
1091 &nbv->grp[eintNonlocal].nbl_lists,
1093 nbv->grp[eintNonlocal].kernel_type,
1096 wallcycle_sub_stop(wcycle, ewcsNBS_SEARCH_NONLOCAL);
1098 if (nbv->grp[eintNonlocal].kernel_type == nbnxnk8x8x8_CUDA)
1100 /* initialize non-local pair-list on the GPU */
1101 nbnxn_cuda_init_pairlist(nbv->cu_nbv,
1102 nbv->grp[eintNonlocal].nbl_lists.nbl[0],
1105 wallcycle_stop(wcycle, ewcNS);
1109 wallcycle_start(wcycle, ewcMOVEX);
1110 dd_move_x(cr->dd, box, x);
1112 /* When we don't need the total dipole we sum it in global_stat */
1113 if (bStateChanged && NEED_MUTOT(*inputrec))
1115 gmx_sumd(2*DIM, mu, cr);
1117 wallcycle_stop(wcycle, ewcMOVEX);
1119 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1120 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
1121 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatNonlocal, FALSE, x,
1122 nbv->grp[eintNonlocal].nbat);
1123 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
1124 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1127 if (bUseGPU && !bDiffKernels)
1129 wallcycle_start(wcycle, ewcLAUNCH_GPU_NB);
1130 /* launch non-local nonbonded F on GPU */
1131 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFNo,
1133 cycles_force += wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1139 /* launch D2H copy-back F */
1140 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1141 if (DOMAINDECOMP(cr) && !bDiffKernels)
1143 nbnxn_cuda_launch_cpyback(nbv->cu_nbv, nbv->grp[eintNonlocal].nbat,
1144 flags, eatNonlocal);
1146 nbnxn_cuda_launch_cpyback(nbv->cu_nbv, nbv->grp[eintLocal].nbat,
1148 cycles_force += wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1151 if (bStateChanged && NEED_MUTOT(*inputrec))
1155 gmx_sumd(2*DIM, mu, cr);
1158 for (i = 0; i < 2; i++)
1160 for (j = 0; j < DIM; j++)
1162 fr->mu_tot[i][j] = mu[i*DIM + j];
1166 if (fr->efep == efepNO)
1168 copy_rvec(fr->mu_tot[0], mu_tot);
1172 for (j = 0; j < DIM; j++)
1175 (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] +
1176 lambda[efptCOUL]*fr->mu_tot[1][j];
1180 /* Reset energies */
1181 reset_enerdata(fr, bNS, enerd, MASTER(cr));
1182 clear_rvecs(SHIFTS, fr->fshift);
1184 if (DOMAINDECOMP(cr) && !(cr->duty & DUTY_PME))
1186 wallcycle_start(wcycle, ewcPPDURINGPME);
1187 dd_force_flop_start(cr->dd, nrnb);
1192 /* Enforced rotation has its own cycle counter that starts after the collective
1193 * coordinates have been communicated. It is added to ddCyclF to allow
1194 * for proper load-balancing */
1195 wallcycle_start(wcycle, ewcROT);
1196 do_rotation(cr, inputrec, box, x, t, step, wcycle, bNS);
1197 wallcycle_stop(wcycle, ewcROT);
1200 /* Start the force cycle counter.
1201 * This counter is stopped in do_forcelow_level.
1202 * No parallel communication should occur while this counter is running,
1203 * since that will interfere with the dynamic load balancing.
1205 wallcycle_start(wcycle, ewcFORCE);
1208 /* Reset forces for which the virial is calculated separately:
1209 * PME/Ewald forces if necessary */
1210 if (fr->bF_NoVirSum)
1212 if (flags & GMX_FORCE_VIRIAL)
1214 fr->f_novirsum = fr->f_novirsum_alloc;
1217 clear_rvecs(fr->f_novirsum_n, fr->f_novirsum);
1221 clear_rvecs(homenr, fr->f_novirsum+start);
1226 /* We are not calculating the pressure so we do not need
1227 * a separate array for forces that do not contribute
1234 /* Clear the short- and long-range forces */
1235 clear_rvecs(fr->natoms_force_constr, f);
1236 if (bSepLRF && do_per_step(step, inputrec->nstcalclr))
1238 clear_rvecs(fr->natoms_force_constr, fr->f_twin);
1241 clear_rvec(fr->vir_diag_posres);
1244 if (inputrec->ePull == epullCONSTRAINT)
1246 clear_pull_forces(inputrec->pull);
1249 /* We calculate the non-bonded forces, when done on the CPU, here.
1250 * We do this before calling do_force_lowlevel, as in there bondeds
1251 * forces are calculated before PME, which does communication.
1252 * With this order, non-bonded and bonded force calculation imbalance
1253 * can be balanced out by the domain decomposition load balancing.
1258 /* Maybe we should move this into do_force_lowlevel */
1259 do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFYes,
1263 if (fr->efep != efepNO)
1265 /* Calculate the local and non-local free energy interactions here.
1266 * Happens here on the CPU both with and without GPU.
1268 if (fr->nbv->grp[eintLocal].nbl_lists.nbl_fep[0]->nrj > 0)
1270 do_nb_verlet_fep(&fr->nbv->grp[eintLocal].nbl_lists,
1272 inputrec->fepvals, lambda,
1273 enerd, flags, nrnb, wcycle);
1276 if (DOMAINDECOMP(cr) &&
1277 fr->nbv->grp[eintNonlocal].nbl_lists.nbl_fep[0]->nrj > 0)
1279 do_nb_verlet_fep(&fr->nbv->grp[eintNonlocal].nbl_lists,
1281 inputrec->fepvals, lambda,
1282 enerd, flags, nrnb, wcycle);
1286 if (!bUseOrEmulGPU || bDiffKernels)
1290 if (DOMAINDECOMP(cr))
1292 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal,
1293 bDiffKernels ? enbvClearFYes : enbvClearFNo,
1303 aloc = eintNonlocal;
1306 /* Add all the non-bonded force to the normal force array.
1307 * This can be split into a local a non-local part when overlapping
1308 * communication with calculation with domain decomposition.
1310 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1311 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1312 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1313 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatAll, nbv->grp[aloc].nbat, f);
1314 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1315 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1316 wallcycle_start_nocount(wcycle, ewcFORCE);
1318 /* if there are multiple fshift output buffers reduce them */
1319 if ((flags & GMX_FORCE_VIRIAL) &&
1320 nbv->grp[aloc].nbl_lists.nnbl > 1)
1322 nbnxn_atomdata_add_nbat_fshift_to_fshift(nbv->grp[aloc].nbat,
1327 /* update QMMMrec, if necessary */
1330 update_QMMMrec(cr, fr, x, mdatoms, box, top);
1333 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_POSRES].nr > 0)
1335 posres_wrapper(fplog, flags, bSepDVDL, inputrec, nrnb, top, box, x,
1339 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_FBPOSRES].nr > 0)
1341 fbposres_wrapper(inputrec, nrnb, top, box, x, enerd, fr);
1344 /* Compute the bonded and non-bonded energies and optionally forces */
1345 do_force_lowlevel(fplog, step, fr, inputrec, &(top->idef),
1346 cr, nrnb, wcycle, mdatoms,
1347 x, hist, f, bSepLRF ? fr->f_twin : f, enerd, fcd, top, fr->born,
1348 &(top->atomtypes), bBornRadii, box,
1349 inputrec->fepvals, lambda, graph, &(top->excls), fr->mu_tot,
1350 flags, &cycles_pme);
1354 if (do_per_step(step, inputrec->nstcalclr))
1356 /* Add the long range forces to the short range forces */
1357 for (i = 0; i < fr->natoms_force_constr; i++)
1359 rvec_add(fr->f_twin[i], f[i], f[i]);
1364 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1368 do_flood(cr, inputrec, x, f, ed, box, step, bNS);
1371 if (bUseOrEmulGPU && !bDiffKernels)
1373 /* wait for non-local forces (or calculate in emulation mode) */
1374 if (DOMAINDECOMP(cr))
1380 wallcycle_start(wcycle, ewcWAIT_GPU_NB_NL);
1381 nbnxn_cuda_wait_gpu(nbv->cu_nbv,
1382 nbv->grp[eintNonlocal].nbat,
1384 enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1386 cycles_tmp = wallcycle_stop(wcycle, ewcWAIT_GPU_NB_NL);
1387 cycles_wait_gpu += cycles_tmp;
1388 cycles_force += cycles_tmp;
1392 wallcycle_start_nocount(wcycle, ewcFORCE);
1393 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFYes,
1395 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1397 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1398 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1399 /* skip the reduction if there was no non-local work to do */
1400 if (nbv->grp[eintLocal].nbl_lists.nbl[0]->nsci > 0)
1402 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatNonlocal,
1403 nbv->grp[eintNonlocal].nbat, f);
1405 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1406 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1410 if (bDoForces && DOMAINDECOMP(cr))
1412 /* Communicate the forces */
1413 wallcycle_start(wcycle, ewcMOVEF);
1414 dd_move_f(cr->dd, f, fr->fshift);
1415 /* Do we need to communicate the separate force array
1416 * for terms that do not contribute to the single sum virial?
1417 * Position restraints and electric fields do not introduce
1418 * inter-cg forces, only full electrostatics methods do.
1419 * When we do not calculate the virial, fr->f_novirsum = f,
1420 * so we have already communicated these forces.
1422 if (EEL_FULL(fr->eeltype) && cr->dd->n_intercg_excl &&
1423 (flags & GMX_FORCE_VIRIAL))
1425 dd_move_f(cr->dd, fr->f_novirsum, NULL);
1429 /* We should not update the shift forces here,
1430 * since f_twin is already included in f.
1432 dd_move_f(cr->dd, fr->f_twin, NULL);
1434 wallcycle_stop(wcycle, ewcMOVEF);
1439 /* wait for local forces (or calculate in emulation mode) */
1442 wallcycle_start(wcycle, ewcWAIT_GPU_NB_L);
1443 nbnxn_cuda_wait_gpu(nbv->cu_nbv,
1444 nbv->grp[eintLocal].nbat,
1446 enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1448 cycles_wait_gpu += wallcycle_stop(wcycle, ewcWAIT_GPU_NB_L);
1450 /* now clear the GPU outputs while we finish the step on the CPU */
1452 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1453 nbnxn_cuda_clear_outputs(nbv->cu_nbv, flags);
1454 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1458 wallcycle_start_nocount(wcycle, ewcFORCE);
1459 do_nb_verlet(fr, ic, enerd, flags, eintLocal,
1460 DOMAINDECOMP(cr) ? enbvClearFNo : enbvClearFYes,
1462 wallcycle_stop(wcycle, ewcFORCE);
1464 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1465 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1466 if (nbv->grp[eintLocal].nbl_lists.nbl[0]->nsci > 0)
1468 /* skip the reduction if there was no non-local work to do */
1469 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatLocal,
1470 nbv->grp[eintLocal].nbat, f);
1472 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1473 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1476 if (DOMAINDECOMP(cr))
1478 dd_force_flop_stop(cr->dd, nrnb);
1481 dd_cycles_add(cr->dd, cycles_force-cycles_pme, ddCyclF);
1484 dd_cycles_add(cr->dd, cycles_wait_gpu, ddCyclWaitGPU);
1491 if (IR_ELEC_FIELD(*inputrec))
1493 /* Compute forces due to electric field */
1494 calc_f_el(MASTER(cr) ? field : NULL,
1495 start, homenr, mdatoms->chargeA, fr->f_novirsum,
1496 inputrec->ex, inputrec->et, t);
1499 /* If we have NoVirSum forces, but we do not calculate the virial,
1500 * we sum fr->f_novirum=f later.
1502 if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1504 wallcycle_start(wcycle, ewcVSITESPREAD);
1505 spread_vsite_f(vsite, x, f, fr->fshift, FALSE, NULL, nrnb,
1506 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1507 wallcycle_stop(wcycle, ewcVSITESPREAD);
1511 wallcycle_start(wcycle, ewcVSITESPREAD);
1512 spread_vsite_f(vsite, x, fr->f_twin, NULL, FALSE, NULL,
1514 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1515 wallcycle_stop(wcycle, ewcVSITESPREAD);
1519 if (flags & GMX_FORCE_VIRIAL)
1521 /* Calculation of the virial must be done after vsites! */
1522 calc_virial(0, mdatoms->homenr, x, f,
1523 vir_force, graph, box, nrnb, fr, inputrec->ePBC);
1527 if (inputrec->ePull == epullUMBRELLA || inputrec->ePull == epullCONST_F)
1529 pull_potential_wrapper(fplog, bSepDVDL, cr, inputrec, box, x,
1530 f, vir_force, mdatoms, enerd, lambda, t);
1533 /* Add the forces from enforced rotation potentials (if any) */
1536 wallcycle_start(wcycle, ewcROTadd);
1537 enerd->term[F_COM_PULL] += add_rot_forces(inputrec->rot, f, cr, step, t);
1538 wallcycle_stop(wcycle, ewcROTadd);
1541 /* Add forces from interactive molecular dynamics (IMD), if bIMD == TRUE. */
1542 IMD_apply_forces(inputrec->bIMD, inputrec->imd, cr, f, wcycle);
1544 if (PAR(cr) && !(cr->duty & DUTY_PME))
1546 /* In case of node-splitting, the PP nodes receive the long-range
1547 * forces, virial and energy from the PME nodes here.
1549 pme_receive_force_ener(fplog, bSepDVDL, cr, wcycle, enerd, fr);
1554 post_process_forces(cr, step, nrnb, wcycle,
1555 top, box, x, f, vir_force, mdatoms, graph, fr, vsite,
1559 /* Sum the potential energy terms from group contributions */
1560 sum_epot(&(enerd->grpp), enerd->term);
1563 void do_force_cutsGROUP(FILE *fplog, t_commrec *cr,
1564 t_inputrec *inputrec,
1565 gmx_int64_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
1566 gmx_localtop_t *top,
1567 gmx_groups_t *groups,
1568 matrix box, rvec x[], history_t *hist,
1572 gmx_enerdata_t *enerd, t_fcdata *fcd,
1573 real *lambda, t_graph *graph,
1574 t_forcerec *fr, gmx_vsite_t *vsite, rvec mu_tot,
1575 double t, FILE *field, gmx_edsam_t ed,
1576 gmx_bool bBornRadii,
1582 gmx_bool bSepDVDL, bStateChanged, bNS, bFillGrid, bCalcCGCM, bBS;
1583 gmx_bool bDoLongRangeNS, bDoForces, bDoPotential, bSepLRF;
1584 gmx_bool bDoAdressWF;
1586 rvec vzero, box_diag;
1587 real e, v, dvdlambda[efptNR];
1589 float cycles_pme, cycles_force;
1592 homenr = mdatoms->homenr;
1594 bSepDVDL = (fr->bSepDVDL && do_per_step(step, inputrec->nstlog));
1596 clear_mat(vir_force);
1599 if (DOMAINDECOMP(cr))
1601 cg1 = cr->dd->ncg_tot;
1612 bStateChanged = (flags & GMX_FORCE_STATECHANGED);
1613 bNS = (flags & GMX_FORCE_NS) && (fr->bAllvsAll == FALSE);
1614 /* Should we update the long-range neighborlists at this step? */
1615 bDoLongRangeNS = fr->bTwinRange && bNS;
1616 /* Should we perform the long-range nonbonded evaluation inside the neighborsearching? */
1617 bFillGrid = (bNS && bStateChanged);
1618 bCalcCGCM = (bFillGrid && !DOMAINDECOMP(cr));
1619 bDoForces = (flags & GMX_FORCE_FORCES);
1620 bDoPotential = (flags & GMX_FORCE_ENERGY);
1621 bSepLRF = ((inputrec->nstcalclr > 1) && bDoForces &&
1622 (flags & GMX_FORCE_SEPLRF) && (flags & GMX_FORCE_DO_LR));
1624 /* should probably move this to the forcerec since it doesn't change */
1625 bDoAdressWF = ((fr->adress_type != eAdressOff));
1629 update_forcerec(fr, box);
1631 if (NEED_MUTOT(*inputrec))
1633 /* Calculate total (local) dipole moment in a temporary common array.
1634 * This makes it possible to sum them over nodes faster.
1636 calc_mu(start, homenr,
1637 x, mdatoms->chargeA, mdatoms->chargeB, mdatoms->nChargePerturbed,
1642 if (fr->ePBC != epbcNONE)
1644 /* Compute shift vectors every step,
1645 * because of pressure coupling or box deformation!
1647 if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
1649 calc_shifts(box, fr->shift_vec);
1654 put_charge_groups_in_box(fplog, cg0, cg1, fr->ePBC, box,
1655 &(top->cgs), x, fr->cg_cm);
1656 inc_nrnb(nrnb, eNR_CGCM, homenr);
1657 inc_nrnb(nrnb, eNR_RESETX, cg1-cg0);
1659 else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph)
1661 unshift_self(graph, box, x);
1666 calc_cgcm(fplog, cg0, cg1, &(top->cgs), x, fr->cg_cm);
1667 inc_nrnb(nrnb, eNR_CGCM, homenr);
1670 if (bCalcCGCM && gmx_debug_at)
1672 pr_rvecs(debug, 0, "cgcm", fr->cg_cm, top->cgs.nr);
1676 if (!(cr->duty & DUTY_PME))
1678 /* Send particle coordinates to the pme nodes.
1679 * Since this is only implemented for domain decomposition
1680 * and domain decomposition does not use the graph,
1681 * we do not need to worry about shifting.
1686 wallcycle_start(wcycle, ewcPP_PMESENDX);
1688 bBS = (inputrec->nwall == 2);
1691 copy_mat(box, boxs);
1692 svmul(inputrec->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
1695 if (EEL_PME(fr->eeltype))
1697 pme_flags |= GMX_PME_DO_COULOMB;
1700 if (EVDW_PME(fr->vdwtype))
1702 pme_flags |= GMX_PME_DO_LJ;
1705 gmx_pme_send_coordinates(cr, bBS ? boxs : box, x,
1706 mdatoms->nChargePerturbed, mdatoms->nTypePerturbed, lambda[efptCOUL], lambda[efptVDW],
1707 (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)),
1710 wallcycle_stop(wcycle, ewcPP_PMESENDX);
1712 #endif /* GMX_MPI */
1714 /* Communicate coordinates and sum dipole if necessary */
1715 if (DOMAINDECOMP(cr))
1717 wallcycle_start(wcycle, ewcMOVEX);
1718 dd_move_x(cr->dd, box, x);
1719 wallcycle_stop(wcycle, ewcMOVEX);
1722 /* update adress weight beforehand */
1723 if (bStateChanged && bDoAdressWF)
1725 /* need pbc for adress weight calculation with pbc_dx */
1726 set_pbc(&pbc, inputrec->ePBC, box);
1727 if (fr->adress_site == eAdressSITEcog)
1729 update_adress_weights_cog(top->idef.iparams, top->idef.il, x, fr, mdatoms,
1730 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1732 else if (fr->adress_site == eAdressSITEcom)
1734 update_adress_weights_com(fplog, cg0, cg1, &(top->cgs), x, fr, mdatoms,
1735 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1737 else if (fr->adress_site == eAdressSITEatomatom)
1739 update_adress_weights_atom_per_atom(cg0, cg1, &(top->cgs), x, fr, mdatoms,
1740 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1744 update_adress_weights_atom(cg0, cg1, &(top->cgs), x, fr, mdatoms,
1745 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1749 if (NEED_MUTOT(*inputrec))
1756 gmx_sumd(2*DIM, mu, cr);
1758 for (i = 0; i < 2; i++)
1760 for (j = 0; j < DIM; j++)
1762 fr->mu_tot[i][j] = mu[i*DIM + j];
1766 if (fr->efep == efepNO)
1768 copy_rvec(fr->mu_tot[0], mu_tot);
1772 for (j = 0; j < DIM; j++)
1775 (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] + lambda[efptCOUL]*fr->mu_tot[1][j];
1780 /* Reset energies */
1781 reset_enerdata(fr, bNS, enerd, MASTER(cr));
1782 clear_rvecs(SHIFTS, fr->fshift);
1786 wallcycle_start(wcycle, ewcNS);
1788 if (graph && bStateChanged)
1790 /* Calculate intramolecular shift vectors to make molecules whole */
1791 mk_mshift(fplog, graph, fr->ePBC, box, x);
1794 /* Do the actual neighbour searching */
1796 groups, top, mdatoms,
1797 cr, nrnb, bFillGrid,
1800 wallcycle_stop(wcycle, ewcNS);
1803 if (inputrec->implicit_solvent && bNS)
1805 make_gb_nblist(cr, inputrec->gb_algorithm,
1806 x, box, fr, &top->idef, graph, fr->born);
1809 if (DOMAINDECOMP(cr) && !(cr->duty & DUTY_PME))
1811 wallcycle_start(wcycle, ewcPPDURINGPME);
1812 dd_force_flop_start(cr->dd, nrnb);
1817 /* Enforced rotation has its own cycle counter that starts after the collective
1818 * coordinates have been communicated. It is added to ddCyclF to allow
1819 * for proper load-balancing */
1820 wallcycle_start(wcycle, ewcROT);
1821 do_rotation(cr, inputrec, box, x, t, step, wcycle, bNS);
1822 wallcycle_stop(wcycle, ewcROT);
1825 /* Start the force cycle counter.
1826 * This counter is stopped in do_forcelow_level.
1827 * No parallel communication should occur while this counter is running,
1828 * since that will interfere with the dynamic load balancing.
1830 wallcycle_start(wcycle, ewcFORCE);
1834 /* Reset forces for which the virial is calculated separately:
1835 * PME/Ewald forces if necessary */
1836 if (fr->bF_NoVirSum)
1838 if (flags & GMX_FORCE_VIRIAL)
1840 fr->f_novirsum = fr->f_novirsum_alloc;
1843 clear_rvecs(fr->f_novirsum_n, fr->f_novirsum);
1847 clear_rvecs(homenr, fr->f_novirsum+start);
1852 /* We are not calculating the pressure so we do not need
1853 * a separate array for forces that do not contribute
1860 /* Clear the short- and long-range forces */
1861 clear_rvecs(fr->natoms_force_constr, f);
1862 if (bSepLRF && do_per_step(step, inputrec->nstcalclr))
1864 clear_rvecs(fr->natoms_force_constr, fr->f_twin);
1867 clear_rvec(fr->vir_diag_posres);
1869 if (inputrec->ePull == epullCONSTRAINT)
1871 clear_pull_forces(inputrec->pull);
1874 /* update QMMMrec, if necessary */
1877 update_QMMMrec(cr, fr, x, mdatoms, box, top);
1880 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_POSRES].nr > 0)
1882 posres_wrapper(fplog, flags, bSepDVDL, inputrec, nrnb, top, box, x,
1886 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_FBPOSRES].nr > 0)
1888 fbposres_wrapper(inputrec, nrnb, top, box, x, enerd, fr);
1891 /* Compute the bonded and non-bonded energies and optionally forces */
1892 do_force_lowlevel(fplog, step, fr, inputrec, &(top->idef),
1893 cr, nrnb, wcycle, mdatoms,
1894 x, hist, f, bSepLRF ? fr->f_twin : f, enerd, fcd, top, fr->born,
1895 &(top->atomtypes), bBornRadii, box,
1896 inputrec->fepvals, lambda,
1897 graph, &(top->excls), fr->mu_tot,
1903 if (do_per_step(step, inputrec->nstcalclr))
1905 /* Add the long range forces to the short range forces */
1906 for (i = 0; i < fr->natoms_force_constr; i++)
1908 rvec_add(fr->f_twin[i], f[i], f[i]);
1913 cycles_force = wallcycle_stop(wcycle, ewcFORCE);
1917 do_flood(cr, inputrec, x, f, ed, box, step, bNS);
1920 if (DOMAINDECOMP(cr))
1922 dd_force_flop_stop(cr->dd, nrnb);
1925 dd_cycles_add(cr->dd, cycles_force-cycles_pme, ddCyclF);
1931 if (IR_ELEC_FIELD(*inputrec))
1933 /* Compute forces due to electric field */
1934 calc_f_el(MASTER(cr) ? field : NULL,
1935 start, homenr, mdatoms->chargeA, fr->f_novirsum,
1936 inputrec->ex, inputrec->et, t);
1939 if (bDoAdressWF && fr->adress_icor == eAdressICThermoForce)
1941 /* Compute thermodynamic force in hybrid AdResS region */
1942 adress_thermo_force(start, homenr, &(top->cgs), x, fr->f_novirsum, fr, mdatoms,
1943 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1946 /* Communicate the forces */
1947 if (DOMAINDECOMP(cr))
1949 wallcycle_start(wcycle, ewcMOVEF);
1950 dd_move_f(cr->dd, f, fr->fshift);
1951 /* Do we need to communicate the separate force array
1952 * for terms that do not contribute to the single sum virial?
1953 * Position restraints and electric fields do not introduce
1954 * inter-cg forces, only full electrostatics methods do.
1955 * When we do not calculate the virial, fr->f_novirsum = f,
1956 * so we have already communicated these forces.
1958 if (EEL_FULL(fr->eeltype) && cr->dd->n_intercg_excl &&
1959 (flags & GMX_FORCE_VIRIAL))
1961 dd_move_f(cr->dd, fr->f_novirsum, NULL);
1965 /* We should not update the shift forces here,
1966 * since f_twin is already included in f.
1968 dd_move_f(cr->dd, fr->f_twin, NULL);
1970 wallcycle_stop(wcycle, ewcMOVEF);
1973 /* If we have NoVirSum forces, but we do not calculate the virial,
1974 * we sum fr->f_novirum=f later.
1976 if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1978 wallcycle_start(wcycle, ewcVSITESPREAD);
1979 spread_vsite_f(vsite, x, f, fr->fshift, FALSE, NULL, nrnb,
1980 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1981 wallcycle_stop(wcycle, ewcVSITESPREAD);
1985 wallcycle_start(wcycle, ewcVSITESPREAD);
1986 spread_vsite_f(vsite, x, fr->f_twin, NULL, FALSE, NULL,
1988 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1989 wallcycle_stop(wcycle, ewcVSITESPREAD);
1993 if (flags & GMX_FORCE_VIRIAL)
1995 /* Calculation of the virial must be done after vsites! */
1996 calc_virial(0, mdatoms->homenr, x, f,
1997 vir_force, graph, box, nrnb, fr, inputrec->ePBC);
2001 if (inputrec->ePull == epullUMBRELLA || inputrec->ePull == epullCONST_F)
2003 pull_potential_wrapper(fplog, bSepDVDL, cr, inputrec, box, x,
2004 f, vir_force, mdatoms, enerd, lambda, t);
2007 /* Add the forces from enforced rotation potentials (if any) */
2010 wallcycle_start(wcycle, ewcROTadd);
2011 enerd->term[F_COM_PULL] += add_rot_forces(inputrec->rot, f, cr, step, t);
2012 wallcycle_stop(wcycle, ewcROTadd);
2015 /* Add forces from interactive molecular dynamics (IMD), if bIMD == TRUE. */
2016 IMD_apply_forces(inputrec->bIMD, inputrec->imd, cr, f, wcycle);
2018 if (PAR(cr) && !(cr->duty & DUTY_PME))
2020 /* In case of node-splitting, the PP nodes receive the long-range
2021 * forces, virial and energy from the PME nodes here.
2023 pme_receive_force_ener(fplog, bSepDVDL, cr, wcycle, enerd, fr);
2028 post_process_forces(cr, step, nrnb, wcycle,
2029 top, box, x, f, vir_force, mdatoms, graph, fr, vsite,
2033 /* Sum the potential energy terms from group contributions */
2034 sum_epot(&(enerd->grpp), enerd->term);
2037 void do_force(FILE *fplog, t_commrec *cr,
2038 t_inputrec *inputrec,
2039 gmx_int64_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
2040 gmx_localtop_t *top,
2041 gmx_groups_t *groups,
2042 matrix box, rvec x[], history_t *hist,
2046 gmx_enerdata_t *enerd, t_fcdata *fcd,
2047 real *lambda, t_graph *graph,
2049 gmx_vsite_t *vsite, rvec mu_tot,
2050 double t, FILE *field, gmx_edsam_t ed,
2051 gmx_bool bBornRadii,
2054 /* modify force flag if not doing nonbonded */
2055 if (!fr->bNonbonded)
2057 flags &= ~GMX_FORCE_NONBONDED;
2060 switch (inputrec->cutoff_scheme)
2063 do_force_cutsVERLET(fplog, cr, inputrec,
2079 do_force_cutsGROUP(fplog, cr, inputrec,
2094 gmx_incons("Invalid cut-off scheme passed!");
2099 void do_constrain_first(FILE *fplog, gmx_constr_t constr,
2100 t_inputrec *ir, t_mdatoms *md,
2101 t_state *state, t_commrec *cr, t_nrnb *nrnb,
2102 t_forcerec *fr, gmx_localtop_t *top)
2104 int i, m, start, end;
2106 real dt = ir->delta_t;
2110 snew(savex, state->natoms);
2117 fprintf(debug, "vcm: start=%d, homenr=%d, end=%d\n",
2118 start, md->homenr, end);
2120 /* Do a first constrain to reset particles... */
2121 step = ir->init_step;
2124 char buf[STEPSTRSIZE];
2125 fprintf(fplog, "\nConstraining the starting coordinates (step %s)\n",
2126 gmx_step_str(step, buf));
2130 /* constrain the current position */
2131 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
2132 ir, NULL, cr, step, 0, md,
2133 state->x, state->x, NULL,
2134 fr->bMolPBC, state->box,
2135 state->lambda[efptBONDED], &dvdl_dum,
2136 NULL, NULL, nrnb, econqCoord,
2137 ir->epc == epcMTTK, state->veta, state->veta);
2140 /* constrain the inital velocity, and save it */
2141 /* also may be useful if we need the ekin from the halfstep for velocity verlet */
2142 /* might not yet treat veta correctly */
2143 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
2144 ir, NULL, cr, step, 0, md,
2145 state->x, state->v, state->v,
2146 fr->bMolPBC, state->box,
2147 state->lambda[efptBONDED], &dvdl_dum,
2148 NULL, NULL, nrnb, econqVeloc,
2149 ir->epc == epcMTTK, state->veta, state->veta);
2151 /* constrain the inital velocities at t-dt/2 */
2152 if (EI_STATE_VELOCITY(ir->eI) && ir->eI != eiVV)
2154 for (i = start; (i < end); i++)
2156 for (m = 0; (m < DIM); m++)
2158 /* Reverse the velocity */
2159 state->v[i][m] = -state->v[i][m];
2160 /* Store the position at t-dt in buf */
2161 savex[i][m] = state->x[i][m] + dt*state->v[i][m];
2164 /* Shake the positions at t=-dt with the positions at t=0
2165 * as reference coordinates.
2169 char buf[STEPSTRSIZE];
2170 fprintf(fplog, "\nConstraining the coordinates at t0-dt (step %s)\n",
2171 gmx_step_str(step, buf));
2174 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
2175 ir, NULL, cr, step, -1, md,
2176 state->x, savex, NULL,
2177 fr->bMolPBC, state->box,
2178 state->lambda[efptBONDED], &dvdl_dum,
2179 state->v, NULL, nrnb, econqCoord,
2180 ir->epc == epcMTTK, state->veta, state->veta);
2182 for (i = start; i < end; i++)
2184 for (m = 0; m < DIM; m++)
2186 /* Re-reverse the velocities */
2187 state->v[i][m] = -state->v[i][m];
2196 integrate_table(real vdwtab[], real scale, int offstart, int rstart, int rend,
2197 double *enerout, double *virout)
2199 double enersum, virsum;
2200 double invscale, invscale2, invscale3;
2201 double r, ea, eb, ec, pa, pb, pc, pd;
2203 int ri, offset, tabfactor;
2205 invscale = 1.0/scale;
2206 invscale2 = invscale*invscale;
2207 invscale3 = invscale*invscale2;
2209 /* Following summation derived from cubic spline definition,
2210 * Numerical Recipies in C, second edition, p. 113-116. Exact for
2211 * the cubic spline. We first calculate the negative of the
2212 * energy from rvdw to rvdw_switch, assuming that g(r)=1, and then
2213 * add the more standard, abrupt cutoff correction to that result,
2214 * yielding the long-range correction for a switched function. We
2215 * perform both the pressure and energy loops at the same time for
2216 * simplicity, as the computational cost is low. */
2220 /* Since the dispersion table has been scaled down a factor
2221 * 6.0 and the repulsion a factor 12.0 to compensate for the
2222 * c6/c12 parameters inside nbfp[] being scaled up (to save
2223 * flops in kernels), we need to correct for this.
2234 for (ri = rstart; ri < rend; ++ri)
2238 eb = 2.0*invscale2*r;
2242 pb = 3.0*invscale2*r;
2243 pc = 3.0*invscale*r*r;
2246 /* this "8" is from the packing in the vdwtab array - perhaps
2247 should be defined? */
2249 offset = 8*ri + offstart;
2250 y0 = vdwtab[offset];
2251 f = vdwtab[offset+1];
2252 g = vdwtab[offset+2];
2253 h = vdwtab[offset+3];
2255 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);
2256 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);
2258 *enerout = 4.0*M_PI*enersum*tabfactor;
2259 *virout = 4.0*M_PI*virsum*tabfactor;
2262 void calc_enervirdiff(FILE *fplog, int eDispCorr, t_forcerec *fr)
2264 double eners[2], virs[2], enersum, virsum, y0, f, g, h;
2265 double r0, r1, r, rc3, rc9, ea, eb, ec, pa, pb, pc, pd;
2266 double invscale, invscale2, invscale3;
2267 int ri0, ri1, ri, i, offstart, offset;
2268 real scale, *vdwtab, tabfactor, tmp;
2270 fr->enershiftsix = 0;
2271 fr->enershifttwelve = 0;
2272 fr->enerdiffsix = 0;
2273 fr->enerdifftwelve = 0;
2275 fr->virdifftwelve = 0;
2277 if (eDispCorr != edispcNO)
2279 for (i = 0; i < 2; i++)
2284 if (fr->vdwtype == evdwSWITCH || fr->vdwtype == evdwSHIFT ||
2285 fr->vdw_modifier == eintmodPOTSWITCH ||
2286 fr->vdw_modifier == eintmodFORCESWITCH)
2288 if (fr->rvdw_switch == 0)
2291 "With dispersion correction rvdw-switch can not be zero "
2292 "for vdw-type = %s", evdw_names[fr->vdwtype]);
2295 scale = fr->nblists[0].table_elec_vdw.scale;
2296 vdwtab = fr->nblists[0].table_vdw.data;
2298 /* Round the cut-offs to exact table values for precision */
2299 ri0 = floor(fr->rvdw_switch*scale);
2300 ri1 = ceil(fr->rvdw*scale);
2306 if (fr->vdwtype == evdwSHIFT ||
2307 fr->vdw_modifier == eintmodFORCESWITCH)
2309 /* Determine the constant energy shift below rvdw_switch.
2310 * Table has a scale factor since we have scaled it down to compensate
2311 * for scaling-up c6/c12 with the derivative factors to save flops in analytical kernels.
2313 fr->enershiftsix = (real)(-1.0/(rc3*rc3)) - 6.0*vdwtab[8*ri0];
2314 fr->enershifttwelve = (real)( 1.0/(rc9*rc3)) - 12.0*vdwtab[8*ri0 + 4];
2316 /* Add the constant part from 0 to rvdw_switch.
2317 * This integration from 0 to rvdw_switch overcounts the number
2318 * of interactions by 1, as it also counts the self interaction.
2319 * We will correct for this later.
2321 eners[0] += 4.0*M_PI*fr->enershiftsix*rc3/3.0;
2322 eners[1] += 4.0*M_PI*fr->enershifttwelve*rc3/3.0;
2323 for (i = 0; i < 2; i++)
2327 integrate_table(vdwtab, scale, (i == 0 ? 0 : 4), ri0, ri1, &enersum, &virsum);
2328 eners[i] -= enersum;
2332 /* now add the correction for rvdw_switch to infinity */
2333 eners[0] += -4.0*M_PI/(3.0*rc3);
2334 eners[1] += 4.0*M_PI/(9.0*rc9);
2335 virs[0] += 8.0*M_PI/rc3;
2336 virs[1] += -16.0*M_PI/(3.0*rc9);
2338 else if (fr->vdwtype == evdwCUT ||
2339 EVDW_PME(fr->vdwtype) ||
2340 fr->vdwtype == evdwUSER)
2342 if (fr->vdwtype == evdwUSER && fplog)
2345 "WARNING: using dispersion correction with user tables\n");
2348 /* Note that with LJ-PME, the dispersion correction is multiplied
2349 * by the difference between the actual C6 and the value of C6
2350 * that would produce the combination rule.
2351 * This means the normal energy and virial difference formulas
2355 rc3 = fr->rvdw*fr->rvdw*fr->rvdw;
2357 /* Contribution beyond the cut-off */
2358 eners[0] += -4.0*M_PI/(3.0*rc3);
2359 eners[1] += 4.0*M_PI/(9.0*rc9);
2360 if (fr->vdw_modifier == eintmodPOTSHIFT)
2362 /* Contribution within the cut-off */
2363 eners[0] += -4.0*M_PI/(3.0*rc3);
2364 eners[1] += 4.0*M_PI/(3.0*rc9);
2366 /* Contribution beyond the cut-off */
2367 virs[0] += 8.0*M_PI/rc3;
2368 virs[1] += -16.0*M_PI/(3.0*rc9);
2373 "Dispersion correction is not implemented for vdw-type = %s",
2374 evdw_names[fr->vdwtype]);
2377 /* TODO: remove this code once we have group LJ-PME kernels
2378 * that calculate the exact, full LJ param C6/r^6 within the cut-off,
2379 * as the current nbnxn kernels do.
2381 if (fr->vdwtype == evdwPME && fr->cutoff_scheme == ecutsGROUP)
2383 /* Calculate self-interaction coefficient (assuming that
2384 * the reciprocal-space contribution is constant in the
2385 * region that contributes to the self-interaction).
2387 fr->enershiftsix = pow(fr->ewaldcoeff_lj, 6) / 6.0;
2389 eners[0] += -pow(sqrt(M_PI)*fr->ewaldcoeff_lj, 3)/3.0;
2390 virs[0] += pow(sqrt(M_PI)*fr->ewaldcoeff_lj, 3);
2393 fr->enerdiffsix = eners[0];
2394 fr->enerdifftwelve = eners[1];
2395 /* The 0.5 is due to the Gromacs definition of the virial */
2396 fr->virdiffsix = 0.5*virs[0];
2397 fr->virdifftwelve = 0.5*virs[1];
2401 void calc_dispcorr(FILE *fplog, t_inputrec *ir, t_forcerec *fr,
2402 gmx_int64_t step, int natoms,
2403 matrix box, real lambda, tensor pres, tensor virial,
2404 real *prescorr, real *enercorr, real *dvdlcorr)
2406 gmx_bool bCorrAll, bCorrPres;
2407 real dvdlambda, invvol, dens, ninter, avcsix, avctwelve, enerdiff, svir = 0, spres = 0;
2417 if (ir->eDispCorr != edispcNO)
2419 bCorrAll = (ir->eDispCorr == edispcAllEner ||
2420 ir->eDispCorr == edispcAllEnerPres);
2421 bCorrPres = (ir->eDispCorr == edispcEnerPres ||
2422 ir->eDispCorr == edispcAllEnerPres);
2424 invvol = 1/det(box);
2427 /* Only correct for the interactions with the inserted molecule */
2428 dens = (natoms - fr->n_tpi)*invvol;
2433 dens = natoms*invvol;
2434 ninter = 0.5*natoms;
2437 if (ir->efep == efepNO)
2439 avcsix = fr->avcsix[0];
2440 avctwelve = fr->avctwelve[0];
2444 avcsix = (1 - lambda)*fr->avcsix[0] + lambda*fr->avcsix[1];
2445 avctwelve = (1 - lambda)*fr->avctwelve[0] + lambda*fr->avctwelve[1];
2448 enerdiff = ninter*(dens*fr->enerdiffsix - fr->enershiftsix);
2449 *enercorr += avcsix*enerdiff;
2451 if (ir->efep != efepNO)
2453 dvdlambda += (fr->avcsix[1] - fr->avcsix[0])*enerdiff;
2457 enerdiff = ninter*(dens*fr->enerdifftwelve - fr->enershifttwelve);
2458 *enercorr += avctwelve*enerdiff;
2459 if (fr->efep != efepNO)
2461 dvdlambda += (fr->avctwelve[1] - fr->avctwelve[0])*enerdiff;
2467 svir = ninter*dens*avcsix*fr->virdiffsix/3.0;
2468 if (ir->eDispCorr == edispcAllEnerPres)
2470 svir += ninter*dens*avctwelve*fr->virdifftwelve/3.0;
2472 /* The factor 2 is because of the Gromacs virial definition */
2473 spres = -2.0*invvol*svir*PRESFAC;
2475 for (m = 0; m < DIM; m++)
2477 virial[m][m] += svir;
2478 pres[m][m] += spres;
2483 /* Can't currently control when it prints, for now, just print when degugging */
2488 fprintf(debug, "Long Range LJ corr.: <C6> %10.4e, <C12> %10.4e\n",
2494 "Long Range LJ corr.: Epot %10g, Pres: %10g, Vir: %10g\n",
2495 *enercorr, spres, svir);
2499 fprintf(debug, "Long Range LJ corr.: Epot %10g\n", *enercorr);
2503 if (fr->bSepDVDL && do_per_step(step, ir->nstlog))
2505 gmx_print_sepdvdl(fplog, "Dispersion correction", *enercorr, dvdlambda);
2507 if (fr->efep != efepNO)
2509 *dvdlcorr += dvdlambda;
2514 void do_pbc_first(FILE *fplog, matrix box, t_forcerec *fr,
2515 t_graph *graph, rvec x[])
2519 fprintf(fplog, "Removing pbc first time\n");
2521 calc_shifts(box, fr->shift_vec);
2524 mk_mshift(fplog, graph, fr->ePBC, box, x);
2527 p_graph(debug, "do_pbc_first 1", graph);
2529 shift_self(graph, box, x);
2530 /* By doing an extra mk_mshift the molecules that are broken
2531 * because they were e.g. imported from another software
2532 * will be made whole again. Such are the healing powers
2535 mk_mshift(fplog, graph, fr->ePBC, box, x);
2538 p_graph(debug, "do_pbc_first 2", graph);
2543 fprintf(fplog, "Done rmpbc\n");
2547 static void low_do_pbc_mtop(FILE *fplog, int ePBC, matrix box,
2548 gmx_mtop_t *mtop, rvec x[],
2553 gmx_molblock_t *molb;
2555 if (bFirst && fplog)
2557 fprintf(fplog, "Removing pbc first time\n");
2562 for (mb = 0; mb < mtop->nmolblock; mb++)
2564 molb = &mtop->molblock[mb];
2565 if (molb->natoms_mol == 1 ||
2566 (!bFirst && mtop->moltype[molb->type].cgs.nr == 1))
2568 /* Just one atom or charge group in the molecule, no PBC required */
2569 as += molb->nmol*molb->natoms_mol;
2573 /* Pass NULL iso fplog to avoid graph prints for each molecule type */
2574 mk_graph_ilist(NULL, mtop->moltype[molb->type].ilist,
2575 0, molb->natoms_mol, FALSE, FALSE, graph);
2577 for (mol = 0; mol < molb->nmol; mol++)
2579 mk_mshift(fplog, graph, ePBC, box, x+as);
2581 shift_self(graph, box, x+as);
2582 /* The molecule is whole now.
2583 * We don't need the second mk_mshift call as in do_pbc_first,
2584 * since we no longer need this graph.
2587 as += molb->natoms_mol;
2595 void do_pbc_first_mtop(FILE *fplog, int ePBC, matrix box,
2596 gmx_mtop_t *mtop, rvec x[])
2598 low_do_pbc_mtop(fplog, ePBC, box, mtop, x, TRUE);
2601 void do_pbc_mtop(FILE *fplog, int ePBC, matrix box,
2602 gmx_mtop_t *mtop, rvec x[])
2604 low_do_pbc_mtop(fplog, ePBC, box, mtop, x, FALSE);
2607 void finish_run(FILE *fplog, t_commrec *cr,
2608 t_inputrec *inputrec,
2609 t_nrnb nrnb[], gmx_wallcycle_t wcycle,
2610 gmx_walltime_accounting_t walltime_accounting,
2611 wallclock_gpu_t *gputimes,
2612 gmx_bool bWriteStat)
2615 t_nrnb *nrnb_tot = NULL;
2618 double elapsed_time,
2619 elapsed_time_over_all_ranks,
2620 elapsed_time_over_all_threads,
2621 elapsed_time_over_all_threads_over_all_ranks;
2622 wallcycle_sum(cr, wcycle);
2628 MPI_Allreduce(nrnb->n, nrnb_tot->n, eNRNB, MPI_DOUBLE, MPI_SUM,
2629 cr->mpi_comm_mysim);
2637 elapsed_time = walltime_accounting_get_elapsed_time(walltime_accounting);
2638 elapsed_time_over_all_ranks = elapsed_time;
2639 elapsed_time_over_all_threads = walltime_accounting_get_elapsed_time_over_all_threads(walltime_accounting);
2640 elapsed_time_over_all_threads_over_all_ranks = elapsed_time_over_all_threads;
2644 /* reduce elapsed_time over all MPI ranks in the current simulation */
2645 MPI_Allreduce(&elapsed_time,
2646 &elapsed_time_over_all_ranks,
2647 1, MPI_DOUBLE, MPI_SUM,
2648 cr->mpi_comm_mysim);
2649 elapsed_time_over_all_ranks /= cr->nnodes;
2650 /* Reduce elapsed_time_over_all_threads over all MPI ranks in the
2651 * current simulation. */
2652 MPI_Allreduce(&elapsed_time_over_all_threads,
2653 &elapsed_time_over_all_threads_over_all_ranks,
2654 1, MPI_DOUBLE, MPI_SUM,
2655 cr->mpi_comm_mysim);
2661 print_flop(fplog, nrnb_tot, &nbfs, &mflop);
2668 if ((cr->duty & DUTY_PP) && DOMAINDECOMP(cr))
2670 print_dd_statistics(cr, inputrec, fplog);
2675 wallcycle_print(fplog, cr->nnodes, cr->npmenodes,
2676 elapsed_time_over_all_ranks,
2679 if (EI_DYNAMICS(inputrec->eI))
2681 delta_t = inputrec->delta_t;
2690 print_perf(fplog, elapsed_time_over_all_threads_over_all_ranks,
2691 elapsed_time_over_all_ranks,
2692 walltime_accounting_get_nsteps_done(walltime_accounting),
2693 delta_t, nbfs, mflop);
2697 print_perf(stderr, elapsed_time_over_all_threads_over_all_ranks,
2698 elapsed_time_over_all_ranks,
2699 walltime_accounting_get_nsteps_done(walltime_accounting),
2700 delta_t, nbfs, mflop);
2705 extern void initialize_lambdas(FILE *fplog, t_inputrec *ir, int *fep_state, real *lambda, double *lam0)
2707 /* this function works, but could probably use a logic rewrite to keep all the different
2708 types of efep straight. */
2711 t_lambda *fep = ir->fepvals;
2713 if ((ir->efep == efepNO) && (ir->bSimTemp == FALSE))
2715 for (i = 0; i < efptNR; i++)
2727 *fep_state = fep->init_fep_state; /* this might overwrite the checkpoint
2728 if checkpoint is set -- a kludge is in for now
2730 for (i = 0; i < efptNR; i++)
2732 /* overwrite lambda state with init_lambda for now for backwards compatibility */
2733 if (fep->init_lambda >= 0) /* if it's -1, it was never initializd */
2735 lambda[i] = fep->init_lambda;
2738 lam0[i] = lambda[i];
2743 lambda[i] = fep->all_lambda[i][*fep_state];
2746 lam0[i] = lambda[i];
2752 /* need to rescale control temperatures to match current state */
2753 for (i = 0; i < ir->opts.ngtc; i++)
2755 if (ir->opts.ref_t[i] > 0)
2757 ir->opts.ref_t[i] = ir->simtempvals->temperatures[*fep_state];
2763 /* Send to the log the information on the current lambdas */
2766 fprintf(fplog, "Initial vector of lambda components:[ ");
2767 for (i = 0; i < efptNR; i++)
2769 fprintf(fplog, "%10.4f ", lambda[i]);
2771 fprintf(fplog, "]\n");
2777 void init_md(FILE *fplog,
2778 t_commrec *cr, t_inputrec *ir, const output_env_t oenv,
2779 double *t, double *t0,
2780 real *lambda, int *fep_state, double *lam0,
2781 t_nrnb *nrnb, gmx_mtop_t *mtop,
2783 int nfile, const t_filenm fnm[],
2784 gmx_mdoutf_t *outf, t_mdebin **mdebin,
2785 tensor force_vir, tensor shake_vir, rvec mu_tot,
2786 gmx_bool *bSimAnn, t_vcm **vcm, unsigned long Flags)
2791 /* Initial values */
2792 *t = *t0 = ir->init_t;
2795 for (i = 0; i < ir->opts.ngtc; i++)
2797 /* set bSimAnn if any group is being annealed */
2798 if (ir->opts.annealing[i] != eannNO)
2805 update_annealing_target_temp(&(ir->opts), ir->init_t);
2808 /* Initialize lambda variables */
2809 initialize_lambdas(fplog, ir, fep_state, lambda, lam0);
2813 *upd = init_update(ir);
2819 *vcm = init_vcm(fplog, &mtop->groups, ir);
2822 if (EI_DYNAMICS(ir->eI) && !(Flags & MD_APPENDFILES))
2824 if (ir->etc == etcBERENDSEN)
2826 please_cite(fplog, "Berendsen84a");
2828 if (ir->etc == etcVRESCALE)
2830 please_cite(fplog, "Bussi2007a");
2838 *outf = init_mdoutf(fplog, nfile, fnm, Flags, cr, ir, mtop, oenv);
2840 *mdebin = init_mdebin((Flags & MD_APPENDFILES) ? NULL : mdoutf_get_fp_ene(*outf),
2841 mtop, ir, mdoutf_get_fp_dhdl(*outf));
2846 please_cite(fplog, "Fritsch12");
2847 please_cite(fplog, "Junghans10");
2849 /* Initiate variables */
2850 clear_mat(force_vir);
2851 clear_mat(shake_vir);