You are on page 1of 27

Two different computational approaches for finding properties

of paramagnetic and ferromagnetic materials


Omar Andres Castano
Solid State Group , U niversity of Antioquia

Abstract
In this work two different computational approaches will be applied in order to
obtain properties such as magnetization and heat capacity from paramagnetic and fer-
romagnetic material. We will study three paramagnetic material both of them with
spin 1/2 and 2/3, the other one will be consider as a classic system where the mag-
netic momentum can point somewhere, we will obtain isotherms and verify the law of
corresponding states using the Metropolis algorithm. We also consider a ferromagnetic
material over which we will use the Ising model along with Metropolis algorithm in or-
der to obtain curves for hysteresis and transition phase, we will also plot heat capacity
curves using a different computation approach in which we will find the energy find the
average value of the energy by finding the energy for each micro state.
Keywords: Ising model, Metropolis Algorithm , Paramagnetism , Ferro magnetism.

1 Introduction
In order to develop the observables, such as energy , magnetization , heat capacity, etc , of a
paramagnet and a ferromagnet classic and quantum models as well as computing techniques
are used. Considering the materials as a 2D square lattice Heisenberg’s quantum model and
Heisenberg’s classic model are used to investigate the properties of paramagnetic materials
with-integer spin and infinite spin respectively, Ising model is used to develop the properties
of paramagnetic and ferromagnetic materials with spin 1/2. For dealing with the problem
of finding the observables computationally, the energy provided by each model to compute
the energy for each microstate can be used; however, this method is inefficient when lattices
are big , therefore Monte Carlo simulations, which is a very easy and powerful method, is
used instead, in particular the Metropolis-Hastings algorithm.
In the first part of this work the energy provided by Ising model is used computationally to
obtain the energy of each microstate for a ferromagnet with spin 1/2 and obtain the critical
point by plotting the specific heat capacity, after that, Metropolis-Hastings algorithm is
implemented along with Heisenberg’s quantum model, Heisenberg’sclassic model and Ising
model for approaching the magnetization of paramagnets with spin 3/1, inf init and 1/2
respectively, Then Metropolis-Hastings algorithm is implemented along with Ising model
for approaching the magnetization of a ferromagnet with spin 1/2, finally computational

1
and theoretical approaches are compared and conclusion are given. All the codes used for
this work are given in Appendix A

2 Theory
2.1 Ising Model
In the Ising model, we consider a lattice of magnetic moments, as shown in Fig.1.(In this
figure,two-dimensional (2D) square lattice, was drown but in general, it could have any
lattice structure in any dimension.) On each lattice site, the local magnetic moment is
represented by a spin, drawn as an arrow in the figure. We assume that the spin is 1/2;
therefore, it has just two possible states, either pointing up or pointing down. Mathemat-
ically, we represent the spin at site i by the variable σi = ±1. In this notation, +1 means
that the spin is pointing up, and −1 means that it is pointing down.

Figure 1: Example
of the Ising model
on a 2D square lat-
tice. Each arrow is
a spin, which repre-
sents a magnetic mo-
ment that can point
either up or down [2]

The energy for the Ising model includes two contributions: the interaction between neigh-
bouring spins and the effect of an applied magnetic field on each individual spin. The
interaction between neighbouring spins tends to induce parallel alignment of the neigh-
bours, so it should be favourable (negative) when the neighbours are both +1 or both −1,
and unfavourable (positive) when the neighbours are +1 next to −1. Hence, for each pair
of neighbours i and j, the interaction energy can be written as −Jσi σj , where J is a
positive coefficient giving the interaction strength. If the magnetic field H is pointing up,
it favours each spin pointing up; if the field is pointing down, it favours each spin pointing
down. Hence, for each site i, the field energy can be written as −Hσi . Putting these pieces
together, the total energy for the system becomes [1]

2
X X
E = −J σi σj − H σj (1)
<ij> j

In the first sum, the angle brackets < i, j > represent nearest neighbour pairs (for example,
north-south and east-west on the 2D square lattice); the sum is taken over all nearest-
neighbour pairs. By comparison, the second sum is taken over all individual sites j, which
are each affected by the magnetic field. In the edges of the system we will apply boundary
condition.

For the case of a paramagnetic material we will consider that J = 0, which means that there
is not interaction between spins but there is interactions between spins and the external
magnetic field. For a ferromagnetic material we will consider that J = 1, which means that
there is interaction between spins as well as between spins and the external magnetic field.

According to quantum mechanics mechanics the magnetic momentum in z-direction for


each molecule in the lattice is given by Eq.2

µ = µ0 m (2)
Where m is the magnetic quantum number. The possibles values for a paramagnetic
molecule is given by

Em = −µ0 Hm (3)
Where H is a external magnetic field.
Using Eq.2 and Eq.3 statistical physics provide the magnetization for a paramagnetic ma-
terial formed by paramagnetic molecules of spin 1/2, this mean magnetization is given
by[1]
µH
 
M = N tanh (4)
kB T
Where kB is Boltzmann constant and T is the temperature, and according to mean-field
theory the phase transition temperature for a ferromagnetic material is given by[1].

Jq
Tc = (5)
kB
Where kB is Boltzmann constant, T is the temperature and q is the coordination number.

2.2 Heisenberg’s Quantum Model


Using Eq.2 and Eq.3 statistical physics provide the magnetization for a paramagnetic ma-
terial formed by paramagnetic molecules of integer spin or half-odd spin, this mean magne-
tization is given by[1]

3
1 1 η 1
   
M = N gL µB (J + )coth((J + )η) − coth (6)
2 2 2 2
Where gl is the Landef actor, J is the magnitude of the total angular momentum and η is

µ0 H
η= (7)
kB T

2.3 Heisenberg’s Classic Model


For the case when the magnetic momentum is not quantized the Hamiltonian used is

H = −µ0 Hcos(θ) (8)


