I asked a similar question earlier, but I am now going to try to make the question more specific:
I have an equation which I would like to discretize:
$x \cdot u(t) \cdot \frac{\partial \rho}{\partial x}=a$
where:
- $a$ is just some constant
- $x$ is the spatial coordinate
- $u(t)$ is the velocity, and is only time-dependent
- $\rho$ is the variable which is to be calculated, here density
The left side of the equation above represents some sort of a flux (flow of fluid).
I need to use the upwind scheme for stability reasons, and this is how I have tried to do it:
$F_{II}=x_i \cdot u(t) \cdot \rho_{i+\frac{1}{2}}=x_i \cdot u(t) \cdot \left(\frac{\rho_{i+1}+\rho_i}{2}\right)-\frac{|x_i \cdot u(t)|}{2}\left(\rho_{i+1}-\rho_i\right)$
$F_{I}=x_i \cdot u(t) \cdot \rho_{i-\frac{1}{2}}=x_i \cdot u(t) \cdot \left(\frac{\rho_{i}+\rho_{i-1}}{2}\right)-\frac{|x_i \cdot u(t)|}{2}\left(\rho_{i}-\rho_{i-1}\right)$
where $F_I$ and $F_{II}$ are the fluxes coming into cell $i$ ($F_I$) and out of cell $i$ ($F_{II}$).
The reason I have included $-\frac{|x_i \cdot u(t)|}{2}\left(\rho_{i+1}-\rho_i\right)$ and $-\frac{|x_i \cdot u(t)|}{2}\left(\rho_{i}-\rho_{i-1}\right)$ is because we don't know if the velocity $u$ is negative or positive. $\rho$ is defined at the cell-centers (not on the cell-boundaries as $[i+\frac{1}{2}]$ or [$i-\frac{1}{2}]$), so:
- $\rho_{i+\frac{1}{2}}$ must either be approximated as $\rho_i$ or $\rho_{i+1}$. By using the upwind method, $\rho_{i+\frac{1}{2}}$ is approximated as $\rho_{i}$ if the velocity $u$ is positive or $\rho_{i+1}$ if the velocity $u$ is negative.
- $\rho_{i-\frac{1}{2}}$ must be approximated by either $\rho_{i-1}$ if the velocity is positive, or by $\rho_i$ if the velocity is negative.
From the above expression for $F_{II}$ it can be seen that the expression becomes :
- Negative velocity: $F_{II}=-\frac{x_i |u(t)|}{\Delta x}\rho_{i+1}$
- Positive velocity: $F_{II}=\frac{x_i |u(t)|}{\Delta x}\rho_{i}$
The expression for $F_I$ becomes:
- Negative velocity: $F_{I}=-\frac{x_i |u(t)|}{\Delta x}\rho_{i}$
- Positive velocity: $F_{I}=\frac{x_i |u(t)|}{\Delta x}\rho_{i-1}$
The discretized expression is therefore the flux leaving cell $i$ minus the flux coming into cell $i$ divided by the width of cell $i$:
$\frac{F_{II}-F_I}{\Delta x}=a$
Inserting the expressions for the fluxes:
$\frac{F_{II}-F_I}{\Delta x}=\frac{\left(x_i \cdot u(t) \cdot \left(\frac{\rho_{i+1}+\rho_i}{2}\right)-\frac{x_i \cdot |u(t)|}{2}\left(\rho_{i+1}-\rho_i\right)-x_i \cdot u(t) \cdot \left(\frac{\rho_{i}+\rho_{i-1}}{2}\right)+\frac{x_i \cdot |u(t)|}{2}\left(\rho_{i}-\rho_{i-1}\right)\right)}{\Delta x}=a$
This is how I believe it should be, but I am not totally sure if it is correct. My question is therefore if this looks correct, or is there something wrong with the discretization?