# Homework Help: Flipping state: Metropolis in Ising model

1. Apr 8, 2014

### unscientific

1. The problem statement, all variables and given/known data

Hi everyone, I'm having some difficulties with my code:

1. Randomly Choose a lattice at position (x, y) within the NxN lattice
2. Why by writing "int x = int(drand48()*L);" and "int y = int(drand48()*L);" it doesn't extract the value stored at that location (x, y)?
3. Calculate change in energy (dE) if flipped
4. If dE < 0, flip. If dE> 0, flip only with acceptance rate given by boltzmann exponential (which is between 0 and 1).

I'm referring to some possible codes already written online:

http://physics.clarku.edu/sip/programs/C/chapter17/ising.h
http://www.pages.drexel.edu/~cfa22/msim/node11.html

I'm still learning to code in C++, so there are many things I don't really get, so please bear with me.

2. Relevant equations

Here's the general structure of my code so far:

1. Generating a matrix
2. Assigning randomly +/- 1 to each site
3. Choosing a site randomly
4. If dE < 0, flip. If dE > 0, then flip only with acceptance rate given by boltzmann exponential.

3. The attempt at a solution

Code (Text):

#include <cstdlib>
#include <ctime>
using namespace std;
#include <iostream>
int main() //random generation of spin configuration
{
int L;              //Total number of spins L = NxN
double B=1;         //magnetic field
double M;           //Total Magnetization = Sum Si
double E;           //Total Energy
int T = 1.0;
int nsweeps = 100;      //number of sweeps
int de;             //change in energy when flipped
double Boltzmann;       //Boltzmann factor
int x,y;            //randomly chosen lattice site
int i,j,a,c;            //counters
int ROWS = 5;
int COLS = 5;
int matrix[ROWS][COLS];
srand ( static_cast<unsigned> ( time ( 0 ) ) );
for ( int i = 0; i < ROWS; i++ )
{
for ( int j = 0; j < COLS; j++ )
{
matrix[i][j] = rand () % 2 *2-1;
}
}

// showing the matrix on the screen
for(int i=0;i<ROWS;i++)  // loop 3 times for three lines
{
for(int j=0;j<COLS;j++)  // loop for the three elements on the line
{
cout<<matrix[i][j];  // display the current element out of the array
}
cout<<endl;  // when the inner loop is done, go to a new line
}
return 0;  // return 0 to the OS.

//boundary conditions and range
if(x<0) x += N;
if(x>=L) x -= N;
if(y<0) y += N;
if(y>=L) y -= N;

//counting total energy of configuration
{  int neighbour = 0;    // nearest neighbour count

for(int i=0; i<L; i++)
for(int j=0; j<L; j++)
{  if(spin(i,j)==spin(i+1, j))     // count from each spin to the right and above
neighbour++;
else
neighbour--;
if(spin(i, j)==spin(i, j+1))
neighbour++;
else
neighbour--;
}

E = -J*neighbour - B*M;

//flipping spin
int x = int(drand48()*L);   //retrieves spin from randomly choosen site
int y = int(drand48()*L);

int delta_M = -2*(x, y);    //calculate change in Magnetization M
int delta_neighbour = (x-1, y) + spin(x+1, y)+ spin(x, y-1) + spin(x, y+1);
int delta_neighbour = -2*(x,y)* int delta_neighbour;

double delta_E = -J*delta_neighbour -B*delta_M;

//flip or not
if (delta_E<=0)
{  (x, y) *= -1;     // flip spin and update values
M += delta_M;
E += delta_E;

}

}

2. Apr 9, 2014

bumpp

3. Apr 9, 2014

### D H

Staff Emeritus
Your code doesn't even compile. The braces don't match. What's N? What's J? What's spin?
What's this: int delta_neighbour = -2*(x,y)* int delta_neighbour; ?

4. Apr 10, 2014

### unscientific

Ok, I've realised my code is too messy so I've decided to break the code into portions, each portion doing one thing. Let's start with these two portions:

1. Generate NxN lattice with -1 or +1 at each site randomly.
2. How to extract the value at a site at location (x, y).

Part 1 of code:

Code (Text):

//Generate NxN lattice with -1 or +1 at each site randomly.

#include <stdlib.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

#define spin(a, b) (matrix[(a)][(b)])  //function spin (a, b) extracts value of site at location (a, b)

int main()
{

const int LatSize = 5;
const int L = LatSize * LatSize;
int matrix[LatSize][LatSize];

srand ( static_cast<unsigned> ( time ( 0 ) ) );
for ( int i = 0; i < LatSize; i++ )
{
for ( int j = 0; j < LatSize; j++ )
{
matrix[i][j] = rand () % 2 *2-1;
}
}

//End of Generating matrix

Part 2 of code:
Code (Text):

//Extracting value of a site at location (x,y)

int x = int (rand()*LatSize);
int y = int (rand()*LatSize);

int delta_M = -2*spin(x, y); //change in magnetization energy

int delta_neighbour = spin(x-1, y) + spin(x+1, y)+ spin(x, y-1) + spin(x, y+1);
delta_neighbour = -2*spin(x,y)* delta_neighbour; //change in neighbour product energy

cout << delta_neighbour;

//End of extracting value of site at (x, y)

return 0;
}

Doesn't seem to work, as it doesn't display the value of delta_neighbour..