Where θ is the angle between the magnetic field pointing in the z-direction and the magnetic
momentum pointing anywhere. For this case, according to statistical mechanics the mean
magnetization is given by

1
 
M = N µ cothη − (9)
η
Where N is the number of total particles, µ is the magnetic momentum in the z-direction
and η is given by

µH
η= (10)
kB T

3 Computational Approach
3.1 Monte Carlo Method
The fact that we can make a successful stochastic interpretation of the sampling procedure
proves to be pivotal in allowing us to introduce the Monte Carlo method as a technique
to solve for the observables of the Ising Model. A Monte Carlo calculation is defined,
fundamentally, as explicitly using random variates and a stochastic process is characterized
by a random process that develops with time. The Monte Carlo method thus lends itself
very naturally to simulating systems in which stochastic processes occur. From what we
have established with regards to the sampling being analogous to a time averaging along a
stochastic trajectory in the phase space it is possible to simulated this process by using the
Monte Carlo method. The algorithm used is design around the principle of the Metropolis
sampling.

4
Figure 2: Flowchart for Metropolis algorithm[3]

3.2 Metropolis Algorithm


The steps followed in the Metropolis algorithm are summarized in the flowchart of Fig.2.

• In the first step the lattice is INITIALIZED to a starting configuration. This may either
be a homogeneous or random configuration. A random configuration has a benefit in that it
uses less computing time to reach a equilibrated configuration with the associated heat bath.

• In the following PROCESS the random number generator is used to select a position on
the lattice by producing a uniformly generated number between 1 and N.

• A DECISION is then made whether the change in energy of flipping that particular spin

5
selected is lower than zero. This is in accordance with the principle of energy minimization.
– If the change in energy is lower than zero then a PROCESS is invoked to flip the spin at
the selected site and the associated change in the observables that want to be monitored
are stored.
– If the change in energy is higher than zero then a DECISION has to be used to establish
if the spin is going to be flipped, regardless of the higher energy consideration. A random
number is generated between 0 and 1 and then weighed against the Boltzmann Probability
factor. If the random number is less than the associated probability, e−β∆E , then the spin is
flipped (This would allow for the spin to be flipped as a result of energy absorbed from the
heat bath, as in keeping with the principle of entropy maximization) else it is left unchanged
in its original configuration.

• The above steps are repeated N times and checked at this point in a DECISION to deter-
mine if the loop is completed. The steps referred to here do not include the initialization
which is only required once in the beginning of the algorithm.

• Once the N steps are completed a PROCESS is used to add all the progressive changes
in the lattice configuration together with the original configuration in order to produce a
new lattice.
configuration.

• All these steps are, in turn, contained within a Monte Carlo loop. A DECISION is used
to see if these steps are completed.

• Once the Monte Carlo loop is completed the program is left with, what amounts to, the
sum of all the generated lattices within the N loops. A PROCESS is thus employed to
average the accumulated change in observables over the number of spins and the number
of Monte Carlo steps.

3.3 Ising Model


Firs we will use Isign model for a ferromagnetic material, which is consider as LXL square
lattice, in absence of a external magnetic field to compute the energy for each microstate
and obtain histograms for the microstates and the energy related with each microstate using
(Eq.3), after that, specific heat capacity is plotted using (Eq.4), and critical point (Tc ) is
find.

e−βEr = Ω(Er )e−βEr


X X
Z= (11)
Er Er

1
Cv = (< E 2 > − < E >2 ) (12)
kB T 2

6
We will use again Ising model but know in presence of a magnetic field,the Hamiltonian
given by Eq.1 will be used along with the Metropolis algorithm for obtained a hysteresis
curve for a ferromagnetic material, after that we will turn off the magnetic field and plot
a graph which shows a transition phase, finally in order to visualize better the transition
phase we will plot 6 magnetic states for different temperatures.

3.4 Heisenberg’s Quantum Model


We will use Eq.3 along with Metropolis algorithm in order to plot 3 isotherms of M vsHfor 3
different values of the temperature,for a paramagnetic material with paramagnetic molecules
that have spin 3/2, then we will use Eq.6 for plot the same isotherms and compare theoretical
results with computational approaches. We will also verify the law of corresponding states
using the computational approach.

3.5 Heisenberg’s Classic Model


We will use Eq.8 along with Metropolis algorithm in order to plot 3 isotherms of M vsHfor
3 different values of the temperature for a paramagnetic material formed by paramagnetic
molecules whose magnetic momentums can point anywhere, then we will use Eq.9 for plot
the same isotherms and compare theoretical results with computational approaches. We
will also verify the law of corresponding states using the computational approach.

4 Results and Discussions


4.1 Ising model in absence of a magnetic field
In absence of a magnetic field and using Eq.1. we have compute the energies and the
microstates for lattices of sizes 4x4 and 3x3, with those values of energies we plotted his-
tograms shows in Fig.3 and Fig.4

Figure 3: Histogram 4 × 4 lattice Figure 4: Histogram 5 × 5 lattice

7
We can see that for all cases the microstates are around a maximum number of microstates,
and that number of energy which has the maximum number of microstates is zero or near
it. the codes used for plotting those histograms is Code1 and is shown in Appendix A

Usign those values of energy and its corresponding microstate along with equation 11 we
obtained the curves for specific heat capacity shown in Fig.5

Figure 5: Curves of specific heat capacity. Blue solid line is the curve for the 4 × 4 lattice
and green solid lien is the curve for the 5 × 5 lattice.

The picks on Fig.5 show the transition from a ferromagnetic state to a paramagnetic state,
the positions of those picks correspond with the critical points (Tc ). For the 4 × 4 (blue
solid line) the pick is located at Tc = 2.41K/kB , For the 5 × 5 (blue solid line) the pick is
located at Tc = 2.35K/kB , and according Eq.5 the critical point is located at Tc = 2K/kB ,
therefore when the size lattice is increased the pick must be located at Tc = 2K/kB .
code.2 was used to plot Fig.5 and obtain the critical points. (See Appendix A)
To obtain Fig.5 was required 5.2minutes that was a short time considering that more than
33millions of energies were computed. Although the code was parallelized and the time
for computing those energies were short, if we try to increase the size of the lattice, the
program is useless because more than 2 days are required to compute the energies for a
8 × 6 lattice using Code.2.

