Flipping state: Metropolis in Ising model

Click For Summary

Discussion Overview

The discussion revolves around coding issues related to implementing the Ising model in C++. Participants are addressing problems with generating a lattice, extracting values from it, and calculating changes in energy and magnetization when spins are flipped.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their code structure for generating a lattice and flipping spins based on energy calculations, but expresses confusion about specific lines of code.
  • Another participant points out that the code does not compile due to mismatched braces and undefined variables, questioning the meaning of certain expressions and variables like N, J, and spin.
  • A later reply acknowledges the messy code and suggests breaking it into smaller portions for clarity, specifically focusing on generating the lattice and extracting values from it.
  • Participants discuss the calculation of changes in magnetization and neighbor interactions, with one participant noting that their output for delta_neighbour is not displayed as expected.

Areas of Agreement / Disagreement

Participants generally agree that the code has issues that need addressing, but there is no consensus on the specific solutions or the correctness of the proposed code snippets.

Contextual Notes

Limitations include unclear definitions of variables such as N and J, potential issues with the random number generation method, and unresolved questions about the logic used in calculating delta_neighbour.

unscientific
Messages
1,728
Reaction score
13

Homework Statement



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.

Homework 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.

The Attempt at a Solution



Code:
#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;
       
        }

	

}
 
Physics news on Phys.org
bumpp
 
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; ?
 
D H said:
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; ?

Ok, I've realized 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:
//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:
//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..
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
9K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 6 ·
Replies
6
Views
5K