Prof. Tony Saad (www.tsaad.net)
Department of Chemical Engineering
University of Utah
We will be solving the linear advection equation
\begin{equation}
\frac{\partial u}{\partial t} = - c \frac{\partial u}{\partial x}
\end{equation}
subject to the initial condition
import numpy as np
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
import matplotlib.pyplot as plt
import matplotlib.animation as animation
plt.rcParams['animation.html'] = 'html5' # this is used to display animations in jupyter notebooks
Let's start by defining an initial condition - let's take \begin{equation} u_0(x) = \sin(\omega x) \end{equation}
n = 41
L = 1.0
dx = L/(n-1)
x = np.linspace(0,L,n)
u0 = np.sin(2.0*np.pi*x)
plt.plot(x,u0,'o')
plt.grid()
Using a forward Euler method for the time derivative and a central discretization for the spatial derivative, the FTCS scheme results in \begin{equation} u_i^{n + 1} = u_i^n + c\Delta t\frac{u_{i + 1}^n - u_{i - 1}^n}{2\Delta x} \end{equation}
# the following implementation does not use ghost cells
dt = 0.001 # s
tend = 2.0 # s
c = 10.0 # m/s - wave speed
cfl = c*dt/2.0/dx
sol = []
sol.append(u0)
t = 0.0
while t < tend:
un = sol[-1]
unew = un.copy()
unew[1:-1] = un[1:-1] - cfl * (un[2:] - un[:-2])
unew[-1] = un[-1] - cfl*(un[1] - un[-2]) # compute last point on the right using periodicity
unew[0] = unew[-1] # set periodic boundary on the left
sol.append(unew)
t += dt
ims = []
fig = plt.figure(figsize=[4,3])
plt.grid()
i = 0
for solution in sol:
if (i%10==0): # output frequency for frames
im = plt.plot(x,solution,'k-',animated=True)
plt.ylim(-1,1)
ims.append(im)
i+=1
ani = animation.ArtistAnimation(fig, ims, interval=35, blit=True,
repeat_delay=1000)
# ani.save('ftcs.mp4')
ani
We have shown in class that the previous FTCS scheme is unconditionally unstable. Here we will implement the upwind scheme in space
tend = 1.0 # s
c = 1.0 # m/s - wave speed
dt = dx/c # run just at the CFL condition
cfl = c*dt/dx
sol = []
sol.append(u0)
t = 0.0
while t < tend:
un = sol[-1]
unew = un.copy()
unew[1:-1] = un[1:-1] - cfl * (un[1:-1] - un[:-2])
unew[-1] = un[-1] - cfl*(un[-1] - un[-2]) # compute last point on the right using periodicity
unew[0] = unew[-1] # set periodic boundary on the left
sol.append(unew)
t += dt
ims = []
fig = plt.figure(figsize=[5,4],dpi = 150)
plt.grid()
i = 0
for solution in sol:
if (i%2==0):
im = plt.plot(x,solution,'k-',animated=True)
plt.ylim(-1,1)
ims.append(im)
i+=1
ani = animation.ArtistAnimation(fig, ims, interval=35, blit=True,
repeat_delay=1000)
# ani.save('ftus.mp4')
ani
import urllib
import requests
from IPython.core.display import HTML
def css_styling():
styles = requests.get("https://raw.githubusercontent.com/saadtony/NumericalMethods/master/styles/custom.css")
return HTML(styles.text)
css_styling()
CSS style adapted from https://github.com/barbagroup/CFDPython. Copyright (c) Barba group
<style> @font-face { font-family: "Computer Modern"; src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf'); } /*div.cell{ width:800px; margin-left:16% !important; margin-right:auto; } */ /* set the font size in tables */ tr, td, th{ font-size:110%; } /* spec for headers */ h1 { font-family: 'Bitter', serif; } h2 { font-family: 'Fenix', serif; } h3{ font-family: 'Fenix', serif; margin-top:12px; margin-bottom: 3px; } h4{ font-family: 'Fenix', serif; } h5 { font-family: 'Alegreya Sans', sans-serif; } div.text_cell_render{ font-family: 'Merriweather','Alegreya Sans','Lora', 'Oxygen', "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif; line-height: 160%; font-size: 130%; } .CodeMirror{ font-family: "Source Code Pro"; font-size: 100%; } .text_cell_render h1 { font-weight: 200; font-size: 32pt; line-height: 120%; color:#CD2305; margin-bottom: 0.5em; margin-top: 0.5em; display: block; } .text_cell_render h2 { font-size: 26pt; text-align: center; } .text_cell_render h3 { font-size: 20pt; } .text_cell_render h4 { font-size: 18pt; } .text_cell_render h5 { font-weight: 300; font-size: 16pt; color: #CD2305; font-style: italic; margin-bottom: .5em; margin-top: 0.5em; display: block; } .warning{ color: rgb( 240, 20, 20 ) } /* div#notebook {background-color: #1e1e1e; border-top: none;} div#notebook-container {background-color: rgb(180, 180, 180);} */ </style> <script> MathJax.Hub.Config({ TeX: { extensions: ["AMSmath.js"] }, tex2jax: { inlineMath: [ ['$','$'], ["\\(","\\)"] ], displayMath: [ ['$$','$$'], ["\\[","\\]"] ] }, displayAlign: 'center', // Change this to 'center' to center equations. "HTML-CSS": { availableFonts: ["TeX"], scale: 100, styles: {'.MathJax_Display': {"margin": 4}} } }); </script>