Re: Replica-exchange metadynamics scripts

From: Robert Elder (
Date: Thu Feb 14 2013 - 18:29:53 CST

Hi Aron,

Sorry I missed your email by several months. I just found it
serendipitously with Google!

To clarify, my script does maintain the swapping of coordinates (or rather,
the swapping of temperatures, which is equivalent but simpler). Without
that, you wouldn't have much replica exchange going on!

And you're exactly correct: the different temperatures do have different
free energy landscapes, with the higher temperatures having flatter
distributions. That's why I also had to swap the state files, which must
maintain the running history of the metadynamics bias for a given
temperature. When you're running replica exchange with NAMD, the master
script controlling the replicas decides make an exchange of two replicas,
and then issues a new langevinTemp directive and rescales the velocities in
the two replicas that exchange (which are two unique instances of NAMD
outputting to two unique directories). In order to maintain the running
history of the metadynamics bias for those two unique temperatures, you
must also exchange the state files between the two directories that just
swapped temperatures. That way, the correct state file follows the correct
temperature around through the whole series of exchanges.

I hadn't thought much about it, but you're also correct on your statement
about bias exchange: if you eliminate the temperature swapping part(s) of
the original replica exchange scripts and add what I sent below, you could
be swapping between replicas that have different biases. A cautionary note
in this case: don't switch the .colvars.traj files when exchanging
replicas, because those files can be taken care of by the sortreplicas.c
program that is distributed with NAMD. And be careful, because it's easy to
do something that "works" (i.e. doesn't crash), but might be mixing
temperatures or be incorrect in many other ways... trust me :)

Thanks for the response,

On Sat, Nov 17, 2012 at 9:52 AM, Aron Broom <> wrote:

> Hi Robert,
> I'm slightly confused. In this script, the replicas are swapping state
> files, rather than coordinates? Is that reasonable?
> I had thought the idea with replica-exchange was to swap coordinates, but
> preserve velocity (temperature) and all other properties.
> I understand that if you use a script like this for bias-exchange that
> will be perfect, because you expect both replicas would have the same
> underlying PMF. But for replica-exchange where the temperatures differ,
> the two replicas will not have the same underlying PMF (i.e. the higher
> temperature one will be flatter). So if you exchange the biases, does this
> still make sense?
> I don't have a good theoretical understanding of this topic, so I could
> easily just be confused, but something seems a bit off to me.
> ~Aron
> On Tue, Nov 13, 2012 at 1:12 PM, Robert Elder <> wrote:
>> Hi NAMD users,
>> I would like to provide a script I recently developed to run temperature
>> replica-exchange (a.k.a. parallel tempering) metadynamics (see Bussi, G.,
>> Gervasio, F. L., Laio, A. & Parrinello, M. *JACS*. 2006.). I want to
>> share this with the community and, more importantly, to verify that this
>> method is correct with people more familiar with the code. I'm in the
>> process of verifying that this produces expected results for a simple
>> system, so for now ****be aware that this script may not work as
>> expected!**** In particular, additional work may be required to restart a
>> simulation, but I haven't reached that point yet.
>> To implement this method, I made some rather small changes to the
>> existing replica.namd script (compliments to the author(s) of that script
>> for making it easy to adapt). Without these changes, it is already possible
>> to run metadynamics concurrently with replica-exchange; however, the
>> history-dependent metadynamics bias will be a mash-up of sampling at the
>> various temperatures. My solution was to simply swap the Colvars .state
>> files between output directories each time a replica swap occurs. I think
>> this ensures that the accumulated bias in the state file only belongs to a
>> single temperature. I will outline the code changes I made in this email;
>> the marked changes can be copy-pasted into the replica.namd script. Please
>> keep in mind I'm new to MPI so I would appreciate any advice on this.
>> First, to prevent file contention between the two MPI ranks that are
>> being exchanged, I added a new variable $domove to identify which rank was
>> responsible for initiating the swap; only this process is responsible for
>> swapping the state files, later in the script:
>> # Employ the Metropolis criterion
>>> set doswap [expr $delta < 0. || exp(-1. * $delta) > rand()]
>>> # Communicate the success/failure of this swap to the other MPI rank
>>> replicaSend $doswap $replica(loc.$swap)
>>> #### Change to script
>>> # DO NOT communicate the indicator for moving the state files
>>> set domove $doswap
>>> #### End change to script
>> Then, after all the other swap communication is completed but before
>> writing restart files, I added this section of code to actually swap the
>> Colvars .state files. Note that $replica_id is the MPI rank of the current
>> process, which was responsible for initiating the exchange, and $newloc is
>> the MPI rank that is being exchanged with. Here, $domove is used so that
>> only the MPI rank $replica_id will move the state files.
>> #### Change to script
>>> # Barrier the replicas to prevent possible file contention
>>> replicaBarrier
>>> if { $doswap && $domove } {
>>> # Optionally, output to stderr which MPI ranks are swapping to
>>> verify that things are proceeding as expected
>>> #puts stderr [format ">> COPY $replica_id TO $newloc"]
>>> # Swap the current MPI rank's state file to a temporary file in the
>>> other MPI rank's output directory
>>> file rename -force [format
>>> "$output_root.job$i_job.$replica_id.colvars.state" $replica_id] [format
>>> "$output_root.job$i_job.$newloc.colvars.state.swap" $newloc]
>>> # Swap the other MPI rank's state file to a temporary file in the
>>> current MPI rank's output directory
>>> file rename -force [format
>>> "$output_root.job$i_job.$newloc.colvars.state" $newloc] [format
>>> "$output_root.job$i_job.$replica_id.colvars.state.swap" $replica_id]
>>> # Rename both of the temporary state files
>>> file rename -force [format
>>> "$output_root.job$i_job.$replica_id.colvars.state.swap" $replica_id]
>>> [format "$output_root.job$i_job.$replica_id.colvars.state" $replica_id]
>>> file rename -force [format
>>> "$output_root.job$i_job.$newloc.colvars.state.swap" $newloc] [format
>>> "$output_root.job$i_job.$newloc.colvars.state" $newloc]
>>> }
>>> # Barrier the replicas to prevent possible file contention
>>> replicaBarrier
>>> #### End change to script
>> Then you run this as you would any other replica-exchange simulation, and
>> you introduce metadynamics as you would with any other simulation within
>> the "base" file (e.g. alanin_base.namd in the alanine folding example
>> provided with NAMD 2.9). I have been using this Colvars configuration file
>> to perform metadynamics on the distance between two ions (Na+ and Cl- in
>> explicit water) as a test case:
>> colvar {
>>> name Distance
>>> width 0.25
>>> lowerBoundary 0
>>> upperBoundary 15.0
>>> upperWall 10.0
>>> upperWallConstant 25.0
>>> distance {
>>> group1 {
>>> atomNumbers { 1 }
>>> }
>>> group2 {
>>> atomNumbers { 2 }
>>> }
>>> }
>>> }
>>> metadynamics {
>>> colvars Distance
>>> hillWeight 0.1
>>> newHillFrequency 100
>>> useGrids on
>>> dumpFreeEnergyFile on
>>> }
>> Well-tempered metadynamics also "works" but I'm not sure how
>> theoretically sound the combination of replica-exchange and well-tempered
>> metadynamics at the current time, although there are a couple of recent
>> papers on the combination. Also keep in mind that I'm using pretty
>> arbitrary parameters for metadynamics in that script. I would strongly
>> caution against combining replica-exchange with multiple walkers, though.
>> If this method is correct, it could easily be used to implement
>> bias-exchange metadynamics, as suggested by Dr. Fiorin on this list a few
>> months ago.
>> So, developers and fellow users, is this approach reasonable? What have I
>> forgotten and/or botched? Can you suggest another small model system where
>> the temperature-dependence is known, in order to verify this method? I'd be
>> happy to provide additional details if they would be helpful.
>> Thanks very much for reading,
>> Robert Elder
>> PhD Student, Chemical and Biological Engineering
>> University of Colorado-Boulder
> --
> Aron Broom M.Sc
> PhD Student
> Department of Chemistry
> University of Waterloo

This archive was generated by hypermail 2.1.6 : Wed Dec 31 2014 - 23:20:55 CST