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
47 #include "gromacs/fileio/gmxfio.h"
50 #include "gromacs/fileio/confio.h"
54 #include "chargegroup.h"
73 #include "gromacs/fileio/trnio.h"
74 #include "gromacs/fileio/xtcio.h"
76 #include "pull_rotation.h"
77 #include "gmx_random.h"
80 #include "gmx_wallcycle.h"
82 #include "nbnxn_atomdata.h"
83 #include "nbnxn_search.h"
84 #include "nbnxn_kernels/nbnxn_kernel_ref.h"
85 #include "nbnxn_kernels/simd_4xn/nbnxn_kernel_simd_4xn.h"
86 #include "nbnxn_kernels/simd_2xnn/nbnxn_kernel_simd_2xnn.h"
87 #include "nbnxn_kernels/nbnxn_kernel_gpu_ref.h"
89 #include "gromacs/utility/gmxmpi.h"
90 #include "gromacs/timing/walltime_accounting.h"
95 #include "nbnxn_cuda_data_mgmt.h"
96 #include "nbnxn_cuda/nbnxn_cuda.h"
98 void print_time(FILE *out,
99 gmx_walltime_accounting_t walltime_accounting,
100 gmx_large_int_t step,
102 t_commrec gmx_unused *cr)
105 char timebuf[STRLEN];
106 double dt, elapsed_seconds, time_per_step;
109 #ifndef GMX_THREAD_MPI
115 fprintf(out, "step %s", gmx_step_str(step, buf));
116 if ((step >= ir->nstlist))
118 double seconds_since_epoch = gmx_gettime();
119 elapsed_seconds = seconds_since_epoch - walltime_accounting_get_start_time_stamp(walltime_accounting);
120 time_per_step = elapsed_seconds/(step - ir->init_step + 1);
121 dt = (ir->nsteps + ir->init_step - step) * time_per_step;
127 finish = (time_t) (seconds_since_epoch + dt);
128 gmx_ctime_r(&finish, timebuf, STRLEN);
129 sprintf(buf, "%s", timebuf);
130 buf[strlen(buf)-1] = '\0';
131 fprintf(out, ", will finish %s", buf);
135 fprintf(out, ", remaining wall clock time: %5d s ", (int)dt);
140 fprintf(out, " performance: %.1f ns/day ",
141 ir->delta_t/1000*24*60*60/time_per_step);
144 #ifndef GMX_THREAD_MPI
154 void print_date_and_time(FILE *fplog, int nodeid, const char *title,
155 const gmx_walltime_accounting_t walltime_accounting)
158 char timebuf[STRLEN];
159 char time_string[STRLEN];
164 if (walltime_accounting != NULL)
166 tmptime = (time_t) walltime_accounting_get_start_time_stamp(walltime_accounting);
167 gmx_ctime_r(&tmptime, timebuf, STRLEN);
171 tmptime = (time_t) gmx_gettime();
172 gmx_ctime_r(&tmptime, timebuf, STRLEN);
174 for (i = 0; timebuf[i] >= ' '; i++)
176 time_string[i] = timebuf[i];
178 time_string[i] = '\0';
180 fprintf(fplog, "%s on node %d %s\n", title, nodeid, time_string);
184 static void sum_forces(int start, int end, rvec f[], rvec flr[])
190 pr_rvecs(debug, 0, "fsr", f+start, end-start);
191 pr_rvecs(debug, 0, "flr", flr+start, end-start);
193 for (i = start; (i < end); i++)
195 rvec_inc(f[i], flr[i]);
200 * calc_f_el calculates forces due to an electric field.
202 * force is kJ mol^-1 nm^-1 = e * kJ mol^-1 nm^-1 / e
204 * Et[] contains the parameters for the time dependent
205 * part of the field (not yet used).
206 * Ex[] contains the parameters for
207 * the spatial dependent part of the field. You can have cool periodic
208 * fields in principle, but only a constant field is supported
210 * The function should return the energy due to the electric field
211 * (if any) but for now returns 0.
214 * There can be problems with the virial.
215 * Since the field is not self-consistent this is unavoidable.
216 * For neutral molecules the virial is correct within this approximation.
217 * For neutral systems with many charged molecules the error is small.
218 * But for systems with a net charge or a few charged molecules
219 * the error can be significant when the field is high.
220 * Solution: implement a self-consitent electric field into PME.
222 static void calc_f_el(FILE *fp, int start, int homenr,
223 real charge[], rvec f[],
224 t_cosines Ex[], t_cosines Et[], double t)
230 for (m = 0; (m < DIM); m++)
237 Ext[m] = cos(Et[m].a[0]*(t-t0))*exp(-sqr(t-t0)/(2.0*sqr(Et[m].a[2])));
241 Ext[m] = cos(Et[m].a[0]*t);
250 /* Convert the field strength from V/nm to MD-units */
251 Ext[m] *= Ex[m].a[0]*FIELDFAC;
252 for (i = start; (i < start+homenr); i++)
254 f[i][m] += charge[i]*Ext[m];
264 fprintf(fp, "%10g %10g %10g %10g #FIELD\n", t,
265 Ext[XX]/FIELDFAC, Ext[YY]/FIELDFAC, Ext[ZZ]/FIELDFAC);
269 static void calc_virial(int start, int homenr, rvec x[], rvec f[],
270 tensor vir_part, t_graph *graph, matrix box,
271 t_nrnb *nrnb, const t_forcerec *fr, int ePBC)
276 /* The short-range virial from surrounding boxes */
278 calc_vir(SHIFTS, fr->shift_vec, fr->fshift, vir_part, ePBC == epbcSCREW, box);
279 inc_nrnb(nrnb, eNR_VIRIAL, SHIFTS);
281 /* Calculate partial virial, for local atoms only, based on short range.
282 * Total virial is computed in global_stat, called from do_md
284 f_calc_vir(start, start+homenr, x, f, vir_part, graph, box);
285 inc_nrnb(nrnb, eNR_VIRIAL, homenr);
287 /* Add position restraint contribution */
288 for (i = 0; i < DIM; i++)
290 vir_part[i][i] += fr->vir_diag_posres[i];
293 /* Add wall contribution */
294 for (i = 0; i < DIM; i++)
296 vir_part[i][ZZ] += fr->vir_wall_z[i];
301 pr_rvecs(debug, 0, "vir_part", vir_part, DIM);
305 static void posres_wrapper(FILE *fplog,
311 matrix box, rvec x[],
312 gmx_enerdata_t *enerd,
320 /* Position restraints always require full pbc */
321 set_pbc(&pbc, ir->ePBC, box);
323 v = posres(top->idef.il[F_POSRES].nr, top->idef.il[F_POSRES].iatoms,
324 top->idef.iparams_posres,
325 (const rvec*)x, fr->f_novirsum, fr->vir_diag_posres,
326 ir->ePBC == epbcNONE ? NULL : &pbc,
327 lambda[efptRESTRAINT], &dvdl,
328 fr->rc_scaling, fr->ePBC, fr->posres_com, fr->posres_comB);
331 gmx_print_sepdvdl(fplog, interaction_function[F_POSRES].longname, v, dvdl);
333 enerd->term[F_POSRES] += v;
334 /* If just the force constant changes, the FEP term is linear,
335 * but if k changes, it is not.
337 enerd->dvdl_nonlin[efptRESTRAINT] += dvdl;
338 inc_nrnb(nrnb, eNR_POSRES, top->idef.il[F_POSRES].nr/2);
340 if ((ir->fepvals->n_lambda > 0) && (flags & GMX_FORCE_DHDL))
342 for (i = 0; i < enerd->n_lambda; i++)
344 real dvdl_dum, lambda_dum;
346 lambda_dum = (i == 0 ? lambda[efptRESTRAINT] : ir->fepvals->all_lambda[efptRESTRAINT][i-1]);
347 v = posres(top->idef.il[F_POSRES].nr, top->idef.il[F_POSRES].iatoms,
348 top->idef.iparams_posres,
349 (const rvec*)x, NULL, NULL,
350 ir->ePBC == epbcNONE ? NULL : &pbc, lambda_dum, &dvdl,
351 fr->rc_scaling, fr->ePBC, fr->posres_com, fr->posres_comB);
352 enerd->enerpart_lambda[i] += v;
357 static void pull_potential_wrapper(FILE *fplog,
361 matrix box, rvec x[],
365 gmx_enerdata_t *enerd,
372 /* Calculate the center of mass forces, this requires communication,
373 * which is why pull_potential is called close to other communication.
374 * The virial contribution is calculated directly,
375 * which is why we call pull_potential after calc_virial.
377 set_pbc(&pbc, ir->ePBC, box);
379 enerd->term[F_COM_PULL] +=
380 pull_potential(ir->ePull, ir->pull, mdatoms, &pbc,
381 cr, t, lambda[efptRESTRAINT], x, f, vir_force, &dvdl);
384 gmx_print_sepdvdl(fplog, "Com pull", enerd->term[F_COM_PULL], dvdl);
386 enerd->dvdl_lin[efptRESTRAINT] += dvdl;
389 static void pme_receive_force_ener(FILE *fplog,
392 gmx_wallcycle_t wcycle,
393 gmx_enerdata_t *enerd,
397 float cycles_ppdpme, cycles_seppme;
399 cycles_ppdpme = wallcycle_stop(wcycle, ewcPPDURINGPME);
400 dd_cycles_add(cr->dd, cycles_ppdpme, ddCyclPPduringPME);
402 /* In case of node-splitting, the PP nodes receive the long-range
403 * forces, virial and energy from the PME nodes here.
405 wallcycle_start(wcycle, ewcPP_PMEWAITRECVF);
407 gmx_pme_receive_f(cr, fr->f_novirsum, fr->vir_el_recip, &e, &dvdl,
411 gmx_print_sepdvdl(fplog, "PME mesh", e, dvdl);
413 enerd->term[F_COUL_RECIP] += e;
414 enerd->dvdl_lin[efptCOUL] += dvdl;
417 dd_cycles_add(cr->dd, cycles_seppme, ddCyclPME);
419 wallcycle_stop(wcycle, ewcPP_PMEWAITRECVF);
422 static void print_large_forces(FILE *fp, t_mdatoms *md, t_commrec *cr,
423 gmx_large_int_t step, real pforce, rvec *x, rvec *f)
427 char buf[STEPSTRSIZE];
430 for (i = md->start; i < md->start+md->homenr; i++)
433 /* We also catch NAN, if the compiler does not optimize this away. */
434 if (fn2 >= pf2 || fn2 != fn2)
436 fprintf(fp, "step %s atom %6d x %8.3f %8.3f %8.3f force %12.5e\n",
437 gmx_step_str(step, buf),
438 ddglatnr(cr->dd, i), x[i][XX], x[i][YY], x[i][ZZ], sqrt(fn2));
443 static void post_process_forces(t_commrec *cr,
444 gmx_large_int_t step,
445 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
447 matrix box, rvec x[],
452 t_forcerec *fr, gmx_vsite_t *vsite,
459 /* Spread the mesh force on virtual sites to the other particles...
460 * This is parallellized. MPI communication is performed
461 * if the constructing atoms aren't local.
463 wallcycle_start(wcycle, ewcVSITESPREAD);
464 spread_vsite_f(vsite, x, fr->f_novirsum, NULL,
465 (flags & GMX_FORCE_VIRIAL), fr->vir_el_recip,
467 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
468 wallcycle_stop(wcycle, ewcVSITESPREAD);
470 if (flags & GMX_FORCE_VIRIAL)
472 /* Now add the forces, this is local */
475 sum_forces(0, fr->f_novirsum_n, f, fr->f_novirsum);
479 sum_forces(mdatoms->start, mdatoms->start+mdatoms->homenr,
482 if (EEL_FULL(fr->eeltype))
484 /* Add the mesh contribution to the virial */
485 m_add(vir_force, fr->vir_el_recip, vir_force);
489 pr_rvecs(debug, 0, "vir_force", vir_force, DIM);
494 if (fr->print_force >= 0)
496 print_large_forces(stderr, mdatoms, cr, step, fr->print_force, x, f);
500 static void do_nb_verlet(t_forcerec *fr,
501 interaction_const_t *ic,
502 gmx_enerdata_t *enerd,
503 int flags, int ilocality,
506 gmx_wallcycle_t wcycle)
508 int nnbl, kernel_type, enr_nbnxn_kernel_ljc, enr_nbnxn_kernel_lj;
510 nonbonded_verlet_group_t *nbvg;
513 if (!(flags & GMX_FORCE_NONBONDED))
515 /* skip non-bonded calculation */
519 nbvg = &fr->nbv->grp[ilocality];
521 /* CUDA kernel launch overhead is already timed separately */
522 if (fr->cutoff_scheme != ecutsVERLET)
524 gmx_incons("Invalid cut-off scheme passed!");
527 bCUDA = (nbvg->kernel_type == nbnxnk8x8x8_CUDA);
531 wallcycle_sub_start(wcycle, ewcsNONBONDED);
533 switch (nbvg->kernel_type)
535 case nbnxnk4x4_PlainC:
536 nbnxn_kernel_ref(&nbvg->nbl_lists,
542 enerd->grpp.ener[egCOULSR],
544 enerd->grpp.ener[egBHAMSR] :
545 enerd->grpp.ener[egLJSR]);
548 case nbnxnk4xN_SIMD_4xN:
549 nbnxn_kernel_simd_4xn(&nbvg->nbl_lists,
556 enerd->grpp.ener[egCOULSR],
558 enerd->grpp.ener[egBHAMSR] :
559 enerd->grpp.ener[egLJSR]);
561 case nbnxnk4xN_SIMD_2xNN:
562 nbnxn_kernel_simd_2xnn(&nbvg->nbl_lists,
569 enerd->grpp.ener[egCOULSR],
571 enerd->grpp.ener[egBHAMSR] :
572 enerd->grpp.ener[egLJSR]);
575 case nbnxnk8x8x8_CUDA:
576 nbnxn_cuda_launch_kernel(fr->nbv->cu_nbv, nbvg->nbat, flags, ilocality);
579 case nbnxnk8x8x8_PlainC:
580 nbnxn_kernel_gpu_ref(nbvg->nbl_lists.nbl[0],
585 nbvg->nbat->out[0].f,
587 enerd->grpp.ener[egCOULSR],
589 enerd->grpp.ener[egBHAMSR] :
590 enerd->grpp.ener[egLJSR]);
594 gmx_incons("Invalid nonbonded kernel type passed!");
599 wallcycle_sub_stop(wcycle, ewcsNONBONDED);
602 if (EEL_RF(ic->eeltype) || ic->eeltype == eelCUT)
604 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_RF;
606 else if ((!bCUDA && nbvg->ewald_excl == ewaldexclAnalytical) ||
607 (bCUDA && nbnxn_cuda_is_kernel_ewald_analytical(fr->nbv->cu_nbv)))
609 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_EWALD;
613 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_TAB;
615 enr_nbnxn_kernel_lj = eNR_NBNXN_LJ;
616 if (flags & GMX_FORCE_ENERGY)
618 /* In eNR_??? the nbnxn F+E kernels are always the F kernel + 1 */
619 enr_nbnxn_kernel_ljc += 1;
620 enr_nbnxn_kernel_lj += 1;
623 inc_nrnb(nrnb, enr_nbnxn_kernel_ljc,
624 nbvg->nbl_lists.natpair_ljq);
625 inc_nrnb(nrnb, enr_nbnxn_kernel_lj,
626 nbvg->nbl_lists.natpair_lj);
627 inc_nrnb(nrnb, enr_nbnxn_kernel_ljc-eNR_NBNXN_LJ_RF+eNR_NBNXN_RF,
628 nbvg->nbl_lists.natpair_q);
631 void do_force_cutsVERLET(FILE *fplog, t_commrec *cr,
632 t_inputrec *inputrec,
633 gmx_large_int_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
635 gmx_groups_t gmx_unused *groups,
636 matrix box, rvec x[], history_t *hist,
640 gmx_enerdata_t *enerd, t_fcdata *fcd,
641 real *lambda, t_graph *graph,
642 t_forcerec *fr, interaction_const_t *ic,
643 gmx_vsite_t *vsite, rvec mu_tot,
644 double t, FILE *field, gmx_edsam_t ed,
652 gmx_bool bSepDVDL, bStateChanged, bNS, bFillGrid, bCalcCGCM, bBS;
653 gmx_bool bDoLongRange, bDoForces, bSepLRF, bUseGPU, bUseOrEmulGPU;
654 gmx_bool bDiffKernels = FALSE;
656 rvec vzero, box_diag;
658 float cycles_pme, cycles_force;
659 nonbonded_verlet_t *nbv;
663 nb_kernel_type = fr->nbv->grp[0].kernel_type;
665 start = mdatoms->start;
666 homenr = mdatoms->homenr;
668 bSepDVDL = (fr->bSepDVDL && do_per_step(step, inputrec->nstlog));
670 clear_mat(vir_force);
673 if (DOMAINDECOMP(cr))
675 cg1 = cr->dd->ncg_tot;
686 bStateChanged = (flags & GMX_FORCE_STATECHANGED);
687 bNS = (flags & GMX_FORCE_NS) && (fr->bAllvsAll == FALSE);
688 bFillGrid = (bNS && bStateChanged);
689 bCalcCGCM = (bFillGrid && !DOMAINDECOMP(cr));
690 bDoLongRange = (fr->bTwinRange && bNS && (flags & GMX_FORCE_DO_LR));
691 bDoForces = (flags & GMX_FORCE_FORCES);
692 bSepLRF = (bDoLongRange && bDoForces && (flags & GMX_FORCE_SEPLRF));
693 bUseGPU = fr->nbv->bUseGPU;
694 bUseOrEmulGPU = bUseGPU || (nbv->grp[0].kernel_type == nbnxnk8x8x8_PlainC);
698 update_forcerec(fr, box);
700 if (NEED_MUTOT(*inputrec))
702 /* Calculate total (local) dipole moment in a temporary common array.
703 * This makes it possible to sum them over nodes faster.
705 calc_mu(start, homenr,
706 x, mdatoms->chargeA, mdatoms->chargeB, mdatoms->nChargePerturbed,
711 if (fr->ePBC != epbcNONE)
713 /* Compute shift vectors every step,
714 * because of pressure coupling or box deformation!
716 if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
718 calc_shifts(box, fr->shift_vec);
723 put_atoms_in_box_omp(fr->ePBC, box, homenr, x);
724 inc_nrnb(nrnb, eNR_SHIFTX, homenr);
726 else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph)
728 unshift_self(graph, box, x);
732 nbnxn_atomdata_copy_shiftvec(flags & GMX_FORCE_DYNAMICBOX,
733 fr->shift_vec, nbv->grp[0].nbat);
736 if (!(cr->duty & DUTY_PME))
738 /* Send particle coordinates to the pme nodes.
739 * Since this is only implemented for domain decomposition
740 * and domain decomposition does not use the graph,
741 * we do not need to worry about shifting.
744 wallcycle_start(wcycle, ewcPP_PMESENDX);
746 bBS = (inputrec->nwall == 2);
750 svmul(inputrec->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
753 gmx_pme_send_x(cr, bBS ? boxs : box, x,
754 mdatoms->nChargePerturbed, lambda[efptCOUL],
755 (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)), step);
757 wallcycle_stop(wcycle, ewcPP_PMESENDX);
761 /* do gridding for pair search */
764 if (graph && bStateChanged)
766 /* Calculate intramolecular shift vectors to make molecules whole */
767 mk_mshift(fplog, graph, fr->ePBC, box, x);
771 box_diag[XX] = box[XX][XX];
772 box_diag[YY] = box[YY][YY];
773 box_diag[ZZ] = box[ZZ][ZZ];
775 wallcycle_start(wcycle, ewcNS);
778 wallcycle_sub_start(wcycle, ewcsNBS_GRID_LOCAL);
779 nbnxn_put_on_grid(nbv->nbs, fr->ePBC, box,
781 0, mdatoms->homenr, -1, fr->cginfo, x,
783 nbv->grp[eintLocal].kernel_type,
784 nbv->grp[eintLocal].nbat);
785 wallcycle_sub_stop(wcycle, ewcsNBS_GRID_LOCAL);
789 wallcycle_sub_start(wcycle, ewcsNBS_GRID_NONLOCAL);
790 nbnxn_put_on_grid_nonlocal(nbv->nbs, domdec_zones(cr->dd),
792 nbv->grp[eintNonlocal].kernel_type,
793 nbv->grp[eintNonlocal].nbat);
794 wallcycle_sub_stop(wcycle, ewcsNBS_GRID_NONLOCAL);
797 if (nbv->ngrp == 1 ||
798 nbv->grp[eintNonlocal].nbat == nbv->grp[eintLocal].nbat)
800 nbnxn_atomdata_set(nbv->grp[eintLocal].nbat, eatAll,
801 nbv->nbs, mdatoms, fr->cginfo);
805 nbnxn_atomdata_set(nbv->grp[eintLocal].nbat, eatLocal,
806 nbv->nbs, mdatoms, fr->cginfo);
807 nbnxn_atomdata_set(nbv->grp[eintNonlocal].nbat, eatAll,
808 nbv->nbs, mdatoms, fr->cginfo);
810 wallcycle_stop(wcycle, ewcNS);
813 /* initialize the GPU atom data and copy shift vector */
818 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
819 nbnxn_cuda_init_atomdata(nbv->cu_nbv, nbv->grp[eintLocal].nbat);
820 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
823 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
824 nbnxn_cuda_upload_shiftvec(nbv->cu_nbv, nbv->grp[eintLocal].nbat);
825 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
828 /* do local pair search */
831 wallcycle_start_nocount(wcycle, ewcNS);
832 wallcycle_sub_start(wcycle, ewcsNBS_SEARCH_LOCAL);
833 nbnxn_make_pairlist(nbv->nbs, nbv->grp[eintLocal].nbat,
836 nbv->min_ci_balanced,
837 &nbv->grp[eintLocal].nbl_lists,
839 nbv->grp[eintLocal].kernel_type,
841 wallcycle_sub_stop(wcycle, ewcsNBS_SEARCH_LOCAL);
845 /* initialize local pair-list on the GPU */
846 nbnxn_cuda_init_pairlist(nbv->cu_nbv,
847 nbv->grp[eintLocal].nbl_lists.nbl[0],
850 wallcycle_stop(wcycle, ewcNS);
854 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
855 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
856 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatLocal, FALSE, x,
857 nbv->grp[eintLocal].nbat);
858 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
859 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
864 wallcycle_start(wcycle, ewcLAUNCH_GPU_NB);
865 /* launch local nonbonded F on GPU */
866 do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFNo,
868 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
871 /* Communicate coordinates and sum dipole if necessary +
872 do non-local pair search */
873 if (DOMAINDECOMP(cr))
875 bDiffKernels = (nbv->grp[eintNonlocal].kernel_type !=
876 nbv->grp[eintLocal].kernel_type);
880 /* With GPU+CPU non-bonded calculations we need to copy
881 * the local coordinates to the non-local nbat struct
882 * (in CPU format) as the non-local kernel call also
883 * calculates the local - non-local interactions.
885 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
886 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
887 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatLocal, TRUE, x,
888 nbv->grp[eintNonlocal].nbat);
889 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
890 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
895 wallcycle_start_nocount(wcycle, ewcNS);
896 wallcycle_sub_start(wcycle, ewcsNBS_SEARCH_NONLOCAL);
900 nbnxn_grid_add_simple(nbv->nbs, nbv->grp[eintNonlocal].nbat);
903 nbnxn_make_pairlist(nbv->nbs, nbv->grp[eintNonlocal].nbat,
906 nbv->min_ci_balanced,
907 &nbv->grp[eintNonlocal].nbl_lists,
909 nbv->grp[eintNonlocal].kernel_type,
912 wallcycle_sub_stop(wcycle, ewcsNBS_SEARCH_NONLOCAL);
914 if (nbv->grp[eintNonlocal].kernel_type == nbnxnk8x8x8_CUDA)
916 /* initialize non-local pair-list on the GPU */
917 nbnxn_cuda_init_pairlist(nbv->cu_nbv,
918 nbv->grp[eintNonlocal].nbl_lists.nbl[0],
921 wallcycle_stop(wcycle, ewcNS);
925 wallcycle_start(wcycle, ewcMOVEX);
926 dd_move_x(cr->dd, box, x);
928 /* When we don't need the total dipole we sum it in global_stat */
929 if (bStateChanged && NEED_MUTOT(*inputrec))
931 gmx_sumd(2*DIM, mu, cr);
933 wallcycle_stop(wcycle, ewcMOVEX);
935 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
936 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
937 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatNonlocal, FALSE, x,
938 nbv->grp[eintNonlocal].nbat);
939 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
940 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
943 if (bUseGPU && !bDiffKernels)
945 wallcycle_start(wcycle, ewcLAUNCH_GPU_NB);
946 /* launch non-local nonbonded F on GPU */
947 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFNo,
949 cycles_force += wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
955 /* launch D2H copy-back F */
956 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
957 if (DOMAINDECOMP(cr) && !bDiffKernels)
959 nbnxn_cuda_launch_cpyback(nbv->cu_nbv, nbv->grp[eintNonlocal].nbat,
962 nbnxn_cuda_launch_cpyback(nbv->cu_nbv, nbv->grp[eintLocal].nbat,
964 cycles_force += wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
967 if (bStateChanged && NEED_MUTOT(*inputrec))
971 gmx_sumd(2*DIM, mu, cr);
974 for (i = 0; i < 2; i++)
976 for (j = 0; j < DIM; j++)
978 fr->mu_tot[i][j] = mu[i*DIM + j];
982 if (fr->efep == efepNO)
984 copy_rvec(fr->mu_tot[0], mu_tot);
988 for (j = 0; j < DIM; j++)
991 (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] +
992 lambda[efptCOUL]*fr->mu_tot[1][j];
997 reset_enerdata(fr, bNS, enerd, MASTER(cr));
998 clear_rvecs(SHIFTS, fr->fshift);
1000 if (DOMAINDECOMP(cr))
1002 if (!(cr->duty & DUTY_PME))
1004 wallcycle_start(wcycle, ewcPPDURINGPME);
1005 dd_force_flop_start(cr->dd, nrnb);
1011 /* Enforced rotation has its own cycle counter that starts after the collective
1012 * coordinates have been communicated. It is added to ddCyclF to allow
1013 * for proper load-balancing */
1014 wallcycle_start(wcycle, ewcROT);
1015 do_rotation(cr, inputrec, box, x, t, step, wcycle, bNS);
1016 wallcycle_stop(wcycle, ewcROT);
1019 /* Start the force cycle counter.
1020 * This counter is stopped in do_forcelow_level.
1021 * No parallel communication should occur while this counter is running,
1022 * since that will interfere with the dynamic load balancing.
1024 wallcycle_start(wcycle, ewcFORCE);
1027 /* Reset forces for which the virial is calculated separately:
1028 * PME/Ewald forces if necessary */
1029 if (fr->bF_NoVirSum)
1031 if (flags & GMX_FORCE_VIRIAL)
1033 fr->f_novirsum = fr->f_novirsum_alloc;
1036 clear_rvecs(fr->f_novirsum_n, fr->f_novirsum);
1040 clear_rvecs(homenr, fr->f_novirsum+start);
1045 /* We are not calculating the pressure so we do not need
1046 * a separate array for forces that do not contribute
1053 /* Clear the short- and long-range forces */
1054 clear_rvecs(fr->natoms_force_constr, f);
1055 if (bSepLRF && do_per_step(step, inputrec->nstcalclr))
1057 clear_rvecs(fr->natoms_force_constr, fr->f_twin);
1060 clear_rvec(fr->vir_diag_posres);
1063 if (inputrec->ePull == epullCONSTRAINT)
1065 clear_pull_forces(inputrec->pull);
1068 /* We calculate the non-bonded forces, when done on the CPU, here.
1069 * We do this before calling do_force_lowlevel, as in there bondeds
1070 * forces are calculated before PME, which does communication.
1071 * With this order, non-bonded and bonded force calculation imbalance
1072 * can be balanced out by the domain decomposition load balancing.
1077 /* Maybe we should move this into do_force_lowlevel */
1078 do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFYes,
1082 if (!bUseOrEmulGPU || bDiffKernels)
1086 if (DOMAINDECOMP(cr))
1088 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal,
1089 bDiffKernels ? enbvClearFYes : enbvClearFNo,
1099 aloc = eintNonlocal;
1102 /* Add all the non-bonded force to the normal force array.
1103 * This can be split into a local a non-local part when overlapping
1104 * communication with calculation with domain decomposition.
1106 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1107 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1108 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1109 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatAll, nbv->grp[aloc].nbat, f);
1110 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1111 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1112 wallcycle_start_nocount(wcycle, ewcFORCE);
1114 /* if there are multiple fshift output buffers reduce them */
1115 if ((flags & GMX_FORCE_VIRIAL) &&
1116 nbv->grp[aloc].nbl_lists.nnbl > 1)
1118 nbnxn_atomdata_add_nbat_fshift_to_fshift(nbv->grp[aloc].nbat,
1123 /* update QMMMrec, if necessary */
1126 update_QMMMrec(cr, fr, x, mdatoms, box, top);
1129 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_POSRES].nr > 0)
1131 posres_wrapper(fplog, flags, bSepDVDL, inputrec, nrnb, top, box, x,
1135 /* Compute the bonded and non-bonded energies and optionally forces */
1136 do_force_lowlevel(fplog, step, fr, inputrec, &(top->idef),
1137 cr, nrnb, wcycle, mdatoms,
1138 x, hist, f, bSepLRF ? fr->f_twin : f, enerd, fcd, top, fr->born,
1139 &(top->atomtypes), bBornRadii, box,
1140 inputrec->fepvals, lambda, graph, &(top->excls), fr->mu_tot,
1141 flags, &cycles_pme);
1145 if (do_per_step(step, inputrec->nstcalclr))
1147 /* Add the long range forces to the short range forces */
1148 for (i = 0; i < fr->natoms_force_constr; i++)
1150 rvec_add(fr->f_twin[i], f[i], f[i]);
1155 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1159 do_flood(cr, inputrec, x, f, ed, box, step, bNS);
1162 if (bUseOrEmulGPU && !bDiffKernels)
1164 /* wait for non-local forces (or calculate in emulation mode) */
1165 if (DOMAINDECOMP(cr))
1169 wallcycle_start(wcycle, ewcWAIT_GPU_NB_NL);
1170 nbnxn_cuda_wait_gpu(nbv->cu_nbv,
1171 nbv->grp[eintNonlocal].nbat,
1173 enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1175 cycles_force += wallcycle_stop(wcycle, ewcWAIT_GPU_NB_NL);
1179 wallcycle_start_nocount(wcycle, ewcFORCE);
1180 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFYes,
1182 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1184 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1185 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1186 /* skip the reduction if there was no non-local work to do */
1187 if (nbv->grp[eintLocal].nbl_lists.nbl[0]->nsci > 0)
1189 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatNonlocal,
1190 nbv->grp[eintNonlocal].nbat, f);
1192 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1193 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1199 /* Communicate the forces */
1202 wallcycle_start(wcycle, ewcMOVEF);
1203 if (DOMAINDECOMP(cr))
1205 dd_move_f(cr->dd, f, fr->fshift);
1206 /* Do we need to communicate the separate force array
1207 * for terms that do not contribute to the single sum virial?
1208 * Position restraints and electric fields do not introduce
1209 * inter-cg forces, only full electrostatics methods do.
1210 * When we do not calculate the virial, fr->f_novirsum = f,
1211 * so we have already communicated these forces.
1213 if (EEL_FULL(fr->eeltype) && cr->dd->n_intercg_excl &&
1214 (flags & GMX_FORCE_VIRIAL))
1216 dd_move_f(cr->dd, fr->f_novirsum, NULL);
1220 /* We should not update the shift forces here,
1221 * since f_twin is already included in f.
1223 dd_move_f(cr->dd, fr->f_twin, NULL);
1226 wallcycle_stop(wcycle, ewcMOVEF);
1232 /* wait for local forces (or calculate in emulation mode) */
1235 wallcycle_start(wcycle, ewcWAIT_GPU_NB_L);
1236 nbnxn_cuda_wait_gpu(nbv->cu_nbv,
1237 nbv->grp[eintLocal].nbat,
1239 enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1241 wallcycle_stop(wcycle, ewcWAIT_GPU_NB_L);
1243 /* now clear the GPU outputs while we finish the step on the CPU */
1245 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1246 nbnxn_cuda_clear_outputs(nbv->cu_nbv, flags);
1247 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1251 wallcycle_start_nocount(wcycle, ewcFORCE);
1252 do_nb_verlet(fr, ic, enerd, flags, eintLocal,
1253 DOMAINDECOMP(cr) ? enbvClearFNo : enbvClearFYes,
1255 wallcycle_stop(wcycle, ewcFORCE);
1257 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1258 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1259 if (nbv->grp[eintLocal].nbl_lists.nbl[0]->nsci > 0)
1261 /* skip the reduction if there was no non-local work to do */
1262 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatLocal,
1263 nbv->grp[eintLocal].nbat, f);
1265 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1266 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1269 if (DOMAINDECOMP(cr))
1271 dd_force_flop_stop(cr->dd, nrnb);
1274 dd_cycles_add(cr->dd, cycles_force-cycles_pme, ddCyclF);
1280 if (IR_ELEC_FIELD(*inputrec))
1282 /* Compute forces due to electric field */
1283 calc_f_el(MASTER(cr) ? field : NULL,
1284 start, homenr, mdatoms->chargeA, fr->f_novirsum,
1285 inputrec->ex, inputrec->et, t);
1288 /* If we have NoVirSum forces, but we do not calculate the virial,
1289 * we sum fr->f_novirum=f later.
1291 if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1293 wallcycle_start(wcycle, ewcVSITESPREAD);
1294 spread_vsite_f(vsite, x, f, fr->fshift, FALSE, NULL, nrnb,
1295 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1296 wallcycle_stop(wcycle, ewcVSITESPREAD);
1300 wallcycle_start(wcycle, ewcVSITESPREAD);
1301 spread_vsite_f(vsite, x, fr->f_twin, NULL, FALSE, NULL,
1303 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1304 wallcycle_stop(wcycle, ewcVSITESPREAD);
1308 if (flags & GMX_FORCE_VIRIAL)
1310 /* Calculation of the virial must be done after vsites! */
1311 calc_virial(mdatoms->start, mdatoms->homenr, x, f,
1312 vir_force, graph, box, nrnb, fr, inputrec->ePBC);
1316 if (inputrec->ePull == epullUMBRELLA || inputrec->ePull == epullCONST_F)
1318 pull_potential_wrapper(fplog, bSepDVDL, cr, inputrec, box, x,
1319 f, vir_force, mdatoms, enerd, lambda, t);
1322 /* Add the forces from enforced rotation potentials (if any) */
1325 wallcycle_start(wcycle, ewcROTadd);
1326 enerd->term[F_COM_PULL] += add_rot_forces(inputrec->rot, f, cr, step, t);
1327 wallcycle_stop(wcycle, ewcROTadd);
1330 if (PAR(cr) && !(cr->duty & DUTY_PME))
1332 /* In case of node-splitting, the PP nodes receive the long-range
1333 * forces, virial and energy from the PME nodes here.
1335 pme_receive_force_ener(fplog, bSepDVDL, cr, wcycle, enerd, fr);
1340 post_process_forces(cr, step, nrnb, wcycle,
1341 top, box, x, f, vir_force, mdatoms, graph, fr, vsite,
1345 /* Sum the potential energy terms from group contributions */
1346 sum_epot(&(enerd->grpp), enerd->term);
1349 void do_force_cutsGROUP(FILE *fplog, t_commrec *cr,
1350 t_inputrec *inputrec,
1351 gmx_large_int_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
1352 gmx_localtop_t *top,
1353 gmx_groups_t *groups,
1354 matrix box, rvec x[], history_t *hist,
1358 gmx_enerdata_t *enerd, t_fcdata *fcd,
1359 real *lambda, t_graph *graph,
1360 t_forcerec *fr, gmx_vsite_t *vsite, rvec mu_tot,
1361 double t, FILE *field, gmx_edsam_t ed,
1362 gmx_bool bBornRadii,
1368 gmx_bool bSepDVDL, bStateChanged, bNS, bFillGrid, bCalcCGCM, bBS;
1369 gmx_bool bDoLongRangeNS, bDoForces, bDoPotential, bSepLRF;
1370 gmx_bool bDoAdressWF;
1372 rvec vzero, box_diag;
1373 real e, v, dvdlambda[efptNR];
1375 float cycles_pme, cycles_force;
1377 start = mdatoms->start;
1378 homenr = mdatoms->homenr;
1380 bSepDVDL = (fr->bSepDVDL && do_per_step(step, inputrec->nstlog));
1382 clear_mat(vir_force);
1386 pd_cg_range(cr, &cg0, &cg1);
1391 if (DOMAINDECOMP(cr))
1393 cg1 = cr->dd->ncg_tot;
1405 bStateChanged = (flags & GMX_FORCE_STATECHANGED);
1406 bNS = (flags & GMX_FORCE_NS) && (fr->bAllvsAll == FALSE);
1407 /* Should we update the long-range neighborlists at this step? */
1408 bDoLongRangeNS = fr->bTwinRange && bNS;
1409 /* Should we perform the long-range nonbonded evaluation inside the neighborsearching? */
1410 bFillGrid = (bNS && bStateChanged);
1411 bCalcCGCM = (bFillGrid && !DOMAINDECOMP(cr));
1412 bDoForces = (flags & GMX_FORCE_FORCES);
1413 bDoPotential = (flags & GMX_FORCE_ENERGY);
1414 bSepLRF = ((inputrec->nstcalclr > 1) && bDoForces &&
1415 (flags & GMX_FORCE_SEPLRF) && (flags & GMX_FORCE_DO_LR));
1417 /* should probably move this to the forcerec since it doesn't change */
1418 bDoAdressWF = ((fr->adress_type != eAdressOff));
1422 update_forcerec(fr, box);
1424 if (NEED_MUTOT(*inputrec))
1426 /* Calculate total (local) dipole moment in a temporary common array.
1427 * This makes it possible to sum them over nodes faster.
1429 calc_mu(start, homenr,
1430 x, mdatoms->chargeA, mdatoms->chargeB, mdatoms->nChargePerturbed,
1435 if (fr->ePBC != epbcNONE)
1437 /* Compute shift vectors every step,
1438 * because of pressure coupling or box deformation!
1440 if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
1442 calc_shifts(box, fr->shift_vec);
1447 put_charge_groups_in_box(fplog, cg0, cg1, fr->ePBC, box,
1448 &(top->cgs), x, fr->cg_cm);
1449 inc_nrnb(nrnb, eNR_CGCM, homenr);
1450 inc_nrnb(nrnb, eNR_RESETX, cg1-cg0);
1452 else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph)
1454 unshift_self(graph, box, x);
1459 calc_cgcm(fplog, cg0, cg1, &(top->cgs), x, fr->cg_cm);
1460 inc_nrnb(nrnb, eNR_CGCM, homenr);
1467 move_cgcm(fplog, cr, fr->cg_cm);
1471 pr_rvecs(debug, 0, "cgcm", fr->cg_cm, top->cgs.nr);
1476 if (!(cr->duty & DUTY_PME))
1478 /* Send particle coordinates to the pme nodes.
1479 * Since this is only implemented for domain decomposition
1480 * and domain decomposition does not use the graph,
1481 * we do not need to worry about shifting.
1484 wallcycle_start(wcycle, ewcPP_PMESENDX);
1486 bBS = (inputrec->nwall == 2);
1489 copy_mat(box, boxs);
1490 svmul(inputrec->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
1493 gmx_pme_send_x(cr, bBS ? boxs : box, x,
1494 mdatoms->nChargePerturbed, lambda[efptCOUL],
1495 (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)), step);
1497 wallcycle_stop(wcycle, ewcPP_PMESENDX);
1499 #endif /* GMX_MPI */
1501 /* Communicate coordinates and sum dipole if necessary */
1504 wallcycle_start(wcycle, ewcMOVEX);
1505 if (DOMAINDECOMP(cr))
1507 dd_move_x(cr->dd, box, x);
1511 move_x(cr, x, nrnb);
1513 wallcycle_stop(wcycle, ewcMOVEX);
1516 /* update adress weight beforehand */
1517 if (bStateChanged && bDoAdressWF)
1519 /* need pbc for adress weight calculation with pbc_dx */
1520 set_pbc(&pbc, inputrec->ePBC, box);
1521 if (fr->adress_site == eAdressSITEcog)
1523 update_adress_weights_cog(top->idef.iparams, top->idef.il, x, fr, mdatoms,
1524 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1526 else if (fr->adress_site == eAdressSITEcom)
1528 update_adress_weights_com(fplog, cg0, cg1, &(top->cgs), x, fr, mdatoms,
1529 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1531 else if (fr->adress_site == eAdressSITEatomatom)
1533 update_adress_weights_atom_per_atom(cg0, cg1, &(top->cgs), x, fr, mdatoms,
1534 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1538 update_adress_weights_atom(cg0, cg1, &(top->cgs), x, fr, mdatoms,
1539 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1543 if (NEED_MUTOT(*inputrec))
1550 gmx_sumd(2*DIM, mu, cr);
1552 for (i = 0; i < 2; i++)
1554 for (j = 0; j < DIM; j++)
1556 fr->mu_tot[i][j] = mu[i*DIM + j];
1560 if (fr->efep == efepNO)
1562 copy_rvec(fr->mu_tot[0], mu_tot);
1566 for (j = 0; j < DIM; j++)
1569 (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] + lambda[efptCOUL]*fr->mu_tot[1][j];
1574 /* Reset energies */
1575 reset_enerdata(fr, bNS, enerd, MASTER(cr));
1576 clear_rvecs(SHIFTS, fr->fshift);
1580 wallcycle_start(wcycle, ewcNS);
1582 if (graph && bStateChanged)
1584 /* Calculate intramolecular shift vectors to make molecules whole */
1585 mk_mshift(fplog, graph, fr->ePBC, box, x);
1588 /* Do the actual neighbour searching */
1590 groups, top, mdatoms,
1591 cr, nrnb, bFillGrid,
1594 wallcycle_stop(wcycle, ewcNS);
1597 if (inputrec->implicit_solvent && bNS)
1599 make_gb_nblist(cr, inputrec->gb_algorithm,
1600 x, box, fr, &top->idef, graph, fr->born);
1603 if (DOMAINDECOMP(cr))
1605 if (!(cr->duty & DUTY_PME))
1607 wallcycle_start(wcycle, ewcPPDURINGPME);
1608 dd_force_flop_start(cr->dd, nrnb);
1614 /* Enforced rotation has its own cycle counter that starts after the collective
1615 * coordinates have been communicated. It is added to ddCyclF to allow
1616 * for proper load-balancing */
1617 wallcycle_start(wcycle, ewcROT);
1618 do_rotation(cr, inputrec, box, x, t, step, wcycle, bNS);
1619 wallcycle_stop(wcycle, ewcROT);
1622 /* Start the force cycle counter.
1623 * This counter is stopped in do_forcelow_level.
1624 * No parallel communication should occur while this counter is running,
1625 * since that will interfere with the dynamic load balancing.
1627 wallcycle_start(wcycle, ewcFORCE);
1631 /* Reset forces for which the virial is calculated separately:
1632 * PME/Ewald forces if necessary */
1633 if (fr->bF_NoVirSum)
1635 if (flags & GMX_FORCE_VIRIAL)
1637 fr->f_novirsum = fr->f_novirsum_alloc;
1640 clear_rvecs(fr->f_novirsum_n, fr->f_novirsum);
1644 clear_rvecs(homenr, fr->f_novirsum+start);
1649 /* We are not calculating the pressure so we do not need
1650 * a separate array for forces that do not contribute
1657 /* Clear the short- and long-range forces */
1658 clear_rvecs(fr->natoms_force_constr, f);
1659 if (bSepLRF && do_per_step(step, inputrec->nstcalclr))
1661 clear_rvecs(fr->natoms_force_constr, fr->f_twin);
1664 clear_rvec(fr->vir_diag_posres);
1666 if (inputrec->ePull == epullCONSTRAINT)
1668 clear_pull_forces(inputrec->pull);
1671 /* update QMMMrec, if necessary */
1674 update_QMMMrec(cr, fr, x, mdatoms, box, top);
1677 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_POSRES].nr > 0)
1679 posres_wrapper(fplog, flags, bSepDVDL, inputrec, nrnb, top, box, x,
1683 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_FBPOSRES].nr > 0)
1685 /* Flat-bottomed position restraints always require full pbc */
1686 if (!(bStateChanged && bDoAdressWF))
1688 set_pbc(&pbc, inputrec->ePBC, box);
1690 v = fbposres(top->idef.il[F_FBPOSRES].nr, top->idef.il[F_FBPOSRES].iatoms,
1691 top->idef.iparams_fbposres,
1692 (const rvec*)x, fr->f_novirsum, fr->vir_diag_posres,
1693 inputrec->ePBC == epbcNONE ? NULL : &pbc,
1694 fr->rc_scaling, fr->ePBC, fr->posres_com);
1695 enerd->term[F_FBPOSRES] += v;
1696 inc_nrnb(nrnb, eNR_FBPOSRES, top->idef.il[F_FBPOSRES].nr/2);
1699 /* Compute the bonded and non-bonded energies and optionally forces */
1700 do_force_lowlevel(fplog, step, fr, inputrec, &(top->idef),
1701 cr, nrnb, wcycle, mdatoms,
1702 x, hist, f, bSepLRF ? fr->f_twin : f, enerd, fcd, top, fr->born,
1703 &(top->atomtypes), bBornRadii, box,
1704 inputrec->fepvals, lambda,
1705 graph, &(top->excls), fr->mu_tot,
1711 if (do_per_step(step, inputrec->nstcalclr))
1713 /* Add the long range forces to the short range forces */
1714 for (i = 0; i < fr->natoms_force_constr; i++)
1716 rvec_add(fr->f_twin[i], f[i], f[i]);
1721 cycles_force = wallcycle_stop(wcycle, ewcFORCE);
1725 do_flood(cr, inputrec, x, f, ed, box, step, bNS);
1728 if (DOMAINDECOMP(cr))
1730 dd_force_flop_stop(cr->dd, nrnb);
1733 dd_cycles_add(cr->dd, cycles_force-cycles_pme, ddCyclF);
1739 if (IR_ELEC_FIELD(*inputrec))
1741 /* Compute forces due to electric field */
1742 calc_f_el(MASTER(cr) ? field : NULL,
1743 start, homenr, mdatoms->chargeA, fr->f_novirsum,
1744 inputrec->ex, inputrec->et, t);
1747 if (bDoAdressWF && fr->adress_icor == eAdressICThermoForce)
1749 /* Compute thermodynamic force in hybrid AdResS region */
1750 adress_thermo_force(start, homenr, &(top->cgs), x, fr->f_novirsum, fr, mdatoms,
1751 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1754 /* Communicate the forces */
1757 wallcycle_start(wcycle, ewcMOVEF);
1758 if (DOMAINDECOMP(cr))
1760 dd_move_f(cr->dd, f, fr->fshift);
1761 /* Do we need to communicate the separate force array
1762 * for terms that do not contribute to the single sum virial?
1763 * Position restraints and electric fields do not introduce
1764 * inter-cg forces, only full electrostatics methods do.
1765 * When we do not calculate the virial, fr->f_novirsum = f,
1766 * so we have already communicated these forces.
1768 if (EEL_FULL(fr->eeltype) && cr->dd->n_intercg_excl &&
1769 (flags & GMX_FORCE_VIRIAL))
1771 dd_move_f(cr->dd, fr->f_novirsum, NULL);
1775 /* We should not update the shift forces here,
1776 * since f_twin is already included in f.
1778 dd_move_f(cr->dd, fr->f_twin, NULL);
1783 pd_move_f(cr, f, nrnb);
1786 pd_move_f(cr, fr->f_twin, nrnb);
1789 wallcycle_stop(wcycle, ewcMOVEF);
1792 /* If we have NoVirSum forces, but we do not calculate the virial,
1793 * we sum fr->f_novirum=f later.
1795 if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1797 wallcycle_start(wcycle, ewcVSITESPREAD);
1798 spread_vsite_f(vsite, x, f, fr->fshift, FALSE, NULL, nrnb,
1799 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1800 wallcycle_stop(wcycle, ewcVSITESPREAD);
1804 wallcycle_start(wcycle, ewcVSITESPREAD);
1805 spread_vsite_f(vsite, x, fr->f_twin, NULL, FALSE, NULL,
1807 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1808 wallcycle_stop(wcycle, ewcVSITESPREAD);
1812 if (flags & GMX_FORCE_VIRIAL)
1814 /* Calculation of the virial must be done after vsites! */
1815 calc_virial(mdatoms->start, mdatoms->homenr, x, f,
1816 vir_force, graph, box, nrnb, fr, inputrec->ePBC);
1820 if (inputrec->ePull == epullUMBRELLA || inputrec->ePull == epullCONST_F)
1822 pull_potential_wrapper(fplog, bSepDVDL, cr, inputrec, box, x,
1823 f, vir_force, mdatoms, enerd, lambda, t);
1826 /* Add the forces from enforced rotation potentials (if any) */
1829 wallcycle_start(wcycle, ewcROTadd);
1830 enerd->term[F_COM_PULL] += add_rot_forces(inputrec->rot, f, cr, step, t);
1831 wallcycle_stop(wcycle, ewcROTadd);
1834 if (PAR(cr) && !(cr->duty & DUTY_PME))
1836 /* In case of node-splitting, the PP nodes receive the long-range
1837 * forces, virial and energy from the PME nodes here.
1839 pme_receive_force_ener(fplog, bSepDVDL, cr, wcycle, enerd, fr);
1844 post_process_forces(cr, step, nrnb, wcycle,
1845 top, box, x, f, vir_force, mdatoms, graph, fr, vsite,
1849 /* Sum the potential energy terms from group contributions */
1850 sum_epot(&(enerd->grpp), enerd->term);
1853 void do_force(FILE *fplog, t_commrec *cr,
1854 t_inputrec *inputrec,
1855 gmx_large_int_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
1856 gmx_localtop_t *top,
1857 gmx_groups_t *groups,
1858 matrix box, rvec x[], history_t *hist,
1862 gmx_enerdata_t *enerd, t_fcdata *fcd,
1863 real *lambda, t_graph *graph,
1865 gmx_vsite_t *vsite, rvec mu_tot,
1866 double t, FILE *field, gmx_edsam_t ed,
1867 gmx_bool bBornRadii,
1870 /* modify force flag if not doing nonbonded */
1871 if (!fr->bNonbonded)
1873 flags &= ~GMX_FORCE_NONBONDED;
1876 switch (inputrec->cutoff_scheme)
1879 do_force_cutsVERLET(fplog, cr, inputrec,
1895 do_force_cutsGROUP(fplog, cr, inputrec,
1910 gmx_incons("Invalid cut-off scheme passed!");
1915 void do_constrain_first(FILE *fplog, gmx_constr_t constr,
1916 t_inputrec *ir, t_mdatoms *md,
1917 t_state *state, t_commrec *cr, t_nrnb *nrnb,
1918 t_forcerec *fr, gmx_localtop_t *top)
1920 int i, m, start, end;
1921 gmx_large_int_t step;
1922 real dt = ir->delta_t;
1926 snew(savex, state->natoms);
1929 end = md->homenr + start;
1933 fprintf(debug, "vcm: start=%d, homenr=%d, end=%d\n",
1934 start, md->homenr, end);
1936 /* Do a first constrain to reset particles... */
1937 step = ir->init_step;
1940 char buf[STEPSTRSIZE];
1941 fprintf(fplog, "\nConstraining the starting coordinates (step %s)\n",
1942 gmx_step_str(step, buf));
1946 /* constrain the current position */
1947 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
1948 ir, NULL, cr, step, 0, md,
1949 state->x, state->x, NULL,
1950 fr->bMolPBC, state->box,
1951 state->lambda[efptBONDED], &dvdl_dum,
1952 NULL, NULL, nrnb, econqCoord,
1953 ir->epc == epcMTTK, state->veta, state->veta);
1956 /* constrain the inital velocity, and save it */
1957 /* also may be useful if we need the ekin from the halfstep for velocity verlet */
1958 /* might not yet treat veta correctly */
1959 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
1960 ir, NULL, cr, step, 0, md,
1961 state->x, state->v, state->v,
1962 fr->bMolPBC, state->box,
1963 state->lambda[efptBONDED], &dvdl_dum,
1964 NULL, NULL, nrnb, econqVeloc,
1965 ir->epc == epcMTTK, state->veta, state->veta);
1967 /* constrain the inital velocities at t-dt/2 */
1968 if (EI_STATE_VELOCITY(ir->eI) && ir->eI != eiVV)
1970 for (i = start; (i < end); i++)
1972 for (m = 0; (m < DIM); m++)
1974 /* Reverse the velocity */
1975 state->v[i][m] = -state->v[i][m];
1976 /* Store the position at t-dt in buf */
1977 savex[i][m] = state->x[i][m] + dt*state->v[i][m];
1980 /* Shake the positions at t=-dt with the positions at t=0
1981 * as reference coordinates.
1985 char buf[STEPSTRSIZE];
1986 fprintf(fplog, "\nConstraining the coordinates at t0-dt (step %s)\n",
1987 gmx_step_str(step, buf));
1990 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
1991 ir, NULL, cr, step, -1, md,
1992 state->x, savex, NULL,
1993 fr->bMolPBC, state->box,
1994 state->lambda[efptBONDED], &dvdl_dum,
1995 state->v, NULL, nrnb, econqCoord,
1996 ir->epc == epcMTTK, state->veta, state->veta);
1998 for (i = start; i < end; i++)
2000 for (m = 0; m < DIM; m++)
2002 /* Re-reverse the velocities */
2003 state->v[i][m] = -state->v[i][m];
2010 void calc_enervirdiff(FILE *fplog, int eDispCorr, t_forcerec *fr)
2012 double eners[2], virs[2], enersum, virsum, y0, f, g, h;
2013 double r0, r1, r, rc3, rc9, ea, eb, ec, pa, pb, pc, pd;
2014 double invscale, invscale2, invscale3;
2015 int ri0, ri1, ri, i, offstart, offset;
2016 real scale, *vdwtab, tabfactor, tmp;
2018 fr->enershiftsix = 0;
2019 fr->enershifttwelve = 0;
2020 fr->enerdiffsix = 0;
2021 fr->enerdifftwelve = 0;
2023 fr->virdifftwelve = 0;
2025 if (eDispCorr != edispcNO)
2027 for (i = 0; i < 2; i++)
2032 if ((fr->vdwtype == evdwSWITCH) || (fr->vdwtype == evdwSHIFT))
2034 if (fr->rvdw_switch == 0)
2037 "With dispersion correction rvdw-switch can not be zero "
2038 "for vdw-type = %s", evdw_names[fr->vdwtype]);
2041 scale = fr->nblists[0].table_elec_vdw.scale;
2042 vdwtab = fr->nblists[0].table_vdw.data;
2044 /* Round the cut-offs to exact table values for precision */
2045 ri0 = floor(fr->rvdw_switch*scale);
2046 ri1 = ceil(fr->rvdw*scale);
2052 if (fr->vdwtype == evdwSHIFT)
2054 /* Determine the constant energy shift below rvdw_switch.
2055 * Table has a scale factor since we have scaled it down to compensate
2056 * for scaling-up c6/c12 with the derivative factors to save flops in analytical kernels.
2058 fr->enershiftsix = (real)(-1.0/(rc3*rc3)) - 6.0*vdwtab[8*ri0];
2059 fr->enershifttwelve = (real)( 1.0/(rc9*rc3)) - 12.0*vdwtab[8*ri0 + 4];
2061 /* Add the constant part from 0 to rvdw_switch.
2062 * This integration from 0 to rvdw_switch overcounts the number
2063 * of interactions by 1, as it also counts the self interaction.
2064 * We will correct for this later.
2066 eners[0] += 4.0*M_PI*fr->enershiftsix*rc3/3.0;
2067 eners[1] += 4.0*M_PI*fr->enershifttwelve*rc3/3.0;
2069 invscale = 1.0/(scale);
2070 invscale2 = invscale*invscale;
2071 invscale3 = invscale*invscale2;
2073 /* following summation derived from cubic spline definition,
2074 Numerical Recipies in C, second edition, p. 113-116. Exact
2075 for the cubic spline. We first calculate the negative of
2076 the energy from rvdw to rvdw_switch, assuming that g(r)=1,
2077 and then add the more standard, abrupt cutoff correction to
2078 that result, yielding the long-range correction for a
2079 switched function. We perform both the pressure and energy
2080 loops at the same time for simplicity, as the computational
2083 for (i = 0; i < 2; i++)
2085 enersum = 0.0; virsum = 0.0;
2089 /* Since the dispersion table has been scaled down a factor 6.0 and the repulsion
2090 * a factor 12.0 to compensate for the c6/c12 parameters inside nbfp[] being scaled
2091 * up (to save flops in kernels), we need to correct for this.
2100 for (ri = ri0; ri < ri1; ri++)
2104 eb = 2.0*invscale2*r;
2108 pb = 3.0*invscale2*r;
2109 pc = 3.0*invscale*r*r;
2112 /* this "8" is from the packing in the vdwtab array - perhaps should be #define'ed? */
2113 offset = 8*ri + offstart;
2114 y0 = vdwtab[offset];
2115 f = vdwtab[offset+1];
2116 g = vdwtab[offset+2];
2117 h = vdwtab[offset+3];
2119 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);
2120 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);
2123 enersum *= 4.0*M_PI*tabfactor;
2124 virsum *= 4.0*M_PI*tabfactor;
2125 eners[i] -= enersum;
2129 /* now add the correction for rvdw_switch to infinity */
2130 eners[0] += -4.0*M_PI/(3.0*rc3);
2131 eners[1] += 4.0*M_PI/(9.0*rc9);
2132 virs[0] += 8.0*M_PI/rc3;
2133 virs[1] += -16.0*M_PI/(3.0*rc9);
2135 else if ((fr->vdwtype == evdwCUT) || (fr->vdwtype == evdwUSER))
2137 if (fr->vdwtype == evdwUSER && fplog)
2140 "WARNING: using dispersion correction with user tables\n");
2142 rc3 = fr->rvdw*fr->rvdw*fr->rvdw;
2144 /* Contribution beyond the cut-off */
2145 eners[0] += -4.0*M_PI/(3.0*rc3);
2146 eners[1] += 4.0*M_PI/(9.0*rc9);
2147 if (fr->vdw_modifier == eintmodPOTSHIFT)
2149 /* Contribution within the cut-off */
2150 eners[0] += -4.0*M_PI/(3.0*rc3);
2151 eners[1] += 4.0*M_PI/(3.0*rc9);
2153 /* Contribution beyond the cut-off */
2154 virs[0] += 8.0*M_PI/rc3;
2155 virs[1] += -16.0*M_PI/(3.0*rc9);
2160 "Dispersion correction is not implemented for vdw-type = %s",
2161 evdw_names[fr->vdwtype]);
2163 fr->enerdiffsix = eners[0];
2164 fr->enerdifftwelve = eners[1];
2165 /* The 0.5 is due to the Gromacs definition of the virial */
2166 fr->virdiffsix = 0.5*virs[0];
2167 fr->virdifftwelve = 0.5*virs[1];
2171 void calc_dispcorr(FILE *fplog, t_inputrec *ir, t_forcerec *fr,
2172 gmx_large_int_t step, int natoms,
2173 matrix box, real lambda, tensor pres, tensor virial,
2174 real *prescorr, real *enercorr, real *dvdlcorr)
2176 gmx_bool bCorrAll, bCorrPres;
2177 real dvdlambda, invvol, dens, ninter, avcsix, avctwelve, enerdiff, svir = 0, spres = 0;
2187 if (ir->eDispCorr != edispcNO)
2189 bCorrAll = (ir->eDispCorr == edispcAllEner ||
2190 ir->eDispCorr == edispcAllEnerPres);
2191 bCorrPres = (ir->eDispCorr == edispcEnerPres ||
2192 ir->eDispCorr == edispcAllEnerPres);
2194 invvol = 1/det(box);
2197 /* Only correct for the interactions with the inserted molecule */
2198 dens = (natoms - fr->n_tpi)*invvol;
2203 dens = natoms*invvol;
2204 ninter = 0.5*natoms;
2207 if (ir->efep == efepNO)
2209 avcsix = fr->avcsix[0];
2210 avctwelve = fr->avctwelve[0];
2214 avcsix = (1 - lambda)*fr->avcsix[0] + lambda*fr->avcsix[1];
2215 avctwelve = (1 - lambda)*fr->avctwelve[0] + lambda*fr->avctwelve[1];
2218 enerdiff = ninter*(dens*fr->enerdiffsix - fr->enershiftsix);
2219 *enercorr += avcsix*enerdiff;
2221 if (ir->efep != efepNO)
2223 dvdlambda += (fr->avcsix[1] - fr->avcsix[0])*enerdiff;
2227 enerdiff = ninter*(dens*fr->enerdifftwelve - fr->enershifttwelve);
2228 *enercorr += avctwelve*enerdiff;
2229 if (fr->efep != efepNO)
2231 dvdlambda += (fr->avctwelve[1] - fr->avctwelve[0])*enerdiff;
2237 svir = ninter*dens*avcsix*fr->virdiffsix/3.0;
2238 if (ir->eDispCorr == edispcAllEnerPres)
2240 svir += ninter*dens*avctwelve*fr->virdifftwelve/3.0;
2242 /* The factor 2 is because of the Gromacs virial definition */
2243 spres = -2.0*invvol*svir*PRESFAC;
2245 for (m = 0; m < DIM; m++)
2247 virial[m][m] += svir;
2248 pres[m][m] += spres;
2253 /* Can't currently control when it prints, for now, just print when degugging */
2258 fprintf(debug, "Long Range LJ corr.: <C6> %10.4e, <C12> %10.4e\n",
2264 "Long Range LJ corr.: Epot %10g, Pres: %10g, Vir: %10g\n",
2265 *enercorr, spres, svir);
2269 fprintf(debug, "Long Range LJ corr.: Epot %10g\n", *enercorr);
2273 if (fr->bSepDVDL && do_per_step(step, ir->nstlog))
2275 gmx_print_sepdvdl(fplog, "Dispersion correction", *enercorr, dvdlambda);
2277 if (fr->efep != efepNO)
2279 *dvdlcorr += dvdlambda;
2284 void do_pbc_first(FILE *fplog, matrix box, t_forcerec *fr,
2285 t_graph *graph, rvec x[])
2289 fprintf(fplog, "Removing pbc first time\n");
2291 calc_shifts(box, fr->shift_vec);
2294 mk_mshift(fplog, graph, fr->ePBC, box, x);
2297 p_graph(debug, "do_pbc_first 1", graph);
2299 shift_self(graph, box, x);
2300 /* By doing an extra mk_mshift the molecules that are broken
2301 * because they were e.g. imported from another software
2302 * will be made whole again. Such are the healing powers
2305 mk_mshift(fplog, graph, fr->ePBC, box, x);
2308 p_graph(debug, "do_pbc_first 2", graph);
2313 fprintf(fplog, "Done rmpbc\n");
2317 static void low_do_pbc_mtop(FILE *fplog, int ePBC, matrix box,
2318 gmx_mtop_t *mtop, rvec x[],
2323 gmx_molblock_t *molb;
2325 if (bFirst && fplog)
2327 fprintf(fplog, "Removing pbc first time\n");
2332 for (mb = 0; mb < mtop->nmolblock; mb++)
2334 molb = &mtop->molblock[mb];
2335 if (molb->natoms_mol == 1 ||
2336 (!bFirst && mtop->moltype[molb->type].cgs.nr == 1))
2338 /* Just one atom or charge group in the molecule, no PBC required */
2339 as += molb->nmol*molb->natoms_mol;
2343 /* Pass NULL iso fplog to avoid graph prints for each molecule type */
2344 mk_graph_ilist(NULL, mtop->moltype[molb->type].ilist,
2345 0, molb->natoms_mol, FALSE, FALSE, graph);
2347 for (mol = 0; mol < molb->nmol; mol++)
2349 mk_mshift(fplog, graph, ePBC, box, x+as);
2351 shift_self(graph, box, x+as);
2352 /* The molecule is whole now.
2353 * We don't need the second mk_mshift call as in do_pbc_first,
2354 * since we no longer need this graph.
2357 as += molb->natoms_mol;
2365 void do_pbc_first_mtop(FILE *fplog, int ePBC, matrix box,
2366 gmx_mtop_t *mtop, rvec x[])
2368 low_do_pbc_mtop(fplog, ePBC, box, mtop, x, TRUE);
2371 void do_pbc_mtop(FILE *fplog, int ePBC, matrix box,
2372 gmx_mtop_t *mtop, rvec x[])
2374 low_do_pbc_mtop(fplog, ePBC, box, mtop, x, FALSE);
2377 void finish_run(FILE *fplog, t_commrec *cr,
2378 t_inputrec *inputrec,
2379 t_nrnb nrnb[], gmx_wallcycle_t wcycle,
2380 gmx_walltime_accounting_t walltime_accounting,
2381 wallclock_gpu_t *gputimes,
2382 gmx_bool bWriteStat)
2385 t_nrnb *nrnb_tot = NULL;
2388 double elapsed_time,
2389 elapsed_time_over_all_ranks,
2390 elapsed_time_over_all_threads,
2391 elapsed_time_over_all_threads_over_all_ranks;
2392 wallcycle_sum(cr, wcycle);
2398 MPI_Allreduce(nrnb->n, nrnb_tot->n, eNRNB, MPI_DOUBLE, MPI_SUM,
2399 cr->mpi_comm_mysim);
2407 elapsed_time = walltime_accounting_get_elapsed_time(walltime_accounting);
2408 elapsed_time_over_all_ranks = elapsed_time;
2409 elapsed_time_over_all_threads = walltime_accounting_get_elapsed_time_over_all_threads(walltime_accounting);
2410 elapsed_time_over_all_threads_over_all_ranks = elapsed_time_over_all_threads;
2414 /* reduce elapsed_time over all MPI ranks in the current simulation */
2415 MPI_Allreduce(&elapsed_time,
2416 &elapsed_time_over_all_ranks,
2417 1, MPI_DOUBLE, MPI_SUM,
2418 cr->mpi_comm_mysim);
2419 elapsed_time_over_all_ranks /= cr->nnodes;
2420 /* Reduce elapsed_time_over_all_threads over all MPI ranks in the
2421 * current simulation. */
2422 MPI_Allreduce(&elapsed_time_over_all_threads,
2423 &elapsed_time_over_all_threads_over_all_ranks,
2424 1, MPI_DOUBLE, MPI_SUM,
2425 cr->mpi_comm_mysim);
2431 print_flop(fplog, nrnb_tot, &nbfs, &mflop);
2438 if ((cr->duty & DUTY_PP) && DOMAINDECOMP(cr))
2440 print_dd_statistics(cr, inputrec, fplog);
2452 snew(nrnb_all, cr->nnodes);
2453 nrnb_all[0] = *nrnb;
2454 for (s = 1; s < cr->nnodes; s++)
2456 MPI_Recv(nrnb_all[s].n, eNRNB, MPI_DOUBLE, s, 0,
2457 cr->mpi_comm_mysim, &stat);
2459 pr_load(fplog, cr, nrnb_all);
2464 MPI_Send(nrnb->n, eNRNB, MPI_DOUBLE, MASTERRANK(cr), 0,
2465 cr->mpi_comm_mysim);
2472 wallcycle_print(fplog, cr->nnodes, cr->npmenodes,
2473 elapsed_time_over_all_ranks,
2476 if (EI_DYNAMICS(inputrec->eI))
2478 delta_t = inputrec->delta_t;
2487 print_perf(fplog, elapsed_time_over_all_threads_over_all_ranks,
2488 elapsed_time_over_all_ranks,
2489 walltime_accounting_get_nsteps_done(walltime_accounting),
2490 delta_t, nbfs, mflop);
2494 print_perf(stderr, elapsed_time_over_all_threads_over_all_ranks,
2495 elapsed_time_over_all_ranks,
2496 walltime_accounting_get_nsteps_done(walltime_accounting),
2497 delta_t, nbfs, mflop);
2502 extern void initialize_lambdas(FILE *fplog, t_inputrec *ir, int *fep_state, real *lambda, double *lam0)
2504 /* this function works, but could probably use a logic rewrite to keep all the different
2505 types of efep straight. */
2508 t_lambda *fep = ir->fepvals;
2510 if ((ir->efep == efepNO) && (ir->bSimTemp == FALSE))
2512 for (i = 0; i < efptNR; i++)
2524 *fep_state = fep->init_fep_state; /* this might overwrite the checkpoint
2525 if checkpoint is set -- a kludge is in for now
2527 for (i = 0; i < efptNR; i++)
2529 /* overwrite lambda state with init_lambda for now for backwards compatibility */
2530 if (fep->init_lambda >= 0) /* if it's -1, it was never initializd */
2532 lambda[i] = fep->init_lambda;
2535 lam0[i] = lambda[i];
2540 lambda[i] = fep->all_lambda[i][*fep_state];
2543 lam0[i] = lambda[i];
2549 /* need to rescale control temperatures to match current state */
2550 for (i = 0; i < ir->opts.ngtc; i++)
2552 if (ir->opts.ref_t[i] > 0)
2554 ir->opts.ref_t[i] = ir->simtempvals->temperatures[*fep_state];
2560 /* Send to the log the information on the current lambdas */
2563 fprintf(fplog, "Initial vector of lambda components:[ ");
2564 for (i = 0; i < efptNR; i++)
2566 fprintf(fplog, "%10.4f ", lambda[i]);
2568 fprintf(fplog, "]\n");
2574 void init_md(FILE *fplog,
2575 t_commrec *cr, t_inputrec *ir, const output_env_t oenv,
2576 double *t, double *t0,
2577 real *lambda, int *fep_state, double *lam0,
2578 t_nrnb *nrnb, gmx_mtop_t *mtop,
2580 int nfile, const t_filenm fnm[],
2581 gmx_mdoutf_t **outf, t_mdebin **mdebin,
2582 tensor force_vir, tensor shake_vir, rvec mu_tot,
2583 gmx_bool *bSimAnn, t_vcm **vcm, unsigned long Flags)
2588 /* Initial values */
2589 *t = *t0 = ir->init_t;
2592 for (i = 0; i < ir->opts.ngtc; i++)
2594 /* set bSimAnn if any group is being annealed */
2595 if (ir->opts.annealing[i] != eannNO)
2602 update_annealing_target_temp(&(ir->opts), ir->init_t);
2605 /* Initialize lambda variables */
2606 initialize_lambdas(fplog, ir, fep_state, lambda, lam0);
2610 *upd = init_update(ir);
2616 *vcm = init_vcm(fplog, &mtop->groups, ir);
2619 if (EI_DYNAMICS(ir->eI) && !(Flags & MD_APPENDFILES))
2621 if (ir->etc == etcBERENDSEN)
2623 please_cite(fplog, "Berendsen84a");
2625 if (ir->etc == etcVRESCALE)
2627 please_cite(fplog, "Bussi2007a");
2635 *outf = init_mdoutf(nfile, fnm, Flags, cr, ir, oenv);
2637 *mdebin = init_mdebin((Flags & MD_APPENDFILES) ? NULL : (*outf)->fp_ene,
2638 mtop, ir, (*outf)->fp_dhdl);
2643 please_cite(fplog, "Fritsch12");
2644 please_cite(fplog, "Junghans10");
2646 /* Initiate variables */
2647 clear_mat(force_vir);
2648 clear_mat(shake_vir);