What would be a good finite difference discretization for the following equation:

$\frac{\partial \rho}{\partial t} + \nabla \cdot \left(\rho u\right)=0$?

We can take the 1D case:

$\frac{\partial \rho}{\partial t} + \frac{d}{dx}\left(\rho u\right)=0$

For some reason all schemes I can find is for the formulation in Lagrangian coordinates. I came up with this scheme for the time being (disregard the j index):

$ \frac{\rho^{n+1}_{i,j}-\rho^{n}_{i,j}}{\tau} + \frac{1}{h_x}\left(\frac{\rho^{n+1}_{i+1,j}+\rho^{n+1}_{i,j}}{2}u^{n}_{_xi+1/2,j}- \frac{\rho^{n+1}_{i,j}+\rho^{n+1}_{i-1,j}}{2}u^{n}_{_xi-1/2}\right)=0$

But seems to be really unstable or have some horrible stability condition. Is that so?

The velocity is actually calculated through the darcy law $u=-\frac{k}{\mu}\nabla p$. Plus we have the equation of state. The full system consists also of an energy equation and the equation of state for the ideal gas. The velocities can turn negative.

  • $\begingroup$ In the 1D case, the problem is essentially a 1st order hyperbolic pde. Have you tried using a first order upwind finite difference scheme? $\endgroup$
    – Paul
    Apr 18, 2012 at 20:04
  • $\begingroup$ So far I am running with what I've written in the question. My case is actually 2d though. But since this is such a classical equation I thought there would some classical discretization available as well. $\endgroup$
    – tiam
    Apr 18, 2012 at 22:17
  • $\begingroup$ Could you show how an upwind scheme would look for this. I am familiar with the concept from the finite volume method when you use it in the convective term, but there you don't have a spacial derivative a product anymore. $\endgroup$
    – tiam
    Apr 18, 2012 at 22:23
  • $\begingroup$ Is the velocity field given, or does it satisfy an evolution equation as well? $\endgroup$ May 7, 2012 at 4:16
  • $\begingroup$ The velocity is actually calculated through the darcy law $u=-\frac{k}{\mu}\nabla p$. The full system consists also of an energy equation and the equation of state for the ideal gas. The velocities can turn negative. $\endgroup$
    – tiam
    May 8, 2012 at 21:51

2 Answers 2


You are looking at the mass conservation equation:


When considering mass evolution per unit volume, this boils down to the density advection equation in flux form:

$\dfrac{\partial \rho}{\partial t} = -\nabla \cdot (\rho u)$

Good thing about this is that it is just the advection equation of an arbitrary scalar field (in our case, this happens to be density $\rho$) and it is (relatively) easy to solve, provided adequate time and space differencing schemes, and initial and boundary conditions.

When designing a finite differencing scheme, we worry about convergence, stability and accuracy. A scheme is converging if $\dfrac{\Delta A}{\Delta t} \rightarrow \dfrac{\partial A}{\partial t}$ when $\Delta t \rightarrow 0$. Stability of the schemes ensures that the quantity $A$ remains finite when $t \rightarrow \infty$. Formal accuracy of the scheme tells where the truncation error in the Taylor expansion series of the partial derivative lies. Look into a CFD textbook for more details on these fundamental properties of a differencing scheme.

Now, the simplest approach is to go straight to 1st order upstream differencing. This scheme is positive-definite, conservative and computationally efficient. The first two properties are especially important when we model the evolution of a quantity which is always positive (i.e. mass or density).

For simplicity, let's look at 1-D case:

$\dfrac{\partial \rho}{\partial t} = -\dfrac{\partial(\rho u)}{\partial x}$

It is convenient now to define the flux $\Phi = \rho u$, so that:

$\dfrac{\partial(\rho u)}{\partial x} = \dfrac{\partial \Phi}{\partial x} \approx \dfrac{\Delta \Phi}{\Delta x} \approx \dfrac{\Phi_{i+1/2}-\Phi_{i-1/2}}{\Delta x}$

Here's a schematic of what we are simulating:

            u           u
|          -->         -->          |
|    rho    |    rho    |    rho    |
     i-1  i-1/2   i   i+1/2  i+1

We are evaluating the evolution of $\rho$ at cell $i$. The net gain or loss comes from the difference of what comes in, $\Phi_{i-1/2}$ and what goes out, $\Phi_{i+1/2}$. This is where we start to diverge from Paul's answer. In true conservative upstream differencing, the quantity at the cell center is being carried by velocity at its cell edge, in the direction of its motion. In other words, if you imagine you are the advected quantity and you are sitting at the cell center, you are being carried into the cell in front of you by the velocity at the cell edge. Evaluating the flux at the cell edge as a product of density and velocity, both at the cell edge, is not correct and does not conserve the advected quantity.

Incoming and outgoing fluxes are evaluated as:

$\Phi_{i+1/2} = \dfrac{u_{i+1/2}+|u_{i+1/2}|}{2}\rho_{i} + \dfrac{u_{i+1/2}-|u_{i+1/2}|}{2}\rho_{i+1}$

