|
-
April 11th, 2014, 12:14 PM
#15
Re: Ising Model C++ Metropolis Algorithm
I'm implying boundary conditions on this part:
Code:
int delta_neighbour = spin(x-1, y) + spin(x+1, y)+ spin(x, y-1) + spin(x, y+1);
by using:
Code:
if(x<0) x += LatSize;
if(x>=LatSize) x -= LatSize;
if(y<0) y += LatSize;
if(y>=LatSize) y -= LatSize;
So if you choose a site that's along the sides, it ensures continuity. For example, if Site (0,0) top most left is chosen, the right neighbour would be (0,1), the left neighbour would be (0,4), the top neighbour would be (4,0), the bottom neighbour would be (1,0).
However, when I run the code, it doesn't work, and sometimes it shows absurd values like the left neighbour having value of 25!
No. There is no implied boundary conditions in your code. That is why you are getting absurd values. The boundary conditions need to be part of spin() - so spin() needs to be function instead of just a define.
I've re-worked your code to base it upon a class.
Code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
const int LatSize = 5;
class cSpin
{
public:
int operator()(int x, int y)
{
if (x < 0)
x += LatSize;
if (x >= LatSize)
x -= LatSize;
if (y < 0)
y += LatSize;
if (y >= LatSize)
y -= LatSize;
return matrix[x][y];
}
void generate()
{
for (int i = 0; i < LatSize; i++)
for (int j = 0; j < LatSize; j++)
matrix[i][j] = (rand() % 2) * 2 - 1;
}
void display()
{
for (int i = 0; i < LatSize; i++) // loop 3 times for three lines
{
for (int j = 0; j < LatSize; j++) // loop for the three elements on the line
cout << setw(2) << matrix[i][j] << " "; // display the current element out of the array
cout << endl; // when the inner loop is done, go to a new line
}
}
private:
int matrix[LatSize][LatSize];
};
int main()
{
cSpin spin;
srand (static_cast<unsigned> (time(0)));
spin.generate();
spin.display();
int x = int (rand() % LatSize);
int y = int (rand() % LatSize);
int delta_M = -2 * spin(x, y); //change in magnetization energy
int delta_neighbour = delta_M * (spin(x - 1, y) + spin(x + 1, y) + spin(x, y - 1) + spin(x, y + 1));
cout << "The site chosen is at (" << x << ", " << y << ") = " << spin(x, y) << endl;
cout << "The left neighbour has spin = " << spin(x, y - 1) << endl;
cout << "The right neighbour has spin = " << spin(x, y + 1) << endl;
cout << "The above neighbour has spin = " << spin(x - 1, y) << endl;
cout << "The below neighbour has spin = " << spin(x + 1, y) << endl;
cout << "The change in neighbour energy = " << delta_neighbour << endl;
return 0;
}
Last edited by 2kaud; April 11th, 2014 at 12:19 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|