4.2 Ising model using Metropolis algorithm in presence of a magnetic


field
As we have shown in the last section, compute the energy for each microstate for finding so
properties of a material such as heat capacity is inefficient; therefore, in order to calculate
the magnetization for a paramagnetic material(composed of paramagnetic molecules of spin
1/2 ) in a efficient way we have used the Metropolis algorithm along with Eq.1 and we have
plotted the isotherms shown in Fig.6, Fig7 and Fig 8(remember that for a paramagnetic
material J = 0).

8
Figure 6: Isotherm for T = Figure 7: Isotherm for T = Figure 8: Isotherm for T =
10K/kB . Black solid line 20K/kB . Black solid line 30K/kB . Black solid line
is the plot of the theoretical is the plot of the theoretical is the plot of the theoretical
function Eq.4. function Eq.4. function Eq.4.

code.3 was use to obtain those graphs(see Appendix A). In code.3 Metropolis algorithm
was implemented for a square lattice of size 100 × 100. In order to obtain good results,
Metropolis algorithm was applied 200time for each value of magnetic field.
As mentioned before, we have also proof the law of corresponding states shown in Fig.9.That
plot was also made using code 3.

Figure 9: Law of corresponding states. Red dots are the plot with T = 10K/kB , green dots
are the plot with T = 20K/kB , blue dots are the plot with T = 30K/kB

4.3 Ising model using Metropolis algorithm in absence of a magnetic field


We have used Eq.1 along with Metropolis algorithm in order to show the hysteresis that
a ferromagnetic materialism exhibits (remember that for a ferromagnetic material we are
considering J = 1 and also that molecules have spin 1/2), that property is shown in Fig.10
for a given value of temperature.Fig.10 was made with code.4.

9
Figure 10: Hysteresis curve for a ferromagnetic material at T = 5K/kB

Finally we have plotted a curve of the magnetization in function of the temperature (M


VS.T ), in order to show the existence of a phase transition, Fig.11, that figure was made
using code.5.

Figure 11: Phase transition for a ferromagnetic material in absence of magnetic field at
T = 5K/kB

in order have other way to visualize the phase transition we have plot 6 figures of magnetic,
see Fig.12, Fig.13, Fig.14, Fig.15, Fig.16, Fig.17, those figures were made using code.6.

10
0.5K 1.5K 2.5K
Figure 12: T = kB Figure 13: T = kB Figure 14: T = kB

2.5K 2.5K 2.5K


Figure 15: T = kB Figure 16: T = kB Figure 17: T = kB

Figure 18: Isotherm for T = Figure 19: Isotherm for T = Figure 20: Isotherm for T =
10K/kB . Black solid line 20K/kB . Black solid line 30K/kB . Black solid line
is the plot of the theoretical is the plot of the theoretical is the plot of the theoretical
function Eq.6. function Eq.6. function Eq.6.

4.4 Heisenberg’s Quantum Model


We have used Eq.3 along with Metropolis algorithm in order to plot 3 isotherms of M vsHfor
3 different values of the temperature for a paramagnetic material with paramagnetic molecules
that have spin 3/2 see Fig.18, Fig.18 and Fig.20. Code.3 was implemented for plotting those
graphs.
As mentioned before, we have also proof the law of corresponding states shown in Fig.9.That
plot was also made using code 3.See Fig21

11
Figure 21: Law of corresponding states. Red dots are the plot with T = 10K/kB , green
dots are the plot with T = 20K/kB , blue dots are the plot with T = 30K/kB

4.5 Heisenberg’s Quantum Model


We will use Eq.8 along with Metropolis algorithm in order to plot 3 isotherms of M vsHfor
3 different values of the temperature for a paramagnetic material formed by paramagnetic
molecules whose magnetic momentums can point anywhere, see Fig.22, Fig.23 and Fig.24.
Code.3 was implemented for plotting those graphs.
As mentioned before, we have also proof the law of corresponding states shown in Fig.9.That
plot was also made using code 3. See Fig.25

Figure 22: Isotherm for T = Figure 23: Isotherm for T = Figure 24: Isotherm for T =
10K/kB . Black solid line 20K/kB . Black solid line 30K/kB . Black solid line
is the plot of the theoretical is the plot of the theoretical is the plot of the theoretical
function Eq.6. function Eq.6. function Eq.6.

12
Figure 25: Law of corresponding states. Red dots are the plot with T = 10K/kB , green
dots are the plot with T = 20K/kB , blue dots are the plot with T = 30K/kB

5 Conclusions
Firs we have obtained the curve for the specific heat capacity for a ferromagnetic material
by calculating the energy for each microstat. Although we found a critical point which
was near to the theoretical value, we noticed that obtain properties of paramagnetic and
ferromagnetic material based on the compute of each microstate is an inefficient way to
find properties computationally. We have seen that compute the energy for each microstate
in order to find some properties of material such as magnetization and heat capacity in
inefficient computationally; therefore , a power and useful method such as Metropolis-
Hastings mus be implemented in order to be more efficient computationally.
Metropolis algorithm have been used in order to evaluate isotherms for three different cases:
three paramagnetic material with spin 1/2, 3/2 and a classical situation where magnetic
momentum can point somewhere. Data obtained from the use of Metropolis algorithm
fitted to the theoretical curves, we also have shown the la of corresponding states for this
three cases.
We have shown that the ferromagnetic system present magnetism below Tc even when there
is not an external magnetic field, that verify the existence of a phase transition for those
systems.

