<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://chemwiki.ch.ic.ac.uk/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Hc1312</id>
	<title>ChemWiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://chemwiki.ch.ic.ac.uk/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Hc1312"/>
	<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/wiki/Special:Contributions/Hc1312"/>
	<updated>2026-05-16T10:40:37Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481463</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481463"/>
		<updated>2015-02-01T21:25:03Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Locating the Curie temperature */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
In this section, two functions energy() and magnetisation() in file IsingLattice.py were  completed which can be used to calculate energy and magnetisation for a given lattice.&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
In this section, a new function montecarlocycle(T) were completed using the Monte Carlo algorithm to perform a single step simulation. By using the statistic() function average properties such as energy, magnetisation, energy&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; and magnetisation&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; are calculated.&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
Numpy functions were used to accelerate the code in the energy() and magnetisation() functions in order to reduce computational time.&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
In this section, energy and magnetisation were monitored at constant temperature which allows us to calculate the number of steps required to reach equilibrium. &lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
In this section, energy and magnetisation were monitored over a range of temperature in order to study the effect of system size. Some steps were ignored in the calculation, in order to improve the accuracy of the average properties.&lt;br /&gt;
&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
Heat capacity was calculated from energy for different lattices in order to study the effect of size.&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
In this section, the heat capacity values calculated from the Python data were compared to the values obtained from the C++ datat. Then, the C++ heat capacity against temperature curves were fitted using polynomial function in all range and selected range. By doing so, the temperature (Tmax) is obtained at which the heat capacity is at maximum. Tmax was then plotted against 1/L in order to extrapolate the Tmax for infinite lattice.&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481462</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481462"/>
		<updated>2015-02-01T21:24:43Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Locating the Curie temperature */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
In this section, two functions energy() and magnetisation() in file IsingLattice.py were  completed which can be used to calculate energy and magnetisation for a given lattice.&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
In this section, a new function montecarlocycle(T) were completed using the Monte Carlo algorithm to perform a single step simulation. By using the statistic() function average properties such as energy, magnetisation, energy&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; and magnetisation&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; are calculated.&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
Numpy functions were used to accelerate the code in the energy() and magnetisation() functions in order to reduce computational time.&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
In this section, energy and magnetisation were monitored at constant temperature which allows us to calculate the number of steps required to reach equilibrium. &lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
In this section, energy and magnetisation were monitored over a range of temperature in order to study the effect of system size. Some steps were ignored in the calculation, in order to improve the accuracy of the average properties.&lt;br /&gt;
&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
Heat capacity was calculated from energy for different lattices in order to study the effect of size.&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
In this section, the heat capacity values calculated from the Python data were compared to the values obtained from the C++ datat. Then, the C++ heat capacity against temperature curves were fitted using polynomial function in all range and selected range. By doing so, the temperature (Tmax) is obtained at which the heat capacity is at maximum. Emax was then plotted against 1/L in order to extrapolate the Tmax for infinite lattice.&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481461</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481461"/>
		<updated>2015-02-01T21:24:03Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Locating the Curie temperature */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
In this section, two functions energy() and magnetisation() in file IsingLattice.py were  completed which can be used to calculate energy and magnetisation for a given lattice.&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
In this section, a new function montecarlocycle(T) were completed using the Monte Carlo algorithm to perform a single step simulation. By using the statistic() function average properties such as energy, magnetisation, energy&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; and magnetisation&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; are calculated.&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
Numpy functions were used to accelerate the code in the energy() and magnetisation() functions in order to reduce computational time.&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
In this section, energy and magnetisation were monitored at constant temperature which allows us to calculate the number of steps required to reach equilibrium. &lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
In this section, energy and magnetisation were monitored over a range of temperature in order to study the effect of system size. Some steps were ignored in the calculation, in order to improve the accuracy of the average properties.&lt;br /&gt;
&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
Heat capacity was calculated from energy for different lattices in order to study the effect of size.&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
In this section, the heat capacity values calculated from the Python data were compared to the values obtained from the C++ datat. Then, the C++ heat capacity against temperature curves were fitted using polynomial function in all range and selected range. By doing so, the temperature (Tmax) is obtained at which the heat capacity is at maximum. Emax was then plotted against 1/L in order to extrapolate the Emax for infinite lattice.&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481458</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481458"/>
		<updated>2015-02-01T21:22:02Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Locating the Curie temperature */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
