Numerical Solution of 1D Advection Model Using Python Code

Verified

Added on  2022/08/13

|3
|657
|45
Practical Assignment
AI Summary
This assignment presents a Python implementation of a 1D advection model, a fundamental concept in computational physics and fluid dynamics. The code simulates the advection equation using three different numerical schemes: FTBS (Forward Time, Backward Space), FTFS (Forward Time, Forward Space), and CTCS (Centered Time, Centered Space). The code initializes parameters such as the advection velocity, time step, spatial step, and grid size. It then applies each scheme to solve the advection equation over a specified time period, storing the results in arrays. The code includes calculation loops for each scheme and displays the values of 'u' at each time step. Finally, the solution visualizes the results by plotting the value of 'u' at a specific spatial point (x=48) against time for each of the three schemes, allowing for a comparison of their behavior and stability characteristics. The plots utilize the matplotlib library to generate line graphs, providing a clear representation of the numerical solutions.
Document Page
Python code for 1D Advection model:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
# initial parameters
Ubar = 1
delt = 1
delx = 1
nx = 50
tmax = 50
uFTBS = np.zeros([tmax,nx],dtype = float) # rows = time index and columns = x index
uFTFS = np.zeros([tmax,nx],dtype = float)
uCTCS = np.zeros([tmax,nx],dtype = float)
uFTBS[0,:] = 0.1; uFTBS[1,:] = 0 # setting u[0] = 0.1 and u[1] = 0 for all x points
uFTFS[0,:] = 0.1; uFTFS[1,:] = 0
uCTCS[0,:] = 0.1; uCTCS[1,:] = 0
for n in range(tmax-1):
for i in range(1,nx):
uFTBS[n+1,i] = uFTBS[n,i] - Ubar*(delt/delx)*(uFTBS[n,i]-uFTBS[n,i-1]) #
calculation loop for FTBS scheme
for n in range(tmax-1):
for i in range(nx-1):
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
uFTFS[n+1,i] = uFTFS[n,i] - Ubar*(delt/delx)*(uFTFS[n,i+1]-uFTFS[n,i]) # calculation
loop for FTFS scheme
for n in range(1,tmax-1):
for i in range(1,nx-1):
uCTCS[n+1,i] = uCTCS[n-1,i] - Ubar*((2*delt)/(2*delx))*(uCTCS[n,i+1]-uCTCS[n,i-
1]) # calculation loop for CTCS scheme
for n in range(tmax):
print('At t = ',i,'sec u by FTBS scheme at 50 x points are',uFTBS[i,:]) # displaying u in
each time instant
print('At t = ',i,'sec u by FTFS scheme at 50 x points are',uFTFS[i,:]) # displaying u in
each time instant
print('At t = ',i,'sec u by CTCS scheme at 50 x points are',uCTCS[i,:]) # displaying u in
each time instant
# plotting u in a 2d line plot
x = np.linspace(1,nx,nx)
t = np.linspace(0,tmax-1,tmax)
fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(1,2,1)
ax.plot(t, uFTBS[:,48], 'gray') # selecting x point x = 48 for plotting against time
ax.set_xlabel('time t')
ax.set_ylabel('u')
ax.set_title('Line plot of u in FTBS scheme')
ax = fig.add_subplot(2,2,2)
ax.plot(t, uFTFS[:,48], 'red') # selecting x point x = 48 for plotting against time
Document Page
ax.set_xlabel('time t')
ax.set_ylabel('u')
ax.set_title('Line plot of u in FTFS scheme')
ax = fig.add_subplot(2,2,4)
ax.plot(t, uCTCS[:,48], 'green') # selecting x point x = 48 for plotting against time
ax.set_xlabel('time t')
ax.set_ylabel('u')
ax.set_title('Line plot of u in CTCS scheme')
plt.show()
Output:
chevron_up_icon
1 out of 3
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]