```python
import numpy as np
import sympy
from matplotlib import pyplot
%matplotlib inline
from sympy.utilities.lambdify import lambdify
```


```python
#Part A
```


```python
#Conditions 
Vmax = 80 #km/h
L = 11 #km
rhomax = 250 #cars/km
nx = 51 #spatial discretization
dt = 0.001 #h
dx = L/(nx-1)

```


```python
x = np.linspace(0,L,nx)
rho0 = np.ones(nx)*10
rho0[10:20] = 50
```


```python
T1 = 6/60.0
nt = int(T1/dt)
print('Number of steps',nt)
```

    Number of steps 100



```python
v = np.ones(nx)

for i in range(nx):

    v[i] = Vmax*(1 - (rho0[i]/rhomax))

```


```python
vminimum = np.min(v)
vmin0 = vminimum/3.6
vmin0
print('Minimum velocity at t=0 (m/s): {:.2f}'.format(vmin0))
```

    Minimum velocity at t=0 (m/s): 17.78



```python
rho = rho0.copy()
v = np.ones(nx)
F = np.ones(nx)
for n in range(1,nt):
    F[1:]=Vmax*rho[1:]*(1-rho[1:]/rhomax)
    F[:-1]=Vmax*rho[:-1]*(1-rho[:-1]/rhomax)
    rho[1:]=rho[1:]-dt/dx*(F[1:]-F[:-1])
    v=F/rho
    if n==50:
        vmean3 = np.mean(v)
        vms3 = vmean3/3.6
        print('Average velocity at t=3 min (m/s): {:.2f}'.format(vms3))
        
vmin = np.min(v)
vmsmin = vmin/3.6
print('Minimum velocity at t=6 min (m/s): {:.2f}'.format(vmsmin))
```

    Average velocity at t=3 min (m/s): 20.62
    Minimum velocity at t=6 min (m/s): 18.74



```python
vmsmin
```




    18.744756132701628




```python
# Plot the solution after nt time steps
# along with the initial conditions.
pyplot.figure(figsize=(6.0, 4.0))
pyplot.xlabel('x')
pyplot.ylabel('rho0,rho,v')
pyplot.grid()
pyplot.plot(x, rho0, label='Rho0',
            color='C0', linestyle='-', linewidth=2)
pyplot.plot(x, rho, label='Rho',
            color='C1', linestyle='-', linewidth=2)
pyplot.plot(x, v, label='v',
            color='C2', linestyle='-', linewidth=2)
pyplot.legend(loc='center right')

```


```python
pyplot.figure(figsize=(6.0, 4.0))
pyplot.xlabel('x')
pyplot.ylabel('F')
pyplot.grid()
pyplot.plot(x, F, label='Flux',
            color='C3', linestyle='-', linewidth=2)
pyplot.legend(loc='upper left')
```


```python
#Part B
```


```python
#Conditions 
Vmax = 136 #km/h
L = 11 #km
rhomax = 250 #cars/km
nx = 51 #spatial discretization
dt = 0.001 #h
dx = L/(nx-1)

```


```python
x = np.linspace(0,L,nx)
rho0 = np.ones(nx)*20
rho0[10:20] = 50
```


```python
T1 = 3/60.0
T1
nt = int(T1/dt)
print('Number of steps',nt)
```

    Number of steps 50



```python
v = np.ones(nx)

for i in range(nx):

    v[i] = Vmax*(1 - (rho0[i]/rhomax))
```


```python
vminimum = np.min(v)
vmin0 = vminimum/3.6
vmin0
print('Minimum velocity at t=0 (m/s): {:.2f}'.format(vmin0))
```

    Minimum velocity at t=0 (m/s): 30.22



```python
rho = rho0.copy()
v = np.ones(nx)
F = np.ones(nx)
for n in range(1,nt):
    F[1:]=Vmax*rho[1:]*(1-rho[1:]/rhomax)
    F[:-1]=Vmax*rho[:-1]*(1-rho[:-1]/rhomax)
    rho[1:]=rho[1:]-dt/dx*(F[1:]-F[:-1])
    v=F/rho

vmean3 = np.mean(v)
vms3 = vmean3/3.6
print('Average velocity at t=3 min (m/s): {:.2f}'.format(vms3))

vmin = np.min(v)
vmsmin = vmin/3.6
print('Minimum velocity at t=3 min (m/s): {:.2f}'.format(vmsmin))
```

    Average velocity at t=3 min (m/s): 33.88
    Minimum velocity at t=3 min (m/s): 30.83



```python
# Plot the solution after nt time steps
# along with the initial conditions.
pyplot.figure(figsize=(6.0, 4.0))
pyplot.xlabel('x')
pyplot.ylabel('rho0,rho,v')
pyplot.grid()
pyplot.plot(x, rho0, label='Rho0',
            color='C0', linestyle='-', linewidth=2)
pyplot.plot(x, rho, label='Rho',
            color='C1', linestyle='-', linewidth=2)
pyplot.plot(x, v, label='v',
            color='C2', linestyle='-', linewidth=2)
pyplot.legend(loc='center right')
```


```python
pyplot.figure(figsize=(6.0, 4.0))
pyplot.xlabel('x')
pyplot.ylabel('F')
pyplot.grid()
pyplot.plot(x, F, label='Flux',
            color='C3', linestyle='-', linewidth=2)
pyplot.legend(loc='upper left')
```

f95b546199d4380d9553d9f58b82dd3d990f05f0
ipynb
Jupyter Notebook
working/Traffic_flow/Module2-Traffic_flow_Assignment-AndresMenes.ipynb
AndresMenes/numerical-mooc
0b5ea2b9875152ee36bccd0261c0ef98142259a2
CC-BY-3.0
working/Traffic_flow/Module2-Traffic_flow_Assignment-AndresMenes.ipynb
AndresMenes/numerical-mooc
0b5ea2b9875152ee36bccd0261c0ef98142259a2
CC-BY-3.0
working/Traffic_flow/Module2-Traffic_flow_Assignment-AndresMenes.ipynb
AndresMenes/numerical-mooc
0b5ea2b9875152ee36bccd0261c0ef98142259a2
CC-BY-3.0
Qwen/Qwen-72B
1. YES
2. YES