-
Notifications
You must be signed in to change notification settings - Fork 0
/
fluid_model_traffic_sim_forward_difference.py
58 lines (52 loc) · 1.44 KB
/
fluid_model_traffic_sim_forward_difference.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
import numpy as np
import matplotlib.pyplot as plt
road_t = []
road_dt = []
road_ev = []
size = 100
time_elapsed = 0
v_max = 5
max_density = 30
def v(density):
return (1-density/max_density)*v_max
bc_rho = 0
for i in range(size):
if i < size/2:
road_t.append(30)
road_dt.append(30)
else:
road_t.append(15)
road_dt.append(15)
while time_elapsed < 1000:
for i in range(size):
if i == size - 1:
road_dt[i] += 0.01/2 * (v(road_t[i]) * (road_t[i] - bc_rho) + road_t[i] * (v(road_t[i]) - v(bc_rho)))
else:
road_dt[i] += 0.01/2 * (v(road_t[i]) * (road_t[i] - road_t[i+1]) + road_t[i] * (v(road_t[i]) - v(road_t[i+1])))
time_elapsed += 1
road_t = road_dt.copy()
if time_elapsed % 1 == 0:
road_ev.append(road_t.copy())
if time_elapsed % 100 == 0:
print(time_elapsed)
array2 = []
#for i in road_ev:
# temp_array = []
# for k in i:
# if(k < 0):
## temp_array.append([0,0,-1*k*255/10]) #white
# temp_array.append(0)
# else:
# temp_array.append(k*255/10) #black
# array2.append(temp_array)
#pic_array = np.array(array2)
#img = Image.fromarray(pic_array)
x = range(0, 100)
for i in range(100):
if i % 1 == 0:
y = np.array(road_ev[i])
plt.plot(x,y)
plt.xlabel('Position')
plt.ylabel('Density')
plt.savefig('plot' + str(i) + '.png')
plt.clf()