Statistical mechanics simulator

Author: Nathan Clisby, 2013 (homepage,

The statistical mechanics simulator provides a platform for the visualisation of various algorithms for studying statistical mechanical systems. For the moment the only simulation is of the pivot algorithm for self-avoiding walks, but more will be added in future. (There's also a simple implementation of the calculation of the area of a circle via Monte Carlo which you are welcome to play around with, but I won't discuss that more here.)

The code is written in Processing and released under the GPL.

Quick links: simulator, instructions, “How to”, download, pivot algorithm, future simulations, technical details, acknowledgments.

The statistical mechanics simulator should be visible immediately below the instructions for compatible browsers. If there is a blank space, please visit the “How to” section further below for information on how to either enable WebGL for your browser, or download the application and run it on your computer via the Java Virtual Machine.

Brief instructions: click on “Pivot algorithm” on the initial screen to take you to the options menu. Clicking on menu items will change the corresponding value, and the simulation is run by clicking “START”. Selecting the exit button in the top right corner will return you to the parent menu, or terminate the application if you are at the root menu screen. New: Holding down the up and down arrow keys during the animation allows you to zoom in and out of the walk (this takes some getting used to, as there is some lag so that the change occurs smoothly). New: Pressing the left arrow once selects the next slowest animation speed, and pressing the right arrow once selects the next fastest. New: Pressing “i” will toggle the display of auxiliary information. From the top this displays t, the number of pivot attempts (Markov chain time), f, the fraction of pivot attempts which are successful, R, the square root of the mean square end-to-end distance, and z, the zoom factor relative to the default value.

How to run the simulator

Browser compatibility: needs WebGL enabled, and thus works with recent versions of Chrome, Firefox and Safari, but not Internet Explorer (Internet Explorer will have support from version 11 which will be released soon). For Safari, I had to enable WebGL by clicking on Safari → Preferences → Advanced → “Show Develop in menu bar”, and then selecting Develop → “Enable WebGL”. If your browser does not work, and you are sufficiently keen to view this simulation, I recommend installing Chrome.

Download: The simulation runs significantly faster and better on your computer as an application. The simulator is written in Processing, which is based on Java and runs in the Java Virtual Machine or JVM. Download files: (1 MB, to run in the Processing interpreter), Linux 32 bit (6.4 MB), Linux 64 bit (6.4 MB), MacOS X (38 MB, includes Java), Windows 32 bit (6.4 MB), and Windows 64 bit (6.4 MB).

If you have trouble running the application, you may be interested in seeing this demo video to get an idea of what the simulation looks like: simulatorDemo.mp4 (25 MB). This demo is not intended to teach you how to use the simulator, as it is not possible to follow what options are selected.

The pivot algorithm

The pivot algorithm is the most efficient known method for sampling self-avoiding walks of fixed length, or equivalently sampling polymer configurations for a polymer in a good solvent.

The simulator shows how the pivot algorithm works when applied to self-avoiding walks on the square and simple cubic lattices. All possible lattice symmetries are used; only successful pivot moves are shown. The colours used in the walk have no particular meaning, their primary purpose is to allow one's eye to follow the motion of the walk in three dimensions.

Simulation options:

  • Lattice: square or simple cubic.
  • Animation speed: very slow, slow, medium, fast, very fast.
  • Length: 10, 50, 100, 500, 1000, 5000, 10000, 50000, 100000.
  • Initialisation.
    • Equilibrium option reads from a file a given seed self-avoiding walk which was sampled uniformly at random using the SAW-tree implementation of the pivot algorithm.
    • Straight rod is the most commonly used initial configuration, but it is not very useful if you wish to see a typical self-avoiding walks of 10000 steps. Note how the walk diminishes in size as it evolves from typical dimension O(N) to O(Nν).
    • Random walk option corresponds to seeding the algorithm with a simple random walk with no immediate reversals. The pivot algorithm as implemented here then evolves by only allowing updates when the new walk generated by a pivot move has no more self-intersections than the current walk. I am not aware of this seed state being used in the literature, but it could be regarded as a variant of the Domb-Joyce / weakly avoiding walk model, where we start at infinitely high temperature but suddenly cool the system down to the self-avoiding walk temperature, T=0. Note how the walk increases in size as it evolves from typical dimension O(N1/2) to O(Nν).

Here are links to some important papers on the topic. This list is not exhaustive by any means!

  • Lal (1969) invented the pivot algorithm.
  • Madras and Sokal (1988) studied the pivot algorithm in depth, and showed for the first time how powerful it is. Their implementation of the pivot algorithm utilised a hash table and for an N-step walk takes CPU time O(N) per successful pivot. They applied the pivot algorithm to the calculation of the critical exponent ν for two- and three-dimensional self-avoiding walks. This is the most important paper on the topic, and also serves as an excellent introduction.
  • Madras, Orlitsky, and Shepp (1990) and Janse van Rensburg, Whittington, and Madras (1990) showed that a variant of the pivot algorithm could be used to sample self-avoiding walks with fixed endpoints, and in particular self-avoiding polygons.
  • Li, Madras, and Sokal (1995) accurately calculated ν for two- and three-dimensional self-avoiding walks as well as various amplitude ratios.
  • Caracciolo, Pelissetto, and Sokal (1992) and Caracciolo, Causo, and Pelissetto (1998) developed the Join-and-Cut algorithm which utilises pivot moves, and applied it to the estimation of the critical exponent γ for self-avoiding walks.
  • Kennedy (2002) showed that the pivot algorithm can be implemented in CPU time o(N) for N-step walks.
  • Clisby (2010a) and Clisby (2010b) introduced the SAW-tree implementation which takes CPU time O(log N) per pivot attempt and used it to estimate the critical exponent ν for self-avoiding walks.
  • Clisby (2013) accurately calculated the connective constant μ for self-avoiding walks using the pivot algorithm.

This section on the pivot algorithm will be incrementally expanded in future to provide a guide to the literature which I hope will prove useful to newcomers to the field.

Future simulations

This is a list of simulations which I'm interested in adding in the future. If you have any suggestions please let me know.

Please note that my time is quite limited and so there may not be much progress in the immediate future (as of June 2013). Each of the proposed applications will take in the range of 1 day - 1 week of work to pull something together which will do a decent job.

Walks and polymers:

  • Brute force generation of self-avoiding walks.
  • Animation of the SAW-tree intersection testing algorithm, showing how the intersection tests are performed via bounding boxes for successively shorter pieces of the walk.
  • New variant of the finite lattice method (link) as applied to the enumeration of self-avoiding polygons on the square lattice.
  • BFACF algorithm for sampling self-avoiding polygons.
  • Animation of Hamiltonian path generation algorithm. For a pure javascript version see this).
  • Brute force generation of endless self-avoiding walks (see this).

