CFD 5
CFD 5
CFDPython (/github/barbagroup/CFDPython/tree/master)
/ lessons (/github/barbagroup/CFDPython/tree/master/lessons)
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.
@LorenaABarba (https://twitter.com/LorenaABarba)
12 steps to Navier–Stokes
Up to now, all of our work has been in one spatial dimension (Steps 1 (./01_Step_1.ipynb) to 4
(./05_Step_4.ipynb)). We can learn a lot in just 1D, but let's grow up to flatland: two dimensions.
In the following exercises, you will extend the first four steps to 2D. To extend the 1D finite-
difference formulas to partial derivatives in 2D or 3D, just apply the definition: a partial derivative
with respect to x is the variation in the x direction at constant y .
xi = x0 + iΔx
yi = y0 + iΔy
Now, define ui,j = u(xi , yj ) and apply the finite-difference formulas on either variable x, y
acting separately on the i and j indices. All derivatives are based on the 2D Taylor expansion of a
mesh point value around ui,j .
Hence, for a first-order partial derivative in the x-direction, a finite-difference formula is:
∂u ∣ ui+1,j − ui,j
∣ = + O(Δx)
∂x ∣ i,j Δx
∂u ∂u ∂u
+ c + c = 0
∂t ∂x ∂y
This is the exact same form as with 1-D Linear Convection, except that we now have two spatial
dimensions to account for as we step forward in time.
https://nbviewer.org/github/barbagroup/CFDPython/blob/master/lessons/07_Step_5.ipynb 1/7
12/26/24, 4:03 PM Jupyter Notebook Viewer
Again, the timestep will be discretized as a forward difference and both spatial steps will be
discretized as backward differences.
With 1-D implementations, we used i subscripts to denote movement in space (e.g. uni n
− u
i−1
).
Now that we have two dimensions to account for, we need to add a second subscript, j, to
account for all the information in the regime.
Here, we'll again use i as the index for our x values, and we'll add the j subscript to track our y
values.
With that in mind, our discretization of the PDE should be relatively straightforward.
n+1 n n n n n
u − u u − u u − u
i,j i,j i,j i−1,j i,j i,j−1
+ c + c = 0
Δt Δx Δy
n+1
Δt Δt
n n n n n
u = u − c (u − u ) − c (u − u )
i,j i,j i,j i−1,j i,j i,j−1
Δx Δy
2 for 0.5 ≤ x, y ≤ 1
u(x, y) = {
1 for everywhere else
x = 0, 2
u = 1 for {
y = 0, 2
https://nbviewer.org/github/barbagroup/CFDPython/blob/master/lessons/07_Step_5.ipynb 2/7
12/26/24, 4:03 PM Jupyter Notebook Viewer
In [1]: from mpl_toolkits.mplot3d import Axes3D ##New Library required for projected 3d plots
import numpy
from matplotlib import pyplot, cm
%matplotlib inline
###variable declarations
nx = 81
ny = 81
nt = 100
c=1
dx = 2 / (nx - 1)
dy = 2 / (ny - 1)
sigma = .2
dt = sigma * dx
x = numpy.linspace(0, 2, nx)
y = numpy.linspace(0, 2, ny)
https://nbviewer.org/github/barbagroup/CFDPython/blob/master/lessons/07_Step_5.ipynb 3/7
12/26/24, 4:03 PM Jupyter Notebook Viewer
3D Plotting Notes
To plot a projected 3D result, make sure that you have added the Axes3D library.
The actual plotting commands are a little more involved than with simple 2d plots.
The first line here is initializing a figure window. The figsize and dpi commands are optional and
simply specify the size and resolution of the figure being produced. You may omit them, but you
will still require the
fig = pyplot.figure()
The next line assigns the plot window the axes label 'ax' and also specifies that it will be a 3d
projection plot. The final line uses the command
plot_surface()
which is equivalent to the regular plot command, but it takes a grid of X and Y values for the data
point positions.
Note
The X and Y values that you pass to plot_surface are not the 1-D vectors x and y . In
order to use matplotlibs 3D plotting functions, you need to generate a grid of x, y values
which correspond to each coordinate in the plotting frame. This coordinate grid is generated
using the numpy function meshgrid .
X, Y = numpy.meshgrid(x, y)
https://nbviewer.org/github/barbagroup/CFDPython/blob/master/lessons/07_Step_5.ipynb 4/7
12/26/24, 4:03 PM Jupyter Notebook Viewer
Array Operations
Here the same 2D convection code is implemented, but instead of using nested for-loops, the
same calculations are evaluated using array operations.
https://nbviewer.org/github/barbagroup/CFDPython/blob/master/lessons/07_Step_5.ipynb 5/7
12/26/24, 4:03 PM Jupyter Notebook Viewer
Learn More
The video lesson that walks you through the details for Step 5 (and onwards to Step 8) is Video
Lesson 6 on You Tube:
https://nbviewer.org/github/barbagroup/CFDPython/blob/master/lessons/07_Step_5.ipynb 6/7
12/26/24, 4:03 PM Jupyter Notebook Viewer
Out[4]:
Out[5]:
https://nbviewer.org/github/barbagroup/CFDPython/blob/master/lessons/07_Step_5.ipynb 7/7