What is free energy
The free energy refers to the potential energy of a system that is available to perform work. In molecular simulations, it is common to calculate free energy differences between different states or conformations of a molecular system. This can be useful in understanding the thermodynamics of a system, predicting reaction pathways, and determining the stability of different molecular configurations.
Method 1: Free sampling¶
The most direct way to calculate a free energy profile is to extract the partition function from a classical (i.e. unbiased) molecular dynamics simulation, and then estimate the Gibbs free energy by using
where
Struggling with your molecular simulations project?
Get guidance for your LAMMPS simulations and receive personalized advice for your project.
Basic LAMMPS parameters¶
To begin this tutorial, if you are using LAMMPS–GUI, select Start Tutorial 7
from the Tutorials
menu and follow the instructions. Alternatively, if you are
not using LAMMPS–GUI, create a new folder and add a file named
free-sampling.lmp. Open the file in a text editor and paste in the following
content:
variable sigma equal 3.405
variable epsilon equal 0.238
variable U0 equal 1.5*${epsilon}
variable dlt equal 1.0
variable x0 equal 10.0
units real
atom_style atomic
pair_style lj/cut 3.822
pair_modify shift yes
boundary p p p
Here, we begin by defining variables for the Lennard-Jones interaction
pair_modify
command.
System creation and settings¶
Let us define the simulation box and randomly add atoms by addying the following lines to free-sampling.lmp:
region myreg block -50 50 -15 15 -50 50
create_box 1 myreg
create_atoms 1 random 200 34134 myreg overlap 3 maxtry 50
mass * 39.95
pair_coeff * * ${epsilon} ${sigma}
The variables
Taking the derivative of the potential with respect to
The figure below shows the potential


Figure: Potential
We impose the force fix addforce
command. Add the following
lines to free-sampling.lmp:
variable U atom ${U0}*atan((x+${x0})/${dlt})-${U0}*atan((x-${x0})/${dlt})
variable F atom ${U0}/((x-${x0})^2/${dlt}^2+1)/${dlt}-${U0}/((x+${x0})^2/${dlt}^2+1)/${dlt}
fix myadf all addforce v_F 0.0 0.0 energy v_U
Next, we combine the fix nve
with a fix langevin
thermostat:
fix mynve all nve
fix mylgv all langevin 119.8 119.8 500 30917
When combining these two commands, the MD simulation operates
in the NVT ensemble, maintaining a constant number of
atoms
To ensure that the equilibration time is sufficient, we will track the evolution of
the number of atoms in the central - energetically unfavorable - region,
referred to as mymes
, using the n_center
variable:
region mymes block -${x0} ${x0} INF INF INF INF
variable n_center equal count(all,mymes)
thermo_style custom step temp etotal v_n_center
thermo 10000
For visualization, use one of the following options: the dump image
command to
create .ppm images of the system, or the dump atom
command to write a
VMD-compatible trajectory to a file:
# Option 1
dump viz1 all image 50000 myimage-*.ppm type type shiny 0.1 box yes 0.01 view 180 90 zoom 6 size 1600 500 fsaa yes
dump_modify viz1 backcolor white acolor 1 cyan adiam 1 3 boxcolor black
# Option 2
dump viz2 all atom 50000 free-sampling.lammpstrj
Finally, let us perform an equilibration of 50000 steps,
using a timestep of
timestep 2.0
run 50000
Run the simulation with LAMMPS. The number of atoms in the
central region,


Figure: Evolution of the number of atoms mymes
as a function of time
Run and data acquisition¶
Once the system is equilibrated, we will record the density profile of
the atoms along the ave/chunk
command.
Add the following line to free-sampling.lmp:
# undump viz2 # Uncomment this line if you're using Option 2
reset_timestep 0
thermo 200000
compute cc1 all chunk/atom bin/1d x 0.0 2.0
fix myac all ave/chunk 100 20000 2000000 cc1 density/number file free-sampling.dat
run 2000000
The step count is reset to 0 using reset_timestep
to synchronize it
with the output times of fix density/number
. Run the simulation using
LAMMPS.


Figure: Snapshot of the system simulated during the free sampling step of the tutorial.
The atoms density is the lowest in the central part of the box, mymes
. Here,
Data analysis¶
Once the simulation is complete, the density profile from free-sampling.dat
shows that the density in the center of the box is
about two orders of magnitude lower than inside the reservoir.
Next, we plot