Statistical mechanics:

  • Wolff algorithm for the Ising model on square and simple cubic lattices. (I think this would look beautiful on a 3d lattice if done well.)
  • Worm algorithm.
  • Sweeny algorithm.
  • Hard discs and spheres - various Monte Carlo algorithms including standard Metropolis and the event chain algorithm.

Technical details

The simulator is written in Processing which is a beautiful language for this purpose. For scientific visualisation it is a remarkably powerful tool.

The statistical mechanics simulator is written in such a manner that it is fully compatible with Processing.js, which is a javascript interpreter of Processing. This allows for Processing animations to be viewed directly in html5 compliant browsers, with the only (slight) downside that one can no longer leverage the full power of Java. This is because although Processing.js can interpret the full Processing language, it cannot interpret Java, and so Java libraries must be re-written if they are to be used. For the statistical mechanics simulator this means that I have developed my own rudimentary menu system, rather than relying on the much better libraries which others have created. It would be possible to have a “pretty” application version and “ugly” web version, but I do not have time to maintain two separate versions. Another minor downside from maintaining compatibility is that all files (including data files) need to be kept in the source directory.

The programming style in this simulator is not the best, as it has been rapidly developed and should be regarded as a prototype. If you have sufficient time and interest to re-factor the code please go ahead!

My goal is to redesign the architecture so that individual “apps” or “sims” can be written completely independently of each other. The apps would just have to supply an appropriate menu item, and then they could be combined straightforwardly by insert function to insert an app into a given menu.

If you are interested in contributing to this project either by improving existing code or by adding additional simulations then I strongly encourage you to so. I would really like to hear from you if you're considering this.


Thanks to Mireille Bousquet-Mélou for her initial inquiry as to whether I knew of any nice pivot algorithm videos, which led to me developing the simulator after discovering what a great tool Processing is.

Thanks to the Australian Research Council for funding my work through the ARC Centre of Excellence for Mathematics and Statistics of Complex Systems (MASCOS), and more recently via a future fellowship.

Thanks to the developers of Processing and Processing.js for creating such marvellous tools, and then making them freely available.