$\Phi_{i-1/2} = \dfrac{u_{i-1/2}+|u_{i-1/2}|}{2}\rho_{i-1} + \dfrac{u_{i-1/2}-|u_{i-1/2}|}{2}\rho_{i}$

The above treatment of flux differencing ensures upstream-definiteness. In other words, it adjusts the differencing direction according to the sign of velocity.

The Courant-Friedrichs-Lewy (CFL) stability criterion, when doing time differencing with simple first order, forward Euler differencing is given as:

$\mu = \dfrac{u\Delta t}{\Delta x} \leq 1$

Note that in 2 dimensions, the CFL stability criterion is more strict:

$\mu = \dfrac{c\Delta t}{\Delta x} \leq \dfrac{1}{\sqrt{2}}$

where $c$ is velocity magnitude, $\sqrt{u^{2}+v^{2}}$.

Some things to consider. This scheme may or may not be appropriate for your application depending on what kind of process you are simulating. This scheme is highly diffusive, and is appropriate for very smooth flows without sharp gradients. It is also more diffusive for shorter time steps. In the 1-D case, you will obtain an almost exact solution if the gradients are very small, and if $\mu = 1$. In the 2-D case, this is not possible, and diffusion is anisotropic.

If your physical system considers shock waves or high gradients of other sort, you should look into upstream differencing of higher order (e.g. 3rd or 5th order). Also, it may be worthwhile looking into the Flux Corrected Transport family of schemes (Zalesak, 1979, JCP); anti-diffusion correction for the above scheme by Smolarkiewicz (1984, JCP); MPDATA family of schemes by Smolarkiewicz (1998, JCP).

For time differencing, 1st order forward Euler differencing may be satisfactory for your needs. Otherwise, look into higher-order methods such as Runge-Kutta (iterative), or Adams-Bashforth and Adams-Moulton (multi-level).

It would be worthwhile looking into some CFD graduate-level textbook for a summary of above mentioned schemes and many more.

  • $\begingroup$ Thank you for the answer. Now I clearly see the upwinding :). I will try to implement that now! I am wondering, can the fact that $u$ changes at every timestep affect the stability? $\endgroup$
    – tiam
    May 12, 2012 at 15:22
  • 1
    $\begingroup$ No, as long as you satisfy the CFL constraint. You can do either adaptive time-stepping, i.e. $\Delta t = \dfrac{\Delta x}{max(u)}$, or set $\Delta t$ constant according to maximum expected velocity in your problem. Remember that various combinations of time and space differencing methods will give you different CFL constraints. $\endgroup$ May 13, 2012 at 2:02
  • $\begingroup$ its a bit weird, I implemented the scheme and I managed to send a pulse from one boundary to the other and back again (by reversing the speed). But as soon as I say that $u=-C\nabla \rho$ it begins to demand an extremly small timestep, even though the speed is below 1. Setting the dynamic timestep as u define above didn't help either. $\endgroup$
    – tiam
    May 14, 2012 at 14:19
  • $\begingroup$ Or maybe it aint weird at all, maybe your comment above didn't govern the case where $u$ and $\rho$ are coupled. $\endgroup$
    – tiam
    May 14, 2012 at 14:23
  • $\begingroup$ The stability constraints and order of accuracy are formally derived and valid for linearized advection equation - where $u$ does not depend on $\rho$. In the past, I have successfully coupled this equation with non-linear Navier-Stokes equations for u,v. Formal stability constraints are not satisfied in that case, but keeping your increments reasonably low works. When setting $u=-C\nabla\rho$, your equation becomes $\dfrac{\partial \rho}{\partial t} = C[(\nabla \rho)^{2} + \rho \nabla^{2}\rho]$. You should investigate (if possible) what is the stability criterion for your equation. $\endgroup$ May 14, 2012 at 14:53

In the 1D case, you don't want to use a forward or central difference scheme for the spatial derivative term $(\frac{d}{dx})$ because they are numerically unstable. Instead, it is better to discretize the equation with an explicit backwards (upwind) finite difference for the spatial derivative:

$\frac{\rho_i^{k+1}-\rho_i^k}{\Delta t} + \frac{\rho_i^k U_i^k-\rho_{i-1}^k U_{i-1}^k}{\Delta x} = 0$.

If the velocities are positive, then this backward scheme is stable. If they are negative, then a forward difference will work. Regardless, there is always a constraint on your choice of $\Delta x$ and $\Delta t$ (courant number) to make the scheme stable.

  • $\begingroup$ Would evaluating $\rho$ at $k+1$ instead remove the $\Delta t$ constraint? $\endgroup$
    – tiam
    Apr 28, 2012 at 18:40
  • $\begingroup$ I'm not entirely sure... I think you would have to check the truncation error to make sure that it approximates the PDE correctly. You may want to consider the other implict schemes on this website: web.mit.edu/dongs/www/publications/projects/… $\endgroup$
    – Paul
    Apr 28, 2012 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.