References
[1] P.M. Chaikin, T.C.Lubensky, Principles of Condensed Matter Physics (Cam-bridge,
1995)

[2] http://rutgersscholar.rutgers.edu/volume02/cowldevl/cowldevl.htm

[3] Monte Carlo investigation of the Ising model, T obinF ricke ,December 2006

[4] https : //docs.scipy.org/doc/numpy/ref erence/generated/numpy.std.html

13
6 Appendix A
Code.1: Computer program for plotting a histograms
1 import numpy a s np
2 from c o l l e c t i o n s import d e f a u l t d i c t
3 import m a t p l o t l i b . p y p l o t a s p l t
4 import o s
5 from m u l t i p r o c e s s i n g import Pool
6 from sympy . u t i l i t i e s . i t e r a b l e s import m u l t i s e t p e r m u t a t i o n s
7
8 ###################################################################
9 # BOUNDARY CONDITIONS
10 ###################################################################
11 d e f BC( i ) :
12 i f ( ( i +1−1.)/N) . i s i n t e g e r ( ) :
13 r e t u r n abs ( i −N)
14 e l i f i>=N∗N:
15 r e t u r n abs ( i −N)
16 else :
17 return i
18
19 d e f BC2( i ) :
20 i f i >= N∗N:
21 r e t u r n abs (N∗N−i )
22 else :
23 return i
24
25
26
27 ###################################################################
28 # ENERGY COMPUTATION
29 ###################################################################
30 d e f Energy ( k ) :
31 x=open ( ’%dDATOS. t x t ’ %(k ) , ’w ’ )
32 f o r A i n m u l t i s e t p e r m u t a t i o n s (U[ k ] ) : #make p e r m u t a t i o n s o f an a r r a y
33 E=0;
34 f o r i i n r a n g e (N∗N) : #compute e n e r g y
35 E+=A[ i ] ∗A[BC( i +1)]+A[ i ] ∗A[ BC2( i+N) ]
36 x . w r i t e ( ’%d \n ’ %(−E) ) #s a v e s e n e r g y i n a l i s t
37 x . close ()
38
39
40
41 ###################################################################
42 # Run t h e code i n p a r a l l e l u s i n g a l l c o r e s
43 ###################################################################
44 N=4 # s i z e o f t h e system
45 f = [ 1 ] ∗N∗N
46 U=[ l i s t ( f ) ]
47
48 f o r i i n r a n g e (N∗N) :

14
49 f [ i ]=−1
50 U. append ( l i s t ( f ) )
51
52
53 if name == ” main ”:
54
55 p=Pool ( )
56 p . map( Energy , r a n g e (N∗N+1) )
57
58
59
60 o s . system ( ’ c a t ∗ . t x t > data ’ ) #e x e c u t e commands i n t h e t e r m i n a l
61 o s . system ( ’ rm ∗ . t x t ’ )
62
63 G= [ ]
64 appearances = d e f a u l t d i c t ( i n t ) #c r e a t e a matrix with t h e f r e q u e n c y
65 with open ( ’ data ’ ) a s l : #v a l u e s o f t h e e n e r g y
66 for m in l :
67 a p p e a r a n c e s [ i n t (m) ] += 1
68
69 a=a p p e a r a n c e s . i t e m s ( )
70 a . sort () #s o r t t h e e n e r g y v a l u e s
71 G=np . r e s h a p e ( a , ( l e n ( a ) , 2 ) ) #c r e a t e a matrix
72
73
74 y=G [ : , 1 ]
75 x=G [ : , 0 ]
76 p l t . bar ( x , y , a l i g n= ’ c e n t e r ’ , width =2) #p l o t s a h i s t o g r a m f o r t h e e n e r g y v a l u e s
77 p l t . xl im ( np . min ( x ) −3,np . max( x ) +3) #d e f i n e d x−l i m i t s
78 p l t . yl im ( 0 , np . max( y )+N∗N) #d e f i n e d y−l i m i t s
79 p l t . t i t l e ( ’ $Energy \ Histogram$ ’ ) #s e t t i t e l
80 p l t . y l a b e l ( ’ $ \Omega(E) $ ’ ) #s e t y−l a b e l
81 p l t . x l a b e l ( ’ $E$ ’ ) #s e t x−l a b e l
82 p l t . s a v e f i g ( ’ i s i n g%d . png ’ %(N) ) #s a v e t h e p l o t
83 p l t . show ( )

Code.2: Computer program for plotting specific heat capacity graph


1 import time
2 s t a r t = time . time ( )
3 import numpy a s np
4 from c o l l e c t i o n s import d e f a u l t d i c t
5 import m a t p l o t l i b . p y p l o t a s p l t
6 import o s
7 from m u l t i p r o c e s s i n g import Pool
8 from sympy . u t i l i t i e s . i t e r a b l e s import m u l t i s e t p e r m u t a t i o n s
9
10 ###################################################################
11 # BOUNDARY CONDITIONS
12 ###################################################################
13 d e f BC( i ) :

