Wednesday, September 14, 2016

F2PY Example: Radial Distribution Function

In a recent post, I discussed the use of F2PY for speeding up parts of python code.

The radial distribution function (RDF) or the pair correlation function \(g(r)\) is a statistical mechanical property which quantifies density variation as a function of distance from a reference particle as a function in a system of particles.

It is a fairly fundamental property that can easily be computed from molecular simulations. The Fourier transform of the RDF is experimentally accesible via light scattering.

For the purposes of this demonstration, we are given a bunch of points in a periodic simulation box. Our goal is to find the pair correlation function \(g(r)\), and compare the relative advantage of using f2py to wrap a Fortran 90 subroutine and a vectorized python implementation of the same algorithm.

The details are in this shared iPython notebook on GitHub. You should be able to both (i) see a static html rendition on the linked site, and (ii) download a jupyter notebook for further experimentation.

The bottom line here was that  eventually both python and Fortran gravitated to a \(N^2\) scaling, where \(N\) is the number of particles. Since the Python code exploits vectorization, its performance is not as bad. Indeed, its relative performance in this case seems to improve as the number of particles increase from 500x worse to only 10x worse as the number of particles was increased from 10 to 10000.


No comments: