In [1]:
import numpy as np
import matplotlib.pyplot as plt
import bisect
from IPython.html.widgets import interact

# example based on http://lorenabarba.com/blog/cfd-python-12-steps-to-navier-stokes/

In [2]:
c = 1.0

In [3]:
# example showing numerical diffusion and osciliations
def plot(nx, dt, nt, bc):
    # create the x grid
    x = np.arange(0,1,1.0/nx)
    # equidistant grid
    dx = x[1] - x[0]
    # initial waterlevel
    s1 = np.zeros(nx) + 0.1
    # use current waterlevel as previous waterlevel
    s0 = s1.copy()
    for n in range(nt):  
        # set the boundary condition
        s1[0] = bc
        # upwind scheme
        s1[1:] = s0[1:] - c*dt/dx*(s0[1:] - s0[:-1])
        # set previous to current
        s0 = s1.copy()
    plt.step(x, s1)
    plt.ylim(0, 3)
_ = interact(plot, nx=(10, 100), dt=(0.005, 0.02,0.001), bc=(0, 2.0), nt=(0,50))



In [3]: