Third year CMP compulsory experiment/Introduction to Monte Carlo simulation
This is the third section of the third year CMP experiment. You can return to the previous page, Calculating the energy and magnetisation, or jump ahead to the next section, Accelerating the code.
Average Energy and Magnetisation
Consider again the expressions for the average energy and magnetisation that we gave in the introduction.
Imagine we want to evaluate these at a particular temperature, in a system of 100 spins.
TASK 3a: How many configurations are available to a system with 100 spins? To evaluate these expressions, we have to calculate the energy and magnetisation for each of these configurations, then perform the sum. Let's be very, very, generous, and say that we can analyse
configurations per second with our computer. How long will it take to evaluate a single value of
?
Clearly, we need to try a cleverer approach.
For the overwhelming majority of the possible configurations, the Boltzmann factor,
, will be very small, and that state will contribute very little to the average value. We can save an enormous amount of time, and make this problem tractable, if we only consider the states whose Boltzmann factor is not so vanishingly small. This technique is called importance sampling — instead of sampling every point in phase space, we sample only those that the system is likely to occupy.
Importance Sampling
This is easily stated, of course, but how can we know which states contribute a lot to the average without actually calculating the energy? So far, we have been imagining generating random states of the system; that is to say, we have been choosing
from a uniform distribution in which no arrangement of spins is any more likely to be chosen than any other. If we instead had a method which generated states randomly from the probability distribution
, then our problem would be solved. The method which allows us to do this was developed in the 1950s, and is called the Metropolis Monte Carlo (MMC) method, or more often simply "Monte Carlo simulation". This was one of the major breakthroughs of the early days of computational science, and in the year 2000, the IEEE named it first in its list of the "Top 10 Algorithms of the 20th Century".
The algorithm is as follows:
- Start from a given configuration of spins,
, with energy
. - Choose a single spin at random, and "flip" it, to generate a new configuration

- Calculate the energy of this new configuration,

- Calculate the energy difference between the states,
- If the
(the spin flipping decreased the energy), then we accept the new configuration.
- We set
, and
, and then go to step 5
- We set
- If
, the spin flipping increased the energy. By considering the probability of observing the starting and final states,
and
, it can be shown that the probability for the transition between the two to occur is
. To ensure that we only accept this kind of spin flip with the correct probability, we use the following procedure:
- Choose a random number,
, in the interval 
- If
, we accept the new configuration.
- We set
, and
, and then go to step 5
- We set
- If
, we reject the new configuration.
-
and
are left unchanged. Go to step 5
-
- Choose a random number,
- If the
- Update the running averages of the energy and magnetisation.
- Monte Carlo "cycle" complete, return to step 2.
Step 4 is the key to the method. By accepting moves which increase the energy only with a certain probability, we ensure that the sequence of states that we generate is correctly distributed. A transition which carries a very large energy penalty,
is extremely unlikely to be selected. The use of random numbers in this step is the reason that the method acquired the name "Monte Carlo", after the casinos located there!
If you are interested in the mathematical details of why this procedure generates a sequence of states distributed in the correct way, consult the Monte Carlo chapter of "Understanding Molecular Simulation", by Frenkel and Smit, but a discussion of this is not required for the experiment. You can find this as an ebook in the library here. Chapter 7 of 'Statistical Mechanics: Theory and Molecular Simulation' by Tuckerman also gives a good description.
Modifying IsingLattice.py
Our IsingLattice object contains a stub function, montecarlocycle(T), which takes a single argument — the temperature at which we want to do the cycle. There are also attributes to measure the average energy and magnetisation: self.E, self.E2, self.M, self.M2, and self.n_cycles. You should use these values to keep a running sum of all of the energies, squared energies, magnetisations, and squared magnetisations that your system has sampled, as well as the number of cycles that have been performed. Finally, there is also a function statistics(), that takes no arguments.
TASK 3b: Implement a single cycle of the above algorithm in the montecarlocycle(T) function. This function should return the energy of your lattice and the magnetisation at the end of the cycle. You may assume that the energy returned by your energy() function is in units of
! Complete the statistics() function. This should return the following quantities whenever it is called:
, and the number of Monte Carlo steps that have elapsed.
You have been provided with a script, ILanim.py, which will use your IsingLattice object to run a Monte Carlo simulation and display the output on the screen in real time. By default, this simulation will run an
lattice at
, which is below the critical temperature. You will see a representation of the lattice, and graphs of the energy and magnetisation per spin. When you close the window, the script will use your statistics() function to print the average energy and magnetisation from the simulation.
TASK 3c: If
, do you expect a spontaneous magnetisation (i.e. do you expect
)? When the state of the simulation appears to stop changing (when you have reached an equilibrium state), use the controls to export the output to PNG and attach this to your report. You should also include the output from your statistics() function.
This is the third section of the third year CMP experiment. You can return to the previous page, Calculating the energy and magnetisation, or jump ahead to the next section, Accelerating the code.