Copyright (C) 2022 Andreas Kloeckner
import sympy as sp
x, h, a, b, c, d = sp.symbols("x, h, a, b, c, d")
ubarj, ubarjm1, ubarjp1 = sp.symbols("ubarj, ubarjm1, ubarjp1")
Set u
to a generic polynomial in terms of unknown coefficients a
, b
, c
, ... of the right degree for our reconstruction:
u = a * x + b
u
For simplicity, assume that cell $j$ is the interval $(-h/2, h/2)$, cell $j+1$ is the interval $(h/2,3h/2)$ and so on. Compute the (symbolic) cell averages for cells $j$, $j+1$ and $j-1$, assigning the symbolic values to uavgj
, uavgjm1
and uavgjp1
.
To compute integrals: sp.integrate(u, (x, left, right))
uavgj = sp.integrate(u, (x, -h/2, h/2))/h
uavgjp1 = sp.integrate(u, (x, h/2, 3*h/2))/h
uavgjm1 = sp.integrate(u, (x, -3*h/2, -h/2))/h
uavgj
Find the coefficients for centered reconstruction, i.e. finding the coefficients in u
from $\bar u_j$ and $\bar u_{j+1}$:
Use sp.solve([f1, f2], [unknown1, unknown2])
.
sol = sp.solve([ubarjp1 - uavgjp1, ubarj - uavgj], [a,b])
sol
{a: (-ubarj + ubarjp1)/h, b: ubarj}
Find the reconstructed u
in terms of the cell averages:
Use u.subs({var: value})
.
ureconstructed = u.subs(sol)
ureconstructed
Evaluate the reconstructed u
at the cell interface:
ureconstructed.subs({x: h/2})
Perform the same process for upwind reconstruction, i.e. finding the coefficients in u
from $\bar u_j$ and $\bar u_{j-1}$:
sol = sp.solve([ubarjm1 - uavgjm1, ubarj - uavgj], [a,b])
ureconstructed = u.subs(sol)
ureconstructed.subs({x: h/2})