Académique Documents
Professionnel Documents
Culture Documents
• numpy is a library that provides a bunch of useful matrix operations akin to MATLAB
• matplotlib is a 2D plotting library that we will use to plot our results
• time and sys provide basic timing functions that we’ll use to slow down animations for viewing
def linearconv(nx):
dx = 2./(nx-1)
time = 20*0.025
dt = .025 #dt is the amount of time each timestep covers (delta t)
1
nt = int(time/dt) #nt is the number of timesteps we want to calculate
c = 1
print dx/dt
u = np.ones(nx) #defining a numpy array which is nx elements long with every value equa
u[.5/dx : 1/dx+1]=2 #setting u = 2 between 0.5 and 1 as per our I.C.s
un = np.ones(nx) #initializing our placeholder array, un, to hold the values we calculate fo
plt.plot(np.linspace(0,2,nx),u)
2.0
This is the same result as our Step 1 calculation, reproduced here for reference.
In [3]: linearconv(61)
1.33333333333
2
Here, there is still numerical diffusion present, but it is less severe.
In [4]: linearconv(71)
1.14285714286
Here the same pattern is present – the wave is more square than in the previous runs.
3
In [5]: linearconv(85)
0.952380952381
def linearconv(nx):
4
dx = 2./(nx-1)
time = 20*0.025
c = 1
sigma = .5
dt = sigma*dx/c #dt is the amount of time each timestep covers (delta t)
nt = int(time/dt) #nt is the number of timesteps we want to calculate
print dx/dt
u = np.ones(nx)
u[.5/dx : 1/dx+1]=2
un = np.ones(nx)
plt.plot(np.linspace(0,2,nx),u)
In [7]: linearconv(41)
2.0
In [8]: linearconv(61)
2.0
5
In [9]: linearconv(81)
2.0
In [10]: linearconv(101)
6
2.0
In [11]: linearconv(501)
2.0
7
2 Non-linear Convection
Now we’re going to implement non-linear convection using the same methods as in step 1. The 1D convection
equation is:
∂u ∂u
+u =0
∂t ∂x
Instead of a constant factor c multiplying the second term, now we have the solution u multiplying it.
Thus, the second term of the equation is now non-linear We’re going to use the same discretization as in
Step 1 — forward difference in time and backward difference in space. Here is the discretized equation.
def nonlinearconv(nx):
dx = 2./(nx-1)
time = 20*0.025
sigma = .5
dt = sigma*dx #dt is the amount of time each timestep covers (delta t)
nt = int(time/dt) #nt is the number of timesteps we want to calculate
c = 1
print dt/dx
u = np.ones(nx)
u[.5/dx : 1/dx+1]=2
un = np.ones(nx)
plt.plot(np.linspace(0,2,nx),u)
In [13]: nonlinearconv(41)
0.5
8
In [14]: nonlinearconv(61)
0.5
In [15]: nonlinearconv(81)
9
0.5
In [16]: nonlinearconv(101)
0.5
10
In [17]: nonlinearconv(501)
0.5
∂u ∂2u
=ν 2
∂t ∂x
The first thing you should notice is that —unlike the previous two simple equations we have studied—
this equation has a second-order derivative. We first need to learn what to do with it!
∂2u
3.1 Discretizing ∂x2
The second-order derivative can be represented geometrically as the line tangent to the curve given by the first
derivative. We will discretize the second-order derivative with a Central Difference scheme: a combination
of Forward Difference and Backward Difference of the first derivative. Consider the Taylor expansion of ui+1
and ui−1 around ui :
+ ∆x2 ∂ 2 u2 + ∆x3 ∂ 3 u3 + O(∆x4 )
ui+1 = ui + ∆x ∂u
∂x 2 ∂x 3 ∂x
i i i
∆x2 ∂ 2 u ∆x3 ∂ 3 u
∂u
ui−1 = ui − ∆x ∂x + 2 ∂x2 − 3 ∂x3 + O(∆x4 )
i i i
If we add these two expansions, you can see that the odd-numbered derivative terms will cancel each
other out. If we neglect any terms of O(∆x4 ) or higher (and really, those are very small), then we can
rearrange the sum of these two expansions
to solve for our second-derivative.
2
ui+1 + ui−1 = 2ui + ∆x2 ∂∂xu2 + O(∆x4 )
i
11
∂2u
Then rearrange to solve for ∂x2 and the result is:
i
∂2u ui+1 − 2ui + ui−1
2
= + O(∆x2 )
∂x ∆x2
We can now write the discretized version of the diffusion equation in 1D:
4 Burgers’ Equation
You can read about Burgers’ Equation on its wikipedia page.
Burgers’ equation in one spatial dimension looks like this:
∂u ∂u ∂2u
+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 to 3. Using forward difference
for time, backward difference for space and our 2nd-order method for the second derivatives yields:
2ν ∂φ
u = − +4 (1)
φ ∂x
2
−(x − 2π)2
−x
φ = exp + exp (2)
4ν 4ν
This has an analytical solution, given by:
2ν ∂φ
u = − +4 (3)
φ ∂x
−(x − 4t)2 −(x − 4t − 2π)2
φ = exp + exp (4)
4ν(t + 1) 4ν(t + 1)
12
Our boundary condition will be:
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.
13