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

(本文經Prof. Lorena A. Barba授權轉載翻譯自Prof. Lorena A. Barba的教材”12 Steps to Navier-Stokes”，原文鏈結請參照底下參考資料)

Text provided under a Creative Commons Attribution license, CC-BY. All code is made available under the FSF-approved BSD-3 license. (c) Lorena A. Barba, Gilbert F. Forsyth 2017. Thanks to NSF for support via CAREER award #1149784.

CFD Python：求解Navier-Stokes方程的12個步驟(1)
CFD Python：求解Navier-Stokes方程的12個步驟(2)
CFD Python：求解Navier-Stokes方程的12個步驟(3)
CFD Python：求解Navier-Stokes方程的12個步驟(4)
CFD Python：求解Navier-Stokes方程的12個步驟(5)
CFD Python：求解Navier-Stokes方程的12個步驟(6)
CFD Python：求解Navier-Stokes方程的12個步驟(7)
CFD Python：求解Navier-Stokes方程的12個步驟(8)
CFD Python：求解Navier-Stokes方程的12個步驟(9)
CFD Python：求解Navier-Stokes方程的12個步驟(10)
CFD Python：求解Navier-Stokes方程的12個步驟(11)
CFD Python：求解Navier-Stokes方程的12個步驟(12)

## 步驟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