-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsing.cpp
More file actions
65 lines (58 loc) · 1.54 KB
/
Copy pathIsing.cpp
File metadata and controls
65 lines (58 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
using namespace std;
// constant
double J = 1;
double beta = 1;
int n = 10;
random_device rnd;
mt19937 mt(rnd());
uniform_real_distribution<> udist(0.0, 1.0);
uniform_int_distribution<> uidist(0, n - 1);
double E(vector<vector<int>> &s, double h) // calculate energy
{
vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
double inter = 0, sm = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
sm += s[i][j];
for (int d = 0; d < 4; d++)
{
int ni = (i + dx[d] + n) % n, nj = (j + dy[d] + n) % n;
inter += s[i][j] * s[ni][nj];
}
}
}
return -J / 2 * inter - h * sm; // 相互作用項は2回足しているので /2 をする
}
int main()
{
cout << "Ising" << endl;
int niter = 15000;
int report_interval = 100;
double h = -0.4;
vector<vector<int>> s(n, vector<int>(n, 1));
int cnt = n * n; // +1の数
for (int i = 0; i < niter; i++)
{
if (i > 0 && i % report_interval == 0)
{
cout << h << " " << cnt << " ";
h -= 0.02;
}
int x = uidist(mt), y = uidist(mt); //ランダムにサイトを選択
cnt -= s[x][y];
s[x][y] = 1;
double Ep = E(s, h);
s[x][y] = -1;
double Em = E(s, h);
double p = exp(-beta * Ep) / (exp(-beta * Ep) + exp(-beta * Em));
if (udist(mt) < p)
{
s[x][y] = 1;
}
cnt += s[x][y];
}
cout << endl;
}