-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_tasep.cpp
272 lines (241 loc) · 7.71 KB
/
simple_tasep.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
/***********************************************************
* 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 totally asymmetric simple exclusion process (TASEP) //
//================================================================================//
// For details on the exact solution see:
// B. Derrida, M.R. Evans, V. Hakim, V. Pasquier, Exact solution of a 1d asymmetric exclusion model using a matrix formulation J. Phys. A26, 1493-1517 (1993)
#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;
long int site;
long int nextsite;
long double J;
long double SiteDens;
long double AvgDens;
long double alpha;
long double beta;
long double densprof[L+1], lattice[L+1], p[L+1];
long double enter, eject, hop;
long int j;
//////////////////////////////////////// Functions ///////////////////////////////////////
void initialise() // Initialize lattice.
{
J = 0.0; // Initial current
AvgDens = 0.0;
for (j = 1; j <= L; j++)
{
lattice[j]=0; densprof[j]=0; p[j]=1;
}
enter = dt * alpha * p[1]; // The probability of entering.
eject = dt * beta * p[L]; // The probability of exiting.
hop = dt * p[L/2]; // The probability of hopping.
}
void move()
{
for (j = 1;j <= L; j++) // Goes through as many iterations as the array's length.
{
site = rand()%L + 1; // Picks out a random site in the array.
nextsite = site +1; // Defines the next site. Saves the program from unecessary calculations.
if (site == 1 && lattice[site] == 0 && ran3(&dum) <= enter) // Enter first site.
{
lattice[site] = 1; // If the criteria are met, the site is filled.
}
if (site == L && lattice[site] == 1 && ran3(&dum) <= eject) // Exit last site.
{
lattice[site] = 0; // If the citeria are met, the site is emptied.
}
if (lattice[site] == 1 && lattice[nextsite] == 0 && site >=1 && site <= L-1 && ran3(&dum) <= hop) // Bulk of the array.
{
lattice[site] = 0; // If the criteria are met the particle leave site.
lattice[nextsite] = 1; // And enters the next site.
if(site == L/2 && t >= Teq) // Measuring current. It's measured when a particle crosses the midpoint of the array.
{
J++;
}
}
} // End of loop through array. J-loop.
}
void update()
{
dum=-time(NULL);
ran3(&dum);
for (t = 0; t <= T; t += dt)
{
move(); // Move function.
if (t >= Teq) // Building the density profile after the system has reached steady state. Not averaged yet.
{
for (j = 1; j <= L; j++) // Sweeps throught the array.
{
if (lattice[j] == 1) // If it finds a site which contains a particle.
{
densprof[j] += dt; // It adds one to its density counter. In reality this can be >1, it will be time-averaged later.
}
}
}
} // 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 totally asymmetric simple exclusion process (TASEP).";
const string spaces(program.size(), '*');
const string stars = spaces;
cout<<"\n"<<endl;
cout<<stars<<endl;
cout<< program <<endl;
cout<<stars<<endl;
//ask user for entrace and exit rates
cout<< "\nalpha = ";
cin>> alpha;
cin.ignore();
cout<< "beta = ";
cin>> beta;
cin.ignore();
// display paramteres
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<< " beta = " << beta << endl;
cout<< "\n" << stars <<endl;
cout<< "Exact Solution:\n" <<endl;
cout<< "B. Derrida, M.R. Evans, V. Hakim, V. Pasquier, Exact solution of a 1d asymmetric exclusion model using a matrix formulation J. Phys. A26, 1493-1517 (1993)" <<endl;
if (alpha > 0.5 && beta > 0.5)
{
cout<< " Maximal Current Phase" <<endl;
cout<< "\n";
cout<< " J = 0.25" <<endl;
cout<< " Bulk Density = 0.5" <<endl;
}
if (beta < alpha && beta < 0.5)
{
cout<< " High Density Phase" <<endl;
cout<< "\n";
cout<< " J = b(1-b) = " << beta*(1-beta) <<endl;
cout<< " Bulk Density = " << 1.0-beta <<endl;
}
if (alpha < beta && alpha < 0.5)
{
cout<< " Low Density Phase"<<endl;
cout<< "\n";
cout<< " J = a(1-a) = " << alpha*(1-alpha) <<endl;
cout<< " Bulk Density = " << alpha <<endl;
}
cout<< "\n" << stars <<endl;
std::ostringstream fileNameStreamD("");
fileNameStreamD << "Densprof" <<"_a=" << alpha << "_b=" << beta << ".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++) // Time averaging of the density. It sweeps through the array.
{
SiteDens = densprof[j]/Tdif;
q1<< j <<" "<< SiteDens <<endl;
AvgDens += SiteDens;
}
q1.close();
cputime2 = cputime ();
cputime0 = cputime2 - cputime1;
cout<< "Simulation Results:\n" <<endl;
cout<< " J = "<< J/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 << "Log" << "_a=" << alpha << "_b=" << beta <<".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<< " beta = " << beta << endl;
q2<< "\n" << stars << endl;
q2<< "Exact Solution:\n" <<endl;
q2<< "B. Derrida, M.R. Evans, V. Hakim, V. Pasquier, Exact solution of a 1d asymmetric exclusion model using a matrix formulation J. Phys. A26, 1493-1517 (1993)" <<endl;
if (alpha > 0.5 && beta > 0.5)
{
q2<< " Maximal Current Phase" <<endl;
q2<< "\n";
q2<< " J = 0.25" <<endl;
q2<< " Bulk Density = 0.5" <<endl;
}
if (beta < alpha && beta < 0.5)
{
q2<< " High Density Phase" <<endl;
q2<< "\n";
q2<< " J = b(1-b) = " << beta*(1-beta) <<endl;
q2<< " Bulk Density = " << 1.0-beta <<endl;
}
if (alpha < beta && alpha < 0.5)
{
q2<< " Low Density Phase"<<endl;
q2<< "\n";
q2<< " J = a(1-a) = " << alpha*(1-alpha) <<endl;
q2<< " Bulk Density = " << alpha <<endl;
}
q2<< "\n" << stars <<endl;
q2<< "Simulation Results:\n" <<endl;
q2<< " J = "<< J/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();
}