Skip to content

Commit 7cbc57e

Browse files
committed
Refactor Advection chapter to Devito-only
- Fix broken LaTeX subscripts (**{ → _{) in advec.qmd (13 occurrences) - Remove NumPy implementation src/advec/advec1D.py - Update file references to point to advec1D_devito.py All 247 tests pass, PDF builds without errors.
1 parent c11aba9 commit 7cbc57e

2 files changed

Lines changed: 20 additions & 403 deletions

File tree

chapters/advec/advec.qmd

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ u(i\Delta x, (n+1)\Delta t) &= I(i\Delta x - v(n+1)\Delta t) \nonumber \\
7272
provided $v = \Delta x/\Delta t$. So, whenever we see a scheme that
7373
collapses 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}
7777
for the PDE in question, we have in fact a scheme that reproduces the
7878
analytical solution, and many of the schemes to be presented possess
@@ -771,11 +771,12 @@ $$
771771
$$
772772
which 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
$$
776776
A 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
781782
We now need to work with three time levels and must modify our solver a bit:
@@ -888,7 +889,7 @@ $$
888889
$$ {#eq-advec-1D-upwind}
889890
written 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
$$
893894
gives a generally popular and robust scheme that is stable if $C\leq 1$.
894895
As with the Leapfrog scheme, it becomes exact if $C=1$, exactly as shown in
@@ -929,7 +930,7 @@ $$
929930
$$
930931
by 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
$$
934935
actually 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
11781180
Another obvious candidate for time discretization is the Crank-Nicolson
11791181
method 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
$$
11831185
It 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
$$
11881190
When $\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
$$
11931195
for $i=1,\ldots,N_x-1$. At the boundaries we set $u=0$ and simulate just to
11941196
the 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
$$
11981200
The elements on the diagonal in the matrix become:
11991201
$$
@@ -1208,7 +1210,7 @@ And finally, the right-hand side becomes
12081210
12091211
\begin{align*}
12101212
b_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\\
12121214
b_{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
$$
12741276
or 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
$$
12791281
This is the explicit Lax-Wendroff scheme.
@@ -1576,15 +1578,15 @@ is the dominating term, collect its information in the flow direction, i.e.,
15761578
upstream or upwind of the point in question. So, instead of using a
15771579
centered 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
$$
15811583
we 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
$$
15851587
in 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
$$
15891591
On compact operator notation form, our upwind scheme can be expressed
15901592
as

0 commit comments

Comments
 (0)