the ising model of a ferromagnet

30
 1 The Ising Model of a Ferromagnet Part II Computational Physics Project  Abstract Ferromagnetism was investigated numerically by using a Monte-Carlo technique with the Metropolis algorithm. The Ising model was used as a simplified model for the spin intera ctions. The time evolution of magnetisation towards equilibrium was investigated with various initial conditions at different temperatures. From this, it was decided to initialise the simulation with a uniform set of spin configurations. Next, the magnetisation, ene rgy and heat capacity (derived from two different approaches: the derivative of energy with respect to temperature and the fluctuation-dissipation theorem) as a function of temperature we re studied. Their plots showed a sudden tra nsition in their behaviour at a particular temp erature, called the cr itical temperature. The critical temperature was deduced from the plots and the values were   ,  and  respectively. These estimates were compared with Onsager’s tour de force analytical expression  of the critical temperature and they were consistent within thei r random errors in the simulation. The critical exponent was then d etermined from t he relationship between magnetisation and temperature near the critical temperature.  The critical exponent was found to be . The behaviour of a ferromagnet in an applied magn etic field was then investigated and shown to be hysteretic. The size of the hysteresis loo p decreased as the temperature was increased until the critical point where it became non-h ysteretic. 1. Introduction Ferromagnetism is a n intrinsically many-body quantum phenomenon. As in typical many-body quantum situations, analytic solutions a re rare. However, many aspects of ferr omagnetism can be investigated numerically. The Ising model provides a simplified model for the spin-spin interactions that captures the essence of the behaviour. The purposes of this project are to simulate a 2D ferromagnetic system using the Ising model and study the behaviour, in the absence of the magnetic field, of magnetisation and energy as a function of temperature from which the critical temperature can be d educed. Also, we will estimate the heat capacity of the system by two various approaches: the derivative of energy with respect to temperature and t he fluctuation-dissipation theorem. These give a better estimate of the critical tempe rature. Furthermore, we will focus on the behaviour near the critical temperature and finally investigate the behaviour of the system when the magnetic field is turned on. The analysis of the computational aspects of the problem is presented in the next section. The implementation of the al gorithm, along with the overall a pproach to obtain the solutions of the problem, is then given in section 3 . The results and discussions are summarised in section 4. The conclusions are then presented in section 5, following by the references in section 6.

Upload: jirawit-ratanapreechachai

Post on 02-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 1/30

  1

The Ising Model of a Ferromagnet

Part II Computational Physics Project

 Abstract

Ferromagnetism was investigated numerically by using a Monte-Carlo technique

with the Metropolis algorithm. The Ising model was used as a simplified model

for the spin interactions. The time evolution of magnetisation towards

equilibrium was investigated with various initial conditions at different

temperatures. From this, it was decided to initialise the simulation with a

uniform set of spin configurations. Next, the magnetisation, energy and heat

capacity (derived from two different approaches: the derivative of energy with

respect to temperature and the fluctuation-dissipation theorem) as a function of

temperature were studied. Their plots showed a sudden transition in their

behaviour at a particular temperature, called the critical temperature. The

critical temperature was deduced from the plots and the values were  ,  and  respectively. These

estimates were compared with Onsager’s tour de force analytical expression of

the critical temperature and they were consistent within their random errors in

the simulation. The critical exponent was then determined from the relationship

between magnetisation and temperature near the critical temperature.  The

critical exponent was found to be . The behaviour of a ferromagnet in an

applied magnetic field was then investigated and shown to be hysteretic. The

size of the hysteresis loop decreased as the temperature was increased until the

critical point where it became non-hysteretic. 

1. Introduction

Ferromagnetism is an intrinsically many-body quantum phenomenon. As in

typical many-body quantum situations, analytic solutions are rare. However,

many aspects of ferromagnetism can be investigated numerically. The Ising

model provides a simplified model for the spin-spin interactions that captures

the essence of the behaviour.

The purposes of this project are to simulate a 2D ferromagnetic system using the

Ising model and study the behaviour, in the absence of the magnetic field, of

magnetisation and energy as a function of temperature from which the criticaltemperature can be deduced. Also, we will estimate the heat capacity of the

system by two various approaches: the derivative of energy with respect to