In this section, two functions energy() and magnetisation() in file IsingLattice.py were  completed which can be used to calculate energy and magnetisation for a given lattice.&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
In this section, a new function montecarlocycle(T) were completed using the Monte Carlo algorithm to perform a single step simulation. By using the statistic() function average properties such as energy, magnetisation, energy&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; and magnetisation&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; are calculated.&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
Numpy functions were used to accelerate the code in the energy() and magnetisation() functions in order to reduce computational time.&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
In this section, energy and magnetisation were monitored at constant temperature which allows us to calculate the number of steps required to reach equilibrium. &lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
In this section, energy and magnetisation were monitored over a range of temperature in order to study the effect of system size. Some steps were ignored in the calculation, in order to improve the accuracy of the average properties.&lt;br /&gt;
&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
Heat capacity was calculated from energy for different lattices in order to study the effect of size.&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
In this section, the heat capacity values calculated from the Python data were compared to the values obtained from the C++ data, which allows us to determined the more accurate result. Then, the result were fitted using polynomial function in all range and selected range. By doing so, the temperature (Tmax) is obtained at which the heat capacity is at maximum. Emax was then plotted against 1/L in order to extrapolate the Emax for infinite lattice.&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481457</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481457"/>
		<updated>2015-02-01T21:17:00Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Determining the heat capacity */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
In this section, two functions energy() and magnetisation() in file IsingLattice.py were  completed which can be used to calculate energy and magnetisation for a given lattice.&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
In this section, a new function montecarlocycle(T) were completed using the Monte Carlo algorithm to perform a single step simulation. By using the statistic() function average properties such as energy, magnetisation, energy&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; and magnetisation&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; are calculated.&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
Numpy functions were used to accelerate the code in the energy() and magnetisation() functions in order to reduce computational time.&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
In this section, energy and magnetisation were monitored at constant temperature which allows us to calculate the number of steps required to reach equilibrium. &lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
In this section, energy and magnetisation were monitored over a range of temperature in order to study the effect of system size. Some steps were ignored in the calculation, in order to improve the accuracy of the average properties.&lt;br /&gt;
&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
Heat capacity was calculated from energy for different lattices in order to study the effect of size.&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481286</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481286"/>
		<updated>2015-01-30T11:58:42Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* The effect of system size */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
In this section, two functions energy() and magnetisation() in file IsingLattice.py were  completed which can be used to calculate energy and magnetisation for a given lattice.&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
In this section, a new function montecarlocycle(T) were completed using the Monte Carlo algorithm to perform a single step simulation. By using the statistic() function average properties such as energy, magnetisation, energy&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; and magnetisation&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; are calculated.&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
Numpy functions were used to accelerate the code in the energy() and magnetisation() functions in order to reduce computational time.&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
In this section, energy and magnetisation were monitored at constant temperature which allows us to calculate the number of steps required to reach equilibrium. &lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
In this section, energy and magnetisation were monitored over a range of temperature in order to study the effect of system size. Some steps were ignored in the calculation, in order to improve the accuracy of the average properties.&lt;br /&gt;
&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481282</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481282"/>
		<updated>2015-01-30T11:56:15Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* The effect of temperature */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
In this section, two functions energy() and magnetisation() in file IsingLattice.py were  completed which can be used to calculate energy and magnetisation for a given lattice.&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
In this section, a new function montecarlocycle(T) were completed using the Monte Carlo algorithm to perform a single step simulation. By using the statistic() function average properties such as energy, magnetisation, energy&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; and magnetisation&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; are calculated.&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
Numpy functions were used to accelerate the code in the energy() and magnetisation() functions in order to reduce computational time.&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
In this section, energy and magnetisation were monitored at constant temperature which allows us to calculate the number of steps required to reach equilibrium. &lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481279</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481279"/>
		<updated>2015-01-30T11:53:59Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* The effect of temperature */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
In this section, two functions energy() and magnetisation() in file IsingLattice.py were  completed which can be used to calculate energy and magnetisation for a given lattice.&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
In this section, a new function montecarlocycle(T) were completed using the Monte Carlo algorithm to perform a single step simulation. By using the statistic() function average properties such as energy, magnetisation, energy&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; and magnetisation&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; are calculated.&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
Numpy functions were used to accelerate the code in the energy() and magnetisation() functions in order to reduce computational time.&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
In this section, energy and magnetisation were monitored at constant temperature in order to study the affect of temperature and lattice size.&lt;br /&gt;
&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481275</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481275"/>
		<updated>2015-01-30T11:51:21Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Accelerating the code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
In this section, two functions energy() and magnetisation() in file IsingLattice.py were  completed which can be used to calculate energy and magnetisation for a given lattice.&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
In this section, a new function montecarlocycle(T) were completed using the Monte Carlo algorithm to perform a single step simulation. By using the statistic() function average properties such as energy, magnetisation, energy&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; and magnetisation&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; are calculated.&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
Numpy functions were used to accelerate the code in the energy() and magnetisation() functions in order to reduce computational time.&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481274</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481274"/>
		<updated>2015-01-30T11:51:08Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Accelerating the code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
