1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROwing Monsters And Cloning Shrimps
41 #ifdef HAVE_SYS_TIME_H
52 #include "chargegroup.h"
72 #include "pull_rotation.h"
73 #include "gmx_random.h"
77 #include "nbnxn_atomdata.h"
78 #include "nbnxn_search.h"
79 #include "nbnxn_kernels/nbnxn_kernel_ref.h"
80 #include "nbnxn_kernels/simd_4xn/nbnxn_kernel_simd_4xn.h"
81 #include "nbnxn_kernels/simd_2xnn/nbnxn_kernel_simd_2xnn.h"
82 #include "nbnxn_kernels/nbnxn_kernel_gpu_ref.h"
84 #include "gromacs/timing/wallcycle.h"
85 #include "gromacs/timing/walltime_accounting.h"
86 #include "gromacs/utility/gmxmpi.h"
91 #include "nbnxn_cuda_data_mgmt.h"
92 #include "nbnxn_cuda/nbnxn_cuda.h"
94 void print_time(FILE *out,
95 gmx_walltime_accounting_t walltime_accounting,
98 t_commrec gmx_unused *cr)
101 char timebuf[STRLEN];
102 double dt, elapsed_seconds, time_per_step;
105 #ifndef GMX_THREAD_MPI
111 fprintf(out, "step %s", gmx_step_str(step, buf));
112 if ((step >= ir->nstlist))
114 double seconds_since_epoch = gmx_gettime();
115 elapsed_seconds = seconds_since_epoch - walltime_accounting_get_start_time_stamp(walltime_accounting);
116 time_per_step = elapsed_seconds/(step - ir->init_step + 1);
117 dt = (ir->nsteps + ir->init_step - step) * time_per_step;
123 finish = (time_t) (seconds_since_epoch + dt);
124 gmx_ctime_r(&finish, timebuf, STRLEN);
125 sprintf(buf, "%s", timebuf);
126 buf[strlen(buf)-1] = '\0';
127 fprintf(out, ", will finish %s", buf);
131 fprintf(out, ", remaining wall clock time: %5d s ", (int)dt);
136 fprintf(out, " performance: %.1f ns/day ",
137 ir->delta_t/1000*24*60*60/time_per_step);
140 #ifndef GMX_THREAD_MPI
150 void print_date_and_time(FILE *fplog, int nodeid, const char *title,
151 const gmx_walltime_accounting_t walltime_accounting)
154 char timebuf[STRLEN];
155 char time_string[STRLEN];
160 if (walltime_accounting != NULL)
162 tmptime = (time_t) walltime_accounting_get_start_time_stamp(walltime_accounting);
163 gmx_ctime_r(&tmptime, timebuf, STRLEN);
167 tmptime = (time_t) gmx_gettime();
168 gmx_ctime_r(&tmptime, timebuf, STRLEN);
170 for (i = 0; timebuf[i] >= ' '; i++)
172 time_string[i] = timebuf[i];
174 time_string[i] = '\0';
176 fprintf(fplog, "%s on node %d %s\n", title, nodeid, time_string);
180 static void sum_forces(int start, int end, rvec f[], rvec flr[])
186 pr_rvecs(debug, 0, "fsr", f+start, end-start);
187 pr_rvecs(debug, 0, "flr", flr+start, end-start);
189 for (i = start; (i < end); i++)
191 rvec_inc(f[i], flr[i]);
196 * calc_f_el calculates forces due to an electric field.
198 * force is kJ mol^-1 nm^-1 = e * kJ mol^-1 nm^-1 / e
200 * Et[] contains the parameters for the time dependent
201 * part of the field (not yet used).
202 * Ex[] contains the parameters for
203 * the spatial dependent part of the field. You can have cool periodic
204 * fields in principle, but only a constant field is supported
206 * The function should return the energy due to the electric field
207 * (if any) but for now returns 0.
210 * There can be problems with the virial.
211 * Since the field is not self-consistent this is unavoidable.
212 * For neutral molecules the virial is correct within this approximation.
213 * For neutral systems with many charged molecules the error is small.
214 * But for systems with a net charge or a few charged molecules
215 * the error can be significant when the field is high.
216 * Solution: implement a self-consitent electric field into PME.
218 static void calc_f_el(FILE *fp, int start, int homenr,
219 real charge[], rvec f[],
220 t_cosines Ex[], t_cosines Et[], double t)
226 for (m = 0; (m < DIM); m++)
233 Ext[m] = cos(Et[m].a[0]*(t-t0))*exp(-sqr(t-t0)/(2.0*sqr(Et[m].a[2])));
237 Ext[m] = cos(Et[m].a[0]*t);
246 /* Convert the field strength from V/nm to MD-units */
247 Ext[m] *= Ex[m].a[0]*FIELDFAC;
248 for (i = start; (i < start+homenr); i++)
250 f[i][m] += charge[i]*Ext[m];
260 fprintf(fp, "%10g %10g %10g %10g #FIELD\n", t,
261 Ext[XX]/FIELDFAC, Ext[YY]/FIELDFAC, Ext[ZZ]/FIELDFAC);
265 static void calc_virial(int start, int homenr, rvec x[], rvec f[],
266 tensor vir_part, t_graph *graph, matrix box,
267 t_nrnb *nrnb, const t_forcerec *fr, int ePBC)
272 /* The short-range virial from surrounding boxes */
274 calc_vir(SHIFTS, fr->shift_vec, fr->fshift, vir_part, ePBC == epbcSCREW, box);
275 inc_nrnb(nrnb, eNR_VIRIAL, SHIFTS);
277 /* Calculate partial virial, for local atoms only, based on short range.
278 * Total virial is computed in global_stat, called from do_md
280 f_calc_vir(start, start+homenr, x, f, vir_part, graph, box);
281 inc_nrnb(nrnb, eNR_VIRIAL, homenr);
283 /* Add position restraint contribution */
284 for (i = 0; i < DIM; i++)
286 vir_part[i][i] += fr->vir_diag_posres[i];
289 /* Add wall contribution */
290 for (i = 0; i < DIM; i++)
292 vir_part[i][ZZ] += fr->vir_wall_z[i];
297 pr_rvecs(debug, 0, "vir_part", vir_part, DIM);
301 static void posres_wrapper(FILE *fplog,
307 matrix box, rvec x[],
308 gmx_enerdata_t *enerd,
316 /* Position restraints always require full pbc */
317 set_pbc(&pbc, ir->ePBC, box);
319 v = posres(top->idef.il[F_POSRES].nr, top->idef.il[F_POSRES].iatoms,
320 top->idef.iparams_posres,
321 (const rvec*)x, fr->f_novirsum, fr->vir_diag_posres,
322 ir->ePBC == epbcNONE ? NULL : &pbc,
323 lambda[efptRESTRAINT], &dvdl,
324 fr->rc_scaling, fr->ePBC, fr->posres_com, fr->posres_comB);
327 gmx_print_sepdvdl(fplog, interaction_function[F_POSRES].longname, v, dvdl);
329 enerd->term[F_POSRES] += v;
330 /* If just the force constant changes, the FEP term is linear,
331 * but if k changes, it is not.
333 enerd->dvdl_nonlin[efptRESTRAINT] += dvdl;
334 inc_nrnb(nrnb, eNR_POSRES, top->idef.il[F_POSRES].nr/2);
336 if ((ir->fepvals->n_lambda > 0) && (flags & GMX_FORCE_DHDL))
338 for (i = 0; i < enerd->n_lambda; i++)
340 real dvdl_dum, lambda_dum;
342 lambda_dum = (i == 0 ? lambda[efptRESTRAINT] : ir->fepvals->all_lambda[efptRESTRAINT][i-1]);
343 v = posres(top->idef.il[F_POSRES].nr, top->idef.il[F_POSRES].iatoms,
344 top->idef.iparams_posres,
345 (const rvec*)x, NULL, NULL,
346 ir->ePBC == epbcNONE ? NULL : &pbc, lambda_dum, &dvdl,
347 fr->rc_scaling, fr->ePBC, fr->posres_com, fr->posres_comB);
348 enerd->enerpart_lambda[i] += v;
353 static void pull_potential_wrapper(FILE *fplog,
357 matrix box, rvec x[],
361 gmx_enerdata_t *enerd,
368 /* Calculate the center of mass forces, this requires communication,
369 * which is why pull_potential is called close to other communication.
370 * The virial contribution is calculated directly,
371 * which is why we call pull_potential after calc_virial.
373 set_pbc(&pbc, ir->ePBC, box);
375 enerd->term[F_COM_PULL] +=
376 pull_potential(ir->ePull, ir->pull, mdatoms, &pbc,
377 cr, t, lambda[efptRESTRAINT], x, f, vir_force, &dvdl);
380 gmx_print_sepdvdl(fplog, "Com pull", enerd->term[F_COM_PULL], dvdl);
382 enerd->dvdl_lin[efptRESTRAINT] += dvdl;
385 static void pme_receive_force_ener(FILE *fplog,
388 gmx_wallcycle_t wcycle,
389 gmx_enerdata_t *enerd,
393 float cycles_ppdpme, cycles_seppme;
395 cycles_ppdpme = wallcycle_stop(wcycle, ewcPPDURINGPME);
396 dd_cycles_add(cr->dd, cycles_ppdpme, ddCyclPPduringPME);
398 /* In case of node-splitting, the PP nodes receive the long-range
399 * forces, virial and energy from the PME nodes here.
401 wallcycle_start(wcycle, ewcPP_PMEWAITRECVF);
403 gmx_pme_receive_f(cr, fr->f_novirsum, fr->vir_el_recip, &e, &dvdl,
407 gmx_print_sepdvdl(fplog, "PME mesh", e, dvdl);
409 enerd->term[F_COUL_RECIP] += e;
410 enerd->dvdl_lin[efptCOUL] += dvdl;
413 dd_cycles_add(cr->dd, cycles_seppme, ddCyclPME);
415 wallcycle_stop(wcycle, ewcPP_PMEWAITRECVF);
418 static void print_large_forces(FILE *fp, t_mdatoms *md, t_commrec *cr,
419 gmx_large_int_t step, real pforce, rvec *x, rvec *f)
423 char buf[STEPSTRSIZE];
426 for (i = md->start; i < md->start+md->homenr; i++)
429 /* We also catch NAN, if the compiler does not optimize this away. */
430 if (fn2 >= pf2 || fn2 != fn2)
432 fprintf(fp, "step %s atom %6d x %8.3f %8.3f %8.3f force %12.5e\n",
433 gmx_step_str(step, buf),
434 ddglatnr(cr->dd, i), x[i][XX], x[i][YY], x[i][ZZ], sqrt(fn2));
439 static void post_process_forces(t_commrec *cr,
440 gmx_large_int_t step,
441 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
443 matrix box, rvec x[],
448 t_forcerec *fr, gmx_vsite_t *vsite,
455 /* Spread the mesh force on virtual sites to the other particles...
456 * This is parallellized. MPI communication is performed
457 * if the constructing atoms aren't local.
459 wallcycle_start(wcycle, ewcVSITESPREAD);
460 spread_vsite_f(vsite, x, fr->f_novirsum, NULL,
461 (flags & GMX_FORCE_VIRIAL), fr->vir_el_recip,
463 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
464 wallcycle_stop(wcycle, ewcVSITESPREAD);
466 if (flags & GMX_FORCE_VIRIAL)
468 /* Now add the forces, this is local */
471 sum_forces(0, fr->f_novirsum_n, f, fr->f_novirsum);
475 sum_forces(mdatoms->start, mdatoms->start+mdatoms->homenr,
478 if (EEL_FULL(fr->eeltype))
480 /* Add the mesh contribution to the virial */
481 m_add(vir_force, fr->vir_el_recip, vir_force);
485 pr_rvecs(debug, 0, "vir_force", vir_force, DIM);
490 if (fr->print_force >= 0)
492 print_large_forces(stderr, mdatoms, cr, step, fr->print_force, x, f);
496 static void do_nb_verlet(t_forcerec *fr,
497 interaction_const_t *ic,
498 gmx_enerdata_t *enerd,
499 int flags, int ilocality,
502 gmx_wallcycle_t wcycle)
504 int nnbl, kernel_type, enr_nbnxn_kernel_ljc, enr_nbnxn_kernel_lj;
506 nonbonded_verlet_group_t *nbvg;
509 if (!(flags & GMX_FORCE_NONBONDED))
511 /* skip non-bonded calculation */
515 nbvg = &fr->nbv->grp[ilocality];
517 /* CUDA kernel launch overhead is already timed separately */
518 if (fr->cutoff_scheme != ecutsVERLET)
520 gmx_incons("Invalid cut-off scheme passed!");
523 bCUDA = (nbvg->kernel_type == nbnxnk8x8x8_CUDA);
527 wallcycle_sub_start(wcycle, ewcsNONBONDED);
529 switch (nbvg->kernel_type)
531 case nbnxnk4x4_PlainC:
532 nbnxn_kernel_ref(&nbvg->nbl_lists,
538 enerd->grpp.ener[egCOULSR],
540 enerd->grpp.ener[egBHAMSR] :
541 enerd->grpp.ener[egLJSR]);
544 case nbnxnk4xN_SIMD_4xN:
545 nbnxn_kernel_simd_4xn(&nbvg->nbl_lists,
552 enerd->grpp.ener[egCOULSR],
554 enerd->grpp.ener[egBHAMSR] :
555 enerd->grpp.ener[egLJSR]);
557 case nbnxnk4xN_SIMD_2xNN:
558 nbnxn_kernel_simd_2xnn(&nbvg->nbl_lists,
565 enerd->grpp.ener[egCOULSR],
567 enerd->grpp.ener[egBHAMSR] :
568 enerd->grpp.ener[egLJSR]);
571 case nbnxnk8x8x8_CUDA:
572 nbnxn_cuda_launch_kernel(fr->nbv->cu_nbv, nbvg->nbat, flags, ilocality);
575 case nbnxnk8x8x8_PlainC:
576 nbnxn_kernel_gpu_ref(nbvg->nbl_lists.nbl[0],
581 nbvg->nbat->out[0].f,
583 enerd->grpp.ener[egCOULSR],
585 enerd->grpp.ener[egBHAMSR] :
586 enerd->grpp.ener[egLJSR]);
590 gmx_incons("Invalid nonbonded kernel type passed!");
595 wallcycle_sub_stop(wcycle, ewcsNONBONDED);
598 if (EEL_RF(ic->eeltype) || ic->eeltype == eelCUT)
600 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_RF;
602 else if ((!bCUDA && nbvg->ewald_excl == ewaldexclAnalytical) ||
603 (bCUDA && nbnxn_cuda_is_kernel_ewald_analytical(fr->nbv->cu_nbv)))
605 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_EWALD;
609 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_TAB;
611 enr_nbnxn_kernel_lj = eNR_NBNXN_LJ;
612 if (flags & GMX_FORCE_ENERGY)
614 /* In eNR_??? the nbnxn F+E kernels are always the F kernel + 1 */
615 enr_nbnxn_kernel_ljc += 1;
616 enr_nbnxn_kernel_lj += 1;
619 inc_nrnb(nrnb, enr_nbnxn_kernel_ljc,
620 nbvg->nbl_lists.natpair_ljq);
621 inc_nrnb(nrnb, enr_nbnxn_kernel_lj,
622 nbvg->nbl_lists.natpair_lj);
623 inc_nrnb(nrnb, enr_nbnxn_kernel_ljc-eNR_NBNXN_LJ_RF+eNR_NBNXN_RF,
624 nbvg->nbl_lists.natpair_q);
627 void do_force_cutsVERLET(FILE *fplog, t_commrec *cr,
628 t_inputrec *inputrec,
629 gmx_large_int_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
631 gmx_groups_t gmx_unused *groups,
632 matrix box, rvec x[], history_t *hist,
636 gmx_enerdata_t *enerd, t_fcdata *fcd,
637 real *lambda, t_graph *graph,
638 t_forcerec *fr, interaction_const_t *ic,
639 gmx_vsite_t *vsite, rvec mu_tot,
640 double t, FILE *field, gmx_edsam_t ed,
648 gmx_bool bSepDVDL, bStateChanged, bNS, bFillGrid, bCalcCGCM, bBS;
649 gmx_bool bDoLongRange, bDoForces, bSepLRF, bUseGPU, bUseOrEmulGPU;
650 gmx_bool bDiffKernels = FALSE;
652 rvec vzero, box_diag;
654 float cycles_pme, cycles_force;
655 nonbonded_verlet_t *nbv;
659 nb_kernel_type = fr->nbv->grp[0].kernel_type;
661 start = mdatoms->start;
662 homenr = mdatoms->homenr;
664 bSepDVDL = (fr->bSepDVDL && do_per_step(step, inputrec->nstlog));
666 clear_mat(vir_force);
669 if (DOMAINDECOMP(cr))
671 cg1 = cr->dd->ncg_tot;
682 bStateChanged = (flags & GMX_FORCE_STATECHANGED);
683 bNS = (flags & GMX_FORCE_NS) && (fr->bAllvsAll == FALSE);
684 bFillGrid = (bNS && bStateChanged);
685 bCalcCGCM = (bFillGrid && !DOMAINDECOMP(cr));
686 bDoLongRange = (fr->bTwinRange && bNS && (flags & GMX_FORCE_DO_LR));
687 bDoForces = (flags & GMX_FORCE_FORCES);
688 bSepLRF = (bDoLongRange && bDoForces && (flags & GMX_FORCE_SEPLRF));
689 bUseGPU = fr->nbv->bUseGPU;
690 bUseOrEmulGPU = bUseGPU || (nbv->grp[0].kernel_type == nbnxnk8x8x8_PlainC);
694 update_forcerec(fr, box);
696 if (NEED_MUTOT(*inputrec))
698 /* Calculate total (local) dipole moment in a temporary common array.
699 * This makes it possible to sum them over nodes faster.
701 calc_mu(start, homenr,
702 x, mdatoms->chargeA, mdatoms->chargeB, mdatoms->nChargePerturbed,
707 if (fr->ePBC != epbcNONE)
709 /* Compute shift vectors every step,
710 * because of pressure coupling or box deformation!
712 if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
714 calc_shifts(box, fr->shift_vec);
719 put_atoms_in_box_omp(fr->ePBC, box, homenr, x);
720 inc_nrnb(nrnb, eNR_SHIFTX, homenr);
722 else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph)
724 unshift_self(graph, box, x);
728 nbnxn_atomdata_copy_shiftvec(flags & GMX_FORCE_DYNAMICBOX,
729 fr->shift_vec, nbv->grp[0].nbat);
732 if (!(cr->duty & DUTY_PME))
734 /* Send particle coordinates to the pme nodes.
735 * Since this is only implemented for domain decomposition
736 * and domain decomposition does not use the graph,
737 * we do not need to worry about shifting.
740 wallcycle_start(wcycle, ewcPP_PMESENDX);
742 bBS = (inputrec->nwall == 2);
746 svmul(inputrec->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
749 gmx_pme_send_x(cr, bBS ? boxs : box, x,
750 mdatoms->nChargePerturbed, lambda[efptCOUL],
751 (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)), step);
753 wallcycle_stop(wcycle, ewcPP_PMESENDX);
757 /* do gridding for pair search */
760 if (graph && bStateChanged)
762 /* Calculate intramolecular shift vectors to make molecules whole */
763 mk_mshift(fplog, graph, fr->ePBC, box, x);
767 box_diag[XX] = box[XX][XX];
768 box_diag[YY] = box[YY][YY];
769 box_diag[ZZ] = box[ZZ][ZZ];
771 wallcycle_start(wcycle, ewcNS);
774 wallcycle_sub_start(wcycle, ewcsNBS_GRID_LOCAL);
775 nbnxn_put_on_grid(nbv->nbs, fr->ePBC, box,
777 0, mdatoms->homenr, -1, fr->cginfo, x,
779 nbv->grp[eintLocal].kernel_type,
780 nbv->grp[eintLocal].nbat);
781 wallcycle_sub_stop(wcycle, ewcsNBS_GRID_LOCAL);
785 wallcycle_sub_start(wcycle, ewcsNBS_GRID_NONLOCAL);
786 nbnxn_put_on_grid_nonlocal(nbv->nbs, domdec_zones(cr->dd),
788 nbv->grp[eintNonlocal].kernel_type,
789 nbv->grp[eintNonlocal].nbat);
790 wallcycle_sub_stop(wcycle, ewcsNBS_GRID_NONLOCAL);
793 if (nbv->ngrp == 1 ||
794 nbv->grp[eintNonlocal].nbat == nbv->grp[eintLocal].nbat)
796 nbnxn_atomdata_set(nbv->grp[eintLocal].nbat, eatAll,
797 nbv->nbs, mdatoms, fr->cginfo);
801 nbnxn_atomdata_set(nbv->grp[eintLocal].nbat, eatLocal,
802 nbv->nbs, mdatoms, fr->cginfo);
803 nbnxn_atomdata_set(nbv->grp[eintNonlocal].nbat, eatAll,
804 nbv->nbs, mdatoms, fr->cginfo);
806 wallcycle_stop(wcycle, ewcNS);
809 /* initialize the GPU atom data and copy shift vector */
814 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
815 nbnxn_cuda_init_atomdata(nbv->cu_nbv, nbv->grp[eintLocal].nbat);
816 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
819 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
820 nbnxn_cuda_upload_shiftvec(nbv->cu_nbv, nbv->grp[eintLocal].nbat);
821 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
824 /* do local pair search */
827 wallcycle_start_nocount(wcycle, ewcNS);
828 wallcycle_sub_start(wcycle, ewcsNBS_SEARCH_LOCAL);
829 nbnxn_make_pairlist(nbv->nbs, nbv->grp[eintLocal].nbat,
832 nbv->min_ci_balanced,
833 &nbv->grp[eintLocal].nbl_lists,
835 nbv->grp[eintLocal].kernel_type,
837 wallcycle_sub_stop(wcycle, ewcsNBS_SEARCH_LOCAL);
841 /* initialize local pair-list on the GPU */
842 nbnxn_cuda_init_pairlist(nbv->cu_nbv,
843 nbv->grp[eintLocal].nbl_lists.nbl[0],
846 wallcycle_stop(wcycle, ewcNS);
850 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
851 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
852 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatLocal, FALSE, x,
853 nbv->grp[eintLocal].nbat);
854 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
855 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
860 wallcycle_start(wcycle, ewcLAUNCH_GPU_NB);
861 /* launch local nonbonded F on GPU */
862 do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFNo,
864 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
867 /* Communicate coordinates and sum dipole if necessary +
868 do non-local pair search */
869 if (DOMAINDECOMP(cr))
871 bDiffKernels = (nbv->grp[eintNonlocal].kernel_type !=
872 nbv->grp[eintLocal].kernel_type);
876 /* With GPU+CPU non-bonded calculations we need to copy
877 * the local coordinates to the non-local nbat struct
878 * (in CPU format) as the non-local kernel call also
879 * calculates the local - non-local interactions.
881 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
882 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
883 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatLocal, TRUE, x,
884 nbv->grp[eintNonlocal].nbat);
885 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
886 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
891 wallcycle_start_nocount(wcycle, ewcNS);
892 wallcycle_sub_start(wcycle, ewcsNBS_SEARCH_NONLOCAL);
896 nbnxn_grid_add_simple(nbv->nbs, nbv->grp[eintNonlocal].nbat);
899 nbnxn_make_pairlist(nbv->nbs, nbv->grp[eintNonlocal].nbat,
902 nbv->min_ci_balanced,
903 &nbv->grp[eintNonlocal].nbl_lists,
905 nbv->grp[eintNonlocal].kernel_type,
908 wallcycle_sub_stop(wcycle, ewcsNBS_SEARCH_NONLOCAL);
910 if (nbv->grp[eintNonlocal].kernel_type == nbnxnk8x8x8_CUDA)
912 /* initialize non-local pair-list on the GPU */
913 nbnxn_cuda_init_pairlist(nbv->cu_nbv,
914 nbv->grp[eintNonlocal].nbl_lists.nbl[0],
917 wallcycle_stop(wcycle, ewcNS);
921 wallcycle_start(wcycle, ewcMOVEX);
922 dd_move_x(cr->dd, box, x);
924 /* When we don't need the total dipole we sum it in global_stat */
925 if (bStateChanged && NEED_MUTOT(*inputrec))
927 gmx_sumd(2*DIM, mu, cr);
929 wallcycle_stop(wcycle, ewcMOVEX);
931 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
932 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
933 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatNonlocal, FALSE, x,
934 nbv->grp[eintNonlocal].nbat);
935 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
936 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
939 if (bUseGPU && !bDiffKernels)
941 wallcycle_start(wcycle, ewcLAUNCH_GPU_NB);
942 /* launch non-local nonbonded F on GPU */
943 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFNo,
945 cycles_force += wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
951 /* launch D2H copy-back F */
952 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
953 if (DOMAINDECOMP(cr) && !bDiffKernels)
955 nbnxn_cuda_launch_cpyback(nbv->cu_nbv, nbv->grp[eintNonlocal].nbat,
958 nbnxn_cuda_launch_cpyback(nbv->cu_nbv, nbv->grp[eintLocal].nbat,
960 cycles_force += wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
963 if (bStateChanged && NEED_MUTOT(*inputrec))
967 gmx_sumd(2*DIM, mu, cr);
970 for (i = 0; i < 2; i++)
972 for (j = 0; j < DIM; j++)
974 fr->mu_tot[i][j] = mu[i*DIM + j];
978 if (fr->efep == efepNO)
980 copy_rvec(fr->mu_tot[0], mu_tot);
984 for (j = 0; j < DIM; j++)
987 (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] +
988 lambda[efptCOUL]*fr->mu_tot[1][j];
993 reset_enerdata(fr, bNS, enerd, MASTER(cr));
994 clear_rvecs(SHIFTS, fr->fshift);
996 if (DOMAINDECOMP(cr))
998 if (!(cr->duty & DUTY_PME))
1000 wallcycle_start(wcycle, ewcPPDURINGPME);
1001 dd_force_flop_start(cr->dd, nrnb);
1007 /* Enforced rotation has its own cycle counter that starts after the collective
1008 * coordinates have been communicated. It is added to ddCyclF to allow
1009 * for proper load-balancing */
1010 wallcycle_start(wcycle, ewcROT);
1011 do_rotation(cr, inputrec, box, x, t, step, wcycle, bNS);
1012 wallcycle_stop(wcycle, ewcROT);
1015 /* Start the force cycle counter.
1016 * This counter is stopped in do_forcelow_level.
1017 * No parallel communication should occur while this counter is running,
1018 * since that will interfere with the dynamic load balancing.
1020 wallcycle_start(wcycle, ewcFORCE);
1023 /* Reset forces for which the virial is calculated separately:
1024 * PME/Ewald forces if necessary */
1025 if (fr->bF_NoVirSum)
1027 if (flags & GMX_FORCE_VIRIAL)
1029 fr->f_novirsum = fr->f_novirsum_alloc;
1032 clear_rvecs(fr->f_novirsum_n, fr->f_novirsum);
1036 clear_rvecs(homenr, fr->f_novirsum+start);
1041 /* We are not calculating the pressure so we do not need
1042 * a separate array for forces that do not contribute
1049 /* Clear the short- and long-range forces */
1050 clear_rvecs(fr->natoms_force_constr, f);
1051 if (bSepLRF && do_per_step(step, inputrec->nstcalclr))
1053 clear_rvecs(fr->natoms_force_constr, fr->f_twin);
1056 clear_rvec(fr->vir_diag_posres);
1059 if (inputrec->ePull == epullCONSTRAINT)
1061 clear_pull_forces(inputrec->pull);
1064 /* We calculate the non-bonded forces, when done on the CPU, here.
1065 * We do this before calling do_force_lowlevel, as in there bondeds
1066 * forces are calculated before PME, which does communication.
1067 * With this order, non-bonded and bonded force calculation imbalance
1068 * can be balanced out by the domain decomposition load balancing.
1073 /* Maybe we should move this into do_force_lowlevel */
1074 do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFYes,
1078 if (!bUseOrEmulGPU || bDiffKernels)
1082 if (DOMAINDECOMP(cr))
1084 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal,
1085 bDiffKernels ? enbvClearFYes : enbvClearFNo,
1095 aloc = eintNonlocal;
1098 /* Add all the non-bonded force to the normal force array.
1099 * This can be split into a local a non-local part when overlapping
1100 * communication with calculation with domain decomposition.
1102 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1103 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1104 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1105 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatAll, nbv->grp[aloc].nbat, f);
1106 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1107 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1108 wallcycle_start_nocount(wcycle, ewcFORCE);
1110 /* if there are multiple fshift output buffers reduce them */
1111 if ((flags & GMX_FORCE_VIRIAL) &&
1112 nbv->grp[aloc].nbl_lists.nnbl > 1)
1114 nbnxn_atomdata_add_nbat_fshift_to_fshift(nbv->grp[aloc].nbat,
1119 /* update QMMMrec, if necessary */
1122 update_QMMMrec(cr, fr, x, mdatoms, box, top);
1125 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_POSRES].nr > 0)
1127 posres_wrapper(fplog, flags, bSepDVDL, inputrec, nrnb, top, box, x,
1131 /* Compute the bonded and non-bonded energies and optionally forces */
1132 do_force_lowlevel(fplog, step, fr, inputrec, &(top->idef),
1133 cr, nrnb, wcycle, mdatoms,
1134 x, hist, f, bSepLRF ? fr->f_twin : f, enerd, fcd, top, fr->born,
1135 &(top->atomtypes), bBornRadii, box,
1136 inputrec->fepvals, lambda, graph, &(top->excls), fr->mu_tot,
1137 flags, &cycles_pme);
1141 if (do_per_step(step, inputrec->nstcalclr))
1143 /* Add the long range forces to the short range forces */
1144 for (i = 0; i < fr->natoms_force_constr; i++)
1146 rvec_add(fr->f_twin[i], f[i], f[i]);
1151 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1155 do_flood(cr, inputrec, x, f, ed, box, step, bNS);
1158 if (bUseOrEmulGPU && !bDiffKernels)
1160 /* wait for non-local forces (or calculate in emulation mode) */
1161 if (DOMAINDECOMP(cr))
1165 wallcycle_start(wcycle, ewcWAIT_GPU_NB_NL);
1166 nbnxn_cuda_wait_gpu(nbv->cu_nbv,
1167 nbv->grp[eintNonlocal].nbat,
1169 enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1171 cycles_force += wallcycle_stop(wcycle, ewcWAIT_GPU_NB_NL);
1175 wallcycle_start_nocount(wcycle, ewcFORCE);
1176 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFYes,
1178 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1180 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1181 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1182 /* skip the reduction if there was no non-local work to do */
1183 if (nbv->grp[eintLocal].nbl_lists.nbl[0]->nsci > 0)
1185 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatNonlocal,
1186 nbv->grp[eintNonlocal].nbat, f);
1188 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1189 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1195 /* Communicate the forces */
1198 wallcycle_start(wcycle, ewcMOVEF);
1199 if (DOMAINDECOMP(cr))
1201 dd_move_f(cr->dd, f, fr->fshift);
1202 /* Do we need to communicate the separate force array
1203 * for terms that do not contribute to the single sum virial?
1204 * Position restraints and electric fields do not introduce
1205 * inter-cg forces, only full electrostatics methods do.
1206 * When we do not calculate the virial, fr->f_novirsum = f,
1207 * so we have already communicated these forces.
1209 if (EEL_FULL(fr->eeltype) && cr->dd->n_intercg_excl &&
1210 (flags & GMX_FORCE_VIRIAL))
1212 dd_move_f(cr->dd, fr->f_novirsum, NULL);
1216 /* We should not update the shift forces here,
1217 * since f_twin is already included in f.
1219 dd_move_f(cr->dd, fr->f_twin, NULL);
1222 wallcycle_stop(wcycle, ewcMOVEF);
1228 /* wait for local forces (or calculate in emulation mode) */
1231 wallcycle_start(wcycle, ewcWAIT_GPU_NB_L);
1232 nbnxn_cuda_wait_gpu(nbv->cu_nbv,
1233 nbv->grp[eintLocal].nbat,
1235 enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1237 wallcycle_stop(wcycle, ewcWAIT_GPU_NB_L);
1239 /* now clear the GPU outputs while we finish the step on the CPU */
1241 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1242 nbnxn_cuda_clear_outputs(nbv->cu_nbv, flags);
1243 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1247 wallcycle_start_nocount(wcycle, ewcFORCE);
1248 do_nb_verlet(fr, ic, enerd, flags, eintLocal,
1249 DOMAINDECOMP(cr) ? enbvClearFNo : enbvClearFYes,
1251 wallcycle_stop(wcycle, ewcFORCE);
1253 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1254 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1255 if (nbv->grp[eintLocal].nbl_lists.nbl[0]->nsci > 0)
1257 /* skip the reduction if there was no non-local work to do */
1258 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatLocal,
1259 nbv->grp[eintLocal].nbat, f);
1261 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1262 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1265 if (DOMAINDECOMP(cr))
1267 dd_force_flop_stop(cr->dd, nrnb);
1270 dd_cycles_add(cr->dd, cycles_force-cycles_pme, ddCyclF);
1276 if (IR_ELEC_FIELD(*inputrec))
1278 /* Compute forces due to electric field */
1279 calc_f_el(MASTER(cr) ? field : NULL,
1280 start, homenr, mdatoms->chargeA, fr->f_novirsum,
1281 inputrec->ex, inputrec->et, t);
1284 /* If we have NoVirSum forces, but we do not calculate the virial,
1285 * we sum fr->f_novirum=f later.
1287 if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1289 wallcycle_start(wcycle, ewcVSITESPREAD);
1290 spread_vsite_f(vsite, x, f, fr->fshift, FALSE, NULL, nrnb,
1291 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1292 wallcycle_stop(wcycle, ewcVSITESPREAD);
1296 wallcycle_start(wcycle, ewcVSITESPREAD);
1297 spread_vsite_f(vsite, x, fr->f_twin, NULL, FALSE, NULL,
1299 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1300 wallcycle_stop(wcycle, ewcVSITESPREAD);
1304 if (flags & GMX_FORCE_VIRIAL)
1306 /* Calculation of the virial must be done after vsites! */
1307 calc_virial(mdatoms->start, mdatoms->homenr, x, f,
1308 vir_force, graph, box, nrnb, fr, inputrec->ePBC);
1312 if (inputrec->ePull == epullUMBRELLA || inputrec->ePull == epullCONST_F)
1314 pull_potential_wrapper(fplog, bSepDVDL, cr, inputrec, box, x,
1315 f, vir_force, mdatoms, enerd, lambda, t);
1318 /* Add the forces from enforced rotation potentials (if any) */
1321 wallcycle_start(wcycle, ewcROTadd);
1322 enerd->term[F_COM_PULL] += add_rot_forces(inputrec->rot, f, cr, step, t);
1323 wallcycle_stop(wcycle, ewcROTadd);
1326 if (PAR(cr) && !(cr->duty & DUTY_PME))
1328 /* In case of node-splitting, the PP nodes receive the long-range
1329 * forces, virial and energy from the PME nodes here.
1331 pme_receive_force_ener(fplog, bSepDVDL, cr, wcycle, enerd, fr);
1336 post_process_forces(cr, step, nrnb, wcycle,
1337 top, box, x, f, vir_force, mdatoms, graph, fr, vsite,
1341 /* Sum the potential energy terms from group contributions */
1342 sum_epot(&(enerd->grpp), enerd->term);
1345 void do_force_cutsGROUP(FILE *fplog, t_commrec *cr,
1346 t_inputrec *inputrec,
1347 gmx_large_int_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
1348 gmx_localtop_t *top,
1349 gmx_groups_t *groups,
1350 matrix box, rvec x[], history_t *hist,
1354 gmx_enerdata_t *enerd, t_fcdata *fcd,
1355 real *lambda, t_graph *graph,
1356 t_forcerec *fr, gmx_vsite_t *vsite, rvec mu_tot,
1357 double t, FILE *field, gmx_edsam_t ed,
1358 gmx_bool bBornRadii,
1364 gmx_bool bSepDVDL, bStateChanged, bNS, bFillGrid, bCalcCGCM, bBS;
1365 gmx_bool bDoLongRangeNS, bDoForces, bDoPotential, bSepLRF;
1366 gmx_bool bDoAdressWF;
1368 rvec vzero, box_diag;
1369 real e, v, dvdlambda[efptNR];
1371 float cycles_pme, cycles_force;
1373 start = mdatoms->start;
1374 homenr = mdatoms->homenr;
1376 bSepDVDL = (fr->bSepDVDL && do_per_step(step, inputrec->nstlog));
1378 clear_mat(vir_force);
1382 pd_cg_range(cr, &cg0, &cg1);
1387 if (DOMAINDECOMP(cr))
1389 cg1 = cr->dd->ncg_tot;
1401 bStateChanged = (flags & GMX_FORCE_STATECHANGED);
1402 bNS = (flags & GMX_FORCE_NS) && (fr->bAllvsAll == FALSE);
1403 /* Should we update the long-range neighborlists at this step? */
1404 bDoLongRangeNS = fr->bTwinRange && bNS;
1405 /* Should we perform the long-range nonbonded evaluation inside the neighborsearching? */
1406 bFillGrid = (bNS && bStateChanged);
1407 bCalcCGCM = (bFillGrid && !DOMAINDECOMP(cr));
1408 bDoForces = (flags & GMX_FORCE_FORCES);
1409 bDoPotential = (flags & GMX_FORCE_ENERGY);
1410 bSepLRF = ((inputrec->nstcalclr > 1) && bDoForces &&
1411 (flags & GMX_FORCE_SEPLRF) && (flags & GMX_FORCE_DO_LR));
1413 /* should probably move this to the forcerec since it doesn't change */
1414 bDoAdressWF = ((fr->adress_type != eAdressOff));
1418 update_forcerec(fr, box);
1420 if (NEED_MUTOT(*inputrec))
1422 /* Calculate total (local) dipole moment in a temporary common array.
1423 * This makes it possible to sum them over nodes faster.
1425 calc_mu(start, homenr,
1426 x, mdatoms->chargeA, mdatoms->chargeB, mdatoms->nChargePerturbed,
1431 if (fr->ePBC != epbcNONE)
1433 /* Compute shift vectors every step,
1434 * because of pressure coupling or box deformation!
1436 if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
1438 calc_shifts(box, fr->shift_vec);
1443 put_charge_groups_in_box(fplog, cg0, cg1, fr->ePBC, box,
1444 &(top->cgs), x, fr->cg_cm);
1445 inc_nrnb(nrnb, eNR_CGCM, homenr);
1446 inc_nrnb(nrnb, eNR_RESETX, cg1-cg0);
1448 else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph)
1450 unshift_self(graph, box, x);
1455 calc_cgcm(fplog, cg0, cg1, &(top->cgs), x, fr->cg_cm);
1456 inc_nrnb(nrnb, eNR_CGCM, homenr);
1463 move_cgcm(fplog, cr, fr->cg_cm);
1467 pr_rvecs(debug, 0, "cgcm", fr->cg_cm, top->cgs.nr);
1472 if (!(cr->duty & DUTY_PME))
1474 /* Send particle coordinates to the pme nodes.
1475 * Since this is only implemented for domain decomposition
1476 * and domain decomposition does not use the graph,
1477 * we do not need to worry about shifting.
1480 wallcycle_start(wcycle, ewcPP_PMESENDX);
1482 bBS = (inputrec->nwall == 2);
1485 copy_mat(box, boxs);
1486 svmul(inputrec->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
1489 gmx_pme_send_x(cr, bBS ? boxs : box, x,
1490 mdatoms->nChargePerturbed, lambda[efptCOUL],
1491 (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)), step);
1493 wallcycle_stop(wcycle, ewcPP_PMESENDX);
1495 #endif /* GMX_MPI */
1497 /* Communicate coordinates and sum dipole if necessary */
1500 wallcycle_start(wcycle, ewcMOVEX);
1501 if (DOMAINDECOMP(cr))
1503 dd_move_x(cr->dd, box, x);
1507 move_x(cr, x, nrnb);
1509 wallcycle_stop(wcycle, ewcMOVEX);
1512 /* update adress weight beforehand */
1513 if (bStateChanged && bDoAdressWF)
1515 /* need pbc for adress weight calculation with pbc_dx */
1516 set_pbc(&pbc, inputrec->ePBC, box);
1517 if (fr->adress_site == eAdressSITEcog)
1519 update_adress_weights_cog(top->idef.iparams, top->idef.il, x, fr, mdatoms,
1520 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1522 else if (fr->adress_site == eAdressSITEcom)
1524 update_adress_weights_com(fplog, cg0, cg1, &(top->cgs), x, fr, mdatoms,
1525 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1527 else if (fr->adress_site == eAdressSITEatomatom)
1529 update_adress_weights_atom_per_atom(cg0, cg1, &(top->cgs), x, fr, mdatoms,
1530 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1534 update_adress_weights_atom(cg0, cg1, &(top->cgs), x, fr, mdatoms,
1535 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1539 if (NEED_MUTOT(*inputrec))
1546 gmx_sumd(2*DIM, mu, cr);
1548 for (i = 0; i < 2; i++)
1550 for (j = 0; j < DIM; j++)
1552 fr->mu_tot[i][j] = mu[i*DIM + j];
1556 if (fr->efep == efepNO)
1558 copy_rvec(fr->mu_tot[0], mu_tot);
1562 for (j = 0; j < DIM; j++)
1565 (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] + lambda[efptCOUL]*fr->mu_tot[1][j];
1570 /* Reset energies */
1571 reset_enerdata(fr, bNS, enerd, MASTER(cr));
1572 clear_rvecs(SHIFTS, fr->fshift);
1576 wallcycle_start(wcycle, ewcNS);
1578 if (graph && bStateChanged)
1580 /* Calculate intramolecular shift vectors to make molecules whole */
1581 mk_mshift(fplog, graph, fr->ePBC, box, x);
1584 /* Do the actual neighbour searching */
1586 groups, top, mdatoms,
1587 cr, nrnb, bFillGrid,
1590 wallcycle_stop(wcycle, ewcNS);
1593 if (inputrec->implicit_solvent && bNS)
1595 make_gb_nblist(cr, inputrec->gb_algorithm,
1596 x, box, fr, &top->idef, graph, fr->born);
1599 if (DOMAINDECOMP(cr))
1601 if (!(cr->duty & DUTY_PME))
1603 wallcycle_start(wcycle, ewcPPDURINGPME);
1604 dd_force_flop_start(cr->dd, nrnb);
1610 /* Enforced rotation has its own cycle counter that starts after the collective
1611 * coordinates have been communicated. It is added to ddCyclF to allow
1612 * for proper load-balancing */
1613 wallcycle_start(wcycle, ewcROT);
1614 do_rotation(cr, inputrec, box, x, t, step, wcycle, bNS);
1615 wallcycle_stop(wcycle, ewcROT);
1618 /* Start the force cycle counter.
1619 * This counter is stopped in do_forcelow_level.
1620 * No parallel communication should occur while this counter is running,
1621 * since that will interfere with the dynamic load balancing.
1623 wallcycle_start(wcycle, ewcFORCE);
1627 /* Reset forces for which the virial is calculated separately:
1628 * PME/Ewald forces if necessary */
1629 if (fr->bF_NoVirSum)
1631 if (flags & GMX_FORCE_VIRIAL)
1633 fr->f_novirsum = fr->f_novirsum_alloc;
1636 clear_rvecs(fr->f_novirsum_n, fr->f_novirsum);
1640 clear_rvecs(homenr, fr->f_novirsum+start);
1645 /* We are not calculating the pressure so we do not need
1646 * a separate array for forces that do not contribute
1653 /* Clear the short- and long-range forces */
1654 clear_rvecs(fr->natoms_force_constr, f);
1655 if (bSepLRF && do_per_step(step, inputrec->nstcalclr))
1657 clear_rvecs(fr->natoms_force_constr, fr->f_twin);
1660 clear_rvec(fr->vir_diag_posres);
1662 if (inputrec->ePull == epullCONSTRAINT)
1664 clear_pull_forces(inputrec->pull);
1667 /* update QMMMrec, if necessary */
1670 update_QMMMrec(cr, fr, x, mdatoms, box, top);
1673 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_POSRES].nr > 0)
1675 posres_wrapper(fplog, flags, bSepDVDL, inputrec, nrnb, top, box, x,
1679 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_FBPOSRES].nr > 0)
1681 /* Flat-bottomed position restraints always require full pbc */
1682 if (!(bStateChanged && bDoAdressWF))
1684 set_pbc(&pbc, inputrec->ePBC, box);
1686 v = fbposres(top->idef.il[F_FBPOSRES].nr, top->idef.il[F_FBPOSRES].iatoms,
1687 top->idef.iparams_fbposres,
1688 (const rvec*)x, fr->f_novirsum, fr->vir_diag_posres,
1689 inputrec->ePBC == epbcNONE ? NULL : &pbc,
1690 fr->rc_scaling, fr->ePBC, fr->posres_com);
1691 enerd->term[F_FBPOSRES] += v;
1692 inc_nrnb(nrnb, eNR_FBPOSRES, top->idef.il[F_FBPOSRES].nr/2);
1695 /* Compute the bonded and non-bonded energies and optionally forces */
1696 do_force_lowlevel(fplog, step, fr, inputrec, &(top->idef),
1697 cr, nrnb, wcycle, mdatoms,
1698 x, hist, f, bSepLRF ? fr->f_twin : f, enerd, fcd, top, fr->born,
1699 &(top->atomtypes), bBornRadii, box,
1700 inputrec->fepvals, lambda,
1701 graph, &(top->excls), fr->mu_tot,
1707 if (do_per_step(step, inputrec->nstcalclr))
1709 /* Add the long range forces to the short range forces */
1710 for (i = 0; i < fr->natoms_force_constr; i++)
1712 rvec_add(fr->f_twin[i], f[i], f[i]);
1717 cycles_force = wallcycle_stop(wcycle, ewcFORCE);
1721 do_flood(cr, inputrec, x, f, ed, box, step, bNS);
1724 if (DOMAINDECOMP(cr))
1726 dd_force_flop_stop(cr->dd, nrnb);
1729 dd_cycles_add(cr->dd, cycles_force-cycles_pme, ddCyclF);
1735 if (IR_ELEC_FIELD(*inputrec))
1737 /* Compute forces due to electric field */
1738 calc_f_el(MASTER(cr) ? field : NULL,
1739 start, homenr, mdatoms->chargeA, fr->f_novirsum,
1740 inputrec->ex, inputrec->et, t);
1743 if (bDoAdressWF && fr->adress_icor == eAdressICThermoForce)
1745 /* Compute thermodynamic force in hybrid AdResS region */
1746 adress_thermo_force(start, homenr, &(top->cgs), x, fr->f_novirsum, fr, mdatoms,
1747 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1750 /* Communicate the forces */
1753 wallcycle_start(wcycle, ewcMOVEF);
1754 if (DOMAINDECOMP(cr))
1756 dd_move_f(cr->dd, f, fr->fshift);
1757 /* Do we need to communicate the separate force array
1758 * for terms that do not contribute to the single sum virial?
1759 * Position restraints and electric fields do not introduce
1760 * inter-cg forces, only full electrostatics methods do.
1761 * When we do not calculate the virial, fr->f_novirsum = f,
1762 * so we have already communicated these forces.
1764 if (EEL_FULL(fr->eeltype) && cr->dd->n_intercg_excl &&
1765 (flags & GMX_FORCE_VIRIAL))
1767 dd_move_f(cr->dd, fr->f_novirsum, NULL);
1771 /* We should not update the shift forces here,
1772 * since f_twin is already included in f.
1774 dd_move_f(cr->dd, fr->f_twin, NULL);
1779 pd_move_f(cr, f, nrnb);
1782 pd_move_f(cr, fr->f_twin, nrnb);
1785 wallcycle_stop(wcycle, ewcMOVEF);
1788 /* If we have NoVirSum forces, but we do not calculate the virial,
1789 * we sum fr->f_novirum=f later.
1791 if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1793 wallcycle_start(wcycle, ewcVSITESPREAD);
1794 spread_vsite_f(vsite, x, f, fr->fshift, FALSE, NULL, nrnb,
1795 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1796 wallcycle_stop(wcycle, ewcVSITESPREAD);
1800 wallcycle_start(wcycle, ewcVSITESPREAD);
1801 spread_vsite_f(vsite, x, fr->f_twin, NULL, FALSE, NULL,
1803 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1804 wallcycle_stop(wcycle, ewcVSITESPREAD);
1808 if (flags & GMX_FORCE_VIRIAL)
1810 /* Calculation of the virial must be done after vsites! */
1811 calc_virial(mdatoms->start, mdatoms->homenr, x, f,
1812 vir_force, graph, box, nrnb, fr, inputrec->ePBC);
1816 if (inputrec->ePull == epullUMBRELLA || inputrec->ePull == epullCONST_F)
1818 pull_potential_wrapper(fplog, bSepDVDL, cr, inputrec, box, x,
1819 f, vir_force, mdatoms, enerd, lambda, t);
1822 /* Add the forces from enforced rotation potentials (if any) */
1825 wallcycle_start(wcycle, ewcROTadd);
1826 enerd->term[F_COM_PULL] += add_rot_forces(inputrec->rot, f, cr, step, t);
1827 wallcycle_stop(wcycle, ewcROTadd);
1830 if (PAR(cr) && !(cr->duty & DUTY_PME))
1832 /* In case of node-splitting, the PP nodes receive the long-range
1833 * forces, virial and energy from the PME nodes here.
1835 pme_receive_force_ener(fplog, bSepDVDL, cr, wcycle, enerd, fr);
1840 post_process_forces(cr, step, nrnb, wcycle,
1841 top, box, x, f, vir_force, mdatoms, graph, fr, vsite,
1845 /* Sum the potential energy terms from group contributions */
1846 sum_epot(&(enerd->grpp), enerd->term);
1849 void do_force(FILE *fplog, t_commrec *cr,
1850 t_inputrec *inputrec,
1851 gmx_large_int_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
1852 gmx_localtop_t *top,
1853 gmx_groups_t *groups,
1854 matrix box, rvec x[], history_t *hist,
1858 gmx_enerdata_t *enerd, t_fcdata *fcd,
1859 real *lambda, t_graph *graph,
1861 gmx_vsite_t *vsite, rvec mu_tot,
1862 double t, FILE *field, gmx_edsam_t ed,
1863 gmx_bool bBornRadii,
1866 /* modify force flag if not doing nonbonded */
1867 if (!fr->bNonbonded)
1869 flags &= ~GMX_FORCE_NONBONDED;
1872 switch (inputrec->cutoff_scheme)
1875 do_force_cutsVERLET(fplog, cr, inputrec,
1891 do_force_cutsGROUP(fplog, cr, inputrec,
1906 gmx_incons("Invalid cut-off scheme passed!");
1911 void do_constrain_first(FILE *fplog, gmx_constr_t constr,
1912 t_inputrec *ir, t_mdatoms *md,
1913 t_state *state, t_commrec *cr, t_nrnb *nrnb,
1914 t_forcerec *fr, gmx_localtop_t *top)
1916 int i, m, start, end;
1917 gmx_large_int_t step;
1918 real dt = ir->delta_t;
1922 snew(savex, state->natoms);
1925 end = md->homenr + start;
1929 fprintf(debug, "vcm: start=%d, homenr=%d, end=%d\n",
1930 start, md->homenr, end);
1932 /* Do a first constrain to reset particles... */
1933 step = ir->init_step;
1936 char buf[STEPSTRSIZE];
1937 fprintf(fplog, "\nConstraining the starting coordinates (step %s)\n",
1938 gmx_step_str(step, buf));
1942 /* constrain the current position */
1943 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
1944 ir, NULL, cr, step, 0, md,
1945 state->x, state->x, NULL,
1946 fr->bMolPBC, state->box,
1947 state->lambda[efptBONDED], &dvdl_dum,
1948 NULL, NULL, nrnb, econqCoord,
1949 ir->epc == epcMTTK, state->veta, state->veta);
1952 /* constrain the inital velocity, and save it */
1953 /* also may be useful if we need the ekin from the halfstep for velocity verlet */
1954 /* might not yet treat veta correctly */
1955 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
1956 ir, NULL, cr, step, 0, md,
1957 state->x, state->v, state->v,
1958 fr->bMolPBC, state->box,
1959 state->lambda[efptBONDED], &dvdl_dum,
1960 NULL, NULL, nrnb, econqVeloc,
1961 ir->epc == epcMTTK, state->veta, state->veta);
1963 /* constrain the inital velocities at t-dt/2 */
1964 if (EI_STATE_VELOCITY(ir->eI) && ir->eI != eiVV)
1966 for (i = start; (i < end); i++)
1968 for (m = 0; (m < DIM); m++)
1970 /* Reverse the velocity */
1971 state->v[i][m] = -state->v[i][m];
1972 /* Store the position at t-dt in buf */
1973 savex[i][m] = state->x[i][m] + dt*state->v[i][m];
1976 /* Shake the positions at t=-dt with the positions at t=0
1977 * as reference coordinates.
1981 char buf[STEPSTRSIZE];
1982 fprintf(fplog, "\nConstraining the coordinates at t0-dt (step %s)\n",
1983 gmx_step_str(step, buf));
1986 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
1987 ir, NULL, cr, step, -1, md,
1988 state->x, savex, NULL,
1989 fr->bMolPBC, state->box,
1990 state->lambda[efptBONDED], &dvdl_dum,
1991 state->v, NULL, nrnb, econqCoord,
1992 ir->epc == epcMTTK, state->veta, state->veta);
1994 for (i = start; i < end; i++)
1996 for (m = 0; m < DIM; m++)
1998 /* Re-reverse the velocities */
1999 state->v[i][m] = -state->v[i][m];
2006 void calc_enervirdiff(FILE *fplog, int eDispCorr, t_forcerec *fr)
2008 double eners[2], virs[2], enersum, virsum, y0, f, g, h;
2009 double r0, r1, r, rc3, rc9, ea, eb, ec, pa, pb, pc, pd;
2010 double invscale, invscale2, invscale3;
2011 int ri0, ri1, ri, i, offstart, offset;
2012 real scale, *vdwtab, tabfactor, tmp;
2014 fr->enershiftsix = 0;
2015 fr->enershifttwelve = 0;
2016 fr->enerdiffsix = 0;
2017 fr->enerdifftwelve = 0;
2019 fr->virdifftwelve = 0;
2021 if (eDispCorr != edispcNO)
2023 for (i = 0; i < 2; i++)
2028 if ((fr->vdwtype == evdwSWITCH) || (fr->vdwtype == evdwSHIFT))
2030 if (fr->rvdw_switch == 0)
2033 "With dispersion correction rvdw-switch can not be zero "
2034 "for vdw-type = %s", evdw_names[fr->vdwtype]);
2037 scale = fr->nblists[0].table_elec_vdw.scale;
2038 vdwtab = fr->nblists[0].table_vdw.data;
2040 /* Round the cut-offs to exact table values for precision */
2041 ri0 = floor(fr->rvdw_switch*scale);
2042 ri1 = ceil(fr->rvdw*scale);
2048 if (fr->vdwtype == evdwSHIFT)
2050 /* Determine the constant energy shift below rvdw_switch.
2051 * Table has a scale factor since we have scaled it down to compensate
2052 * for scaling-up c6/c12 with the derivative factors to save flops in analytical kernels.
2054 fr->enershiftsix = (real)(-1.0/(rc3*rc3)) - 6.0*vdwtab[8*ri0];
2055 fr->enershifttwelve = (real)( 1.0/(rc9*rc3)) - 12.0*vdwtab[8*ri0 + 4];
2057 /* Add the constant part from 0 to rvdw_switch.
2058 * This integration from 0 to rvdw_switch overcounts the number
2059 * of interactions by 1, as it also counts the self interaction.
2060 * We will correct for this later.
2062 eners[0] += 4.0*M_PI*fr->enershiftsix*rc3/3.0;
2063 eners[1] += 4.0*M_PI*fr->enershifttwelve*rc3/3.0;
2065 invscale = 1.0/(scale);
2066 invscale2 = invscale*invscale;
2067 invscale3 = invscale*invscale2;
2069 /* following summation derived from cubic spline definition,
2070 Numerical Recipies in C, second edition, p. 113-116. Exact
2071 for the cubic spline. We first calculate the negative of
2072 the energy from rvdw to rvdw_switch, assuming that g(r)=1,
2073 and then add the more standard, abrupt cutoff correction to
2074 that result, yielding the long-range correction for a
2075 switched function. We perform both the pressure and energy
2076 loops at the same time for simplicity, as the computational
2079 for (i = 0; i < 2; i++)
2081 enersum = 0.0; virsum = 0.0;
2085 /* Since the dispersion table has been scaled down a factor 6.0 and the repulsion
2086 * a factor 12.0 to compensate for the c6/c12 parameters inside nbfp[] being scaled
2087 * up (to save flops in kernels), we need to correct for this.
2096 for (ri = ri0; ri < ri1; ri++)
2100 eb = 2.0*invscale2*r;
2104 pb = 3.0*invscale2*r;
2105 pc = 3.0*invscale*r*r;
2108 /* this "8" is from the packing in the vdwtab array - perhaps should be #define'ed? */
2109 offset = 8*ri + offstart;
2110 y0 = vdwtab[offset];
2111 f = vdwtab[offset+1];
2112 g = vdwtab[offset+2];
2113 h = vdwtab[offset+3];
2115 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);
2116 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);
2119 enersum *= 4.0*M_PI*tabfactor;
2120 virsum *= 4.0*M_PI*tabfactor;
2121 eners[i] -= enersum;
2125 /* now add the correction for rvdw_switch to infinity */
2126 eners[0] += -4.0*M_PI/(3.0*rc3);
2127 eners[1] += 4.0*M_PI/(9.0*rc9);
2128 virs[0] += 8.0*M_PI/rc3;
2129 virs[1] += -16.0*M_PI/(3.0*rc9);
2131 else if ((fr->vdwtype == evdwCUT) || (fr->vdwtype == evdwUSER))
2133 if (fr->vdwtype == evdwUSER && fplog)
2136 "WARNING: using dispersion correction with user tables\n");
2138 rc3 = fr->rvdw*fr->rvdw*fr->rvdw;
2140 /* Contribution beyond the cut-off */
2141 eners[0] += -4.0*M_PI/(3.0*rc3);
2142 eners[1] += 4.0*M_PI/(9.0*rc9);
2143 if (fr->vdw_modifier == eintmodPOTSHIFT)
2145 /* Contribution within the cut-off */
2146 eners[0] += -4.0*M_PI/(3.0*rc3);
2147 eners[1] += 4.0*M_PI/(3.0*rc9);
2149 /* Contribution beyond the cut-off */
2150 virs[0] += 8.0*M_PI/rc3;
2151 virs[1] += -16.0*M_PI/(3.0*rc9);
2156 "Dispersion correction is not implemented for vdw-type = %s",
2157 evdw_names[fr->vdwtype]);
2159 fr->enerdiffsix = eners[0];
2160 fr->enerdifftwelve = eners[1];
2161 /* The 0.5 is due to the Gromacs definition of the virial */
2162 fr->virdiffsix = 0.5*virs[0];
2163 fr->virdifftwelve = 0.5*virs[1];
2167 void calc_dispcorr(FILE *fplog, t_inputrec *ir, t_forcerec *fr,
2168 gmx_large_int_t step, int natoms,
2169 matrix box, real lambda, tensor pres, tensor virial,
2170 real *prescorr, real *enercorr, real *dvdlcorr)
2172 gmx_bool bCorrAll, bCorrPres;
2173 real dvdlambda, invvol, dens, ninter, avcsix, avctwelve, enerdiff, svir = 0, spres = 0;
2183 if (ir->eDispCorr != edispcNO)
2185 bCorrAll = (ir->eDispCorr == edispcAllEner ||
2186 ir->eDispCorr == edispcAllEnerPres);
2187 bCorrPres = (ir->eDispCorr == edispcEnerPres ||
2188 ir->eDispCorr == edispcAllEnerPres);
2190 invvol = 1/det(box);
2193 /* Only correct for the interactions with the inserted molecule */
2194 dens = (natoms - fr->n_tpi)*invvol;
2199 dens = natoms*invvol;
2200 ninter = 0.5*natoms;
2203 if (ir->efep == efepNO)
2205 avcsix = fr->avcsix[0];
2206 avctwelve = fr->avctwelve[0];
2210 avcsix = (1 - lambda)*fr->avcsix[0] + lambda*fr->avcsix[1];
2211 avctwelve = (1 - lambda)*fr->avctwelve[0] + lambda*fr->avctwelve[1];
2214 enerdiff = ninter*(dens*fr->enerdiffsix - fr->enershiftsix);
2215 *enercorr += avcsix*enerdiff;
2217 if (ir->efep != efepNO)
2219 dvdlambda += (fr->avcsix[1] - fr->avcsix[0])*enerdiff;
2223 enerdiff = ninter*(dens*fr->enerdifftwelve - fr->enershifttwelve);
2224 *enercorr += avctwelve*enerdiff;
2225 if (fr->efep != efepNO)
2227 dvdlambda += (fr->avctwelve[1] - fr->avctwelve[0])*enerdiff;
2233 svir = ninter*dens*avcsix*fr->virdiffsix/3.0;
2234 if (ir->eDispCorr == edispcAllEnerPres)
2236 svir += ninter*dens*avctwelve*fr->virdifftwelve/3.0;
2238 /* The factor 2 is because of the Gromacs virial definition */
2239 spres = -2.0*invvol*svir*PRESFAC;
2241 for (m = 0; m < DIM; m++)
2243 virial[m][m] += svir;
2244 pres[m][m] += spres;
2249 /* Can't currently control when it prints, for now, just print when degugging */
2254 fprintf(debug, "Long Range LJ corr.: <C6> %10.4e, <C12> %10.4e\n",
2260 "Long Range LJ corr.: Epot %10g, Pres: %10g, Vir: %10g\n",
2261 *enercorr, spres, svir);
2265 fprintf(debug, "Long Range LJ corr.: Epot %10g\n", *enercorr);
2269 if (fr->bSepDVDL && do_per_step(step, ir->nstlog))
2271 gmx_print_sepdvdl(fplog, "Dispersion correction", *enercorr, dvdlambda);
2273 if (fr->efep != efepNO)
2275 *dvdlcorr += dvdlambda;
2280 void do_pbc_first(FILE *fplog, matrix box, t_forcerec *fr,
2281 t_graph *graph, rvec x[])
2285 fprintf(fplog, "Removing pbc first time\n");
2287 calc_shifts(box, fr->shift_vec);
2290 mk_mshift(fplog, graph, fr->ePBC, box, x);
2293 p_graph(debug, "do_pbc_first 1", graph);
2295 shift_self(graph, box, x);
2296 /* By doing an extra mk_mshift the molecules that are broken
2297 * because they were e.g. imported from another software
2298 * will be made whole again. Such are the healing powers
2301 mk_mshift(fplog, graph, fr->ePBC, box, x);
2304 p_graph(debug, "do_pbc_first 2", graph);
2309 fprintf(fplog, "Done rmpbc\n");
2313 static void low_do_pbc_mtop(FILE *fplog, int ePBC, matrix box,
2314 gmx_mtop_t *mtop, rvec x[],
2319 gmx_molblock_t *molb;
2321 if (bFirst && fplog)
2323 fprintf(fplog, "Removing pbc first time\n");
2328 for (mb = 0; mb < mtop->nmolblock; mb++)
2330 molb = &mtop->molblock[mb];
2331 if (molb->natoms_mol == 1 ||
2332 (!bFirst && mtop->moltype[molb->type].cgs.nr == 1))
2334 /* Just one atom or charge group in the molecule, no PBC required */
2335 as += molb->nmol*molb->natoms_mol;
2339 /* Pass NULL iso fplog to avoid graph prints for each molecule type */
2340 mk_graph_ilist(NULL, mtop->moltype[molb->type].ilist,
2341 0, molb->natoms_mol, FALSE, FALSE, graph);
2343 for (mol = 0; mol < molb->nmol; mol++)
2345 mk_mshift(fplog, graph, ePBC, box, x+as);
2347 shift_self(graph, box, x+as);
2348 /* The molecule is whole now.
2349 * We don't need the second mk_mshift call as in do_pbc_first,
2350 * since we no longer need this graph.
2353 as += molb->natoms_mol;
2361 void do_pbc_first_mtop(FILE *fplog, int ePBC, matrix box,
2362 gmx_mtop_t *mtop, rvec x[])
2364 low_do_pbc_mtop(fplog, ePBC, box, mtop, x, TRUE);
2367 void do_pbc_mtop(FILE *fplog, int ePBC, matrix box,
2368 gmx_mtop_t *mtop, rvec x[])
2370 low_do_pbc_mtop(fplog, ePBC, box, mtop, x, FALSE);
2373 void finish_run(FILE *fplog, t_commrec *cr,
2374 t_inputrec *inputrec,
2375 t_nrnb nrnb[], gmx_wallcycle_t wcycle,
2376 gmx_walltime_accounting_t walltime_accounting,
2377 wallclock_gpu_t *gputimes,
2378 gmx_bool bWriteStat)
2381 t_nrnb *nrnb_tot = NULL;
2384 double elapsed_time,
2385 elapsed_time_over_all_ranks,
2386 elapsed_time_over_all_threads,
2387 elapsed_time_over_all_threads_over_all_ranks;
2388 wallcycle_sum(cr, wcycle);
2394 MPI_Allreduce(nrnb->n, nrnb_tot->n, eNRNB, MPI_DOUBLE, MPI_SUM,
2395 cr->mpi_comm_mysim);
2403 elapsed_time = walltime_accounting_get_elapsed_time(walltime_accounting);
2404 elapsed_time_over_all_ranks = elapsed_time;
2405 elapsed_time_over_all_threads = walltime_accounting_get_elapsed_time_over_all_threads(walltime_accounting);
2406 elapsed_time_over_all_threads_over_all_ranks = elapsed_time_over_all_threads;
2410 /* reduce elapsed_time over all MPI ranks in the current simulation */
2411 MPI_Allreduce(&elapsed_time,
2412 &elapsed_time_over_all_ranks,
2413 1, MPI_DOUBLE, MPI_SUM,
2414 cr->mpi_comm_mysim);
2415 elapsed_time_over_all_ranks /= cr->nnodes;
2416 /* Reduce elapsed_time_over_all_threads over all MPI ranks in the
2417 * current simulation. */
2418 MPI_Allreduce(&elapsed_time_over_all_threads,
2419 &elapsed_time_over_all_threads_over_all_ranks,
2420 1, MPI_DOUBLE, MPI_SUM,
2421 cr->mpi_comm_mysim);
2427 print_flop(fplog, nrnb_tot, &nbfs, &mflop);
2434 if ((cr->duty & DUTY_PP) && DOMAINDECOMP(cr))
2436 print_dd_statistics(cr, inputrec, fplog);
2448 snew(nrnb_all, cr->nnodes);
2449 nrnb_all[0] = *nrnb;
2450 for (s = 1; s < cr->nnodes; s++)
2452 MPI_Recv(nrnb_all[s].n, eNRNB, MPI_DOUBLE, s, 0,
2453 cr->mpi_comm_mysim, &stat);
2455 pr_load(fplog, cr, nrnb_all);
2460 MPI_Send(nrnb->n, eNRNB, MPI_DOUBLE, MASTERRANK(cr), 0,
2461 cr->mpi_comm_mysim);
2468 wallcycle_print(fplog, cr->nnodes, cr->npmenodes,
2469 elapsed_time_over_all_ranks,
2472 if (EI_DYNAMICS(inputrec->eI))
2474 delta_t = inputrec->delta_t;
2483 print_perf(fplog, elapsed_time_over_all_threads_over_all_ranks,
2484 elapsed_time_over_all_ranks,
2485 walltime_accounting_get_nsteps_done(walltime_accounting),
2486 delta_t, nbfs, mflop);
2490 print_perf(stderr, elapsed_time_over_all_threads_over_all_ranks,
2491 elapsed_time_over_all_ranks,
2492 walltime_accounting_get_nsteps_done(walltime_accounting),
2493 delta_t, nbfs, mflop);
2498 extern void initialize_lambdas(FILE *fplog, t_inputrec *ir, int *fep_state, real *lambda, double *lam0)
2500 /* this function works, but could probably use a logic rewrite to keep all the different
2501 types of efep straight. */
2504 t_lambda *fep = ir->fepvals;
2506 if ((ir->efep == efepNO) && (ir->bSimTemp == FALSE))
2508 for (i = 0; i < efptNR; i++)
2520 *fep_state = fep->init_fep_state; /* this might overwrite the checkpoint
2521 if checkpoint is set -- a kludge is in for now
2523 for (i = 0; i < efptNR; i++)
2525 /* overwrite lambda state with init_lambda for now for backwards compatibility */
2526 if (fep->init_lambda >= 0) /* if it's -1, it was never initializd */
2528 lambda[i] = fep->init_lambda;
2531 lam0[i] = lambda[i];
2536 lambda[i] = fep->all_lambda[i][*fep_state];
2539 lam0[i] = lambda[i];
2545 /* need to rescale control temperatures to match current state */
2546 for (i = 0; i < ir->opts.ngtc; i++)
2548 if (ir->opts.ref_t[i] > 0)
2550 ir->opts.ref_t[i] = ir->simtempvals->temperatures[*fep_state];
2556 /* Send to the log the information on the current lambdas */
2559 fprintf(fplog, "Initial vector of lambda components:[ ");
2560 for (i = 0; i < efptNR; i++)
2562 fprintf(fplog, "%10.4f ", lambda[i]);
2564 fprintf(fplog, "]\n");
2570 void init_md(FILE *fplog,
2571 t_commrec *cr, t_inputrec *ir, const output_env_t oenv,
2572 double *t, double *t0,
2573 real *lambda, int *fep_state, double *lam0,
2574 t_nrnb *nrnb, gmx_mtop_t *mtop,
2576 int nfile, const t_filenm fnm[],
2577 gmx_mdoutf_t **outf, t_mdebin **mdebin,
2578 tensor force_vir, tensor shake_vir, rvec mu_tot,
2579 gmx_bool *bSimAnn, t_vcm **vcm, unsigned long Flags)
2584 /* Initial values */
2585 *t = *t0 = ir->init_t;
2588 for (i = 0; i < ir->opts.ngtc; i++)
2590 /* set bSimAnn if any group is being annealed */
2591 if (ir->opts.annealing[i] != eannNO)
2598 update_annealing_target_temp(&(ir->opts), ir->init_t);
2601 /* Initialize lambda variables */
2602 initialize_lambdas(fplog, ir, fep_state, lambda, lam0);
2606 *upd = init_update(ir);
2612 *vcm = init_vcm(fplog, &mtop->groups, ir);
2615 if (EI_DYNAMICS(ir->eI) && !(Flags & MD_APPENDFILES))
2617 if (ir->etc == etcBERENDSEN)
2619 please_cite(fplog, "Berendsen84a");
2621 if (ir->etc == etcVRESCALE)
2623 please_cite(fplog, "Bussi2007a");
2631 *outf = init_mdoutf(nfile, fnm, Flags, cr, ir, oenv);
2633 *mdebin = init_mdebin((Flags & MD_APPENDFILES) ? NULL : (*outf)->fp_ene,
2634 mtop, ir, (*outf)->fp_dhdl);
2639 please_cite(fplog, "Fritsch12");
2640 please_cite(fplog, "Junghans10");
2642 /* Initiate variables */
2643 clear_mat(force_vir);
2644 clear_mat(shake_vir);