-
Notifications
You must be signed in to change notification settings - Fork 0
/
phi4.cl
183 lines (150 loc) · 6.7 KB
/
phi4.cl
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
#define DERIV_STEP 0.01
__kernel void metropolis_sweep(
__global const float *deltas_g, __global const float *probs_g, __global float *phi, __global int *acceptance_rate_g)
{
// initialize variables
int gid = get_global_id(0); // each gid will get its own lattice to play with - these will be added up by the cpu
float delta;
int accept, phi_i;
int phi_i_dt0, phi_i_dt1;
int phi_i_dx0, phi_i_dx1;
int phi_i_dy0, phi_i_dy1;
float a0,a1,a2,a3, N, phi_i_val, dS;
for (int t = 1; t < {{Nt-1}}; t++){
for (int x = 1; x < {{Nx-1}}; x++){
for (int y = 1; y < {{Ny-1}}; y++){
// compute some indices for ease of use
phi_i = gid*{{Nt*Nx*Ny}} + t*{{Nx*Ny}} + x*{{Ny}} + y;
phi_i_dt0 = phi_i - {{Nx*Ny}};
phi_i_dt1 = phi_i + {{Nx*Ny}};
phi_i_dx0 = phi_i - {{Ny}};
phi_i_dx1 = phi_i + {{Ny}};
phi_i_dy0 = phi_i - 1;
phi_i_dy1 = phi_i + 1;
// compute some things to add up to make dS
phi_i_val = phi[phi_i];
a3 = {{lam}};
a2 = {{4*lam}}*phi_i_val;
a1 = {{1-2*lam}}+{{6*lam}}*phi_i_val*phi_i_val;
N = {{-2*kappa*zeta}}*(phi[phi_i_dt1] - phi[phi_i_dt0])
+ {{-2*kappa/zeta}}*(
(phi[phi_i_dx1] - phi[phi_i_dx0]) +
(phi[phi_i_dy1] - phi[phi_i_dy0])
);
a0 = N+2*phi_i_val*({{1-2*lam}}+{{2*lam}}*phi_i_val*phi_i_val);
// compute dS
delta = deltas_g[phi_i];
dS = delta*(a0+delta*(a1+delta*(a2+delta*a3)));
// calculate accept bool
accept = probs_g[phi_i] < exp(-dS);
// replace value in phi
phi[phi_i] += accept*delta;
// record accept
acceptance_rate_g[gid] += accept;
}
}
}
}
__kernel void microcanonical_sweep_lambda0(
__global const float *deltas_g, __global const float *probs_g, __global const float *mu, __global float *phi, __global int *acceptance_rate_g)
{
// initialize variables
int gid = get_global_id(0); // each gid will get its own phi to play with - these will be added up later
//float delta;
int accept, phi_i;
int phi_i_dt0, phi_i_dt1;
int phi_i_dx0, phi_i_dx1;
int phi_i_dy0, phi_i_dy1;
float a0, N, phi_i_val, dS;
float dSdp, dSdp_proposed;
// Metropolis sweep
for (int t = 1; t < {{Nt-1}}; t++){
for (int x = 1; x < {{Nx-1}}; x++){
for (int y = 1; y < {{Ny-1}}; y++){
// compute some indices for ease of use
phi_i = gid*{{Nt*Nx*Ny}} + t*{{Nx*Ny}} + x*{{Ny}} + y;
if (mu[phi_i] < 0.98) {
phi_i_dt0 = phi_i - {{Nx*Ny}};
phi_i_dt1 = phi_i + {{Nx*Ny}};
phi_i_dx0 = phi_i - {{Ny}};
phi_i_dx1 = phi_i + {{Ny}};
phi_i_dy0 = phi_i - 1;
phi_i_dy1 = phi_i + 1;
// compute some things to add up to make dS
phi_i_val = phi[phi_i];
N = {{-2*kappa*zeta}}*(phi[phi_i_dt1] + phi[phi_i_dt0])
+ {{-2*kappa/zeta}}*(
(phi[phi_i_dx1] + phi[phi_i_dx0]) +
(phi[phi_i_dy1] + phi[phi_i_dy0])
);
dSdp = N+2*phi_i_val+DERIV_STEP;
phi_i_val = -0.5*(DERIV_STEP+N);
dSdp_proposed = N+2*phi_i_val+DERIV_STEP;
// calculate accept bool
accept = probs_g[phi_i] < fabs(dSdp/dSdp_proposed);
// replace value in phi
if (accept) {
phi[phi_i] = phi_i_val;
}
acceptance_rate_g[gid] += accept;
}
}
}
}
}
__kernel void microcanonical_sweep(
__global const float *deltas_g, __global const float *probs_g, __global const float *mu, __global float *phi, __global int *acceptance_rate_g)
{
// initialize variables
int gid = get_global_id(0); // each gid will get its own phi to play with - these will be added up later
//float delta;
int accept, phi_i;
int phi_i_dt0, phi_i_dt1;
int phi_i_dx0, phi_i_dx1;
int phi_i_dy0, phi_i_dy1;
float a0,a1,a2,a3, N, phi_i_val, dS;
float dSdp, dSdp_proposed;
// Metropolis sweep
for (int t = 1; t < {{Nt-1}}; t++){
for (int x = 1; x < {{Nx-1}}; x++){
for (int y = 1; y < {{Ny-1}}; y++){
// compute some indices for ease of use
phi_i = gid*{{Nt*Nx*Ny}} + t*{{Nx*Ny}} + x*{{Ny}} + y;
if (mu[phi_i] < 0.98) {
phi_i_dt0 = phi_i - {{Nx*Ny}};
phi_i_dt1 = phi_i + {{Nx*Ny}};
phi_i_dx0 = phi_i - {{Ny}};
phi_i_dx1 = phi_i + {{Ny}};
phi_i_dy0 = phi_i - 1;
phi_i_dy1 = phi_i + 1;
// compute some things to add up to make dS
phi_i_val = phi[phi_i];
a3 = {{lam}};
a2 = {{4*lam}}*phi_i_val;
a1 = {{1-2*lam}}+{{6*lam}}*phi_i_val*phi_i_val;
N = {{-2*kappa*zeta}}*(phi[phi_i_dt1] + phi[phi_i_dt0])
+ {{-2*kappa/zeta}}*(
(phi[phi_i_dx1] + phi[phi_i_dx0]) +
(phi[phi_i_dy1] + phi[phi_i_dy0])
);
a0 = N+2*phi_i_val*({{1-2*lam}}+{{2*lam}}*phi_i_val*phi_i_val);
dSdp = (a0+DERIV_STEP*(a1+DERIV_STEP*(a2+DERIV_STEP*a3)));
// TODO select this new value randomly from the 3 solutions to the cubic
// phi_i_val =
// a\left(c+\left(a+2x\right)\left(1+b\left(\left(a+2x\right)a+2\left(x^{2}-1\right)\right)\right)\right)=0
a2 = {{4*lam}}*phi_i_val;
a1 = {{1-2*lam}}+{{6*lam}}*phi_i_val*phi_i_val;
a0 = N+2*phi_i_val*({{1-2*lam}}+{{2*lam}}*phi_i_val*phi_i_val);
dSdp_proposed = (a0+DERIV_STEP*(a1+DERIV_STEP*(a2+DERIV_STEP*a3)));
// calculate accept bool
accept = probs_g[phi_i] < fabs(dSdp/dSdp_proposed);
// replace value in phi
if (accept) {
phi[phi_i] = phi_i_val;
}
acceptance_rate_g[gid] += accept;
}
}
}
}
}