0% found this document useful (0 votes)
14 views6 pages

Assignment2 210121004 Final

Uploaded by

quixsilverop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Assignment2 210121004 Final

Uploaded by

quixsilverop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Name: Aditya Dhananjay Bahirat, Roll Number: 210121004

Simulating a 2D Ising Model


In this notebook we have simulated a 16 x 16 2D Ising model in absence of external
magnetic field using Monte-Carlo methods.

Introduction to Ising Model


The Ising model is a fundamental statistical mechanics model that describes the behavior of
magnetic materials. It represents a lattice of spins, each with two possible states (+1 or -1),
interacting with their nearest neighbors. This simple model captures the essence of phase
transitions and critical phenomena, making it a powerful tool for understanding complex
systems in various fields, including physics, biology, and computer science.

For studying physical phase transitions, the Hamiltonian the system of spins follows is:
H = −J∑<i,j> SiSj −μH∑i Si which in presence of no magnetic field
i.e. H = 0, reduces to :

H = −J∑<i,j> SiSj
Note that we consider only nearest neighbour interaction

In case of such a Hamiltonian, for a particular configuration 'm', Energy Em and


Magnetization Mm are given by:

Em
and, Mm = ∑i Sj(m)

both these quantities can be averaged over for different values of temperature to find heat
capacity C and magnetic susceptibility χ, using the below formulas, which will show sharp
transitions with temperature, supporting our original claim of phase transition and help us in
estimating a transition critical temperature Tc.

C= kB1T 2 (< E 2 > − < E > 2)

and, χ = N (< M 2 > − < M >2)


kBT

Explaination of code and follow along


In the code below, as per the instructions of the assignment, we have used Monte Carlo
simulations on a 16 x 16 Ising model and have calculated Average Energy, Average
Magnetization, Heat Capacity and Magnetic Susceptibility for such a range of temperature
that phase transition is clearly visibile. We will follow along the code and explain each line as
it comes.
In [16]: #importing some libraries
import numpy as np
import matplotlib.pyplot as plt
import scienceplots
import random
plt.style.use(['simulation techniques',
'notebook'])

Step 1: Initialization
We define the function "lattice" which takes the size of the lattice as the input and generates
the required 2D Lattice of spins. The spins can take values 1 or -1 which are uniformly
distributed over the entire lattice.

In [17]: def lattice(L): conf = 2*np.random.randint(2,


size=(N,N))-1 return conf

Step 2: Perturbing the lattice


We choose a spin at random and flip it. We then follow the metropolis algorithm to either
accept this change or not. This algorithm calculates the energy change induced by this spin
flip and checks whether the change is negative or positive. If the change is negative, it
physically implies that the system has moved towards a more favourable configuration,
hence the spin flip is accepted. If it is positive, we take a chance. We generate random
number between 0 and 1 and compare it with the Boltzmann thermal probability of this spin
flip. If our random number is greater than this thermal probability, then we reject the change
and accept it otherwise.
∑ S where l refers to the 4 nearest neighbours
The change in energy is given by : ΔE = 2JSj l l

of the spin in consideration.


− ΔE

The Boltzmann Thermal probability is given by: p = e kBT .


Important to note is that in this simulation we take J = kB = 1.
The function "switch" below does exactly that.

In [18]: def switch(conf, beta):