15
14 i f ( ( i +1−1.)/N) . i s i n t e g e r ( ) :
15 r e t u r n abs ( i −N)
16 e l i f i>=N∗N:
17 r e t u r n abs ( i −N)
18 else :
19 return i
20
21 d e f BC2( i ) :
22 i f i >= N∗N:
23 r e t u r n abs (N∗N−i )
24 else :
25 return i
26
27 ###################################################################
28 # ENERGY COMPUTATION
29 ###################################################################
30 d e f Energy ( k ) :
31 x=open ( ’%dDATOS. t x t ’ %(k ) , ’w ’ )
32 f o r A i n m u l t i s e t p e r m u t a t i o n s (U[ k ] ) :
33 E=0;
34 f o r i i n r a n g e (N∗N) :
35 E+=A[ i ] ∗A[BC( i +1)]+A[ i ] ∗A[ BC2( i+N) ]
36 x . w r i t e ( ’%d \n ’ %(−E) )
37 x . close ()
38
39 ###################################################################
40 # Run t h e code i n p a r a l l e l u s i n g a l l c o r e s
41 ###################################################################
42 for N in [ 4 , 5 ] : # s i z e o f t h e system
43 p r i n t (N)
44 f = [ 1 ] ∗N∗N
45 U=[ l i s t ( f ) ]
46
47 f o r i i n r a n g e (N∗N) :
48 f [ i ]=−1
49 U. append ( l i s t ( f ) )
50
51 if name == ” main ”:
52
53 p=Pool ( )
54 p . map( Energy , r a n g e (N∗N+1) )
55
56
57 o s . system ( ’ c a t ∗ . t x t > data ’ ) #e x e c u t e commands i n t h e t e r m i n a l
58 o s . system ( ’ rm ∗ . t x t ’ )
59
60 G= [ ]
61 appearances = d e f a u l t d i c t ( i n t ) #c r e a t e a matrix with t h e f r e q u e n c y
62 with open ( ’ data ’ ) a s l : #v a l u e s o f t h e e n e r g y
63 for m in l :
64 a p p e a r a n c e s [ i n t (m) ] += 1
65

16
66 a=a p p e a r a n c e s . i t e m s ( )
67 a . sort () #s o r t t h e e n e r g y v a l u e s
68 G=np . r e s h a p e ( a , ( l e n ( a ) , 2 ) ) #c r e a t e a matrix
69
70
71 T=np . l i n s p a c e ( 0 . 5 , 2 0 , 5 0 0 ) #t e m p e r a t u r e g r i l l
72 Z=sum ( b∗np . exp(−E/T) f o r E , b i n G ) #p a r t i t i o n f u n c t i o n
73 meanE=sum ( b∗E∗np . exp(−E/T) f o r E , b i n G ) /Z #a v e r a g e o f E
74 meanE2=sum ( b∗E∗E∗np . exp(−E/T) f o r E , b i n G ) /Z #a v e r a g e o f Eˆ2
75 c a p a c i t y =(meanE2−(meanE ) ∗ ∗ 2 ) / (T∗T∗N) #h e a t c a p a c i t y
76
77 p r i n t (max( c a p a c i t y ) ) #maximum o f c a p a c i t y
78 p l t . xl im ( 0 , 7 )
79 p l t . y l a b e l ( r ’ S p e c i f i c heat capacity ’ )
80 p l t . x l a b e l ( r ’ $T$ ’ )
81 p l t . p l o t (T, c a p a c i t y ) #p l o t t h e h e a t c a p a c i t y
82
83 p l t . s a v e f i g ( ’ h e a t c a p a c i t y . png ’ )
84 #p l t . show ( )
85
86 s t o p = time . time ( )
87 print (( stop − s t a r t ) /60.) #CPU time

Code.3: Computer program with implements Ising model along with Metropo-
lis algorithm
1 from p y l ab import ∗
2 from random import uni for m
3 import random
4 from s c i p y . o p t i m i z e import c u r v e f i t
5 import numpy a s np
6
7 #===============================================================
8 # I n i t i a l Microstate
9 #===============================================================
10
11 d e f IMS ( ) :
12 # a r r a y f o t t h e c a s e S=1/2
13 # s p i n=r e s h a p e ( a r r a y ( [ random . c h o i c e ( [ − 1 . / 2 , 1 . / 2 ] ) f o r i i n r a n g e (N∗N) ] ) , (N,N)
)
14
15 #a r r a y f o r t h e c a s e s =3/2
16 s p i n=r e s h a p e ( a r r a y ( [ random . c h o i c e ( [ − 1 . / 2 , 1 . / 2 , 3 . / 2 , − 3 / 2 ] ) f o r i i n r a n g e (N∗N
) ] ) , (N,N) )
17
18 #a r r a y f o r t h e c a s e s=i n f i n i t y
19 # s p i n=r e s h a p e ( a r r a y ( [ u nif orm ( −1 ,1) f o r i i n r a n g e (N∗N) ] ) , (N,N) )
20 return spin
21
22
23 #===============================================================

17
24 # Boundary C o n d i t i o n s
25 #===============================================================
26 d e f BC( i ) :
27 i f i +1 > (N−1) :
28 return 0
29 e l i f ( i −1) < 0 :
30 r e t u r n (N−1)
31 else :
32 return i
33
34
35 #===============================================================
36 # Energy Computation
37 #===============================================================
38
39 d e f Energy ( s p i n , J , i , j ,H) :
40 E=−J∗ s p i n [ i ] [ j ] ∗ ( s p i n [BC( i −1) ] [ j ]+ s p i n [BC( i +1) ] [ j ] +
41 s p i n [ i ] [ BC( j +1)]+ s p i n [ i ] [ BC( j −1) ] ) − s p i n [ i ] [ j ] ∗H
42 return E
43
44
45 #===============================================================
46 # M a g n e t i z a t i o n Computation
47 #===============================================================
48 d e f Mag( s p i n ,N) :
49 M=sum ( s p i n )
50 r e t u r n (M)
51
52 #===============================================================
53 # Loop Monte C a r l o P r i n c i p a l
54 #===============================================================
55
56 d e f main (T, H, J , s p i n ) :
57 #u s e m e t r o p o l i s a l g o r i t h m t o f i n thh most
58 #p r o b a b l i y s t a t e s
59 f o r x in range (50) :
60 j=np . random . r a n d i n t ( 0 ,N)
61 i=np . random . r a n d i n t ( 0 ,N)
62
63 DeltaE=−2∗Energy ( s p i n , J , i , j ,H)
64
65 f=np . exp ( −( DeltaE ) / ( 0 . 2 ∗T) )
66 i f DeltaE <= 0 . :
67 s p i n [ i ] [ j ] ∗=−1
68
69 e l i f f >= uni for m ( 0 , 1 ) :
70 s p i n [ i ] [ j ] ∗=−1
71
72 return ( spin )
73 #===============================================================
74 # Compute The mean m a g n e t i z a t i o n
75 #===============================================================