temperature and the fluctuation-dissipation theorem. These give a better

estimate of the critical temperature. Furthermore, we will focus on the

behaviour near the critical temperature and finally investigate the behaviour of

the system when the magnetic field is turned on.

The analysis of the computational aspects of the problem is presented in the next

section. The implementation of the algorithm, along with the overall approach to

obtain the solutions of the problem, is then given in section 3. The results and

discussions are summarised in section 4. The conclusions are then presented insection 5, following by the references in section 6.

Page 2: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 2/30

  2

2. Analysis

2.1 Ising Model

In the Ising model, only the nearest neighbour spin-spin interactions are

considered. In a two-dimensional  lattice, the energy E of the system can

then be written as

 

(1)

where the sum  is over all the nearest neighbour pairs of spins only,  is the exchange energy, is the magnetic moment, and is the applied magnetic field.

The spin values

 are taken to be

 here.

It is easiest to use periodic boundary conditions, so that each lattice site,including those on the edges, has 4 nearest neighbours in two dimensions. Thefact that the spins on the lattice edges do not have 4 nearest neighbours will

introduce artefacts, which do not concern us at this level.

The value of  will be optimised such that the program does not takeunreasonably long to compute but can still represent the statistical behaviour of

a macroscopic system.

If we measure E in units of J , equation (1) becomes

 

 

(2) 

where  .

Equation (2) simplifies in the absence of the magnetic field H as

 

(3)

The magnetisation M is given in units where it is defined by

 

(4)

Page 3: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 3/30

  3

2.2 Metropolis Algorithm

We will develop a Monte-Carlo code using the Metropolis algorithm toinvestigate the behaviour of the Ising model.

Given an initial set of spin orientations, the system is evolved in time by theMetropolis algorithm. Considering the energy  required to flip a particular

spin, if , flip the spin. Otherwise, flip the spin only if

 

(5)where  is a uniform random number in the range [ ], and is the Boltzmann constant.

If we measure  in units of J  and  in units of . In other words,        

(6)Therefore, by substitution, equation (5) becomes

 

(7)

Next, this is repeated for all the spins and these steps together are regarded asa single time step. After these step are performed, the spin system is evolvedin time by one time step. 

We need to run the simulation for many time steps to ensure that it approaches a

near-equilibrium point.

Note that there is a choice of initial spin orientations with which to start the

simulation. Both uniform and random sets of spin orientations should work.

The effects of initial conditions will be investigated when the time evolution of

magnetisation towards equilibrium is studied.

2.3 Heat capacity

The heat capacity C  of the system can be estimated by two different approaches.

Firstly, from the definition

 

(8)

Page 4: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 4/30

  4

Secondly, from the fluctuation-dissipation theorem

 

(9)

where 

(∑ ̅)  

∑ ̅    

( is the number of samples used to calculate the average and standarddeviation)

The heat capacity, in principle, diverges at the critical temperature. In other

words, equation (9) implies that the fluctuation of the energy diverges at thecritical temperature.1

2.4 Behaviour near the critical temperature

Near the critical temperature  (from below i.e. ), the magnetisation M

is expected to vary as

 

(10)where  is a constant.

From equation (10), (11)

Therefore, a plot of  against  will be a straight line with gradient.

We will denote

 as

 and

 as

 in all the following sections because all the

calculations and measurements will be done in these reduced units.

2.5 Behaviour when  

The presence of two equilibrium states below the critical temperature gives rise

to the hysteretic behaviour. When , one of the equilibrium states is meta-stable and the other is stable.2

Therefore, the code will be written to show this kind of behaviour by looping the

magnetic field from some value

 to

– . Note that the spin array cannot be re-

initialised during the magnetic field looping in order to get hysteresis loops.

Page 5: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 5/30

  5

3. Implementation

3.1 Overall approach

The ferromagnetic system is represented by a set of spins on an  

lattice. This will be denoted by a C++ vector of size

. The indexing of the

vector is done such that the spin in the  row and  column of the lattice isstored in the  argument of the vector. Keeping in mind that the first

row and first column are denoted by  and   respectively.

The random number p , needed in equation (4), is generated from the GSL

random number generator code. Therefore, h GSL hdr fi ‘gsl/gsl_rng.h’ ineeded for this purpose.

