forked from sphCow/SUS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsus_ising2d.cpp
More file actions
281 lines (216 loc) · 7.11 KB
/
sus_ising2d.cpp
File metadata and controls
281 lines (216 loc) · 7.11 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
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
#include <iostream>
#include <iomanip>
#include <vector>
#include <fstream>
#include <cmath>
#include <random>
#include <string>
using namespace std;
enum Direction {L=0,R=1};
//***** GLOBAL PARAMETERS *****//
int nx;
int N;
double T;
long mcs;
vector<int> lattice; //this contains spin values, +/- 1 int ix = index%nx;int iy = index/nx;
//rng
std::random_device rd;
std::mt19937 rng(123456);
std::uniform_real_distribution<double> dist(0.0,1.0);
//rng; //std::random_device()());
//files
ofstream flat;
ofstream fthermo;
ofstream fthermo2;
//***** Generate lattice ******//
void generate_lattice(int init) {
/* init = 1 => all lattice +1
* = -1 => all lattice -1
* = 0 => random */
for(int i=0; i<N; i++) {
if(init == 1 || init == -1) lattice[i] = init;
else {
lattice[i] = ( dist(rng)>0.5 ? 1 : -1);
}
}
}
//******* print lattice ******//
void print_lattice() {
for(int i=0; i<N; i++) {
flat << i%nx << " " << int(i/nx) << " "
<< (lattice[i]==1 ? "+" : "-") << endl;
}
flat << endl << endl;
}
//******* calculate energy ******//
int get_energy_site(int index) {
//get position in lattice
int ix = index%nx;
int iy = index/nx;
int n_cell_x = nx;
int n_cell_y = nx;
// get neighbours with pbc
int nb[4], e;
nb[0] = ((ix + 1) % n_cell_x + ((iy + n_cell_y) % n_cell_y) * n_cell_x); //B
nb[1] = ((ix + n_cell_x) % n_cell_x + ((iy + n_cell_y + 1) % n_cell_y) * n_cell_x); //F
nb[2] = ((ix + n_cell_x - 1) % n_cell_x + ((iy + n_cell_y) % n_cell_y) * n_cell_x); //H
nb[3] = ((ix + n_cell_x) % n_cell_x + ((iy + n_cell_y - 1) % n_cell_y) * n_cell_x); //D
//calc energy
e = -1*lattice[index]*(lattice[nb[0]] + lattice[nb[1]] + lattice[nb[2]] + lattice[nb[3]]);
return e;
}
int get_total_energy() {
int tote=0;
for(int i=0; i<N; i++) tote += get_energy_site(i);
return tote;
}
int get_total_magnetization() {
int totm=0;
for(int i=0; i<N; i++) totm += lattice.at(i);
return totm;
}
//*********** trial move ***********//
bool test_flip(int index, int &de) {
de = -2*get_energy_site(index);
if(de < 0)
return true;
else if(dist(rng) < std::exp(-de/T))
return true;
else
return false;
}
bool test_flip(int de) {
if(de <= 0)
return true;
else if(dist(rng) < std::exp(-de/T))
return true;
else
return false;
}
// transient steps [Metropolis ONLY]
void discard_before_eq(int mcs) {
int de;
//Monte Carlo loop
for(long i=0; i<mcs; i++) {
//Metropolis loop
for(int i=0; i<N; i++) {
int rand_pos = static_cast<int>(dist(rng)*N);
if(test_flip(rand_pos,de)) {
//flip
lattice[rand_pos] = -lattice[rand_pos];
}
} // Metropolis loop end
} //Monte Carlo loop end
}
//*********** main ***********//
int main(int argc, char *argv[]) {
//set parameters
nx = atoi(argv[1]);
N = nx*nx;
T = atof(argv[2]);
mcs = atoi(argv[3]);
int bin_count = atoi(argv[4]);
fthermo.open(argv[5], std::ios::out);
string fname =(string(argv[5])+".debug");
fthermo2.open(fname.c_str(), std::ios::out);
//thermos
double E,M,m;
double etot,mtot;
cout << "lattice -> " << nx << "x" << nx << ", T=" << T << ", mcs=" << mcs << endl;
cout << "nbins -> " << bin_count << "thermo_file -> " << argv[5] << endl;
//SUS bins
//vector<long> sus_hist;
vector<double> P;
vector<int> lattice_saved;
//int window_count = bin_count-1;///2;
//double bin_min = -N;
//double bin_max = N;
//double bin_width = (bin_max-bin_min)/double(bin_count);
cout << "N -> " << N << endl;
//sus_hist.resize(bin_count);
P.resize(N+1);
//for(auto &i:sus_hist) i=0;
//open files
flat.open("lattice.dat", std::ios::out);
//allocate
lattice.resize(N);
lattice_saved.resize(N);
//Generate
generate_lattice(-1);
M = get_total_magnetization();
//print_lattice();
//cout << get_total_energy() << " " << get_total_magnetization() << endl;
//DEBUG BINS
//for(int i=0; i<bin_count; i++) {
// int bin_mag_min = i *bin_width+bin_min;
// int bin_mag_max = (i+1)*bin_width+bin_min;
// cout << i << ": " << bin_mag_min << " -> " << bin_mag_max << endl;
//}
cout << "------------------------------------" << endl;
int ibin=0;
int window_edges[2];
int l,r,de;
P[0]=1;
double Nd=0;
lattice_saved = lattice;
int lattice_saved_count = 0;
int M_trial;
int rand_pos;
for(int i=0; i<N; i++) { //dir=0 => left; dir=1 =>right
cout << i << "\r";
cout.flush();
lattice = lattice_saved;
//lattice.swap(lattice_saved);
l=0;
r=0;
lattice_saved_count = 0;
M = get_total_magnetization();
// Monte Carlo loop
for(long j=0; j<mcs; j++) {
// calculate change in energy & magnetization
// if a spin at random site rand_pos is flipped
rand_pos = int(dist(rng)*N);
M_trial = M - 2*lattice[rand_pos] ;
//***************** SUS ******************
int pre = -N+2*i;
int post = -N+2*(i+1);
// IF M_trial is in the current window
if(M_trial >= pre && M_trial <= post ) {
//now lets see whether this move is ACCEPTED by Metropolis
if(test_flip(rand_pos,de)) {
lattice[rand_pos] *= -1; //FLIP
M = M_trial; // As the move is accepted, so our new M is equal to M_trial
}
if(M == pre) {
l++;
}
if(M == post) {
r++;
//if(lattice_saved_count==0) {
// ?? What is the FASTEST way to copy a c++ vector to another one ??
//lattice_saved = lattice;
lattice_saved.assign(lattice.begin(), lattice.end());
lattice_saved_count++;
//}
}
}
//If this move is outside the window => REJECT it
else {
if(M_trial < pre) l++;
if(M_trial > post) r++;
}
} //Monte Carlo loop end
P[i + 1] = P[i] + log(r*pow(l,-1));
Nd += exp(P[(i + 1)]);
cout << l << " " << r << " " << Nd << " " << lattice_saved_count << endl;
} // SUS loop ends
// op to file
for (int i = 0; i < N; i++) {
fthermo << -N+2*i << " " << exp(P[i + 1])/Nd << endl;
}
// close all
flat.close();
fthermo.close();
return 0;
}
// 7207101844