-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasep_interactions.cpp
357 lines (329 loc) · 9.7 KB
/
tasep_interactions.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**********************************************************************************************************
* 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) with interacting particles //
//===========================================================================================================//
// For details on the exacto 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 <omp.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <math.h>
#include <time.h>
#include <numeric>
#include <cstdlib>
#include <vector>
#include <valarray>
#include <algorithm>
#include <cstddef>
#include <iomanip>
#include <ctime>
#include <cmath>
#include "ran3.h"
#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.1;
long double t;
int site;
int prevsite;
int nextsite;
int nnextsite;
long double J;
long double SiteDens;
long double AvgDens;
long double alpha;
long double beta;
long double epsilon;
long double theta;
long double q;
long double r;
long double densprof[L+1], lattice[L+2], p[L+1];
long double n_i_enter, i_enter, n_i_eject, i_eject, n_i_hop, q_hop, r_hop;
int j;
///////////////////////// Functions.////////////////////////
void initialise()
{
J = 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]=1;
}
n_i_enter = dt*alpha*p[1];
i_enter = q*n_i_enter;
n_i_eject = dt*beta*p[L];
i_eject = r*n_i_eject;
n_i_hop = dt*p[L/2];
q_hop = dt*q*p[L/2];
r_hop = dt*r*p[L/2];
}
void boundary_interactions()
{
if (site == 1 && lattice[site] == 0)
{
// Injection with rate alpha if in state (0,0)
if (lattice[nextsite] == 0 && ran3(&dum) <= n_i_enter)
{
lattice[site] = 1;
}
// Injection with rate q*alpha if in state (0,1) => Binding
if (lattice[nextsite] == 1 && ran3(&dum) <= i_enter)
{
lattice[site] = 1;
}
}
if (site == L && lattice[site] == 1)
{
// Ejection rate beta if in state (0,1)
if (lattice[prevsite] == 0 && ran3(&dum) <= n_i_eject)
{
lattice[site] = 0;
}
// Ejection rate r*beta if in state (1,1) => Disassociation
if (lattice[prevsite] == 1 && ran3(&dum) <= i_eject)
{
lattice[site] = 0;
}
}
}
void move()
{
for (j = 1; j <= L; j++)
{
site = rand()%L + 1;
prevsite = site - 1;
nextsite = site + 1;
nnextsite = nextsite + 1;
boundary_interactions();
if (lattice[site] == 1 && lattice[nextsite] == 0 && site >= 1 && site <= L-1)
{
if (lattice[prevsite] == lattice[nnextsite] && ran3(&dum) <= n_i_hop) // Bulk hopping with rate 1 if in state (0,1,0,0) or (1,1,0,1)
{
lattice[nextsite] = 1; // moves to state (0,0,1,0)
lattice[site] = 0;
if (site == L/2 && t >= Teq)
{
J++;
}
}
if (lattice[prevsite] != lattice[nnextsite])
{
if (lattice[prevsite] == 1 && ran3(&dum) <= r_hop)
{
lattice[nextsite] = 1; // moves to state (1,0,1,0)
lattice[site] = 0;
if (site == L/2 && t >= Teq)
{
J++;
}
}
if (lattice[prevsite] == 0 && ran3(&dum) <= q_hop)
{
lattice[nextsite] = 1; // moves to state (1,0,1,0)
lattice[site] = 0;
if (site == L/2 && t >= Teq)
{
J++;
}
}
}
} // 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(); // 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) with interacting particles.";
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();
//ask user for interaction energy
cout<< "E = ";
cin>> epsilon;
cin.ignore();
//ask user for the interaction energy spliting fraction
if (epsilon != 0.0)
{
cout<< "theta = ";
cin>> theta;
cin.ignore();
}
// binding & unbinding rates are related in a thermodynamically consistent fashion
q = exp(theta * epsilon); // binding rate
r = exp((theta - 1) * epsilon); // unbinding rate
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<< " E = " << epsilon << endl;
cout<< " theta = " << theta << endl;
cout<< " q = " << q << endl;
cout<< " r = " << r << endl;
cout<< "\n" << stars <<endl;
if (epsilon == 0.0)
{
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 (q=r=1)" <<endl;
cout<< "\n";
cout<< " J = 0.25" <<endl;
cout<< " Bulk Density = 1/2" <<endl;
}
if (beta < alpha && beta < 0.5)
{
cout<< " High Density Phase (q=r=1)" <<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 (q=r=1)"<<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" << "_E=" << epsilon << "_th=" << theta <<"_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";
cout<< "\n" << stars <<endl;
std::ostringstream fileNameStreamL("");
fileNameStreamL << "Log" << "_E=" << epsilon << "_th=" << theta <<"_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<< " E = " << epsilon << endl;
q2<< " theta = " << theta << endl;
q2<< " q = " << q << endl;
q2<< " r = " << r << endl;
q2<< "\n" << stars << endl;
if (epsilon == 0.0)
{
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 (q=r=1)" <<endl;
q2<< "\n";
q2<< " J = 0.25" <<endl;
q2<< " Bulk Density = 1/2" <<endl;
}
if (beta < alpha && beta < 0.5)
{
q2<< " High Density Phase (q=r=1)" <<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 (q=r=1)"<<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();
}