18
76
77 d e f m e a n m a g n e t i z a t i o n ( J , T) :
78 h=[]
79 m= [ ]
80 s t e p s =300 #numer o f r e p e t i t i o n s
81
82 #computes t h e mean m a g n e t i z a c i o n u s i n g t h e m i c r o s t a t e s
83 #g i v e n by t h e m e t r o p o l i s a l g o r i t h m
84 f o r H in l i n s p a c e ( −11 ,11 ,200) :
85 M=0.
86 s p i n=IMS ( )
87
88 f o r k i n r a n g e ( s t e p s ) : #c o n d i t i o n o f e q u i l i b r i u m
89 main (T, H, J , s p i n )
90
91 f o r k i n r a n g e ( s t e p s ) : #computaion o f mean m a g n e t i z a t i o n
92 main (T, H, J , s p i n )
93 M+=Mag( s p i n ,N)
94 h . append (H/ ( 0 . 2 ∗T) )
95 m. append (M/ ( s t e p s ∗N∗N) )
96
97 r e t u r n ( h ,m)
98
99 #===============================================================
100 # T h e o r e t i c a l f u n c t i o s f o r the magnetization
101 #===============================================================
102 #d e f f u n c ( h , a , b ) :
103 #f o r i s i n g
104 # r e t u r n a ∗ tanh ( b∗np . a s a r r a y ( h ) )
105
106 #f o r B r i l l o u i n
107 # r e t u r n a ∗ ( ( ( 3 . / 2 + 1 . / 2 ) ∗ 1 . / ( np . tanh ( ( 3 . / 2 + 1 . / 2 ) ∗ ( b∗np . a s a r r a y ( h ) ) ) ) −1./( np .
tanh ( b∗np . a s a r r a y ( h ) / 2 ) ) ∗ 1 . / 2 ) )
108
109 #f o r Langevin
110 # r e t u r n a ∗ ( c o s h ( b∗np . a s a r r a y ( h ) ) / s i n h ( b∗np . a s a r r a y ( h ) ) −1./( b∗np . a s a r r a y ( h ) ) )
111
112
113
114 #
=================================================================================

115 # P l o t o f t h e o r e t i c a l f u n c t i o n s a l o g with c o m p u t a i o n a l a p p r o e a c h e s
116 # f o r three d i f f e r e n t values fo temperature
117 #
=================================================================================

118 N=100 #s i z e o f t h e m a t r i z
119 f o r T i n [ 1 0 . , 2 0 . , 3 0 . ] : #t e m e p e r a t u r e s
120 h ,m=m e a n m a g n e t i z a t i o n ( 0 ,T)
121 xl im ( −10 ,10)
122 yl im ( amin (m) −0.1 ,amax (m) +0.1)

19
123 t i t l e ( ’ M a g n e t i z a t i o n vs Magnetic F i e l d ’ )
124 y l a b e l ( r ’$M$ ’ )
125 x l a b e l ( r ’$H/T$ ’ )
126 p l o t ( h ,m, ’ . ’ )
127 # param=c u r v e f i t ( func , h , m)
128 # a , b=param [ 0 ]
129 # p e s=p l o t ( h , f u n c ( h , a , b ) , ’ b l a c k ’ )
130 s a v e f i g ( ’ law3 . png ’ )
131 show ( )

Code.4: Computer program for showing hysteresis


1 from p y l ab import ∗
2 from random import uni for m
3 import random
4 from s c i p y . o p t i m i z e import c u r v e f i t
5 import numpy a s np
6
7 #================================================================
8 # I n i t i a l Microstate
9 #================================================================
10
11 d e f IMS ( ) :
12 #a r r a y f o t t h e c a s e S=1/2
13 s p i n=r e s h a p e ( a r r a y ( [ random . c h o i c e ( [ − 1 . / 2 , 1 . / 2 ] ) f o r i i n r a n g e (N∗N) ] ) , (N,N)
)
14 return spin
15
16
17 #================================================================
18 # Boundary C o n d i t i o n s
19 #================================================================
20 d e f BC( i ) :
21 i f i +1 > (N−1) :
22 return 0
23 e l i f ( i −1) < 0 :
24 r e t u r n (N−1)
25 else :
26 return i
27
28 #================================================================
29 # Energy Computation
30 #================================================================
31
32 d e f Energy ( s p i n , J , i , j ,H) :
33 E=−1∗ s p i n [ i ] [ j ] ∗ ( s p i n [BC( i −1) ] [ j ]+ s p i n [BC( i +1) ] [ j ] +
34 s p i n [ i ] [ BC( j +1)]+ s p i n [ i ] [ BC( j −1) ] ) − s p i n [ i ] [ j ] ∗H
35 return E
36
37 #================================================================
38 # M a g n e t i z a t i o n Computation

