import numpy as np
from matplotlib import pyplot as plt
import matplotlib.cm as cm
#%matplotlib inline
def nonlinear_convection(nt, nx, tmax, xmax, c):
    """
    Returns the velocity field and distance for 1D non-linear convection
    """
    # Inrements in space and time
    dt = tmax/(nt-1)
    dx = xmax/(nx-1)
    
    # Initialization of data structures
    u = np.zeros((nx, nt))
    x = np.zeros(nx)
    
    # BCs
    u[0, :] = u[nx-1, :] = 1
    
    # ICs
    for i in range(1, nx-1):
        if(i > (nx-1)/4 and i < (nx-1)/2):
            u[i, 0] = 2
        else:
            u[i, 0] = 1
            
    # Loop
    for n in range(0, nt-1):
        for i in range(1, nx-1):
            u[i, n+1] = u[i, n] - u[i, n]*(dt/dx)*(u[i, n]-u[i-1, n])
    
    # Loop
    for i in range(0, nx):
        x[i] = i*dx
        
    return u, x
def plot(u, x, nt, title):
    """
    Plots the Velocity Field Results in 1D
    """
    plt.figure()
    plt.show()
    colour=iter(cm.rainbow(np.linspace(0,10,nt)))
    for i in range(0, nt, 10):
        c=next(colour)
        plt.plot(x, u[:,i], c=c)
        plt.xlabel('x [m]')
        plt.ylabel('u [m/s]')
        plt.ylim([0, 2.2])
        plt.title(title)
        plt.grid(True, linestyle='-.', linewidth='0.5', color='black')
u,x = nonlinear_convection(151, 51, 0.5, 2.0, 0.5)
plot(u,x,151,'Figure 1: c=0.5m/s, nt=151, nx=51, tmax=0.5s')
u,x = nonlinear_convection(151, 302, 0.5, 2.0, 0.5)
plot(u,x,151,'Figure 2: c=0.5m/s, nt=151, nx=302, tmax=0.5s')
u,x = nonlinear_convection(151, 51, 2.0, 2.0, 0.5)
plot(u,x,151,'Figure 3: c=0.5m/s, nt=151, nx=51, tmax=2s')
Why isn’t the square wave maintained?
Why does the wave shift to the right?
What happens at the wall?