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_search.h"
90 #include "nbnxn_kernels/nbnxn_kernel_ref.h"
91 #include "nbnxn_kernels/nbnxn_kernel_x86_simd128.h"
92 #include "nbnxn_kernels/nbnxn_kernel_x86_simd256.h"
93 #include "nbnxn_kernels/nbnxn_kernel_gpu_ref.h"
105 #include "nbnxn_cuda_data_mgmt.h"
106 #include "nbnxn_cuda/nbnxn_cuda.h"
109 typedef struct gmx_timeprint {
114 /* Portable version of ctime_r implemented in src/gmxlib/string2.c, but we do not want it declared in public installed headers */
116 gmx_ctime_r(const time_t *clock,char *buf, int n);
122 #ifdef HAVE_GETTIMEOFDAY
126 gettimeofday(&t,NULL);
128 seconds = (double) t.tv_sec + 1e-6*(double)t.tv_usec;
134 seconds = time(NULL);
141 #define difftime(end,start) ((double)(end)-(double)(start))
143 void print_time(FILE *out,gmx_runtime_t *runtime,gmx_large_int_t step,
144 t_inputrec *ir, t_commrec *cr)
147 char timebuf[STRLEN];
151 #ifndef GMX_THREAD_MPI
157 fprintf(out,"step %s",gmx_step_str(step,buf));
158 if ((step >= ir->nstlist))
160 runtime->last = gmx_gettime();
161 dt = difftime(runtime->last,runtime->real);
162 runtime->time_per_step = dt/(step - ir->init_step + 1);
164 dt = (ir->nsteps + ir->init_step - step)*runtime->time_per_step;
170 finish = (time_t) (runtime->last + dt);
171 gmx_ctime_r(&finish,timebuf,STRLEN);
172 sprintf(buf,"%s",timebuf);
173 buf[strlen(buf)-1]='\0';
174 fprintf(out,", will finish %s",buf);
177 fprintf(out,", remaining runtime: %5d s ",(int)dt);
181 fprintf(out," performance: %.1f ns/day ",
182 ir->delta_t/1000*24*60*60/runtime->time_per_step);
185 #ifndef GMX_THREAD_MPI
199 static double set_proctime(gmx_runtime_t *runtime)
205 prev = runtime->proc;
206 runtime->proc = dclock();
208 diff = runtime->proc - prev;
212 prev = runtime->proc;
213 runtime->proc = clock();
215 diff = (double)(runtime->proc - prev)/(double)CLOCKS_PER_SEC;
219 /* The counter has probably looped, ignore this data */
226 void runtime_start(gmx_runtime_t *runtime)
228 runtime->real = gmx_gettime();
230 set_proctime(runtime);
231 runtime->realtime = 0;
232 runtime->proctime = 0;
234 runtime->time_per_step = 0;
237 void runtime_end(gmx_runtime_t *runtime)
243 runtime->proctime += set_proctime(runtime);
244 runtime->realtime = now - runtime->real;
248 void runtime_upd_proc(gmx_runtime_t *runtime)
250 runtime->proctime += set_proctime(runtime);
253 void print_date_and_time(FILE *fplog,int nodeid,const char *title,
254 const gmx_runtime_t *runtime)
257 char timebuf[STRLEN];
258 char time_string[STRLEN];
265 tmptime = (time_t) runtime->real;
266 gmx_ctime_r(&tmptime,timebuf,STRLEN);
270 tmptime = (time_t) gmx_gettime();
271 gmx_ctime_r(&tmptime,timebuf,STRLEN);
273 for(i=0; timebuf[i]>=' '; i++)
275 time_string[i]=timebuf[i];
279 fprintf(fplog,"%s on node %d %s\n",title,nodeid,time_string);
283 static void sum_forces(int start,int end,rvec f[],rvec flr[])
288 pr_rvecs(debug,0,"fsr",f+start,end-start);
289 pr_rvecs(debug,0,"flr",flr+start,end-start);
291 for(i=start; (i<end); i++)
292 rvec_inc(f[i],flr[i]);
296 * calc_f_el calculates forces due to an electric field.
298 * force is kJ mol^-1 nm^-1 = e * kJ mol^-1 nm^-1 / e
300 * Et[] contains the parameters for the time dependent
301 * part of the field (not yet used).
302 * Ex[] contains the parameters for
303 * the spatial dependent part of the field. You can have cool periodic
304 * fields in principle, but only a constant field is supported
306 * The function should return the energy due to the electric field
307 * (if any) but for now returns 0.
310 * There can be problems with the virial.
311 * Since the field is not self-consistent this is unavoidable.
312 * For neutral molecules the virial is correct within this approximation.
313 * For neutral systems with many charged molecules the error is small.
314 * But for systems with a net charge or a few charged molecules
315 * the error can be significant when the field is high.
316 * Solution: implement a self-consitent electric field into PME.
318 static void calc_f_el(FILE *fp,int start,int homenr,
319 real charge[],rvec x[],rvec f[],
320 t_cosines Ex[],t_cosines Et[],double t)
326 for(m=0; (m<DIM); m++)
333 Ext[m] = cos(Et[m].a[0]*(t-t0))*exp(-sqr(t-t0)/(2.0*sqr(Et[m].a[2])));
337 Ext[m] = cos(Et[m].a[0]*t);
346 /* Convert the field strength from V/nm to MD-units */
347 Ext[m] *= Ex[m].a[0]*FIELDFAC;
348 for(i=start; (i<start+homenr); i++)
349 f[i][m] += charge[i]*Ext[m];
358 fprintf(fp,"%10g %10g %10g %10g #FIELD\n",t,
359 Ext[XX]/FIELDFAC,Ext[YY]/FIELDFAC,Ext[ZZ]/FIELDFAC);
363 static void calc_virial(FILE *fplog,int start,int homenr,rvec x[],rvec f[],
364 tensor vir_part,t_graph *graph,matrix box,
365 t_nrnb *nrnb,const t_forcerec *fr,int ePBC)
370 /* The short-range virial from surrounding boxes */
372 calc_vir(fplog,SHIFTS,fr->shift_vec,fr->fshift,vir_part,ePBC==epbcSCREW,box);
373 inc_nrnb(nrnb,eNR_VIRIAL,SHIFTS);
375 /* Calculate partial virial, for local atoms only, based on short range.
376 * Total virial is computed in global_stat, called from do_md
378 f_calc_vir(fplog,start,start+homenr,x,f,vir_part,graph,box);
379 inc_nrnb(nrnb,eNR_VIRIAL,homenr);
381 /* Add position restraint contribution */
382 for(i=0; i<DIM; i++) {
383 vir_part[i][i] += fr->vir_diag_posres[i];
386 /* Add wall contribution */
387 for(i=0; i<DIM; i++) {
388 vir_part[i][ZZ] += fr->vir_wall_z[i];
392 pr_rvecs(debug,0,"vir_part",vir_part,DIM);
395 static void posres_wrapper(FILE *fplog,
403 gmx_enerdata_t *enerd,
411 /* Position restraints always require full pbc */
412 set_pbc(&pbc,ir->ePBC,box);
414 v = posres(top->idef.il[F_POSRES].nr,top->idef.il[F_POSRES].iatoms,
415 top->idef.iparams_posres,
416 (const rvec*)x,fr->f_novirsum,fr->vir_diag_posres,
417 ir->ePBC==epbcNONE ? NULL : &pbc,
418 lambda[efptRESTRAINT],&dvdl,
419 fr->rc_scaling,fr->ePBC,fr->posres_com,fr->posres_comB);
422 fprintf(fplog,sepdvdlformat,
423 interaction_function[F_POSRES].longname,v,dvdl);
425 enerd->term[F_POSRES] += v;
426 /* If just the force constant changes, the FEP term is linear,
427 * but if k changes, it is not.
429 enerd->dvdl_nonlin[efptRESTRAINT] += dvdl;
430 inc_nrnb(nrnb,eNR_POSRES,top->idef.il[F_POSRES].nr/2);
432 if ((ir->fepvals->n_lambda > 0) && (flags & GMX_FORCE_DHDL))
434 for(i=0; i<enerd->n_lambda; i++)
436 real dvdl_dum,lambda_dum;
438 lambda_dum = (i==0 ? lambda[efptRESTRAINT] : ir->fepvals->all_lambda[efptRESTRAINT][i-1]);
439 v = posres(top->idef.il[F_POSRES].nr,top->idef.il[F_POSRES].iatoms,
440 top->idef.iparams_posres,
441 (const rvec*)x,NULL,NULL,
442 ir->ePBC==epbcNONE ? NULL : &pbc,lambda_dum,&dvdl,
443 fr->rc_scaling,fr->ePBC,fr->posres_com,fr->posres_comB);
444 enerd->enerpart_lambda[i] += v;
449 static void pull_potential_wrapper(FILE *fplog,
457 gmx_enerdata_t *enerd,
464 /* Calculate the center of mass forces, this requires communication,
465 * which is why pull_potential is called close to other communication.
466 * The virial contribution is calculated directly,
467 * which is why we call pull_potential after calc_virial.
469 set_pbc(&pbc,ir->ePBC,box);
471 enerd->term[F_COM_PULL] +=
472 pull_potential(ir->ePull,ir->pull,mdatoms,&pbc,
473 cr,t,lambda[efptRESTRAINT],x,f,vir_force,&dvdl);
476 fprintf(fplog,sepdvdlformat,"Com pull",enerd->term[F_COM_PULL],dvdl);
478 enerd->dvdl_lin[efptRESTRAINT] += dvdl;
481 static void pme_receive_force_ener(FILE *fplog,
484 gmx_wallcycle_t wcycle,
485 gmx_enerdata_t *enerd,
489 float cycles_ppdpme,cycles_seppme;
491 cycles_ppdpme = wallcycle_stop(wcycle,ewcPPDURINGPME);
492 dd_cycles_add(cr->dd,cycles_ppdpme,ddCyclPPduringPME);
494 /* In case of node-splitting, the PP nodes receive the long-range
495 * forces, virial and energy from the PME nodes here.
497 wallcycle_start(wcycle,ewcPP_PMEWAITRECVF);
499 gmx_pme_receive_f(cr,fr->f_novirsum,fr->vir_el_recip,&e,&dvdl,
503 fprintf(fplog,sepdvdlformat,"PME mesh",e,dvdl);
505 enerd->term[F_COUL_RECIP] += e;
506 enerd->dvdl_lin[efptCOUL] += dvdl;
509 dd_cycles_add(cr->dd,cycles_seppme,ddCyclPME);
511 wallcycle_stop(wcycle,ewcPP_PMEWAITRECVF);
514 static void print_large_forces(FILE *fp,t_mdatoms *md,t_commrec *cr,
515 gmx_large_int_t step,real pforce,rvec *x,rvec *f)
519 char buf[STEPSTRSIZE];
522 for(i=md->start; i<md->start+md->homenr; i++) {
524 /* We also catch NAN, if the compiler does not optimize this away. */
525 if (fn2 >= pf2 || fn2 != fn2) {
526 fprintf(fp,"step %s atom %6d x %8.3f %8.3f %8.3f force %12.5e\n",
527 gmx_step_str(step,buf),
528 ddglatnr(cr->dd,i),x[i][XX],x[i][YY],x[i][ZZ],sqrt(fn2));
533 static void post_process_forces(FILE *fplog,
535 gmx_large_int_t step,
536 t_nrnb *nrnb,gmx_wallcycle_t wcycle,
543 t_forcerec *fr,gmx_vsite_t *vsite,
550 /* Spread the mesh force on virtual sites to the other particles...
551 * This is parallellized. MPI communication is performed
552 * if the constructing atoms aren't local.
554 wallcycle_start(wcycle,ewcVSITESPREAD);
555 spread_vsite_f(fplog,vsite,x,fr->f_novirsum,NULL,
556 (flags & GMX_FORCE_VIRIAL),fr->vir_el_recip,
558 &top->idef,fr->ePBC,fr->bMolPBC,graph,box,cr);
559 wallcycle_stop(wcycle,ewcVSITESPREAD);
561 if (flags & GMX_FORCE_VIRIAL)
563 /* Now add the forces, this is local */
566 sum_forces(0,fr->f_novirsum_n,f,fr->f_novirsum);
570 sum_forces(mdatoms->start,mdatoms->start+mdatoms->homenr,
573 if (EEL_FULL(fr->eeltype))
575 /* Add the mesh contribution to the virial */
576 m_add(vir_force,fr->vir_el_recip,vir_force);
580 pr_rvecs(debug,0,"vir_force",vir_force,DIM);
585 if (fr->print_force >= 0)
587 print_large_forces(stderr,mdatoms,cr,step,fr->print_force,x,f);
591 static void do_nb_verlet(t_forcerec *fr,
592 interaction_const_t *ic,
593 gmx_enerdata_t *enerd,
594 int flags, int ilocality,
597 gmx_wallcycle_t wcycle)
599 int nnbl, kernel_type, sh_e;
601 nonbonded_verlet_group_t *nbvg;
603 if (!(flags & GMX_FORCE_NONBONDED))
605 /* skip non-bonded calculation */
609 nbvg = &fr->nbv->grp[ilocality];
611 /* CUDA kernel launch overhead is already timed separately */
612 if (fr->cutoff_scheme != ecutsVERLET)
614 gmx_incons("Invalid cut-off scheme passed!");
617 if (nbvg->kernel_type != nbk8x8x8_CUDA)
619 wallcycle_sub_start(wcycle, ewcsNONBONDED);
621 switch (nbvg->kernel_type)
624 nbnxn_kernel_ref(&nbvg->nbl_lists,
630 enerd->grpp.ener[egCOULSR],
632 enerd->grpp.ener[egBHAMSR] :
633 enerd->grpp.ener[egLJSR]);
636 case nbk4xN_X86_SIMD128:
637 nbnxn_kernel_x86_simd128(&nbvg->nbl_lists,
643 enerd->grpp.ener[egCOULSR],
645 enerd->grpp.ener[egBHAMSR] :
646 enerd->grpp.ener[egLJSR]);
648 case nbk4xN_X86_SIMD256:
649 nbnxn_kernel_x86_simd256(&nbvg->nbl_lists,
655 enerd->grpp.ener[egCOULSR],
657 enerd->grpp.ener[egBHAMSR] :
658 enerd->grpp.ener[egLJSR]);
662 nbnxn_cuda_launch_kernel(fr->nbv->cu_nbv, nbvg->nbat, flags, ilocality);
665 case nbk8x8x8_PlainC:
666 nbnxn_kernel_gpu_ref(nbvg->nbl_lists.nbl[0],
671 nbvg->nbat->out[0].f,
673 enerd->grpp.ener[egCOULSR],
675 enerd->grpp.ener[egBHAMSR] :
676 enerd->grpp.ener[egLJSR]);
680 gmx_incons("Invalid nonbonded kernel type passed!");
683 if (nbvg->kernel_type != nbk8x8x8_CUDA)
685 wallcycle_sub_stop(wcycle, ewcsNONBONDED);
688 /* In eNR_??? the nbnxn F+E kernels are always the F kernel + 1 */
689 sh_e = ((flags & GMX_FORCE_ENERGY) ? 1 : 0);
691 ((EEL_RF(ic->eeltype) || ic->eeltype == eelCUT) ?
692 eNR_NBNXN_LJ_RF : eNR_NBNXN_LJ_TAB) + sh_e,
693 nbvg->nbl_lists.natpair_ljq);
694 inc_nrnb(nrnb,eNR_NBNXN_LJ+sh_e,nbvg->nbl_lists.natpair_lj);
696 ((EEL_RF(ic->eeltype) || ic->eeltype == eelCUT) ?
697 eNR_NBNXN_RF : eNR_NBNXN_TAB)+sh_e,
698 nbvg->nbl_lists.natpair_q);
701 void do_force_cutsVERLET(FILE *fplog,t_commrec *cr,
702 t_inputrec *inputrec,
703 gmx_large_int_t step,t_nrnb *nrnb,gmx_wallcycle_t wcycle,
706 gmx_groups_t *groups,
707 matrix box,rvec x[],history_t *hist,
711 gmx_enerdata_t *enerd,t_fcdata *fcd,
712 real *lambda,t_graph *graph,
713 t_forcerec *fr, interaction_const_t *ic,
714 gmx_vsite_t *vsite,rvec mu_tot,
715 double t,FILE *field,gmx_edsam_t ed,
723 gmx_bool bSepDVDL,bStateChanged,bNS,bFillGrid,bCalcCGCM,bBS;
724 gmx_bool bDoLongRange,bDoForces,bSepLRF,bUseGPU,bUseOrEmulGPU;
725 gmx_bool bDiffKernels=FALSE;
729 float cycles_pme,cycles_force;
730 nonbonded_verlet_t *nbv;
734 nb_kernel_type = fr->nbv->grp[0].kernel_type;
736 start = mdatoms->start;
737 homenr = mdatoms->homenr;
739 bSepDVDL = (fr->bSepDVDL && do_per_step(step,inputrec->nstlog));
741 clear_mat(vir_force);
744 if (DOMAINDECOMP(cr))
746 cg1 = cr->dd->ncg_tot;
757 bStateChanged = (flags & GMX_FORCE_STATECHANGED);
758 bNS = (flags & GMX_FORCE_NS) && (fr->bAllvsAll==FALSE);
759 bFillGrid = (bNS && bStateChanged);
760 bCalcCGCM = (bFillGrid && !DOMAINDECOMP(cr));
761 bDoLongRange = (fr->bTwinRange && bNS && (flags & GMX_FORCE_DOLR));
762 bDoForces = (flags & GMX_FORCE_FORCES);
763 bSepLRF = (bDoLongRange && bDoForces && (flags & GMX_FORCE_SEPLRF));
764 bUseGPU = fr->nbv->bUseGPU;
765 bUseOrEmulGPU = bUseGPU || (nbv->grp[0].kernel_type == nbk8x8x8_PlainC);
769 update_forcerec(fplog,fr,box);
771 if (NEED_MUTOT(*inputrec))
773 /* Calculate total (local) dipole moment in a temporary common array.
774 * This makes it possible to sum them over nodes faster.
776 calc_mu(start,homenr,
777 x,mdatoms->chargeA,mdatoms->chargeB,mdatoms->nChargePerturbed,
782 if (fr->ePBC != epbcNONE) {
783 /* Compute shift vectors every step,
784 * because of pressure coupling or box deformation!
786 if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
787 calc_shifts(box,fr->shift_vec);
790 put_atoms_in_box_omp(fr->ePBC,box,homenr,x);
791 inc_nrnb(nrnb,eNR_SHIFTX,homenr);
793 else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph) {
794 unshift_self(graph,box,x);
798 nbnxn_atomdata_copy_shiftvec(flags & GMX_FORCE_DYNAMICBOX,
799 fr->shift_vec,nbv->grp[0].nbat);
802 if (!(cr->duty & DUTY_PME)) {
803 /* Send particle coordinates to the pme nodes.
804 * Since this is only implemented for domain decomposition
805 * and domain decomposition does not use the graph,
806 * we do not need to worry about shifting.
809 wallcycle_start(wcycle,ewcPP_PMESENDX);
811 bBS = (inputrec->nwall == 2);
814 svmul(inputrec->wall_ewald_zfac,boxs[ZZ],boxs[ZZ]);
817 gmx_pme_send_x(cr,bBS ? boxs : box,x,
818 mdatoms->nChargePerturbed,lambda[efptCOUL],
819 (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)),step);
821 wallcycle_stop(wcycle,ewcPP_PMESENDX);
825 /* do gridding for pair search */
828 if (graph && bStateChanged)
830 /* Calculate intramolecular shift vectors to make molecules whole */
831 mk_mshift(fplog,graph,fr->ePBC,box,x);
835 box_diag[XX] = box[XX][XX];
836 box_diag[YY] = box[YY][YY];
837 box_diag[ZZ] = box[ZZ][ZZ];
839 wallcycle_start(wcycle,ewcNS);
842 wallcycle_sub_start(wcycle,ewcsNBS_GRID_LOCAL);
843 nbnxn_put_on_grid(nbv->nbs,fr->ePBC,box,
845 0,mdatoms->homenr,-1,fr->cginfo,x,
847 nbv->grp[eintLocal].kernel_type,
848 nbv->grp[eintLocal].nbat);
849 wallcycle_sub_stop(wcycle,ewcsNBS_GRID_LOCAL);
853 wallcycle_sub_start(wcycle,ewcsNBS_GRID_NONLOCAL);
854 nbnxn_put_on_grid_nonlocal(nbv->nbs,domdec_zones(cr->dd),
856 nbv->grp[eintNonlocal].kernel_type,
857 nbv->grp[eintNonlocal].nbat);
858 wallcycle_sub_stop(wcycle,ewcsNBS_GRID_NONLOCAL);
861 if (nbv->ngrp == 1 ||
862 nbv->grp[eintNonlocal].nbat == nbv->grp[eintLocal].nbat)
864 nbnxn_atomdata_set(nbv->grp[eintLocal].nbat,eatAll,
865 nbv->nbs,mdatoms,fr->cginfo);
869 nbnxn_atomdata_set(nbv->grp[eintLocal].nbat,eatLocal,
870 nbv->nbs,mdatoms,fr->cginfo);
871 nbnxn_atomdata_set(nbv->grp[eintNonlocal].nbat,eatAll,
872 nbv->nbs,mdatoms,fr->cginfo);
874 wallcycle_stop(wcycle, ewcNS);
877 /* initialize the GPU atom data and copy shift vector */
882 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
883 nbnxn_cuda_init_atomdata(nbv->cu_nbv, nbv->grp[eintLocal].nbat);
884 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
887 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
888 nbnxn_cuda_upload_shiftvec(nbv->cu_nbv, nbv->grp[eintLocal].nbat);
889 wallcycle_stop(wcycle, ewcLAUNCH_GPU_NB);
892 /* do local pair search */
895 wallcycle_start_nocount(wcycle,ewcNS);
896 wallcycle_sub_start(wcycle,ewcsNBS_SEARCH_LOCAL);
897 nbnxn_make_pairlist(nbv->nbs,nbv->grp[eintLocal].nbat,
900 nbv->min_ci_balanced,
901 &nbv->grp[eintLocal].nbl_lists,
903 nbv->grp[eintLocal].kernel_type,
905 wallcycle_sub_stop(wcycle,ewcsNBS_SEARCH_LOCAL);
909 /* initialize local pair-list on the GPU */
910 nbnxn_cuda_init_pairlist(nbv->cu_nbv,
911 nbv->grp[eintLocal].nbl_lists.nbl[0],
914 wallcycle_stop(wcycle, ewcNS);
918 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
919 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
920 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs,eatLocal,FALSE,x,
921 nbv->grp[eintLocal].nbat);
922 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
923 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
928 wallcycle_start(wcycle,ewcLAUNCH_GPU_NB);
929 /* launch local nonbonded F on GPU */
930 do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFNo,
932 wallcycle_stop(wcycle,ewcLAUNCH_GPU_NB);
935 /* Communicate coordinates and sum dipole if necessary +
936 do non-local pair search */
937 if (DOMAINDECOMP(cr))
939 bDiffKernels = (nbv->grp[eintNonlocal].kernel_type !=
940 nbv->grp[eintLocal].kernel_type);
944 /* With GPU+CPU non-bonded calculations we need to copy
945 * the local coordinates to the non-local nbat struct
946 * (in CPU format) as the non-local kernel call also
947 * calculates the local - non-local interactions.
949 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
950 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
951 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs,eatLocal,TRUE,x,
952 nbv->grp[eintNonlocal].nbat);
953 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
954 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
959 wallcycle_start_nocount(wcycle,ewcNS);
960 wallcycle_sub_start(wcycle,ewcsNBS_SEARCH_NONLOCAL);
964 nbnxn_grid_add_simple(nbv->nbs,nbv->grp[eintNonlocal].nbat);
967 nbnxn_make_pairlist(nbv->nbs,nbv->grp[eintNonlocal].nbat,
970 nbv->min_ci_balanced,
971 &nbv->grp[eintNonlocal].nbl_lists,
973 nbv->grp[eintNonlocal].kernel_type,
976 wallcycle_sub_stop(wcycle,ewcsNBS_SEARCH_NONLOCAL);
978 if (nbv->grp[eintNonlocal].kernel_type == nbk8x8x8_CUDA)
980 /* initialize non-local pair-list on the GPU */
981 nbnxn_cuda_init_pairlist(nbv->cu_nbv,
982 nbv->grp[eintNonlocal].nbl_lists.nbl[0],
985 wallcycle_stop(wcycle,ewcNS);
989 wallcycle_start(wcycle,ewcMOVEX);
990 dd_move_x(cr->dd,box,x);
992 /* When we don't need the total dipole we sum it in global_stat */
993 if (bStateChanged && NEED_MUTOT(*inputrec))
995 gmx_sumd(2*DIM,mu,cr);
997 wallcycle_stop(wcycle,ewcMOVEX);
999 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1000 wallcycle_sub_start(wcycle, ewcsNB_X_BUF_OPS);
1001 nbnxn_atomdata_copy_x_to_nbat_x(nbv->nbs,eatNonlocal,FALSE,x,
1002 nbv->grp[eintNonlocal].nbat);
1003 wallcycle_sub_stop(wcycle, ewcsNB_X_BUF_OPS);
1004 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1007 if (bUseGPU && !bDiffKernels)
1009 wallcycle_start(wcycle,ewcLAUNCH_GPU_NB);
1010 /* launch non-local nonbonded F on GPU */
1011 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFNo,
1013 cycles_force += wallcycle_stop(wcycle,ewcLAUNCH_GPU_NB);
1019 /* launch D2H copy-back F */
1020 wallcycle_start_nocount(wcycle, ewcLAUNCH_GPU_NB);
1021 if (DOMAINDECOMP(cr) && !bDiffKernels)
1023 nbnxn_cuda_launch_cpyback(nbv->cu_nbv, nbv->grp[eintNonlocal].nbat,
1024 flags, eatNonlocal);
1026 nbnxn_cuda_launch_cpyback(nbv->cu_nbv, nbv->grp[eintLocal].nbat,
1028 cycles_force += wallcycle_stop(wcycle,ewcLAUNCH_GPU_NB);
1031 if (bStateChanged && NEED_MUTOT(*inputrec))
1035 gmx_sumd(2*DIM,mu,cr);
1042 fr->mu_tot[i][j] = mu[i*DIM + j];
1046 if (fr->efep == efepNO)
1048 copy_rvec(fr->mu_tot[0],mu_tot);
1052 for(j=0; j<DIM; j++)
1055 (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] +
1056 lambda[efptCOUL]*fr->mu_tot[1][j];
1060 /* Reset energies */
1061 reset_enerdata(&(inputrec->opts),fr,bNS,enerd,MASTER(cr));
1062 clear_rvecs(SHIFTS,fr->fshift);
1064 if (DOMAINDECOMP(cr))
1066 if (!(cr->duty & DUTY_PME))
1068 wallcycle_start(wcycle,ewcPPDURINGPME);
1069 dd_force_flop_start(cr->dd,nrnb);
1073 /* Start the force cycle counter.
1074 * This counter is stopped in do_forcelow_level.
1075 * No parallel communication should occur while this counter is running,
1076 * since that will interfere with the dynamic load balancing.
1078 wallcycle_start(wcycle,ewcFORCE);
1081 /* Reset forces for which the virial is calculated separately:
1082 * PME/Ewald forces if necessary */
1083 if (fr->bF_NoVirSum)
1085 if (flags & GMX_FORCE_VIRIAL)
1087 fr->f_novirsum = fr->f_novirsum_alloc;
1090 clear_rvecs(fr->f_novirsum_n,fr->f_novirsum);
1094 clear_rvecs(homenr,fr->f_novirsum+start);
1099 /* We are not calculating the pressure so we do not need
1100 * a separate array for forces that do not contribute
1109 /* Add the long range forces to the short range forces */
1110 for(i=0; i<fr->natoms_force_constr; i++)
1112 copy_rvec(fr->f_twin[i],f[i]);
1115 else if (!(fr->bTwinRange && bNS))
1117 /* Clear the short-range forces */
1118 clear_rvecs(fr->natoms_force_constr,f);
1121 clear_rvec(fr->vir_diag_posres);
1123 if (inputrec->ePull == epullCONSTRAINT)
1125 clear_pull_forces(inputrec->pull);
1128 /* update QMMMrec, if necessary */
1131 update_QMMMrec(cr,fr,x,mdatoms,box,top);
1134 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_POSRES].nr > 0)
1136 posres_wrapper(fplog,flags,bSepDVDL,inputrec,nrnb,top,box,x,
1140 /* Compute the bonded and non-bonded energies and optionally forces */
1141 /* if we use the GPU turn off the nonbonded */
1142 do_force_lowlevel(fplog,step,fr,inputrec,&(top->idef),
1143 cr,nrnb,wcycle,mdatoms,&(inputrec->opts),
1144 x,hist,f,enerd,fcd,mtop,top,fr->born,
1145 &(top->atomtypes),bBornRadii,box,
1146 inputrec->fepvals,lambda,graph,&(top->excls),fr->mu_tot,
1147 ((nb_kernel_type == nbk8x8x8_CUDA || nb_kernel_type == nbk8x8x8_PlainC)
1148 ? flags&~GMX_FORCE_NONBONDED : flags),
1153 /* Maybe we should move this into do_force_lowlevel */
1154 do_nb_verlet(fr, ic, enerd, flags, eintLocal, enbvClearFYes,
1159 if (!bUseOrEmulGPU || bDiffKernels)
1163 if (DOMAINDECOMP(cr))
1165 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal,
1166 bDiffKernels ? enbvClearFYes : enbvClearFNo,
1176 aloc = eintNonlocal;
1179 /* Add all the non-bonded force to the normal force array.
1180 * This can be split into a local a non-local part when overlapping
1181 * communication with calculation with domain decomposition.
1183 cycles_force += wallcycle_stop(wcycle,ewcFORCE);
1184 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1185 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1186 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs,eatAll,nbv->grp[aloc].nbat,f);
1187 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1188 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1189 wallcycle_start_nocount(wcycle,ewcFORCE);
1191 /* if there are multiple fshift output buffers reduce them */
1192 if ((flags & GMX_FORCE_VIRIAL) &&
1193 nbv->grp[aloc].nbl_lists.nnbl > 1)
1195 nbnxn_atomdata_add_nbat_fshift_to_fshift(nbv->grp[aloc].nbat,
1200 cycles_force += wallcycle_stop(wcycle,ewcFORCE);
1204 do_flood(fplog,cr,x,f,ed,box,step,bNS);
1207 if (bUseOrEmulGPU && !bDiffKernels)
1209 /* wait for non-local forces (or calculate in emulation mode) */
1210 if (DOMAINDECOMP(cr))
1214 wallcycle_start(wcycle,ewcWAIT_GPU_NB_NL);
1215 nbnxn_cuda_wait_gpu(nbv->cu_nbv,
1216 nbv->grp[eintNonlocal].nbat,
1218 enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1220 cycles_force += wallcycle_stop(wcycle,ewcWAIT_GPU_NB_NL);
1224 wallcycle_start_nocount(wcycle,ewcFORCE);
1225 do_nb_verlet(fr, ic, enerd, flags, eintNonlocal, enbvClearFYes,
1227 cycles_force += wallcycle_stop(wcycle,ewcFORCE);
1229 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1230 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1231 /* skip the reduction if there was no non-local work to do */
1232 if (nbv->grp[eintLocal].nbl_lists.nbl[0]->nsci > 0)
1234 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs,eatNonlocal,
1235 nbv->grp[eintNonlocal].nbat,f);
1237 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1238 cycles_force += wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1244 /* Communicate the forces */
1247 wallcycle_start(wcycle,ewcMOVEF);
1248 if (DOMAINDECOMP(cr))
1250 dd_move_f(cr->dd,f,fr->fshift);
1251 /* Do we need to communicate the separate force array
1252 * for terms that do not contribute to the single sum virial?
1253 * Position restraints and electric fields do not introduce
1254 * inter-cg forces, only full electrostatics methods do.
1255 * When we do not calculate the virial, fr->f_novirsum = f,
1256 * so we have already communicated these forces.
1258 if (EEL_FULL(fr->eeltype) && cr->dd->n_intercg_excl &&
1259 (flags & GMX_FORCE_VIRIAL))
1261 dd_move_f(cr->dd,fr->f_novirsum,NULL);
1265 /* We should not update the shift forces here,
1266 * since f_twin is already included in f.
1268 dd_move_f(cr->dd,fr->f_twin,NULL);
1271 wallcycle_stop(wcycle,ewcMOVEF);
1277 /* wait for local forces (or calculate in emulation mode) */
1280 wallcycle_start(wcycle,ewcWAIT_GPU_NB_L);
1281 nbnxn_cuda_wait_gpu(nbv->cu_nbv,
1282 nbv->grp[eintLocal].nbat,
1284 enerd->grpp.ener[egLJSR], enerd->grpp.ener[egCOULSR],
1286 wallcycle_stop(wcycle,ewcWAIT_GPU_NB_L);
1288 /* now clear the GPU outputs while we finish the step on the CPU */
1289 nbnxn_cuda_clear_outputs(nbv->cu_nbv, flags);
1293 wallcycle_start_nocount(wcycle,ewcFORCE);
1294 do_nb_verlet(fr, ic, enerd, flags, eintLocal,
1295 DOMAINDECOMP(cr) ? enbvClearFNo : enbvClearFYes,
1297 wallcycle_stop(wcycle,ewcFORCE);
1299 wallcycle_start(wcycle, ewcNB_XF_BUF_OPS);
1300 wallcycle_sub_start(wcycle, ewcsNB_F_BUF_OPS);
1301 if (nbv->grp[eintLocal].nbl_lists.nbl[0]->nsci > 0)
1303 /* skip the reduction if there was no non-local work to do */
1304 nbnxn_atomdata_add_nbat_f_to_f(nbv->nbs,eatLocal,
1305 nbv->grp[eintLocal].nbat,f);
1307 wallcycle_sub_stop(wcycle, ewcsNB_F_BUF_OPS);
1308 wallcycle_stop(wcycle, ewcNB_XF_BUF_OPS);
1311 if (DOMAINDECOMP(cr))
1313 dd_force_flop_stop(cr->dd,nrnb);
1316 dd_cycles_add(cr->dd,cycles_force-cycles_pme,ddCyclF);
1322 if (IR_ELEC_FIELD(*inputrec))
1324 /* Compute forces due to electric field */
1325 calc_f_el(MASTER(cr) ? field : NULL,
1326 start,homenr,mdatoms->chargeA,x,fr->f_novirsum,
1327 inputrec->ex,inputrec->et,t);
1330 /* If we have NoVirSum forces, but we do not calculate the virial,
1331 * we sum fr->f_novirum=f later.
1333 if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1335 wallcycle_start(wcycle,ewcVSITESPREAD);
1336 spread_vsite_f(fplog,vsite,x,f,fr->fshift,FALSE,NULL,nrnb,
1337 &top->idef,fr->ePBC,fr->bMolPBC,graph,box,cr);
1338 wallcycle_stop(wcycle,ewcVSITESPREAD);
1342 wallcycle_start(wcycle,ewcVSITESPREAD);
1343 spread_vsite_f(fplog,vsite,x,fr->f_twin,NULL,FALSE,NULL,
1345 &top->idef,fr->ePBC,fr->bMolPBC,graph,box,cr);
1346 wallcycle_stop(wcycle,ewcVSITESPREAD);
1350 if (flags & GMX_FORCE_VIRIAL)
1352 /* Calculation of the virial must be done after vsites! */
1353 calc_virial(fplog,mdatoms->start,mdatoms->homenr,x,f,
1354 vir_force,graph,box,nrnb,fr,inputrec->ePBC);
1358 if (inputrec->ePull == epullUMBRELLA || inputrec->ePull == epullCONST_F)
1360 pull_potential_wrapper(fplog,bSepDVDL,cr,inputrec,box,x,
1361 f,vir_force,mdatoms,enerd,lambda,t);
1364 if (PAR(cr) && !(cr->duty & DUTY_PME))
1366 /* In case of node-splitting, the PP nodes receive the long-range
1367 * forces, virial and energy from the PME nodes here.
1369 pme_receive_force_ener(fplog,bSepDVDL,cr,wcycle,enerd,fr);
1374 post_process_forces(fplog,cr,step,nrnb,wcycle,
1375 top,box,x,f,vir_force,mdatoms,graph,fr,vsite,
1379 /* Sum the potential energy terms from group contributions */
1380 sum_epot(&(inputrec->opts),enerd);
1383 void do_force_cutsGROUP(FILE *fplog,t_commrec *cr,
1384 t_inputrec *inputrec,
1385 gmx_large_int_t step,t_nrnb *nrnb,gmx_wallcycle_t wcycle,
1386 gmx_localtop_t *top,
1388 gmx_groups_t *groups,
1389 matrix box,rvec x[],history_t *hist,
1393 gmx_enerdata_t *enerd,t_fcdata *fcd,
1394 real *lambda,t_graph *graph,
1395 t_forcerec *fr,gmx_vsite_t *vsite,rvec mu_tot,
1396 double t,FILE *field,gmx_edsam_t ed,
1397 gmx_bool bBornRadii,
1403 gmx_bool bSepDVDL,bStateChanged,bNS,bFillGrid,bCalcCGCM,bBS;
1404 gmx_bool bDoLongRange,bDoForces,bSepLRF;
1405 gmx_bool bDoAdressWF;
1407 rvec vzero,box_diag;
1408 real e,v,dvdlambda[efptNR];
1410 float cycles_pme,cycles_force;
1412 start = mdatoms->start;
1413 homenr = mdatoms->homenr;
1415 bSepDVDL = (fr->bSepDVDL && do_per_step(step,inputrec->nstlog));
1417 clear_mat(vir_force);
1421 pd_cg_range(cr,&cg0,&cg1);
1426 if (DOMAINDECOMP(cr))
1428 cg1 = cr->dd->ncg_tot;
1440 bStateChanged = (flags & GMX_FORCE_STATECHANGED);
1441 bNS = (flags & GMX_FORCE_NS) && (fr->bAllvsAll==FALSE);
1442 bFillGrid = (bNS && bStateChanged);
1443 bCalcCGCM = (bFillGrid && !DOMAINDECOMP(cr));
1444 bDoLongRange = (fr->bTwinRange && bNS && (flags & GMX_FORCE_DOLR));
1445 bDoForces = (flags & GMX_FORCE_FORCES);
1446 bSepLRF = (bDoLongRange && bDoForces && (flags & GMX_FORCE_SEPLRF));
1447 /* should probably move this to the forcerec since it doesn't change */
1448 bDoAdressWF = ((fr->adress_type!=eAdressOff));
1452 update_forcerec(fplog,fr,box);
1454 if (NEED_MUTOT(*inputrec))
1456 /* Calculate total (local) dipole moment in a temporary common array.
1457 * This makes it possible to sum them over nodes faster.
1459 calc_mu(start,homenr,
1460 x,mdatoms->chargeA,mdatoms->chargeB,mdatoms->nChargePerturbed,
1465 if (fr->ePBC != epbcNONE) {
1466 /* Compute shift vectors every step,
1467 * because of pressure coupling or box deformation!
1469 if ((flags & GMX_FORCE_DYNAMICBOX) && bStateChanged)
1470 calc_shifts(box,fr->shift_vec);
1473 put_charge_groups_in_box(fplog,cg0,cg1,fr->ePBC,box,
1474 &(top->cgs),x,fr->cg_cm);
1475 inc_nrnb(nrnb,eNR_CGCM,homenr);
1476 inc_nrnb(nrnb,eNR_RESETX,cg1-cg0);
1478 else if (EI_ENERGY_MINIMIZATION(inputrec->eI) && graph) {
1479 unshift_self(graph,box,x);
1482 else if (bCalcCGCM) {
1483 calc_cgcm(fplog,cg0,cg1,&(top->cgs),x,fr->cg_cm);
1484 inc_nrnb(nrnb,eNR_CGCM,homenr);
1489 move_cgcm(fplog,cr,fr->cg_cm);
1492 pr_rvecs(debug,0,"cgcm",fr->cg_cm,top->cgs.nr);
1496 if (!(cr->duty & DUTY_PME)) {
1497 /* Send particle coordinates to the pme nodes.
1498 * Since this is only implemented for domain decomposition
1499 * and domain decomposition does not use the graph,
1500 * we do not need to worry about shifting.
1503 wallcycle_start(wcycle,ewcPP_PMESENDX);
1505 bBS = (inputrec->nwall == 2);
1508 svmul(inputrec->wall_ewald_zfac,boxs[ZZ],boxs[ZZ]);
1511 gmx_pme_send_x(cr,bBS ? boxs : box,x,
1512 mdatoms->nChargePerturbed,lambda[efptCOUL],
1513 (flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY)),step);
1515 wallcycle_stop(wcycle,ewcPP_PMESENDX);
1517 #endif /* GMX_MPI */
1519 /* Communicate coordinates and sum dipole if necessary */
1522 wallcycle_start(wcycle,ewcMOVEX);
1523 if (DOMAINDECOMP(cr))
1525 dd_move_x(cr->dd,box,x);
1529 move_x(fplog,cr,GMX_LEFT,GMX_RIGHT,x,nrnb);
1531 wallcycle_stop(wcycle,ewcMOVEX);
1534 /* update adress weight beforehand */
1535 if(bStateChanged && bDoAdressWF)
1537 /* need pbc for adress weight calculation with pbc_dx */
1538 set_pbc(&pbc,inputrec->ePBC,box);
1539 if(fr->adress_site == eAdressSITEcog)
1541 update_adress_weights_cog(top->idef.iparams,top->idef.il,x,fr,mdatoms,
1542 inputrec->ePBC==epbcNONE ? NULL : &pbc);
1544 else if (fr->adress_site == eAdressSITEcom)
1546 update_adress_weights_com(fplog,cg0,cg1,&(top->cgs),x,fr,mdatoms,
1547 inputrec->ePBC==epbcNONE ? NULL : &pbc);
1549 else if (fr->adress_site == eAdressSITEatomatom){
1550 update_adress_weights_atom_per_atom(cg0,cg1,&(top->cgs),x,fr,mdatoms,
1551 inputrec->ePBC==epbcNONE ? NULL : &pbc);
1555 update_adress_weights_atom(cg0,cg1,&(top->cgs),x,fr,mdatoms,
1556 inputrec->ePBC==epbcNONE ? NULL : &pbc);
1560 if (NEED_MUTOT(*inputrec))
1567 gmx_sumd(2*DIM,mu,cr);
1573 fr->mu_tot[i][j] = mu[i*DIM + j];
1577 if (fr->efep == efepNO)
1579 copy_rvec(fr->mu_tot[0],mu_tot);
1583 for(j=0; j<DIM; j++)
1586 (1.0 - lambda[efptCOUL])*fr->mu_tot[0][j] + lambda[efptCOUL]*fr->mu_tot[1][j];
1591 /* Reset energies */
1592 reset_enerdata(&(inputrec->opts),fr,bNS,enerd,MASTER(cr));
1593 clear_rvecs(SHIFTS,fr->fshift);
1597 wallcycle_start(wcycle,ewcNS);
1599 if (graph && bStateChanged)
1601 /* Calculate intramolecular shift vectors to make molecules whole */
1602 mk_mshift(fplog,graph,fr->ePBC,box,x);
1605 /* Reset long range forces if necessary */
1608 /* Reset the (long-range) forces if necessary */
1609 clear_rvecs(fr->natoms_force_constr,bSepLRF ? fr->f_twin : f);
1612 /* Do the actual neighbour searching and if twin range electrostatics
1613 * also do the calculation of long range forces and energies.
1615 for (i=0;i<efptNR;i++) {dvdlambda[i] = 0;}
1617 groups,&(inputrec->opts),top,mdatoms,
1618 cr,nrnb,lambda,dvdlambda,&enerd->grpp,bFillGrid,
1619 bDoLongRange,bDoForces,bSepLRF ? fr->f_twin : f);
1622 fprintf(fplog,sepdvdlformat,"LR non-bonded",0.0,dvdlambda);
1624 enerd->dvdl_lin[efptVDW] += dvdlambda[efptVDW];
1625 enerd->dvdl_lin[efptCOUL] += dvdlambda[efptCOUL];
1627 wallcycle_stop(wcycle,ewcNS);
1630 if (inputrec->implicit_solvent && bNS)
1632 make_gb_nblist(cr,inputrec->gb_algorithm,inputrec->rlist,
1633 x,box,fr,&top->idef,graph,fr->born);
1636 if (DOMAINDECOMP(cr))
1638 if (!(cr->duty & DUTY_PME))
1640 wallcycle_start(wcycle,ewcPPDURINGPME);
1641 dd_force_flop_start(cr->dd,nrnb);
1647 /* Enforced rotation has its own cycle counter that starts after the collective
1648 * coordinates have been communicated. It is added to ddCyclF to allow
1649 * for proper load-balancing */
1650 wallcycle_start(wcycle,ewcROT);
1651 do_rotation(cr,inputrec,box,x,t,step,wcycle,bNS);
1652 wallcycle_stop(wcycle,ewcROT);
1655 /* Start the force cycle counter.
1656 * This counter is stopped in do_forcelow_level.
1657 * No parallel communication should occur while this counter is running,
1658 * since that will interfere with the dynamic load balancing.
1660 wallcycle_start(wcycle,ewcFORCE);
1664 /* Reset forces for which the virial is calculated separately:
1665 * PME/Ewald forces if necessary */
1666 if (fr->bF_NoVirSum)
1668 if (flags & GMX_FORCE_VIRIAL)
1670 fr->f_novirsum = fr->f_novirsum_alloc;
1673 clear_rvecs(fr->f_novirsum_n,fr->f_novirsum);
1677 clear_rvecs(homenr,fr->f_novirsum+start);
1682 /* We are not calculating the pressure so we do not need
1683 * a separate array for forces that do not contribute
1692 /* Add the long range forces to the short range forces */
1693 for(i=0; i<fr->natoms_force_constr; i++)
1695 copy_rvec(fr->f_twin[i],f[i]);
1698 else if (!(fr->bTwinRange && bNS))
1700 /* Clear the short-range forces */
1701 clear_rvecs(fr->natoms_force_constr,f);
1704 clear_rvec(fr->vir_diag_posres);
1706 if (inputrec->ePull == epullCONSTRAINT)
1708 clear_pull_forces(inputrec->pull);
1711 /* update QMMMrec, if necessary */
1714 update_QMMMrec(cr,fr,x,mdatoms,box,top);
1717 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_POSRES].nr > 0)
1719 posres_wrapper(fplog,flags,bSepDVDL,inputrec,nrnb,top,box,x,
1723 if ((flags & GMX_FORCE_BONDED) && top->idef.il[F_FBPOSRES].nr > 0)
1725 /* Flat-bottomed position restraints always require full pbc */
1726 if(!(bStateChanged && bDoAdressWF))
1728 set_pbc(&pbc,inputrec->ePBC,box);
1730 v = fbposres(top->idef.il[F_FBPOSRES].nr,top->idef.il[F_FBPOSRES].iatoms,
1731 top->idef.iparams_fbposres,
1732 (const rvec*)x,fr->f_novirsum,fr->vir_diag_posres,
1733 inputrec->ePBC==epbcNONE ? NULL : &pbc,
1734 fr->rc_scaling,fr->ePBC,fr->posres_com);
1735 enerd->term[F_FBPOSRES] += v;
1736 inc_nrnb(nrnb,eNR_FBPOSRES,top->idef.il[F_FBPOSRES].nr/2);
1739 /* Compute the bonded and non-bonded energies and optionally forces */
1740 do_force_lowlevel(fplog,step,fr,inputrec,&(top->idef),
1741 cr,nrnb,wcycle,mdatoms,&(inputrec->opts),
1742 x,hist,f,enerd,fcd,mtop,top,fr->born,
1743 &(top->atomtypes),bBornRadii,box,
1744 inputrec->fepvals,lambda,
1745 graph,&(top->excls),fr->mu_tot,
1749 cycles_force = wallcycle_stop(wcycle,ewcFORCE);
1753 do_flood(fplog,cr,x,f,ed,box,step,bNS);
1756 if (DOMAINDECOMP(cr))
1758 dd_force_flop_stop(cr->dd,nrnb);
1761 dd_cycles_add(cr->dd,cycles_force-cycles_pme,ddCyclF);
1767 if (IR_ELEC_FIELD(*inputrec))
1769 /* Compute forces due to electric field */
1770 calc_f_el(MASTER(cr) ? field : NULL,
1771 start,homenr,mdatoms->chargeA,x,fr->f_novirsum,
1772 inputrec->ex,inputrec->et,t);
1775 if (bDoAdressWF && fr->adress_icor == eAdressICThermoForce)
1777 /* Compute thermodynamic force in hybrid AdResS region */
1778 adress_thermo_force(start,homenr,&(top->cgs),x,fr->f_novirsum,fr,mdatoms,
1779 inputrec->ePBC==epbcNONE ? NULL : &pbc);
1782 /* Communicate the forces */
1785 wallcycle_start(wcycle,ewcMOVEF);
1786 if (DOMAINDECOMP(cr))
1788 dd_move_f(cr->dd,f,fr->fshift);
1789 /* Do we need to communicate the separate force array
1790 * for terms that do not contribute to the single sum virial?
1791 * Position restraints and electric fields do not introduce
1792 * inter-cg forces, only full electrostatics methods do.
1793 * When we do not calculate the virial, fr->f_novirsum = f,
1794 * so we have already communicated these forces.
1796 if (EEL_FULL(fr->eeltype) && cr->dd->n_intercg_excl &&
1797 (flags & GMX_FORCE_VIRIAL))
1799 dd_move_f(cr->dd,fr->f_novirsum,NULL);
1803 /* We should not update the shift forces here,
1804 * since f_twin is already included in f.
1806 dd_move_f(cr->dd,fr->f_twin,NULL);
1811 pd_move_f(cr,f,nrnb);
1814 pd_move_f(cr,fr->f_twin,nrnb);
1817 wallcycle_stop(wcycle,ewcMOVEF);
1820 /* If we have NoVirSum forces, but we do not calculate the virial,
1821 * we sum fr->f_novirum=f later.
1823 if (vsite && !(fr->bF_NoVirSum && !(flags & GMX_FORCE_VIRIAL)))
1825 wallcycle_start(wcycle,ewcVSITESPREAD);
1826 spread_vsite_f(fplog,vsite,x,f,fr->fshift,FALSE,NULL,nrnb,
1827 &top->idef,fr->ePBC,fr->bMolPBC,graph,box,cr);
1828 wallcycle_stop(wcycle,ewcVSITESPREAD);
1832 wallcycle_start(wcycle,ewcVSITESPREAD);
1833 spread_vsite_f(fplog,vsite,x,fr->f_twin,NULL,FALSE,NULL,
1835 &top->idef,fr->ePBC,fr->bMolPBC,graph,box,cr);
1836 wallcycle_stop(wcycle,ewcVSITESPREAD);
1840 if (flags & GMX_FORCE_VIRIAL)
1842 /* Calculation of the virial must be done after vsites! */
1843 calc_virial(fplog,mdatoms->start,mdatoms->homenr,x,f,
1844 vir_force,graph,box,nrnb,fr,inputrec->ePBC);
1848 if (inputrec->ePull == epullUMBRELLA || inputrec->ePull == epullCONST_F)
1850 pull_potential_wrapper(fplog,bSepDVDL,cr,inputrec,box,x,
1851 f,vir_force,mdatoms,enerd,lambda,t);
1854 /* Add the forces from enforced rotation potentials (if any) */
1857 wallcycle_start(wcycle,ewcROTadd);
1858 enerd->term[F_COM_PULL] += add_rot_forces(inputrec->rot, f, cr,step,t);
1859 wallcycle_stop(wcycle,ewcROTadd);
1862 if (PAR(cr) && !(cr->duty & DUTY_PME))
1864 /* In case of node-splitting, the PP nodes receive the long-range
1865 * forces, virial and energy from the PME nodes here.
1867 pme_receive_force_ener(fplog,bSepDVDL,cr,wcycle,enerd,fr);
1872 post_process_forces(fplog,cr,step,nrnb,wcycle,
1873 top,box,x,f,vir_force,mdatoms,graph,fr,vsite,
1877 /* Sum the potential energy terms from group contributions */
1878 sum_epot(&(inputrec->opts),enerd);
1881 void do_force(FILE *fplog,t_commrec *cr,
1882 t_inputrec *inputrec,
1883 gmx_large_int_t step,t_nrnb *nrnb,gmx_wallcycle_t wcycle,
1884 gmx_localtop_t *top,
1886 gmx_groups_t *groups,
1887 matrix box,rvec x[],history_t *hist,
1891 gmx_enerdata_t *enerd,t_fcdata *fcd,
1892 real *lambda,t_graph *graph,
1894 gmx_vsite_t *vsite,rvec mu_tot,
1895 double t,FILE *field,gmx_edsam_t ed,
1896 gmx_bool bBornRadii,
1899 /* modify force flag if not doing nonbonded */
1900 if (!fr->bNonbonded)
1902 flags &= ~GMX_FORCE_NONBONDED;
1905 switch (inputrec->cutoff_scheme)
1908 do_force_cutsVERLET(fplog, cr, inputrec,
1924 do_force_cutsGROUP(fplog, cr, inputrec,
1939 gmx_incons("Invalid cut-off scheme passed!");
1944 void do_constrain_first(FILE *fplog,gmx_constr_t constr,
1945 t_inputrec *ir,t_mdatoms *md,
1946 t_state *state,rvec *f,
1947 t_graph *graph,t_commrec *cr,t_nrnb *nrnb,
1948 t_forcerec *fr, gmx_localtop_t *top, tensor shake_vir)
1951 gmx_large_int_t step;
1952 real dt=ir->delta_t;
1956 snew(savex,state->natoms);
1959 end = md->homenr + start;
1962 fprintf(debug,"vcm: start=%d, homenr=%d, end=%d\n",
1963 start,md->homenr,end);
1964 /* Do a first constrain to reset particles... */
1965 step = ir->init_step;
1968 char buf[STEPSTRSIZE];
1969 fprintf(fplog,"\nConstraining the starting coordinates (step %s)\n",
1970 gmx_step_str(step,buf));
1974 /* constrain the current position */
1975 constrain(NULL,TRUE,FALSE,constr,&(top->idef),
1976 ir,NULL,cr,step,0,md,
1977 state->x,state->x,NULL,
1978 fr->bMolPBC,state->box,
1979 state->lambda[efptBONDED],&dvdl_dum,
1980 NULL,NULL,nrnb,econqCoord,
1981 ir->epc==epcMTTK,state->veta,state->veta);
1984 /* constrain the inital velocity, and save it */
1985 /* also may be useful if we need the ekin from the halfstep for velocity verlet */
1986 /* might not yet treat veta correctly */
1987 constrain(NULL,TRUE,FALSE,constr,&(top->idef),
1988 ir,NULL,cr,step,0,md,
1989 state->x,state->v,state->v,
1990 fr->bMolPBC,state->box,
1991 state->lambda[efptBONDED],&dvdl_dum,
1992 NULL,NULL,nrnb,econqVeloc,
1993 ir->epc==epcMTTK,state->veta,state->veta);
1995 /* constrain the inital velocities at t-dt/2 */
1996 if (EI_STATE_VELOCITY(ir->eI) && ir->eI!=eiVV)
1998 for(i=start; (i<end); i++)
2000 for(m=0; (m<DIM); m++)
2002 /* Reverse the velocity */
2003 state->v[i][m] = -state->v[i][m];
2004 /* Store the position at t-dt in buf */
2005 savex[i][m] = state->x[i][m] + dt*state->v[i][m];
2008 /* Shake the positions at t=-dt with the positions at t=0
2009 * as reference coordinates.
2013 char buf[STEPSTRSIZE];
2014 fprintf(fplog,"\nConstraining the coordinates at t0-dt (step %s)\n",
2015 gmx_step_str(step,buf));
2018 constrain(NULL,TRUE,FALSE,constr,&(top->idef),
2019 ir,NULL,cr,step,-1,md,
2020 state->x,savex,NULL,
2021 fr->bMolPBC,state->box,
2022 state->lambda[efptBONDED],&dvdl_dum,
2023 state->v,NULL,nrnb,econqCoord,
2024 ir->epc==epcMTTK,state->veta,state->veta);
2026 for(i=start; i<end; i++) {
2027 for(m=0; m<DIM; m++) {
2028 /* Re-reverse the velocities */
2029 state->v[i][m] = -state->v[i][m];
2036 void calc_enervirdiff(FILE *fplog,int eDispCorr,t_forcerec *fr)
2038 double eners[2],virs[2],enersum,virsum,y0,f,g,h;
2039 double r0,r1,r,rc3,rc9,ea,eb,ec,pa,pb,pc,pd;
2040 double invscale,invscale2,invscale3;
2041 int ri0,ri1,ri,i,offstart,offset;
2044 fr->enershiftsix = 0;
2045 fr->enershifttwelve = 0;
2046 fr->enerdiffsix = 0;
2047 fr->enerdifftwelve = 0;
2049 fr->virdifftwelve = 0;
2051 if (eDispCorr != edispcNO) {
2052 for(i=0; i<2; i++) {
2056 if ((fr->vdwtype == evdwSWITCH) || (fr->vdwtype == evdwSHIFT)) {
2057 if (fr->rvdw_switch == 0)
2059 "With dispersion correction rvdw-switch can not be zero "
2060 "for vdw-type = %s",evdw_names[fr->vdwtype]);
2062 scale = fr->nblists[0].tab.scale;
2063 vdwtab = fr->nblists[0].vdwtab;
2065 /* Round the cut-offs to exact table values for precision */
2066 ri0 = floor(fr->rvdw_switch*scale);
2067 ri1 = ceil(fr->rvdw*scale);
2073 if (fr->vdwtype == evdwSHIFT) {
2074 /* Determine the constant energy shift below rvdw_switch */
2075 fr->enershiftsix = (real)(-1.0/(rc3*rc3)) - vdwtab[8*ri0];
2076 fr->enershifttwelve = (real)( 1.0/(rc9*rc3)) - vdwtab[8*ri0 + 4];
2078 /* Add the constant part from 0 to rvdw_switch.
2079 * This integration from 0 to rvdw_switch overcounts the number
2080 * of interactions by 1, as it also counts the self interaction.
2081 * We will correct for this later.
2083 eners[0] += 4.0*M_PI*fr->enershiftsix*rc3/3.0;
2084 eners[1] += 4.0*M_PI*fr->enershifttwelve*rc3/3.0;
2086 invscale = 1.0/(scale);
2087 invscale2 = invscale*invscale;
2088 invscale3 = invscale*invscale2;
2090 /* following summation derived from cubic spline definition,
2091 Numerical Recipies in C, second edition, p. 113-116. Exact
2092 for the cubic spline. We first calculate the negative of
2093 the energy from rvdw to rvdw_switch, assuming that g(r)=1,
2094 and then add the more standard, abrupt cutoff correction to
2095 that result, yielding the long-range correction for a
2096 switched function. We perform both the pressure and energy
2097 loops at the same time for simplicity, as the computational
2101 enersum = 0.0; virsum = 0.0;
2106 for (ri=ri0; ri<ri1; ri++) {
2109 eb = 2.0*invscale2*r;
2113 pb = 3.0*invscale2*r;
2114 pc = 3.0*invscale*r*r;
2117 /* this "8" is from the packing in the vdwtab array - perhaps
2118 should be #define'ed? */
2119 offset = 8*ri + offstart;
2120 y0 = vdwtab[offset];
2121 f = vdwtab[offset+1];
2122 g = vdwtab[offset+2];
2123 h = vdwtab[offset+3];
2125 enersum += y0*(ea/3 + eb/2 + ec) + f*(ea/4 + eb/3 + ec/2)+
2126 g*(ea/5 + eb/4 + ec/3) + h*(ea/6 + eb/5 + ec/4);
2127 virsum += f*(pa/4 + pb/3 + pc/2 + pd) +
2128 2*g*(pa/5 + pb/4 + pc/3 + pd/2) + 3*h*(pa/6 + pb/5 + pc/4 + pd/3);
2131 enersum *= 4.0*M_PI;
2133 eners[i] -= enersum;
2137 /* now add the correction for rvdw_switch to infinity */
2138 eners[0] += -4.0*M_PI/(3.0*rc3);
2139 eners[1] += 4.0*M_PI/(9.0*rc9);
2140 virs[0] += 8.0*M_PI/rc3;
2141 virs[1] += -16.0*M_PI/(3.0*rc9);
2143 else if ((fr->vdwtype == evdwCUT) || (fr->vdwtype == evdwUSER)) {
2144 if (fr->vdwtype == evdwUSER && fplog)
2146 "WARNING: using dispersion correction with user tables\n");
2147 rc3 = fr->rvdw*fr->rvdw*fr->rvdw;
2149 /* Contribution beyond the cut-off */
2150 eners[0] += -4.0*M_PI/(3.0*rc3);
2151 eners[1] += 4.0*M_PI/(9.0*rc9);
2152 if (fr->vdw_pot_shift) {
2153 /* Contribution within the cut-off */
2154 eners[0] += -4.0*M_PI/(3.0*rc3);
2155 eners[1] += 4.0*M_PI/(3.0*rc9);
2157 /* Contribution beyond the cut-off */
2158 virs[0] += 8.0*M_PI/rc3;
2159 virs[1] += -16.0*M_PI/(3.0*rc9);
2162 "Dispersion correction is not implemented for vdw-type = %s",
2163 evdw_names[fr->vdwtype]);
2165 fr->enerdiffsix = eners[0];
2166 fr->enerdifftwelve = eners[1];
2167 /* The 0.5 is due to the Gromacs definition of the virial */
2168 fr->virdiffsix = 0.5*virs[0];
2169 fr->virdifftwelve = 0.5*virs[1];
2173 void calc_dispcorr(FILE *fplog,t_inputrec *ir,t_forcerec *fr,
2174 gmx_large_int_t step,int natoms,
2175 matrix box,real lambda,tensor pres,tensor virial,
2176 real *prescorr, real *enercorr, real *dvdlcorr)
2178 gmx_bool bCorrAll,bCorrPres;
2179 real dvdlambda,invvol,dens,ninter,avcsix,avctwelve,enerdiff,svir=0,spres=0;
2189 if (ir->eDispCorr != edispcNO) {
2190 bCorrAll = (ir->eDispCorr == edispcAllEner ||
2191 ir->eDispCorr == edispcAllEnerPres);
2192 bCorrPres = (ir->eDispCorr == edispcEnerPres ||
2193 ir->eDispCorr == edispcAllEnerPres);
2195 invvol = 1/det(box);
2198 /* Only correct for the interactions with the inserted molecule */
2199 dens = (natoms - fr->n_tpi)*invvol;
2204 dens = natoms*invvol;
2205 ninter = 0.5*natoms;
2208 if (ir->efep == efepNO)
2210 avcsix = fr->avcsix[0];
2211 avctwelve = fr->avctwelve[0];
2215 avcsix = (1 - lambda)*fr->avcsix[0] + lambda*fr->avcsix[1];
2216 avctwelve = (1 - lambda)*fr->avctwelve[0] + lambda*fr->avctwelve[1];
2219 enerdiff = ninter*(dens*fr->enerdiffsix - fr->enershiftsix);
2220 *enercorr += avcsix*enerdiff;
2222 if (ir->efep != efepNO)
2224 dvdlambda += (fr->avcsix[1] - fr->avcsix[0])*enerdiff;
2228 enerdiff = ninter*(dens*fr->enerdifftwelve - fr->enershifttwelve);
2229 *enercorr += avctwelve*enerdiff;
2230 if (fr->efep != efepNO)
2232 dvdlambda += (fr->avctwelve[1] - fr->avctwelve[0])*enerdiff;
2238 svir = ninter*dens*avcsix*fr->virdiffsix/3.0;
2239 if (ir->eDispCorr == edispcAllEnerPres)
2241 svir += ninter*dens*avctwelve*fr->virdifftwelve/3.0;
2243 /* The factor 2 is because of the Gromacs virial definition */
2244 spres = -2.0*invvol*svir*PRESFAC;
2246 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 */
2257 fprintf(debug,"Long Range LJ corr.: <C6> %10.4e, <C12> %10.4e\n",
2263 "Long Range LJ corr.: Epot %10g, Pres: %10g, Vir: %10g\n",
2264 *enercorr,spres,svir);
2268 fprintf(debug,"Long Range LJ corr.: Epot %10g\n",*enercorr);
2272 if (fr->bSepDVDL && do_per_step(step,ir->nstlog))
2274 fprintf(fplog,sepdvdlformat,"Dispersion correction",
2275 *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[])
2288 fprintf(fplog,"Removing pbc first time\n");
2289 calc_shifts(box,fr->shift_vec);
2291 mk_mshift(fplog,graph,fr->ePBC,box,x);
2293 p_graph(debug,"do_pbc_first 1",graph);
2294 shift_self(graph,box,x);
2295 /* By doing an extra mk_mshift the molecules that are broken
2296 * because they were e.g. imported from another software
2297 * will be made whole again. Such are the healing powers
2300 mk_mshift(fplog,graph,fr->ePBC,box,x);
2302 p_graph(debug,"do_pbc_first 2",graph);
2305 fprintf(fplog,"Done rmpbc\n");
2308 static void low_do_pbc_mtop(FILE *fplog,int ePBC,matrix box,
2309 gmx_mtop_t *mtop,rvec x[],
2314 gmx_molblock_t *molb;
2316 if (bFirst && fplog)
2317 fprintf(fplog,"Removing pbc first time\n");
2321 for(mb=0; mb<mtop->nmolblock; mb++) {
2322 molb = &mtop->molblock[mb];
2323 if (molb->natoms_mol == 1 ||
2324 (!bFirst && mtop->moltype[molb->type].cgs.nr == 1)) {
2325 /* Just one atom or charge group in the molecule, no PBC required */
2326 as += molb->nmol*molb->natoms_mol;
2328 /* Pass NULL iso fplog to avoid graph prints for each molecule type */
2329 mk_graph_ilist(NULL,mtop->moltype[molb->type].ilist,
2330 0,molb->natoms_mol,FALSE,FALSE,graph);
2332 for(mol=0; mol<molb->nmol; mol++) {
2333 mk_mshift(fplog,graph,ePBC,box,x+as);
2335 shift_self(graph,box,x+as);
2336 /* The molecule is whole now.
2337 * We don't need the second mk_mshift call as in do_pbc_first,
2338 * since we no longer need this graph.
2341 as += molb->natoms_mol;
2349 void do_pbc_first_mtop(FILE *fplog,int ePBC,matrix box,
2350 gmx_mtop_t *mtop,rvec x[])
2352 low_do_pbc_mtop(fplog,ePBC,box,mtop,x,TRUE);
2355 void do_pbc_mtop(FILE *fplog,int ePBC,matrix box,
2356 gmx_mtop_t *mtop,rvec x[])
2358 low_do_pbc_mtop(fplog,ePBC,box,mtop,x,FALSE);
2361 void finish_run(FILE *fplog,t_commrec *cr,const char *confout,
2362 t_inputrec *inputrec,
2363 t_nrnb nrnb[],gmx_wallcycle_t wcycle,
2364 gmx_runtime_t *runtime,
2365 wallclock_gpu_t *gputimes,
2367 gmx_bool bWriteStat)
2370 t_nrnb *nrnb_tot=NULL;
2374 wallcycle_sum(cr,wcycle);
2380 MPI_Allreduce(nrnb->n,nrnb_tot->n,eNRNB,MPI_DOUBLE,MPI_SUM,
2381 cr->mpi_comm_mysim);
2389 #if defined(GMX_MPI) && !defined(GMX_THREAD_MPI)
2392 /* reduce nodetime over all MPI processes in the current simulation */
2394 MPI_Allreduce(&runtime->proctime,&sum,1,MPI_DOUBLE,MPI_SUM,
2395 cr->mpi_comm_mysim);
2396 runtime->proctime = sum;
2402 print_flop(fplog,nrnb_tot,&nbfs,&mflop);
2409 if ((cr->duty & DUTY_PP) && DOMAINDECOMP(cr))
2411 print_dd_statistics(cr,inputrec,fplog);
2423 snew(nrnb_all,cr->nnodes);
2424 nrnb_all[0] = *nrnb;
2425 for(s=1; s<cr->nnodes; s++)
2427 MPI_Recv(nrnb_all[s].n,eNRNB,MPI_DOUBLE,s,0,
2428 cr->mpi_comm_mysim,&stat);
2430 pr_load(fplog,cr,nrnb_all);
2435 MPI_Send(nrnb->n,eNRNB,MPI_DOUBLE,MASTERRANK(cr),0,
2436 cr->mpi_comm_mysim);
2443 wallcycle_print(fplog,cr->nnodes,cr->npmenodes,runtime->realtime,
2446 if (EI_DYNAMICS(inputrec->eI))
2448 delta_t = inputrec->delta_t;
2457 print_perf(fplog,runtime->proctime,runtime->realtime,
2458 cr->nnodes-cr->npmenodes,
2459 runtime->nsteps_done,delta_t,nbfs,mflop,
2464 print_perf(stderr,runtime->proctime,runtime->realtime,
2465 cr->nnodes-cr->npmenodes,
2466 runtime->nsteps_done,delta_t,nbfs,mflop,
2472 extern void initialize_lambdas(FILE *fplog,t_inputrec *ir,int *fep_state,real *lambda,double *lam0)
2474 /* this function works, but could probably use a logic rewrite to keep all the different
2475 types of efep straight. */
2478 t_lambda *fep = ir->fepvals;
2480 if ((ir->efep==efepNO) && (ir->bSimTemp == FALSE)) {
2481 for (i=0;i<efptNR;i++) {
2490 *fep_state = fep->init_fep_state; /* this might overwrite the checkpoint
2491 if checkpoint is set -- a kludge is in for now
2493 for (i=0;i<efptNR;i++)
2495 /* overwrite lambda state with init_lambda for now for backwards compatibility */
2496 if (fep->init_lambda>=0) /* if it's -1, it was never initializd */
2498 lambda[i] = fep->init_lambda;
2500 lam0[i] = lambda[i];
2505 lambda[i] = fep->all_lambda[i][*fep_state];
2507 lam0[i] = lambda[i];
2512 /* need to rescale control temperatures to match current state */
2513 for (i=0;i<ir->opts.ngtc;i++) {
2514 if (ir->opts.ref_t[i] > 0) {
2515 ir->opts.ref_t[i] = ir->simtempvals->temperatures[*fep_state];
2521 /* Send to the log the information on the current lambdas */
2524 fprintf(fplog,"Initial vector of lambda components:[ ");
2525 for (i=0;i<efptNR;i++)
2527 fprintf(fplog,"%10.4f ",lambda[i]);
2529 fprintf(fplog,"]\n");
2535 void init_md(FILE *fplog,
2536 t_commrec *cr,t_inputrec *ir,const output_env_t oenv,
2537 double *t,double *t0,
2538 real *lambda, int *fep_state, double *lam0,
2539 t_nrnb *nrnb,gmx_mtop_t *mtop,
2541 int nfile,const t_filenm fnm[],
2542 gmx_mdoutf_t **outf,t_mdebin **mdebin,
2543 tensor force_vir,tensor shake_vir,rvec mu_tot,
2544 gmx_bool *bSimAnn,t_vcm **vcm, t_state *state, unsigned long Flags)
2549 /* Initial values */
2550 *t = *t0 = ir->init_t;
2553 for(i=0;i<ir->opts.ngtc;i++)
2555 /* set bSimAnn if any group is being annealed */
2556 if(ir->opts.annealing[i]!=eannNO)
2563 update_annealing_target_temp(&(ir->opts),ir->init_t);
2566 /* Initialize lambda variables */
2567 initialize_lambdas(fplog,ir,fep_state,lambda,lam0);
2571 *upd = init_update(fplog,ir);
2577 *vcm = init_vcm(fplog,&mtop->groups,ir);
2580 if (EI_DYNAMICS(ir->eI) && !(Flags & MD_APPENDFILES))
2582 if (ir->etc == etcBERENDSEN)
2584 please_cite(fplog,"Berendsen84a");
2586 if (ir->etc == etcVRESCALE)
2588 please_cite(fplog,"Bussi2007a");
2596 *outf = init_mdoutf(nfile,fnm,Flags,cr,ir,oenv);
2598 *mdebin = init_mdebin((Flags & MD_APPENDFILES) ? NULL : (*outf)->fp_ene,
2599 mtop,ir, (*outf)->fp_dhdl);
2604 please_cite(fplog,"Fritsch12");
2605 please_cite(fplog,"Junghans10");
2607 /* Initiate variables */
2608 clear_mat(force_vir);
2609 clear_mat(shake_vir);