forked from ssumithra/PowerLawCriticalityPaper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPQModel.cpp
More file actions
278 lines (209 loc) · 6.8 KB
/
PQModel.cpp
File metadata and controls
278 lines (209 loc) · 6.8 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
#include <iostream>
using std::cerr;
using namespace std;
#include <fstream>
using std::ofstream;
#include <cstdlib> // for exit function
#include <ctime>
// parameter definitions
#define N 1024 // system size//#define means 'assign value to the global variable that follows.. so wherever you write N it will be 128 automatically
#define p_range 100000 // no. of p values in range 0-1
#define q_range 10000 // no. of q values in range 0-1
#define T 10000 // No. of time steps
#define rep 5 //no. replicates
//====== function to initialize the matrix ===============//
void create_random_matrix(int*U,float x) { // x is the initial density of the system
int i1, i2;
for (i1 = 0; i1 < N; i1++) {
for (i2 = 0; i2 < N; i2++) {
float number = rand()/(float)RAND_MAX;
if (number<x) U[i1*N+i2]=1;
else U[i1*N+i2]=0;
}
}
}
//=======================average density=========================//
float density(int* A){ // it calculates the average density of N*N plot
int i,j;
float sum=0;
for(i=0;i<N;++i){
for(j=0;j<N;++j){
sum+= A[i*N+j];
}
}
float average= sum/(N*N);
return average;
}
//=========================average along rows======================//
/* if we have a matrix with each row representing density of one realization, this function calculates
the average density over all rows i.e. all realizations */
void average_along_row(float* A,float*A1){
int l,k;
float sum=0;
for(k=0;k<p_range;++k){
sum=0;
for(l=0;l<rep;++l){
sum+=A[l*p_range+k];
}
A1[k]=sum/rep;
}
}
//====================select random site==============================//
void select_neighbor_of_site(int i ,int j , int*neighbor){
int left,right,bottom,top ;
int in = i ,jn = j;
float test = rand()/(float)RAND_MAX ;
if (i==0) top=N-1;
else top = i-1;
if (i==N-1) bottom = 0;
else bottom = i+1 ;
if (j==0) left = N-1;
else left = j-1 ;
if (j==N-1) right = 0;
else right = j+1;
if (test <= 0.25) in = top;
else if ( test <= 0.5) in = bottom;
else if ( test <=0.75) jn =left;
else jn = right;
neighbor[0] = in;
neighbor[1] = jn;
}
//========select neighbor of pair=====================================//
int select_neighbor_of_pair(int in ,int jn, int i, int j){
int left,right,top,bottom,leftn,rightn,topn,bottomn , neighbor_of_pair;
if (i==0) top=N-1;
else top = i-1;
if (i==N-1) bottom = 0;
else bottom = i+1 ;
if (j==0) left = N-1;
else left = j-1 ;
if (j==N-1) right = 0;
else right = j+1;
if (in==0) topn=N-1;
else topn = in-1;
if (in==N-1) bottomn = 0;
else bottomn = in+1 ;
if (jn==0) leftn = N-1;
else leftn = jn-1 ;
if (jn==N-1) rightn = 0;
else rightn = jn+1;
int nn[6] ,c=0;
if ((top*N +j) != (in*N+jn)) {
nn[c]=top*N + j;
c+=1;
}
if ((bottom*N + j) != (in*N+jn)) {
nn[c]=bottom*N + j;
c+=1;
}
if ((i*N +right) != (in*N+jn)) {
nn[c]= i*N + right;
c+=1;
}
if ((i*N +left) != (in*N+jn)) {
nn[c] = i*N + left;
c+=1;
}
if ((topn*N +jn) != (i*N+j)) {
nn[c]=topn*N + jn;
c+=1;
}
if ((bottomn*N +jn) != (i*N+j)) {
nn[c]=bottomn*N + jn;
c+=1;
}
if ((in*N +rightn) != (i*N+j)) {
nn[c]= in*N + rightn;
c+=1;
}
if ((in*N +leftn) != (i*N+j)) {
nn[c] = in*N + leftn;
c+=1;
}
float test =rand()/(float)RAND_MAX ;
if (test <=(0.1666)) neighbor_of_pair= nn[0];
else if ( test <= (2*0.1666)) neighbor_of_pair= nn[1];
else if ( test <= (3*0.1666)) neighbor_of_pair= nn[2];
else if ( test <= (4*0.1666)) neighbor_of_pair= nn[3];
else if ( test <= (5*0.1666)) neighbor_of_pair= nn[4];
else neighbor_of_pair = nn[5];
return neighbor_of_pair;
}
////////////// main function //////////////////////////////////
int main(){
srand(time(NULL)); //uses time based seed - since time will always be different, seed will too
int x,l,t,i,j,z,qvec,qy;
float p[p_range],q[q_range]; //=0.95; //p is an array of length = 'p_range'
for (qvec=0;qvec<q_range;++qvec)
q[qvec]=qvec/(float)q_range;
float* den= new float[rep*p_range]; //remember * makes pointer and here den is a matrix of float type entities and the coloums and 'p' values and rows are replicates
float* av_den= new float[p_range];
int* neighbor = new int[2];
//int* neighbor_pair = new int[2];
//p[0] = 0;
//for (qvec=0;i<qvec_range;++qvec)
//q[qvec]=qvec/(float)q_range
for(i=0;i<p_range;++i)
p[i]= i/(float)p_range;
int*A= new int[N*N]; //A matrix of N by N size
//int*A1= new int[N*N]; // another matrix for synchronous update
cout<<q<<endl; //syntax for output // endl=end line
for(qy=0000;qy<10000;qy=qy+9200){
for(x=99000;x>0; x=x-10000){ // each loop will calculate stationary density at p[x], x-- = x-1, you can calculate final density only for a subset of p values… you specify those p values this way.
for(l=0;l<rep;++l){
create_random_matrix(A,0.5); //creates random matrix with 0.5 initial density. this must be changed to very high and very low values to get upperbranch and lower branch phase diagram for high values of q
for(t=0;t<T;t++){ //thus the loop it runs T times
for(z=0;z<N*N ; ++z){ // so that each site gets selected once on an average
i = rand()%N; // selecting one random site. It'll be some random number from 0 t0 N-1
j = rand()%N;
float test = rand()/(float)RAND_MAX;
float test1 = rand()/(float)RAND_MAX;
if (A[i*N+j]==1){ //if the site is occupied, same as 'i,j' element
select_neighbor_of_site(i, j ,neighbor); //look for a neighbor
int in = neighbor[0] , jn = neighbor[1];
if (A[in*N +jn]==0) { //if neighbor is empty
if (test < p[x])
A[in*N+jn]=1; //regular cp
else A[i*N+j]=0;
}
else {
if (test < q[qy]){
int neighbor_of_pair=select_neighbor_of_pair (in, jn, i, j); //look for the neighbor of pair
A[neighbor_of_pair]=1;
}
else if (test1 < 1-p[x] ) // this is probability (1-q)(1-p).. yes?
A[i*N+j]=0;
}
}
}
}
den[l*p_range+x]= density(A); //l is the index of the replicates
cout<<p[x]<<"\t"<<den[l*p_range+x]<<"\n";
ofstream outdata; // outdata is like cin
outdata.open("tcp_q"+ std::to_string(q[qy]) +"_PD.dat",ios::app); // opens the file
/*if( !outdata ) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
} */
outdata<<den[l*p_range+x]<<"\n";
outdata.close();
//============================saving data in a file===============================//
ofstream spatial_structure_fout;
spatial_structure_fout.open("tcp_q"+ std::to_string(q[qy]) +"_spatial_data_PD.dat",ios::app);
for(i=0;i<N;++i){
for(j=0; j<N ;++j){
spatial_structure_fout<< A[i*N+j]<< endl;
}
}
spatial_structure_fout.close();
}
}
}
delete [] A;
//delete [] A1;
delete [] den;
delete [] av_den;
delete [] neighbor;
//delete [] neighbor_pair;
return 0;
}