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 #include <catamount/dclock.h>
47 #ifdef HAVE_SYS_TIME_H
60 #include "chargegroup.h"
83 #include "pull_rotation.h"
84 #include "gmx_random.h"
87 #include "gmx_wallcycle.h"
89 #include "nbnxn_atomdata.h"
90 #include "nbnxn_search.h"
91 #include "nbnxn_kernels/nbnxn_kernel_ref.h"
92 #include "nbnxn_kernels/nbnxn_kernel_simd_4xn.h"
93 #include "nbnxn_kernels/nbnxn_kernel_simd_2xnn.h"
94 #include "nbnxn_kernels/nbnxn_kernel_gpu_ref.h"
96 #include "gromacs/utility/gmxmpi.h"
101 #include "nbnxn_cuda_data_mgmt.h"
102 #include "nbnxn_cuda/nbnxn_cuda.h"
105 typedef struct gmx_timeprint {
110 /* Portable version of ctime_r implemented in src/gmxlib/string2.c, but we do not want it declared in public installed headers */
112 gmx_ctime_r(const time_t *clock, char *buf, int n);
118 #ifdef HAVE_GETTIMEOFDAY
122 gettimeofday(&t, NULL);
124 seconds = (double) t.tv_sec + 1e-6*(double)t.tv_usec;
130 seconds = time(NULL);
137 #define difftime(end, start) ((double)(end)-(double)(start))
139 void print_time(FILE *out, gmx_runtime_t *runtime, gmx_large_int_t step,
140 t_inputrec *ir, t_commrec *cr)
143 char timebuf[STRLEN];
147 #ifndef GMX_THREAD_MPI
153 fprintf(out, "step %s", gmx_step_str(step, buf));
154 if ((step >= ir->nstlist))
156 runtime->last = gmx_gettime();
157 dt = difftime(runtime->last, runtime->real);
158 runtime->time_per_step = dt/(step - ir->init_step + 1);
160 dt = (ir->nsteps + ir->init_step - step)*runtime->time_per_step;
166 finish = (time_t) (runtime->last + dt);
167 gmx_ctime_r(&finish, timebuf, STRLEN);
168 sprintf(buf, "%s", timebuf);
169 buf[strlen(buf)-1] = '\0';
170 fprintf(out, ", will finish %s", buf);
174 fprintf(out, ", remaining runtime: %5d s ", (int)dt);
179 fprintf(out, " performance: %.1f ns/day ",
180 ir->delta_t/1000*24*60*60/runtime->time_per_step);
183 #ifndef GMX_THREAD_MPI
197 static double set_proctime(gmx_runtime_t *runtime)
203 prev = runtime->proc;
204 runtime->proc = dclock();
206 diff = runtime->proc - prev;
210 prev = runtime->proc;
211 runtime->proc = clock();
213 diff = (double)(runtime->proc - prev)/(double)CLOCKS_PER_SEC;
217 /* The counter has probably looped, ignore this data */
224 void runtime_start(gmx_runtime_t *runtime)
226 runtime->real = gmx_gettime();
228 set_proctime(runtime);
229 runtime->realtime = 0;
230 runtime->proctime = 0;
232 runtime->time_per_step = 0;
235 void runtime_end(gmx_runtime_t *runtime)
241 runtime->proctime += set_proctime(runtime);
242 runtime->realtime = now - runtime->real;
246 void runtime_upd_proc(gmx_runtime_t *runtime)
248 runtime->proctime += set_proctime(runtime);
251 void print_date_and_time(FILE *fplog, int nodeid, const char *title,
252 const gmx_runtime_t *runtime)
255 char timebuf[STRLEN];
256 char time_string[STRLEN];
263 tmptime = (time_t) runtime->real;
264 gmx_ctime_r(&tmptime, timebuf, STRLEN);
268 tmptime = (time_t) gmx_gettime();
269 gmx_ctime_r(&tmptime, timebuf, STRLEN);
271 for (i = 0; timebuf[i] >= ' '; i++)
273 time_string[i] = timebuf[i];
275 time_string[i] = '\0';
277 fprintf(fplog, "%s on node %d %s\n", title, nodeid, time_string);
281 static void sum_forces(int start, int end, rvec f[], rvec flr[])
287 pr_rvecs(debug, 0, "fsr", f+start, end-start);
288 pr_rvecs(debug, 0, "flr", flr+start, end-start);
290 for (i = start; (i < end); i++)
292 rvec_inc(f[i], flr[i]);
297 * calc_f_el calculates forces due to an electric field.
299 * force is kJ mol^-1 nm^-1 = e * kJ mol^-1 nm^-1 / e
301 * Et[] contains the parameters for the time dependent
302 * part of the field (not yet used).
303 * Ex[] contains the parameters for
304 * the spatial dependent part of the field. You can have cool periodic
305 * fields in principle, but only a constant field is supported
307 * The function should return the energy due to the electric field
308 * (if any) but for now returns 0.
311 * There can be problems with the virial.
312 * Since the field is not self-consistent this is unavoidable.
313 * For neutral molecules the virial is correct within this approximation.
314 * For neutral systems with many charged molecules the error is small.
315 * But for systems with a net charge or a few charged molecules
316 * the error can be significant when the field is high.
317 * Solution: implement a self-consitent electric field into PME.
319 static void calc_f_el(FILE *fp, int start, int homenr,
320 real charge[], rvec x[], rvec f[],
321 t_cosines Ex[], t_cosines Et[], double t)
327 for (m = 0; (m < DIM); m++)
334 Ext[m] = cos(Et[m].a[0]*(t-t0))*exp(-sqr(t-t0)/(2.0*sqr(Et[m].a[2])));
338 Ext[m] = cos(Et[m].a[0]*t);
347 /* Convert the field strength from V/nm to MD-units */
348 Ext[m] *= Ex[m].a[0]*FIELDFAC;
349 for (i = start; (i < start+homenr); i++)
351 f[i][m] += charge[i]*Ext[m];
361 fprintf(fp, "%10g %10g %10g %10g #FIELD\n", t,
362 Ext[XX]/FIELDFAC, Ext[YY]/FIELDFAC, Ext[ZZ]/FIELDFAC);
366 static void calc_virial(FILE *fplog, int start, int homenr, rvec x[], rvec f[],
367 tensor vir_part, t_graph *graph, matrix box,
368 t_nrnb *nrnb, const t_forcerec *fr, int ePBC)
373 /* The short-range virial from surrounding boxes */
375 calc_vir(fplog, SHIFTS, fr->shift_vec, fr->fshift, vir_part, ePBC == epbcSCREW, box);
376 inc_nrnb(nrnb, eNR_VIRIAL, SHIFTS);
378 /* Calculate partial virial, for local atoms only, based on short range.
379 * Total virial is computed in global_stat, called from do_md
381 f_calc_vir(fplog, start, start+homenr, x, f, vir_part, graph, box);
382 inc_nrnb(nrnb, eNR_VIRIAL, homenr);
384 /* Add position restraint contribution */
385 for (i = 0; i < DIM; i++)
387 vir_part[i][i] += fr->vir_diag_posres[i];
390 /* Add wall contribution */
391 for (i = 0; i < DIM; i++)
393 vir_part[i][ZZ] += fr->vir_wall_z[i];
398 pr_rvecs(debug, 0, "vir_part", vir_part, DIM);
402 static void posres_wrapper(FILE *fplog,
408 matrix box, rvec x[],
410 gmx_enerdata_t *enerd,
418 /* Position restraints always require full pbc */
419 set_pbc(&pbc, ir->ePBC, box);
421 v = posres(top->idef.il[F_POSRES].nr, top->idef.il[F_POSRES].iatoms,
422 top->idef.iparams_posres,
423 (const rvec*)x, fr->f_novirsum, fr->vir_diag_posres,
424 ir->ePBC == epbcNONE ? NULL : &pbc,
425 lambda[efptRESTRAINT], &dvdl,
426 fr->rc_scaling, fr->ePBC, fr->posres_com, fr->posres_comB);
429 fprintf(fplog, sepdvdlformat,
430 interaction_function[F_POSRES].longname, v, dvdl);
432 enerd->term[F_POSRES] += v;
433 /* If just the force constant changes, the FEP term is linear,
434 * but if k changes, it is not.
436 enerd->dvdl_nonlin[efptRESTRAINT] += dvdl;
437 inc_nrnb(nrnb, eNR_POSRES, top->idef.il[F_POSRES].nr/2);
439 if ((ir->fepvals->n_lambda > 0) && (flags & GMX_FORCE_DHDL))
441 for (i = 0; i < enerd->n_lambda; i++)
443 real dvdl_dum, lambda_dum;
445 lambda_dum = (i == 0 ? lambda[efptRESTRAINT] : ir->fepvals->all_lambda[efptRESTRAINT][i-1]);
446 v = posres(top->idef.il[F_POSRES].nr, top->idef.il[F_POSRES].iatoms,
447 top->idef.iparams_posres,
448 (const rvec*)x, NULL, NULL,
449 ir->ePBC == epbcNONE ? NULL : &pbc, lambda_dum, &dvdl,
450 fr->rc_scaling, fr->ePBC, fr->posres_com, fr->posres_comB);
451 enerd->enerpart_lambda[i] += v;
456 static void pull_potential_wrapper(FILE *fplog,
460 matrix box, rvec x[],
464 gmx_enerdata_t *enerd,
471 /* Calculate the center of mass forces, this requires communication,
472 * which is why pull_potential is called close to other communication.
473 * The virial contribution is calculated directly,
474 * which is why we call pull_potential after calc_virial.
476 set_pbc(&pbc, ir->ePBC, box);
478 enerd->term[F_COM_PULL] +=
479 pull_potential(ir->ePull, ir->pull, mdatoms, &pbc,
480 cr, t, lambda[efptRESTRAINT], x, f, vir_force, &dvdl);
483 fprintf(fplog, sepdvdlformat, "Com pull", enerd->term[F_COM_PULL], dvdl);
485 enerd->dvdl_lin[efptRESTRAINT] += dvdl;
488 static void pme_receive_force_ener(FILE *fplog,
491 gmx_wallcycle_t wcycle,
492 gmx_enerdata_t *enerd,
496 float cycles_ppdpme, cycles_seppme;
498 cycles_ppdpme = wallcycle_stop(wcycle, ewcPPDURINGPME);
499 dd_cycles_add(cr->dd, cycles_ppdpme, ddCyclPPduringPME);
501 /* In case of node-splitting, the PP nodes receive the long-range
502 * forces, virial and energy from the PME nodes here.
504 wallcycle_start(wcycle, ewcPP_PMEWAITRECVF);
506 gmx_pme_receive_f(cr, fr->f_novirsum, fr->vir_el_recip, &e, &dvdl,
510 fprintf(fplog, sepdvdlformat, "PME mesh", e, dvdl);
512 enerd->term[F_COUL_RECIP] += e;
513 enerd->dvdl_lin[efptCOUL] += dvdl;
516 dd_cycles_add(cr->dd, cycles_seppme, ddCyclPME);
518 wallcycle_stop(wcycle, ewcPP_PMEWAITRECVF);
521 static void print_large_forces(FILE *fp, t_mdatoms *md, t_commrec *cr,
522 gmx_large_int_t step, real pforce, rvec *x, rvec *f)
526 char buf[STEPSTRSIZE];
529 for (i = md->start; i < md->start+md->homenr; i++)
532 /* We also catch NAN, if the compiler does not optimize this away. */
533 if (fn2 >= pf2 || fn2 != fn2)
535 fprintf(fp, "step %s atom %6d x %8.3f %8.3f %8.3f force %12.5e\n",
536 gmx_step_str(step, buf),
537 ddglatnr(cr->dd, i), x[i][XX], x[i][YY], x[i][ZZ], sqrt(fn2));
542 static void post_process_forces(FILE *fplog,
544 gmx_large_int_t step,
545 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
547 matrix box, rvec x[],
552 t_forcerec *fr, gmx_vsite_t *vsite,
559 /* Spread the mesh force on virtual sites to the other particles...
560 * This is parallellized. MPI communication is performed
561 * if the constructing atoms aren't local.
563 wallcycle_start(wcycle, ewcVSITESPREAD);
564 spread_vsite_f(fplog, vsite, x, fr->f_novirsum, NULL,
565 (flags & GMX_FORCE_VIRIAL), fr->vir_el_recip,
567 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
568 wallcycle_stop(wcycle, ewcVSITESPREAD);
570 if (flags & GMX_FORCE_VIRIAL)
572 /* Now add the forces, this is local */
575 sum_forces(0, fr->f_novirsum_n, f, fr->f_novirsum);
579 sum_forces(mdatoms->start, mdatoms->start+mdatoms->homenr,
582 if (EEL_FULL(fr->eeltype))
584 /* Add the mesh contribution to the virial */
585 m_add(vir_force, fr->vir_el_recip, vir_force);
589 pr_rvecs(debug, 0, "vir_force", vir_force, DIM);
594 if (fr->print_force >= 0)
596 print_large_forces(stderr, mdatoms, cr, step, fr->print_force, x, f);
600 static void do_nb_verlet(t_forcerec *fr,
601 interaction_const_t *ic,
602 gmx_enerdata_t *enerd,
603 int flags, int ilocality,
606 gmx_wallcycle_t wcycle)
608 int nnbl, kernel_type, enr_nbnxn_kernel_ljc, enr_nbnxn_kernel_lj;
610 nonbonded_verlet_group_t *nbvg;
612 if (!(flags & GMX_FORCE_NONBONDED))
614 /* skip non-bonded calculation */
618 nbvg = &fr->nbv->grp[ilocality];
620 /* CUDA kernel launch overhead is already timed separately */
621 if (fr->cutoff_scheme != ecutsVERLET)
623 gmx_incons("Invalid cut-off scheme passed!");
626 if (nbvg->kernel_type != nbnxnk8x8x8_CUDA)
628 wallcycle_sub_start(wcycle, ewcsNONBONDED);
630 switch (nbvg->kernel_type)
632 case nbnxnk4x4_PlainC:
633 nbnxn_kernel_ref(&nbvg->nbl_lists,
639 enerd->grpp.ener[egCOULSR],
641 enerd->grpp.ener[egBHAMSR] :
642 enerd->grpp.ener[egLJSR]);
645 case nbnxnk4xN_SIMD_4xN:
646 nbnxn_kernel_simd_4xn(&nbvg->nbl_lists,
653 enerd->grpp.ener[egCOULSR],
655 enerd->grpp.ener[egBHAMSR] :
656 enerd->grpp.ener[egLJSR]);
658 case nbnxnk4xN_SIMD_2xNN:
659 nbnxn_kernel_simd_2xnn(&nbvg->nbl_lists,
666 enerd->grpp.ener[egCOULSR],
668 enerd->grpp.ener[egBHAMSR] :
669 enerd->grpp.ener[egLJSR]);
672 case nbnxnk8x8x8_CUDA:
673 nbnxn_cuda_launch_kernel(fr->nbv->cu_nbv, nbvg->nbat, flags, ilocality);
676 case nbnxnk8x8x8_PlainC:
677 nbnxn_kernel_gpu_ref(nbvg->nbl_lists.nbl[0],
682 nbvg->nbat->out[0].f,
684 enerd->grpp.ener[egCOULSR],
686 enerd->grpp.ener[egBHAMSR] :
687 enerd->grpp.ener[egLJSR]);
691 gmx_incons("Invalid nonbonded kernel type passed!");
694 if (nbvg->kernel_type != nbnxnk8x8x8_CUDA)
696 wallcycle_sub_stop(wcycle, ewcsNONBONDED);
699 if (EEL_RF(ic->eeltype) || ic->eeltype == eelCUT)
701 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_RF;
703 else if (nbvg->ewald_excl == ewaldexclTable)
705 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_TAB;
709 enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_EWALD;
711 enr_nbnxn_kernel_lj = eNR_NBNXN_LJ;
712 if (flags & GMX_FORCE_ENERGY)
714 /* In eNR_??? the nbnxn F+E kernels are always the F kernel + 1 */
715 enr_nbnxn_kernel_ljc += 1;
716 enr_nbnxn_kernel_lj += 1;
719 inc_nrnb(nrnb, enr_nbnxn_kernel_ljc,
720 nbvg->nbl_lists.natpair_ljq);
721 inc_nrnb(nrnb, enr_nbnxn_kernel_lj,
722 nbvg->nbl_lists.natpair_lj);
723 inc_nrnb(nrnb, enr_nbnxn_kernel_ljc-eNR_NBNXN_LJ_RF+eNR_NBNXN_RF,
724 nbvg->nbl_lists.natpair_q);
727 void do_force_cutsVERLET(FILE *fplog, t_commrec *cr,
728 t_inputrec *inputrec,
729 gmx_large_int_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
732 gmx_groups_t *groups,
733 matrix box, rvec x[], history_t *hist,
737 gmx_enerdata_t *enerd, t_fcdata *fcd,
738 real *lambda, t_graph *graph,
739 t_forcerec *fr, interaction_const_t *ic,
740 gmx_vsite_t *vsite, rvec mu_tot,
741 double t, FILE *field, gmx_edsam_t ed,
749 gmx_bool bSepDVDL, bStateChanged, bNS, bFillGrid, bCalcCGCM, bBS;
750 gmx_bool bDoLongRange, bDoForces, bSepLRF, bUseGPU, bUseOrEmulGPU;
751 gmx_bool bDiffKernels = FALSE;
753 rvec vzero, box_diag;
755 float cycles_pme, cycles_force;
756 nonbonded_verlet_t *nbv;
760 nb_kernel_type = fr->nbv->grp[0].kernel_type;
762 start = mdatoms->start;
763 homenr = mdatoms->homenr;
765 bSepDVDL = (fr->bSepDVDL && do_per_step(step, inputrec->nstlog));
767 clear_mat(vir_force);
770 if (DOMAINDECOMP(cr))
772 cg1 = cr->dd->ncg_tot;
783 bStateChanged = (flags & GMX_FORCE_STATECHANGED);
784 bNS = (flags & GMX_FORCE_NS) && (fr->bAllvsAll == FALSE);
785 bFillGrid = (bNS && bStateChanged);
786 bCalcCGCM = (bFillGrid && !DOMAINDECOMP(cr));
787 bDoLongRange = (fr->bTwinRange && bNS && (flags & GMX_FORCE_DO_LR));
788 bDoForces = (flags & GMX_FORCE_FORCES);
789 bSepLRF = (bDoLongRange && bDoForces && (flags & GMX_FORCE_SEPLRF));
790 bUseGPU = fr->nbv->bUseGPU;
791 bUseOrEmulGPU = bUseGPU || (nbv->grp[0].kernel_type == nbnxnk8x8x8_PlainC);
795 update_forcerec(fplog, fr, box);
797 if (NEED_MUTOT(*inputrec))
799 /* Calculate total (local) dipole moment in a temporary common array.
800 * This makes it possible to sum them over nodes faster.
802 calc_mu(start, homenr,
803 x, mdatoms->chargeA, mdatoms->chargeB, mdatoms->nChargePerturbed,
808 if (fr->ePBC != epbcNONE)
810 /* Compute shift vectors every step,
811 * because of pressure coupling or box deformation!
813 if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
815 calc_shifts(box, fr->shift_vec);
820 put_atoms_in_box_omp(fr->ePBC, box, homenr, x);
821 inc_nrnb(nrnb, eNR_SHIFTX, homenr);
823 else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph)
825 unshift_self(graph, box, x);
829 nbnxn_atomdata_copy_shiftvec(flags & GMX_FORCE_DYNAMICBOX,
830 fr->shift_vec, nbv->grp[0].nbat);
833 if (!(cr->duty & DUTY_PME))
835 /* Send particle coordinates to the pme nodes.
836 * Since this is only implemented for domain decomposition
837 * and domain decomposition does not use the graph,
838 * we do not need to worry about shifting.
841 wallcycle_start(wcycle, ewcPP_PMESENDX);
843 bBS = (inputrec->nwall == 2);
847 svmul(inputrec->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
850 gmx_pme_send_x(cr, bBS ? boxs : box, x,
851 mdatoms->nChargePerturbed, lambda[efptCOUL],
852 (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)), step);
854 wallcycle_stop(wcycle, ewcPP_PMESENDX);
858 /* do gridding for pair search */
861 if (graph && bStateChanged)
863 /* Calculate intramolecular shift vectors to make molecules whole */
864 mk_mshift(fplog, graph, fr->ePBC, box, x);
868 box_diag[XX] = box[XX][XX];
869 box_diag[YY] = box[YY][YY];
870 box_diag[ZZ] = box[ZZ][ZZ];
872 wallcycle_start(wcycle, ewcNS);
875 wallcycle_sub_start(wcycle, ewcsNBS_GRID_LOCAL);
876 nbnxn_put_on_grid(nbv->nbs, fr->ePBC, box,
878 0, mdatoms->homenr, -1, fr->cginfo, x,
880 nbv->grp[eintLocal].kernel_type,
881 nbv->grp[eintLocal].nbat);
882 wallcycle_sub_stop(wcycle, ewcsNBS_GRID_LOCAL);
886 wallcycle_sub_start(wcycle, ewcsNBS_GRID_NONLOCAL);
887 nbnxn_put_on_grid_nonlocal(nbv->nbs, domdec_zones(cr->dd),
889 nbv->grp[eintNonlocal].kernel_type,
890 nbv->grp[eintNonlocal].nbat);
891 wallcycle_sub_stop(wcycle, ewcsNBS_GRID_NONLOCAL);
894 if (nbv->ngrp == 1 ||
895 nbv->grp[eintNonlocal].nbat == nbv->grp[eintLocal].nbat)
897 nbnxn_atomdata_set(nbv->grp[eintLocal].nbat, eatAll,
898 nbv->nbs, mdatoms, fr->cginfo);
902 nbnxn_atomdata_set(nbv->grp[eintLocal].nbat, eatLocal,
903 nbv->nbs, mdatoms, fr->cginfo);
904 nbnxn_atomdata_set(nbv->grp[eintNonlocal].nbat, eatAll,
905 nbv->nbs, mdatoms, fr->cginfo);
907 wallcycle_stop(wcycle, ewcNS);
910 /* initialize the GPU atom data and copy shift vector */
915 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
916 nbnxn_cuda_init_atomdata(nbv->cu_nbv, nbv->grp[eintLocal].nbat);
917 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
920 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
921 nbnxn_cuda_upload_shiftvec(nbv->cu_nbv, nbv->grp[eintLocal].nbat);
922 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
925 /* do local pair search */
928 wallcycle_start_nocount(wcycle, ewcNS);
929 wallcycle_sub_start(wcycle, ewcsNBS_SEARCH_LOCAL);
930 nbnxn_make_pairlist(nbv->nbs, nbv->grp[eintLocal].nbat,
933 nbv->min_ci_balanced,
934 &nbv->grp[eintLocal].nbl_lists,
936 nbv->grp[eintLocal].kernel_type,
938 wallcycle_sub_stop(wcycle, ewcsNBS_SEARCH_LOCAL);
942 /* initialize local pair-list on the GPU */
943 nbnxn_cuda_init_pairlist(nbv->cu_nbv,
944 nbv->grp[eintLocal].nbl_lists.nbl[0],
947 wallcycle_stop(wcycle, ewcNS);
951 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
952 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
953 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatLocal, FALSE, x,
954 nbv->grp[eintLocal].nbat);
955 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
956 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
961 wallcycle_start(wcycle, ewcLAUNCH_GPU_NB);
962 /* launch local nonbonded F on GPU */
963 do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFNo,
965 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
968 /* Communicate coordinates and sum dipole if necessary +
969 do non-local pair search */
970 if (DOMAINDECOMP(cr))
972 bDiffKernels = (nbv->grp[eintNonlocal].kernel_type !=
973 nbv->grp[eintLocal].kernel_type);
977 /* With GPU+CPU non-bonded calculations we need to copy
978 * the local coordinates to the non-local nbat struct
979 * (in CPU format) as the non-local kernel call also
980 * calculates the local - non-local interactions.
982 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
983 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
984 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatLocal, TRUE, x,
985 nbv->grp[eintNonlocal].nbat);
986 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
987 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
992 wallcycle_start_nocount(wcycle, ewcNS);
993 wallcycle_sub_start(wcycle, ewcsNBS_SEARCH_NONLOCAL);
997 nbnxn_grid_add_simple(nbv->nbs, nbv->grp[eintNonlocal].nbat);
1000 nbnxn_make_pairlist(nbv->nbs, nbv->grp[eintNonlocal].nbat,
1003 nbv->min_ci_balanced,
1004 &nbv->grp[eintNonlocal].nbl_lists,
1006 nbv->grp[eintNonlocal].kernel_type,
1009 wallcycle_sub_stop(wcycle, ewcsNBS_SEARCH_NONLOCAL);
1011 if (nbv->grp[eintNonlocal].kernel_type == nbnxnk8x8x8_CUDA)
1013 /* initialize non-local pair-list on the GPU */
1014 nbnxn_cuda_init_pairlist(nbv->cu_nbv,
1015 nbv->grp[eintNonlocal].nbl_lists.nbl[0],
1018 wallcycle_stop(wcycle, ewcNS);
1022 wallcycle_start(wcycle, ewcMOVEX);
1023 dd_move_x(cr->dd, box, x);
1025 /* When we don't need the total dipole we sum it in global_stat */
1026 if (bStateChanged && NEED_MUTOT(*inputrec))
1028 gmx_sumd(2*DIM, mu, cr);
1030 wallcycle_stop(wcycle, ewcMOVEX);
1032 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1033 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
1034 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs, eatNonlocal, FALSE, x,
1035 nbv->grp[eintNonlocal].nbat);
1036 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
1037 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1040 if (bUseGPU && !bDiffKernels)
1042 wallcycle_start(wcycle, ewcLAUNCH_GPU_NB);
1043 /* launch non-local nonbonded F on GPU */
1044 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFNo,
1046 cycles_force += wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1052 /* launch D2H copy-back F */
1053 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1054 if (DOMAINDECOMP(cr) && !bDiffKernels)
1056 nbnxn_cuda_launch_cpyback(nbv->cu_nbv, nbv->grp[eintNonlocal].nbat,
1057 flags, eatNonlocal);
1059 nbnxn_cuda_launch_cpyback(nbv->cu_nbv, nbv->grp[eintLocal].nbat,
1061 cycles_force += wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1064 if (bStateChanged && NEED_MUTOT(*inputrec))
1068 gmx_sumd(2*DIM, mu, cr);
1071 for (i = 0; i < 2; i++)
1073 for (j = 0; j < DIM; j++)
1075 fr->mu_tot[i][j] = mu[i*DIM + j];
1079 if (fr->efep == efepNO)
1081 copy_rvec(fr->mu_tot[0], mu_tot);
1085 for (j = 0; j < DIM; j++)
1088 (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] +
1089 lambda[efptCOUL]*fr->mu_tot[1][j];
1093 /* Reset energies */
1094 reset_enerdata(&(inputrec->opts), fr, bNS, enerd, MASTER(cr));
1095 clear_rvecs(SHIFTS, fr->fshift);
1097 if (DOMAINDECOMP(cr))
1099 if (!(cr->duty & DUTY_PME))
1101 wallcycle_start(wcycle, ewcPPDURINGPME);
1102 dd_force_flop_start(cr->dd, nrnb);
1108 /* Enforced rotation has its own cycle counter that starts after the collective
1109 * coordinates have been communicated. It is added to ddCyclF to allow
1110 * for proper load-balancing */
1111 wallcycle_start(wcycle, ewcROT);
1112 do_rotation(cr, inputrec, box, x, t, step, wcycle, bNS);
1113 wallcycle_stop(wcycle, ewcROT);
1116 /* Start the force cycle counter.
1117 * This counter is stopped in do_forcelow_level.
1118 * No parallel communication should occur while this counter is running,
1119 * since that will interfere with the dynamic load balancing.
1121 wallcycle_start(wcycle, ewcFORCE);
1124 /* Reset forces for which the virial is calculated separately:
1125 * PME/Ewald forces if necessary */
1126 if (fr->bF_NoVirSum)
1128 if (flags & GMX_FORCE_VIRIAL)
1130 fr->f_novirsum = fr->f_novirsum_alloc;
1133 clear_rvecs(fr->f_novirsum_n, fr->f_novirsum);
1137 clear_rvecs(homenr, fr->f_novirsum+start);
1142 /* We are not calculating the pressure so we do not need
1143 * a separate array for forces that do not contribute
1150 /* Clear the short- and long-range forces */
1151 clear_rvecs(fr->natoms_force_constr, f);
1152 if (bSepLRF && do_per_step(step, inputrec->nstcalclr))
1154 clear_rvecs(fr->natoms_force_constr, fr->f_twin);
1157 clear_rvec(fr->vir_diag_posres);
1160 if (inputrec->ePull == epullCONSTRAINT)
1162 clear_pull_forces(inputrec->pull);
1165 /* We calculate the non-bonded forces, when done on the CPU, here.
1166 * We do this before calling do_force_lowlevel, as in there bondeds
1167 * forces are calculated before PME, which does communication.
1168 * With this order, non-bonded and bonded force calculation imbalance
1169 * can be balanced out by the domain decomposition load balancing.
1174 /* Maybe we should move this into do_force_lowlevel */
1175 do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFYes,
1179 if (!bUseOrEmulGPU || bDiffKernels)
1183 if (DOMAINDECOMP(cr))
1185 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal,
1186 bDiffKernels ? enbvClearFYes : enbvClearFNo,
1196 aloc = eintNonlocal;
1199 /* Add all the non-bonded force to the normal force array.
1200 * This can be split into a local a non-local part when overlapping
1201 * communication with calculation with domain decomposition.
1203 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1204 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1205 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1206 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatAll, nbv->grp[aloc].nbat, f);
1207 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1208 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1209 wallcycle_start_nocount(wcycle, ewcFORCE);
1211 /* if there are multiple fshift output buffers reduce them */
1212 if ((flags & GMX_FORCE_VIRIAL) &&
1213 nbv->grp[aloc].nbl_lists.nnbl > 1)
1215 nbnxn_atomdata_add_nbat_fshift_to_fshift(nbv->grp[aloc].nbat,
1220 /* update QMMMrec, if necessary */
1223 update_QMMMrec(cr, fr, x, mdatoms, box, top);
1226 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_POSRES].nr > 0)
1228 posres_wrapper(fplog, flags, bSepDVDL, inputrec, nrnb, top, box, x,
1229 f, enerd, lambda, fr);
1232 /* Compute the bonded and non-bonded energies and optionally forces */
1233 do_force_lowlevel(fplog, step, fr, inputrec, &(top->idef),
1234 cr, nrnb, wcycle, mdatoms, &(inputrec->opts),
1235 x, hist, f, bSepLRF ? fr->f_twin : f, enerd, fcd, mtop, top, fr->born,
1236 &(top->atomtypes), bBornRadii, box,
1237 inputrec->fepvals, lambda, graph, &(top->excls), fr->mu_tot,
1238 flags, &cycles_pme);
1242 if (do_per_step(step, inputrec->nstcalclr))
1244 /* Add the long range forces to the short range forces */
1245 for (i = 0; i < fr->natoms_force_constr; i++)
1247 rvec_add(fr->f_twin[i], f[i], f[i]);
1252 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1256 do_flood(cr, inputrec, x, f, ed, box, step, bNS);
1259 if (bUseOrEmulGPU && !bDiffKernels)
1261 /* wait for non-local forces (or calculate in emulation mode) */
1262 if (DOMAINDECOMP(cr))
1266 wallcycle_start(wcycle, ewcWAIT_GPU_NB_NL);
1267 nbnxn_cuda_wait_gpu(nbv->cu_nbv,
1268 nbv->grp[eintNonlocal].nbat,
1270 enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1272 cycles_force += wallcycle_stop(wcycle, ewcWAIT_GPU_NB_NL);
1276 wallcycle_start_nocount(wcycle, ewcFORCE);
1277 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFYes,
1279 cycles_force += wallcycle_stop(wcycle, ewcFORCE);
1281 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1282 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1283 /* skip the reduction if there was no non-local work to do */
1284 if (nbv->grp[eintLocal].nbl_lists.nbl[0]->nsci > 0)
1286 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatNonlocal,
1287 nbv->grp[eintNonlocal].nbat, f);
1289 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1290 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1296 /* Communicate the forces */
1299 wallcycle_start(wcycle, ewcMOVEF);
1300 if (DOMAINDECOMP(cr))
1302 dd_move_f(cr->dd, f, fr->fshift);
1303 /* Do we need to communicate the separate force array
1304 * for terms that do not contribute to the single sum virial?
1305 * Position restraints and electric fields do not introduce
1306 * inter-cg forces, only full electrostatics methods do.
1307 * When we do not calculate the virial, fr->f_novirsum = f,
1308 * so we have already communicated these forces.
1310 if (EEL_FULL(fr->eeltype) && cr->dd->n_intercg_excl &&
1311 (flags & GMX_FORCE_VIRIAL))
1313 dd_move_f(cr->dd, fr->f_novirsum, NULL);
1317 /* We should not update the shift forces here,
1318 * since f_twin is already included in f.
1320 dd_move_f(cr->dd, fr->f_twin, NULL);
1323 wallcycle_stop(wcycle, ewcMOVEF);
1329 /* wait for local forces (or calculate in emulation mode) */
1332 wallcycle_start(wcycle, ewcWAIT_GPU_NB_L);
1333 nbnxn_cuda_wait_gpu(nbv->cu_nbv,
1334 nbv->grp[eintLocal].nbat,
1336 enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1338 wallcycle_stop(wcycle, ewcWAIT_GPU_NB_L);
1340 /* now clear the GPU outputs while we finish the step on the CPU */
1342 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1343 nbnxn_cuda_clear_outputs(nbv->cu_nbv, flags);
1344 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
1348 wallcycle_start_nocount(wcycle, ewcFORCE);
1349 do_nb_verlet(fr, ic, enerd, flags, eintLocal,
1350 DOMAINDECOMP(cr) ? enbvClearFNo : enbvClearFYes,
1352 wallcycle_stop(wcycle, ewcFORCE);
1354 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1355 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1356 if (nbv->grp[eintLocal].nbl_lists.nbl[0]->nsci > 0)
1358 /* skip the reduction if there was no non-local work to do */
1359 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs, eatLocal,
1360 nbv->grp[eintLocal].nbat, f);
1362 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1363 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1366 if (DOMAINDECOMP(cr))
1368 dd_force_flop_stop(cr->dd, nrnb);
1371 dd_cycles_add(cr->dd, cycles_force-cycles_pme, ddCyclF);
1377 if (IR_ELEC_FIELD(*inputrec))
1379 /* Compute forces due to electric field */
1380 calc_f_el(MASTER(cr) ? field : NULL,
1381 start, homenr, mdatoms->chargeA, x, fr->f_novirsum,
1382 inputrec->ex, inputrec->et, t);
1385 /* If we have NoVirSum forces, but we do not calculate the virial,
1386 * we sum fr->f_novirum=f later.
1388 if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1390 wallcycle_start(wcycle, ewcVSITESPREAD);
1391 spread_vsite_f(fplog, vsite, x, f, fr->fshift, FALSE, NULL, nrnb,
1392 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1393 wallcycle_stop(wcycle, ewcVSITESPREAD);
1397 wallcycle_start(wcycle, ewcVSITESPREAD);
1398 spread_vsite_f(fplog, vsite, x, fr->f_twin, NULL, FALSE, NULL,
1400 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1401 wallcycle_stop(wcycle, ewcVSITESPREAD);
1405 if (flags & GMX_FORCE_VIRIAL)
1407 /* Calculation of the virial must be done after vsites! */
1408 calc_virial(fplog, mdatoms->start, mdatoms->homenr, x, f,
1409 vir_force, graph, box, nrnb, fr, inputrec->ePBC);
1413 if (inputrec->ePull == epullUMBRELLA || inputrec->ePull == epullCONST_F)
1415 pull_potential_wrapper(fplog, bSepDVDL, cr, inputrec, box, x,
1416 f, vir_force, mdatoms, enerd, lambda, t);
1419 /* Add the forces from enforced rotation potentials (if any) */
1422 wallcycle_start(wcycle, ewcROTadd);
1423 enerd->term[F_COM_PULL] += add_rot_forces(inputrec->rot, f, cr, step, t);
1424 wallcycle_stop(wcycle, ewcROTadd);
1427 if (PAR(cr) && !(cr->duty & DUTY_PME))
1429 /* In case of node-splitting, the PP nodes receive the long-range
1430 * forces, virial and energy from the PME nodes here.
1432 pme_receive_force_ener(fplog, bSepDVDL, cr, wcycle, enerd, fr);
1437 post_process_forces(fplog, cr, step, nrnb, wcycle,
1438 top, box, x, f, vir_force, mdatoms, graph, fr, vsite,
1442 /* Sum the potential energy terms from group contributions */
1443 sum_epot(&(inputrec->opts), &(enerd->grpp), enerd->term);
1446 void do_force_cutsGROUP(FILE *fplog, t_commrec *cr,
1447 t_inputrec *inputrec,
1448 gmx_large_int_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
1449 gmx_localtop_t *top,
1451 gmx_groups_t *groups,
1452 matrix box, rvec x[], history_t *hist,
1456 gmx_enerdata_t *enerd, t_fcdata *fcd,
1457 real *lambda, t_graph *graph,
1458 t_forcerec *fr, gmx_vsite_t *vsite, rvec mu_tot,
1459 double t, FILE *field, gmx_edsam_t ed,
1460 gmx_bool bBornRadii,
1466 gmx_bool bSepDVDL, bStateChanged, bNS, bFillGrid, bCalcCGCM, bBS;
1467 gmx_bool bDoLongRangeNS, bDoForces, bDoPotential, bSepLRF;
1468 gmx_bool bDoAdressWF;
1470 rvec vzero, box_diag;
1471 real e, v, dvdlambda[efptNR];
1473 float cycles_pme, cycles_force;
1475 start = mdatoms->start;
1476 homenr = mdatoms->homenr;
1478 bSepDVDL = (fr->bSepDVDL && do_per_step(step, inputrec->nstlog));
1480 clear_mat(vir_force);
1484 pd_cg_range(cr, &cg0, &cg1);
1489 if (DOMAINDECOMP(cr))
1491 cg1 = cr->dd->ncg_tot;
1503 bStateChanged = (flags & GMX_FORCE_STATECHANGED);
1504 bNS = (flags & GMX_FORCE_NS) && (fr->bAllvsAll == FALSE);
1505 /* Should we update the long-range neighborlists at this step? */
1506 bDoLongRangeNS = fr->bTwinRange && bNS;
1507 /* Should we perform the long-range nonbonded evaluation inside the neighborsearching? */
1508 bFillGrid = (bNS && bStateChanged);
1509 bCalcCGCM = (bFillGrid && !DOMAINDECOMP(cr));
1510 bDoForces = (flags & GMX_FORCE_FORCES);
1511 bDoPotential = (flags & GMX_FORCE_ENERGY);
1512 bSepLRF = ((inputrec->nstcalclr > 1) && bDoForces &&
1513 (flags & GMX_FORCE_SEPLRF) && (flags & GMX_FORCE_DO_LR));
1515 /* should probably move this to the forcerec since it doesn't change */
1516 bDoAdressWF = ((fr->adress_type != eAdressOff));
1520 update_forcerec(fplog, fr, box);
1522 if (NEED_MUTOT(*inputrec))
1524 /* Calculate total (local) dipole moment in a temporary common array.
1525 * This makes it possible to sum them over nodes faster.
1527 calc_mu(start, homenr,
1528 x, mdatoms->chargeA, mdatoms->chargeB, mdatoms->nChargePerturbed,
1533 if (fr->ePBC != epbcNONE)
1535 /* Compute shift vectors every step,
1536 * because of pressure coupling or box deformation!
1538 if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
1540 calc_shifts(box, fr->shift_vec);
1545 put_charge_groups_in_box(fplog, cg0, cg1, fr->ePBC, box,
1546 &(top->cgs), x, fr->cg_cm);
1547 inc_nrnb(nrnb, eNR_CGCM, homenr);
1548 inc_nrnb(nrnb, eNR_RESETX, cg1-cg0);
1550 else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph)
1552 unshift_self(graph, box, x);
1557 calc_cgcm(fplog, cg0, cg1, &(top->cgs), x, fr->cg_cm);
1558 inc_nrnb(nrnb, eNR_CGCM, homenr);
1565 move_cgcm(fplog, cr, fr->cg_cm);
1569 pr_rvecs(debug, 0, "cgcm", fr->cg_cm, top->cgs.nr);
1574 if (!(cr->duty & DUTY_PME))
1576 /* Send particle coordinates to the pme nodes.
1577 * Since this is only implemented for domain decomposition
1578 * and domain decomposition does not use the graph,
1579 * we do not need to worry about shifting.
1582 wallcycle_start(wcycle, ewcPP_PMESENDX);
1584 bBS = (inputrec->nwall == 2);
1587 copy_mat(box, boxs);
1588 svmul(inputrec->wall_ewald_zfac, boxs[ZZ], boxs[ZZ]);
1591 gmx_pme_send_x(cr, bBS ? boxs : box, x,
1592 mdatoms->nChargePerturbed, lambda[efptCOUL],
1593 (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)), step);
1595 wallcycle_stop(wcycle, ewcPP_PMESENDX);
1597 #endif /* GMX_MPI */
1599 /* Communicate coordinates and sum dipole if necessary */
1602 wallcycle_start(wcycle, ewcMOVEX);
1603 if (DOMAINDECOMP(cr))
1605 dd_move_x(cr->dd, box, x);
1609 move_x(fplog, cr, GMX_LEFT, GMX_RIGHT, x, nrnb);
1611 wallcycle_stop(wcycle, ewcMOVEX);
1614 /* update adress weight beforehand */
1615 if (bStateChanged && bDoAdressWF)
1617 /* need pbc for adress weight calculation with pbc_dx */
1618 set_pbc(&pbc, inputrec->ePBC, box);
1619 if (fr->adress_site == eAdressSITEcog)
1621 update_adress_weights_cog(top->idef.iparams, top->idef.il, x, fr, mdatoms,
1622 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1624 else if (fr->adress_site == eAdressSITEcom)
1626 update_adress_weights_com(fplog, cg0, cg1, &(top->cgs), x, fr, mdatoms,
1627 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1629 else if (fr->adress_site == eAdressSITEatomatom)
1631 update_adress_weights_atom_per_atom(cg0, cg1, &(top->cgs), x, fr, mdatoms,
1632 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1636 update_adress_weights_atom(cg0, cg1, &(top->cgs), x, fr, mdatoms,
1637 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1641 if (NEED_MUTOT(*inputrec))
1648 gmx_sumd(2*DIM, mu, cr);
1650 for (i = 0; i < 2; i++)
1652 for (j = 0; j < DIM; j++)
1654 fr->mu_tot[i][j] = mu[i*DIM + j];
1658 if (fr->efep == efepNO)
1660 copy_rvec(fr->mu_tot[0], mu_tot);
1664 for (j = 0; j < DIM; j++)
1667 (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] + lambda[efptCOUL]*fr->mu_tot[1][j];
1672 /* Reset energies */
1673 reset_enerdata(&(inputrec->opts), fr, bNS, enerd, MASTER(cr));
1674 clear_rvecs(SHIFTS, fr->fshift);
1678 wallcycle_start(wcycle, ewcNS);
1680 if (graph && bStateChanged)
1682 /* Calculate intramolecular shift vectors to make molecules whole */
1683 mk_mshift(fplog, graph, fr->ePBC, box, x);
1686 /* Do the actual neighbour searching and if twin range electrostatics
1687 * also do the calculation of long range forces and energies.
1689 for (i = 0; i < efptNR; i++)
1693 ns(fplog, fr, x, box,
1694 groups, &(inputrec->opts), top, mdatoms,
1695 cr, nrnb, lambda, dvdlambda, &enerd->grpp, bFillGrid,
1699 fprintf(fplog, sepdvdlformat, "LR non-bonded", 0.0, dvdlambda);
1701 enerd->dvdl_lin[efptVDW] += dvdlambda[efptVDW];
1702 enerd->dvdl_lin[efptCOUL] += dvdlambda[efptCOUL];
1704 wallcycle_stop(wcycle, ewcNS);
1707 if (inputrec->implicit_solvent && bNS)
1709 make_gb_nblist(cr, inputrec->gb_algorithm, inputrec->rlist,
1710 x, box, fr, &top->idef, graph, fr->born);
1713 if (DOMAINDECOMP(cr))
1715 if (!(cr->duty & DUTY_PME))
1717 wallcycle_start(wcycle, ewcPPDURINGPME);
1718 dd_force_flop_start(cr->dd, nrnb);
1724 /* Enforced rotation has its own cycle counter that starts after the collective
1725 * coordinates have been communicated. It is added to ddCyclF to allow
1726 * for proper load-balancing */
1727 wallcycle_start(wcycle, ewcROT);
1728 do_rotation(cr, inputrec, box, x, t, step, wcycle, bNS);
1729 wallcycle_stop(wcycle, ewcROT);
1732 /* Start the force cycle counter.
1733 * This counter is stopped in do_forcelow_level.
1734 * No parallel communication should occur while this counter is running,
1735 * since that will interfere with the dynamic load balancing.
1737 wallcycle_start(wcycle, ewcFORCE);
1741 /* Reset forces for which the virial is calculated separately:
1742 * PME/Ewald forces if necessary */
1743 if (fr->bF_NoVirSum)
1745 if (flags & GMX_FORCE_VIRIAL)
1747 fr->f_novirsum = fr->f_novirsum_alloc;
1750 clear_rvecs(fr->f_novirsum_n, fr->f_novirsum);
1754 clear_rvecs(homenr, fr->f_novirsum+start);
1759 /* We are not calculating the pressure so we do not need
1760 * a separate array for forces that do not contribute
1767 /* Clear the short- and long-range forces */
1768 clear_rvecs(fr->natoms_force_constr, f);
1769 if (bSepLRF && do_per_step(step, inputrec->nstcalclr))
1771 clear_rvecs(fr->natoms_force_constr, fr->f_twin);
1774 clear_rvec(fr->vir_diag_posres);
1776 if (inputrec->ePull == epullCONSTRAINT)
1778 clear_pull_forces(inputrec->pull);
1781 /* update QMMMrec, if necessary */
1784 update_QMMMrec(cr, fr, x, mdatoms, box, top);
1787 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_POSRES].nr > 0)
1789 posres_wrapper(fplog, flags, bSepDVDL, inputrec, nrnb, top, box, x,
1790 f, enerd, lambda, fr);
1793 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_FBPOSRES].nr > 0)
1795 /* Flat-bottomed position restraints always require full pbc */
1796 if (!(bStateChanged && bDoAdressWF))
1798 set_pbc(&pbc, inputrec->ePBC, box);
1800 v = fbposres(top->idef.il[F_FBPOSRES].nr, top->idef.il[F_FBPOSRES].iatoms,
1801 top->idef.iparams_fbposres,
1802 (const rvec*)x, fr->f_novirsum, fr->vir_diag_posres,
1803 inputrec->ePBC == epbcNONE ? NULL : &pbc,
1804 fr->rc_scaling, fr->ePBC, fr->posres_com);
1805 enerd->term[F_FBPOSRES] += v;
1806 inc_nrnb(nrnb, eNR_FBPOSRES, top->idef.il[F_FBPOSRES].nr/2);
1809 /* Compute the bonded and non-bonded energies and optionally forces */
1810 do_force_lowlevel(fplog, step, fr, inputrec, &(top->idef),
1811 cr, nrnb, wcycle, mdatoms, &(inputrec->opts),
1812 x, hist, f, bSepLRF ? fr->f_twin : f, enerd, fcd, mtop, top, fr->born,
1813 &(top->atomtypes), bBornRadii, box,
1814 inputrec->fepvals, lambda,
1815 graph, &(top->excls), fr->mu_tot,
1821 if (do_per_step(step, inputrec->nstcalclr))
1823 /* Add the long range forces to the short range forces */
1824 for (i = 0; i < fr->natoms_force_constr; i++)
1826 rvec_add(fr->f_twin[i], f[i], f[i]);
1831 cycles_force = wallcycle_stop(wcycle, ewcFORCE);
1835 do_flood(cr, inputrec, x, f, ed, box, step, bNS);
1838 if (DOMAINDECOMP(cr))
1840 dd_force_flop_stop(cr->dd, nrnb);
1843 dd_cycles_add(cr->dd, cycles_force-cycles_pme, ddCyclF);
1849 if (IR_ELEC_FIELD(*inputrec))
1851 /* Compute forces due to electric field */
1852 calc_f_el(MASTER(cr) ? field : NULL,
1853 start, homenr, mdatoms->chargeA, x, fr->f_novirsum,
1854 inputrec->ex, inputrec->et, t);
1857 if (bDoAdressWF && fr->adress_icor == eAdressICThermoForce)
1859 /* Compute thermodynamic force in hybrid AdResS region */
1860 adress_thermo_force(start, homenr, &(top->cgs), x, fr->f_novirsum, fr, mdatoms,
1861 inputrec->ePBC == epbcNONE ? NULL : &pbc);
1864 /* Communicate the forces */
1867 wallcycle_start(wcycle, ewcMOVEF);
1868 if (DOMAINDECOMP(cr))
1870 dd_move_f(cr->dd, f, fr->fshift);
1871 /* Do we need to communicate the separate force array
1872 * for terms that do not contribute to the single sum virial?
1873 * Position restraints and electric fields do not introduce
1874 * inter-cg forces, only full electrostatics methods do.
1875 * When we do not calculate the virial, fr->f_novirsum = f,
1876 * so we have already communicated these forces.
1878 if (EEL_FULL(fr->eeltype) && cr->dd->n_intercg_excl &&
1879 (flags & GMX_FORCE_VIRIAL))
1881 dd_move_f(cr->dd, fr->f_novirsum, NULL);
1885 /* We should not update the shift forces here,
1886 * since f_twin is already included in f.
1888 dd_move_f(cr->dd, fr->f_twin, NULL);
1893 pd_move_f(cr, f, nrnb);
1896 pd_move_f(cr, fr->f_twin, nrnb);
1899 wallcycle_stop(wcycle, ewcMOVEF);
1902 /* If we have NoVirSum forces, but we do not calculate the virial,
1903 * we sum fr->f_novirum=f later.
1905 if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1907 wallcycle_start(wcycle, ewcVSITESPREAD);
1908 spread_vsite_f(fplog, vsite, x, f, fr->fshift, FALSE, NULL, nrnb,
1909 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1910 wallcycle_stop(wcycle, ewcVSITESPREAD);
1914 wallcycle_start(wcycle, ewcVSITESPREAD);
1915 spread_vsite_f(fplog, vsite, x, fr->f_twin, NULL, FALSE, NULL,
1917 &top->idef, fr->ePBC, fr->bMolPBC, graph, box, cr);
1918 wallcycle_stop(wcycle, ewcVSITESPREAD);
1922 if (flags & GMX_FORCE_VIRIAL)
1924 /* Calculation of the virial must be done after vsites! */
1925 calc_virial(fplog, mdatoms->start, mdatoms->homenr, x, f,
1926 vir_force, graph, box, nrnb, fr, inputrec->ePBC);
1930 if (inputrec->ePull == epullUMBRELLA || inputrec->ePull == epullCONST_F)
1932 pull_potential_wrapper(fplog, bSepDVDL, cr, inputrec, box, x,
1933 f, vir_force, mdatoms, enerd, lambda, t);
1936 /* Add the forces from enforced rotation potentials (if any) */
1939 wallcycle_start(wcycle, ewcROTadd);
1940 enerd->term[F_COM_PULL] += add_rot_forces(inputrec->rot, f, cr, step, t);
1941 wallcycle_stop(wcycle, ewcROTadd);
1944 if (PAR(cr) && !(cr->duty & DUTY_PME))
1946 /* In case of node-splitting, the PP nodes receive the long-range
1947 * forces, virial and energy from the PME nodes here.
1949 pme_receive_force_ener(fplog, bSepDVDL, cr, wcycle, enerd, fr);
1954 post_process_forces(fplog, cr, step, nrnb, wcycle,
1955 top, box, x, f, vir_force, mdatoms, graph, fr, vsite,
1959 /* Sum the potential energy terms from group contributions */
1960 sum_epot(&(inputrec->opts), &(enerd->grpp), enerd->term);
1963 void do_force(FILE *fplog, t_commrec *cr,
1964 t_inputrec *inputrec,
1965 gmx_large_int_t step, t_nrnb *nrnb, gmx_wallcycle_t wcycle,
1966 gmx_localtop_t *top,
1968 gmx_groups_t *groups,
1969 matrix box, rvec x[], history_t *hist,
1973 gmx_enerdata_t *enerd, t_fcdata *fcd,
1974 real *lambda, t_graph *graph,
1976 gmx_vsite_t *vsite, rvec mu_tot,
1977 double t, FILE *field, gmx_edsam_t ed,
1978 gmx_bool bBornRadii,
1981 /* modify force flag if not doing nonbonded */
1982 if (!fr->bNonbonded)
1984 flags &= ~GMX_FORCE_NONBONDED;
1987 switch (inputrec->cutoff_scheme)
1990 do_force_cutsVERLET(fplog, cr, inputrec,
2006 do_force_cutsGROUP(fplog, cr, inputrec,
2021 gmx_incons("Invalid cut-off scheme passed!");
2026 void do_constrain_first(FILE *fplog, gmx_constr_t constr,
2027 t_inputrec *ir, t_mdatoms *md,
2028 t_state *state, rvec *f,
2029 t_graph *graph, t_commrec *cr, t_nrnb *nrnb,
2030 t_forcerec *fr, gmx_localtop_t *top, tensor shake_vir)
2032 int i, m, start, end;
2033 gmx_large_int_t step;
2034 real dt = ir->delta_t;
2038 snew(savex, state->natoms);
2041 end = md->homenr + start;
2045 fprintf(debug, "vcm: start=%d, homenr=%d, end=%d\n",
2046 start, md->homenr, end);
2048 /* Do a first constrain to reset particles... */
2049 step = ir->init_step;
2052 char buf[STEPSTRSIZE];
2053 fprintf(fplog, "\nConstraining the starting coordinates (step %s)\n",
2054 gmx_step_str(step, buf));
2058 /* constrain the current position */
2059 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
2060 ir, NULL, cr, step, 0, md,
2061 state->x, state->x, NULL,
2062 fr->bMolPBC, state->box,
2063 state->lambda[efptBONDED], &dvdl_dum,
2064 NULL, NULL, nrnb, econqCoord,
2065 ir->epc == epcMTTK, state->veta, state->veta);
2068 /* constrain the inital velocity, and save it */
2069 /* also may be useful if we need the ekin from the halfstep for velocity verlet */
2070 /* might not yet treat veta correctly */
2071 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
2072 ir, NULL, cr, step, 0, md,
2073 state->x, state->v, state->v,
2074 fr->bMolPBC, state->box,
2075 state->lambda[efptBONDED], &dvdl_dum,
2076 NULL, NULL, nrnb, econqVeloc,
2077 ir->epc == epcMTTK, state->veta, state->veta);
2079 /* constrain the inital velocities at t-dt/2 */
2080 if (EI_STATE_VELOCITY(ir->eI) && ir->eI != eiVV)
2082 for (i = start; (i < end); i++)
2084 for (m = 0; (m < DIM); m++)
2086 /* Reverse the velocity */
2087 state->v[i][m] = -state->v[i][m];
2088 /* Store the position at t-dt in buf */
2089 savex[i][m] = state->x[i][m] + dt*state->v[i][m];
2092 /* Shake the positions at t=-dt with the positions at t=0
2093 * as reference coordinates.
2097 char buf[STEPSTRSIZE];
2098 fprintf(fplog, "\nConstraining the coordinates at t0-dt (step %s)\n",
2099 gmx_step_str(step, buf));
2102 constrain(NULL, TRUE, FALSE, constr, &(top->idef),
2103 ir, NULL, cr, step, -1, md,
2104 state->x, savex, NULL,
2105 fr->bMolPBC, state->box,
2106 state->lambda[efptBONDED], &dvdl_dum,
2107 state->v, NULL, nrnb, econqCoord,
2108 ir->epc == epcMTTK, state->veta, state->veta);
2110 for (i = start; i < end; i++)
2112 for (m = 0; m < DIM; m++)
2114 /* Re-reverse the velocities */
2115 state->v[i][m] = -state->v[i][m];
2122 void calc_enervirdiff(FILE *fplog, int eDispCorr, t_forcerec *fr)
2124 double eners[2], virs[2], enersum, virsum, y0, f, g, h;
2125 double r0, r1, r, rc3, rc9, ea, eb, ec, pa, pb, pc, pd;
2126 double invscale, invscale2, invscale3;
2127 int ri0, ri1, ri, i, offstart, offset;
2128 real scale, *vdwtab, tabfactor, tmp;
2130 fr->enershiftsix = 0;
2131 fr->enershifttwelve = 0;
2132 fr->enerdiffsix = 0;
2133 fr->enerdifftwelve = 0;
2135 fr->virdifftwelve = 0;
2137 if (eDispCorr != edispcNO)
2139 for (i = 0; i < 2; i++)
2144 if ((fr->vdwtype == evdwSWITCH) || (fr->vdwtype == evdwSHIFT))
2146 if (fr->rvdw_switch == 0)
2149 "With dispersion correction rvdw-switch can not be zero "
2150 "for vdw-type = %s", evdw_names[fr->vdwtype]);
2153 scale = fr->nblists[0].table_elec_vdw.scale;
2154 vdwtab = fr->nblists[0].table_vdw.data;
2156 /* Round the cut-offs to exact table values for precision */
2157 ri0 = floor(fr->rvdw_switch*scale);
2158 ri1 = ceil(fr->rvdw*scale);
2164 if (fr->vdwtype == evdwSHIFT)
2166 /* Determine the constant energy shift below rvdw_switch.
2167 * Table has a scale factor since we have scaled it down to compensate
2168 * for scaling-up c6/c12 with the derivative factors to save flops in analytical kernels.
2170 fr->enershiftsix = (real)(-1.0/(rc3*rc3)) - 6.0*vdwtab[8*ri0];
2171 fr->enershifttwelve = (real)( 1.0/(rc9*rc3)) - 12.0*vdwtab[8*ri0 + 4];
2173 /* Add the constant part from 0 to rvdw_switch.
2174 * This integration from 0 to rvdw_switch overcounts the number
2175 * of interactions by 1, as it also counts the self interaction.
2176 * We will correct for this later.
2178 eners[0] += 4.0*M_PI*fr->enershiftsix*rc3/3.0;
2179 eners[1] += 4.0*M_PI*fr->enershifttwelve*rc3/3.0;
2181 invscale = 1.0/(scale);
2182 invscale2 = invscale*invscale;
2183 invscale3 = invscale*invscale2;
2185 /* following summation derived from cubic spline definition,
2186 Numerical Recipies in C, second edition, p. 113-116. Exact
2187 for the cubic spline. We first calculate the negative of
2188 the energy from rvdw to rvdw_switch, assuming that g(r)=1,
2189 and then add the more standard, abrupt cutoff correction to
2190 that result, yielding the long-range correction for a
2191 switched function. We perform both the pressure and energy
2192 loops at the same time for simplicity, as the computational
2195 for (i = 0; i < 2; i++)
2197 enersum = 0.0; virsum = 0.0;
2201 /* Since the dispersion table has been scaled down a factor 6.0 and the repulsion
2202 * a factor 12.0 to compensate for the c6/c12 parameters inside nbfp[] being scaled
2203 * up (to save flops in kernels), we need to correct for this.
2212 for (ri = ri0; ri < ri1; ri++)
2216 eb = 2.0*invscale2*r;
2220 pb = 3.0*invscale2*r;
2221 pc = 3.0*invscale*r*r;
2224 /* this "8" is from the packing in the vdwtab array - perhaps should be #define'ed? */
2225 offset = 8*ri + offstart;
2226 y0 = vdwtab[offset];
2227 f = vdwtab[offset+1];
2228 g = vdwtab[offset+2];
2229 h = vdwtab[offset+3];
2231 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);
2232 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);
2235 enersum *= 4.0*M_PI*tabfactor;
2236 virsum *= 4.0*M_PI*tabfactor;
2237 eners[i] -= enersum;
2241 /* now add the correction for rvdw_switch to infinity */
2242 eners[0] += -4.0*M_PI/(3.0*rc3);
2243 eners[1] += 4.0*M_PI/(9.0*rc9);
2244 virs[0] += 8.0*M_PI/rc3;
2245 virs[1] += -16.0*M_PI/(3.0*rc9);
2247 else if ((fr->vdwtype == evdwCUT) || (fr->vdwtype == evdwUSER))
2249 if (fr->vdwtype == evdwUSER && fplog)
2252 "WARNING: using dispersion correction with user tables\n");
2254 rc3 = fr->rvdw*fr->rvdw*fr->rvdw;
2256 /* Contribution beyond the cut-off */
2257 eners[0] += -4.0*M_PI/(3.0*rc3);
2258 eners[1] += 4.0*M_PI/(9.0*rc9);
2259 if (fr->vdw_modifier == eintmodPOTSHIFT)
2261 /* Contribution within the cut-off */
2262 eners[0] += -4.0*M_PI/(3.0*rc3);
2263 eners[1] += 4.0*M_PI/(3.0*rc9);
2265 /* Contribution beyond the cut-off */
2266 virs[0] += 8.0*M_PI/rc3;
2267 virs[1] += -16.0*M_PI/(3.0*rc9);
2272 "Dispersion correction is not implemented for vdw-type = %s",
2273 evdw_names[fr->vdwtype]);
2275 fr->enerdiffsix = eners[0];
2276 fr->enerdifftwelve = eners[1];
2277 /* The 0.5 is due to the Gromacs definition of the virial */
2278 fr->virdiffsix = 0.5*virs[0];
2279 fr->virdifftwelve = 0.5*virs[1];
2283 void calc_dispcorr(FILE *fplog, t_inputrec *ir, t_forcerec *fr,
2284 gmx_large_int_t step, int natoms,
2285 matrix box, real lambda, tensor pres, tensor virial,
2286 real *prescorr, real *enercorr, real *dvdlcorr)
2288 gmx_bool bCorrAll, bCorrPres;
2289 real dvdlambda, invvol, dens, ninter, avcsix, avctwelve, enerdiff, svir = 0, spres = 0;
2299 if (ir->eDispCorr != edispcNO)
2301 bCorrAll = (ir->eDispCorr == edispcAllEner ||
2302 ir->eDispCorr == edispcAllEnerPres);
2303 bCorrPres = (ir->eDispCorr == edispcEnerPres ||
2304 ir->eDispCorr == edispcAllEnerPres);
2306 invvol = 1/det(box);
2309 /* Only correct for the interactions with the inserted molecule */
2310 dens = (natoms - fr->n_tpi)*invvol;
2315 dens = natoms*invvol;
2316 ninter = 0.5*natoms;
2319 if (ir->efep == efepNO)
2321 avcsix = fr->avcsix[0];
2322 avctwelve = fr->avctwelve[0];
2326 avcsix = (1 - lambda)*fr->avcsix[0] + lambda*fr->avcsix[1];
2327 avctwelve = (1 - lambda)*fr->avctwelve[0] + lambda*fr->avctwelve[1];
2330 enerdiff = ninter*(dens*fr->enerdiffsix - fr->enershiftsix);
2331 *enercorr += avcsix*enerdiff;
2333 if (ir->efep != efepNO)
2335 dvdlambda += (fr->avcsix[1] - fr->avcsix[0])*enerdiff;
2339 enerdiff = ninter*(dens*fr->enerdifftwelve - fr->enershifttwelve);
2340 *enercorr += avctwelve*enerdiff;
2341 if (fr->efep != efepNO)
2343 dvdlambda += (fr->avctwelve[1] - fr->avctwelve[0])*enerdiff;
2349 svir = ninter*dens*avcsix*fr->virdiffsix/3.0;
2350 if (ir->eDispCorr == edispcAllEnerPres)
2352 svir += ninter*dens*avctwelve*fr->virdifftwelve/3.0;
2354 /* The factor 2 is because of the Gromacs virial definition */
2355 spres = -2.0*invvol*svir*PRESFAC;
2357 for (m = 0; m < DIM; m++)
2359 virial[m][m] += svir;
2360 pres[m][m] += spres;
2365 /* Can't currently control when it prints, for now, just print when degugging */
2370 fprintf(debug, "Long Range LJ corr.: <C6> %10.4e, <C12> %10.4e\n",
2376 "Long Range LJ corr.: Epot %10g, Pres: %10g, Vir: %10g\n",
2377 *enercorr, spres, svir);
2381 fprintf(debug, "Long Range LJ corr.: Epot %10g\n", *enercorr);
2385 if (fr->bSepDVDL && do_per_step(step, ir->nstlog))
2387 fprintf(fplog, sepdvdlformat, "Dispersion correction",
2388 *enercorr, dvdlambda);
2390 if (fr->efep != efepNO)
2392 *dvdlcorr += dvdlambda;
2397 void do_pbc_first(FILE *fplog, matrix box, t_forcerec *fr,
2398 t_graph *graph, rvec x[])
2402 fprintf(fplog, "Removing pbc first time\n");
2404 calc_shifts(box, fr->shift_vec);
2407 mk_mshift(fplog, graph, fr->ePBC, box, x);
2410 p_graph(debug, "do_pbc_first 1", graph);
2412 shift_self(graph, box, x);
2413 /* By doing an extra mk_mshift the molecules that are broken
2414 * because they were e.g. imported from another software
2415 * will be made whole again. Such are the healing powers
2418 mk_mshift(fplog, graph, fr->ePBC, box, x);
2421 p_graph(debug, "do_pbc_first 2", graph);
2426 fprintf(fplog, "Done rmpbc\n");
2430 static void low_do_pbc_mtop(FILE *fplog, int ePBC, matrix box,
2431 gmx_mtop_t *mtop, rvec x[],
2436 gmx_molblock_t *molb;
2438 if (bFirst && fplog)
2440 fprintf(fplog, "Removing pbc first time\n");
2445 for (mb = 0; mb < mtop->nmolblock; mb++)
2447 molb = &mtop->molblock[mb];
2448 if (molb->natoms_mol == 1 ||
2449 (!bFirst && mtop->moltype[molb->type].cgs.nr == 1))
2451 /* Just one atom or charge group in the molecule, no PBC required */
2452 as += molb->nmol*molb->natoms_mol;
2456 /* Pass NULL iso fplog to avoid graph prints for each molecule type */
2457 mk_graph_ilist(NULL, mtop->moltype[molb->type].ilist,
2458 0, molb->natoms_mol, FALSE, FALSE, graph);
2460 for (mol = 0; mol < molb->nmol; mol++)
2462 mk_mshift(fplog, graph, ePBC, box, x+as);
2464 shift_self(graph, box, x+as);
2465 /* The molecule is whole now.
2466 * We don't need the second mk_mshift call as in do_pbc_first,
2467 * since we no longer need this graph.
2470 as += molb->natoms_mol;
2478 void do_pbc_first_mtop(FILE *fplog, int ePBC, matrix box,
2479 gmx_mtop_t *mtop, rvec x[])
2481 low_do_pbc_mtop(fplog, ePBC, box, mtop, x, TRUE);
2484 void do_pbc_mtop(FILE *fplog, int ePBC, matrix box,
2485 gmx_mtop_t *mtop, rvec x[])
2487 low_do_pbc_mtop(fplog, ePBC, box, mtop, x, FALSE);
2490 void finish_run(FILE *fplog, t_commrec *cr, const char *confout,
2491 t_inputrec *inputrec,
2492 t_nrnb nrnb[], gmx_wallcycle_t wcycle,
2493 gmx_runtime_t *runtime,
2494 wallclock_gpu_t *gputimes,
2496 gmx_bool bWriteStat)
2499 t_nrnb *nrnb_tot = NULL;
2503 wallcycle_sum(cr, wcycle);
2509 MPI_Allreduce(nrnb->n, nrnb_tot->n, eNRNB, MPI_DOUBLE, MPI_SUM,
2510 cr->mpi_comm_mysim);
2518 #if defined(GMX_MPI) && !defined(GMX_THREAD_MPI)
2521 /* reduce nodetime over all MPI processes in the current simulation */
2523 MPI_Allreduce(&runtime->proctime, &sum, 1, MPI_DOUBLE, MPI_SUM,
2524 cr->mpi_comm_mysim);
2525 runtime->proctime = sum;
2531 print_flop(fplog, nrnb_tot, &nbfs, &mflop);
2538 if ((cr->duty & DUTY_PP) && DOMAINDECOMP(cr))
2540 print_dd_statistics(cr, inputrec, fplog);
2552 snew(nrnb_all, cr->nnodes);
2553 nrnb_all[0] = *nrnb;
2554 for (s = 1; s < cr->nnodes; s++)
2556 MPI_Recv(nrnb_all[s].n, eNRNB, MPI_DOUBLE, s, 0,
2557 cr->mpi_comm_mysim, &stat);
2559 pr_load(fplog, cr, nrnb_all);
2564 MPI_Send(nrnb->n, eNRNB, MPI_DOUBLE, MASTERRANK(cr), 0,
2565 cr->mpi_comm_mysim);
2572 wallcycle_print(fplog, cr->nnodes, cr->npmenodes, runtime->realtime,
2575 if (EI_DYNAMICS(inputrec->eI))
2577 delta_t = inputrec->delta_t;
2586 print_perf(fplog, runtime->proctime, runtime->realtime,
2587 cr->nnodes-cr->npmenodes,
2588 runtime->nsteps_done, delta_t, nbfs, mflop,
2593 print_perf(stderr, runtime->proctime, runtime->realtime,
2594 cr->nnodes-cr->npmenodes,
2595 runtime->nsteps_done, delta_t, nbfs, mflop,
2601 extern void initialize_lambdas(FILE *fplog, t_inputrec *ir, int *fep_state, real *lambda, double *lam0)
2603 /* this function works, but could probably use a logic rewrite to keep all the different
2604 types of efep straight. */
2607 t_lambda *fep = ir->fepvals;
2609 if ((ir->efep == efepNO) && (ir->bSimTemp == FALSE))
2611 for (i = 0; i < efptNR; i++)
2623 *fep_state = fep->init_fep_state; /* this might overwrite the checkpoint
2624 if checkpoint is set -- a kludge is in for now
2626 for (i = 0; i < efptNR; i++)
2628 /* overwrite lambda state with init_lambda for now for backwards compatibility */
2629 if (fep->init_lambda >= 0) /* if it's -1, it was never initializd */
2631 lambda[i] = fep->init_lambda;
2634 lam0[i] = lambda[i];
2639 lambda[i] = fep->all_lambda[i][*fep_state];
2642 lam0[i] = lambda[i];
2648 /* need to rescale control temperatures to match current state */
2649 for (i = 0; i < ir->opts.ngtc; i++)
2651 if (ir->opts.ref_t[i] > 0)
2653 ir->opts.ref_t[i] = ir->simtempvals->temperatures[*fep_state];
2659 /* Send to the log the information on the current lambdas */
2662 fprintf(fplog, "Initial vector of lambda components:[ ");
2663 for (i = 0; i < efptNR; i++)
2665 fprintf(fplog, "%10.4f ", lambda[i]);
2667 fprintf(fplog, "]\n");
2673 void init_md(FILE *fplog,
2674 t_commrec *cr, t_inputrec *ir, const output_env_t oenv,
2675 double *t, double *t0,
2676 real *lambda, int *fep_state, double *lam0,
2677 t_nrnb *nrnb, gmx_mtop_t *mtop,
2679 int nfile, const t_filenm fnm[],
2680 gmx_mdoutf_t **outf, t_mdebin **mdebin,
2681 tensor force_vir, tensor shake_vir, rvec mu_tot,
2682 gmx_bool *bSimAnn, t_vcm **vcm, t_state *state, unsigned long Flags)
2687 /* Initial values */
2688 *t = *t0 = ir->init_t;
2691 for (i = 0; i < ir->opts.ngtc; i++)
2693 /* set bSimAnn if any group is being annealed */
2694 if (ir->opts.annealing[i] != eannNO)
2701 update_annealing_target_temp(&(ir->opts), ir->init_t);
2704 /* Initialize lambda variables */
2705 initialize_lambdas(fplog, ir, fep_state, lambda, lam0);
2709 *upd = init_update(fplog, ir);
2715 *vcm = init_vcm(fplog, &mtop->groups, ir);
2718 if (EI_DYNAMICS(ir->eI) && !(Flags & MD_APPENDFILES))
2720 if (ir->etc == etcBERENDSEN)
2722 please_cite(fplog, "Berendsen84a");
2724 if (ir->etc == etcVRESCALE)
2726 please_cite(fplog, "Bussi2007a");
2734 *outf = init_mdoutf(nfile, fnm, Flags, cr, ir, oenv);
2736 *mdebin = init_mdebin((Flags & MD_APPENDFILES) ? NULL : (*outf)->fp_ene,
2737 mtop, ir, (*outf)->fp_dhdl);
2742 please_cite(fplog, "Fritsch12");
2743 please_cite(fplog, "Junghans10");
2745 /* Initiate variables */
2746 clear_mat(force_vir);
2747 clear_mat(shake_vir);