-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_pasep.cpp
342 lines (307 loc) · 8.33 KB
/
simple_pasep.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/***********************************************************
* Copyright (C) 2014
* Authors: Hamid Teimouri & Daniel Celis
* Rice university--Department of Chemistry
* This file is distributed under the terms of the
* GNU General Public License as published by the
* Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
* http://www.gnu.org/copyleft/gpl.txt
***********************************************************/
//==================================================================================//
// Monte Carlo Siumulation of partially asymmetric simple exclusion process (PASEP) //
//==================================================================================//
// For details on the exact solution see:
// R. A. Blythe and M. R. Evans. "Nonequilibrium steady state of matrix-product form: as solver's guide." J. Phys. A: Math. Theor. Vol. 40 (2007) R333-R441.
#include <iostream>
#include <fstream>
#include <sstream>
#include <math.h>
#include <time.h>
#include <numeric>
#include <cstdlib>
#include <vector>
#include <valarray>
#include <algorithm>
#include <cstddef>
#include <iomanip>
#include <ctime>
#include "ran3.h"
#include <cmath>
#pragma hdrstop
#define MBIG 1000000000
#define MSEED 161803398
#define MZ 0
#define FAC (1.0/MBIG)
using std::vector;
using namespace std;
long int dum;
const int L = 100;
long double T = 1e4;
long double Teq = T * 0.2;
long double Tdif = T - Teq;
long double dt = 0.01;
long double t;
int site;
int nextsite;
int prevsite;
int pprevsite;
int nnextsite;
long double J1;
long double J2;
long double SiteDens;
long double AvgDens;
long double alpha;
long double beta;
long double gama;
long double delta;
long double eta;
long double chi;
long double densprof[L+1], lattice[L+2], p[L+1], pn[L+1];
long double l_en, l_ex, r_en, r_ex, f_hop, b_hop;
int j;
////////////////////////Functions/////////////////////////
void initialise()
{
J1 = 0.0;
J2 = 0.0;
AvgDens = 0.0;
lattice[0] = 0;
lattice[L+1] = 0;
for(j = 1; j <= L; j++)
{
lattice[j]=0; densprof[j]=0; p[j]=gama; pn[j]=delta;
}
l_en = dt*alpha*p[1];
l_ex = dt*eta*pn[1];
r_en = dt*chi*pn[L];
r_ex = dt*beta*p[L];
f_hop = dt*p[L/2];
b_hop = dt*pn[L/2];
}
void boundary_interactions()
{
// Left
if (site == 1)
{
// FORWARD
if (lattice[site]==0 && ran3(&dum)<=l_en)
{
lattice[site]=1;
}
//BACKWARD
if (lattice[site]==1 && ran3(&dum)<=l_ex)
{
lattice[site]=0;
}
}
// Right
if (site == L)
{
// FORWARD
if (lattice[site] == 1 && ran3(&dum) <= r_ex)
{
lattice[site] = 0; // moves to state (1,0)
}
// BACKWARD
if (lattice[site] == 0 && ran3(&dum) <= r_en)
{
lattice[site] = 1; // moves to state (1,1)
}
}
}
void move()
{
for(j = 1; j <= L; j++)
{
site = (rand() % L+1);
prevsite = site - 1;
nextsite = site + 1;
boundary_interactions();
// Forward
if (lattice[site] == 1 && lattice[nextsite] == 0 && site >= 1 && site <= L-1 && ran3(&dum) <= f_hop)
{
lattice[nextsite] = 1; // moves to state (0,0,1,0)
lattice[site] = 0;
if (site == L/2 && t >= Teq)
{
J1++;
}
} // End of site availability check.
//Backward
if (lattice[site] == 1 && lattice[prevsite] == 0 && site >= 2 && site <= L && ran3(&dum) <= b_hop)
{
lattice[prevsite] = 1; // moves to state (0,0,1,0)
lattice[site] = 0;
if (site == L/2 && t >= Teq)
{
J2++;
}
} // End of site availability check.
} // End of loop through array.
}
void update()
{
dum=-time(NULL);
ran3(&dum);
for (t = 0.0; t <= T; t+=dt) // Time loop allows for better averaging, and allows the system to reach steady state.
{
move();
if(t >= Teq)
{
for(j = 1;j <= L; j++)
{
if (lattice[j] == 1)
{
densprof[j] += dt;
}
}
}
} // End of time loop.
}
double cputime ( )
{
double time;
time = ( double ) clock ( ) / ( double ) CLOCKS_PER_SEC;
return time;
}
//===================================================================//
//============================ MAIN CODE ============================//
//===================================================================//
int main()
{
double cputime0;
double cputime1;
double cputime2;
const string program ="Monte Carlo Siumulation of partially asymmetric simple exclusion process (PASEP).";
const string spaces(program.size(), '*');
const string stars = spaces;
cout<<"\n"<<endl;
cout<<stars<<endl;
cout<< program <<endl;
cout<<stars<<endl;
cout<< "\nalpha = ";
cin>> alpha;
cin.ignore();
cout<< "eta = ";
cin>> eta;
cin.ignore();
cout<< "beta = ";
cin>> beta;
cin.ignore();
cout<< "chi = ";
cin>> chi;
cin.ignore();
cout<< "gamma = ";
cin>> gama;
cin.ignore();
cout<< "delta = ";
cin>> delta;
cin.ignore();
cout<< "\n" << stars <<endl;
cout<< "Parameters:\n" <<endl;
cout<< " L = " << L << endl;
cout<< " T = " << T << endl;
cout<< " dt = " << dt <<endl;
cout<< " alpha = " << alpha << endl;
cout<< " eta = " << eta << endl;
cout<< " beta = " << beta << endl;
cout<< " chi = " << chi << endl;
cout<< " gamma = " << gama << endl;
cout<< " delta = " << delta << endl;
cout<< "\n" << stars <<endl;
cout<< "Exact Solution:\n" <<endl;
cout<< " R. A. Blythe and M. R. Evans." <<endl;
cout<< " ''Nonequilibrium steady state of matrix-product form: as solver's guide.''" <<endl;
cout<< " J. Phys. A: Math. Theor. Vol. 40 (2007) R333-R441.\n" <<endl;
if (alpha > 0.5 && beta > (1-delta)/2.0)
{
cout<< " Maximal Current Phase" <<endl;
cout<< "\n";
cout<< " J = (1-d)/4 = " << (1-delta)/4.0 <<endl;
}
if (beta < alpha && beta < (1-delta)/2.0)
{
cout<< " High Density Phase" <<endl;
cout<< "\n";
cout<< " J = b(1-d-b)/(1-d) = " << beta*(1-delta-beta)/(1-delta) <<endl;
}
if (alpha < beta && alpha < (1-delta)/2.0)
{
cout<< " Low Density Phase"<<endl;
cout<< "\n";
cout<< " J = a(1-d-a)/(1-d) = " << alpha*(1-delta-alpha)/(1-delta) <<endl;
}
cout<< "\n" << stars <<endl;
std::ostringstream fileNameStreamD("");
fileNameStreamD << "D" <<"_a" << alpha <<"et" << eta << "b" << beta << "c" << chi << "g" << gama << "d" << delta << ".txt";
std::string fileNameD = fileNameStreamD.str();
ofstream q1(fileNameD.c_str());
#pragma omp parallel
#pragma omp for lastprivate(lattice, J)
initialise();
update();
for(j = 1; j <= L; j++)
{
SiteDens = densprof[j]/Tdif;
q1<< j <<" "<< SiteDens <<endl;
AvgDens += SiteDens;
}
q1.close();
cputime2 = cputime ();
cputime0 = cputime2 - cputime1;
cout<< "Simulation Results:\n" <<endl;
cout<< " J = "<< (J1-J2)/Tdif <<endl;
cout<< " Bulk Density = "<< AvgDens/L <<endl;
cout<< "\n";
cout<< " Elapsed cpu time for main computation:\n";
cout<< " " << cputime2 << " seconds" <<endl;
cout<< "\n" << stars <<endl;
std::ostringstream fileNameStreamL("");
fileNameStreamL << "L" <<"_a" << alpha <<"et" << eta << "b" << beta << "c" << chi << "g" << gama << "d" << delta << ".txt";
std::string fileNameL = fileNameStreamL.str();
ofstream q2(fileNameL.c_str());
q2<< stars <<endl;
q2<< "Parameters:\n" <<endl;
q2<< " L = " << L << endl;
q2<< " T = " << T << endl;
q2<< " dt = " << dt <<endl;
q2<< " alpha = " << alpha << endl;
q2<< " eta = " << eta << endl;
q2<< " beta = " << beta << endl;
q2<< " chi = " << chi << endl;
q2<< " gamma = " << gama << endl;
q2<< " delta = " << delta << endl;
q2<< "\n" << stars << endl;
q2<< "Exact Solution:\n" <<endl;
q2<< " R. A. Blythe and M. R. Evans." <<endl;
q2<< " ''Nonequilibrium steady state of matrix-product form: as solver's guide.''" <<endl;
q2<< " J. Phys. A: Math. Theor. Vol. 40 (2007) R333-R441.\n" <<endl;
if (alpha > 0.5 && beta > (1-delta)/2.0)
{
q2<< " Maximal Current Phase" <<endl;
q2<< "\n";
q2<< " J = (1-d)/4 = " << (1-delta)/4.0 <<endl;
}
if (beta < alpha && beta < (1-delta)/2.0)
{
q2<< " High Density Phase" <<endl;
q2<< "\n";
q2<< " J = b(1-d-b)/(1-d) = " << beta*(1-delta-beta)/(1-delta) <<endl;
}
if (alpha < beta && alpha < (1-delta)/2.0)
{
q2<< " Low Density Phase"<<endl;
q2<< "\n";
q2<< " J = a(1-d-a)/(1-d) = " << alpha*(1-delta-alpha)/(1-delta) <<endl;
}
q2<< "\n" << stars << endl;
q2<< "Simulation Results:\n" <<endl;
q2<< " J = "<< (J1-J2)/Tdif <<endl;
q2<< " Bulk Density = "<< AvgDens/L <<endl;
q2<< "\n";
q2<< " Elapsed cpu time for main computation:\n";
q2<< " " << cputime2 << " seconds" <<endl;
q2<< "\n" << stars << endl;
q2.close();
}