MM Notes
MM Notes
MM Notes
2019
Dr Thomas Rodgers
Contents
Nomenclature v
2 Molecular Interactions 11
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2 Molecular Force Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 Intra-Molecular Interactions . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3.1 Stretching energy . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3.2 Bending Energy . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.3 Torsional Energy . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.4 Cross Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4 Inter-Molecular Interactions . . . . . . . . . . . . . . . . . . . . . . . . 18
2.4.1 Van der Waals . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.4.2 Electrostatic Energy . . . . . . . . . . . . . . . . . . . . . . . . 20
2.4.3 Potential Truncation and Long-Range Corrections . . . . . . . . 21
2.5 Choice of Force Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.5.1 Universal Force Field (UFF) . . . . . . . . . . . . . . . . . . . . 22
2.5.2 Assisted Model Building with Energy Refinement (AMBER) . . . 22
2.5.3 Chemistry at HARvard Molecular Mechanics (CHARMM) . . . . 22
i
2.5.4 Optimised Potentials for Liquid Simulations (OPLS) . . . . . . . 23
2.5.5 GROningen MOlecular Simulation (GROMOS) . . . . . . . . . . 23
2.5.6 Transferable Potentials for Phase Equilibria (TraPPE) . . . . . . . 23
2.6 Water Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.6.1 Explicit Water . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.6.2 Implicit Water . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.7 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
ii
List of Figures
iii
iv
Nomenclature
Roman
a Acceleration m s−2
A Helmholtz energy J
Cp Heat capacity at constant pressure J mol−1 K−1
Cv Heat capacity at constant volume J mol−1 K−1
E Energy J
F Force N
G Gibbs free energy J
mathcalH Classical Hamiltonian −
H Enthalpy J
k Boltzmann constant 1.38 × 10−23 m2 kg s−2 K−1
KE Kinetic energy J
m Mass kg
N Number of particles −
−1
p Momentum kg m s
Ebend Energy for bending an angle between three atoms J
Eel Electrostatic energy J
Estr Energy for stretching a bond between two atoms J
Etors Energy for torsion of an angle around a bond J
EV dW Van der Waals energy J
EXterms Coupling energy terms J
P Pressure Pa
r Position
S Entropy J K−1
T Temperature K
t Time s
U Internal energy J
V Potential energy J
v Velocity m s−1
V Volume m3
Greek
β Thermodynamic beta 1/kT
Lennard-Jones energy J
Λ Thermal de Broglie wavelength m
σ Lennard-Jones length m
σ Standard deviation −
v
vi
Course Information
These notes provide a short practical course on molecular dynamics. Although it provides
a quick recap, it is assumed that the user has a basic understanding of classical mechanics,
statistical mechanics, and thermodynamics, but knows very little on methods for atomistic
simulations. The basics of molecular dynamics are explained in terms of practical code
writing.
No attempt is made to address advanced topics in molecular dynamics, such as calculation
of free energies and non-equilibrium simulations, while sum important subjects are treated
superficially. This handbook is meant to be a starting point, but does cover all key material
for this course. Many books are devoted to computer simulations and you are advised to
make use of them to find more information1,2,3 .
These notes contain example functions written for Matlab and are also available to down-
load from blackboard. As part of the course you will be using these functions and creating
some of your own to produce a working molecular dynamics program. While Matlab is a
very uncommon language to use for scientific programming of this type due to its speed,
it has been selected for this course as it is a very clear language, and it has been used in
previous modules.
In practical situations for molecular dynamics a full code would be used which are gen-
erally freely available online, e.g. Gromacs, DL_POLY, etc. However, as these are highly
optimised they are difficult to read as code, and to understand these programs it is impor-
tant to have a good understanding of the basics of molecular dynamics.
1
M. P. Allen and D. J. Tildesley. 2017. Computer Simulation of Liquids. Oxford University Press; 2
edition.
2
D. Frenkel and B. Smit. 2001. Understanding Molecular Simulation: From Algorithms to Applica-
tions. Academic Press; 2 edition.
3
A. Leach. 2001. Molecular Modelling: Principles and Applications Paperback. Prentice Hall; 2
edition.
vii
viii
Chapter 1
Background to Molecular Dynamics
Contents
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Molecular Dynamics . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.1 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.2 Classical Mechanics . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Statistical Mechanics . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Thermodynamic Ensembles . . . . . . . . . . . . . . . . . . . . . . . 7
1.4.1 Canonical (NVT) . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4.2 Grand-canonical (µVT) . . . . . . . . . . . . . . . . . . . . . 8
1.4.3 Microcanonical (NVE) . . . . . . . . . . . . . . . . . . . . . . 8
1.4.4 Isothermal-Isobaric (NPT) . . . . . . . . . . . . . . . . . . . . 8
1.4.5 Other Ensembles . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5 Simulation Time and Length Scales . . . . . . . . . . . . . . . . . . 9
1.6 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1
2
1.1. INTRODUCTION
1.1 Introduction
Computer experiments play an important role in science. Traditionally the physical sci-
ences were characterised by linking experiments with theory. In experiments, a system
is subjected to measurements, and results, expressed in numeric form are obtained. In
theory, a model of the system is constructed, usually in the form of a set of mathematical
equations. The model is then validated by its ability to describe the system.
In the past, theoretical models could only be easily tested in a few simple circumstances.
For example, a model for intermolecular forces in a specific material could be verified
in a diatomic molecule, or in a perfect crystal. Even then, approximations were often
required to carry out the calculation. Most problems of interest fall outside of these simple
circumstances.
The advent of high speed computers allowed a new joint element to be developed between
experiments and theory; the computer experiment. In a computer experiment, a model is
still provided by theory, but calculations are carried out by the computer following a stan-
dard procedure as in an experiment. These computer simulations altered the traditional
relationship between theory and experiment in three key way. The first is, the demand
for more accurate models. For example, molecular dynamics simulations allow the eval-
uation of the melting temperature of a material; but for this accurate interaction laws are
needed. The second is, that simulations can come very close to experimental conditions,
to the extent that then can be compared with experimental results. When this happens,
simulations become a powerful tool to interpret the experiments at the molecular level.
The third is, that computer simulations allow experiments all the investigation of things
that are just impossible (or too expensive) to do in reality, but whose outcome greatly
increases our understanding of a phenomena.
One of the computer experimental tools that is commonly used is molecular dynamics.
Molecular dynamics is the imitation of the operation of a real-world process or system
over time. It is one of the most widely used tools for decision making, usually performed
on a computer with appropriate software. Molecular dynamics can be an analysis/descrip-
tive tool (can answer what if questions) or a synthesis/prescriptive tool (can develop new
systems). Simulations are generally applied to complex systems that are impossible to
solve mathematically.
Fi = mi ai (1.2.1)
Here mi is the particle mass, ai its acceleration, and Fi is the force acting upon it due
to the interactions with other atoms. After equilibrium, we can then perform an actual
measurement on the system. Therefore, in contrast with Monte Carlo methods, molecular
dynamics is a deterministic technique: given an initial set of positions and velocities, the
subsequent time evolution is in principle1 completely determined.
The computer calculates a trajectory in a 6N -dimensional phase space (3N positions and
3N momenta). However, such trajectory is usually not particularly relevant by itself.
Molecular dynamics is a statistical mechanics method, and it is a way to obtain a set of
configurations distributed according to some statistical distribution function, or statistical
ensemble. According to statistical physics, physical quantities are represented by aver-
ages over configurations distributed according to ensembles. A trajectory obtained by
molecular dynamics provides such a set of configurations. Therefore, measurements of a
physical quantity by simulations is simply obtained as an arithmetic average of the various
instantaneous values assumed by that quantity during the simulation. Statistical mechan-
ics is the link between the microscopic behaviour and the thermodynamics properties.
More information is given in sections 1.3 and 1.4.
1.2.1 History
The techniques for Molecular Dynamics we first developed during the 1940s and 50s at
the Los Alamos National Lab in California, where computers became avaliable as a fringe
benefit of weapons work. Modelled on celestial mechanics, with molecules represented
by mass points interacting with central forces, these calculations led to rapid advances
in both equilibrium and nonequilibrium systems [2]. Figure 1.1 shows some of the key
developers of Molecular Dynamics. These methods were then further developed by Alder
and Wainwright in the late 1950’s [1, 3] to study the interactions of hard spheres. Many
important insights concerning the behavior of simple liquids emerged from their studies.
The next major advance was in 1964, when Rahman carried out the first simulation using
a realistic potential for liquid argon [4]. The first molecular dynamics simulation of a
realistic system was done by Rahman and Stillinger in their simulation of liquid water in
1974 [5].
Figure 1.1: Key developers of Molecular Dynamics (a) Enrico Fermi, (b) John Pasta, (c)
Stanislaw Ulam, and (d) Berni Alder.
Although simulations of liquids are where molecular dynamics started, these are still
1
In practice, the finiteness of the integration time step and arithmetic rounding errors will eventually
cause the computed trajectory to deviate from the true trajectory
investigated now. Availability of new interaction models allows the study of new systems,
and non-equilibrium techniques allow the study of transport phenomena such as viscosity
and heat flow. Other key areas studied with molecular dynamics (though not an exhaustive
list) are: Defects in crystals, fracture of solids, physics of surfaces, adhesion and friction
between materials, surfactants and liquid crystal properties, and biomolecules.
The molecular dynamics simulation method is based on Newton’s second law for the
equation of motion, F = ma, where F is the force exerted on the particle, m is its mass
and a is its acceleration. From a knowledge of the force on each atom, it is possible to
determine the acceleration of each atom in the system. Integration of the equations of
motion then yields a trajectory that describes the positions, velocities and accelerations
of the particles as they vary with time. From this trajectory, the average values of prop-
erties can be determined. Molecular dynamics simulations can be time consuming and
computationally expensive. However, computers are getting faster and cheaper.
Newton’s equations of motion for a simple system are given by equation 1.2.2, ri is the
atomic coordinates and mi is the mass of particle i, t is time, and V is the potential
energy.
∂ 2 ri
mi 2 = −∇Vi (1.2.2)
∂t
The velocity of each particle is therefore given by equation 1.2.3, the acceleration by
equation 1.2.4, and the kinetic energy by equation 1.2.5.
d ri
vi = (1.2.3)
dt
d vi Fi
ai = = (1.2.4)
dt mi
mi
KEi = vi · vi (1.2.5)
2
In a classical many-body system, we can define the temperature using the equipartition of
energy over all degrees of freedom, such that,
1 2 1
mi vi = kB T (1.2.6)
2 2
In a practical simulation, we would measure the total kinetic energy of the system and
divide this by the number of degrees of freedom, Nf = 3N − 3 for a system with N par-
ticles1 . As the velocity of the particles fluctuates, so does the instantaneous temperature
(from equation 1.2.6),
N N
X mi |vi (t)|2 1 X |pi (t)|
T (t) = = (1.2.7)
i=1
kB Nf 3N kB i=1 2mi
The first thing we need to build our molecular dynamics simulation is a model for the real
systems, this is the focus of Chapter 2.
1
With a large number of particles this is essentially 3N .
The larger the number of samples the closer the sample mean will be to the true mean and
the smaller the sample deviation will become.
Consider an ensemble of N ∗ cells, comprising the original cell and N ∗ − 1 replications.
Energy may flow between the cells but the total ensemble energy is constant. Suppose that
the possible total energies of the N particles contained in each cell are E1∗ , E2∗ , and so on.
If a snapshot is taken there is a distribution of energies amongst the cells as follows:
N1∗ cells have energy E1∗ ,
N2∗ cells have energy E2∗ , etc.
According to Boltzmann, the E ∗ and the N ∗ are related by equation 1.3.2.
Ei∗
exp −
Ni∗ kT
=X (1.3.2)
Ei∗
∗
N
exp −
i
kT
The denominator is called the partition function and is generally given by Q as in equa-
tion 1.3.3, this is a simplification and strict ensemble partition function is dependent on
the thermodynamic ensemble as shown in Section 1.4.
Ei∗
X
Q= exp − (1.3.3)
i
kT
The canonical ensemble, describes a system which cannot exchange mass (i.e. number of
molecules N ) or volume V with the outside world, but exchanges energy such that the
temperature T of the system is uniform and constant. The canonical partition function,
which describes the distribution of energies of the microstates is given by an expression
taking into account both the kinetic and potential energies, equation 1.4.1, where Λ is the
thermal de Broglie wavelength.
Z
1 N N
N N
QNVT = d p d r exp −βH p , r
N !Λ3N
X Ei
= exp − (1.4.1)
i
kT
The relationship between the partition function and the thermodynamic potential of the
ensemble is the Helmholtz free energy, A, equation 1.4.2.
A = U − T S = −kT ln Q (1.4.2)
The micro-canonical ensemble keeps system mass and volume constant. Where it differs
from all other ensembles is that the system energy is controlled by changing the tem-
perature of the system. The NVE ensemble’s main advantage was that it does not require
random numbers. Since modern everyday computers are capable of running effective ran-
dom number generators in negligible time, this ensemble is hardly ever used for molecular
simulation. It can however be useful during the preparation of an initial configuration for
a simulation run in a different ensemble.
Isobaric-Isenthalpic (NPH)
Gibbs Ensemble
The Gibbs ensemble is used to simulate phase coexistence, usually two liquid phases or a
liquid and a vapour phase. This is accomplished by the use of two simulation boxes, each
held at a constant temperature. The boxes may exchange both mass and volume between
each other, but in such a way that there is no change in mass or volume for the overall
system.
The semi-grand canonical ensemble keeps N1 , µ2 , V , and T constant. Its main use is for
the study of solvent-solute systems, where the number of solvent molecules N1 is kept
constant, but the amount of solute N2 is allowed to fluctuate, keeping µ2 constant.
Figure 1.2: Schematic of simulation scales. The representation on the left has the most
detail however would take a long time to paint, whereas the representation on the right
still has the general features but would be a lot faster to produce.
Figure 1.3: Examples of soft matter simulation techniques and their time- and size-scales.
1.6 References
[1] B. J. Alder and T. E. Wainwright. Studies in Molecular Dynamics. I. General Method.
Journal of Chemical Physics, 31:459, 1959.
[2] E. Fermi, J. C. Pasta, and S. Ulam. Studies of Nonlinear Problems. Technical Report,
LASL Report LA-1940, 1955.
[3] B. J. Alder and T. E. Wainwright. Phase Transition for a Hard Sphere System. Journal
of Chemical Physics, 27:1208–1209, 1957.
[4] A. Rahman. Correlations in the Motion of Atoms in Liquid Argon. Physical Review,
A136:405, 1964.
[5] F. H. Stillinger and A. Rahman. Improved Simulation of Liquid Water by Molecular
Dynamics. Journal of Chemical Physics, 60:1545–1557, 1974.
Contents
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2 Molecular Force Fields . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 Intra-Molecular Interactions . . . . . . . . . . . . . . . . . . . . . . 14
2.3.1 Stretching energy . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3.2 Bending Energy . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.3 Torsional Energy . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.4 Cross Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4 Inter-Molecular Interactions . . . . . . . . . . . . . . . . . . . . . . 18
2.4.1 Van der Waals . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.4.2 Electrostatic Energy . . . . . . . . . . . . . . . . . . . . . . . 20
2.4.3 Potential Truncation and Long-Range Corrections . . . . . . . 21
2.5 Choice of Force Field . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.5.1 Universal Force Field (UFF) . . . . . . . . . . . . . . . . . . . 22
2.5.2 Assisted Model Building with Energy Refinement (AMBER) . . 22
2.5.3 Chemistry at HARvard Molecular Mechanics (CHARMM) . . . 22
2.5.4 Optimised Potentials for Liquid Simulations (OPLS) . . . . . . 23
2.5.5 GROningen MOlecular Simulation (GROMOS) . . . . . . . . . 23
2.5.6 Transferable Potentials for Phase Equilibria (TraPPE) . . . . . . 23
2.6 Water Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.6.1 Explicit Water . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.6.2 Implicit Water . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.7 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
11
12
2.1. INTRODUCTION
2.1 Introduction
When molecules are near each other, they can influence one another. Therefore, the bal-
ance between the forces of attraction and repulsion needs to be understood. Such forces
must exist in reality otherwise there would be nothing to bring molecules together to form
liquid and solid states. People have speculated about these intermolecular forces since the
concept of atoms and molecules first existed. The present theory is that molecules attract
at long range but repel strongly at short range.
A system of interacting atoms is made up of nuclei and electrons which interact with each
other. The true Hamiltonian for the system may be written as,
X P 2 X p2 1 X Zi Zj e2 1X e2 X Zi e2
i n
H= + + + − (2.1.1)
i
2M i n
2m 2 ij
|R i − R j | 2 nn 0
|rn − r n0|
in
|R i − rn |
where indexes i, j run on the nuclei, n and n0 on electrons, R and P are the positions and
momenta of the nuclei, r and p of the electrons, Z is the atomic number of the nucleus, M
its mass, m the electron mass, and e the unit charge. Coupled with the total wavefunction,
Ψ (Ri , rn ), calculated from the Schrödinger equation then everything about the system is
known.
Of course, this is impossible to carry out in practice, and approximation schemes have to
be employed. In 1923, Born and Oppenheimer noted that nuclei are much heavier than
electrons, and thus move on a time scale which is about two orders of magnitude slower
than that of the electrons. It is therefore sensible to think of the nuclei as fixed as far
as the electronic part of the problem is concerned, thus factorize the total wavefunction
as,
Ψ (Ri , rn ) = Ξ (Ri ) Φ (rn ; Ri ) (2.1.2)
where Ξ (Ri ) describes the nuclei, and Φ (rn ; Ri ) the electrons (depending parametrically
on the positions of the nuclei). With the assumptions, the problem is reformulated in terms
of two separate Schrödinger equations,
Hel Φ (rn ; Ri ) = V (Ri ) Φ (rn ; Ri )
X p2 1 X Zi Zj e2 1X e2 X Zi e2
n
where Hel = + + −
n
2m 2 ij |Ri − Rj | 2 nn0 |rn − rn0 | in
|Ri − rn |
(2.1.3)
and " #
X P2
i
+ V (Ri ) Ξ (Ri ) = EΞ (Ri ) (2.1.4)
i
2M i
Equation 2.1.3 is the electronic problem, considering the nuclei as fixed. The eigenvalue
of the energy, V (Ri ), will depend parametrically on the coordinates of the nuclei; we call
this the interatomic potential. Once found, this quantity enters equations 2.1.4 which will
give the motion of the nuclei. Note how in this equation there are no electronic degrees
of freedom; it is all incorporated in V (Ri ). For the nuclei, it is customary to replace this
Schrödinger equation with a Newtonian equation1 .
1
This is acceptable if quantum effects are small. For example light systems such as H2 and He or
systems at very low temperatures may have quantum effects and MD results should be interpreted with
caution.
The first three terms represent the bonded interactions. The electrostatic and Van der
Waals energies are non bonded interactions between atoms. To be able to calculate the
force field energy it is necessary to know atomic coordinates, geometries, and the relative
energies of the atoms in the system.
The calculation of the total potential energy is dominated by the number of non-bonded
interactions. The time for calculation of the bonded interactions increase on the order of
∼ N , while the time for calculation of the non-bonded interactions increase on the order
of ∼ N 2 .
The stretching energy of a bond is the energy of the extension of the spring, bonding
two atoms, Figure 2.1. The shape of the energy curve is well described by a Taylor
expansion, equation 2.3.1, where r0 is an equilibrium bond length for the given system
d E AB
Estr rAB − r0AB = E (r0 ) + r − r0AB
dr
1 d2 E AB AB 2 1 d3 E AB 3
r − r0AB + O r4 (2.3.1)
+ 2 r − r0 + 3
2 dr 6 dr
The Morse potential reproduces stretching well over a wide range of distances, at long
distances tends to zero. Hence the Morse potential is slow in bringing atoms to equilib-
rium bond lengths, when the initial geometry has a large bond length. The comparison
between harmonic and Morse potentials is shown in Figure 2.2.
Figure 2.2: Bond stretching energy. Harmonic potential compared to the Morse potential.
The stretching energy has the highest contribution to overall energy of the system, fol-
lowed by the bending energy.
Similarly to the stretching energy, bending energy can also be expressed by a Taylor
expansion, that can then be approximated to a harmonic potential, equation 2.3.4, where
θABC is an angle between three atoms A, B and C, as shown in Figure 2.3, and k ABC is
the force constant for the bending between three given atoms.
1 2
Ebend = k ABC θABC − θ0ABC (2.3.4)
2
Four aligned atoms frequently will rotate along the central bond, as shown in Figure 2.5.
Rotation along the bond is continuous, so when the bond rotates by 360◦ the energy
should return to the initial value, making torsional energy periodic. The potential is well
represented by the Fourier series, equation 2.3.6, where n is a periodicity term, depending
on the allowed rotation it can be n = 1 for full 360◦ rotation, n = 2 for 180◦ periodicity,
n = 3 for 120◦ periodicity, etc. Vn is a constant determining the barrier for the rotation,
Figure 2.5: Torsional / dihedral angle, created by four linearly aligned atoms.
Vn is not equal to zero for allowed periodic rotations, ϕ is the torsional angle (also called
the dihedral angle) and is shown on the Figure 2.5.
X
Etors = Vn cos (nϕ − ϕ0 ) (2.3.6)
n
Figure 2.6 shows a plot of the two torsional potentials with n = 3, Vn = 4 (dashed line),
n = 2, Vn = 3 (solid line) and the corresponding total torsional potential in red if the two
are combined.
Figure 2.6: Variation of torsional potential for different values of n and Vn and the total
potential (red).
There are also cross terms available for some force fields. These can cover the coupling
between the fundamental stretching, bending and torsional interactions.
The Van der Waals energy describes the repulsion and attraction between non bonded
atoms. At large interatomic distances the Van der Waals energy goes to zero, whereas at
short distances it is positive and very repulsive. This mimics the overlap of the negatively
charged electronic clouds of atoms. At intermediate distances there is a mild attraction
between electronic clouds, due to electron-electron correlation. The attraction between
two atoms arises because of an induced dipole–dipole moment created by the motion of
electrons through the molecule, which effects the neighbouring molecule. The attraction
between two fragments does not only depend on dipole–dipole interactions, but also on
dipole–quadrupole, quadrupole–quadrupole etc. interactions. These interactions do not
have a high contribution to the overall energy, so for simplicity they are neglected in
calculations.
Hard-Sphere
Hard spheres are widely used as model particles in the statistical mechanical theory of
fluids and solids. They are defined simply as impenetrable spheres that cannot overlap
in space. They mimic the extremely strong repulsion that atoms and spherical molecules
experience at very close distances. Hard spheres of diameter, σ, are particles with the
following pairwise interaction potential:
(
0 if |r1 − r2 | ≥ σ
V (r1 , r2 ) = (2.4.1)
∞ if |r1 − r2 | < σ
Square-Well Potential
Square-Well potential is an update on the hard spheres model. They are defined simply as
impenetrable spheres that cannot overlap in space, but they can have an attractive region
around the hard sphere. For a spheres of diameter, σ, are particles with the following
pairwise interaction potential:
0
if |r1 − r2 | ≥ λσ
V (r1 , r2 ) = −Vi if σ ≤ |r1 − r2 | < λσ (2.4.2)
∞ if |r1 − r2 | < σ
Where λ is the relative size of the attractive region. The sphere in the centre does not have
to have an infinite repulsion, it can be set at a large positive value allowing some overlap
of the spheres.
In 1903, Gustav Mie proposed an intermolecular pair potential[2], comprising two parts to
represent attractive and repulsive forces, equation 2.4.3, where r is interatomic distance,
|r1 − r2 |, is the depth of the well at σ, the interatomic separation at which repulsive and
attractive terms balance out and m, n can be adjusted.
n n m/(n−m) h σ n σ m i
V (r1 , r2 ) = − (2.4.3)
n−m m r r
There have been a number of models suggested, where m and n take different values and
show an accurate representation of experimentally known interactions [3, 4]. Neverthe-
less, the most popular one is the 12–6 Lennard-Jones potential [5], equation 2.4.4, where
σ is the interatomic separation at which repulsive and attractive terms balance out and
is the depth of the well. The usage of 12 term reduces the amount of calculations needed,
making it the lightest method available.
" AB 6 #
AB 12
σ σ
V LJ (r1 , r2 ) = 4AB − (2.4.4)
rAB rAB
The 12–6 Lennard-Jones potential is shown on the Figure 2.7. Red shows the repulsive
term, blue the attractive term and the overall potential is shown in black. Position of σ
and is also shown on the graph.
Figure 2.7: The Lennard-Jones Potential (black) is a combination of attractive (blue) and
repulsive (red) terms.
Activity
The energy between two particles can be given by the Lennard-Jones potential,
equation 2.4.4. Produce an expresion for the force between these two particles.
Using this expression write a program in matlab to calculate the force between two
particles based on the distance between them and to plot this variation in force with
distance.
Use the example system of Lennard-Jonesium, σ = 3.405 Å and /kB = 119.8 K
[6].
It is possible to mix two Lennard-Jones potentials for single type atoms to obtain a
potential describing interaction between two different atoms. There are three main ap-
proaches:
• Arithmetic or Lorentz-Berthelot rules, equations 2.4.5 and 2.4.6 [7].
σii + σjj
σij = (2.4.5)
2
√
ij = ii jj (2.4.6)
• Geometric rules equation 2.4.7, with ij calculated as given in equation 2.4.6 [8].
√
σij = σii σjj (2.4.7)
Buckingham Potential
The electrostatic energy calculates non bonded interactions that appear due to an uneven
internal distribution of electrons. This leads to positively and negatively charged parts in
molecule. The simplest way to model this behaviour is to place charges on atoms. The
law of the interaction between two point charges was investigated by Charles Augustin
Coulomb in 1780s and can be expressed by the Coulomb potential, equation 2.4.10, where
0 is the dielectric constant, the Q values are partial electronic charges on atoms A and B,
and r is a distance between them.
1 QA QB
Vel (r1 , r2 ) = (2.4.10)
4π0 rAB
The atomic charges are mainly taken from electrostatic potential calculations carried out
using higher precision methods. Another approach arises from assigning bond dipole
moments. This gives similar results to the partial charge method, but these two methods
will only give identical results for interactions at larger distances.
Commonly used truncation radii for the Lennard-Jones potential are 2.5σ and 3.2σ. Phys-
ical quantities are of course affected by the potential truncation. The effects of truncating
a full-ranged potential can be approximately estimated by treating the system as a uni-
form continuum beyond Rc . For a bulk system this usually amounts to a constant additive
correction.
1
Shifting eliminates the energy discontinuity, but not the force discontinuity. Some researchers altered
the form of the potential near Rc to obtain a smoother junction, but there is no standard way to do that.
UFF [13] was developed by Rappe et. al. in 1992. It is an unusual force field, unlike
others it covers the full periodic table, from hydrogen to lawrencium. The parameters are
obtained by estimating from given general rules, that convert individual parameters for
the element into the intermolecular and intramolecular parameters for calculations.
The performance of this force field with respect to the experimental results [17], is not
very consistency, so it is not really suitable for high accuracy condensed state calculations.
This force field is however useful for calculations of exotic molecules, where other force
field have not been created.
AMBER is both a molecular dynamics package and a set of force fields [12, 18]. The force
field uses 12-6 Lennard-Jones, equation 2.4.4, with parameters computed by Lorentz-
Berthelot mixing rules, equations 2.4.5 and 2.4.6, 1-4 interactions are scaled by 21 and
Columbic interactions are represented by point charges and are scaled by 56 . Bond and
angle interactions are expressed as harmonic potentials and torsional interactions are rep-
resented as a cosine series.
AMBER force fields are all atom only (though the newest force field, ff12sb, does include
some united atom parameter), parametrised for biomolecular simulations and contains
the 20 common amino acids as functional groups. More recently there have been some
work put in to extend the AMBER force field to a wider range of systems through the
development of the general AMBER force field (GAFF)[19].
CHARMM is also molecular dynamics program [11], as well as a set of force fields. There
are all atom CHARMM22 [20, 21], CHARMM27 [22], CgenFF [23], and united atom
CHARMM19 [24] force fields. The potential function is of the same form as AMBER;
however, non-bonded parameters are not scaled.
All of the force fields, apart from more recent CgenFF, were parametrised for biomolec-
ular simulations, similarly to AMBER, containing the 20 common amino acid groups.
CgenFF was created as a general force field for drug-type molecules, to accompany the
rest of the force fields.
OPLS force field was developed by group of William L. Jorgensen and contains several
sets of parameters, for both all atom and united atom calculations that can be mixed
[10, 14]. The potential functions are the same as in AMBER, Lennard-Jones is computed
by geometrical mean mixing rules, equation 2.4.7. Both, Lennard-Jones and Coulombic,
parameters are scaled by a 1/2.
OPLS at the beginning was parametrised not only for biomolecular systems, but also
organic liquids. The parameters were optimized to fit experimental densities and heats
of vaporisation of liquids, as well as gas-phase torsional profiles. The force field has
being updated and many independent modifications to the force field have been developed
[25, 26]. OPLS is, arguably, the most widely parametrised force field available.
GROMOS is also both a molecular dynamics package [15] and the complementary force
field. The first was also the base for GROMACS package [27]. GROMOS is a united atom
force field, with ongoing improvements being done, currently most widely used version
of the force field is GROMOS-96 [28] and its latest update [29]. This force field is also
aimed at biomolecular calculations and is parametrised for proteins.
Unlike AMBER, the Lennard-Jones parameters for heteroatomic interactions are readily
provided by the force field and should not be calculated via mixing rules. The force
field uses a quartic expression for the bond length, a harmonic cosine potential for angle
bending and a single cosine series for torsional angle potential.
TraPPE force field evolved from the previous SKS (Smit-Karaborni-Siepmann) force field
[30]. SKS was developed in Shell laboratories in the 1990s and is the first force field,
aimed at reproducing properties of n-alkanes, the main components in fuels. The TraPPE
force field contains parameters for alkanes, aromatics, some oxygen, sulphur and nitrogen
containing compounds.
TraPPE force field is mainly united atom [16, 31–36] with two explicit hydrogen models
[37, 38]. Unlike most of united atom force fields, in TraPPE hydrogen atoms are also
united in aromatic interactions.
The functional form is the same as in AMBER, including mixing rules, Coulombic terms
are scaled by a 21 . Since the force field was developed with Monte Carlo calculations,
there is no improper torsional angle term what can lead to incorrect conformations when
performing molecular dynamics. The force field is parametrised to fit the vapour-liquid
coexistence curve for small organic molecules.
successfully predict the physical properties of liquid water then the (unknown) structure of
liquid water is determined. There are two general forms for these models of water:
Explicit Water, where each water molecule is simulated directly.
Implicit Water, where the general effects of water are taken into account but the molecules
are not directly simulated.
When simulating water explicitly it is important to use an accurate but efficient model.
This is due to the computational expensiveness of simulating water in a solvated system,
e.g. in simulating a protein in periodic box full of water at aqueous density about 90% of
the computational time is used in simulating water-water interactions.
The original atomic-scale computational model for liquid water was proposed by Bernal
and Fowler in 1933 [39]. This model took into account the position and the charge on
the hydrogens and the position of the negative charge in the molecule. It was a highly
insightful model and similar to the general view in Figure 2.8(c). At the time of the
earliest developments of protein force fields the ST2 model of Stillinger and Rahman
[40] was in wide use, which lead to the ST2 model being for some of the first protein
simulations.
In the early 1980s two new force fields were developed, the SPC [41] and TIP3P [42] mod-
els. They involve orienting electrostatic effects and Lennard-Jones sites. The Lennard-
Jones interaction accounts for the size of the molecules. It is repulsive at short distances,
a ensuring that the structure does not completely collapse due to the electrostatic inter-
actions. At intermediate distances it is significantly attractive but non-directional and
competes with the directional attractive electrostatic interactions. This competition en-
sures a tension between an expanded tetrahedral network and a collapsed non-directional
network (for example, similar to that found in liquid noble gases).
Since then many models for water have been created around these general principles, al-
though the charge and Lennard-Jones sites don’t always coincide. The four main general
model types are shown in Figure 2.8. Generally each model is developed to fit well with
one particular physical structure or parameter (for example, the density anomaly, radial
distribution function or the critical parameters) and it comes as no surprise when a model
developed to fit certain parameters it gives good compliance with these same parameters
[43]. In spite of the heavy computational investment in the calculations, the final agree-
ment with experimental data is often ‘by eye’ and not statistically tested or checked for
parametric sensitivity. Also, tests for ‘quality’ often use the radial distribution fit with
diffraction data in spite of the major fitted peaks being derived from the tetrahedral nature
of water that is built into every model and overpowers any disagreement in the fine detail.
In particular, the O—O radial distribution function seems to be a poor discriminator be-
tween widely differently performing models [44]. Table 2.1 shows the values predicted
for some key water properties by some of the most popular water force fields.
There are many circumstances in molecular modeling studies in which a simplified de-
scription of solvent effects has advantages over the explicit modelling of each solvent
Figure 2.8: Models for water molecules, (a) is a 3-site model, (b) and (c) are alternative
4-site model, and (d) is a 5-site model. Red spheres are oxygen atoms, white spheres are
hydrogen atoms, and yellow spheres are dummy charge locations.
molecule. Most of these are where the saving of computer time are needed over using
explicit water. Most of the simulation techniques have models in which the solute de-
grees of freedom are treated explicitly but the solvent degrees of freedom are not. This
requires that the energy surface used for the protein degrees of freedom be a potential
of mean force (PMF) in which the solvent degrees of freedom are implicitly averaged
over [68]. Assuming that the full potential energy function consists of a term, Uvac for
the interactions within the protein, depending only on the protein degrees of freedom, r,
and an additional term for the protein-solvent and solvent-solvent interactions, the PMF
is ideally equation 2.6.1, where DeltaGsol (r) is the free energy of transferring the protein
from vacuum to the solvent with its internal degrees of freedom fixed at r.
Generalized Born Model [71, 72] computes the electrostatic work required to move a
charged sphere from a vacuum environment into a continuous dielectric region. The
result is proportional to the square of the charge and is inversely proportional to the
size of the ion. The basis of generalized Born theory is to extend these ideas to non-
spherical molecules by casting the electrostatic contribution to solvation. The Born
model have not traditionally considered salt effects, but the model has be extended
to low-salt concentrations [73]. The key to making Generalized Born calculations
more accurate (in the sense of agreement with Poisson-Boltzmann calculations) is
improved estimation of the effective Born radii [74].
2.7 References
[1] Philip M. Morse. Diatomic Molecules According to the Wave Mechanics. II. Vibra-
tional Levels. Phys. Rev., 34:57–64, Jul 1929.
[2] Gustav Mie. Zur kinetischen Theorie der einatomigen Körper. Annalen der Physik,
316(8):657–697, 1903.
[3] A. Warshel and S. Lifson. Consistent force field calculations. II. Crystal structures,
sublimation energies, molecular and lattice vibrations, molecular conformations,
and enthalpies of alkanes. The Journal of Chemical Physics, 53:582, 1970.
[4] David N.J. White. A computationally efficient alternative to the Buckingham poten-
tial for molecular mechanics calculations. Journal of Computer-Aided Molecular
Design, 11:517–521, 1997. 10.1023/A:1007911511862.
[5] J. E. Jones. On the Determination of Molecular Fields. I. From the Variation of the
Viscosity of a Gas with Temperature. Proceedings of the Royal Society of London.
Series A, 106(738):441–462, 1924.
[6] J.-P. Hansen and L. Verlet. Phase Transitions of the Lennard-Jones System. Physical
Review, 184:151–161, 1969.
[7] H. A. Lorentz. Ueber die Anwendung des Satzes vom Virial in der kinetischen
Theorie der Gase. Annalen der Physik, 248(1):127–136, 1881.
[8] Robert J. Good and Christopher J. Hope. New Combining Rule for Intermolecular
Distances in Intermolecular Potential Functions. The Journal of Chemical Physics,
53(2):540–543, 1970.
[9] B. E. F. Fender and Jr. G. D. Halsey. Second Virial Coefficients of Argon, Kryp-
ton, and Argon-Krypton Mixtures at Low Temperatures. The Journal of Chemical
Physics, 36(7):1881–1888, 1962.
[10] William L. Jorgensen, David S. Maxwell, and Julian Tirado-Rives. Development
and Testing of the OPLS All-Atom Force Field on Conformational Energetics
and Properties of Organic Liquids. Journal of the American Chemical Society,
118(45):11225–11236, 1996.
[11] B. R. Brooks, C. L. Brooks, A. D. Mackerell, L. Nilsson, R. J. Petrella, B. Roux,
Y. Won, G. Archontis, C. Bartels, S. Boresch, A. Caflisch, L. Caves, Q. Cui, A. R.
Dinner, M. Feig, S. Fischer, J. Gao, M. Hodoscek, W. Im, K. Kuczera, T. Lazaridis,
[34] J.M. Stubbs, J.J. Potoff, and J.I. Siepmann. Transferable potentials for phase equi-
libria. 6. United-atom description for ethers, glycols, ketones, and aldehydes. The
Journal of Physical Chemistry B, 108(45):17596–17605, 2004.
[35] C.D. Wick, J.M. Stubbs, N. Rai, and J.I. Siepmann. Transferable potentials for phase
equilibria. 7. Primary, secondary, and tertiary amines, nitroalkanes and nitrobenzene,
nitriles, amides, pyridine, and pyrimidine. The Journal of Physical Chemistry B,
109(40):18974–18982, 2005.
[36] N. Lubna, G. Kamath, J.J. Potoff, N. Rai, and J.I. Siepmann. Transferable potentials
for phase equilibria. 8. United-atom description for thiols, sulfides, disulfides, and
thiophene. The Journal of Physical Chemistry B, 109(50):24100–24107, 2005.
[37] B. Chen and J.I. Siepmann. Transferable potentials for phase equilibria. 3. Explicit-
hydrogen description of normal alkanes. The Journal of Physical Chemistry B,
103(25):5370–5379, 1999.
[38] N. Rai and J.I. Siepmann. Transferable potentials for phase equilibria. 9. Explicit
hydrogen description of benzene and five-membered and six-membered heterocyclic
aromatic compounds. The Journal of Physical Chemistry B, 111(36):10790–10799,
2007.
[39] J. D. Bernal and R. H. Fowler. A Theory of Water and Ionic Solution, with Particular
Reference to Hydrogen and Hydroxyl Ions. Journal of Chemical Physics, 1:515–
548, 1933.
[40] F. H. Stillinger and A. Rahman. Improved Simulation of Liquid Water by Molecular
Dynamics. Journal of Chemical Physics, 60:1545–1557, 1974.
[41] H. J. C. Berendsen, J. P. M. Postma, W. F. van Gunsteren, and J. Hermans. Inter-
molecular Forces. Reidel, Dordrecht, 1981.
[42] W. L. Jorgensen, J. Chandrasekhar, J. D. Madura, R. W. Impey, and M. L. Klein.
Comparison of simple potential functions for simulating liquid water. Journal of
Chemical Physics, 79:926–935, 1983.
[43] J. L. F. Abascal and C. Vega. A general purpose model for the condensed phases of
water: TIP4P/2005. Journal of Chemical Physics, 123:234505, 2005.
[44] P. E. Mason and J. W. Brady. “Tetrahedrality” and the relationship between collec-
tive structure and radial distribution functions in liquid water. Journal of Physical
Chemistry B, 111:5669–5679, 2007.
[45] M-L. Tan, J. T. Fischer, A. Chandra, B. R. Brooks, and T. Ichiye. A temperature of
maximum density in soft sticky dipole water. Chemical Physics Letters, 376:646–
652, 2003.
[46] K. Kiyohara, K. E. Gubbins, and A. Z. Panagiotopoulos. Phase coexistence proper-
ties of polarizable water models. Molecular Physics, 94:803–808, 1998.
[47] D. van der Spoel, P. J. van Maaren, and H. J. C. Berendsen. A systematic study of
water models for molecular simulation: Derivation of water models optimized for
use with a reaction field. Journal of Chemical Physics, 108:10220–10230, 1998.
[48] M. W. Mahoney and W. L. Jorgensen. Diffusion constant of the TIP5P model of
liquid water. Journal of Chemical Physics, 114:363–366, 2001.
[49] C. Vega and J. L. F. Abascal. Relation between the melting temperature and the
temperature of maximum density for the most common models of water. Journal of
Chemical Physics, 123:144504, 2005.
[50] H. Yu and W. F. van Gunsteren. Charge-on-spring polarizable water models re-
visited: from water clusters to liquid water to ice. Journal of Chemical Physics,
121:9549–9564, 2004.
[51] H. J. C. Berendsen, J. R. Grigera, and T. P. Straatsma. The missing term in effective
pair potentials. Journal of Physical Chemistry, 91:6269–6271, 1987.
[52] L. A. Baez and P. Clancy. Existence of a density maximum in extended simple
point-charge water. Journal of Chemical Physics, 101:9837–9840, 1994.
[53] Y. Wu, H. L. Tepper, and G. A. Voth. Flexible simple point-charge water model with
improved liquid state properties. Journal of Chemical Physics, 124 :024503, 2006.
[54] I. M. Svishchev, P. G. Kusalik, J. Wang, and R. J. Boyd. Polarizable point-charge
model for water. Results under normal and extreme conditions. Journal of Chemical
Physics, 105:4742–4750, 1996.
[55] M. W. Mahoney and W. L. Jorgensen. A five-site model for liquid water and the
reproduction of the density anomaly by rigid, nonpolarizable potential functions.
Journal of Chemical Physics, 112:8910–8922, 2000.
[56] H. W. Horn, W. C. Swope, J. W. Pitera, J. D. Madura, T. J. Dick, G. L. Hura, and
T. Head-Gordon. Development of an improved four-site water model for biomolec-
ular simulations. Journal of Chemical Physics, 120:9665–9678, 2004.
[57] S. W. Rick. Simulation of ice and liquid water over a range of temperatures using
the fluctuating charge model. Journal of Chemical Physics, 114:2276–2283, 2001.
[58] P. J. van Maaren and D. van der Spoel. Molecular dynamics of water with novel
shell-model potentials. Journal of Physical Chemistry B, 105:2618–2626, 2001.
[59] M. A. González and J. L. F. Abascal. A flexible model for water based on
TIP4P/2005. Journal of Chemical Physics, 135:224516, 2011.
[60] A.-P. E. Kunz and W. F. van Gunsteren. Development of a nonlinear classical pPolar-
ization model for liquid water and aqueous solutions: COS/D. Journal of Physical
Chemistry A, 113:11570–11579, 2009.
[61] P. Paricaud, M. Predota, A. A. Chialvo, and P. T. Cummings. From dimer and con-
densed phases at extreme conditions: Accurate predictions of the properties of water
by a Gaussian charge polarizable model. Journal of Chemical Physics, 122:244511,
2005.
[62] G. Lamoureux, E. Harder, I. V. Vorobyov, B. Roux, and A. D. MacKerell Jr. A
polarizable model of water for molecular dynamics simulations of biomolecules.
Chemical Physics Letters, 418:241–245, 2005.
[63] S. W. Rick. A reoptimization of the five-site water potential (TIP5P) for use with
Ewald sums. Journal of Chemcal Physics, 120:6085–6093, 2004.
[64] G. S. Fanourgakis and S. S. Xantheas. The flexible, polarizable, Thole-type inter-
action potential for water (TTM2-F) revisited. Journal of Physical Chemistry A,
110:4100–4106, 2006.
32
2
Calculated at 27 ◦ C
3
Calculated at 20 ◦ C
4
Polarizable model
Chapter 3
Practical Aspects of Molecular
Dynamics
Contents
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.2 Reduced Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.3 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.4 Force Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
3.5 Simulation Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.6 Integration Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.6.1 Verlet algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.6.2 The Leap-Frog Algorithm . . . . . . . . . . . . . . . . . . . . 41
3.6.3 The velocity Verlet algorithm . . . . . . . . . . . . . . . . . . 42
3.6.4 Beeman’s algorithm . . . . . . . . . . . . . . . . . . . . . . . 43
3.7 Temperature Coupling . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.7.1 Velocity Rescaling . . . . . . . . . . . . . . . . . . . . . . . . 44
3.7.2 Berendsen Thermostat . . . . . . . . . . . . . . . . . . . . . . 44
3.7.3 Andersen Thermostat . . . . . . . . . . . . . . . . . . . . . . . 44
3.7.4 Nose-Hoover Thermostat . . . . . . . . . . . . . . . . . . . . . 45
3.7.5 Practical use of Thermostats . . . . . . . . . . . . . . . . . . . 45
3.8 Pressure coupling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.8.1 Berendsen Barastat . . . . . . . . . . . . . . . . . . . . . . . . 46
3.8.2 Parrinello-Rahman Barastat . . . . . . . . . . . . . . . . . . . 46
3.9 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
33
34
3.1. INTRODUCTION
3.1 Introduction
The best way to examine the practical aspects of Molecular Dynamics is to consider a
simple program. This program is kept as simple as possible to allow illustration of the
key features of a Molecular Dynamics simulaion, algorithm 3.1.
The key steps in the program are:
1. Read in the parameters that specify the conditions of the simulation, e.g. initial
temperature, number of particles, density, and time step.
2. Initialize the system, i.e. select the initial position and velocities.
3. Conpute the forces on all particles.
4. Integrate Newton’s equations of motion.
5. Steps 3 and 4 are the core part of the simulation and are repeared until the time of
the simulation has reached the desired time.
6. After completion, we compute and print the averages of the measured quantities we
want.
The most important reason for this is that there are many combinations of ρ, T , , and
σ that correspond to the same state in reduced units, Figure 3.1 provides an example for
some components. Another practical reason is that if we were working with SI units the
absolute values would be very small meaning that computational errors could appear in
the simulations.
1 % Simple MD program
2
3 % Read in initial parameters
4 MDparam;
5
6 % Initialize the system
7 [coords,L,velSTD,vels] = init(Temp,mass,density,nPart,nDim);
8
9 % Set initial time
10 time = 0;
11
12 for step = 1:nSteps
13
14 % Calculate the forces
15 [forces,virial,en] = calcforce(coords,L);
16
17 % Intergrate the equations of motion
18 [coords,vels] = integrate(coords,forces,vels,mass,L,dt,nDim,velSTD,nu);
19
20 % Step time forward
21 time = time + dt;
22
23 % Sample measured qualities
24 samples(step,nSteps,sampleFreq,vels,coords,L);
25
26 end
(a) (b)
Figure 3.1: Variation of the vapour-liquid equlibria for some example components,
squares are saturated vapour and diamonds are saturated liquid. (a) The plots in S.I. units
and (b) the plots in reduced units. LJ parameters: Nitrogen /k = 99 K and σ = 360 pm,
Methane /k = 148 K and σ = 373 pm, Argon /k = 117 K and σ = 339 pm.
3.3 Initialization
To start the simulation we need to assign initial positions and velocities to all particles in
the system. The particles should be positioned so that the structure is compatible with the
structure we are aiming to simulate, and should be picked so that the positions don’t result
in an overlap of the molecular cores. Often the most convenient option for this is to place
the particles on a simple cubic lattice, as in Algorithm 3.2. Assuming that the values of
the density and temperature are chosen such that the simple cubic lattice is unstable and
melts rapidly.
After placing each particle on its lattice site we need to attribute a velocity to each particle.
To allow each particle to have a velocity which fits to the expected ditribution of velocities
we can attribute each particle a velocity based on a Gaussian distribution with a mean
p
value of 0 and a standard deviation of (T /m)1 , as in Algorithm 3.3. This process should
make the total momentum zero; however, as the distribution is random, the resulting
velocities should be adjusted so that the total momentum is zero,
N
X
vi
i=1
vi = vi − (3.3.1)
N
The velocities also need to be adjusted so that the mean kinetic energy is the desired
value for the temperature of the simulation equation 1.2.7. We can adjust the initial tem-
perature
p to match the desired temperature, T , by scaling all velocities with the factor
T /Tinit , r
T
vi = vi (3.3.2)
Tinit
1
as at thermal equlibrium vi2 = kB T /m
This initial setting of the temperature is not particularly critical, as the temperature will
change anyway during the equilibration; however, this speeds up the equilibration process.
Activity
Create the function for init to produce the inital coordinates and velocities for the
MD program. Algorithms 3.2 and 3.3 maybe useful for this.
Check your code by calculating the mean and standard deviation of the velocities.
What does the distrubution of velocities look like?
Activity
Create the function calcforce based on the interaction between the particles being
given by the Lennard-Jones potential, equation 2.4.4. Remember we are using re-
duced units. Algorithm 3.4 may be useful.
Check that this gives the correct shape of the force.
1
There exist effcient techniques to speed up this evaluation, to scale with N ; however, these are beyond
the scope of this course
2
Note that we don’t compute |rij | because it is computationally expensive due to the calculation of a
square root
r + la + mb + nc, {∀ l, m, n ∈ Z} (3.5.1)
where a, b, and c are the vectors corresponding to the edges of the box. All the image
molecules move together, and infact only one is actually represented in the program.
This results in a simulation with bulk properties across the whole original box. When
the molecule leaves the box on one side, as shown on the Figure 3.2, it will enter the
neighbouring ‘pseudo’ box, and by such re-enter the initial box from the opposite side.
To be able to perform calculations at the reasonable speed, the interaction of the molecule
is only calculated with it’s direct neighbours. The standard way is to use a Verlet list [1]
that, for every particle, defines a surrounding shell at a distance slightly larger than the
cut-off radius for the interactions. The cut-off distance should be less or equal to the half
of the box size.
This means when we calculate the distance between particles we need to check to see if
the image of the particle over the periodic boundary condition is closer, Algorithm 3.4.
Figure 3.2: Periodic boundary condition, two-dimensional illustration: the box contain-
ing blue atoms is the simulation box, while the boxes containing white atoms are ‘pseudo’
boxes, that mimic the behaviour of the main simulation box.
Algorithm 3.4: Code to check distance between particles with cubic periodic
bondary conditions.
This algorithm was developed by Loup Verlet in 1967 [2] and became the base for most
algorithms used in modern simulations. The algorithm calculates positions forward and
backward in time allowing a better calculation of the new positions than a simple single
step numerical integration. This is performed via a third order Taylor expansion, equa-
tion 3.6.1.
1 1 ...
r (t + δt) = r (t) + v (t) δt + a (t) δt2 + r (t) δt3 + O δt4
2 6
1 1 ...
r (t − δt) = r (t) − v (t) δt + a (t) δt2 − r (t) δt3 + O δt4
(3.6.1)
2 6
The main problem with this algorithm is that the velocities are not directly generated,
though it makes it computationally cheaper as lower data storage is required. The Verlet
algorithm can be seen graphical as Figure 3.3(a).
These are then used to calculate the position at the full time step, δt, equation 3.6.4.
δt
r (t + δt) = r (t) − v t + δt (3.6.4)
2
(a) Verlet
(b) Leap-Frog
In this method the velocities leap over positions, which in turn leap over velocities. The
advantage of this method is that velocities are calculated accurately. However, they are
not calculated at the same time as positions. So if velocities at a given time, t, are required
they must be averaged as equation 3.6.5.
1 δt δt
v (t) = v t− +v t+ (3.6.5)
2 2 2
This algorithm is easily applied to complex molecules and may also be combined with
constraint algorithms. The speed and simplicity of this algorithm means it often makes it
the first choice for molecular simulation. The leap-frog algorithm can be seen graphical
as Figure 3.3(b).
This is another modification of the Verlet algorithm, similar to the leap-frog algorithm,
but allowing the calculation of positions, velocities and accelerations at the given time
[3],
1
r (t + δ t) = r (t) + v (t) δ t + a (t) δ t2 (3.6.6)
2
a (t) + a (t + δ t)
v (t + δ t) = v (t) + δt (3.6.7)
2
This results in more accurate trajectories and less drift of the total energy. The increase in
precision also comes with increase in computational cost. The standard implementation
scheme of this algorithm is:
1. Calculate r (t + δ t) from equation 3.6.6.
2. Calculate the half step velocity, v (t + 0.5δ t) = v (t) + 0.5a (t) δ t.
3. Derive a (t + δ t) from the interaction force using r (t + δ t).
4. Calculate the final velocity, v (t + δ t) = v (t + 0.5δ t) + 0.5a (t + δ t) δ t
The velocity verlet algorithm can be seen graphical as Figure 3.3(c).
Beeman’s algorithm is also related to Verlet algorithm. Beeman’s algorithm [4] uses more
accurate expressions, that allows for more accurate velocities and provides better energy
conservation. This method is computationally more expensive and is only used when high
accuracy is required.
Activity
Create the function integrate based on the velocity verlet algorithm. Your Molecular
Dynamics simulation can now be ran. Try the parameters below:
1 % Set configuration parameters
2 nPart = 125; % Number of particles
3 density = 0.85; % Density of particles
4 mass = 1; % Particles’ mass
5 nDim = 3; % The dimensionality of the system
6
7 % Set simulation parameters
8 dt = 0.0005; % Integration time
9 Temp = 2.0; % Simulation temperature
10
11 nSteps = 1000; % Total simulation time (in integration steps)
Monitor the temperature and total energy to check your simulation; Algorithms 4.2
to 4.4 will be useful. How do these vary with time?
in the canonical ensemble, N V T . There are several approaches to doing this and we must
consider whether the methods:
• preserve the correct thermodynamics, i.e. the correct microstate distribution in the
canonical ensemble.
• preserve realistic dynamics in that the equations of motion can be used to compute
transport quantities accurately.
As the temperature of the system is related to the average kinetic energy, the temperature
can be controlled by scaling velocities.
The idea behind this is that we rescale the velocities at each step (or after a preset number
of steps) so that the kinetic energy gives the desired target temperature,
vnew = λv (3.7.1)
p
as in equation 3.3.2 where λ = T /T (t).
Although simple, this approach does not generate the correct thermodynamic properties
of the canonical ensemble. Consider the limit in which we rescale the velocities at every
time step. In this case, the kinetic energy will remain constant in time, with zero fluc-
tuations. This is not correct, as the statistical mechanics of the canonical ensemble says
2 2 2
that σK.E. = 3/2N kB T . In other words, velocity rescaling does not capture the correct
energy fluctuations in the system.
The Berendsen thermostat [5] is similar to the velocity rescaling approach, but assigns
a time scale for the updating of the velocities, rather than assuming they are completely
scaled to the target temperature at each time step. Underlying this approach is the as-
sumption that the system is weakly coupled to a heat bath whose coupling constant, or
time scale of heat transfer, is τ , equation 3.7.2.
2 δt T
λ =1+ −1 (3.7.2)
τ T (t)
Here, δ t is the time step in the molecular dynamics simulation. Typically, τ = 0.1−0.4 ps
for δ t = 1 fs. λ is the velocity rescaling factor as before.
The Berendsen thermostat, however, suffers from the same problems as velocity rescaling
in that the energy fluctuations are not captured correctly. There are also some notable
pathologies for specific systems.
The Andersen thermostat [6] introduces a stochastic element to the temperature by having
random collisions of molecules with an imaginary heat bath at the desired temperature.
In the single-particle approach, a random particle is chosen and its velocity is reassigned
randomly from a Maxwell-Boltzmann distribution at the desired temperature,
1/2
mi v2i
mi
P (vi ) = exp − (3.7.3)
2πkB T 2kB T
In the Andersen scheme, one does not perform a collision during each molecular dynamics
time step, but rather it is customary to adopt a collision frequency ν or collision time
τ = 1/ν. The collision frequency should be chosen so as not to be too short to the time
scales of molecular motions, Algorithm 3.5.
In the limit of an infinitely long trajectory averaged over many heat bath collisions, it
can be shown that the Andersen thermostat rigorously generates the correct canonical
ensemble probabilities. That is, the distribution of kinetic and potential energies, and the
microstate probabilities of different configurations of the system, all rigorously approach
their true form in the canonical ensemble.
However, the presence of random collisions causes the velocities of particles to decorre-
late (“lose memory” of their initial values from some previous point in time) much faster
than the N V E dynamics. As a result, true molecular kinetics are not preserved by the
Andersen thermostat. For example, the computed diffusion constants for particles would
give erroneous values.
This algorithm has the advantage that it can reproduce a correct canonical ensemble [7, 8].
The other thermostats above are weak coupling and are used for relaxing systems to a
desired temperature, whereas this thermostat can be used for the final simulations. In this
method, a thermal reservoir and friction coefficient are introduced into the equation of
motion, and thereby allow kinetic energy changes in the system.
1 function [vels]=Andersen(vels,nu,dt,velSTD,nDim)
2 % calculate number of particles
3 nPart = size(vels,2);
4 % Implement the Andersen thermostat
5 for part =1:nPart
6 % Test for collisions with the Andersen heat bath
7 if (rand < nu*dt)
8 % If the particle collided with the bath, draw a new velocity
9 % out of a normal distribution
10 vels(:,part) = randGauss(0,velSTD,nDim);
11 end
12 end
13 end
Algorithm 3.5: Code to rescale the particle velocities based on the Andersen
Thermostat.
system using a thermostat, but then to allow the production period to correspond to NVE
dynamics.
Activity
Add the Andersen Thermostat, Algorithm 3.5, into the integrate function and com-
pare the temperature produced in the simulation with that without the thermostat.
Use the parameters below:
1 % Set simulation parameters
2 nu = 1; % Thermostat parameter - frequency of collisions
3 % with the heat bath
This algorithm rescales the coordinates and box vectors at every step towards a given
reference pressure, P0 , equation 3.8.1.
dP P0 − P
= (3.8.1)
dt τP
Rescaling is performed by applying the scaling matrix, µ, equation 3.8.2, where β is the
isothermal compressibility of the system.
∆t
µ = δij − βij [P0ij − Pij (t)] (3.8.2)
3τP
Like the Berendsen thermostat, the barastat is a weak coupling so does not generate a
proper thermodynamic ensemble.
This approach is similar to the Nose-Hoover for temperature coupling, where equation of
motion for particles is updated at every step [9], and a correct thermodynamic ensemble
is produced.
3.9 References
[1] Loup Verlet. Computer "Experiments" on Classical Fluids. I. Thermodynamical Prop-
erties of Lennard-Jones Molecules. Phys. Rev., 159:98–103, Jul 1967.
[2] Loup Verlet. Computer "Experiments" on Classical Fluids. I. Thermodynamical Prop-
erties of Lennard-Jones Molecules. Phys. Rev., 159:98–103, Jul 1967.
[3] W. C. Swope, H. C. Anderson, P. H. Berens, and K. R. Wilson. A Computer Sim-
ulation Method for the Calculation of Equlibrium Constants for the Formation of
Physical Clusters of Molecules: Applications to Small Water Clusters. Journal of
Chemical Physics, 76:637–649, 1982.
[4] D. and Beeman. Some multistep methods for use in molecular dynamics calculations.
Journal of Computational Physics, 20(2):130 – 139, 1976.
[5] H. J. C. Berendsen, J. P. M. Postma, W. F. van Gunsteren, A. DiNola, and J. R. Haak.
Molecular dynamics with coupling to an external bath. The Journal of Chemical
Physics, 81(8):3684–3690, 1984.
[6] H. C. Andersen. Molecular dynamics simulations at constant pressure and/or temper-
ature. Journal of Chemical Physics, 72:2384–2393, 1980.
[7] Shuichi Nosé and M.L. Klein. Constant pressure molecular dynamics for molecular
systems. Molecular Physics, 50(5):1055–1076, 1983.
[8] Shuichi Nosé. A unified formulation of the constant temperature molecular dynamics
methods. The Journal of Chemical Physics, 81(1):511–519, 1984.
[9] M. Parrinello and A. Rahman. Polymorphic transitions in single crystals: A new
molecular dynamics method. Journal of Applied Physics, 52(12):7182–7190, 1981.
Contents
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.2 Energies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.2.1 Kinetic Energy . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.2.2 Potential Energy . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.2.3 Total Energy . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.3 Temperature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.4 Pressure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.5 Diffusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.6 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
49
50
4.1. INTRODUCTION
4.1 Introduction
Often we start our simulation with initial velocities and positions that are not represen-
tative of the state condition of interest (e.g. as specified by the temperature and density).
As such, we must equilibrate our system by first running the simulation for an amount
of time that lets it evolve to configurations representative of the target state conditions.
Once we are sure we have equilibrated, we then perform a production period of simula-
tion time that we used to study the system and/or compute properties at the target state
conditions.
One approach to check if we have equilibrated our system is to monitor the time-dependence
of simple properties, like the potential energy or pressure.
Measuring quantities in Molecular Dynamics usually means performing time average of
physical properties over te system trajectory. Physical properties are usually a function
of the particle coordinates and velocities. So for instance we can define the instantaneous
value of a generic physical property A at time t,
where t is an index which runs over the time steps from 1 to the total number of steps,
M . This can be be added in the sampling function of the Molecular Dynamics simultion,
Algorithm 4.1.
1 function [h] = samples(step,nSteps,sampleFreq,vels,coords,L,h)
2
3 % Initialisation
4 if step==1
5 % Any code needed to set up the sampling
6 end
7 % Sampling
8 if mod(step,sampleFreq) == 0
9 % Code for sampling on given steps
10 end
11
12 % End
13 if step==nSteps
14 % Code at end of simulation
15 end
16 end
4.2 Energies
The potential energy, V , is obtained by based on the force field, in the case of two-body
interactions, XX
V (t) = φ |ri (t) − rj (t)| (4.2.2)
i j>i
This is often calculated along with the force calculation for convenience with Algo-
rithm 4.3 for the Lennard-Jones potential.
1 en=0;
2 % cut off energy
3 ecut=(1/(L/2)^12)-(1/(L/2)^6);
4 % Loop over all particle pairs
5 for partA = 1:nPart-1
6 for partB = (partA+1):nPart
7 % Calculate particle-particle distance
8 dr = coords(:,partA) - coords(:,partB);
9 % Fix according to periodic boundary conditions
10 dr = distPBC3D(dr,L);
11 % Get the distance squared
12 dr2 = dot(dr,dr);
13 % Check if the particles are within the cutoff
14 if dr2 < (L/2)^2
15 % Lennard-Jones potential:
16 % U(r) = 4*epsilon* [(sigma/r)^12 - (sigma/r)^6]
17 %
18 % U(r) = 4 * [(1/r)^12 - (1/r)^6]
19 invDr2 = 1.0/dr2; % 1/r^2
20 invDr6 = invDr2^3;
21 en=en+invDr6*(invDr6-1)-ecut;
22 end
23 end
24 end
25 % Multiply energy by 4
26 en=en*4;
The term ecut is the value of the potential as r = rc (the cut off), for the Lennard-Jones
potential we have,
1 1
ecut = 4 12 − 6 (4.2.3)
rc rc
In practice there could be small fluctuations in the total energy. These fluctuations are
usually caused by errors in the time integration and can be reduced in magnitude by re-
ducing the time step. Slow drifts of the total energy are also sometimes observed in very
long runs. Such drifts could also originate from large time steps. Drifts are more disturb-
ing than fluctuations because the thermodynamic state of the system is also changing with
the energy. If drifts over long runs tend to occur, they can be prevented, for instance by
breaking the long run into smaller pieces and restoring the energy to the nominal value
between one piece and the next. A common mechanism to adjust the energy is to modify
the kinetic energy via the rescaling of velocities1 .
4.3 Temperature
There is no rigorous microscopic definition of the temperature in the microcanonical en-
senble. Instead we can use the macroscopic thermodynamic results,
1 ∂S ∂ ln Ω
= = kB (4.3.1)
T ∂ E V,N ∂ E V,N
As mentioned in Chapter 1, it can be shown using the equipartition theorem that the
average kinetic energy relates to the temperature via equation 1.2.6. This means that we
can calculate the temperature for a time step as,
2EK (t)
T (t) = (4.3.2)
kB Nf
Because the kinetic energy fluctuates during a simulation the temperature also fluctuates.
This is an estimator of the temperature and we must perform an average to find the true
temperature.
Although it is not fequently used, we can also compute a configurational estimate of the
temperature (rather than kinetic estimate) [1],
N N
F ·F
kB T = (4.3.3)
−∇ · FN
This estimate depends on the forces and their derivatives. Both the kinetic and configura-
tional temperatures are equal in the limit of infinite simulation time and equilibrum.
1
Temperature scaling will of course change the total energy of the system as the velocities are being
rescaled.
4.4 Pressure
To compute the pressure, we can often use the virial theorem,
* +
1 X
P = 3N kB T + Fi · ri (4.4.1)
3V i
This expression is derived for the canonical ensemble (N V T ), but it is often applied to
Molecular Dynamics simulations regardless. Though for large systems the difference
between N V E and N V T is very small.
The expression above is not generally used for systems of pairwise-interacting molecules
subject to periodic boundary conditions. Instead, we can rewrite the force sum,
!
X X X X
Fi · ri = Fij · ri = Fij · ri
i i j i,j
X X
= Fij · ri + Fij · ri
i<j i>j
X X
= Fij · ri + Fji · rj
i<j i<j
X
= Fij · (ri − rj )
i<j
X d u (rij )
= − rij (4.4.2)
i<j
dr
This term is called the virial and contains the sum of pairwise interactions. It is therefore
convenient to compute it in the pairwise loop with the forces and energies. For an example
with the Lennard-Jones Potential (equation 2.4.4),
X X
−12 −6
Fi · ri = −24 2rij − rij (4.4.3)
i i<j
The virial can be implemented in the Molecular Dynamics simulation as Algorithm 4.5.
1 virial=0;
2 % Loop over all particle pairs
3 for partA = 1:nPart-1
4 for partB = (partA+1):nPart
5 % Calculate particle-particle distance
6 dr = coords(:,partA) - coords(:,partB);
7 % Fix according to periodic boundary conditions
8 dr = distPBC3D(dr,L);
9 % Get the distance squared
10 dr2 = dot(dr,dr);
11 % Check if the particles are within the cutoff
12 if dr2 < (L/2)^2
13 % Lennard-Jones potential:
14 % Fx(r) = 4 * (x/r) * [12*(1/r)^13 - 6*(1/r)^7]
15 % = 48 * x * (1/r)^8 * [(1/r)^6 - 0.5]
16 invDr2 = 1.0/dr2; % 1/r^2
17 invDr6 = invDr2^3;
18 forceFact = invDr2 * invDr6 * (invDr6 - 0.5);
19 virial=virial+forceFact*dr2;
20 end
21 end
22 end
23 % Multiply virial by 48
24 virial = virial*48;
Activity
Add the sampling of the pressure to your Molecular Dynamics program using Algo-
rithms 4.5 and 4.6. How does the pressure change with temperature from T ∗ = 0.8
to T ∗ = 1.1?
How is the pressure variation different if ρ∗ = 0.005?
Why?
4.5 Diffusion
Diffusion is the process whereby an initially nonuniform concentration profile (e.g. an ink
drop in water) is smoothed in the absence of flow. Diffusion is caused by the molecular
motion of the particles in the fluid. The macroscopic law that describes diffusion is known
as Fick’s law, which stats that the flux j of the diffusing species is proportional to the
negative gradient in the concentration of that species,
j = −D∇ c (4.5.1)
∂ c (r, t)
+ ∇ · j (r, t) = 0 (4.5.2)
∂t
∂ c (r, t)
− D∇2 c (r, t) = 0 (4.5.3)
∂t
What we need to link with simulations is actually R not an expression for c (r, t) itself
but the time dependence of its second moment, drc (r, t) r2 . Thus we can multiply
equation 4.5.3 by r2 and integrate over all space,
Z Z
∂
drr c (r, t) = D drr2 ∇2 c (r, t)
2
(4.5.4)
∂t
R
Defining the fact that drc (r, t) = 1, i.e. a total amount of material equal to 1, then the
left hand side of equation 4.5.4 reduces to ∂ hr2 (t)i /∂ t, thus,
∂ r2 (t)
Z
=D drr2 ∇2 c (r, t)
∂t
Z Z
= D dr∇ · r ∇c (r, t) − D dr∇r2 · ∇c (r, t)
2
Z Z
2
= D dS r ∇c (r, t) − 2D drr · ∇c (r, t)
Z Z
= 0 − 2D dr (∇ · rc (r, t)) − dr (∇ · r) c (r, t)
Z
= 0 + 2dD drc (r, t)
= 2dD (4.5.5)
where d is denotes the dimensionality of the system. Equation 4.5.5 relates the diffusion
coefficient D to the width of the concentration profile. This relation was first derived
by Einstein. It should be noted that whereas D is a macroscopic transport coefficient,
hr2 (t)i has a microscopic interpretation; it is the mean-squared distance over which the
molecules have moved in a time interval t. Therefore, to find D for every particle i, we
measure the distance travelled in time t, ∆ ri (t),
N
2 1 X
r (t) = ∆ r2i (t) (4.5.6)
N i=1
4.6 References
[1] B. D. Butler, G. Ayton, O. G. Jepps, and D. J. Evans. Configurational temperature:
Verification of Monte Carlo simulations. Journal of Chemical Physics, 109:6519–
6522, 1998.
57
58
To think about how we translate instructions, be them mathematical equations or written
directions, into code let’s take the example of making a cake. These instructions can be
converted into blocks of code as functions containing actions, e.g. for loops, if statements,
etc.
Original Recipe
Wet Ingredients
150g margarine
300ml soya milk
1 tbsp cider vinegar
0.5 tsp vanilla extract
Dry Ingredients
300g plain flour
200g golden caster sugar
4 tbsp cocoa powder
1.5 tbsp baking power
1 tsp bicarbonate of soda
Method
1. Preheat the oven to 190 ◦ C (170 ◦ C for fan).
2. Grease the base and sides of a 25 cm diameter circular tin with margarine, then line
the base with baking parchment.
3. Add the wet ingredients together in a bowl and mix together well.
4. In a second bowl, add the dry ingredients together and mix.
5. Add the mixed dry ingredients to the wet ingredients and mix until smooth.
6. Add the cake batter to the prepared tin and bake for 30 mins or until a skewer in-
serted into the middle of the cakes comes out cleanly.
7. Leave to cool in the tin for 10 mins then turn out onto a wire rack to cool completely.
Translating to Code
To translate the above instructions into code, we can start by taking the steps and defining
a function1 for the steps2 ,
1 % Program to bake a cake
2
3 % Read in ingredient list
4 wet_ingredients = [150g margarine, 300ml soya milk, 1 tbsp cider vinegar,
0.5 tsp vanilla extract];
5 dry_ingredients = [300g plain flour, 200g golden caster sugar, 4 tbsp cocoa
powder, 1.5 tbsp baking power, 1 tsp bicarbonate of soda];
6
7 % Step 1
8 [T] = set_oven(oven);
9
10 % Step 2
11 [ready_tin] = prepare_tin(tin,marge,parchment);
12
1
Functions are “self contained” modules of code that accomplish a specific task. Functions usually take
in data, process it, and return a result. Once a function is written, it can be used over and over and over
again.
2
NB. Anything following a % sign is a comment only and is not used when the program is ran.
13 % Step 3-5
14 [mix_bowl] = make_cake(wet_ingredients,dry_ingredients)
15
16 % Step 6-7
17 [cake] = bake_cake(mix_bowl,ready_tin,T);
18
19 % Eat
20 Eat
We can now work through the steps in order and pick out the key information to fill in the
functions we need,
1 Preheat the oven to 190 ◦ C (170 ◦ C for fan).
The key things here are to set the temperature based on the type of oven; therefore, we
check the oven type an use this test to allow us to define the correct temperature,
1 function [T] = set_oven(oven)
2
3 % Check the oven type
4 if oven=="Fan"
5 % If the oven is a fan oven
6 T=170;
7 else
8 % If the oven is not a fan oven
9 T=190;
10 end
11 end
2 Grease the base and sides of a 25 cm diameter circular tin with margarine, then line
the base with baking parchment.
In this step, we are adding items together; the tin, the margarine, and the baking parch-
ment. The key thing we want here is variables that make sense so we remember what they
are,
1 function [ready_tin] = prepare_tin(tin,marge,parchment)
2
3 grease_tin = tin + marge;
4 ready_tin = grease_tin + parchment;
5
6 end
3 Add the wet ingredients together in a bowl and mix together well.
4 In a second bowl, add the dry ingredients together and mix.
5 Add the mixed dry ingredients to the wet ingredients and mix until smooth.
The next 3 steps have been grouped together due to their similarity. We need to set up the
size of the bowls, then add the ingredients. We also need to notice that the first bowl for
the wet ingredients needs to be large enough for all the ingredients,
1 function [mix_bowl] = make_cake(wet_ingredients,dry_ingredients)
2
3 % Find the number of wet and dry ingredients
4 nWet = size(wet_ingredients);
5 nDry = size(dry_ingredients);
6
7 % Get the bowls and set to the right size with zeros, i.e. empty bowls
8 % wet bowl, but large enough for all
9 mix_bowl = zeros(nWet+nDry);
For steps 3 and 4 we can actually use the same function, and as we have to do the same
activity several times we can use a for loop,
1 function [bowl] = mix_ingredients(ingredients,bowl)
2
3 % Add all the ingredients to the bowl
4 for i in 1:size(ingredients)
5 bowl(i) = ingredients(i);
6 end
7
8 Mix
9 end
6 Add the cake batter to the prepared tin and bake for 30 mins or until a skewer in-
serted into the middle of the cakes comes out cleanly.
7 Leave to cool in the tin for 10 mins then turn out onto a wire rack to cool completely.
Now we place the cake mix into the prepared tin and bake. The important information to
take here is that we need to wait 30 minutes, unless our test for the cake being baked is
already true,
1 function [cake] = bake_cake(mix_bowl,ready_tin,T);
2
3 % Put the cake and the tin together
4 to_bake.cake_tin = ready_tin;
5 to_bake.batter = mix_bowl;
6
7 % Put the cake in the oven at the correct temperature
8 Oven.T = T;
9 Oven.bake = to_bake;
10
11 % Set the initial time, i.e. 0 minutes, for starting to bake
12 t = 0;
13
14 % Check if the time is over 30 minutes
15 while t < 30
16 % Time increase by 1 minute
17 t=t+1;
18
19 % Test cake readiness, skewer will return either "dirty" or "clean"
20 [skewer] = test_cake;
21
22 % If the skewer is clean the cake is ready, so exit the loop
23 if skewer == "clean"
24 break
25 end
26 end
27
28 % Take the cake out of the oven
29 done_cake = Oven.bake;
30
63
64
Modularization
• Programs should use good modular design - the program should be broken into
suitable functions and sections
• Each function should perform a single well-defined operation and be designed
thinking of reuse of that function e.g. handling of variables into and out of the
function
• If functions are associated with m-files it must be obvious what m-file is what func-
tion
Design
• Program should be written in a clear and logical manner
• The most appropriate algorithms should be used e.g. differentials of functions ex-
plicitly used, constants taken out of loops
• Avoid slow operations e.g. square roots, high powers
• Pre-allocation of arrays is faster than building them within loops
• Do not use global variables - pass variables into functions if needed. Global con-
stants are acceptable if needed
Readability
• Use indentation appropriately and consistently to delineate code blocks
• Use meaningful identifier names, e.g., function and variables names
• Make sure variable names are consistent
• Use appropriate white space between code blocks
• Use white space within lines of code. Operators such as = and + and << should
have a space on either side
• Lines of code should be a reasonable length - Break long lines into shorter lines
when possible
• Use a non-proportional font, so indentation is meaningful (each character takes up
same amount of space).
Comments
• The contents of each file are listed if multiple functions are in each file
• The contents of each function are described at the beginning of the function - what
is the purpose of each function and any assumptions made
• Comments are used to explain key parts of the code
Function
• Programs must work correctly
• It needs to be clear where input parameters are given to the program and what these
input parameters are
• The code produces sensible outputs, which can be easily used for analysis