The spin configurations are initialised by the function ‘initial_uniform’ , which

assigns  to all the spins, or the function ‘initial_random’  , which assigns  

randomly to all the spins by means of the random number generator function

‘random_uniform’ .

The magnetisation is calculated by the function ‘magnetisation’  , which simply

takes the sum of all the spin values. The system energy is calculated from

equation (2) by the function ‘energy ’  and the energy required to flip a particular

spin is computed by the function ‘energydiff ’ .

The spin system is evolved in time according to the Metropolis algorithm as

described in section 2.2 by the function ‘metropolis’  , which decides whether or

not to flip the spin at a particular lattice site. This is done for all the  lattice

sites by going through a regular sequence. All these  operations are regardedas a single time step. We need to run the simulation for many time steps to

ensure it reaches equilibrium.

3.2 Time evolution of magnetisation towards equilibrium (  

This is done by the code ‘isinguniform.cc’ , which assigns an initial uniform set of

spins and then evolves the system by 10,000 – 20,000 time steps until it reaches

equilibrium, or by the code ‘isingrandom.cc’  , which is identical to ‘isinguniform.cc’  

except that it assigns an initial random set of spins to the system

The value of magnetisation is recorded at the start of every time step

3.3 Magnetisation and energy as a function of temperature

This is done by the code ‘isingtemp.cc’ . This evolves the system by 6,000 time

steps first and then evolves it further another 1,000 time steps. With the last

1,000 time step, the values of magnetisation and energy are recorded and

averaged to give the mean magnetisation and energy at a particular temperature

After the total 7,000 steps, the temperature is changed slightly and the whole

process is repeated again at a slightly different temperature.

Page 6: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 6/30

  6

3.4 Heat capacity as a function of temperature

This is done by the code ‘isingheat.cc’ . The steps in ‘isingtemp.cc’  are repeated to

obtain the mean energy at each temperature. In addition to the average of the

energy values, the average of the squares of the energy values is also taken. The

heat capacity at each temperature can then be easily calculated by usingequations (8) and (9)

3.5 Behaviour near the critical temperature

The data outputted from the program ‘isingtemp.cc’  are forwarded to Microsoft

Excel to perform data analysis in order to find the critical exponent. Note that

only the values close to the critical temperature concern us in this section.

3.6 Behaviour when  

This is done by the code ‘isingmagnet.cc ’    . This is essentially the same as‘isingtemp.cc ’     , but the magnetic field is being varied instead of the temperature.

The mean magnetisation at each magnetic field strength is therefore calculatedin the same fashion as in ‘isingtemp.cc .’ 

4. Results and Discussion

4.1 Preliminary tests for the program

Firstly, the value of N was set to some small value, 10 in my case. The indexing of

the spin vector was checked by printing out the values contained in it as an array

of size , that is, starting a new line every N values.

Next, The system energy and magnetisation were computed for some simple spin

configurations. For example, if all the spins are aligned with each other in the

absence of the magnetic field, the magnetisation is . Each nearest neighbour

interaction will contribute 1 to the system energy. Because there are  

interactions, the total energy is  in units of J . Indeed, this is the minimum

energy in the absence of the field.

The time evolution of magnetisation in the absence of the magnetic field was

then tested for some simple cases. The magnetisation is approximately  atlow temperatures, while it is roughly 0 at high temperatures. By ‘low’ or ‘high’,

we can use Onsager’s expression for the critical temperature as a quick

indication.

4.2 Time evolution of magnetisation towards equilibrium (  

The spin system was evolved in time by 10,000 and 20,000 time steps,

depending on how long it takes to reach equilibrium, at the following

temperatures: 1.0, 1.5, 2.0, 2.5, 3.0 . At each temperature, the simulation

was initialised with both a uniform set and a random set of spin orientations.

These were done with  and the results are plotted below.

Page 7: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 7/30

  7

Figure 1: Time evolution of magnetisation at  for

initial uniform (above) and random (below) sets of spin configurations

Page 8: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 8/30

  8

Figure 2: Time evolution of magnetisation at  for

initial uniform (above) and random (below) sets of spin configurations

Page 9: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 9/30

  9

Figure 3: Time evolution of magnetisation at  for

initial uniform (above) and random (below) sets of spin configurations

Page 10: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 10/30

  10

Figure 4: Time evolution of magnetisation at  for

initial uniform (above) and random (below) sets of spin configurations

Page 11: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 11/30

  11

Figure 5: Time evolution of magnetisation at  for

initial uniform (above) and random (below) sets of spin configurations

Page 12: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 12/30

  12

At temperatures below the critical temperature, the system initialised with a

uniform set of spins reaches a near-equilibrium point significantly faster than

that initiallised randomly as shown in Figures 1-3. These all make sense because

most of the spins are aligned in the equilibrium state at these temperatures

At temperatures above the critical temperature, both initial conditions worksimilarly as shown if Figures 4-5. Note that at , the equilibrium

fluctuations are much larger than the fluctuations at the other temperatures.

This is because this temperature is close to the critical temperature.

Therefore, it was decided to start the simulation with a uniform set of spin

configurations.

4.3 Determination of the value of N

Using an initial uniform set of spins, the time evolution of magnetisation wasinvestigated for  and compared to the cases when  , which were

already given in section 4.2

Figure 6: Time evolution of magnetisation at  for an

initial uniform set of spin configurations

Page 13: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 13/30

  13

Figure 7: Time evolution of magnetisation at  for an

initial uniform set of spin configurations

Figure 8: Time evolution of magnetisation at  for aninitial uniform set of spin configurations

Page 14: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 14/30

  14

Figure 9: Time evolution of magnetisation at  for an

initial uniform set of spin configurations

Figure 10: Time evolution of magnetisation at  for aninitial uniform set of spin configurations

Page 15: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 15/30

  15

It can be seen that the time evolution of magnetisation when  and  

below the critical temperature are nearly the same. Above the critical

temperature, it seems that the magnetisation in the  case has smaller

fluctuations than the magnetisation in the  case. However, the times

required to reach a near equilibrium point are nearly identical. 

The major factor is that the compute time increases rapidly with N , so it was

decided that all the following simulations would be done with  in order to

keep the compute time of all the following programs reasonable.

4.4 Magnetisation & energy as a function of temperature

From section 4.2, we saw that 7,000 time steps were sufficient for the system to

reach a near equilibrium point. Therefore, the system was evolved by 7,000 time

steps and the data were collected during the last 1,000 time steps to find the

mean magnetisation and energy as described in section 3.3.

A plot illustrating how the mean magnetisation depends on temperature is given

in Figure 11.

Figure 11: Magnetisation as a function of temperature at   The

vertical lines are to help estimate the critical temperature.

The plot shows a sharp transition in the magnetisation at a particular

temperature called the critical temperature . As mentioned in section 4.2, the

random errors near the critical temperature are very large.

The critical temperature was then deduced from the plot. Such an attempt isillustrated in Figure 11, which gives the following result.

Page 16: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 16/30

  16

 Next, a plot of energy as a function of temperature is given in Figure 12.

Figure 12: System energy as a function of temperature. The vertical and

horizontal lines are to help estimate the critical temperature.

It can be seen that at low temperatures, the system energy approaches -1800 J

(i.e.  for ). This is expected for the reason explained in section .

As the temperature is increased, the energy rises increasingly faster until a

particular temperature is reached. After that the gradient of the plot decreases

continuously. This transitional temperature is identified as the critical

temperature.

 

 

Onsager’s tour de force analytical result gives

√   

This is consistent within the random errors with both the estimated values

obtained from the magnetisation and energy.

4.5 Heat capacity as a function of temperature

Page 17: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 17/30

  17

The heat capacity was derived from the two methods detailed in section 2.3.

These gave a better estimate of the critical temperature than the estimates made

from the previous plots because the critical temperature can be deduced from

the peak of the heat capacity plot, which can be measured more accurately.

A plot of the heat capacity from  as a function of temperature is given inFigure 13. 

Figure 13: Heat capacity from  as a function of temperature. The vertical

lines are to help identify the peak, which occurs at the critical temperature.

It can be seen that the heat capacity rises steeply to extremely high values near

the critical temperature. Also, the data are very noisy near the critical

temperature due to the theoretically divergent random errors. Despite the high

level of noise,  can still be determined from the plot quite accurately as the

peak is very sharp.   

Page 18: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 18/30

  18

Next, a plot of the heat capacity from the fluctuation-dissipation theorem as a

function of temperature is given in Figure 14. 

Figure 14: Heat capacity from the fluctuation-dissipation theorem as a function

of temperature. The vertical lines are to help identify the peak, which occurs at

the critical temperature.

The noise level now is lower but the peak is also broader. So it is more difficult

to locate the position of the peak accurately.

 

Both of these estimates are again consistent with Onsager’s expression 

 within their random errors.

Page 19: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 19/30

  19

4.6 Behaviour near the critical temperature

Figure 15: A plot of  against  for temperatures near the critical

temperature.

A plot of  against  is should be a straight line for  close enough to  from below. Here, .  Ignoring the divergent standard

deviation of the data as the system approaches the critical temperature, the plot

is a straight line. The critical exponent  is given by the gradient of the plot.

 

In principle,  is 0.125 for the two-dimensional Ising model.1

4.7 Effects of the magnetic field

The plots of magnetisation as a function of magnetic field are given at various

temperatures below.

Page 20: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 20/30

  20

Figure 16: Magnetisation as a function of applied magnetic field at .

Figure 17: Magnetisation as a function of applied magnetic field at

.

Page 21: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 21/30

  21

Figure 18: Magnetisation as a function of applied magnetic field at .

Figure 19: Magnetisation as a function of applied magnetic field at

.

Page 22: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 22/30

  22

Figure 20: Magnetisation as a function of applied magnetic field at .

A hysteresis loop is present below the critical temperature with the size of the

loop decreasing as the temperature increases towards

. At the critical point,

the loop disappears and it becomes a curve with an infinite gradient at the origin.As the temperature increases further, the gradient becomes finite and decreases

steadily.

4.8 Performance of the program

As mentioned above, the parameters of the program were optimised to reduce

the run time while still retain the accuracy of the data. Firstly, I experimented

with the initial conditions and chose to work with a uniform set of spins as it

reaches equilibrium with fewer numbers of time steps (refer to section 4.2).

Next, I chose a smaller lattice as it can still represent the essence of thebehaviour, while reduces the run time significantly. I began with 10,000 time

steps in the beginning but reduced it to 7,000 in subsequent calculations as they

are in equilibrium already.

isinguniform.cc, isingrandom.cc ~ 45 seconds

isingtemp.cc, isingtemp.cc ~ 3 hours for 300 temperatures

isingmagnet.cc ~5hours for 400 values of magnetic fields

Page 23: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 23/30

  23

5. Conclusions

1. In the absence of the field, there are two equilibrium states. Depending on the

initial conditions, the system will evolve into one of these states

2. The magnetisation of the system is non-zero below and zero above a particular

temperature called the critical temperature .

3.The fluctuations of the system are largest at .

4. The heat capacity has a peak at the critical temperature and this provides an

efficient way to measure the critical temperature.

5. Near , some parameter can be expressed in terms of , where  is

the corresponding critical exponent.

6. The presence of two equilibrium states is the origin of the hysteresis loop

when the magnetic field is varied from one direction to the opposite direction.

7. The hysteresis loop becomes smaller as the temperature is increased towardsthe critical temperature. At and above the critical temperature, the system

becomes non-hysteretic.

6. References

1. R.J. Needs & E.M. Terentjev. Experimental and Theoretical Physics NST Part 2.

Thermal and Statistical Physics Supplementary Course Material, Michaelmas

Term 2013, pp95-116

2. C. Castelnovo. Experimental and Theoretical Physics NST Part 2. Theoretical

Physics, Michaelmas Term 2013, pp52-60

Page 24: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 24/30

  24

Appendix: Program listing 

// Header files #include <iostream> #include <cmath> 

#include <vector> #include "gsl/gsl_rng.h" 

// Function to generate a random number using the GSL random number generatorcode double random_uniform( unsigned long int seed = 0 ){

static gsl_rng* rng = 0;if  ( rng == 0 )rng = gsl_rng_alloc( gsl_rng_default );

if  ( seed != 0 )

gsl_rng_set( rng, seed );return gsl_rng_uniform( rng );

// Function to calculate the total energy E for a given set of spin orientations // E/J = - sum_s_i_s_j - (A*sum_s_i) where A = uH/J // H is the magnetic field, u is the magnetic moment, J is the exchange energydouble energy( std::vector<int>& spin, int N, double A ){

int sum_spin = 0;for  (int i = 0; i < N; i++)

{for  (int j = 0; j < N; j++)

sum_spin += spin[(i*N) + j];}

int product_spin = 0;int down, right;for  (int i = 0; i < N; i++){for (int j = 0; j < N; j++){

if (i == N-1)

down = spin[j];else 

down = spin[N*(i+1) + j];if (j == N-1)

right = spin[i*N];else 

right = spin[(i*N) + (j+1)];product_spin += spin[(i*N) + j] * (right + down);

}}return - product_spin - (A*sum_spin);

}

Page 25: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 25/30

  25

// Function to calculate the energy required to flip a particular spindouble energydiff(std::vector<int> spin, int N, int i, int j, double A){

double E1, E2;E1 = energy(spin, N, A);if  (spin[(i*N) + j] == 1)

spin[(i*N) + j] = -1;else 

spin[(i*N) + j] = 1;E2 = energy(spin, N, A);return E2 - E1;

}

