2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2011,2012,2013,2014,2015,2016,2017, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
48 #include "gromacs/domdec/domdec.h"
49 #include "gromacs/gmxlib/network.h"
50 #include "gromacs/math/units.h"
51 #include "gromacs/math/vec.h"
52 #include "gromacs/mdlib/main.h"
53 #include "gromacs/mdtypes/commrec.h"
54 #include "gromacs/mdtypes/inputrec.h"
55 #include "gromacs/mdtypes/md_enums.h"
56 #include "gromacs/mdtypes/state.h"
57 #include "gromacs/random/threefry.h"
58 #include "gromacs/random/uniformintdistribution.h"
59 #include "gromacs/random/uniformrealdistribution.h"
60 #include "gromacs/utility/fatalerror.h"
61 #include "gromacs/utility/pleasecite.h"
62 #include "gromacs/utility/smalloc.h"
65 #define PROBABILITYCUTOFF 100
66 /* we don't bother evaluating if events are more rare than exp(-100) = 3.7x10^-44 */
68 //! Rank in the multisimulaiton
69 #define MSRANK(ms, nodeid) (nodeid)
72 ereTEMP
, ereLAMBDA
, ereENDSINGLE
, ereTL
, ereNR
74 const char *erename
[ereNR
] = { "temperature", "lambda", "end_single_marker", "temperature and lambda"};
75 /* end_single_marker merely notes the end of single variable replica exchange. All types higher than
76 it are multiple replica exchange methods */
77 /* Eventually, should add 'pressure', 'temperature and pressure', 'lambda_and_pressure', 'temperature_lambda_pressure'?;
78 Let's wait until we feel better about the pressure control methods giving exact ensembles. Right now, we assume constant pressure */
80 typedef struct gmx_repl_ex
82 int repl
; /* replica ID */
83 int nrepl
; /* total number of replica */
84 real temp
; /* temperature */
85 int type
; /* replica exchange type from ere enum */
86 real
**q
; /* quantity, e.g. temperature or lambda; first index is ere, second index is replica ID */
87 gmx_bool bNPT
; /* use constant pressure and temperature */
88 real
*pres
; /* replica pressures */
89 int *ind
; /* replica indices */
90 int *allswaps
; /* used for keeping track of all the replica swaps */
91 int nst
; /* replica exchange interval (number of steps) */
92 int nex
; /* number of exchanges per interval */
93 int seed
; /* random seed */
94 int nattempt
[2]; /* number of even and odd replica change attempts */
95 real
*prob_sum
; /* sum of probabilities */
96 int **nmoves
; /* number of moves between replicas i and j */
97 int *nexchange
; /* i-th element of the array is the number of exchanges between replica i-1 and i */
99 /* these are helper arrays for replica exchange; allocated here so they
100 don't have to be allocated each time */
108 /* helper arrays to hold the quantities that are exchanged */
117 static gmx_bool
repl_quantity(const gmx_multisim_t
*ms
,
118 struct gmx_repl_ex
*re
, int ere
, real q
)
124 snew(qall
, ms
->nsim
);
126 gmx_sum_sim(ms
->nsim
, qall
, ms
);
129 for (s
= 1; s
< ms
->nsim
; s
++)
131 if (qall
[s
] != qall
[0])
139 /* Set the replica exchange type and quantities */
142 snew(re
->q
[ere
], re
->nrepl
);
143 for (s
= 0; s
< ms
->nsim
; s
++)
145 re
->q
[ere
][s
] = qall
[s
];
153 init_replica_exchange(FILE *fplog
,
154 const gmx_multisim_t
*ms
,
155 int numAtomsInSystem
,
156 const t_inputrec
*ir
,
157 const ReplicaExchangeParameters
&replExParams
)
161 struct gmx_repl_ex
*re
;
163 gmx_bool bLambda
= FALSE
;
165 fprintf(fplog
, "\nInitializing Replica Exchange\n");
167 if (ms
== nullptr || ms
->nsim
== 1)
169 gmx_fatal(FARGS
, "Nothing to exchange with only one replica, maybe you forgot to set the -multi option of mdrun?");
171 if (!EI_DYNAMICS(ir
->eI
))
173 gmx_fatal(FARGS
, "Replica exchange is only supported by dynamical simulations");
174 /* Note that PAR(cr) is defined by cr->nnodes > 1, which is
175 * distinct from MULTISIM(cr). A multi-simulation only runs
176 * with real MPI parallelism, but this does not imply PAR(cr)
179 * Since we are using a dynamical integrator, the only
180 * decomposition is DD, so PAR(cr) and DOMAINDECOMP(cr) are
181 * synonymous. The only way for cr->nnodes > 1 to be true is
182 * if we are using DD. */
188 re
->nrepl
= ms
->nsim
;
189 snew(re
->q
, ereENDSINGLE
);
191 fprintf(fplog
, "Repl There are %d replicas:\n", re
->nrepl
);
193 /* We only check that the number of atoms in the systms match.
194 * This, of course, do not guarantee that the systems are the same,
195 * but it does guarantee that we can perform replica exchange.
197 check_multi_int(fplog
, ms
, numAtomsInSystem
, "the number of atoms", FALSE
);
198 check_multi_int(fplog
, ms
, ir
->eI
, "the integrator", FALSE
);
199 check_multi_int64(fplog
, ms
, ir
->init_step
+ir
->nsteps
, "init_step+nsteps", FALSE
);
200 const int nst
= replExParams
.exchangeInterval
;
201 check_multi_int64(fplog
, ms
, (ir
->init_step
+nst
-1)/nst
,
202 "first exchange step: init_step/-replex", FALSE
);
203 check_multi_int(fplog
, ms
, ir
->etc
, "the temperature coupling", FALSE
);
204 check_multi_int(fplog
, ms
, ir
->opts
.ngtc
,
205 "the number of temperature coupling groups", FALSE
);
206 check_multi_int(fplog
, ms
, ir
->epc
, "the pressure coupling", FALSE
);
207 check_multi_int(fplog
, ms
, ir
->efep
, "free energy", FALSE
);
208 check_multi_int(fplog
, ms
, ir
->fepvals
->n_lambda
, "number of lambda states", FALSE
);
210 re
->temp
= ir
->opts
.ref_t
[0];
211 for (i
= 1; (i
< ir
->opts
.ngtc
); i
++)
213 if (ir
->opts
.ref_t
[i
] != re
->temp
)
215 fprintf(fplog
, "\nWARNING: The temperatures of the different temperature coupling groups are not identical\n\n");
216 fprintf(stderr
, "\nWARNING: The temperatures of the different temperature coupling groups are not identical\n\n");
221 bTemp
= repl_quantity(ms
, re
, ereTEMP
, re
->temp
);
222 if (ir
->efep
!= efepNO
)
224 bLambda
= repl_quantity(ms
, re
, ereLAMBDA
, (real
)ir
->fepvals
->init_fep_state
);
226 if (re
->type
== -1) /* nothing was assigned */
228 gmx_fatal(FARGS
, "The properties of the %d systems are all the same, there is nothing to exchange", re
->nrepl
);
230 if (bLambda
&& bTemp
)
237 please_cite(fplog
, "Sugita1999a");
238 if (ir
->epc
!= epcNO
)
241 fprintf(fplog
, "Repl Using Constant Pressure REMD.\n");
242 please_cite(fplog
, "Okabe2001a");
244 if (ir
->etc
== etcBERENDSEN
)
246 gmx_fatal(FARGS
, "REMD with the %s thermostat does not produce correct potential energy distributions, consider using the %s thermostat instead",
247 ETCOUPLTYPE(ir
->etc
), ETCOUPLTYPE(etcVRESCALE
));
252 if (ir
->fepvals
->delta_lambda
!= 0) /* check this? */
254 gmx_fatal(FARGS
, "delta_lambda is not zero");
259 snew(re
->pres
, re
->nrepl
);
260 if (ir
->epct
== epctSURFACETENSION
)
262 pres
= ir
->ref_p
[ZZ
][ZZ
];
268 for (i
= 0; i
< DIM
; i
++)
270 if (ir
->compress
[i
][i
] != 0)
272 pres
+= ir
->ref_p
[i
][i
];
278 re
->pres
[re
->repl
] = pres
;
279 gmx_sum_sim(re
->nrepl
, re
->pres
, ms
);
282 /* Make an index for increasing replica order */
283 /* only makes sense if one or the other is varying, not both!
284 if both are varying, we trust the order the person gave. */
285 snew(re
->ind
, re
->nrepl
);
286 for (i
= 0; i
< re
->nrepl
; i
++)
291 if (re
->type
< ereENDSINGLE
)
294 for (i
= 0; i
< re
->nrepl
; i
++)
296 for (j
= i
+1; j
< re
->nrepl
; j
++)
298 if (re
->q
[re
->type
][re
->ind
[j
]] < re
->q
[re
->type
][re
->ind
[i
]])
300 /* Unordered replicas are supposed to work, but there
301 * is still an issues somewhere.
302 * Note that at this point still re->ind[i]=i.
304 gmx_fatal(FARGS
, "Replicas with indices %d < %d have %ss %g > %g, please order your replicas on increasing %s",
307 re
->q
[re
->type
][i
], re
->q
[re
->type
][j
],
311 re
->ind
[i
] = re
->ind
[j
];
314 else if (re
->q
[re
->type
][re
->ind
[j
]] == re
->q
[re
->type
][re
->ind
[i
]])
316 gmx_fatal(FARGS
, "Two replicas have identical %ss", erename
[re
->type
]);
322 /* keep track of all the swaps, starting with the initial placement. */
323 snew(re
->allswaps
, re
->nrepl
);
324 for (i
= 0; i
< re
->nrepl
; i
++)
326 re
->allswaps
[i
] = re
->ind
[i
];
332 fprintf(fplog
, "\nReplica exchange in temperature\n");
333 for (i
= 0; i
< re
->nrepl
; i
++)
335 fprintf(fplog
, " %5.1f", re
->q
[re
->type
][re
->ind
[i
]]);
337 fprintf(fplog
, "\n");
340 fprintf(fplog
, "\nReplica exchange in lambda\n");
341 for (i
= 0; i
< re
->nrepl
; i
++)
343 fprintf(fplog
, " %3d", (int)re
->q
[re
->type
][re
->ind
[i
]]);
345 fprintf(fplog
, "\n");
348 fprintf(fplog
, "\nReplica exchange in temperature and lambda state\n");
349 for (i
= 0; i
< re
->nrepl
; i
++)
351 fprintf(fplog
, " %5.1f", re
->q
[ereTEMP
][re
->ind
[i
]]);
353 fprintf(fplog
, "\n");
354 for (i
= 0; i
< re
->nrepl
; i
++)
356 fprintf(fplog
, " %5d", (int)re
->q
[ereLAMBDA
][re
->ind
[i
]]);
358 fprintf(fplog
, "\n");
361 gmx_incons("Unknown replica exchange quantity");
365 fprintf(fplog
, "\nRepl p");
366 for (i
= 0; i
< re
->nrepl
; i
++)
368 fprintf(fplog
, " %5.2f", re
->pres
[re
->ind
[i
]]);
371 for (i
= 0; i
< re
->nrepl
; i
++)
373 if ((i
> 0) && (re
->pres
[re
->ind
[i
]] < re
->pres
[re
->ind
[i
-1]]))
375 fprintf(fplog
, "\nWARNING: The reference pressures decrease with increasing temperatures\n\n");
376 fprintf(stderr
, "\nWARNING: The reference pressures decrease with increasing temperatures\n\n");
381 if (replExParams
.randomSeed
== -1)
385 re
->seed
= static_cast<int>(gmx::makeRandomSeed());
391 gmx_sumi_sim(1, &(re
->seed
), ms
);
395 re
->seed
= replExParams
.randomSeed
;
397 fprintf(fplog
, "\nReplica exchange interval: %d\n", re
->nst
);
398 fprintf(fplog
, "\nReplica random seed: %d\n", re
->seed
);
403 snew(re
->prob_sum
, re
->nrepl
);
404 snew(re
->nexchange
, re
->nrepl
);
405 snew(re
->nmoves
, re
->nrepl
);
406 for (i
= 0; i
< re
->nrepl
; i
++)
408 snew(re
->nmoves
[i
], re
->nrepl
);
410 fprintf(fplog
, "Replica exchange information below: ex and x = exchange, pr = probability\n");
412 /* generate space for the helper functions so we don't have to snew each time */
414 snew(re
->destinations
, re
->nrepl
);
415 snew(re
->incycle
, re
->nrepl
);
416 snew(re
->tmpswap
, re
->nrepl
);
417 snew(re
->cyclic
, re
->nrepl
);
418 snew(re
->order
, re
->nrepl
);
419 for (i
= 0; i
< re
->nrepl
; i
++)
421 snew(re
->cyclic
[i
], re
->nrepl
+1);
422 snew(re
->order
[i
], re
->nrepl
);
424 /* allocate space for the functions storing the data for the replicas */
425 /* not all of these arrays needed in all cases, but they don't take
426 up much space, since the max size is nrepl**2 */
427 snew(re
->prob
, re
->nrepl
);
428 snew(re
->bEx
, re
->nrepl
);
429 snew(re
->beta
, re
->nrepl
);
430 snew(re
->Vol
, re
->nrepl
);
431 snew(re
->Epot
, re
->nrepl
);
432 snew(re
->de
, re
->nrepl
);
433 for (i
= 0; i
< re
->nrepl
; i
++)
435 snew(re
->de
[i
], re
->nrepl
);
437 re
->nex
= replExParams
.numExchanges
;
441 static void exchange_reals(const gmx_multisim_t gmx_unused
*ms
, int gmx_unused b
, real
*v
, int n
)
451 MPI_Sendrecv(v, n*sizeof(real),MPI_BYTE,MSRANK(ms,b),0,
452 buf,n*sizeof(real),MPI_BYTE,MSRANK(ms,b),0,
453 ms->mpi_comm_masters,MPI_STATUS_IGNORE);
458 MPI_Isend(v
, n
*sizeof(real
), MPI_BYTE
, MSRANK(ms
, b
), 0,
459 ms
->mpi_comm_masters
, &mpi_req
);
460 MPI_Recv(buf
, n
*sizeof(real
), MPI_BYTE
, MSRANK(ms
, b
), 0,
461 ms
->mpi_comm_masters
, MPI_STATUS_IGNORE
);
462 MPI_Wait(&mpi_req
, MPI_STATUS_IGNORE
);
465 for (i
= 0; i
< n
; i
++)
474 static void exchange_doubles(const gmx_multisim_t gmx_unused
*ms
, int gmx_unused b
, double *v
, int n
)
484 MPI_Sendrecv(v, n*sizeof(double),MPI_BYTE,MSRANK(ms,b),0,
485 buf,n*sizeof(double),MPI_BYTE,MSRANK(ms,b),0,
486 ms->mpi_comm_masters,MPI_STATUS_IGNORE);
491 MPI_Isend(v
, n
*sizeof(double), MPI_BYTE
, MSRANK(ms
, b
), 0,
492 ms
->mpi_comm_masters
, &mpi_req
);
493 MPI_Recv(buf
, n
*sizeof(double), MPI_BYTE
, MSRANK(ms
, b
), 0,
494 ms
->mpi_comm_masters
, MPI_STATUS_IGNORE
);
495 MPI_Wait(&mpi_req
, MPI_STATUS_IGNORE
);
498 for (i
= 0; i
< n
; i
++)
506 static void exchange_rvecs(const gmx_multisim_t gmx_unused
*ms
, int gmx_unused b
, rvec
*v
, int n
)
516 MPI_Sendrecv(v[0], n*sizeof(rvec),MPI_BYTE,MSRANK(ms,b),0,
517 buf[0],n*sizeof(rvec),MPI_BYTE,MSRANK(ms,b),0,
518 ms->mpi_comm_masters,MPI_STATUS_IGNORE);
523 MPI_Isend(v
[0], n
*sizeof(rvec
), MPI_BYTE
, MSRANK(ms
, b
), 0,
524 ms
->mpi_comm_masters
, &mpi_req
);
525 MPI_Recv(buf
[0], n
*sizeof(rvec
), MPI_BYTE
, MSRANK(ms
, b
), 0,
526 ms
->mpi_comm_masters
, MPI_STATUS_IGNORE
);
527 MPI_Wait(&mpi_req
, MPI_STATUS_IGNORE
);
530 for (i
= 0; i
< n
; i
++)
532 copy_rvec(buf
[i
], v
[i
]);
538 static void exchange_state(const gmx_multisim_t
*ms
, int b
, t_state
*state
)
540 /* When t_state changes, this code should be updated. */
542 ngtc
= state
->ngtc
* state
->nhchainlength
;
543 nnhpres
= state
->nnhpres
* state
->nhchainlength
;
544 exchange_rvecs(ms
, b
, state
->box
, DIM
);
545 exchange_rvecs(ms
, b
, state
->box_rel
, DIM
);
546 exchange_rvecs(ms
, b
, state
->boxv
, DIM
);
547 exchange_reals(ms
, b
, &(state
->veta
), 1);
548 exchange_reals(ms
, b
, &(state
->vol0
), 1);
549 exchange_rvecs(ms
, b
, state
->svir_prev
, DIM
);
550 exchange_rvecs(ms
, b
, state
->fvir_prev
, DIM
);
551 exchange_rvecs(ms
, b
, state
->pres_prev
, DIM
);
552 exchange_doubles(ms
, b
, state
->nosehoover_xi
.data(), ngtc
);
553 exchange_doubles(ms
, b
, state
->nosehoover_vxi
.data(), ngtc
);
554 exchange_doubles(ms
, b
, state
->nhpres_xi
.data(), nnhpres
);
555 exchange_doubles(ms
, b
, state
->nhpres_vxi
.data(), nnhpres
);
556 exchange_doubles(ms
, b
, state
->therm_integral
.data(), state
->ngtc
);
557 exchange_doubles(ms
, b
, &state
->baros_integral
, 1);
558 exchange_rvecs(ms
, b
, as_rvec_array(state
->x
.data()), state
->natoms
);
559 exchange_rvecs(ms
, b
, as_rvec_array(state
->v
.data()), state
->natoms
);
562 static void copy_state_serial(const t_state
*src
, t_state
*dest
)
566 /* Currently the local state is always a pointer to the global
567 * in serial, so we should never end up here.
568 * TODO: Implement a (trivial) t_state copy once converted to C++.
570 GMX_RELEASE_ASSERT(false, "State copying is currently not implemented in replica exchange");
574 static void scale_velocities(t_state
*state
, real fac
)
578 if (as_rvec_array(state
->v
.data()))
580 for (i
= 0; i
< state
->natoms
; i
++)
582 svmul(fac
, state
->v
[i
], state
->v
[i
]);
587 static void print_transition_matrix(FILE *fplog
, int n
, int **nmoves
, int *nattempt
)
592 ntot
= nattempt
[0] + nattempt
[1];
593 fprintf(fplog
, "\n");
594 fprintf(fplog
, "Repl");
595 for (i
= 0; i
< n
; i
++)
597 fprintf(fplog
, " "); /* put the title closer to the center */
599 fprintf(fplog
, "Empirical Transition Matrix\n");
601 fprintf(fplog
, "Repl");
602 for (i
= 0; i
< n
; i
++)
604 fprintf(fplog
, "%8d", (i
+1));
606 fprintf(fplog
, "\n");
608 for (i
= 0; i
< n
; i
++)
610 fprintf(fplog
, "Repl");
611 for (j
= 0; j
< n
; j
++)
614 if (nmoves
[i
][j
] > 0)
616 Tprint
= nmoves
[i
][j
]/(2.0*ntot
);
618 fprintf(fplog
, "%8.4f", Tprint
);
620 fprintf(fplog
, "%3d\n", i
);
624 static void print_ind(FILE *fplog
, const char *leg
, int n
, int *ind
, gmx_bool
*bEx
)
628 fprintf(fplog
, "Repl %2s %2d", leg
, ind
[0]);
629 for (i
= 1; i
< n
; i
++)
631 fprintf(fplog
, " %c %2d", (bEx
!= nullptr && bEx
[i
]) ? 'x' : ' ', ind
[i
]);
633 fprintf(fplog
, "\n");
636 static void print_allswitchind(FILE *fplog
, int n
, int *pind
, int *allswaps
, int *tmpswap
)
640 for (i
= 0; i
< n
; i
++)
642 tmpswap
[i
] = allswaps
[i
];
644 for (i
= 0; i
< n
; i
++)
646 allswaps
[i
] = tmpswap
[pind
[i
]];
649 fprintf(fplog
, "\nAccepted Exchanges: ");
650 for (i
= 0; i
< n
; i
++)
652 fprintf(fplog
, "%d ", pind
[i
]);
654 fprintf(fplog
, "\n");
656 /* the "Order After Exchange" is the state label corresponding to the configuration that
657 started in state listed in order, i.e.
662 configuration starting in simulation 3 is now in simulation 0,
663 configuration starting in simulation 0 is now in simulation 1,
664 configuration starting in simulation 1 is now in simulation 2,
665 configuration starting in simulation 2 is now in simulation 3
667 fprintf(fplog
, "Order After Exchange: ");
668 for (i
= 0; i
< n
; i
++)
670 fprintf(fplog
, "%d ", allswaps
[i
]);
672 fprintf(fplog
, "\n\n");
675 static void print_prob(FILE *fplog
, const char *leg
, int n
, real
*prob
)
680 fprintf(fplog
, "Repl %2s ", leg
);
681 for (i
= 1; i
< n
; i
++)
685 sprintf(buf
, "%4.2f", prob
[i
]);
686 fprintf(fplog
, " %3s", buf
[0] == '1' ? "1.0" : buf
+1);
693 fprintf(fplog
, "\n");
696 static void print_count(FILE *fplog
, const char *leg
, int n
, int *count
)
700 fprintf(fplog
, "Repl %2s ", leg
);
701 for (i
= 1; i
< n
; i
++)
703 fprintf(fplog
, " %4d", count
[i
]);
705 fprintf(fplog
, "\n");
708 static real
calc_delta(FILE *fplog
, gmx_bool bPrint
, struct gmx_repl_ex
*re
, int a
, int b
, int ap
, int bp
)
711 real ediff
, dpV
, delta
= 0;
712 real
*Epot
= re
->Epot
;
715 real
*beta
= re
->beta
;
717 /* Two cases; we are permuted and not. In all cases, setting ap = a and bp = b will reduce
718 to the non permuted case */
724 * Okabe et. al. Chem. Phys. Lett. 335 (2001) 435-439
726 ediff
= Epot
[b
] - Epot
[a
];
727 delta
= -(beta
[bp
] - beta
[ap
])*ediff
;
730 /* two cases: when we are permuted, and not. */
732 ediff = E_new - E_old
733 = [H_b(x_a) + H_a(x_b)] - [H_b(x_b) + H_a(x_a)]
734 = [H_b(x_a) - H_a(x_a)] + [H_a(x_b) - H_b(x_b)]
735 = de[b][a] + de[a][b] */
738 ediff = E_new - E_old
739 = [H_bp(x_a) + H_ap(x_b)] - [H_bp(x_b) + H_ap(x_a)]
740 = [H_bp(x_a) - H_ap(x_a)] + [H_ap(x_b) - H_bp(x_b)]
741 = [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)]
742 = [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)]
743 = (de[bp][a] - de[ap][a]) + (de[ap][b] - de[bp][b]) */
744 /* but, in the current code implementation, we flip configurations, not indices . . .
745 So let's examine that.
746 = [H_b(x_ap) - H_a(x_a)] - [H_a(x_ap) - H_a(x_a)] + [H_a(x_bp) - H_b(x_b)] - H_b(x_bp) - H_b(x_b)]
747 = [H_b(x_ap) - H_a(x_ap)] + [H_a(x_bp) - H_b(x_pb)]
748 = (de[b][ap] - de[a][ap]) + (de[a][bp] - de[b][bp]
749 So, if we exchange b<=> bp and a<=> ap, we return to the same result.
750 So the simple solution is to flip the
751 position of perturbed and original indices in the tests.
754 ediff
= (de
[bp
][a
] - de
[ap
][a
]) + (de
[ap
][b
] - de
[bp
][b
]);
755 delta
= ediff
*beta
[a
]; /* assume all same temperature in this case */
759 /* delta = reduced E_new - reduced E_old
760 = [beta_b H_b(x_a) + beta_a H_a(x_b)] - [beta_b H_b(x_b) + beta_a H_a(x_a)]
761 = [beta_b H_b(x_a) - beta_a H_a(x_a)] + [beta_a H_a(x_b) - beta_b H_b(x_b)]
762 = [beta_b dH_b(x_a) + beta_b H_a(x_a) - beta_a H_a(x_a)] +
763 [beta_a dH_a(x_b) + beta_a H_b(x_b) - beta_b H_b(x_b)]
764 = [beta_b dH_b(x_a) + [beta_a dH_a(x_b) +
765 beta_b (H_a(x_a) - H_b(x_b)]) - beta_a (H_a(x_a) - H_b(x_b))
766 = beta_b dH_b(x_a) + beta_a dH_a(x_b) - (beta_b - beta_a)(H_b(x_b) - H_a(x_a) */
767 /* delta = beta[b]*de[b][a] + beta[a]*de[a][b] - (beta[b] - beta[a])*(Epot[b] - Epot[a]; */
768 /* permuted (big breath!) */
769 /* delta = reduced E_new - reduced E_old
770 = [beta_bp H_bp(x_a) + beta_ap H_ap(x_b)] - [beta_bp H_bp(x_b) + beta_ap H_ap(x_a)]
771 = [beta_bp H_bp(x_a) - beta_ap H_ap(x_a)] + [beta_ap H_ap(x_b) - beta_bp H_bp(x_b)]
772 = [beta_bp H_bp(x_a) - beta_ap H_ap(x_a)] + [beta_ap H_ap(x_b) - beta_bp H_bp(x_b)]
773 - beta_pb H_a(x_a) + beta_ap H_a(x_a) + beta_pb H_a(x_a) - beta_ap H_a(x_a)
774 - beta_ap H_b(x_b) + beta_bp H_b(x_b) + beta_ap H_b(x_b) - beta_bp H_b(x_b)
775 = [(beta_bp H_bp(x_a) - beta_bp H_a(x_a)) - (beta_ap H_ap(x_a) - beta_ap H_a(x_a))] +
776 [(beta_ap H_ap(x_b) - beta_ap H_b(x_b)) - (beta_bp H_bp(x_b) - beta_bp H_b(x_b))]
777 + beta_pb H_a(x_a) - beta_ap H_a(x_a) + beta_ap H_b(x_b) - beta_bp H_b(x_b)
778 = [beta_bp (H_bp(x_a) - H_a(x_a)) - beta_ap (H_ap(x_a) - H_a(x_a))] +
779 [beta_ap (H_ap(x_b) - H_b(x_b)) - beta_bp (H_bp(x_b) - H_b(x_b))]
780 + beta_pb (H_a(x_a) - H_b(x_b)) - beta_ap (H_a(x_a) - H_b(x_b))
781 = ([beta_bp de[bp][a] - beta_ap de[ap][a]) + beta_ap de[ap][b] - beta_bp de[bp][b])
782 + (beta_pb-beta_ap)(H_a(x_a) - H_b(x_b)) */
783 delta
= beta
[bp
]*(de
[bp
][a
] - de
[bp
][b
]) + beta
[ap
]*(de
[ap
][b
] - de
[ap
][a
]) - (beta
[bp
]-beta
[ap
])*(Epot
[b
]-Epot
[a
]);
786 gmx_incons("Unknown replica exchange quantity");
790 fprintf(fplog
, "Repl %d <-> %d dE_term = %10.3e (kT)\n", a
, b
, delta
);
794 /* revist the calculation for 5.0. Might be some improvements. */
795 dpV
= (beta
[ap
]*re
->pres
[ap
]-beta
[bp
]*re
->pres
[bp
])*(Vol
[b
]-Vol
[a
])/PRESFAC
;
798 fprintf(fplog
, " dpV = %10.3e d = %10.3e\n", dpV
, delta
+ dpV
);
806 test_for_replica_exchange(FILE *fplog
,
807 const gmx_multisim_t
*ms
,
808 struct gmx_repl_ex
*re
,
809 const gmx_enerdata_t
*enerd
,
814 int m
, i
, j
, a
, b
, ap
, bp
, i0
, i1
, tmp
;
816 gmx_bool bPrint
, bMultiEx
;
817 gmx_bool
*bEx
= re
->bEx
;
818 real
*prob
= re
->prob
;
819 int *pind
= re
->destinations
; /* permuted index */
820 gmx_bool bEpot
= FALSE
;
821 gmx_bool bDLambda
= FALSE
;
822 gmx_bool bVol
= FALSE
;
823 gmx::ThreeFry2x64
<64> rng(re
->seed
, gmx::RandomDomain::ReplicaExchange
);
824 gmx::UniformRealDistribution
<real
> uniformRealDist
;
825 gmx::UniformIntDistribution
<int> uniformNreplDist(0, re
->nrepl
-1);
827 bMultiEx
= (re
->nex
> 1); /* multiple exchanges at each state */
828 fprintf(fplog
, "Replica exchange at step %" GMX_PRId64
" time %.5f\n", step
, time
);
832 for (i
= 0; i
< re
->nrepl
; i
++)
837 re
->Vol
[re
->repl
] = vol
;
839 if ((re
->type
== ereTEMP
|| re
->type
== ereTL
))
841 for (i
= 0; i
< re
->nrepl
; i
++)
846 re
->Epot
[re
->repl
] = enerd
->term
[F_EPOT
];
847 /* temperatures of different states*/
848 for (i
= 0; i
< re
->nrepl
; i
++)
850 re
->beta
[i
] = 1.0/(re
->q
[ereTEMP
][i
]*BOLTZ
);
855 for (i
= 0; i
< re
->nrepl
; i
++)
857 re
->beta
[i
] = 1.0/(re
->temp
*BOLTZ
); /* we have a single temperature */
860 if (re
->type
== ereLAMBDA
|| re
->type
== ereTL
)
863 /* lambda differences. */
864 /* de[i][j] is the energy of the jth simulation in the ith Hamiltonian
865 minus the energy of the jth simulation in the jth Hamiltonian */
866 for (i
= 0; i
< re
->nrepl
; i
++)
868 for (j
= 0; j
< re
->nrepl
; j
++)
873 for (i
= 0; i
< re
->nrepl
; i
++)
875 re
->de
[i
][re
->repl
] = (enerd
->enerpart_lambda
[(int)re
->q
[ereLAMBDA
][i
]+1]-enerd
->enerpart_lambda
[0]);
879 /* now actually do the communication */
882 gmx_sum_sim(re
->nrepl
, re
->Vol
, ms
);
886 gmx_sum_sim(re
->nrepl
, re
->Epot
, ms
);
890 for (i
= 0; i
< re
->nrepl
; i
++)
892 gmx_sum_sim(re
->nrepl
, re
->de
[i
], ms
);
896 /* make a duplicate set of indices for shuffling */
897 for (i
= 0; i
< re
->nrepl
; i
++)
899 pind
[i
] = re
->ind
[i
];
902 rng
.restart( step
, 0 );
906 /* multiple random switch exchange */
910 for (i
= 0; i
< re
->nex
+ nself
; i
++)
912 // For now this is superfluous, but just in case we ever add more
913 // calls in different branches it is safer to always reset the distribution.
914 uniformNreplDist
.reset();
916 /* randomly select a pair */
917 /* in theory, could reduce this by identifying only which switches had a nonneglibible
918 probability of occurring (log p > -100) and only operate on those switches */
919 /* find out which state it is from, and what label that state currently has. Likely
920 more work that useful. */
921 i0
= uniformNreplDist(rng
);
922 i1
= uniformNreplDist(rng
);
926 continue; /* self-exchange, back up and do it again */
929 a
= re
->ind
[i0
]; /* what are the indices of these states? */
934 bPrint
= FALSE
; /* too noisy */
935 /* calculate the energy difference */
936 /* if the code changes to flip the STATES, rather than the configurations,
937 use the commented version of the code */
938 /* delta = calc_delta(fplog,bPrint,re,a,b,ap,bp); */
939 delta
= calc_delta(fplog
, bPrint
, re
, ap
, bp
, a
, b
);
941 /* we actually only use the first space in the prob and bEx array,
942 since there are actually many switches between pairs. */
952 if (delta
> PROBABILITYCUTOFF
)
958 prob
[0] = exp(-delta
);
960 // roll a number to determine if accepted. For now it is superfluous to
961 // reset, but just in case we ever add more calls in different branches
962 // it is safer to always reset the distribution.
963 uniformRealDist
.reset();
964 bEx
[0] = uniformRealDist(rng
) < prob
[0];
966 re
->prob_sum
[0] += prob
[0];
970 /* swap the states */
976 re
->nattempt
[0]++; /* keep track of total permutation trials here */
977 print_allswitchind(fplog
, re
->nrepl
, pind
, re
->allswaps
, re
->tmpswap
);
981 /* standard nearest neighbor replica exchange */
983 m
= (step
/ re
->nst
) % 2;
984 for (i
= 1; i
< re
->nrepl
; i
++)
989 bPrint
= (re
->repl
== a
|| re
->repl
== b
);
992 delta
= calc_delta(fplog
, bPrint
, re
, a
, b
, a
, b
);
1001 if (delta
> PROBABILITYCUTOFF
)
1007 prob
[i
] = exp(-delta
);
1009 // roll a number to determine if accepted. For now it is superfluous to
1010 // reset, but just in case we ever add more calls in different branches
1011 // it is safer to always reset the distribution.
1012 uniformRealDist
.reset();
1013 bEx
[i
] = uniformRealDist(rng
) < prob
[i
];
1015 re
->prob_sum
[i
] += prob
[i
];
1019 /* swap these two */
1021 pind
[i
-1] = pind
[i
];
1023 re
->nexchange
[i
]++; /* statistics for back compatibility */
1032 /* print some statistics */
1033 print_ind(fplog
, "ex", re
->nrepl
, re
->ind
, bEx
);
1034 print_prob(fplog
, "pr", re
->nrepl
, prob
);
1035 fprintf(fplog
, "\n");
1039 /* record which moves were made and accepted */
1040 for (i
= 0; i
< re
->nrepl
; i
++)
1042 re
->nmoves
[re
->ind
[i
]][pind
[i
]] += 1;
1043 re
->nmoves
[pind
[i
]][re
->ind
[i
]] += 1;
1045 fflush(fplog
); /* make sure we can see what the last exchange was */
1049 cyclic_decomposition(const int *destinations
,
1058 for (i
= 0; i
< nrepl
; i
++)
1062 for (i
= 0; i
< nrepl
; i
++) /* one cycle for each replica */
1073 for (j
= 0; j
< nrepl
; j
++) /* potentially all cycles are part, but we will break first */
1075 p
= destinations
[p
]; /* start permuting */
1083 break; /* we've reached the original element, the cycle is complete, and we marked the end. */
1087 cyclic
[i
][c
] = p
; /* each permutation gives a new member of the cycle */
1093 *nswap
= maxlen
- 1;
1097 for (i
= 0; i
< nrepl
; i
++)
1099 fprintf(debug
, "Cycle %d:", i
);
1100 for (j
= 0; j
< nrepl
; j
++)
1102 if (cyclic
[i
][j
] < 0)
1106 fprintf(debug
, "%2d", cyclic
[i
][j
]);
1108 fprintf(debug
, "\n");
1115 compute_exchange_order(int **cyclic
,
1122 for (j
= 0; j
< maxswap
; j
++)
1124 for (i
= 0; i
< nrepl
; i
++)
1126 if (cyclic
[i
][j
+1] >= 0)
1128 order
[cyclic
[i
][j
+1]][j
] = cyclic
[i
][j
];
1129 order
[cyclic
[i
][j
]][j
] = cyclic
[i
][j
+1];
1132 for (i
= 0; i
< nrepl
; i
++)
1134 if (order
[i
][j
] < 0)
1136 order
[i
][j
] = i
; /* if it's not exchanging, it should stay this round*/
1143 fprintf(debug
, "Replica Exchange Order\n");
1144 for (i
= 0; i
< nrepl
; i
++)
1146 fprintf(debug
, "Replica %d:", i
);
1147 for (j
= 0; j
< maxswap
; j
++)
1149 if (order
[i
][j
] < 0)
1153 fprintf(debug
, "%2d", order
[i
][j
]);
1155 fprintf(debug
, "\n");
1162 prepare_to_do_exchange(struct gmx_repl_ex
*re
,
1163 const int replica_id
,
1165 gmx_bool
*bThisReplicaExchanged
)
1168 /* Hold the cyclic decomposition of the (multiple) replica
1170 gmx_bool bAnyReplicaExchanged
= FALSE
;
1171 *bThisReplicaExchanged
= FALSE
;
1173 for (i
= 0; i
< re
->nrepl
; i
++)
1175 if (re
->destinations
[i
] != re
->ind
[i
])
1177 /* only mark as exchanged if the index has been shuffled */
1178 bAnyReplicaExchanged
= TRUE
;
1182 if (bAnyReplicaExchanged
)
1184 /* reinitialize the placeholder arrays */
1185 for (i
= 0; i
< re
->nrepl
; i
++)
1187 for (j
= 0; j
< re
->nrepl
; j
++)
1189 re
->cyclic
[i
][j
] = -1;
1190 re
->order
[i
][j
] = -1;
1194 /* Identify the cyclic decomposition of the permutation (very
1195 * fast if neighbor replica exchange). */
1196 cyclic_decomposition(re
->destinations
, re
->cyclic
, re
->incycle
, re
->nrepl
, maxswap
);
1198 /* Now translate the decomposition into a replica exchange
1199 * order at each step. */
1200 compute_exchange_order(re
->cyclic
, re
->order
, re
->nrepl
, *maxswap
);
1202 /* Did this replica do any exchange at any point? */
1203 for (j
= 0; j
< *maxswap
; j
++)
1205 if (replica_id
!= re
->order
[replica_id
][j
])
1207 *bThisReplicaExchanged
= TRUE
;
1214 gmx_bool
replica_exchange(FILE *fplog
, const t_commrec
*cr
, struct gmx_repl_ex
*re
,
1215 t_state
*state
, const gmx_enerdata_t
*enerd
,
1216 t_state
*state_local
, gmx_int64_t step
, real time
)
1220 int exchange_partner
;
1222 /* Number of rounds of exchanges needed to deal with any multiple
1224 /* Where each replica ends up after the exchange attempt(s). */
1225 /* The order in which multiple exchanges will occur. */
1226 gmx_bool bThisReplicaExchanged
= FALSE
;
1230 replica_id
= re
->repl
;
1231 test_for_replica_exchange(fplog
, cr
->ms
, re
, enerd
, det(state_local
->box
), step
, time
);
1232 prepare_to_do_exchange(re
, replica_id
, &maxswap
, &bThisReplicaExchanged
);
1234 /* Do intra-simulation broadcast so all processors belonging to
1235 * each simulation know whether they need to participate in
1236 * collecting the state. Otherwise, they might as well get on with
1237 * the next thing to do. */
1238 if (DOMAINDECOMP(cr
))
1241 MPI_Bcast(&bThisReplicaExchanged
, sizeof(gmx_bool
), MPI_BYTE
, MASTERRANK(cr
),
1242 cr
->mpi_comm_mygroup
);
1246 if (bThisReplicaExchanged
)
1248 /* Exchange the states */
1249 /* Collect the global state on the master node */
1250 if (DOMAINDECOMP(cr
))
1252 dd_collect_state(cr
->dd
, state_local
, state
);
1256 copy_state_serial(state_local
, state
);
1261 /* There will be only one swap cycle with standard replica
1262 * exchange, but there may be multiple swap cycles if we
1263 * allow multiple swaps. */
1265 for (j
= 0; j
< maxswap
; j
++)
1267 exchange_partner
= re
->order
[replica_id
][j
];
1269 if (exchange_partner
!= replica_id
)
1271 /* Exchange the global states between the master nodes */
1274 fprintf(debug
, "Exchanging %d with %d\n", replica_id
, exchange_partner
);
1276 exchange_state(cr
->ms
, exchange_partner
, state
);
1279 /* For temperature-type replica exchange, we need to scale
1280 * the velocities. */
1281 if (re
->type
== ereTEMP
|| re
->type
== ereTL
)
1283 scale_velocities(state
, sqrt(re
->q
[ereTEMP
][replica_id
]/re
->q
[ereTEMP
][re
->destinations
[replica_id
]]));
1288 /* With domain decomposition the global state is distributed later */
1289 if (!DOMAINDECOMP(cr
))
1291 /* Copy the global state to the local state data structure */
1292 copy_state_serial(state
, state_local
);
1296 return bThisReplicaExchanged
;
1299 void print_replica_exchange_statistics(FILE *fplog
, struct gmx_repl_ex
*re
)
1303 fprintf(fplog
, "\nReplica exchange statistics\n");
1307 fprintf(fplog
, "Repl %d attempts, %d odd, %d even\n",
1308 re
->nattempt
[0]+re
->nattempt
[1], re
->nattempt
[1], re
->nattempt
[0]);
1310 fprintf(fplog
, "Repl average probabilities:\n");
1311 for (i
= 1; i
< re
->nrepl
; i
++)
1313 if (re
->nattempt
[i
%2] == 0)
1319 re
->prob
[i
] = re
->prob_sum
[i
]/re
->nattempt
[i
%2];
1322 print_ind(fplog
, "", re
->nrepl
, re
->ind
, nullptr);
1323 print_prob(fplog
, "", re
->nrepl
, re
->prob
);
1325 fprintf(fplog
, "Repl number of exchanges:\n");
1326 print_ind(fplog
, "", re
->nrepl
, re
->ind
, nullptr);
1327 print_count(fplog
, "", re
->nrepl
, re
->nexchange
);
1329 fprintf(fplog
, "Repl average number of exchanges:\n");
1330 for (i
= 1; i
< re
->nrepl
; i
++)
1332 if (re
->nattempt
[i
%2] == 0)
1338 re
->prob
[i
] = ((real
)re
->nexchange
[i
])/re
->nattempt
[i
%2];
1341 print_ind(fplog
, "", re
->nrepl
, re
->ind
, nullptr);
1342 print_prob(fplog
, "", re
->nrepl
, re
->prob
);
1344 fprintf(fplog
, "\n");
1346 /* print the transition matrix */
1347 print_transition_matrix(fplog
, re
->nrepl
, re
->nmoves
, re
->nattempt
);