-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathising_2d.cpp
199 lines (140 loc) · 4.02 KB
/
ising_2d.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <fstream>
using namespace std;
#define L 10 // The size of the lattice
// here goes the functions for my 16807 generator
float rnd(int seed)
{
/* linear congruential random number generator with shuffling */
/* based on ran1 of second edition of "Numerical Recipes" */
#define min(a,b) ((a) < (b) ? (a) : (b))
#define a 16807
#define m 2147483647 /* 2^31 - 1 */
#define rm 1.0/m
#define q 127773 /* m = a*q + p */
#define p 2836
#define N 32
#define ndiv (1 + (m-1)/N)
#define rmax (1.0 - 1.2e-7)
static int r[n+1],r0,r1;
int j,k;
if (seed != 0)
/* initialize table of random numbers */
{
r1 = abs(seed);
for (j = n+8;j>=1;j--)
{
k = r1/q;
r1 = a*(r1-k*q) - p*k;
if (r1 < 0) r1 = r1 + m;
if (j < n) r[j] = r1;
}
r0 = r[1];
}
/* beginning when not initializing */
/* compute r1 = mod(a*r1,m) without overflows */
k = r1/q;
r1 = a*(r1 - k*q) - p*k;
if (r1 < 0) r1 = r1 + m;
j = 1 + r0/ndiv;
r0 = r[j];
r[j] = r1;
return min(rm*r0,rmax);
}
// Here are the operation for the lattice sites
int prev( int x){
// find the previous lattice site using periodicity
int y;
if (x==0)
{y=(L-1);}
else
{y=x-1;}
return(y);
}
int next( int x){
// find the next lattice site using periodicity
int y;
if (x==(L-1))
{y=0;}
else
{y=x+1;}
return(y);
}
void initLattice()
int main(void){
int spin[L][L], s ; //the ising sites
int *site, *spin_ptr,*lsite,*rsite,*usite,*dsite,*againsite;
int sum,n,i,j,spinsum,I,k;
float prob,x,ex,average,Sum;
ofstream record("ising.dat");
int Sweeps = 12000; // The number of updates
float initT = 0.0, finalT=4.0,T=0.0;
int steps= 50;
int seed = 3509;
int Ignore=2000; //The number of sweeps between measuring
float Dt=(finalT - initT)/steps;
for(k=0;k<=steps;k++){
Sum=0.0;
//Let us make all the spins pointing up
//Take down the address of the very first site
site=&spin[0][0];
//Using memset to clear the array first
memset(site,0,sizeof(L));
//Making All the sips at lattice site pointing up.
for ( i=0; i<=(L*L-1); i++){
*(site+i)=1;
};
// Next we have to keep on flipping the spins at sites
// sequentially and then use Metropolis algorithm to
// update the flips
I = seed;
for(n=1; n<= Sweeps; n++){
for(i=0;i<=(L-1);i++){ // i fixes the row to sweep across
for (j=0;j<=(L-1);j++){ // sweeping now along the row
//The address of the site (i+1,j)
spin_ptr=&spin[i][j];
//the address of the site to "north"
usite=&spin[prev(i)][j];
//the address of the site to "south"
dsite=&spin[next(i)][j];
// the address of the site to "east"
rsite=&spin[i][next(j)];
//the address of the site to "west"
lsite=&spin[i][prev(j)];
//find the sum of the spins of the neighbors
sum=( *usite + *dsite + *rsite + *lsite ) ;
//Now calculate the "energy" associated with these bonds
ex= (*spin_ptr) * sum;
//The Boltzmann probability of flipping the spin at site (i,j)
prob = exp(-2.0 * ex/T);
//Metropolis says to flip the flip if the Boltzmann probalility of
//flipping is larger than a random number between 0 and 1,
//else not.
x= rnd(I) * 16807;
if(prob > x){
spin[i][j]=-spin[i][j];
};
};//swept through the row
} //here the sweep finishes across the lattice
//Do not record all the updates for averaging as
//we need measurements to be uncorrelated
if(n > Ignore){
//Next measure the magnetization over the lattice
//which is the sum of the spins on the lattice sites
spinsum=0;
againsite=&spin[0][0];
for(i=0; i<=(L*L-1);i++){
spinsum=( spinsum + *(againsite+i) );
}
spinsum=abs(spinsum);
Sum=Sum+spinsum;
}
};
average=Sum/(Sweeps-Ignore);
record << T << " " << average << endl;
T=T+Dt;
}
return (0);
}