CFD-4
CFD-4
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
We continue our journey to solve the Navier–Stokes equation with Step 4. But don't continue
unless you have completed the previous steps! In fact, this next step will be a combination of the
two previous ones. The wonders of code reuse!
2
∂u ∂u ∂ u
+ u = ν
2
∂t ∂x ∂x
As you can see, it is a combination of non-linear convection and diffusion. It is surprising how
much you learn from this neat little equation!
We can discretize it using the methods we've already detailed in Steps 1 (./01_Step_1.ipynb) to
3 (./04_Step_3.ipynb). Using forward difference for time, backward difference for space and
our 2nd-order method for the second derivatives yields:
n+1 n n n n n n
u − u u − u u − 2u + u
i i i i−1 i+1 i i−1
n
+ u = ν
i 2
Δt Δx Δx
As before, once we have an initial condition, the only unknown is un+1
i
. We will step in time as
follows:
n+1
Δt Δt
n n n n n n n
ui = u − u (u − u ) + ν (u − 2u + u )
i i i i−1 i+1 i i−1
2
Δx Δx
https://nbviewer.org/github/barbagroup/CFDPython/blob/master/lessons/05_Step_4.ipynb 1/7
11/21/24, 1:55 PM Jupyter Notebook Viewer
2ν ∂ ϕ
u = − + 4 (1)
ϕ ∂x
2 2
−x −(x − 2π)
ϕ = exp ( ) + exp ( ) (2)
4ν 4ν
2ν ∂ ϕ
u = − + 4 (3)
ϕ ∂x
2 2
−(x − 4t) −(x − 4t − 2π)
ϕ = exp ( ) + exp ( ) (4)
4ν (t + 1) 4ν (t + 1)
u(0) = u(2π)
This is called a periodic boundary condition. Pay attention! This will cause you a bit of headache if
you don't tread carefully.
∂x
SymPy (http://sympy.org/en/) is the symbolic math library for Python. It has a lot of the same
symbolic math functionality as Mathematica with the added benefit that we can easily translate
its results back into our Python calculations (it is also free and open source).
Start by loading the SymPy library, together with our favorite library, NumPy.
We're also going to tell SymPy that we want all of its output to be rendered using LATEX. This
will make our Notebook beautiful!
Start by setting up symbolic variables for the three variables in our initial condition and then type
out the full equation for ϕ . We should get a nicely rendered version of our ϕ equation.
https://nbviewer.org/github/barbagroup/CFDPython/blob/master/lessons/05_Step_4.ipynb 2/7
11/21/24, 1:55 PM Jupyter Notebook Viewer
Out[3]:
2 2
(−4t+x−2π) (−4t+x)
− −
4ν(t+1) 4ν(t+1)
e + e
It's maybe a little small, but that looks right. Now to evaluate our partial derivative is a trivial
∂ϕ
∂x
task.
Out[4]:
2
(−4t+x)
− 2
(−4t+x−2π)
4ν(t+1)
e 1 −
4ν(t+1)
− (−8t + 2x) − (−8t + 2x − 4π) e
4ν (t + 1) 4ν (t + 1)
If you want to see the unrendered version, just use the Python print command.
In [5]: print(phiprime)
Now what?
Now that we have the Pythonic version of our derivative, we can finish writing out the full initial
condition equation and then translate it into a usable Python expression. For this, we'll use the
lambdify function, which takes a SymPy symbolic equation and turns it into a callable function.
u = -2 * nu * (phiprime / phi) + 4
print(u)
Lambdify
To lambdify this expression into a useable function, we tell lambdify which variables to request
and the function we want to plug them in to.
3.49170664206
https://nbviewer.org/github/barbagroup/CFDPython/blob/master/lessons/05_Step_4.ipynb 3/7
11/21/24, 1:55 PM Jupyter Notebook Viewer
###variable declarations
nx = 101
nt = 100
dx = 2 * numpy.pi / (nx - 1)
nu = .07
dt = dx * nu
https://nbviewer.org/github/barbagroup/CFDPython/blob/master/lessons/05_Step_4.ipynb 4/7
11/21/24, 1:55 PM Jupyter Notebook Viewer
This is definitely not the hat function we've been dealing with until now. We call it a "saw-tooth
function". Let's proceed forward and see what happens.
With periodic boundary conditions, when a point gets to the right-hand side of the frame, it
wraps around back to the front of the frame.
Recall the discretization that we worked out at the beginning of this notebook:
n+1
Δt Δt
n n n n n n n
u = u − u (u − u ) + ν (u − 2u + u )
i i i i i−1 2 i+1 i i−1
Δx Δx
What does uni+1 mean when i is already at the end of the frame?
https://nbviewer.org/github/barbagroup/CFDPython/blob/master/lessons/05_Step_4.ipynb 5/7
11/21/24, 1:55 PM Jupyter Notebook Viewer
What next?
The subsequent steps, from 5 to 12, will be in two dimensions. But it is easy to extend the 1D
finite-difference formulas to the partial derivatives in 2D or 3D. Just apply the definition — a
partial derivative with respect to x is the variation in the x direction while keeping y constant.
Before moving on to Step 5 (./07_Step_5.ipynb), make sure you have completed your own code
for steps 1 through 4 and you have experimented with the parameters and thought about what
is happening. Also, we recommend that you take a slight break to learn about array operations
with NumPy (./06_Array_Operations_with_NumPy.ipynb).
https://nbviewer.org/github/barbagroup/CFDPython/blob/master/lessons/05_Step_4.ipynb 6/7
11/21/24, 1:55 PM Jupyter Notebook Viewer
Out[12]:
https://nbviewer.org/github/barbagroup/CFDPython/blob/master/lessons/05_Step_4.ipynb 7/7