-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBox.cpp
More file actions
283 lines (252 loc) · 8.62 KB
/
Copy pathBox.cpp
File metadata and controls
283 lines (252 loc) · 8.62 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
282
283
#include "Box.h"
double L; // size of whole cube
double a; //LatticeA constant
double M; //number of cubes in LatticeA
double rCutOff; // cut-off on Lennard-Jones potential
double lambda =0;
double ran0(long *idum);
long l = -1; // simulation parameters
const double rho = 1.0; // density (number per unit volume)
Box::Box(int numParticles, int steps, string flag) {
MCSteps = steps;
N = numParticles;
position = flag;
Lattice = new double *[N];
for (int i = 0; i < N; i++) {
Lattice[i] = new double[3];
}
UAvg = new double [MCSteps];
Var= new double [MCSteps];
// compute side of cube from number of particles and number density
L = pow(N / rho, 1.0/3);
rCutOff = 0.49 * L;
M = 1;
while (4 * M * M * M < N)
++M;
a = L / M; // LatticeA constant of conventional cell
double dx[4] = {0.0, 0.5, 0.5, 0.0};//
double dy[4] = {0.0, 0.0, 0.5, 0.5};//
double dz[4] = {0.0, 0.5, 0.0, 0.5};//
int n = 0; // atoms placed so far
for (int x = 0; x < M; x++){
for (int y = 0; y < M; y++){
for (int z = 0; z < M; z++){
for (int p = 0; p < 4; p++){
if (n < N) {
if(position.compare("Right") == 0) {
Lattice[n][0] = (0.25 + x + dx[p]) * a - 0.5 * L;
Lattice[n][1] = (0.25 + y + dy[p]) * a;
Lattice[n][2] = (0.25 + z + dz[p]) * a - 0.5 * L;
}else {
Lattice[n][0] = (0.25 + x + dx[p]) * a - 0.5 * L;
Lattice[n][1] = n == 0 ?
-(0.75 + y + dy[p]) * a :
-(0.25 + y + dy[p]) * a;
Lattice[n][2] = (0.25 + z + dz[p]) * a - 0.5 * L;
}
++n;
}
}
}
}
}
}
double Box::MinImage(int i, int j) {
// find separation using closest image convention
double dr[3];
double sum = 0;
for (int d = 0; d < 3; d++) {
dr[d] = Lattice[i][d] - Lattice[j][d];
if (dr[d] >= 0.5*L) dr[d] -= L;
if (dr[d] < -0.5*L) dr[d] += L;
sum += dr[d] * dr[d];
}
return sum;
}
void Box::computeOrderParameter() {
//compute the order parameter after each MonteCarlo time step
lambda = 0;
for (int i = 0; i < N; i++){
lambda += cos((4 * PI * Lattice[i][0]) / a) +
cos((4 * PI * Lattice[i][1]) / a) +
cos((4 * PI * Lattice[i][2]) / a);
}
lambda /= -3 * (double) N;
}
void Box::MMC(int s, double T, string filename, double step) {
ofstream file;
file.precision(8);
file.open(filename, ios::app);
long ll = -1;
double Ui = 0;
double U = 0;
double delta_r = step; //0.03;
double AcceptanceRatio = 0;
computeOrderParameter();
vector<int> indexes;
for (int i = 0; i < N; ++i) indexes.push_back(i);
while (indexes.size() != 0) {
// choose a random point, here we are using idx as index
random_shuffle(indexes.begin(), indexes.end());
int idx = indexes[indexes.size() - 1];
indexes.pop_back();
double Uold = computeEnergy();
vector<double> dr = Move(idx, delta_r);
// Apply periodic boundary conditions
for (int k = 0; k < 3; k++){
if (Lattice[idx][k] < 0) Lattice[idx][k] += L;
if (Lattice[idx][k] >= L)Lattice[idx][k] -= L;
if (Lattice[idx][k] < 0) Lattice[idx][k] += L;
if (Lattice[idx][k] >= L)Lattice[idx][k] -= L;
}
//check energy difference due to move
double Unew = computeEnergy();
double delta_U = Unew - Uold;
if (delta_U > 0 && ran0(&ll) > exp(-(delta_U / T))) { //move is rejected
UnMove(idx, dr);
Ui = Uold;
}
else {//move is accepted
Ui = Unew;
AcceptanceRatio +=1;
}
U += (Ui)/((double)N);
}
//compute the average energy and standard deviation using the method on
UAvg[s] = s == 0 ? U : UAvg[s - 1] + ((U - UAvg[s - 1]) / s);
Var[s] = s == 0 ? 0 : Var[s - 1] * s + (U - UAvg[s - 1]) * (Ui - UAvg[s]);
Var[s] /= (s + 1);
computeOrderParameter();
AcceptanceRatio = AcceptanceRatio * 100 / N;
file << s << "\t " << setw(12) << U << "\t ";
file << setw(12) << UAvg[s] << "\t ";//running average
file << setw(12) << Var[s] << "\t ";//running variance
file << setw(12) << lambda << "\t ";
file << setw(12) << AcceptanceRatio << "\t";
file << setw(12) << T << "\n";
file.close();
}
void Box::MMC(int s, double T, string filename, double step, Box B) {
ofstream file;
file.precision(8);
file.open(filename, ios::app);
long ll = -1;
double Ui = 0;
double U = 0;
double delta_r = step; //0.03;
double AcceptanceRatio = 0;
computeOrderParameter();
vector<int> indexes;
for (int i = 0; i < N; ++i) indexes.push_back(i);
while (indexes.size() != 0) {
// choose a random point, here we are using idx as index
random_shuffle(indexes.begin(), indexes.end());
int idx = indexes[indexes.size() - 1];
indexes.pop_back();
double Uold = computeEnergy();
vector<double> dr = Move(idx, delta_r);
// Apply periodic boundary conditions
for (int k = 0; k < 3; k++){
if (Lattice[idx][k] < 0) Lattice[idx][k] += L;
if (Lattice[idx][k] >= L)Lattice[idx][k] -= L;
if (Lattice[idx][k] < 0) Lattice[idx][k] += L;
if (Lattice[idx][k] >= L)Lattice[idx][k] -= L;
}
//check energy difference due to move
double Unew = computeEnergy();
double delta_U = Unew - Uold;
if (delta_U > 0 && ran0(&ll) > exp(-(delta_U / T))) { //move is rejected
UnMove(idx, dr);
Ui = Uold;
}
else {//move is accepted
Ui = Unew;
AcceptanceRatio +=1;
}
U += (Ui)/((double)N);
}
//compute the average energy and standard deviation using the method on
UAvg[s] = s == 0 ? U : UAvg[s - 1] + ((U - UAvg[s - 1]) / s);
Var[s] = s == 0 ? 0 : Var[s - 1] * s + (U - UAvg[s - 1]) * (Ui - UAvg[s]);
Var[s] /= (s + 1);
computeOrderParameter();
AcceptanceRatio = (AcceptanceRatio * 100)/N;
file << s << "\t " << setw(12) << U << "\t ";
file << setw(12) << UAvg[s] << "\t ";//running average
file << setw(12) << Var[s] << "\t ";//running variance
file << setw(12) << lambda << "\t ";
file << setw(12) << AcceptanceRatio << "\t";
file << setw(12) << T << "\n";
file.close();
}
double Box::computeEnergy() {
double U = 0;
for (int i = 0; i < N-1; i++) { // all distinct pairs
for (int j = i+1; j < N; j++) { // of particles i,j
double rSqd = MinImage(i, j);
if (rSqd < rCutOff*rCutOff){
U += (pow(1 / rSqd, 6) - pow(1 / rSqd, 3));
}
}
}
return 4 * U;
}
void Box::writeHeaders(ofstream &f, int index, string *fnames) {
f.open(fnames[index], ios::trunc);
f.precision(8);
f << "MCStep s " << setw(12) << "\t " <<
"Energy_U" << "\t" <<
setw(12) << "Average_Energy_<U>" << "\t " <<
setw(12) << "Variance" << "\t " <<
setw(12) << "Order_Parameter" << "\t " <<
setw(12) << "Acceptance_Ratio" << "\t " <<
setw(12) << "Temperature\n";
}
vector<double> Box::Move(int index, double dr) {
vector<double>delta (3);
double t = ran0(&l);
t = 2 * t - 1;
t = acos(t);
double ph = ran0(&l);
delta.push_back( dr * cos(t) * sin(2 * ph * PI));
delta.push_back(dr * sin(t) * sin(2 * ph * PI));
delta.push_back(dr * cos(2 * ph * PI));
Lattice[index][0] += delta[0];
Lattice[index][1] += delta[1];
Lattice[index][2] += delta[2];
return delta;
}
void Box::UnMove(int index, vector<double> delta) {
Lattice[index][0] -= delta[0];
Lattice[index][1] -= delta[1];
Lattice[index][2] -= delta[2];
}
Box::~Box(){
delete [] Lattice;
delete [] UAvg;
delete [] Var;
}
#define IA 16807
#define IM 2147483647
#define AM (1.0/IM)
#define IQ 127773
#define IR 2836
#define MASK 123459876
double ran0(long *idum) {
long k;
double ans;
*idum ^= MASK;
k = (*idum)/IQ;
*idum = IA*(*idum - k*IQ) - IR*k;
if(*idum < 0) *idum += IM;
ans=AM*(*idum);
*idum ^= MASK;
return ans;
}
#undef IA
#undef IM
#undef AM
#undef IQ
#undef IR
#undef MASK
#undef PI