-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoter model.jl
225 lines (122 loc) · 4.25 KB
/
Voter model.jl
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
using Plots
using Statistics
using StatsBase
"""
init_lattice(dims:: Array, qs::Array, p::Array)
Initialise the d-dimensional lattice for the Voter model
Arguments
* `dims`: Array of the dimension of the lattice
* `qs`: Array of opinions
* `p`: Array of initial presence probability for each q
Returns
* `lattice`
"""
function init_lattice(dims, qs, p="Uniform")
if p == "Uniform"
lattice = zeros(dims)
for i in eachindex(lattice)
lattice[i] = rand(qs)
end
else
println("Feature not implemented yet")
end
return lattice
end
function unit_tuple(D::Int, i::Int, val::Int)
tmp = zeros(Int, D)
tmp[i] = val
return Tuple(tmp)
end
"""
build_neighbours(dims:: Array)
Initialise the d-dimensional lattice for the Voter model
Arguments
* `dims`: Array of the dimension of the lattice
Returns
* `neighbours_table`: where neighbours_table[i][site] is the i nearest neighbour of the site i
"""
function build_neighbours(dims)
sites = CartesianIndices(dims)
neighbours_table = [circshift(sites, unit_tuple(length(dims), 1, i)) for i in -1:2:1]
for j in 2 : length(dims)
append!(neighbours_table, [circshift(sites, unit_tuple(length(dims), j, i)) for i in -1:2:1])
end
return neighbours_table
end
function random_imitation(lattice, neighbours_table, site)
rand_neighbour = rand(1 : ndims(lattice) * 2) #Pick randomly one of the neighbours
lattice[site] = lattice[neighbours_table[rand_neighbour][site]] #Adopt its value
return lattice
end
function compute_observables(lattice, neighbours_table, sites)
different_links = 0.
for site in sites
site_val = lattice[site]
for nn in neighbours_table
neighbour_val = lattice[nn[site]]
if neighbour_val != site_val
different_links += 1
end
end
end
different_links = different_links / 2 #Each link has been counted twice
return different_links
end
function simulation(t, lattice, neighbours_table, sites)
density_t = zeros(t)
MC_t = length(lattice)
@inbounds for k in 1 : t
for i in 1 : MC_t
site = rand(sites)
lattice = random_imitation(lattice, neighbours_table, site)
end
density_t[k] = compute_observables(lattice, neighbours_table, sites)
if density_t[k] == 0
break
end
end
return lattice, density_t / (length(lattice))
end
function Voter_model(dims, qs, t)
lattice = init_lattice(dims, qs);
neighbours_table = build_neighbours(dims)
sites = CartesianIndices(dims)
lattice, density = simulation(t, lattice, neighbours_table, sites)
return lattice, density
end
function Avg_Voter_model(dims, qs, t, times)
final_density = zeros(t)
taus = zeros(times)
for k in 1 : times
lattice = init_lattice(dims, qs)
neighbours_table = build_neighbours(dims)
sites = CartesianIndices(dims)
lattice, density = simulation(t, lattice, neighbours_table, sites)
final_density += density
taus[k] = length(density[density .> 0])
end
return final_density ./ times, taus
end
function N_study(Ns, qs, t, times)
f_tau = open("tau_N_reg_net.txt", "w")
println(f_tau, "#N\t<tau>")
for N in Ns
println("N: $N")
dims = (N, N, N)
density_t, taus = @time Avg_Voter_model(dims, qs, t, times);
avg_tau = mean(taus)
f = open("results_reg_net_$N.txt", "w")
println(f,"#rho_t")
@inbounds for i in 1 : length(density_t)
println(f, density_t[i])
end
close(f)
println(f_tau, N, "\t", avg_tau)
end
close(f_tau)
end
Ns = [8, 9, 10, 12, 15, 18]
qs = [1, 2]
t = 10^5
times = 10^3
@time N_study(Ns, qs, t, times)