Calculating the Pair Correlation Function in Python
The pair correlation function, also known as the radial distribution function, is a way to characterize the distribution of particles on a two-dimensional plane or in a three-dimensional space. Please check out Eric Weeks’ web site for an introduction to pair correlation functions. He has written some routines in IDL to compute these functions. Using his foundation, I have written some simple routines in Python to compute 2D and 3D pair correlation functions.
Download
Both the 2D and 3D functions are included in the following Python file. If you have an older version, please upgrade.
Two-Dimensional Pair Correlation (Radial Distribution) Function
Validation
The pair correlation function g(r) is a measure of the structure that is present in a collection of particles. To validate the code, and understand how it works, I generated some populations of particles and plotted the pair correlation functions. The populations were generated by random sequential adsorption (RSA) in which each particle was added to the population in a random position. If the position caused the particle to overlap an existing one, the position was rejected and a new one was generated. The first plot shows a representative low-density population of particles. The red particles were used to compute g(r) because a circle of radius rMax centered at those particles lies entirely within the domain. Because the populations were randomly generated, the plotted g(r) is averaged over fifty different populations (dotted lines are +/- one standard deviation). Note that g(r) is never actually negative!
The pair correlation function is almost uniform for this distribution of particles. Because the particles are randomly located and not tightly packed, they have essentially no structure. g(r) is zero for r<1 because the particles have radius one and they are not allowed to overlap.
Now we will test the function on randomly distributed particles that are more densely packed.
The pair correlation function now shows some structure for distances close to the reference particle. Although the particles are still randomly distributed, the higher density forces them to take on some short-range structure as they try to fit in the square without overlapping.
Now we will examine the pair correlation function of a highly ordered array of particles. These particles are hexagonally packed, which is mathematically proven to be the highest-density arrangement for circles on a plane.
The pair correlation function now shows the large amount of order in the particles. If you draw a circle surrounded by hexagonally packed circles and do a little geometry, you can see that each “spike” corresponds to the center-center distance between the reference circle and another circle.
Three-Dimensional Pair Correlation (Radial Distribution) Function
Validation
The 3D pair correlation function was validated using a low-density and high-density distribution of particles within a cube. Because packing spheres in 3D space is much more involved than 2D circle packing, I did not test the routine on packed spheres. If you do, I’d like to hear about it.
Now we will look at spherical particles which are more densely packed.
Short-range order is evident in the pair correlation function.











