Replica-exchange metadynamics scripts

From: Robert Elder (
Date: Tue Nov 13 2012 - 12:12:39 CST

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

This archive was generated by hypermail 2.1.6 : Tue Dec 31 2013 - 23:22:45 CST