I want to solve the advection equation ($v_0 \in \mathbb R$) $$ \frac{\partial f}{\partial t} + v_0 \frac{\partial f}{\partial x} = 0 $$

using 2nd order Runge Kutta like the midpoint rule for the time integration and an upwind (backwards finite differences) for the spatial derivation.

Here is a general Runge-Kutta implementation in python

def rk2a( f, x0, t ):
    n = len( t )
    x = np.array( [ x0 ] * n )
    for i in range( n - 1 ):
        h = t[i+1] - t[i]
        k1 = h * f( x[i], t[i] ) / 2.0
        x[i+1] = x[i] + h * f( x[i] + k1, t[i] + h / 2.0 )

    return x

And that makes sense and if I'd have to solve something like $y'=f=x$ I'd know how to pass $f$ but I'm confused because now we have $f=-v_0\frac{\partial f}{\partial x}$.

Since I want to use an upwind scheme for the spatial derivative, we have

$$ \frac{\partial f}{\partial x}|_{i-1/2} = \frac{q^{n}_i - q^{n}_{i-1}}{x_i - x_{i-1}} $$

whereas $n$ denotes the $n$-th timestep and $i$ denotes the $i$-th grid point.

Now to implement that, I'd have to write down f( x[i], t[i] ) and f( x[i] + k1, t[i] + h / 2.0 ) resp. provide a general f.

Now while I think I can write down f( x[i], t[i] ), I have no idea how I'd write down f( x[i] + k1, t[i] + h / 2.0 ).

So the question is: How do I use a upwind scheme in the midpoint rule?

Edit: I assume equispaced grid.

  • 2
    $\begingroup$ I think you would find it very helpful to read a good introductory book on numerical methods. For instance, chapter 10 of LeVeque's finite difference book will answer your question and give you a lot of other important information about what you're doing. $\endgroup$ Commented Feb 8, 2022 at 4:02
  • $\begingroup$ What you have written here is a bit confusing because you are writing $f$ to mean two different things (the solution of the PDE and the RHS of the ODE). $\endgroup$ Commented Feb 8, 2022 at 4:03
  • $\begingroup$ Ah, that's an issue stemming from rewriting the topic. I know that there's a difference between the f's. $\endgroup$
    – xotix
    Commented Feb 8, 2022 at 7:12


Your Answer

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

Browse other questions tagged or ask your own question.