Third year CMP compulsory experiment/Calculating the energy and magnetisation
This is the second section of the third year CMP experiment. You can return to the previous page, Introduction to the Ising model, or jump ahead to the next section, Introduction to Monte Carlo simulation.
Contents
Getting the files for the experiment
Various scripts have been prepared to assist you with this experiment, and you can obtain them by downloading the files from the Box here. If this link is broken you can alternatively try an older(slightly broken) version here.
We recommend that you use the IPython Qt or the regular ipython console (which you can access via spyder) to do your simulation work. This should be available on all of the college computers, where you can access it through Start -> All Programs -> Anaconda (64-bit) -> IPython QTConsole. On your own Mac or Linux computer you can simply type 'ipython' into the terminal. You are very welcome to use the IPython Notebook, that you were introduced to last year, to perform some of the analysis tasks later in the experiment.
NOTE: QTConsole does not work for running the ILanim.py script you run latter in the lab. The best solution is to just run the script from the command line using python ILanim.py (if you're in the directory where files are stored). On a Mac/Linux computer you run this from Terminal or on windows you can use the app 'Conda Prompt'
When using the QTConsole, you should start the session by running the commands:
%load_ext autoreload %autoreload 2
This will ensure that when you try executing your code, you always run the most recent version (please note this isn't fool proof and you may need to restart the console)!
For this section of the experiment, you will need the files IsingLattice.py and ILcheck.py. Extract them from the zip file to a convenient location in your H drive, then in the IPython QTConsole use cd H:\... to move to the correct directory.
Modifying the files
The file IsingLattice.py contains a Python class called IsingLattice. Open this file in your favourite text editor. This class will be the basis for our model of the system, and is restricted to modelling the two dimensional case. It contains a number of stub functions that you will complete as you work through the experiment.
A note about Python Classes
The file IsingLattice.py makes use of a Python feature that you may not have met before, by defining a class. You should be familiar with the idea that all Python objects have an associated type -- you have already encountered objects such as integers, string, lists, and NumPy arrays. Classes allow us to define new types of object. In this case, we define a type called IsingLattice:
class IsingLattice:
Each class is allowed to have certain attributes and functions (known as methods) associated with it. Lines 5-10 of IsingLattice.py create 5 variables that every object of type IsingLattice has:
E = 0.0
E2 = 0.0
M = 0.0
M2 = 0.0
n_cycles = 0
There are also functions defined, on lines 12, 17, 22, etc. We can see, for example, that each IsingLattice object has the functions energy(), magnetisation(), montecarlostep(T), and statistics() (the function __init__ will be explained shortly). Each of these functions takes a special first argument called self, which is a variable pointing to the current copy of IsingLattice. To use the new IsingLattice object that we have defined, we use code like:
import IsingLattice
il = IsingLattice(5, 5) #create an IsingLattice object with 5 row and 5 columns
energy = il.energy() #call the energy() function of our IsingLattice object
When writing code "within" the class definition, all variables and functions belonging to the class must be accessed using the "self." notation. You can see an example of this in the montecarlostep() function:
energy = self.energy()
This line creates a variable called energy which contains whatever value was returned by the function energy() which is part of the IsingLattice class. If you just want to define a local variable instead, like an iterator in a loop, the "self." notation is not required.
def magnetisation(self):
#we have to use self.lattice, because the lattice variable is part of the object, not local to this function
for i in self.lattice:
...
#we use i, not self.i
#we don't need to access i outside this function
Have a look at the other files that you have been given to see how this IsingLattice object is used.
The Constructor
The only function currently filled in is __init__. This is a special function called a constructor. Whenever a new IsingLattice object is created, using code likeil = IsingLattice(5,5), this function is called automatically. It takes two arguments (excluding "self"), which are simply the number of rows and columns that our lattice will have. The attribute self.lattice is then created, which is a NumPy array with the requested number of rows and columns. The function np.random.choiceis used to create a random collection of spins for our initial configuration. You can see the documentation for this function here.
TASK 2a: complete the functions energy() and magnetisation(), which should return the energy of the lattice and the total magnetisation, respectively. In the energy() function you may assume that
at all times (in fact, we are working in reduced units in which
, but there will be more information about this in later sections). Do not worry about the efficiency of the code at the moment — we will address the speed in a later part of the experiment.
Testing the files
When you have completed the energy() and magnetisation() functions, we need to test that they work correctly. You are welcome to do this yourself — from the IPython Qt console (ensuring that you are in the correct directory), you can simply typeimport IsingLatticethen create objects of your class with the command
il = IsingLattice.IsingLattice(n_rows, n_columns)
We have also provided you with a script, ILcheck.py, which will create three IsingLattice objects and check their energies and magnetisations, displaying these on a graph. One configuration corresponds to the energy minimum, one to the energy maximum, and one to an intermediate state.
TASK 2b: Run the ILcheck.py script from the IPython Qt console using the command%run ILcheck.pyThe displayed window has a series of control buttons in the bottom left, one of which will allow you to export the figure as a PNG image. Save an image of the ILcheck.py output, and include it in your report.
This is the second section of the third year CMP experiment. You can return to the previous page, Introduction to the Ising model, or jump ahead to the next section, Introduction to Monte Carlo simulation.