Flipping state: Metropolis in Ising model

In summary, the conversation was about difficulties with code and understanding certain concepts in C++. The code mentioned involved generating a matrix with randomly assigned values and extracting the value at a specific location on the matrix. The attempt at a solution involved breaking the code into smaller portions, but it still didn't work.
  • #1
unscientific
1,734
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
  • #2
bumpp
 
  • #3
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
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..
 
  • #5


Hello, it seems like you are working on an Ising model simulation. I am not familiar with coding in C++, but I can provide some general advice to help you with your code.

First, it is important to make sure that your code is properly structured and organized. This will make it easier for you to troubleshoot any errors and for others to understand your code. You can try breaking your code into smaller functions that perform specific tasks, such as generating the lattice, calculating energy, and flipping spins.

Regarding your specific questions, it is difficult to say exactly why your code is not working without seeing the full code and understanding the specific errors you are encountering. However, here are some general tips:

1. It is important to initialize your variables before using them. In your code, you declare variables such as L, B, M, E, etc, but you do not assign any initial values to them. This can lead to unexpected behavior in your code. Make sure to assign initial values to all your variables before using them in calculations.

2. When using random numbers, it is important to make sure that the range of numbers you are generating is correct. For example, in your code you use the function drand48() to generate a random number between 0 and 1. However, when you multiply this by L, which is the size of your lattice, the range of numbers will be much larger than just 0 to 1. You may want to use a different function or approach to generate random numbers within the desired range.

3. Make sure to use proper syntax when accessing elements of your matrix. In your code, you have lines such as (x, y) *= -1; and (x-1, y) + spin(x+1, y)+ spin(x, y-1) + spin(x, y+1); which do not follow the correct syntax for accessing elements in a matrix. You may want to review the proper syntax for accessing elements in a matrix in C++.

4. Finally, make sure to carefully check your calculations and logic. For example, in your code you have lines such as int delta_neighbour = (x-1, y) + spin(x+1, y)+ spin(x, y-1) + spin(x, y+1); which do not seem to be calculating the change in energy correctly. Make sure to carefully think through your calculations and double check them to make sure they are correct
 

FAQ: Flipping state: Metropolis in Ising model

1. What is the Ising model?

The Ising model is a mathematical model used to study the behavior of a system of interacting spins, which can represent magnetic moments of atoms or molecules. It was originally developed to explain the phase transition of ferromagnetic materials, but has since been applied to various other systems in physics, chemistry, and biology.

2. What is "flipping state" in the Ising model?

"Flipping state" refers to the process of changing the spin (magnetic moment) of a single particle in the system from one direction to the opposite direction. This change can affect the overall behavior and properties of the system, such as its energy and magnetization.

3. What is the Metropolis algorithm in the context of the Ising model?

The Metropolis algorithm is a Monte Carlo simulation method used to calculate the equilibrium state of a system. In the Ising model, it is used to determine whether the flipping of a spin will be accepted or rejected based on the change in energy of the system. This algorithm is essential in studying the dynamics and behavior of the Ising model.

4. How does the flipping state affect the overall behavior of the Ising model?

The flipping state of individual spins can have a significant impact on the behavior of the Ising model. For example, in the ferromagnetic phase, where most spins are aligned in the same direction, a single flipped spin can lead to a domino effect, causing a large portion of the spins to flip and changing the overall magnetization of the system.

5. What are some applications of the Ising model and its flipping state in real-world systems?

The Ising model and its flipping state have been applied to various systems in physics, chemistry, and biology, such as studying the behavior of magnetic materials, protein folding, and social interactions. It has also been used in machine learning and optimization problems, as well as in understanding the behavior of complex systems in economics and finance.

Similar threads

Replies
7
Views
2K
Replies
9
Views
2K
Replies
2
Views
1K
Replies
5
Views
2K
Replies
23
Views
7K
Replies
6
Views
4K
Back
Top