-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab10_Q1_B.py
198 lines (189 loc) · 6.45 KB
/
Lab10_Q1_B.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
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
# This program simulates Brownian motion in the presence of walls
# Note that the physical behaviour would be to stick to walls,
# which is the purpose of Q1a.
# Author: Nico Grisouard, University of Toronto
# Date: 14 November 2018
#################################################################
# This program simulates Brownian motion in the presence of walls
# Note that the physical behaviour would be to stick to walls,
# which is the purpose of Q1a.
# Author: Nico Grisouard, University of Toronto
# Date: 14 November 2018
#################################################################
def nextmove(x, y):
""" randomly choose a direction
1 = up, 2 = down, 3 = left, 4 = right"""
direction = np.random.randint(1,5)
if direction == 1: # move up
y += 1
elif direction == 2: # move down
y -= 1
elif direction == 3: # move right
x += 1
elif direction == 4: # move left
x -= 1
else:
print("error: direction isn't 1-4")
if x == 100 :
x -= 1
elif y == 100 :
y -= 1
elif x == 0:
x += 1
elif y == 0:
y += 1
return x, y
Lp = 101 # size of domain
Nt = 5000 # number of time steps
# arrays to record the trajectory of the particle
x_position = np.empty(Nt)
y_position = np.empty(Nt)
centre_point = (Lp-1)//2 # middle point of domain
x_position[0] = centre_point
y_position[0] = centre_point
for i in range(Nt-1):
x_position[i+1],y_position[i+1] = nextmove(x_position[i],y_position[i])
from pylab import clf, plot, xlim, ylim, show, pause
plt.figure(figsize = (10,10))
for i in range(Nt):
clf() # clear the plot
plt.plot(x_position[0:i], y_position[0:i],c = 'brown')
plt.ylim(0,100)
plt.xlim(0,100)
plt.draw()
pause(0.01)
#pause to allow a smooth animation
def nextmove(x, y):
""" randomly choose a direction
1 = up, 2 = down, 3 = left, 4 = right"""
direction = np.random.randint(1,5)
if direction == 1: # move up
y += 1
elif direction == 2: # move down
y -= 1
elif direction == 3: # move right
x += 1
elif direction == 4: # move left
x -= 1
else:
print("error: direction isn't 1-4")
return x, y
x_particle_list = []
y_particle_list = []
Lp = 101 # size of domain
# arrays to record the trajectory of the particle
centre_point = (Lp-1)//2 # middle point of domain
x_position = np.array([centre_point])
y_position = np.array([centre_point])
j = 0
i = 0
while i < 100: # number of particle count
new_particle = False
x_fill,y_fill = nextmove(x_position[j],y_position[j])
if x_fill == Lp-1 or y_fill == Lp-1 or x_fill == 0 or y_fill == 0:
# test for hitting a wall
new_particle = True
for k in range(len(x_particle_list)):
particle_position = np.array([x_fill,y_fill])
end_x_array = x_particle_list[k]
end_y_array = y_particle_list[k]
rest_position = np.array([end_x_array[-1], end_y_array[-1]])
if np.array_equal(particle_position, rest_position) == True:
new_particle = True
j += 1
if new_particle == True:
# print('generated new particle')
# generate a new particle
x_particle_list.append(x_position)
y_particle_list.append(y_position)
x_position = np.array([centre_point])
y_position = np.array([centre_point])
#print(len(x_position))
i+= 1
j = 0
else:
x_position = np.append(x_position,x_fill)
y_position = np.append(y_position,y_fill)
# AND OFF YOU GO!
# AND OFF YOU GO!
from pylab import clf, plot, xlim, ylim, show, pause
plt.figure(figsize = (10,10))
for j in range(len(x_particle_list)):
x_position = x_particle_list[j]
y_position = y_particle_list[j]
for i in range(len(x_position)):
clf() # clear the plot
if j > 0:
clf() # clear the plot
for k in range(j):
x_rest = x_particle_list[k]
y_rest = y_particle_list[k]
plt.scatter(x_rest[-1],y_rest[-1],marker = "2" ,s = 80)
plt.scatter(x_position[i], y_position[i*4],marker = "2" ,s = 80)
plt.ylim(0,Lp-1)
plt.xlim(0,Lp-1)
plt.draw()
pause(0.01)
#
x_particle_list =[]
y_particle_list =[]
Lp = 151 # size of domain
# arrays to record the trajectory of the particle
centre_point = (Lp-1)//2 # middle point of domain
x_position = np.array([centre_point])
y_position = np.array([centre_point])
centre_full = False
new_particle = False
#
while centre_full == False: # testing if centre point is full
new_particle = False
x_fill,y_fill = nextmove(x_position[j],y_position[j])
if x_fill == Lp-1 or y_fill == Lp-1 or x_fill == 0 or y_fill == 0:
# Here you test if particle hits a wall
new_particle = True
for k in range(len(x_particle_list)):
particle_position = np.array([x_fill,y_fill])
end_x_array = x_particle_list[k]
end_y_array = y_particle_list[k]
rest_position = np.array([end_x_array[-1], end_y_array[-1]])
if np.array_equal(particle_position, rest_position) == True:
new_particle = True
if np.array_equal(particle_position, np.array([centre_point,centre_point])) == True:
centre_full = True
# finishing the calculation if hit centre
j += 1
if new_particle == True:
# print('generated new particle')
# generate a new particle
x_particle_list.append(x_position)
y_particle_list.append(y_position)
x_position = np.array([centre_point])
y_position = np.array([centre_point])
#print(len(x_position))
i+= 1
j = 0
else:
x_position = np.append(x_position,x_fill)
y_position = np.append(y_position,y_fill)
# AND OFF YOU GO!
plt.figure(figsize = (10,10))
for j in range(len(x_particle_list)):
x_position = x_particle_list[j]
y_position = y_particle_list[j]
for i in range(len(x_position)):
clf() # clear the plot
if j > 0:
clf() # clear the plot
for k in range(j):
x_rest = x_particle_list[k]
y_rest = y_particle_list[k]
plt.scatter(x_rest[-1],y_rest[-1],marker = "2" ,s = 80)
plt.scatter(x_position[i], y_position[i*4],marker = "2" ,s = 80)
plt.ylim(0,Lp-1)
plt.xlim(0,Lp-1)
plt.draw()
pause(0.01)