-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomWalk3D.py
271 lines (242 loc) · 6.76 KB
/
randomWalk3D.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
import matplotlib.pyplot as plt
import random as ra
import time
# Timing formatting function; strictly for logging/debugging purposes.
def timelapse(secs):
minutes = int(secs/60)
seconds = format((secs % 60), "0.2f")
tlist = [minutes, seconds]
return tlist
def abs1(num):
if num >= 0:
return num
elif num < 0:
return num*(0-1)
def maxmin(ls):
high = -35000000
low = 35000000
for i in range(len(ls)):
if ls[i] > high:
high = ls[i]
if ls[i] < low:
low = ls[i]
res = [high, low]
return res
def rand():
ra.seed()
dire = ra.randrange(1, 7)
if dire == 1:
dire = 'e'
elif dire == 2:
dire = 'n'
elif dire == 3:
dire = 'w'
elif dire == 4:
dire = 's'
elif dire == 5:
dire = 'u'
elif dire == 6:
dire = 'd'
else:
dire = None
return 0
return dire
sd = 'N'
while 1:
try:
sd = str(input("Generate Random Walk? [Y/N]: "))
except Exception as e:
print(f'Invlaid entry; Error message: \n{e}.')
exit()
if sd == 'Y' or sd == 'y':
ls1 = []
xcoor = []
ycoor = []
zcoor = []
xcoor.append(0)
ycoor.append(0)
zcoor.append(0)
try:
n = int(input("Enter the number of steps: "))
except Exception as e2:
print(f'Invlaid entry; Error message: \n{e2}.')
exit()
time_stamp_01 = time.time() # Timer starts.
n_copy = n
while n > 0:
ls1.append(rand())
n = n-1
print(f'Directions Taken: {ls1}')
i = 0
while i < len(ls1):
if ls1[i] == 'e':
xcoor.append(xcoor[i] + 1)
ycoor.append(ycoor[i] + 0)
zcoor.append(zcoor[i] + 0)
elif ls1[i] == 'n':
xcoor.append(xcoor[i] + 0)
ycoor.append(ycoor[i] + 1)
zcoor.append(zcoor[i] + 0)
elif ls1[i] == 'w':
xcoor.append(xcoor[i] - 1)
ycoor.append(ycoor[i] + 0)
zcoor.append(zcoor[i] + 0)
elif ls1[i] == 's':
xcoor.append(xcoor[i] + 0)
ycoor.append(ycoor[i] - 1)
zcoor.append(zcoor[i] + 0)
elif ls1[i] == 'u':
xcoor.append(xcoor[i] + 0)
ycoor.append(ycoor[i] + 0)
zcoor.append(zcoor[i] + 1)
elif ls1[i] == 'd':
xcoor.append(xcoor[i] + 0)
ycoor.append(ycoor[i] + 0)
zcoor.append(zcoor[i] - 1)
i = i+1
xorig = []
yorig = []
zorig = []
null_list_1 = []
null_list_2 = []
##Checking max. values of coordinate range.
resx = maxmin(xcoor)
resy = maxmin(ycoor)
resz = maxmin(zcoor)
res_all = maxmin([abs1(resx[0]), abs1(resx[1]), abs1(
resy[0]), abs1(resy[1]), abs1(resz[0]), abs1(resz[1])])
high = res_all[0]
##Constructing coordinate set for origins of all axes.
for j in range((0-high), (0+high+1), 1):
xorig.append(j)
yorig.append(j)
zorig.append(j)
null_list_1.append(0)
null_list_2.append(0)
# Timer ends.
time_stamp_02 = time.time() - time_stamp_01
# Basic formatting of timing information.
dur = timelapse(time_stamp_02)
elif sd == 'N' or sd == 'n':
print(f'"{sd}" chosen; exiting application.')
exit()
else:
print(f'Invalid input: "{sd}"; exiting application.')
exit()
print(f'Highest coordinate value: {high}')
print(f'X-Axis Graduation: {xorig}')
print(f'Y-Axis Graduation: {yorig}')
print(f'Z-Axis Graduation: {zorig}')
print(f'X-Axis Coordinates: {xcoor}')
print(f'Y-Axis Coordinates: {ycoor}')
print(f'Z-Axis Coordinates: {zcoor}')
##Block V: Logging/Debugging Information
if dur[0] == 1:
print(
f'This operation took {dur[0]} minute and {dur[1]} seconds to complete.')
elif dur[0] == 0 or dur[0] > 1:
print(
f'This operation took {dur[0]} minutes and {dur[1]} seconds to complete.')
plt.axes(projection="3d")
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
##Drawing Origin
plt.plot(
0,
0,
0,
color='#5d9c25',
linewidth=0,
marker='o',
markerfacecolor='#6be302',
markersize=7
)
##Drawing X-Axis:
plt.plot(
xorig,
null_list_1,
null_list_2,
color='#c41818',
linewidth=1,
marker=',',
markerfacecolor='blue',
markersize=0,
label='X-Axis'
)
##Drawing Y-Axis:
plt.plot(
null_list_1,
yorig,
null_list_2,
color='#18c431',
linewidth=1,
marker=',',
markerfacecolor='blue',
markersize=0,
label='Y-Axis'
)
##Drawing Z-Axis:
plt.plot(
null_list_1,
null_list_2,
zorig, color='#185ac4',
linewidth=1,
marker=',',
markerfacecolor='blue',
markersize=0,
label='Z-Axis'
)
##Drawing random walk in three dimensions:
plt.plot(
xcoor,
ycoor,
zcoor,
color='#1a1515',
linewidth=1,
marker='o',
markerfacecolor='#ff0000',
markersize=3,
label=f'User-specified 3D walk of {n_copy} steps.'
)
##Drawing final position:
plt.plot(
xcoor[len(xcoor)-1],
ycoor[len(ycoor)-1],
zcoor[len(zcoor)-1],
color='#050505',
linewidth=1,
marker='o',
markerfacecolor='#ffffff',
markersize=7,
label='Final Position'
)
plt.plot(
0,
0,
0,
color='#5d9c25',
linewidth=0,
marker='o',
markerfacecolor='#6be302',
markersize=7,
label='Initial Position'
)
##Drawing initial position:
plt.title(f'3-Dimensional Unitary Walk after {n_copy} steps.')
##Plot features:
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
##Calculation of net displacement of particle after 'n' steps.
displacement = (
(
xcoor[len(xcoor)-1]**2
)
+ (
ycoor[len(ycoor)-1]**2
)
+ (
zcoor[len(zcoor)-1]**2)
)**0.5
print(f'Net Displacement: {format(displacement, "0.2f")} units.')