CFD Python：求解Navier-Stokes方程的12個步驟(2)

(本文經Prof. Lorena A. Barba授權轉載翻譯，原文鏈結請參照底下參考資料)

CFD Python：求解Navier-Stokes方程的12個步驟(1)
CFD Python：求解Navier-Stokes方程的12個步驟(2)
CFD Python：求解Navier-Stokes方程的12個步驟(3)
CFD Python：求解Navier-Stokes方程的12個步驟(4)

步驟2：一維非線性對流方程

\frac{\partial u}{\partial t} + u \frac{\partial u}{\partial x} = 0

\frac{u_i^{n+1}-u_i^n}{\Delta t} + u_i^n \frac{u_i^n-u_{i-1}^n}{\Delta x} = 0

u_i^{n+1} = u_i^n - u_i^n \frac{\Delta t}{\Delta x} (u_i^n - u_{i-1}^n)

import numpy                 # we're importing numpy
from matplotlib import pyplot    # and our 2D plotting library
%matplotlib inline

nx = 41
dx = 2 / (nx - 1)
nt = 20    #nt is the number of timesteps we want to calculate
dt = .025  #dt is the amount of time each timestep covers (delta t)

u = numpy.ones(nx)      #as before, we initialize u with every value equal to 1.
u[int(.5 / dx) : int(1 / dx + 1)] = 2  #then set u = 2 between 0.5 and 1 as per our I.C.s

un = numpy.ones(nx) #initialize our placeholder array un, to hold the time-stepped solution

for n in range(nt):  #iterate through time
un = u.copy() ##copy the existing values of u into un
for i in range(1, nx):  ##now we'll iterate through the u array

###This is the line from Step 1, copied exactly.  Edit it for our new equation.
###then uncomment it and run the cell to evaluate Step 2

###u[i] = un[i] - c * dt / dx * (un[i] - un[i-1])

pyplot.plot(numpy.linspace(0, 2, nx), u) ##Plot the results

1. 12 steps to Navier–Stokes: Step 2: Nonlinear ConvectionLorena A. Barba