Figure: a) Fluid density,
The limits of free sampling¶
Increasing the value of
Method 2: Umbrella sampling¶
Umbrella sampling is a biased molecular dynamics method in which
additional forces are added to a chosen atom to force it to explore the
more unfavorable areas of the system
[6, 7, 45].
Here, to encourage one
of the atoms to explore the central region of the box, we apply a
potential
LAMMPS input script¶
If you are using LAMMPS–GUI, open the file named umbrella-sampling.lmp. Alternatively, if you are not using LAMMPS–GUI, create a new input file and paste in the following content:
variable sigma equal 3.405
variable epsilon equal 0.238
variable U0 equal 10*${epsilon}
variable dlt equal 1.0
variable x0 equal 10
variable k equal 0.5
units real
atom_style atomic
pair_style lj/cut 3.822
pair_modify shift yes
boundary p p p
The first difference from the previous case is the larger value
for the repulsive potential
Let us create a simulation box with two atom types, including a single particle of type 2, by adding the following lines to umbrella-sampling.lmp:
region myreg block -50 50 -15 15 -50 50
create_box 2 myreg
create_atoms 2 single 0 0 0
create_atoms 1 random 199 34134 myreg overlap 3 maxtry 50
Next, we assign the same mass and LJ parameters to both atom types
1 and 2, and place the atoms of type 2 into a group named topull
:
mass * 39.948
pair_coeff * * ${epsilon} ${sigma}
group topull type 2
Then, the same potential fix nve
and fix langevin
commands:
variable U atom ${U0}*atan((x+${x0})/${dlt})-${U0}*atan((x-${x0})/${dlt})
variable F atom ${U0}/((x-${x0})^2/${dlt}^2+1)/${dlt}-${U0}/((x+${x0})^2/${dlt}^2+1)/${dlt}
fix myadf all addforce v_F 0.0 0.0 energy v_U
fix mynve all nve
fix mylgv all langevin 119.8 119.8 500 30917
Next, we perform a brief equilibration to prepare for the umbrella sampling run:
thermo 5000
# Option 1
dump viz1 all image 50000 myimage-*.ppm type type shiny 0.1 box yes 0.01 view 180 90 zoom 6 size 1600 500 fsaa yes
dump_modify viz1 backcolor white acolor 1 cyan acolor 2 red adiam 1 3 adiam 2 3 boxcolor black
# Option 2
dump viz2 all atom 50000 umbrella-sampling.lammpstrj
timestep 2.0
run 50000
So far, our code resembles that of Method 1, except for the additional particle
of type 2. Particles of types 1 and 2 are identical, with the same mass
and LJ parameters. However, the particle of type 2 will also
be exposed to the biasing potential
Now, we create a loop with 15 steps and progressively move the center of the bias potential by increments of 0.4 nm. Add the following lines to umbrella-sampling.lmp:
variable a loop 15
label loop
variable xdes equal 4*${a}-32
variable xave equal xcm(topull,x)
fix mytth topull spring tether ${k} ${xdes} 0 0 0
run 20000
fix myat1 all ave/time 10 10 100 v_xave v_xdes file umbrella-sampling.${a}.dat
run 200000
unfix myat1
next a
jump SELF loop
The spring
command imposes the additional harmonic potential
Note
The value of
WHAM algorithm¶
To generate the free energy profile from the particle positions saved in
the umbrella-sampling.i.dat files, we use the
WHAM [52, 53] algorithm as implemented
by Alan Grossfield [54]. You can download it
from Alan Grossfield’s website. Make sure you download the WHAM code version
2.1.0 or later which introduces the units
command-line option
used below. The executable called wham
generated by following
the instructions from the website must be placed next to
umbrella-sampling.lmp. To apply the WHAM algorithm to our
simulation, we need a metadata file containing:
the paths to all the data files,
the values of
,the values of
.
Download the umbrella-sampling.meta file and save it next to umbrella-sampling.lmp. Then, run the WHAM algorithm by typing the following command in the terminal:
./wham units real -30 30 50 1e-8 119.8 0 umbrella-sampling.meta umbrella-sampling.dat
where -30 and 30 are the boundaries, 50 is the number of bins, 1e-8 is the tolerance,
and 119.8 is the temperature in Kelvin. A file called umbrella-sampling.dat is created,
containing the free energy profile in kcal/mol. The resulting PMF can be compared
with the imposed potential


Figure: The potential,
Remarkably, this excellent agreement is achieved despite the very short calculation time and the high value for the energy barrier. Achieving similar results through free sampling would require performing extremely long and computationally expensive simulations.
Side note: On the choice of ¶
One difficult part of umbrella sampling is choosing the value of
If
is too small, the biasing potential is too weak to force the particle to explore the region of interest, making it impossible to reconstruct the PMF (see panel a in the figure below).If
is “appropriate”, the particle explores the entire axis, and the probability distributions are strongly impacted by the potential one wants to probe, as shown in panel b.If
is too large, the biasing potential dominates over the potential one wants to probe, which reduces the sensitivity of the method (panel c).


Figure: Probability density for each run with