@@ -72,7 +72,7 @@ u(i\Delta x, (n+1)\Delta t) &= I(i\Delta x - v(n+1)\Delta t) \nonumber \\
7272provided $v = \Delta x/\Delta t$. So, whenever we see a scheme that
7373collapses to
7474$$
75- u^{n+1}** i = u ** {i-1}^n,
75+ u^{n+1}_ i = u _ {i-1}^n,
7676$$ {#eq-advec-1D-pde1-uprop2}
7777for the PDE in question, we have in fact a scheme that reproduces the
7878analytical solution, and many of the schemes to be presented possess
771771$$
772772which results in the updating formula
773773$$
774- u^{n+1}_ i = u^{n-1}** i - C(u ** {i+1}^n-u_ {i-1}^n)\tp
774+ u^{n+1}_ i = u^{n-1}_ i - C(u _ {i+1}^n-u_ {i-1}^n)\tp
775775$$
776776A special scheme is needed to compute $u^1$, but we leave that problem for
777- now. Anyway, this special scheme can be found in
778- [`advec1D.py`](https://github.com/devitocodes/devito_book/tree/main/src/advec/advec1D.py).
777+ now. The Devito implementation handles this automatically; see
778+ [`advec1D_devito.py`](https://github.com/devitocodes/devito_book/tree/main/src/advec/advec1D_devito.py)
779+ and @sec-advec-devito for details.
779780
780781### Implementation
781782We now need to work with three time levels and must modify our solver a bit:
888889$$ {#eq-advec-1D-upwind}
889890written out as
890891$$
891- u^{n+1}_ i = u^n_i - C(u^{n}** {i}-u^{n}** {i-1}),
892+ u^{n+1}_ i = u^n_i - C(u^{n}_ {i}-u^{n}_ {i-1}),
892893$$
893894gives a generally popular and robust scheme that is stable if $C\leq 1$.
894895As with the Leapfrog scheme, it becomes exact if $C=1$, exactly as shown in
929930$$
930931by a forward difference in time and centered differences in space,
931932$$
932- D^+** t u + vD ** {2x} u = \nu D_xD_x u] ^n_i,
933+ D^+_ t u + vD _ {2x} u = \nu D_xD_x u] ^n_i,
933934$$
934935actually gives the upwind scheme (@eq-advec-1D-upwind) if
935936$\nu = v\Delta x/2$. That is, solving the PDE $u_t + vu_x=0$
@@ -1170,30 +1171,31 @@ def run(scheme='UP', case='gaussian', C=1, dt=0.01):
11701171 os.system(cmd)
11711172 print 'Integral of u:', integral.max(), integral.min()
11721173```
1173- The complete code is found in the file
1174- [`advec1D.py`](https://github.com/devitocodes/devito_book/tree/main/src/advec/advec1D.py).
1174+ The Devito implementation is found in
1175+ [`advec1D_devito.py`](https://github.com/devitocodes/devito_book/tree/main/src/advec/advec1D_devito.py).
1176+ See @sec-advec-devito for the complete implementation.
11751177
11761178## A Crank-Nicolson discretization in time and centered differences in space {#sec-advec-1D-CN}
11771179
11781180Another obvious candidate for time discretization is the Crank-Nicolson
11791181method combined with centered differences in space:
11801182$$
1181- [ D_t u] ^n_i + v\half([ D_ {2x} u] ^{n+1}** i + [ D ** {2x} u] ^{n}_ i) = 0\tp
1183+ [ D_t u] ^n_i + v\half([ D_ {2x} u] ^{n+1}_ i + [ D _ {2x} u] ^{n}_ i) = 0\tp
11821184$$
11831185It can be nice to include the Backward Euler scheme too, via the
11841186$\theta$-rule,
11851187$$
1186- [ D_t u] ^n_i + v\theta [ D_ {2x} u] ^{n+1}** i + v(1-\theta)[ D ** {2x} u] ^{n}_ i = 0\tp
1188+ [ D_t u] ^n_i + v\theta [ D_ {2x} u] ^{n+1}_ i + v(1-\theta)[ D _ {2x} u] ^{n}_ i = 0\tp
11871189$$
11881190When $\theta$ is different from zero, this gives rise to an *implicit* scheme,
11891191$$
1190- u^{n+1}** i + \frac{\theta}{2} C (u^{n+1}** {i+1} - u^{n+1}_ {i-1})
1191- = u^n_i - \frac{1-\theta}{2} C (u^{n}** {i+1} - u^{n}** {i-1})
1192+ u^{n+1}_ i + \frac{\theta}{2} C (u^{n+1}_ {i+1} - u^{n+1}_ {i-1})
1193+ = u^n_i - \frac{1-\theta}{2} C (u^{n}_ {i+1} - u^{n}_ {i-1})
11921194$$
11931195for $i=1,\ldots,N_x-1$. At the boundaries we set $u=0$ and simulate just to
11941196the point of time when the signal hits the boundary (and gets reflected).
11951197$$
1196- u^{n+1}** 0 = u^{n+1}** {N_x} = 0\tp
1198+ u^{n+1}_ 0 = u^{n+1}_ {N_x} = 0\tp
11971199$$
11981200The elements on the diagonal in the matrix become:
11991201$$
@@ -1208,7 +1210,7 @@ And finally, the right-hand side becomes
12081210
12091211\begin{align*}
12101212b_0 &= u^n_{N_x}\\
1211- b_i &= u^n_i - \frac{1-\theta}{2} C (u^{n}** {i+1} - u^{n}** {i-1}),\quad i=1,\ldots,N_x-1\\
1213+ b_i &= u^n_i - \frac{1-\theta}{2} C (u^{n}_ {i+1} - u^{n}_ {i-1}),\quad i=1,\ldots,N_x-1\\
12121214b_{N_x} &= u^n_0
12131215\end{align*}
12141216
@@ -1273,7 +1275,7 @@ u^{n+1}_i = u^n_i -v \Delta t [D_{2x} u]^n_i
12731275$$
12741276or written out,
12751277$$
1276- u^{n+1}_ i = u^n_i - \frac{1}{2} C (u^{n}** {i+1} - u^{n}** {i-1})
1278+ u^{n+1}_ i = u^n_i - \frac{1}{2} C (u^{n}_ {i+1} - u^{n}_ {i-1})
12771279+ \frac{1}{2} C^2 (u^{n}_ {i+1}-2u^n_i+u^n_ {i-1})\tp
12781280$$
12791281This is the explicit Lax-Wendroff scheme.
@@ -1576,15 +1578,15 @@ is the dominating term, collect its information in the flow direction, i.e.,
15761578upstream or upwind of the point in question. So, instead of using a
15771579centered difference
15781580$$
1579- \frac{du}{dx}** i \approx \frac{u ** {i+1}-u_ {i-1}}{2\Delta x},
1581+ \frac{du}{dx}_ i \approx \frac{u _ {i+1}-u_ {i-1}}{2\Delta x},
15801582$$
15811583we use the one-sided *upwind* difference
15821584$$
1583- \frac{du}{dx}** i \approx \frac{u ** {i}-u_ {i-1}}{\Delta x},
1585+ \frac{du}{dx}_ i \approx \frac{u _ {i}-u_ {i-1}}{\Delta x},
15841586$$
15851587in case $v>0$. For $v<0$ we set
15861588$$
1587- \frac{du}{dx}** i \approx \frac{u ** {i+1}-u_ {i}}{\Delta x},
1589+ \frac{du}{dx}_ i \approx \frac{u _ {i+1}-u_ {i}}{\Delta x},
15881590$$
15891591On compact operator notation form, our upwind scheme can be expressed
15901592as
0 commit comments