20
39 #================================================================
40 d e f Mag( s p i n ,N) :
41 M=sum ( s p i n )
42 r e t u r n (M)
43
44 #================================================================
45 # Loop Monte C a r l o P r i n c i p a l
46 #================================================================
47
48 d e f main (T, H, J , s p i n ) :
49 #u s e m e t r o p o l i s a l g o r i t h m t o f i n thh most
50 #p r o b a b l i y s t a t e s
51 f o r x in range (50) :
52 j=np . random . r a n d i n t ( 0 ,N)
53 i=np . random . r a n d i n t ( 0 ,N)
54
55 DeltaE=−2∗Energy ( s p i n , J , i , j ,H)
56
57 f=np . exp ( −( DeltaE ) / (T) )
58 i f DeltaE <= 0 . :
59 s p i n [ i ] [ j ] ∗=−1
60
61 e l i f f >= uni for m ( 0 , 1 ) :
62 s p i n [ i ] [ j ] ∗=−1
63
64 return ( spin )
65 #================================================================
66 # Compute The mean m a g n e t i z a t i o n i n c r e a s i n g t h e magnetic f i e l d
67 #================================================================
68
69 d e f m e a n m a g n e t i z a t i o n ( J , T) :
70 h=[]
71 m= [ ]
72
73 s t e p s =1000 #numer o f r e p e t i t i o n s
74
75 #computes t h e mean m a g n e t i z a c i o n u s i n g t h e m i c r o s t a t e s
76 #g i v e n by t h e m e t r o p o l i s a l g o r i t h m
77 f o r H in l i n s p a c e ( −30 ,30 ,200) :
78 M=0.
79 s p i n=IMS ( )
80
81 f o r k i n r a n g e ( s t e p s ) : #c o n d i t i o n o f e q u i l i b r i u m
82 main (T, H, J , s p i n )
83
84 f o r k i n r a n g e ( s t e p s ) : #computaion o f mean m a g n e t i z a t i o n
85 main (T, H, J , s p i n )
86 M+=Mag( s p i n ,N)
87 h . append (H+2)
88 m. append (M/ ( s t e p s ∗N∗N) )
89
90 r e t u r n ( h ,m)

21
91
92
93
94 #================================================================
95 # Compute The mean m a g n e t i z a t i o n d e c r e a s i n g t h e magnetic f i e l d
96 #================================================================
97
98 d e f m e a n m a g n e t i z a t i o n 1 ( J , T) :
99 h=[]
100 m= [ ]
101
102 s t e p s =500 #numer o f r e p e t i t i o n s
103
104 #computes t h e mean m a g n e t i z a c i o n u s i n g t h e m i c r o s t a t e s
105 #g i v e n by t h e m e t r o p o l i s a l g o r i t h m
106 f o r H in l i n s p a c e (30 , −30 ,200) :
107 s p i n=IMS ( )
108 M=0.
109 f o r k i n r a n g e ( s t e p s ) : #c o n d i t i o n o f e q u i l i b r i u m
110 main (T, H, J , s p i n )
111
112 f o r k i n r a n g e ( s t e p s ) : #computaion o f mean m a g n e t i z a t i o n
113 main (T, H, J , s p i n )
114 M+=Mag( s p i n ,N)
115 h . append (H)
116 m. append (M/ ( s t e p s ∗N∗N) )
117
118 r e t u r n ( h ,m)
119
120
121
122
123
124 #================================================================
125 # Plot fo the h y s t e r e s i s curve
126 #================================================================
127 N=100 #s i z e o f t h e m a t r i z
128
129 T=5. #t e m e p e r a t u r e s
130 h1 , m1=m e a n m a g n e t i z a t i o n ( 1 ,T)
131 h ,m=m e a n m a g n e t i z a t i o n 1 ( 1 ,T)
132 xl im ( −30 ,30)
133 yl im ( amin (m) −0.1 ,amax (m) +0.1)
134 t i t l e ( ’ H y s t e r e s i s Curve ’ )
135 y l a b e l ( r ’$M$ ’ )
136 x l a b e l ( r ’ $H$ ’ )
137 p l o t ( h1 , m1, ’ . ’ )
138 p l o t ( h ,m, ’ . ’ )
139 s a v e f i g ( ’ H y s t e r e s i s . png ’ )
140
141
142 show ( )

22
Code.5: Computer program for showing the phase transition
1 from p y l ab import ∗
2 from random import uni for m
3 import random
4 from s c i p y . o p t i m i z e import c u r v e f i t
5 import numpy a s np
6
7 #================================================================
8 # I n i t i a l Microstate
9 #================================================================
10
11 d e f IMS ( ) :
12 #a r r a y f o t t h e c a s e S=1/2
13 s p i n=r e s h a p e ( a r r a y ( [ random . c h o i c e ( [ 1 . / 2 , 1 . / 2 ] ) f o r i i n r a n g e (N∗N) ] ) , (N,N) )
14 return spin
15
16
17 #================================================================
18 # Boundary C o n d i t i o n s
19 #================================================================
20 d e f BC( i ) :
21 i f i +1 > (N−1) :
22 return 0
23 e l i f ( i −1) < 0 :
24 r e t u r n (N−1)
25 else :
26 return i
27
28
29 #================================================================
30 # Energy Computation
31 #================================================================
32
33 d e f Energy ( s p i n , J , i , j ,H) :
34 E=−J∗ s p i n [ i ] [ j ] ∗ ( s p i n [BC( i −1) ] [ j ]+ s p i n [BC( i +1) ] [ j ] +
35 s p i n [ i ] [ BC( j +1)]+ s p i n [ i ] [ BC( j −1) ] ) − s p i n [ i ] [ j ] ∗H
36 return E
37
38
39 #================================================================
40 # M a g n e t i z a t i o n Computation
41 #================================================================
42 d e f Mag( s p i n ,N) :
43 M=sum ( s p i n )
44 r e t u r n (M)
45
46 #================================================================
47 # Loop Monte C a r l o P r i n c i p a l
48 #================================================================
49
50 d e f main (T, H, J , s p i n ) :
51 #u s e m e t r o p o l i s a l g o r i t h m t o f i n thh most