In this section, two functions energy() and magnetisation() in file IsingLattice.py were  completed which can be used to calculate energy and magnetisation for a given lattice.&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
In this section, a new function montecarlocycle(T) were completed using the Monte Carlo algorithm to perform a single step simulation. By using the statistic() function average properties such as energy, magnetisation, energy&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; and magnetisation&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; are calculated.&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
Bumpy functions were used to accelerate the code in the energy() and magnetisation() functions in order to reduce computational time.&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481269</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481269"/>
		<updated>2015-01-30T11:50:20Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Accelerating the code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
In this section, two functions energy() and magnetisation() in file IsingLattice.py were  completed which can be used to calculate energy and magnetisation for a given lattice.&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
In this section, a new function montecarlocycle(T) were completed using the Monte Carlo algorithm to perform a single step simulation. By using the statistic() function average properties such as energy, magnetisation, energy&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; and magnetisation&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; are calculated.&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
 NumPy functions were used to accelerate the code in the energy() and magnetisation() functions in order to reduce computational time.&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481262</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481262"/>
		<updated>2015-01-30T11:47:41Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Introduction to Monte Carlo simulation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
In this section, two functions energy() and magnetisation() in file IsingLattice.py were  completed which can be used to calculate energy and magnetisation for a given lattice.&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
In this section, a new function montecarlocycle(T) were completed using the Monte Carlo algorithm to perform a single step simulation. By using the statistic() function average properties such as energy, magnetisation, energy&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; and magnetisation&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt; are calculated.&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481261</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481261"/>
		<updated>2015-01-30T11:47:16Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Introduction to Monte Carlo simulation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
In this section, two functions energy() and magnetisation() in file IsingLattice.py were  completed which can be used to calculate energy and magnetisation for a given lattice.&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
In this section, a new function montecarlocycle(T) were completed using the Monte Carlo algorithm to perform a single step simulation. By using the statistic() function average properties such as energy, magnetisation, energy^2 and magnetisation are calculated.&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481258</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481258"/>
		<updated>2015-01-30T11:45:55Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Introduction to Monte Carlo simulation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
In this section, two functions energy() and magnetisation() in file IsingLattice.py were  completed which can be used to calculate energy and magnetisation for a given lattice.&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
In this section, a new function montecarlocycle(T) were completed using the Monte Carlo algorithm to perform a single step simulation.&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481240</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481240"/>
		<updated>2015-01-30T11:41:08Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Calculating the energy and magnetisation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
In this section, two functions energy() and magnetisation() in file IsingLattice.py were  completed which can be used to calculate energy and magnetisation for a given lattice.&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481208</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481208"/>
		<updated>2015-01-30T11:19:18Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Task */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
===TASK===&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481207</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481207"/>
		<updated>2015-01-30T11:19:04Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Calculating the energy and magnetisation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
===Task===&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481205</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481205"/>
		<updated>2015-01-30T11:18:37Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in J/k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, where J is a constant measure how strong the interaction is between the spin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481201</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481201"/>
		<updated>2015-01-30T11:13:51Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK4 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in Kelvin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124K. This is relatively close to the theoretical Curie temperature, 2.269K of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481199</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481199"/>
		<updated>2015-01-30T11:13:17Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively. However temperatures are measured in Kelvin.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124J. This is relatively close to the theoretical Curie temperature, 2.269J of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481190</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481190"/>
		<updated>2015-01-30T11:07:20Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;T, reduced Plank constant and k&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;, respectively.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124J. This is relatively close to the theoretical Curie temperature, 2.269J of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481188</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481188"/>
		<updated>2015-01-30T11:06:30Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Note: In the following context, energy, magnetisation and heat capacity are all reduced quantities and they are in the units of k&amp;lt;sub&amp;gt;b&amp;lt;/sub&amp;gt;T,  in the following context are  ,  ,   , respectively.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124J. This is relatively close to the theoretical Curie temperature, 2.269J of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481170</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481170"/>
		<updated>2015-01-30T10:48:16Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Note: The units of energy, magnetisation, heat capacity in the following context are  ,  ,   , respectively.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124J. This is relatively close to the theoretical Curie temperature, 2.269J of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481168</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=481168"/>
		<updated>2015-01-30T10:46:36Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK4 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124J. This is relatively close to the theoretical Curie temperature, 2.269J of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Tmax_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=File:Tmax_cen.png&amp;diff=481166</id>
		<title>File:Tmax cen.png</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=File:Tmax_cen.png&amp;diff=481166"/>
		<updated>2015-01-30T10:45:38Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480731</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480731"/>
		<updated>2015-01-29T17:55:03Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK4 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained T_{C,\infty}&amp;lt;/math&amp;gt;. T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124J. This is relatively close to the theoretical Curie temperature, 2.269J of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Reference==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480730</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480730"/>
		<updated>2015-01-29T17:54:38Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK4 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained T_{C,\infty}&amp;lt;/math&amp;gt;. T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124J. This is relatively close to the theoretical Curie temperature, 2.269J of a infinite 2 dimensional Ising lattice.&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot; /&amp;gt; There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;Curie_temperature&amp;quot;&amp;gt;H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480727</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480727"/>
		<updated>2015-01-29T17:52:33Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK4 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained T_{C,\infty}&amp;lt;/math&amp;gt;. T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124J. This is relatively close to the theoretical Curie temperature, 2.269J of a infinite 2 dimensional Ising lattice. There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
