13.5 The Metropolis Algorithm and the Two-dimensional Ising Model 439
magnetisation. The new values are used to update the averages computed in the main func-
tion.
When setting up the values of the spins it can be useful to havea visualization of the lattice,
as shown for the 7 × 7 lattice of Fig. 13.4.
✒
✒
✒
✒
✒
✒
✒
✒
✒
✠
✒
✒
✒
✠
✒
✠
✒
✠
✒
✒
✠
✒
✒
✒
✠
✒
✒
✒
✒
✠
✒
✠
✒
✒
✠
✒
✠
✒
✒
✠
✒
✠ ✠
✠ ✠ ✠ ✒ ✠ ✠
0 1 2 3 4 5 6
0 1 2 3 4 5 6
Fig. 13.4Example of a two-dimensional 7 × 7 lattice with spins pointing either up or down. The variable
spin_matrix[1][0]takes the value +1 whilespin_matrix[0][6]is− 1.
Another important function is the functioninitialize. This function sets up the initial en-
ergy, magnetisation and spin values for the different lattice positions. The latter sets all spins
equal one if the temperature is low, which for the two-dimensional Ising model means prac-
tically temperaturesT< 1. 5. Else, it keeps the value from the preceeding temperature. The
latter is done in order to get a best possible estimate of the most likely state for the given
temperature.
We have built up a code where we run over a larger temperature span, typically with values
T∈[ 1. 0 , 3. 0 ].
// function to initialise energy, spin matrix and magnetization
voidinitialize(intn_spins,doubletemp,int**spin_matrix,
double& E,double& M)
{
// setup spin matrix and intial magnetization
for(inty =0; y < n_spins; y++){
for(intx= 0; x < n_spins; x++){
if(temp < 1.5) spin_matrix[y][x] = 1;// spin orientation for the ground state
M += (double) spin_matrix[y][x];
}
}
// setup initial energy