The physics of diffusion are:
---write equations---
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.cm as cm
%matplotlib inline
def diffusion(nt, nx, tmax, xmax, nu):
dt = tmax/(nt-1)
dx = xmax/(nx-1)
# Initilisation 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(0,nx-1):
u[i,n+1] = u[i,n] + nu*(dt/dx**2.0)*(u[i+1,n]-2.0*u[i,n]+u[i-1,n])
# X 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 = diffusion(151, 51, 0.5, 2.0, 0.1)
plot(u,x,151,'Figure 1: nu=0.1, nt=151, nx=51, tmax=0.5s')
u,x = diffusion(151, 51, 0.5, 2.0, 0.242)
plot(u,x,151,'Figure 1b: nu=0.242, nt=151, nx=51, tmax=0.5s')
u,x = diffusion(151, 79, 0.5, 2.0, 0.1)
plot(u,x,151,'Figure 2: nu=0.1, nt=151, nx=79, tmax=0.5s')
u,x = diffusion(151, 51, 1.217, 2.0, 0.1)
plot(u,x,151,'Figure 3: nu=0.1, nt=151, nx=51, tmax=1.217s')
Why isn’t the square wave maintained?
Why does increasing the viscosity, spatial points and time period cause instability?