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 * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
54 #define PROBABILITYCUTOFF 100
55 /* we don't bother evaluating if events are more rare than exp(-100) = 3.7x10^-44 */
57 enum { ereTEMP, ereLAMBDA, ereENDSINGLE ,ereTL, ereNR };
58 const char *erename[ereNR] = { "temperature", "lambda", "end_single_marker", "temperature and lambda"};
60 typedef struct gmx_repl_ex
80 static gmx_bool repl_quantity(FILE *fplog,const gmx_multisim_t *ms,
81 struct gmx_repl_ex *re,int ere,real q)
89 gmx_sum_sim(ms->nsim,qall,ms);
92 for (s=1; s<ms->nsim; s++)
94 if (qall[s] != qall[0])
102 /* Set the replica exchange type and quantities */
105 snew(re->q[ere],re->nrepl);
106 for(s=0; s<ms->nsim; s++)
108 re->q[ere][s] = qall[s];
115 gmx_repl_ex_t init_replica_exchange(FILE *fplog,
116 const gmx_multisim_t *ms,
117 const t_state *state,
118 const t_inputrec *ir,
119 int nst, int nex, int init_seed)
123 struct gmx_repl_ex *re;
125 gmx_bool bLambda=FALSE;
127 fprintf(fplog,"\nInitializing Replica Exchange\n");
129 if (ms == NULL || ms->nsim == 1)
131 gmx_fatal(FARGS,"Nothing to exchange with only one replica, maybe you forgot to set the -multi option of mdrun?");
137 re->nrepl = ms->nsim;
138 snew(re->q,ereENDSINGLE);
140 fprintf(fplog,"Repl There are %d replicas:\n",re->nrepl);
142 check_multi_int(fplog,ms,state->natoms,"the number of atoms");
143 check_multi_int(fplog,ms,ir->eI,"the integrator");
144 check_multi_large_int(fplog,ms,ir->init_step+ir->nsteps,"init_step+nsteps");
145 check_multi_large_int(fplog,ms,(ir->init_step+nst-1)/nst,
146 "first exchange step: init_step/-replex");
147 check_multi_int(fplog,ms,ir->etc,"the temperature coupling");
148 check_multi_int(fplog,ms,ir->opts.ngtc,
149 "the number of temperature coupling groups");
150 check_multi_int(fplog,ms,ir->epc,"the pressure coupling");
151 check_multi_int(fplog,ms,ir->efep,"free energy");
152 check_multi_int(fplog,ms,ir->fepvals->n_lambda,"number of lambda states");
154 re->temp = ir->opts.ref_t[0];
155 for(i=1; (i<ir->opts.ngtc); i++)
157 if (ir->opts.ref_t[i] != re->temp)
159 fprintf(fplog,"\nWARNING: The temperatures of the different temperature coupling groups are not identical\n\n");
160 fprintf(stderr,"\nWARNING: The temperatures of the different temperature coupling groups are not identical\n\n");
165 bTemp = repl_quantity(fplog,ms,re,ereTEMP,re->temp);
166 if (ir->efep != efepNO)
168 bLambda = repl_quantity(fplog,ms,re,ereLAMBDA,(real)ir->fepvals->init_fep_state);
170 if (re->type == -1) /* nothing was assigned */
172 gmx_fatal(FARGS,"The properties of the %d systems are all the same, there is nothing to exchange",re->nrepl);
174 if (bLambda && bTemp) {
180 please_cite(fplog,"Sugita1999a");
181 if (ir->epc != epcNO)
184 fprintf(fplog,"Repl Using Constant Pressure REMD.\n");
185 please_cite(fplog,"Okabe2001a");
187 if (ir->etc == etcBERENDSEN)
189 gmx_fatal(FARGS,"REMD with the %s thermostat does not produce correct potential energy distributions, consider using the %s thermostat instead",
190 ETCOUPLTYPE(ir->etc),ETCOUPLTYPE(etcVRESCALE));
194 if (ir->fepvals->delta_lambda != 0) /* check this? */
196 gmx_fatal(FARGS,"delta_lambda is not zero");
201 snew(re->pres,re->nrepl);
202 if (ir->epct == epctSURFACETENSION)
204 pres = ir->ref_p[ZZ][ZZ];
212 if (ir->compress[i][i] != 0)
214 pres += ir->ref_p[i][i];
220 re->pres[re->repl] = pres;
221 gmx_sum_sim(re->nrepl,re->pres,ms);
224 /* Make an index for increasing replica order */
225 /* only makes sense if one or the other is varying, not both!
226 if both are varying, we trust the order the person gave. */
227 snew(re->ind,re->nrepl);
228 for(i=0; i<re->nrepl; i++)
233 if (re->type<ereENDSINGLE) {
235 for(i=0; i<re->nrepl; i++)
237 for(j=i+1; j<re->nrepl; j++)
239 if (re->q[re->type][re->ind[j]] < re->q[re->type][re->ind[i]])
242 re->ind[i] = re->ind[j];
245 else if (re->q[re->type][re->ind[j]] == re->q[re->type][re->ind[i]])
247 gmx_fatal(FARGS,"Two replicas have identical %ss",erename[re->type]);
253 /* keep track of all the swaps, starting with the initial placement. */
254 snew(re->allswaps,re->nrepl);
255 for(i=0; i<re->nrepl; i++)
257 re->allswaps[i] = re->ind[i];
263 fprintf(fplog,"\nReplica exchange in temperature\n");
264 for(i=0; i<re->nrepl; i++)
266 fprintf(fplog," %5.1f",re->q[re->type][re->ind[i]]);
271 fprintf(fplog,"\nReplica exchange in lambda\n");
272 for(i=0; i<re->nrepl; i++)
274 fprintf(fplog," %3d",(int)re->q[re->type][re->ind[i]]);
279 fprintf(fplog,"\nReplica exchange in temperature and lambda state\n");
280 for(i=0; i<re->nrepl; i++)
282 fprintf(fplog," %5.1f",re->q[ereTEMP][re->ind[i]]);
285 for(i=0; i<re->nrepl; i++)
287 fprintf(fplog," %5d",(int)re->q[ereLAMBDA][re->ind[i]]);
292 gmx_incons("Unknown replica exchange quantity");
296 fprintf(fplog,"\nRepl p");
297 for(i=0; i<re->nrepl; i++)
299 fprintf(fplog," %5.2f",re->pres[re->ind[i]]);
302 for(i=0; i<re->nrepl; i++)
304 if ((i > 0) && (re->pres[re->ind[i]] < re->pres[re->ind[i-1]]))
306 fprintf(fplog,"\nWARNING: The reference pressures decrease with increasing temperatures\n\n");
307 fprintf(stderr,"\nWARNING: The reference pressures decrease with increasing temperatures\n\n");
316 re->seed = make_seed();
322 gmx_sumi_sim(1,&(re->seed),ms);
326 re->seed = init_seed;
328 fprintf(fplog,"\nReplica exchange interval: %d\n",re->nst);
329 fprintf(fplog,"\nReplica random seed: %d\n",re->seed);
334 snew(re->prob_sum,re->nrepl);
335 snew(re->nexchange,re->nrepl);
336 snew(re->nmoves,re->nrepl);
337 for (i=0;i<re->nrepl;i++)
339 snew(re->nmoves[i],re->nrepl);
341 fprintf(fplog,"Replica exchange information below: x=exchange, pr=probability\n");
347 static void exchange_reals(const gmx_multisim_t *ms,int b,real *v,int n)
357 MPI_Sendrecv(v, n*sizeof(real),MPI_BYTE,MSRANK(ms,b),0,
358 buf,n*sizeof(real),MPI_BYTE,MSRANK(ms,b),0,
359 ms->mpi_comm_masters,MPI_STATUS_IGNORE);
364 MPI_Isend(v,n*sizeof(real),MPI_BYTE,MSRANK(ms,b),0,
365 ms->mpi_comm_masters,&mpi_req);
366 MPI_Recv(buf,n*sizeof(real),MPI_BYTE,MSRANK(ms,b),0,
367 ms->mpi_comm_masters,MPI_STATUS_IGNORE);
368 MPI_Wait(&mpi_req,MPI_STATUS_IGNORE);
380 static void exchange_ints(const gmx_multisim_t *ms,int b,int *v,int n)
389 MPI_Sendrecv(v, n*sizeof(int),MPI_BYTE,MSRANK(ms,b),0,
390 buf,n*sizeof(int),MPI_BYTE,MSRANK(ms,b),0,
391 ms->mpi_comm_masters,MPI_STATUS_IGNORE);
396 MPI_Isend(v,n*sizeof(int),MPI_BYTE,MSRANK(ms,b),0,
397 ms->mpi_comm_masters,&mpi_req);
398 MPI_Recv(buf,n*sizeof(int),MPI_BYTE,MSRANK(ms,b),0,
399 ms->mpi_comm_masters,MPI_STATUS_IGNORE);
400 MPI_Wait(&mpi_req,MPI_STATUS_IGNORE);
411 static void exchange_doubles(const gmx_multisim_t *ms,int b,double *v,int n)
421 MPI_Sendrecv(v, n*sizeof(double),MPI_BYTE,MSRANK(ms,b),0,
422 buf,n*sizeof(double),MPI_BYTE,MSRANK(ms,b),0,
423 ms->mpi_comm_masters,MPI_STATUS_IGNORE);
428 MPI_Isend(v,n*sizeof(double),MPI_BYTE,MSRANK(ms,b),0,
429 ms->mpi_comm_masters,&mpi_req);
430 MPI_Recv(buf,n*sizeof(double),MPI_BYTE,MSRANK(ms,b),0,
431 ms->mpi_comm_masters,MPI_STATUS_IGNORE);
432 MPI_Wait(&mpi_req,MPI_STATUS_IGNORE);
443 static void exchange_rvecs(const gmx_multisim_t *ms,int b,rvec *v,int n)
453 MPI_Sendrecv(v[0], n*sizeof(rvec),MPI_BYTE,MSRANK(ms,b),0,
454 buf[0],n*sizeof(rvec),MPI_BYTE,MSRANK(ms,b),0,
455 ms->mpi_comm_masters,MPI_STATUS_IGNORE);
460 MPI_Isend(v[0],n*sizeof(rvec),MPI_BYTE,MSRANK(ms,b),0,
461 ms->mpi_comm_masters,&mpi_req);
462 MPI_Recv(buf[0],n*sizeof(rvec),MPI_BYTE,MSRANK(ms,b),0,
463 ms->mpi_comm_masters,MPI_STATUS_IGNORE);
464 MPI_Wait(&mpi_req,MPI_STATUS_IGNORE);
469 copy_rvec(buf[i],v[i]);
475 static void exchange_state(const gmx_multisim_t *ms,int b,t_state *state)
477 /* When t_state changes, this code should be updated. */
479 ngtc = state->ngtc * state->nhchainlength;
480 nnhpres = state->nnhpres* state->nhchainlength;
481 exchange_rvecs(ms,b,state->box,DIM);
482 exchange_rvecs(ms,b,state->box_rel,DIM);
483 exchange_rvecs(ms,b,state->boxv,DIM);
484 exchange_reals(ms,b,&(state->veta),1);
485 exchange_reals(ms,b,&(state->vol0),1);
486 exchange_rvecs(ms,b,state->svir_prev,DIM);
487 exchange_rvecs(ms,b,state->fvir_prev,DIM);
488 exchange_rvecs(ms,b,state->pres_prev,DIM);
489 exchange_doubles(ms,b,state->nosehoover_xi,ngtc);
490 exchange_doubles(ms,b,state->nosehoover_vxi,ngtc);
491 exchange_doubles(ms,b,state->nhpres_xi,nnhpres);
492 exchange_doubles(ms,b,state->nhpres_vxi,nnhpres);
493 exchange_doubles(ms,b,state->therm_integral,state->ngtc);
494 exchange_rvecs(ms,b,state->x,state->natoms);
495 exchange_rvecs(ms,b,state->v,state->natoms);
496 exchange_rvecs(ms,b,state->sd_X,state->natoms);
499 static void copy_rvecs(rvec *s,rvec *d,int n)
507 copy_rvec(s[i],d[i]);
512 static void copy_doubles(const double *s,double *d,int n)
525 static void copy_reals(const real *s,real *d,int n)
538 static void copy_ints(const int *s,int *d,int n)
551 #define scopy_rvecs(v,n) copy_rvecs(state->v,state_local->v,n);
552 #define scopy_doubles(v,n) copy_doubles(state->v,state_local->v,n);
553 #define scopy_reals(v,n) copy_reals(state->v,state_local->v,n);
554 #define scopy_ints(v,n) copy_ints(state->v,state_local->v,n);
556 static void copy_state_nonatomdata(t_state *state,t_state *state_local)
558 /* When t_state changes, this code should be updated. */
560 ngtc = state->ngtc * state->nhchainlength;
561 nnhpres = state->nnhpres* state->nhchainlength;
562 scopy_rvecs(box,DIM);
563 scopy_rvecs(box_rel,DIM);
564 scopy_rvecs(boxv,DIM);
565 state_local->veta = state->veta;
566 state_local->vol0 = state->vol0;
567 scopy_rvecs(svir_prev,DIM);
568 scopy_rvecs(fvir_prev,DIM);
569 scopy_rvecs(pres_prev,DIM);
570 scopy_doubles(nosehoover_xi,ngtc);
571 scopy_doubles(nosehoover_vxi,ngtc);
572 scopy_doubles(nhpres_xi,nnhpres);
573 scopy_doubles(nhpres_vxi,nnhpres);
574 scopy_doubles(therm_integral,state->ngtc);
575 scopy_rvecs(x,state->natoms);
576 scopy_rvecs(v,state->natoms);
577 scopy_rvecs(sd_X,state->natoms);
578 copy_ints(&(state->fep_state),&(state_local->fep_state),1);
579 scopy_reals(lambda,efptNR);
582 static void scale_velocities(t_state *state,real fac)
588 for(i=0; i<state->natoms; i++)
590 svmul(fac,state->v[i],state->v[i]);
595 static void pd_collect_state(const t_commrec *cr,t_state *state)
601 fprintf(debug,"Collecting state before exchange\n");
603 shift = cr->nnodes - cr->npmenodes - 1;
604 move_rvecs(cr,FALSE,FALSE,GMX_LEFT,GMX_RIGHT,state->x,NULL,shift,NULL);
607 move_rvecs(cr,FALSE,FALSE,GMX_LEFT,GMX_RIGHT,state->v,NULL,shift,NULL);
611 move_rvecs(cr,FALSE,FALSE,GMX_LEFT,GMX_RIGHT,state->sd_X,NULL,shift,NULL);
615 static void print_matrix(FILE *fplog,const char *leg,int n,int **nmoves, int *nattempt)
620 ntot = nattempt[0] + nattempt[1];
622 fprintf(fplog," Empirical Transition Matrix\n");
625 fprintf(fplog,"%8d",(i+1));
633 if (nmoves[i][j] > 0)
635 Tprint = nmoves[i][j]/(2.0*ntot);
637 fprintf(fplog,"%8.4f",Tprint);
639 fprintf(fplog,"%3d\n",i);
643 static void print_ind(FILE *fplog,const char *leg,int n,int *ind,gmx_bool *bEx)
647 fprintf(fplog,"Repl %2s %2d",leg,ind[0]);
650 fprintf(fplog," %c %2d",(bEx!=0 && bEx[i]) ? 'x' : ' ',ind[i]);
655 static void print_allswitchind(FILE *fplog,int n,int *ind,int *pind, int *allswaps)
660 snew(tmpswap,n); /* need to save the data */
663 tmpswap[i] = allswaps[i];
667 allswaps[i] = tmpswap[pind[i]];
670 fprintf(fplog,"\nAccepted Exchanges: ");
673 fprintf(fplog,"%d ",pind[i]);
677 fprintf(fplog,"Order After Exchange: ");
680 fprintf(fplog,"%d ",allswaps[i]);
682 fprintf(fplog,"\n\n");
687 static void print_prob(FILE *fplog,const char *leg,int n,real *prob)
692 fprintf(fplog,"Repl %2s ",leg);
697 sprintf(buf,"%4.2f",prob[i]);
698 fprintf(fplog," %3s",buf[0]=='1' ? "1.0" : buf+1);
708 static void print_count(FILE *fplog,const char *leg,int n,int *count)
712 fprintf(fplog,"Repl %2s ",leg);
715 fprintf(fplog," %4d",count[i]);
720 static real calc_delta(FILE *fplog, gmx_bool bPrint, struct gmx_repl_ex *re, real *Epot, real **df, real* Vol, real *beta, int a, int b, int ap, int bp) {
722 real ediff,dpV,delta=0;
724 /* Two cases; we are permuted and not. In all cases, setting ap = a and bp = b will reduce
725 to the non permuted case */
731 * Okabe et. al. Chem. Phys. Lett. 335 (2001) 435-439
733 ediff = Epot[b] - Epot[a];
734 delta = -(beta[bp] - beta[ap])*ediff;
737 /* two cases: when we are permuted, and not. */
739 ediff = E_new - E_old
740 = [H_b(x_a) + H_a(x_b)] - [H_b(x_b) + H_a(x_a)]
741 = [H_b(x_a) - H_a(x_a)] + [H_a(x_b) - H_b(x_b)]
742 = df[b][a] + df[a][b] */
744 ediff = E_new - E_old
745 = [H_bp(x_a) + H_ap(x_b)] - [H_bp(x_b) + H_ap(x_a)]
746 = [H_bp(x_a) - H_ap(x_a)] + [H_ap(x_b) - H_bp(x_b)]
747 = [H_bp(x_a) - H_a(x_a) + H_a(x_a) - H_ap(x_a)] + [H_ap(x_b) - H_b(x_b) + H_b(x_b) - H_bp(x_b)]
748 = [H_bp(x_a) - H_a(x_a)] - [H_ap(x_a) - H_a(x_a)] + [H_ap(x_b) - H_b(x_b)] - H_bp(x_b) - H_b(x_b)]
749 = (df[bp][a] - df[ap][a]) + (df[ap][b] - df[bp][b]) */
750 ediff = (df[bp][a] - df[ap][a]) + (df[ap][b] - df[bp][b]);
751 delta = ediff*beta[a]; /* assume all same temperature in this case */
755 /* delta = reduced E_new - reduced E_old
756 = [beta_b H_b(x_a) + beta_a H_a(x_b)] - [beta_b H_b(x_b) + beta_a H_a(x_a)]
757 = [beta_b H_b(x_a) - beta_a H_a(x_a)] + [beta_a H_a(x_b) - beta_b H_b(x_b)]
758 = [beta_b dH_b(x_a) + beta_b H_a(x_a) - beta_a H_a(x_a)] +
759 [beta_a dH_a(x_b) + beta_a H_b(x_b) - beta_b H_b(x_b)]
760 = [beta_b dH_b(x_a) + [beta_a dH_a(x_b) +
761 beta_b (H_a(x_a) - H_b(x_b)]) - beta_a (H_a(x_a) - H_b(x_b))
762 = beta_b dH_b(x_a) + beta_a dH_a(x_b) - (beta_b - beta_a)(H_b(x_b) - H_a(x_a) */
763 /* delta = beta[b]*df[b][a] + beta[a]*df[a][b] - (beta[b] - beta[a])*(Epot[b] - Epot[a]; */
764 /* permuted (big breath!) */
765 /* delta = reduced E_new - reduced E_old
766 = [beta_bp H_bp(x_a) + beta_ap H_ap(x_b)] - [beta_bp H_bp(x_b) + beta_ap H_ap(x_a)]
767 = [beta_bp H_bp(x_a) - beta_ap H_ap(x_a)] + [beta_ap H_ap(x_b) - beta_bp H_bp(x_b)]
768 = [beta_bp H_bp(x_a) - beta_ap H_ap(x_a)] + [beta_ap H_ap(x_b) - beta_bp H_bp(x_b)]
769 - beta_pb H_a(x_a) + beta_ap H_a(x_a) + beta_pb H_a(x_a) - beta_ap H_a(x_a)
770 - beta_ap H_b(x_b) + beta_bp H_b(x_b) + beta_ap H_b(x_b) - beta_bp H_b(x_b)
771 = [(beta_bp H_bp(x_a) - beta_bp H_a(x_a)) - (beta_ap H_ap(x_a) - beta_ap H_a(x_a))] +
772 [(beta_ap H_ap(x_b) - beta_ap H_b(x_b)) - (beta_bp H_bp(x_b) - beta_bp H_b(x_b))]
773 + beta_pb H_a(x_a) - beta_ap H_a(x_a) + beta_ap H_b(x_b) - beta_bp H_b(x_b)
774 = [beta_bp (H_bp(x_a) - H_a(x_a)) - beta_ap (H_ap(x_a) - H_a(x_a))] +
775 [beta_ap (H_ap(x_b) - H_b(x_b)) - beta_bp (H_bp(x_b) - H_b(x_b))]
776 + beta_pb (H_a(x_a) - H_b(x_b)) - beta_ap (H_a(x_a) - H_b(x_b))
777 = ([beta_bp df[bp][a] - beta_ap df[ap][a]) + beta_ap df[ap][b] - beta_bp df[bp][b])
778 + (beta_pb-beta_ap)(H_a(x_a) - H_b(x_b)) */
779 delta = beta[bp]*(df[bp][a] - df[bp][b]) + beta[ap]*(df[ap][b] - df[ap][a]) - (beta[bp]-beta[ap])*(Epot[b]-Epot[a]);
782 gmx_incons("Unknown replica exchange quantity");
786 fprintf(fplog,"Repl %d <-> %d dE_term = %10.3e (kT)\n",a,b,delta);
790 /* revist the calculation for 5.0. Might be some improvements. */
791 dpV = (beta[ap]*re->pres[ap]-beta[bp]*re->pres[bp])*(Vol[b]-Vol[a])/PRESFAC;
794 fprintf(fplog," dpV = %10.3e d = %10.3e\nb",dpV,delta + dpV);
801 static void get_replica_exchange(FILE *fplog,const gmx_multisim_t *ms,
802 struct gmx_repl_ex *re,gmx_enerdata_t *enerd,real vol,
803 gmx_large_int_t step,real time,int *pind)
805 int m,i,a,b,ap,bp,i0,i1,tmp;
806 real *Epot=NULL,*Vol=NULL,**flambda=NULL,*beta=NULL,*prob;
807 real ediff=0,delta=0,dpV=0;
808 gmx_bool *bEx,bPrint,bMultiEx;
809 gmx_bool bEpot=FALSE;
810 gmx_bool bFLambda=FALSE;
813 bMultiEx = (re->nex > 1); /* multiple exchanges at each state */
814 fprintf(fplog,"Replica exchange at step " gmx_large_int_pfmt " time %g\n",step,time);
816 snew(beta,re->nrepl);
824 if ((re->type == ereTEMP || re->type == ereTL))
827 snew(Epot,re->nrepl);
828 Epot[re->repl] = enerd->term[F_EPOT];
829 /* temperatures of different states*/
830 for (i=0;i<re->nrepl;i++)
832 beta[i] = 1.0/(re->q[ereTEMP][i]*BOLTZ);
837 for (i=0;i<re->nrepl;i++)
839 beta[i] = 1.0/(re->temp*BOLTZ); /* we have a single temperature */
842 if (re->type == ereLAMBDA || re->type == ereTL)
845 /* lambda differences. */
846 /* flambda[i][j] is the energy of the jth simulation in the ith Hamiltonian
847 minus the energy of the jth simulation in the jth Hamiltonian */
848 snew(flambda,re->nrepl);
849 for (i=0;i<re->nrepl;i++)
851 snew(flambda[i],re->nrepl);
852 flambda[i][re->repl] = (enerd->enerpart_lambda[(int)re->q[ereLAMBDA][i]+1]-enerd->enerpart_lambda[0]);
856 /* now actually do the communication */
859 gmx_sum_sim(re->nrepl,Vol,ms);
863 gmx_sum_sim(re->nrepl,Epot,ms);
867 for (i=0;i<re->nrepl;i++)
869 gmx_sum_sim(re->nrepl,flambda[i],ms);
873 snew(prob,re->nrepl);
875 /* make a duplicate set of indices for shuffling */
876 for(i=0;i<re->nrepl;i++)
878 pind[i] = re->ind[i];
883 /* multiple random switch exchange */
884 for (i=0;i<re->nex;i++)
886 /* randomly select a pair */
887 /* find out which state it is from, and what label that state currently has */
888 i0 = (int)(re->nrepl*rando(&(re->seed)));
889 i1 = (int)(re->nrepl*rando(&(re->seed)));
893 continue; /* got the same pair, back up and do it again */
901 bPrint = FALSE; /* too noisy */
902 delta = calc_delta(fplog,bPrint,re,Epot,flambda,Vol,beta,a,b,ap,bp); /* calculate the energy difference */
904 /* we actually only use the first space, since there are actually many switches between pairs. */
914 if (delta > PROBABILITYCUTOFF)
920 prob[0] = exp(-delta);
922 /* roll a number to determine if accepted */
923 bEx[0] = (rando(&(re->seed)) < prob[0]);
925 re->prob_sum[0] += prob[0];
929 /* swap the states */
935 re->nattempt[0]++; /* keep track of total permutation trials here */
936 print_allswitchind(fplog,re->nrepl,re->ind,pind,re->allswaps);
940 /* standard nearest neighbor replica exchange */
941 m = (step / re->nst) % 2;
942 for(i=1; i<re->nrepl; i++)
947 bPrint = (re->repl==a || re->repl==b);
950 delta = calc_delta(fplog,bPrint,re,Epot,flambda,Vol,beta,a,b,a,b);
958 if (delta > PROBABILITYCUTOFF)
964 prob[i] = exp(-delta);
966 /* roll a number to determine if accepted */
967 bEx[i] = (rando(&(re->seed)) < prob[i]);
969 re->prob_sum[i] += prob[i];
985 /* print some statistics */
986 print_ind(fplog,"ex",re->nrepl,re->ind,bEx);
987 print_prob(fplog,"pr",re->nrepl,prob);
992 /* record which moves were made and accepted */
993 for (i=0;i<re->nrepl;i++)
995 re->nmoves[re->ind[i]][pind[i]] +=1;
996 re->nmoves[pind[i]][re->ind[i]] +=1;
1006 if ((re->type == ereTEMP || re->type == ereTL))
1010 if ((re->type == ereLAMBDA || re->type == ereTL))
1012 for (i=0;i<re->nrepl;i++)
1020 static void write_debug_x(t_state *state)
1026 for(i=0; i<state->natoms; i+=10)
1028 fprintf(debug,"dx %5d %10.5f %10.5f %10.5f\n",i,state->x[i][XX],state->x[i][YY],state->x[i][ZZ]);
1033 static void cyclic_decomposition(FILE *fplog, int *pind, int **cyclic, int n, int *nswap)
1041 /* compute cyclic decompositions */
1051 for (i=0;i<n;i++) /* one cycle for each replica */
1062 for (j=0;j<n;j++) /* potentially all cycles are part, but we will break first */
1064 p = pind[p]; /* start permuting */
1072 break; /* we've reached the original element, the cycle is complete, and we marked the end. */
1076 cyclic[i][c] = p; /* each permutation gives a new member of the cycle */
1082 *nswap = maxlen - 1;
1088 fprintf(fplog,"Cycle %d:",i);
1091 if (cyclic[i][j] < 0)
1095 fprintf(fplog,"%2d",cyclic[i][j]);
1097 fprintf(fplog,"\n");
1103 static void compute_exchange_order(FILE *fplog, int **cyclic,int **order, int n, int maxswap)
1109 snew(order[i],maxswap);
1110 for (j=0;j<maxswap;j++)
1115 for (j=0;j<maxswap;j++)
1119 if (cyclic[i][j+1] >= 0)
1121 order[cyclic[i][j+1]][j] = cyclic[i][j];
1122 order[cyclic[i][j]][j] = cyclic[i][j+1];
1127 if (order[i][j] < 0)
1129 order[i][j] = i; /* if it's not exchanging, it should stay this round*/
1135 fprintf(fplog,"Replica Exchange Order\n");
1138 fprintf(fplog,"Replica %d:",i);
1139 for (j=0;j<maxswap;j++)
1141 if (order[i][j] < 0) break;
1142 fprintf(fplog,"%2d",order[i][j]);
1144 fprintf(fplog,"\n");
1150 gmx_bool replica_exchange(FILE *fplog,const t_commrec *cr,struct gmx_repl_ex *re,
1151 t_state *state,gmx_enerdata_t *enerd,
1152 t_state *state_local,gmx_large_int_t step,real time)
1155 int exchange=-1,shift;
1157 int *exchanges=NULL;
1160 gmx_bool bExchanged=FALSE;
1165 snew(exchanges,re->nrepl);
1166 get_replica_exchange(fplog,ms,re,enerd,det(state->box),step,time,exchanges);
1167 bExchanged = (exchanges[re->repl] != re->nrepl); /* only mark as exchanged if it has a shuffled index */
1168 snew(cyclic,re->nrepl);
1169 snew(order,re->nrepl);
1171 /* identify the cyclic decomposition of the permutation (very easy if neighbor replica exchange) */
1172 cyclic_decomposition(fplog,exchanges,cyclic,re->nrepl,&maxswap);
1174 /* now translate the decompsition into a replica exchange order at each step */
1175 compute_exchange_order(fplog,cyclic,order,re->nrepl,maxswap);
1177 sfree(cyclic); /* don't need this anymore */
1182 MPI_Bcast(&bExchanged,sizeof(gmx_bool),MPI_BYTE,MASTERRANK(cr),
1183 cr->mpi_comm_mygroup);
1188 /* Exchange the states */
1192 /* Collect the global state on the master node */
1193 if (DOMAINDECOMP(cr))
1195 dd_collect_state(cr->dd,state_local,state);
1199 pd_collect_state(cr,state);
1205 for (i=0;i<maxswap;i++) /* there will be only one swap cycle with standard replica exchange */
1207 exchange = order[ms->sim][i];
1209 if (exchange != ms->sim)
1211 /* Exchange the global states between the master nodes */
1214 fprintf(debug,"Exchanging %d with %d\n",ms->sim,exchange);
1216 exchange_state(ms,exchange,state);
1219 if (re->type == ereTEMP || re->type == ereTL)
1221 scale_velocities(state,sqrt(re->q[ereTEMP][ms->sim]/re->q[ereTEMP][exchanges[ms->sim]]));
1226 /* With domain decomposition the global state is distributed later */
1227 if (!DOMAINDECOMP(cr))
1229 /* Copy the global state to the local state data structure */
1230 copy_state_nonatomdata(state,state_local);
1234 bcast_state(cr,state,FALSE);
1241 void print_replica_exchange_statistics(FILE *fplog,struct gmx_repl_ex *re)
1246 fprintf(fplog,"\nReplica exchange statistics\n");
1250 fprintf(fplog,"Repl %d attempts, %d odd, %d even\n",
1251 re->nattempt[0]+re->nattempt[1],re->nattempt[1],re->nattempt[0]);
1253 snew(prob,re->nrepl);
1254 for(i=1; i<re->nrepl; i++)
1256 if (re->nattempt[i%2] == 0)
1262 prob[i] = re->prob_sum[i]/re->nattempt[i%2];
1265 print_ind(fplog,"",re->nrepl,re->ind,NULL);
1266 print_prob(fplog,"",re->nrepl,prob);
1268 fprintf(fplog,"Repl number of exchanges:\n");
1269 print_ind(fplog,"",re->nrepl,re->ind,NULL);
1270 print_count(fplog,"",re->nrepl,re->nexchange);
1272 fprintf(fplog,"Repl average number of exchanges:\n");
1273 for(i=1; i<re->nrepl; i++)
1275 if (re->nattempt[i%2] == 0)
1281 prob[i] = ((real)re->nexchange[i])/re->nattempt[i%2];
1284 print_ind(fplog,"",re->nrepl,re->ind,NULL);
1285 print_prob(fplog,"",re->nrepl,prob);
1287 fprintf(fplog,"\n");
1289 /* print the transition matrix */
1290 print_matrix(fplog,"",re->nrepl,re->nmoves,re->nattempt);