Secondly, as we can see from the graph, the data points are concentrated at the bottom left corner, this is because we are plotting Tmax against 1/L. In fact there is no way increase the number of data points towards the upper right corner as L can only be integers. However, we can slightly improve the  the accuracy of the linear fit by including the 3x3 and 5x5 lattice sizes data.&lt;br /&gt;
&lt;br /&gt;
Finally, the data points of 2x2 and 4x4 lattices are less accurate compare to the larger lattice as the possible energy levels are very limited.&lt;br /&gt;
 &lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480713</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480713"/>
		<updated>2015-01-29T17:48:08Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK4 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Tmax for each lattice size was obtained by extracting data from the polynomial fit to the peak region described earlier. Then by fitting the data to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, we obtained T_{C,\infty}&amp;lt;/math&amp;gt;. T_{C,\infty}&amp;lt;/math&amp;gt; = 2.28128933124J. This is relatively close to the theoretical Curie temperature, 2.269J of a infinite 2 dimensional Ising lattice. There are three major source of errors.&lt;br /&gt;
&lt;br /&gt;
Firstly, different temperature range will affect the polynomial fit and therefore give a different Tmax for each lattice. This can greatly affects the position of data point shown in the figure below and in turn the gradient and y intercept of the fitted line. &lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480703</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480703"/>
		<updated>2015-01-29T17:38:49Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK3 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
By limiting the temperature from 2 to 2.5, the polynomial only requires 8th order to make a good fit. Therefore restricting the temperature range is essential to capture the main feature of the peak.&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480699</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480699"/>
		<updated>2015-01-29T17:33:52Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
A 20th order polynomial was used to fit the graph of heat capacity per spin against temperature, which is  calculated from a 64x64 lattice using C++. The graph suggests that it is not a very good fit.&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480694</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480694"/>
		<updated>2015-01-29T17:29:04Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 64x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480693</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480693"/>
		<updated>2015-01-29T17:28:39Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480692</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480692"/>
		<updated>2015-01-29T17:28:16Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of heat capacity against temperature of different lattice sizes calculated using python and C++ are shown below. As we can see, the 32x32 result calculated by python differs the most from the C++ data, this is consistent with the argument made earlier about the accuracy of the 32x32 data.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480687</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480687"/>
		<updated>2015-01-29T17:24:21Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Determining the heat capacity */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205, calculated in the section above).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480685</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480685"/>
		<updated>2015-01-29T17:23:39Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, &amp;lt;math&amp;gt;T_{C, L}&amp;lt;/math&amp;gt; should decreases because &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480684</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480684"/>
		<updated>2015-01-29T17:23:03Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, T_{C, L} should decreases because T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the small effective runtime per spin (205).&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480681</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480681"/>
		<updated>2015-01-29T17:22:15Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|-&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, T_{C, L} should decreases because T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the relatively short runtime per spin (300,000) com&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480680</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480680"/>
		<updated>2015-01-29T17:21:59Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
!effective runtime per spin||2450||1200||703||508||205&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, T_{C, L} should decreases because T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the relatively short runtime per spin (300,000) com&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480677</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480677"/>
		<updated>2015-01-29T17:20:46Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Determining the heat capacity */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak maximum  should increase with lattice size as for infinite lattice, heat capacity diverges at the phase transition temperature, i.e. Curie Temperature. Moreover according to &amp;lt;math&amp;gt;T_{C, L} = \frac{A}{L} + T_{C,\infty}&amp;lt;/math&amp;gt;, as the lattice size increases, T_{C, L} should decreases because T_{C,\infty}&amp;lt;/math&amp;gt; is a constant. Therefore the position of the peak in the graph below should shift to the left with increasing lattice size. However the 32x32 lattice doesn&#039;t fit in the trend described above, this is probably due to the relatively short runtime per spin (300,000) com&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480667</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480667"/>
		<updated>2015-01-29T17:08:41Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* Determining the heat capacity */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak around the critical temperature should increase with lattice size, however.....&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480666</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480666"/>
		<updated>2015-01-29T17:08:04Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* The effect of system size */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from both of the graphs, there is no significant improvement on the quality of the graphs by increasing the lattice size from 16x16 to 32x32. However, the calculation for 32x32 lattice is more computationally expensive than 16x16, therefore 16x16 lattice size is sufficient to monitor the long range fluctuation as a result of a compromise between computational time and accuracy.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