// Function to perform the flipping of a spinvoid metropolis(std::vector<int>& spin, int N, int i , int j,

double T, double deltaE, double p)

{if  (deltaE < 0){

if  (spin[(i*N) + j] == 1)spin[(i*N) + j] = -1;

else spin[(i*N) + j] = 1;

}else if  (deltaE > 0 and exp(-deltaE/T) > p){

if  (spin[(i*N) + j] == 1)spin[(i*N) + j] = -1;

else spin[(i*N) + j] = 1;

}}

// Function to initialise the spin array with all the elements +1 void initial_uniform( std::vector<int>& spin, int N ){for (int i = 0; i < N ; i++){for (int j = 0; j < N; j++)

spin[(i*N) + j] = 1;}

// Function to initialise the spin array with +-1 randomly void initial_random( std::vector<int>& spin, int N ){

double random;for (int i = 0; i < N ; i++){for (int j = 0; j < N; j++){random = random_uniform();

Page 26: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 26/30

  26

if (random < 0.5)spin[(i*N) + j] = 1;else if (random > 0.5)spin[(i*N) + j] = -1;

}}

// Function to calculate the magnetisation for a given set of spin orientations double magnetisation( std::vector<int>& spin, int N ){

double sum = 0.0;for (int i = 0; i < N; i++){for (int j = 0; j < N; j++)

sum += spin[(i*N) + j];

}return sum/(N*N);} 

