/
lattice.cpp
262 lines (216 loc) · 5.95 KB
/
lattice.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
#include <iostream>
#include <cmath>
#include <cassert>
#include <ctime>
#include <gsl/gsl_sf_exp.h> // Exponential functions
#include "lattice.hpp"
#include "util.hpp"
Lattice::Lattice(RR mu2_, RR lambda_, ZZ Nx_, ZZ Ny_)
: mu2(mu2_), lambda(lambda_),
mu2_tilde(2 + (mu2_ / 2)), lambda_tilde(lambda_ / 4),
Nx(Nx_), Ny(Ny_),
size(Nx * Ny),
size_minus_Nx(Nx * (Ny-1))
{
phi_data = new RR[size];
rng = gsl_rng_alloc(gsl_rng_mt19937);
randomize();
}
Lattice::~Lattice()
{
gsl_rng_free(rng);
delete [] phi_data;
}
void Lattice::randomize(unsigned long int seed)
{
if (seed != 0)
gsl_rng_set(rng, seed);
for (phi_iterator pi = begin_phi(); pi != end_phi(); pi++)
*pi = random_phi();
}
void Lattice::iteration()
{
sweep_Metropolis();
step_Wolff(random_site());
}
void Lattice::equilibrate(const ZZ N)
{
const std::clock_t t0 = std::clock();
for (ZZ i = 0; i < N; i++)
iteration();
const std::clock_t t1 = std::clock();
std::cout << "Equilibrate: "
<< 1000 * (t1 - t0) / CLOCKS_PER_SEC
<< "ms" << std::endl;
}
////////////////////////////////////////////////////////
//
// Accessing Lattice Sites
//
////////////////////////////////////////////////////////
inline Lattice::SiteXY Lattice::get_site_xy(Site site) const
{
ZZ prev_x, next_x, prev_y, next_y;
if (likely(site < size_minus_Nx)) {
next_x = site + 1;
next_y = site + Nx;
}
else if (likely(site < size - 1)) {
next_x = site + 1;
next_y = site + Nx - size;
}
else {
assert(site == size - 1);
next_x = 0;
next_y = Nx - 1;
}
if (likely(site >= Nx)) {
prev_x = site - 1;
prev_y = site - Nx;
}
else if (likely(site > 0)) {
prev_x = site - 1;
prev_y = site + size - Nx;
}
else {
assert(site == 0);
prev_x = size - 1;
prev_y = size - Nx;
}
return SiteXY(site, prev_x, next_x, prev_y, next_y);
}
inline Lattice::Phi Lattice::get_phi(Site site)
{
return phi_data[site];
}
inline Lattice::ConstPhi Lattice::get_phi(Site site) const
{
return phi_data[site];
}
inline Lattice::PhiXY Lattice::get_phi_xy(Site site)
{
SiteXY s = get_site_xy(site);
return PhiXY(phi_data[s.site],
phi_data[s.prev_x], phi_data[s.next_x],
phi_data[s.prev_y], phi_data[s.next_y]);
}
inline Lattice::ConstPhiXY Lattice::get_phi_xy(Site site) const
{
SiteXY s = get_site_xy(site);
return ConstPhiXY(phi_data[s.site],
phi_data[s.prev_x], phi_data[s.next_x],
phi_data[s.prev_y], phi_data[s.next_y]);
}
inline Lattice::Site Lattice::random_site() const
{
return Site(floor(size * random()));
}
////////////////////////////////////////////////////////
//
// Metropolis Algorithm
//
////////////////////////////////////////////////////////
void noinline Lattice::sweep_Metropolis() {
for (ZZ i = 0; i < METROPOLIS_SWEEPS_PER_ITERATION * size; i++) {
step_Metropolis(get_phi_xy(random_site()));
}
}
inline void Lattice::step_Metropolis(const PhiXY& phi) {
const double phi_2 = phi.value * phi.value;
const double newphi = phi.value + random_phi();
const double newphi_2 = newphi * newphi;
// Calculate energy difference
const double delta =
(phi.value - newphi) * (phi.next_x + phi.next_y + phi.prev_x + phi.prev_y)
+
mu2_tilde * (newphi_2 - phi_2)
+
lambda_tilde * (newphi_2 * newphi_2 - phi_2 * phi_2);
// Flip if difference <= 0 or probabilistic acceptance
if (delta <= 0 || random() < gsl_sf_exp(-delta))
phi.value = newphi;
}
////////////////////////////////////////////////////////
//
// Wolff Algorithm
//
////////////////////////////////////////////////////////
// Conditionally add new_site to the cluster.
/* Acceptance of new_site depends on the sign at the lattice site and
* a probabilistic factor
*/
inline bool Lattice::cluster_try_add(bool positive, Phi phi, Site new_site) {
if ((get_phi(new_site) > 0) != positive)
return false;
if (cluster.find(new_site) != cluster.end())
return false;
const RR probability =
1 - gsl_sf_exp(-2 * phi * get_phi(new_site));
if (random() < probability) {
cluster.insert(new_site);
cluster_queue.push_back(new_site);
return true;
}
return false;
}
void Lattice::flip_cluster() {
assert(cluster_queue.empty());
for (auto ci = cluster.begin(); ci != cluster.end(); ci++) {
get_phi(*ci) *= -1;
}
cluster.clear();
}
Lattice::ZZ noinline Lattice::step_Wolff(const Site& site) {
const bool positive = (get_phi(site) > 0);
cluster.clear();
cluster.insert(site);
cluster_queue.clear();
cluster_queue.push_back(site);
while (!cluster_queue.empty()) {
Site site = cluster_queue.back();
cluster_queue.pop_back();
Phi phi = get_phi(site);
SiteXY nbhd = get_site_xy(site);
cluster_try_add(positive, phi, nbhd.prev_x);
cluster_try_add(positive, phi, nbhd.next_x);
cluster_try_add(positive, phi, nbhd.prev_y);
cluster_try_add(positive, phi, nbhd.next_y);
}
const ZZ cluster_size = cluster.size();
flip_cluster();
return cluster_size;
}
////////////////////////////////////////////////////////
//
// Calculation of observables
//
////////////////////////////////////////////////////////
inline Lattice::RR Lattice::energy(const ConstPhiXY& phi) const
{
const RR phi_2 = phi.value * phi.value;
return
- phi.value * (phi.next_x + phi.next_y)
+ mu2_tilde * phi_2
+ lambda_tilde * phi_2 * phi_2;
}
Lattice::RR Lattice::total_energy() const
{
RR result = 0;
for (const_phi_xy_iterator phi = begin_phi_xy(); phi != end_phi_xy(); phi++)
result += energy(*phi);
return result / size;
}
Lattice::RR Lattice::average_phi() const
{
RR sum = 0;
for (const_phi_iterator phi_iter = begin_phi(); phi_iter != end_phi(); phi_iter++)
sum += *phi_iter;
return sum / size;
}
Lattice::RR Lattice::average_abs_phi() const
{
RR sum = 0;
for (const_phi_iterator phi_iter = begin_phi(); phi_iter != end_phi(); phi_iter++)
sum += abs(*phi_iter);
return sum / size;
}