# Finite difference method not working for advection PDE with negative coefficient

I'm trying to solve a very simple advection PDE

$\frac{\partial u}{\partial t}+c\frac{\partial u}{\partial x}=0$

where $c<0$.

I have been able to implement a simple Modelica code to solve the equation and it works just fine for $c>0$ with the exact same boundary conditions, though. So my question is why my code is not able to finish the simulation:

1. Is it a mathematical issue? Like such PDE never has a solution for $c<0$?
2. is it a finite difference limitation and maybe I should use other numerical methods?
3. or there is some issue with the code or Modelica compiler?

I would appreciate if you could help me figure this issue out.

P.S. the goal for me is to learn how to solve PDEs in Modelica and this is just one example.

and it works just fine for c>0 with the exact same boundary conditions, though.

That's your issue. Look up the method of characteristics:

https://web.stanford.edu/class/math220a/handouts/firstorder.pdf

The characteristics for a 1D advection problem flow in one direction. So there are two things involved. One is that the BCs on one side of the interval matter since the characteristics are only flowing in one direction. Secondly,

https://en.wikipedia.org/wiki/Upwind_scheme

the upwind scheme has to change directions depending on the direction of the characteristics. So you need to change your implementation to match the direction as given by sign(c). When c<0, you need to use u[i+1] instead of u[i-1].

• I'm not sure if I understand this correctly. would you be so kind to implement in Modelica? you may edit my post in SO?
• Did you read the Wiki on the upwind scheme? u[i] = u[i + 1] + dx * ux[i] the direction changes when c changes sign. This means you'll have to treat the boundaries differently too, since the boundary is just the first/last term. The boundary is implicitly done by your loop being i in 2:N. You change that to i in 1:N-1. That should be more than enough hints to fix it. Aug 5, 2017 at 22:12
• I actually did the same thing and got the error : Variable ux[100] is not present in any equation. That's why I asked :) Should I also change the BC so it starts form the end?