23
52 #p r o b a b l i y s t a t e s
53 f o r x in range (50) :
54 j=np . random . r a n d i n t ( 0 ,N)
55 i=np . random . r a n d i n t ( 0 ,N)
56
57 DeltaE=−2∗Energy ( s p i n , J , i , j ,H)
58
59 f=np . exp ( −( DeltaE ) / ( 0 . 4 ∗T) )
60 i f DeltaE <= 0 . :
61 s p i n [ i ] [ j ] ∗=−1
62
63 e l i f f >= uni for m ( 0 , 1 ) :
64 s p i n [ i ] [ j ] ∗=−1
65
66 return ( spin )
67 #================================================================
68 # Compute The mean m a g n e t i z a t i o n v a r i n g t e m p e r a t u r e
69 #================================================================
70 d e f m e a n m a g n e t i z a t i o n ( J ,H) :
71 h=[]
72 m= [ ]
73
74 s t e p s =500 #numer o f r e p e t i t i o n s
75
76 #computes t h e mean m a g n e t i z a c i o n u s i n g t h e m i c r o s t a t e s
77 #g i v e n by t h e m e t r o p o l i s a l g o r i t h m
78 for T in linspace (0.1 ,15 ,400) :
79 M=0.
80 s p i n=IMS ( )
81 f o r k i n r a n g e ( s t e p s ) : #c o n d i t i o n o f e q u i l i b r i u m
82 main (T, H, J , s p i n )
83 f o r k i n r a n g e ( s t e p s ) : #computaion o f mean m a g n e t i z a t i o n
84 main (T, H, J , s p i n )
85 M+=Mag( s p i n ,N)
86 h . append (T)
87 m. append (M/ ( s t e p s ∗N∗N) )
88
89 r e t u r n ( h ,m)
90
91
92
93 #================================================================
94 # Plot fo the h y s t e r e s i s curve
95 #================================================================
96 N=100 #s i z e o f t h e m a t r i z
97 H=0.
98 h ,m=m e a n m a g n e t i z a t i o n ( 1 ,H)
99 xl im ( 0 , 1 5 )
100 yl im ( amin (m) −0.1 ,amax (m) +0.1)
101 t i t l e ( ’ H y s t e r e s i s Curve ’ )
102 y l a b e l ( r ’$M$ ’ )
103 x l a b e l ( r ’ $H$ ’ )

24
104 p l o t ( h ,m, ’ . ’ )
105 s a v e f i g ( ’ phase . png ’ )
106 show ( )

Code.6: Computer program for showing the magnetic states


1 from p y l ab import ∗
2 from random import uni for m
3 import random
4 from s c i p y . o p t i m i z e import c u r v e f i t
5 import numpy a s np
6
7 #================================================================
8 # I n i t i a l Microstate
9 #================================================================
10
11 d e f IMS ( ) :
12 #a r r a y f o t t h e c a s e S=1/2
13 s p i n=r e s h a p e ( a r r a y ( [ random . c h o i c e ( [ 1 . / 2 , − 1 . / 2 ] ) f o r i i n r a n g e (N∗N) ] ) , (N,N)
)
14 return spin
15
16
17 #================================================================
18 # Boundary C o n d i t i o n s
19 #================================================================
20 d e f BC( i ) :
21 i f i +1 > (N−1) :
22 return 0
23 e l i f ( i −1) < 0 :
24 r e t u r n (N−1)
25 else :
26 return i
27
28
29 #================================================================
30 # Energy Computation
31 #================================================================
32
33 d e f Energy ( s p i n , J , i , j ,H) :
34 E=−J∗ s p i n [ i ] [ j ] ∗ ( s p i n [BC( i −1) ] [ j ]+ s p i n [BC( i +1) ] [ j ] +
35 s p i n [ i ] [ BC( j +1)]+ s p i n [ i ] [ BC( j −1) ] ) − s p i n [ i ] [ j ] ∗H
36 return E
37
38
39 #================================================================
40 # M a g n e t i z a t i o n Computation
41 #================================================================
42 d e f Mag( s p i n ,N) :
43 M=sum ( s p i n )
44 r e t u r n (M)

25
45
46 #================================================================
47 # Loop Monte C a r l o P r i n c i p a l
48 #================================================================
49
50 d e f main (T, H, J , s p i n ) :
51 #u s e m e t r o p o l i s a l g o r i t h m t o f i n thh most
52 #p r o b a b l i y s t a t e s
53 f o r x in range (50) :
54 j=np . random . r a n d i n t ( 0 ,N)
55 i=np . random . r a n d i n t ( 0 ,N)
56
57 DeltaE=−2∗Energy ( s p i n , J , i , j ,H)
58
59 f=np . exp ( −( DeltaE ) / ( 0 . 4 ∗T) )
60 i f DeltaE <= 0 . :
61 s p i n [ i ] [ j ] ∗=−1
62
63 e l i f f >= uni for m ( 0 , 1 ) :
64 s p i n [ i ] [ j ] ∗=−1
65
66 return ( spin )
67 #================================================================
68 # Compute The mean m a g n e t i z a t i o n v a r i n g t e m p e r a t u r e
69 #================================================================
70 d e f m e a n m a g n e t i z a t i o n ( J , H, T) :
71 s t e p s =500 #numer o f r e p e t i t i o n s
72
73 #computes t h e mean m a g n e t i z a c i o n u s i n g t h e m i c r o s t a t e s
74 #g i v e n by t h e m e t r o p o l i s a l g o r i t h m
75 M=0.
76 s p i n=IMS ( )
77
78 f o r k i n r a n g e ( s t e p s ) : #c o n d i t i o n o f e q u i l i b r i u m
79 main (T, H, J , s p i n )
80
81 f o r k i n r a n g e ( s t e p s ) : #computaion o f mean m a g n e t i z a t i o n
82 main (T, H, J , s p i n )
83
84 return spin
85
86
87
88 #================================================================
89 # Plot fo the h y s t e r e s i s curve
90 #================================================================
91 N=100 #s i z e o f t h e m a t r i z
92 H= 0 . ; J=1
93 for T in [ 0 . 5 , 1 . 5 , 2 . 5 , 3 . 5 , 4 . 5 , 5 . 5 ] :
94 s p i n=m e a n m a g n e t i z a t i o n ( J , H, T)
95 figure ()
96 imshow ( s p i n )

26
97 t i t l e ( ’T=%.2 f ’ %(T) )
98 s a v e f i g ( ’ %.2 f s p a n s h o t . png ’ %(T) )
99
100 show ( )

27

You might also like