-
Notifications
You must be signed in to change notification settings - Fork 0
/
brownian_motion_WND.py
281 lines (236 loc) · 9.61 KB
/
brownian_motion_WND.py
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
import numpy as np
import matplotlib.pyplot as plt
from numpy import *
import sys
import matplotlib.animation as manimation
import time
#Elastic collisions with Periodic boundary conditions
#Simulation parameters
N = 150 #Number of particles in system
R_a = 0.3 #Radius of particle a
R_b = 0.1 #Radii of particle b
L = 3.0 #Size of periodic domain
max_vel = 2.0 #Max velocity components of the particles
max_t = 1.0 #Max simulation time
dt = 1e-2 #Time step size
m_a = 1.0 #Particle mass a
m_b = 0.4 #Particle mass b
start_sim = 0
num_sim = 1
err = 1e-6 #Small number
h = L - R_b - err #Domain limit particle injection
#Initialization
E_wall = L
W_wall = -L
N_wall = L
S_wall = -L
E_lab = 0
W_lab = 1
N_lab = 2
S_lab = 3
NE_lab = 4
NW_lab = 5
SW_lab = 6
SE_lab = 7
C_lab = 8
r_wall_x = N + 1
l_wall_x = N + 2
u_wall_y = N + 3
l_wall_y = N + 4
x = np.zeros(N)
y = np.zeros(N)
Ri = np.zeros(N)
Ri[0] = R_a
for i in range(1, N):
Ri[i] = R_b
mi = np.zeros(N)
mi[0] = m_a
for i in range(1, N):
mi[i] = m_b
X = np.zeros((N,9))
Y = np.zeros((N,9))
V_X = np.zeros((N,9))
V_Y = np.zeros((N,9))
#Start Multisims
for sim in range(start_sim, num_sim):
start_time = time.time()
overlap_counter = 0
#Generate initial velocities
v_x = max_vel*np.random.randn(N)
v_y = max_vel*np.random.randn(N)
#Generating initial positions of particles
x[0] = 0
y[0] = 0
#Generating initial positions of particles
for n in range(1, N):
print(n)
x[n] = 2 * h * np.random.uniform(0, 1) - h
y[n] = 2 * h * np.random.uniform(0, 1) - h
overlap = True
while overlap == True:
overlap = False
for i in range(0, n):
dx = x[i] - x[n]
dy = y[i] - y[n]
if dx*dx + dy*dy < (Ri[i]+Ri[n])*(Ri[i]+Ri[n]):
overlap = True
if overlap == True:
x[n] = 2 * h * np.random.uniform(0, 1) - h
y[n] = 2 * h * np.random.uniform(0, 1) - h
#Particle tracking info
x_track = x[0]
x_track_zero = x[0]
y_track = y[0]
y_track_zero = y[0]
t_track = []
r2_track = []
#Start simulation code
time_t = 0
frame_counter = 0
while time_t < max_t:
print(time_t)
#Exporting data to file
if frame_counter == floor(time_t / dt):
#track particle zero
r2 = ((x_track - x_track_zero)*(x_track - x_track_zero) + (y_track - y_track_zero)*(y_track - y_track_zero))
t_track.append(time_t)
r2_track.append(r2)
my_file_x = 'BMW_norm_dist_x_' + str(frame_counter) + '.txt'
file_x = open(my_file_x, "w")
my_file_y = 'BMW_norm_dist_y_' + str(frame_counter) + '.txt'
file_y = open(my_file_y, "w")
for index in range(0, size(x)):
data_x = str(x[index])
data_y = str(y[index])
file_x.write(data_x)
file_x.write('\n')
file_y.write(data_y)
file_y.write('\n')
file_x.close()
file_y.close()
frame_counter = frame_counter + 1
#Initialise collision related info
coll_partner_1 = 0
coll_partner_2 = 0
coll_time = 1e+10
coll_time_particle = coll_time
domain_lab = 0
coll_with_wall = False
coll_with_particle = False
#Checking collision time with particles in main domain
for n in range(0, N):
for i in range(n+1, N):
rab = [x[n] - x[i], y[n] - y[i]]
vab = [v_x[n] - v_x[i], v_y[n] - v_y[i]]
Disc = np.dot(rab,vab)*np.dot(rab,vab)-np.dot(vab, vab)*(np.dot(rab, rab)-(Ri[i]+Ri[n])*(Ri[i]+Ri[n]))
if Disc > 0:
coll_time_particle = (-np.dot(rab, vab) - sqrt(Disc)) / np.dot(vab, vab)
else:
coll_time_particle = 3e+8
if coll_time_particle < coll_time and coll_time_particle >= 0:
coll_time = coll_time_particle
coll_partner_1 = n
coll_partner_2 = i
coll_with_wall = False
coll_with_particle = True
coll_time_r_wall_x = (E_wall - Ri[n] - x[n]) / (v_x[n] + 1e-20)
if coll_time_r_wall_x >= 0 and coll_time_r_wall_x <= coll_time:
coll_time = coll_time_r_wall_x
coll_partner_1 = n
coll_partner_2 = r_wall_x
coll_with_wall = True
coll_with_particle = False
coll_time_l_wall_x = -(x[n] - Ri[n] - W_wall) / (v_x[n] + 1e-20)
if coll_time_l_wall_x >= 0 and coll_time_l_wall_x <= coll_time:
coll_time = coll_time_l_wall_x
coll_partner_1 = n
coll_partner_2 = l_wall_x
coll_with_wall = True
coll_with_particle = False
coll_time_u_wall_y = (N_wall - Ri[n] - y[n]) / (v_y[n] + 1e-20)
if coll_time_u_wall_y >= 0 and coll_time_u_wall_y <= coll_time:
coll_time = coll_time_u_wall_y
coll_partner_1 = n
coll_partner_2 = u_wall_y
coll_with_wall = True
coll_with_particle = False
coll_time_l_wall_y = -(y[n] - Ri[n] - S_wall) / (v_y[n] + 1e-20)
if coll_time_l_wall_y >= 0 and coll_time_l_wall_y <= coll_time:
coll_time = coll_time_l_wall_y
coll_partner_1 = n
coll_partner_2 = l_wall_y
coll_with_wall = True
coll_with_particle = False
#Update positions and velocities
if coll_time < dt:
#Update positions to the point of collision minus some small, negligible, numerical value to
#prevent particles from getting stuck in a wall
x = x + v_x*coll_time*(1-err)
y = y + v_y*coll_time*(1-err)
#track particle zero
x_track = x_track + v_x[0]*coll_time*(1-err)
y_track = y_track + v_y[0]*coll_time*(1-err)
time_t = time_t + coll_time
if coll_with_particle == True:
#Update velocities of colliding particles
ra = np.array([x[coll_partner_1], y[coll_partner_1]])
va = np.array([v_x[coll_partner_1], v_y[coll_partner_1]])
rb = np.array([x[coll_partner_2], y[coll_partner_2]])
vb = np.array([v_x[coll_partner_2], v_y[coll_partner_2]])
n = (ra-rb)/sqrt(np.dot((ra-rb), (ra-rb)))
del_v = n*np.dot((va - vb), n)
#Update velocities
v_x[coll_partner_1] = v_x[coll_partner_1] - del_v[0]*2*mi[coll_partner_2]/(mi[coll_partner_1]+mi[coll_partner_2])
v_y[coll_partner_1] = v_y[coll_partner_1] - del_v[1]*2*mi[coll_partner_2]/(mi[coll_partner_1]+mi[coll_partner_2])
v_x[coll_partner_2] = v_x[coll_partner_2] + del_v[0]*2*mi[coll_partner_1]/(mi[coll_partner_1]+mi[coll_partner_2])
v_y[coll_partner_2] = v_y[coll_partner_2] + del_v[1]*2*mi[coll_partner_1]/(mi[coll_partner_1]+mi[coll_partner_2])
elif coll_with_particle == False:
if coll_partner_2 == r_wall_x or coll_partner_2 == l_wall_x:
v_x[coll_partner_1] = -v_x[coll_partner_1]
if coll_partner_2 == u_wall_y or coll_partner_2 == l_wall_y:
v_y[coll_partner_1] = -v_y[coll_partner_1]
elif coll_time >= dt: #Update positions and velocities using dt
x = x + v_x*dt
y = y + v_y*dt
#track particle zero
x_track = x_track + v_x[0]*dt
y_track = y_track + v_y[0]*dt
time_t = time_t + dt
#Check if particles are within bounds
in_boundaries = True
for n in range(0, N):
in_boundaries = (x[n] <= E_wall) and (x[n] >= W_wall) and (y[n] <= N_wall) and (y[n] >= S_wall)
if not in_boundaries:
print('outside boundaries')
sys.exit()
print('in boundaries')
print(in_boundaries)
#Check overlap
there_is_overlap = False
for n in range(0, N):
for i in range(0, N):
if i != n:
dx = x[i] - x[n]
dy = y[i] - y[n]
if dx*dx + dy*dy < (Ri[i]+Ri[n])*(Ri[i]+Ri[n]) - 1e-8:
there_is_overlap = True
if there_is_overlap == True:
print("there is overlap")
overlap_counter = overlap_counter + 1
sys.exit()
print(there_is_overlap)
print('overlap counter')
print(overlap_counter)
#Exporting data to file
my_file = 'BMW_data_norm_dist_' + str(sim) + '.txt'
print(my_file)
file = open(my_file, "w")
for index in range(0, size(t_track)):
data_r2 = str(r2_track[index])
data_time = str(t_track[index])
file.write(data_time)
file.write(' ')
file.write(data_r2)
file.write('\n')
file.close()
print("--- %s seconds ---" % (time.time() - start_time))