Main for isinguniform.cc

int main(){

const int N = 30;// A = uH/J and let the "temperature" T = k_b*T'/J where k_b is the Boltzmann

constant // J is the exchange energy and T' is the actual temperature in Kelvin double A = 0.0, T = 1.5;double deltaE, p;int max = 10000;std::vector<int> spin(N*N);initial_uniform(spin, N);

//10000 time steps for  (int t = 0; t <= max; t++){

std::cout << t << " " << magnetisation(spin, N) << "\n";

for  (int i = 0; i < N; i++){for (int j = 0; j < N; j++){

deltaE = energydiff(spin, N, i, j, A);p = random_uniform();metropolis(spin, N, i, j, T, deltaE, p);

}}

}return 0;

Page 27: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 27/30

  27

Main for isingrandom.cc

int main(){

const int N = 30;

// A = uH/J and let the "temperature" T = k_b*T'/J where k_b is the Boltzmannconstant // J is the exchange energy and T' is the actual temperature in Kelvin double A = 0.0, T = 1.5;double deltaE, p;int max = 10000;std::vector<int> spin(N*N);initial_random(spin, N);

//10000 time steps for  (int t = 0; t <= max; t++){

std::cout << t << " " << magnetisation(spin, N) << "\n";for  (int i = 0; i < N; i++){for (int j = 0; j < N; j++){

deltaE = energydiff(spin, N, i, j, A);p = random_uniform();metropolis(spin, N, i, j, T, deltaE, p);

}}

}return 0;

