Linear Convection Using Python

Deepak Poudel
3 min readApr 30, 2021

The 1-D Linear Convection equation is the simplest and the most accessible equation in CFD; from the Navier Stokes equation we kept only the accumulation and convection terms for the x-component of the velocity, to make things even simpler, the coefficient of the first derivative of the velocity is constant, making the equation linear. The equation is:

With given initial conditions, this equation represents the propagation of that initial wave with speed c, without change of shape. Let the initial condition be

Now, we discretize this equation in both space and time, using the forward Difference Scheme for the time derivative and the Backward Differentiation Scheme for the space derivative. Consider discretizing the spatial coordinate x into points that we index from i=0 to N, and Stepping in discrete time intervals of size Δt.

From the definition of a derivative, we know that:

Then, our discrete equation is :

where, n and n+1 are two consecutive steps in time, while ‘i-1’ and I are two neighboring points of the discretized x coordinate. If there are given initial conditions, then the only unknown in this discretization is u_i^{n+1}. We can solve for our unknown to get an equation that allows us to advance in time, as follows:

Now, this will be implemented in Python,

Now, we will implement the discretization of the convection equation using a finite difference scheme. For every element of our array u, we need to perform the operation.

We will store the result in a new array u_new, which will be the solution uu for the next time step. We will repeat this operation for as many as many time-steps as we specify and then we can see how fat the wave has convicted.

We first initialize our placeholder array u_new to hold the values we calculate from n+1 timestep, using once again the NumPy function ones ( ).

Then, we will iterate through the array using for loop.

It is clear that the wave is traveling in a positive direction, with no change in shape; the height of the wave is changed until the solution reaches convergence.

--

--

Deepak Poudel

I like to write sometimes. I am fascinated by space, fighter jets, rockets and I love writing about those things. Have an awesome day!!