for i in range(N):
for j in range(N):
c = np.random.randint(0, N)
d = np.random.randint(0, N) s
= conf[c, d]
nb = conf[(c+1)%N,d] + conf[c,(d+1)%N] + conf[(c-1)%N,d] + conf[c,(
en = 2*s*nb if en < 0: s *= -1
elif random.random() < np.exp(-en*beta):
s *= -1
conf[c, d] = s return conf
Step 3: Calculating Energy and Magnetization
For each configuration obtained, we consider each spin and calculate energy due to its
nearest neighbours and sum it up for all spins. Then we divide the obtained sum by 4 to
avert overcounting. We divide this by number of total spins and then by number of times we
run the simulation to obtain the average energy. We square the initial sum divided by 4,
divide that by number of total spins and by total number of times we run the simulation to
obtain energy squared average.
For obtaining average magnetization and magnetization squared average, we just sum up all
the spins and follow the same procedure as for energy.
Functions 'energy' and 'mag' fulfil this in the code below

In [19]:
def energy(conf): energy = 0
for i in range(len(conf)):
for j in range(len(conf)):
S = conf[i,j]
nb = conf[(i+1)%N, j] + conf[i,(j+1)%N] + conf[(i-1)%N, j] + conf[i,(j
energy += -nb*S return energy/4.

In [20]: def mag(conf): m


= np.sum(conf)
return m

4. Running the simulation


Only thing left now is to run the simulation. The process is to initialize the lattice and fill it
with randomly oriented spins. We initialize an array of temperatures for the points where we
wish to analyse heat capacity and magnetic susceptibility to get an idea of phase transitions.
For each temperature point, the first most important step is to let the system equilibriate
because the statistical averaging or the the theory of ensemble averages works only for
systems in equilibrium. We run the simulation enough number of times to ensure that each
spin is affected atleast once and the system equilibriates. We don't record energies and
magnetizations for this step because this will not make any sense.
After the system equilibriates we record energies and magnetizations, calculate their
averages, heat capacities and susceptibilities for each temperature point and plot them to
analyse accordingly.

In [21]: nt = 100 N
= 16 eqSteps =
1024 mcSteps =
1024

T = np.linspace(1.5, 3.5, nt);


E,M,C,X = np.zeros(nt), np.zeros(nt), np.zeros(nt), np.zeros(nt) n1,
n2 = 1.0/(mcSteps*N*N), 1.0/(mcSteps*mcSteps*N*N)
In [22]: for t in range(nt):
E1 = M1 = E2 = M2 = 0
config = lattice(N)

for i in range(eqSteps): # equilibrate


switch(config, 1/T[t]) # Monte Carlo moves

for i in range(mcSteps):
switch(config, 1/T[t])
Ene = energy(config) # calculate the energy
Mag = mag(config) # calculate the magnetisation

E1 = E1 + Ene
M1 = M1 + Mag
M2 = M2 + Mag*Mag
E2 = E2 + Ene*Ene
E[t] = n1*E1
M[t] = n1*M1
C[t] = (n1*E2 - n2*E1*E1)/(T[t]**2)
X[t] = N*N*(n1*M2 - n2*M1*M1)/T[t]

In [23]: #plotting average energy


plt.scatter(T, E, s=50, marker='o', color='blue')
plt.xlabel("Temperature (T)", fontsize=20);
plt.ylabel("Energy ", fontsize=20) plt.grid(True)
plt.show()

In [25]: #plotting average magnetization


plt.scatter(T, abs(M), s=50, marker='o', color='blue')
plt.xlabel("Temperature (T)", fontsize=20);
plt.ylabel("Magnetization", fontsize=20) plt.grid(True)
plt.show()
In [26]:
plt.scatter(T, C, s=50, marker='o', color='blue')
plt.xlabel("Temperature (T)", fontsize=20);
plt.ylabel("Heat Capacity", fontsize=20)
plt.grid(True) plt.show()

In [28]: plt.scatter(T, X/(N*N), s=50, marker='o', color='blue')


plt.xlabel("Temperature (T)", fontsize=20);
plt.ylabel("Magnetic Susceptibility", fontsize=20)
plt.grid(True) plt.show()
Discussion of Results
1. We observe that average energy increase with temperature. Although there is no clear
indication of a phase transition observing the average energy trend, fitting a function
such as sigmoid will give a clear transition temperature. This monotonous increase also
indicates that with temperature increase, we move from an ordered state of lower
energy to a disordered state of higher energy. This also indicates an increase in entropy.
2. We plot the absolute magnetization with temperature and observe that it monotonically
reduces to 0 with temperature with very turbulent and noisy data around transition
temperature. We can make note of the point that this clearly simulates a ferromagnetic
to paramagnetic system transition. From here as well we can determine a sharp
transition temperature by fitting a sigmoid like function. The higher magnetization
implies an ordered state of the system while a low magnetization indicates a disordered
state.
3. The plots of both heat capacity and magnetic susceptibility show a similar trend with a
very sharp peak around the transition region.
4. Critical temperature pertaining to the phase transition in study lies near 2.5 which is
close to the theoretically solved value of 2.269.

In [ ]:

You might also like