Main for isingtemp.cc

int main(){

const int N = 30;// A = uH/J and let the "temperature" T = k_b*T'/J where k_b is the Boltzmann

constant // J is the exchange energy and T' is the actual temperature in Kelvin 

double A = 0.0, T_min = 1.0, T_max = 4.0, dT = 0.001;double sum_M, sum_E;double deltaE, p;int max = 7000, n = 1000;std::vector<int> spin(N*N);

for  (double T = T_min; T < T_max; T += dT){

initial_uniform(spin, N);sum_M = 0.0;sum_E = 0.0;

//7000 time steps at eact temperature T for  (int t = 0; t <= max; t++)

Page 28: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 28/30

  28

{if (t > (max-n)){

sum_M += magnetisation(spin, N);sum_E += energy(spin, N, A);

}for  (int i = 0; i < N; i++){

for (int j = 0; j < N; j++){

deltaE = energydiff(spin, N, i, j, A);p = random_uniform();metropolis(spin, N, i, j, T, deltaE, p);

}}

}std::cout << T << " " << sum_M/1000 << " " << sum_E/1000 << "\n";

}return 0;}

Main for isingheat.cc

int main(){

const int N = 30;// A = uH/J and let the "temperature" T = k_b*T'/J where k_b is the Boltzmann

constant // J is the exchange energy and T' is the actual temperature in Kelvin double A = 0.0, T_min = 2.0, T_max = 2.5, dT = 0.001;double sum_E, sumsq_E, E_i = 0.0, E_f = 0.0, C_FD;double deltaE, p;int max = 7000, n = 1000;std::vector<int> spin(N*N);

for  (double T = T_min; T < T_max; T += dT){

initial_uniform(spin, N);sum_E = 0.0;sumsq_E = 0.0;E_i = E_f;

//7000 time steps at each temperature T for  (int t = 0; t <= max; t++){

if (t > (max - n)){

sum_E += energy(spin, N, A);sumsq_E += energy(spin, N, A) * energy(spin, N, A);

}

for  (int i = 0; i < N; i++){

Page 29: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 29/30

  29

for (int j = 0; j < N; j++){

deltaE = energydiff(spin, N, i, j, A);p = random_uniform();metropolis(spin, N, i, j, T, deltaE, p);

}}

}E_f = sum_E/n;C_FD = (sumsq_E - (n*E_f*E_f))/((n - 1)*T*T);if (T > (T_min + 0.5*dT))std::cout << T << " " << (E_f - E_i)/dT << " " << C_FD << "\n" ;

}return 0;