&#039;&#039;&#039;&amp;lt;big&amp;gt;TASK&amp;lt;/big&amp;gt;: Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak around the critical temperature should increase with lattice size, however.....&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480662</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480662"/>
		<updated>2015-01-29T17:03:14Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature are plotted below. As we can see from the &lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
&#039;&#039;&#039;&amp;lt;big&amp;gt;TASK&amp;lt;/big&amp;gt;: Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak around the critical temperature should increase with lattice size, however.....&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480659</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480659"/>
		<updated>2015-01-29T17:01:00Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The plots of energy and magnetisation per spin against temperature for each lattice sizes are shown below. The plots of 8x8 lattice is shown in the section above.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature were plotted in separate graphs.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
&#039;&#039;&#039;&amp;lt;big&amp;gt;TASK&amp;lt;/big&amp;gt;: Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak around the critical temperature should increase with lattice size, however.....&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480658</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480658"/>
		<updated>2015-01-29T16:58:41Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature were plotted in separate graphs.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
&#039;&#039;&#039;&amp;lt;big&amp;gt;TASK&amp;lt;/big&amp;gt;: Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak around the critical temperature should increase with lattice size, however.....&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480657</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480657"/>
		<updated>2015-01-29T16:58:24Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The number of excluded steps and runtime for different lattice sizes are summarised in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4||8x8||16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
!number of steps ignored || 200 || 800||5000||20000||90000&lt;br /&gt;
|-&lt;br /&gt;
!runtime||10000||20000||50000||150000||300000&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature were plotted in separate graphs.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
&#039;&#039;&#039;&amp;lt;big&amp;gt;TASK&amp;lt;/big&amp;gt;: Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak around the critical temperature should increase with lattice size, however.....&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480653</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480653"/>
		<updated>2015-01-29T16:52:17Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* The effect of system size */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature were plotted in separate graphs.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
&#039;&#039;&#039;&amp;lt;big&amp;gt;TASK&amp;lt;/big&amp;gt;: Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak around the critical temperature should increase with lattice size, however.....&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480652</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480652"/>
		<updated>2015-01-29T16:51:22Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* TASK2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000. These numbers were deducted based on the analysis of the output from ILfinalframe.py, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
&#039;&#039;&#039;&amp;lt;big&amp;gt;TASK&amp;lt;/big&amp;gt;: Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature were plotted in separate graphs.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
&#039;&#039;&#039;&amp;lt;big&amp;gt;TASK&amp;lt;/big&amp;gt;: Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak around the critical temperature should increase with lattice size, however.....&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480651</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480651"/>
		<updated>2015-01-29T16:48:09Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* The effect of temperature */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000, these numbers were decided through analysis the figure which calculated using ILfinalframe.py before, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
&#039;&#039;&#039;&amp;lt;big&amp;gt;TASK&amp;lt;/big&amp;gt;: Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature were plotted in separate graphs.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
&#039;&#039;&#039;&amp;lt;big&amp;gt;TASK&amp;lt;/big&amp;gt;: Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak around the critical temperature should increase with lattice size, however.....&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
	<entry>
		<id>https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480649</id>
		<title>Rep:Mod:CENPYTHON</title>
		<link rel="alternate" type="text/html" href="https://chemwiki.ch.ic.ac.uk/index.php?title=Rep:Mod:CENPYTHON&amp;diff=480649"/>
		<updated>2015-01-29T16:47:18Z</updated>

		<summary type="html">&lt;p&gt;Hc1312: /* The effect of temperature */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Ising Model==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;Show that the lowest possible energy for the Ising model is &amp;lt;math&amp;gt;E\ =\ -DNJ&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; is the number of dimensions and &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the total number of spins. What is the multiplicity of this state? Calculate its entropy.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In one dimensional model, each spin is adjacent to 2 spins; and for the two dimensional model, each spin is adjacent to 4 spins; as for the three dimensional model each spin is adjacent to 6 spins. Therefore there are total number of &amp;lt;math&amp;gt;2D&amp;lt;/math&amp;gt; interactions for one spin in a system with dimension of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;.Since the lowest energy configuration is when the maganetic moment parallel to each other, the lowest interaction energy  =  &amp;lt;math&amp;gt; 2D(- \frac{1}{2} J s_i s_j)&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt; s_i s_j&amp;lt;/math&amp;gt; = 1. Therefore for a system with &amp;lt;math&amp;gt;N &amp;lt;/math&amp;gt;spins, the lowest possible energy =&amp;lt;math&amp;gt; N  2D(- \frac{1}{2} J  \times 1)&amp;lt;/math&amp;gt; =&amp;lt;math&amp;gt; -NDJ&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The two parallel spins can be either both upwards or both downwards, therefore the multiplicity of this state is equal to 2. The entropy S=K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;lnW = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln 2=9.57x10&amp;lt;sup&amp;gt;-24&amp;lt;/sup&amp;gt;JK&amp;lt;sup&amp;gt;-1&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Imagine that the system is in the lowest energy configuration. To move to a different state, one of the spins must spontaneously change direction (&amp;quot;flip&amp;quot;). What is the change in energy if this happens (&amp;lt;math&amp;gt;D=3,\ N=1000&amp;lt;/math&amp;gt;)? How much entropy does the system gain by doing so?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. When one of the spin change its direction, there will be 6 new distinct interactions with each s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = -1. Whereas the original interactions have s&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;s&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt; = 1. Therefore the change in energy = 6J - (-6J) = 12J.&lt;br /&gt;
