-
Notifications
You must be signed in to change notification settings - Fork 0
/
u1.cpp
299 lines (237 loc) · 8.03 KB
/
u1.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <iostream>
#include <math.h>
#include <time.h>
#include <random>
#include <vector>
#include <fstream>
#include <omp.h>
#include "timer.h"
using namespace std;
int DIRS;
int TDir;
int volume;
int half_volume;
int spatial_volume;
int Grid[4];
int iter = 0;
double accept_ratio = 0.;
void indexEO(int id, int parity, int x[4]){
int za = (id / (Grid[0]/2));
int zb = (za / Grid[1]);
x[1] = za - zb * Grid[1];
x[3] = (zb / Grid[2]);
x[2] = zb - x[3] * Grid[2];
int xodd = (x[1] + x[2] + x[3] + parity) & 1;
x[0] = (2 * id + xodd ) - za * Grid[0];
}
int indexId(int x[4]){
return ((((x[3] * Grid[2] + x[2]) * Grid[1]) + x[1] ) * Grid[0] + x[0]);
}
int indexId(int x[4], int parity, int dir){
int id = ((((x[3] * Grid[2] + x[2]) * Grid[1]) + x[1] ) * Grid[0] + x[0]) >> 1;
return id + parity * half_volume + volume * dir;
}
int indexId(int x[4], int dir){
int id = ((((x[3] * Grid[2] + x[2]) * Grid[1]) + x[1] ) * Grid[0] + x[0]) >> 1;
int parity = (x[0] + x[1] + x[2] +x[3]) & 1;
return id + parity * half_volume + volume * dir;
}
int indexEO_neg(const int id, int parity, int mu, int lmu){
int x[4];
indexEO(id, parity, x);
x[mu] = (x[mu]+lmu+Grid[mu]) % Grid[mu];
int pos = ((((x[3] * Grid[2] + x[2]) * Grid[1]) + x[1] ) * Grid[0] + x[0]) >> 1;
int oddbit = (x[0] + x[1] + x[2] +x[3]) & 1;
pos += oddbit * half_volume;
return pos;
}
int indexEO_neg(const int id, int parity, int mu, int lmu, int nu, int lnu){
int x[4];
indexEO(id, parity, x);
x[mu] = (x[mu]+lmu+Grid[mu]) % Grid[mu];
x[nu] = (x[nu]+lnu+Grid[nu]) % Grid[nu];
int pos = ((((x[3] * Grid[2] + x[2]) * Grid[1]) + x[1] ) * Grid[0] + x[0]) >> 1;
int oddbit = (x[0] + x[1] + x[2] +x[3]) & 1;
pos += oddbit * half_volume;
return pos;
}
int Index_4D_Neig_EO(const int x[], const int dx[], const int X[4]) {
int y[4];
for (int i=0; i<4; i++) y[i] = (x[i] + dx[i] + X[i]) % X[i];
int idx = (((y[3]*X[2] + y[2])*X[1] + y[1])*X[0] + y[0]) >> 1;
return idx;
}
/*
default_random_engine seed;
uniform_real_distribution<double> rand02(0., 2.);
uniform_real_distribution<double> rand01(0,1);*/
std::random_device device;
//std::mt19937 generator(device());
std::mt19937 *generator;//(device());
uniform_real_distribution<double> rand02(0., 2.);
uniform_real_distribution<double> rand01(0,1);
void staple(const double *lat, const int id, const int parity, const int mu, double &stapleRe, double &stapleIm){
stapleRe = 0., stapleIm = 0.;
int idmu1 = indexEO_neg(id, parity, mu, 1);
for(int nu = 0; nu < DIRS; nu++) if(mu != nu) {
double upperStaple = lat[idmu1 + volume * nu];
upperStaple -= lat[indexEO_neg(id, parity, nu, 1) + volume * mu];
upperStaple -= lat[id + parity * half_volume + nu * volume];
double lowerStaple = -lat[indexEO_neg(id, parity, mu, 1, nu, -1) + volume * nu];
lowerStaple -= lat[indexEO_neg(id, parity, nu, -1) + volume * mu];
lowerStaple += lat[indexEO_neg(id, parity, nu, -1) + volume * nu];
stapleRe += cos(upperStaple) + cos(lowerStaple);
stapleIm += sin(upperStaple) + sin(lowerStaple);
}
}
void metropolis(double *lat, double beta){
for(int parity = 0; parity < 2; ++parity)
for(int mu = 0; mu < DIRS; mu++){
#pragma omp parallel for
for(int id = 0; id < volume/2; ++id){
double phase_old = lat[id + parity * half_volume + mu * volume];
double stapleRe = 0., stapleIm = 0.;
staple(lat, id, parity, mu, stapleRe, stapleIm);
double r = std::sqrt( stapleRe*stapleRe + stapleIm*stapleIm );
double t2 = atan2(stapleIm, stapleRe);
double new_phase = M_PI * rand02(generator[omp_get_thread_num()]);
double b = rand01(generator[omp_get_thread_num()]);
double S1 = cos(phase_old + t2);
double S2 = cos(new_phase + t2);
double dS = exp(beta*r*(S2-S1));
if(dS > b){
lat[id + parity * half_volume + mu * volume] = new_phase;
accept_ratio += 1.;
}
}
}
}
void overrelaxation(double *lat, double beta){
for(int parity = 0; parity < 2; ++parity)
for(int mu = 0; mu < DIRS; mu++){
#pragma omp parallel for
for(int id = 0; id < volume/2; ++id){
double stapleRe = 0., stapleIm = 0.;
staple(lat, id, parity, mu, stapleRe, stapleIm);
int pos = id + parity * half_volume + mu * volume;
double phase_old = lat[pos];
double t2 = atan2(stapleIm, stapleRe);
double new_phase = fmod(6.* M_PI - phase_old - 2. * t2, 2.* M_PI);
lat[pos] = new_phase;
}
}
}
void plaquette(double *lat, double *plaq){
for(int i = 0; i < 2; ++i) plaq[i] = 0.;
for(int parity = 0; parity < 2; ++parity){
#pragma omp parallel for reduction(+:plaq[:2])
for(int id = 0; id < volume/2; ++id){
for(int mu = 0; mu < DIRS - 1; mu++){
double tmp = lat[id + parity * half_volume + mu * volume];
int idmu1 = indexEO_neg(id, parity, mu, 1);
for (int nu = (mu+1); nu < DIRS; nu++){
double plaqi = tmp;
plaqi += lat[idmu1 + volume * nu];
plaqi -= lat[indexEO_neg(id, parity, nu, 1) + volume * mu];
plaqi -= lat[id + parity * half_volume + nu * volume];
plaq[0] += cos(plaqi);
plaq[1] += sin(plaqi);
}
}
}
}
int numplaqs = 6; //DIRS=4 3D+1
if(DIRS==2) numplaqs = 1.;
else if(DIRS==3) numplaqs = 3.;
double norm = 1. / double(volume * numplaqs);
for(int i = 0; i < 2; ++i) plaq[i] *= norm;
}
void polyakov(double *lat, double *poly){
for(int i = 0; i < 2; ++i) poly[i] = 0.;
for(int parity = 0; parity < 2; ++parity){
#pragma omp parallel for reduction(+:poly[:2])
for(int id = 0; id < spatial_volume/2; ++id){
int x[4];
indexEO(id, parity, x);
double tmp = 0.;
for(x[TDir] = 0; x[TDir] < Grid[TDir]; ++x[TDir])
tmp += lat[ indexId(x, TDir) ];
poly[0] += cos(tmp);
poly[1] += sin(tmp);
}
}
double norm = 1. / double(spatial_volume);
for(int i = 0; i < 2; ++i) poly[i] *= norm;
}
int main(){
Timer a0;
a0.start();
//omp_set_num_threads(1);
int numthreads = 0;
#pragma omp parallel
numthreads = omp_get_num_threads();
cout << "Number of threads: " << numthreads << endl;
cout << endl;
//create one RNG per thread
generator = new std::mt19937[numthreads];
for(int i = 0; i < numthreads; ++i) generator[i].seed(time(NULL)*(i+1));
DIRS = 4;
TDir = DIRS - 1;
int ls = 8; //The number of points in each direction must be an even number!!!!!!!!!
int Nx=ls;
int Ny=ls;
int Nz=ls;
int Nt=ls;
for(int i = 0; i < 4; ++i) Grid[i] = 1;
Grid[0] = Nx;
if(DIRS==2) Grid[1] = Nt;
else if(DIRS > 2) Grid[1] = Ny;
if(DIRS==3) Grid[2] = Nt;
else if(DIRS > 3) Grid[2] = Nz;
if(DIRS==4) Grid[3] = Nt;
int niter = 100;
double beta = 2.;
bool hotstart = false;
int ovrn = 3;
cout << "Number of directions: " << DIRS << endl;
cout << "Lattice Volume: " << Grid[0] << " x " << Grid[1] << " x " << Grid[2] << " x " << Grid[3] << endl;
cout << "Beta: " << beta << endl;
cout << "Iterations: " << niter << endl;
if(hotstart) cout << "Hot start" << endl;
else cout << "Cold start" << endl;
cout << "Number of overrrelaxation steps: " << ovrn << endl;
cout << endl;
volume = 1;
for(int i = 0; i < 4; ++i) volume *= Grid[i];
half_volume = volume / 2;
spatial_volume = 1;
for(int i = 0; i < TDir; ++i) spatial_volume *= Grid[i];
// creates the lattice array and initializes it to 0, cold start
double *lat = new double[volume*DIRS]();
if(hotstart) {
//Initializes lattice array with random phase (hot start) between 0-2Pi
#pragma omp parallel for
for(int id = 0; id < volume*DIRS; ++id)
lat[id] = M_PI * rand02(generator[omp_get_thread_num()]);
}
double plaq[2];
double poly[2];
plaquette(lat, plaq);
for(iter = 1; iter <= niter; ++iter){
metropolis(lat, beta);
for(int ovr = 0; ovr < ovrn; ++ovr)
overrelaxation(lat, beta);
plaquette(lat, plaq);
polyakov(lat, poly);
if( (iter%10)==0){
cout << "iter: " << iter << " \tplaq: " << 1.-plaq[0] << "\t" << plaq[1] << endl;
cout << " " << " \tL: " << poly[0] << "\t" << poly[1] << "\t|L|: " << sqrt(poly[0]*poly[0]+poly[1]*poly[1]) << endl;
}
}
cout << "Acceptation ratio: " << accept_ratio/double(volume*DIRS*iter) << endl;
delete[] lat;
delete[] generator;
a0.stop();
std::cout << "Time: " << a0.getElapsedTime() << endl;
return 0;
}