Main for isingmagnet.cc

int main(){

const int N = 30;// A = uH/J and let the "temperature" T = k_b*T'/J where k_b is the Boltzmann

constant // J is the exchange energy and T' is the actual temperature in Kelvin double T = 1.5, sum_M, A_0 = 1.0, dA = 0.01;double deltaE, p;int max = 7000, n = 1000;

std::vector<int> spin(N*N);initial_uniform(spin, N);

for  (double A = A_0; A > -A_0; A -= dA){

sum_M = 0.0;

//7000 time steps at each temperature T for  (int t = 0; t <= max; t++){

if (t > (max - n))sum_M += magnetisation(spin, N);

for  (int i = 0; i < N; i++){

for (int j = 0; j < N; j++){

deltaE = energydiff(spin, N, i, j, A);p = random_uniform();metropolis(spin, N, i, j, T, deltaE, p);

}}

}std::cout << A << " " << sum_M/1000 << "\n";

}for  (double A = -A_0; A < A_0; A += dA){

Page 30: The Ising Model of a Ferromagnet

8/10/2019 The Ising Model of a Ferromagnet

http://slidepdf.com/reader/full/the-ising-model-of-a-ferromagnet 30/30

sum_M = 0.0;

//10000 time steps at each temperature Tfor  (int t = 0; t <= max; t++){

if (t > (max-n))sum_M += magnetisation(spin, N);

for  (int i = 0; i < N; i++){

for (int j = 0; j < N; j++){

deltaE = energydiff(spin, N, i, j, A);p = random_uniform();metropolis(spin, N, i, j, T, deltaE, p);

}}

}

std::cout << A << " " << sum_M/1000 << "\n";}

return 0;}