Third year CMP compulsory experiment/Accelerating the code
This is the fourth section of the third year CMP experiment. You can return to the previous page, Introduction to Monte Carlo simulation, or jump ahead to the next section, The effect of temperature.
So far, your energy() and magnetisation() functions probably use Python loops to go over every spin in your system (and a double loop for the energy). One of the drawbacks of Python's flexibility is a performance penalty — though developing code in Python tends to be faster than in C++, the resulting program is much, much slower.
Luckily, the NumPy module provides an interface to a number of functions written to give high performance. In this section, we are going to use these functions to improve the performance of our simulation.
TASK: Use the script ILtimetrial.py to record how long your current version of IsingLattice.py takes to perform 2000 Monte Carlo steps. This will vary, depending on what else the computer happens to be doing, so perform repeats and report the error in your average!
TASK: Look at the documentation for the NumPy sum function. You should be able to modify your magnetisation() function so that it uses this to evaluate M. The energy is a little trickier. Familiarise yourself with the NumPy roll and multiply functions, and use these to replace your energy double loop (you will need to call roll and multiply twice!).
TASK: Use the script ILtimetrial.py to record how long your new version of IsingLattice.py takes to perform 2000 Monte Carlo steps. This will vary, depending on what else the computer happens to be doing, so perform repeats and report the error in your average!
This is the fourth section of the third year CMP experiment. You can return to the previous page, Introduction to Monte Carlo simulation, or jump ahead to the next section, The effect of temperature.