&lt;br /&gt;
2. Since N = 1000, and the spin can either be from up to down or down to up. Therefore S = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000, and the ΔS= K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2000 - K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln2 = K&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;ln1000=9.53X10&amp;lt;sup&amp;gt;-23&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===TASK3===&lt;br /&gt;
&#039;&#039;&#039;Calculate the magnetisation of the 1D and 2D lattices in figure 1. What magnetisation would you expect to observe for an Ising lattice with &amp;lt;math&amp;gt;D = 3,\ N=1000&amp;lt;/math&amp;gt; at absolute zero?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1. For 1D, M = 1; for 2D, M = 1.&lt;br /&gt;
&lt;br /&gt;
2. M = ±1000&lt;br /&gt;
&lt;br /&gt;
==Calculating the energy and magnetisation==&lt;br /&gt;
After completing the two functions energy() and magnetisation() in file IsingLattice.py, file ILcheck.py allows us to check whether the two functions are correct or not. The output of ILcheck.py is shown below and indicates the two completed functions are correct.&lt;br /&gt;
&lt;br /&gt;
[[File:ILcheck_out_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Introduction to Monte Carlo simulation==&lt;br /&gt;
===TASK1=== &lt;br /&gt;
&#039;&#039;&#039;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&#039;s be very, very, generous, and say that we can analyse &amp;lt;math&amp;gt;1\times 10^9&amp;lt;/math&amp;gt; configurations per second with our computer. How long will it take to evaluate a single value of &amp;lt;math&amp;gt;\left\langle M\right\rangle_T&amp;lt;/math&amp;gt;?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are 1.27 x 10&amp;lt;sup&amp;gt;30&amp;lt;/sup&amp;gt; configuration and it will take 1.27 x 10&amp;lt;sup&amp;gt;21&amp;lt;/sup&amp;gt; seconds to complete the calculation, which is equivalent to 4 x 10&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt; years.&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;If &amp;lt;math&amp;gt;T &amp;lt; T_C&amp;lt;/math&amp;gt;, do you expect a spontaneous magnetisation (i.e. do you expect &amp;lt;math&amp;gt;\left\langle M\right\rangle \neq 0&amp;lt;/math&amp;gt;)? 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.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the temperature is below the Curie temperature, a spontaneous magnetisation is expected as all the spins are highly ordered. Image below shows how a system reach its energy minimum configuration and exhibit a spontaneous magnetization at this energy minimum configuration.&lt;br /&gt;
&lt;br /&gt;
[[File:ILanim_outNEW2_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The following quantities are printed after closing the window: &amp;lt;math&amp;gt;&amp;lt;E&amp;gt;, &amp;lt;E^2&amp;gt;, &amp;lt;M&amp;gt;, &amp;lt;M^2&amp;gt;&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Averaged quantities:&lt;br /&gt;
E =  -1.65123581848&lt;br /&gt;
E*E =  3.06467154579&lt;br /&gt;
M =  -0.854335494327&lt;br /&gt;
M*M =  0.801154781199 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accelerating the code==&lt;br /&gt;
===TASK===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;current&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.300468650242022s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.24796744654293s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.291973779735898s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.285238196579542s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.252695304894331s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.292496011567579s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.275512911653763s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.238346004247262s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.308245980248785s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.270112189283395s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 7.236859001933453s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed = 7.272440257s&lt;br /&gt;
&lt;br /&gt;
Standard error = 0.008457649&lt;br /&gt;
&lt;br /&gt;
===TASK2===&lt;br /&gt;
&#039;&#039;&#039;Use the script ILtimetrial.py to record how long your &#039;&#039;new&#039;&#039; 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!&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
After acceleration total 10 readings were recorded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13568766164613066s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13802919108715628s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15227367127184266s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13658750685222287s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1391059263605996s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15093039345663328s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15167296950728826s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.13515880260516155s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.14852969941969718s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.1342230360372696s&lt;br /&gt;
&lt;br /&gt;
%run ILtimetrial.py&lt;br /&gt;
Took 0.15064121140113684s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Average speed =   0.142985461s&lt;br /&gt;
&lt;br /&gt;
Standard error =  0.002420864&lt;br /&gt;
&lt;br /&gt;
After acceleration, the new code is 51 times faster than the old code.&lt;br /&gt;
&lt;br /&gt;
==The effect of temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;The script ILfinalframe.py runs for a given number of cycles at a given temperature, then plots a depiction of the &#039;&#039;final&#039;&#039; lattice state as well as graphs of the energy and magnetisation as a function of cycle number. This is much quicker than animating every frame! Experiment with different temperature and lattice sizes. How many cycles are typically needed for the system to go from its random starting position to the equilibrium state? Modify your statistics() and montecarlostep() functions so that the first N cycles of the simulation are ignored when calculating the averages. You should state in your report what period you chose to ignore, and include graphs from ILfinalframe.py to illustrate your motivation in choosing this figure.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Answer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The relaxation steps that the system used to reach equilibrium need to be excluded when calculating the average properties. The steps that need to be excluded for different lattice sizes are shown below. For a 8x8 system, only the first 5000 steps require to be ignored. Whereas for a bigger system such as 16x16, the first 20000 steps need to be excluded. As shown below, as the temperature increases the fluctuation about the equilibrium value increases. However, with a larger lattice, the effect of temperature is less pronounced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!||  Condition 1 || Condition 2&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 8x8 ||8x8&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature || 1 ||1.5&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 500 ||5000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step1.png|400px]]&lt;br /&gt;
|[[File:Figure_step2.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!|| Condition 3 || Condition 4&lt;br /&gt;
|-&lt;br /&gt;
|+&lt;br /&gt;
!Size || 16x16||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Temperature ||1||2&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|+&lt;br /&gt;
!Period to ignore|| 9000||20000&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Figure_step3.png|400px]]&lt;br /&gt;
|[[File:Figure_step4.png|400px]]&lt;br /&gt;
|&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&amp;lt;big&amp;gt;TASK&amp;lt;/big&amp;gt;: Use ILtemperaturerange.py to plot the average energy and magnetisation for each temperature, &#039;&#039;with error bars&#039;&#039;, for an &amp;lt;math&amp;gt;8\times 8&amp;lt;/math&amp;gt; lattice. Use your initution and results from the script ILfinalframe.py to estimate how many cycles each simulation should be. The temperature range 0.25 to 5.0 is sufficient. Use as many temperature points as you feel necessary to illustrate the trend, but do not use a temperature spacing larger than 0.5. T NumPy function savetxt() stores your array of output data on disk &amp;amp;mdash; you will need it later. Save the file as &#039;&#039;8x8.dat&#039;&#039; so that you know which lattice size it came from.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The temperature range is from 0.25 to 5.0 and the temperature spacing = 0.1. For a 8x8 lattice, the first 5000 steps were chosen to ignore and the runtime = 50000, these numbers were decided through analysis the figure which calculated using ILfinalframe.py before, and the average energy and magnetisation against temperature were plotted and shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:Temp_88_025to5_step01.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==The effect of system size==&lt;br /&gt;
&#039;&#039;&#039;&amp;lt;big&amp;gt;TASK&amp;lt;/big&amp;gt;: Repeat the final task of the previous section for the following lattice sizes: 2x2, 4x4, 8x8, 16x16, 32x32. Make sure that you name each datafile that your produce after the corresponding lattice size! Write a Python script to make a plot showing the energy &#039;&#039;per spin&#039;&#039; versus temperature for each of your lattice sizes. Hint: the NumPy loadtxt function is the reverse of the savetxt function, and can be used to read your previously saved files into the script. Repeat this for the magnetisation. As before, use the plot controls to save your a PNG image of your plot and attach this to the report. How big a lattice do you think is big enough to capture the long range fluctuations?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_22_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_44_025to5_step01.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 16x16||32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Temp_1616_025to5_step01.png|400px]]&lt;br /&gt;
|[[File:Temp_3232_025to5_step01.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
Energy and magnetisation against temperature were plotted in separate graphs.&lt;br /&gt;
&lt;br /&gt;
[[File:All_energy_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[[File:All_mag_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Determining the heat capacity==&lt;br /&gt;
&#039;&#039;&#039;&amp;lt;big&amp;gt;TASK&amp;lt;/big&amp;gt;: Write a Python script to make a plot showing the heat capacity versus temperature for each of your lattice sizes from the previous section. You may need to do some research to recall the connection between the variance of a variable, &amp;lt;math&amp;gt;\mathrm{Var}[X]&amp;lt;/math&amp;gt;, the mean of its square &amp;lt;math&amp;gt;\left\langle X^2\right\rangle&amp;lt;/math&amp;gt;, and its squared mean &amp;lt;math&amp;gt;\left\langle X\right\rangle^2&amp;lt;/math&amp;gt;. You may find that the data around the peak is very noisy &amp;amp;mdash; this is normal, and is a result of being in the critical region. As before, use the plot controls to save your a PNG image of your plot and attach this to the report.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:All_heatcapacity_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
The peak around the critical temperature should increase with lattice size, however.....&lt;br /&gt;
&lt;br /&gt;
==Locating the Curie temperature==&lt;br /&gt;
===TASK1===&lt;br /&gt;
&#039;&#039;&#039;A C++ program has been used to run some much longer simulations than would be possible on the college computers in Python. You can view its source code [https://github.com/niallj/ducking-avenger/tree/master/Ising here] if you are interested. Each file contains five columns: &amp;lt;math&amp;gt;T, E, E^2, M, M^2, C_V&amp;lt;/math&amp;gt;, and you can read them with the NumPy loadtxt function as before. For each lattice size, plot the C++ data against your data. For &#039;&#039;one&#039;&#039; lattice size, save a PNG of this comparison and add it to your report &amp;amp;mdash; add a legend to the graph to label which is which. To do this, you will need to pass the label=&amp;quot;...&amp;quot; keyword to the plot function, then call the legend() function of the axis object (documentation [http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend here]).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Graphs of Heat capacity against temperatue of different sizes lattice using data calculated with different codes, python and C++.&lt;br /&gt;
 &lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 2x2 ||4x4&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_02.png|400px]]&lt;br /&gt;
|[[File:Compare_04.png|400px]]&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 8x8||16x16&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_08.png|400px]]&lt;br /&gt;
|[[File:Compare_16.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|+ &lt;br /&gt;
!Size || 32x32&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&lt;br /&gt;
|[[File:Compare_32.png|400px]]&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
===TASK2=== &lt;br /&gt;
&#039;&#039;&#039;write a script to read the data from a particular file, and plot C vs T, as well as a fitted polynomial. Try changing the degree of the polynomial to improve the fit &amp;amp;mdash; in general, it might be difficult to get a good fit! Attach a PNG of an example fit to your report.&#039;&#039;&#039;&lt;br /&gt;
[[File:Tc_fitting_task2_20polynomial_poorfit.png |600px]]&lt;br /&gt;
&lt;br /&gt;
Fitting 63x64 lattice, 20polynomial, warning showed up poor fit.&lt;br /&gt;
&lt;br /&gt;
===TASK3=== &lt;br /&gt;
&#039;&#039;&#039;Modify your script from the previous section. You should still plot the whole temperature range, but fit the polynomial only to the peak of the heat capacity! You should find it easier to get a good fit when restricted to this region.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Tc_fitting_temrange_2to2_5_6464.png|600px]]&lt;br /&gt;
&lt;br /&gt;
===TASK4===&lt;br /&gt;
&#039;&#039;&#039;Find the temperature at which the maximum in C occurs for each datafile that you were given. Make a text file containing two colums: the lattice side length (2,4,8, etc.), and the temperature at which C is a maximum. This is your estimate of &amp;lt;math&amp;gt;T_C&amp;lt;/math&amp;gt; for that side length. Make a plot that uses the scaling relation given above to determine &amp;lt;math&amp;gt;T_{C,\infty}&amp;lt;/math&amp;gt;. By doing a little research online, you should be able to find the theoretical exact Curie temperature for the infinite 2D Ising lattice. How does your value compare to this? Are you surprised by how good/bad the agreement is? Attach a PNG of this final graph to your report, and discuss briefly what you think the major sources of error are in your estimate.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:Slope_Cmaxx_cen.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Intercept = 2.28128933124&lt;br /&gt;
&lt;br /&gt;
Theoretical Curie temperature =  2.269 Reference: H.A. Kramers and G.H. Wannier, Phys. Rev. 60, 252 (1941)&lt;/div&gt;</summary>
		<author><name>Hc1312</name></author>
	</entry>
</feed>