-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_finite-differences.qmd
212 lines (157 loc) · 5.14 KB
/
02_finite-differences.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
---
title: "Finite Differences"
jupyter: python3
---
## Taylor expansion
Assume the Poisson equation $$\div(a\grad u)=f$$
Taylor expansion $$f(x)=f(x_0)+f'(x0)(x-x_0)+f''(x_0)(x-x_0)^2/2$$
# The Poisson equation
## FD Approximation
:::{.callout-tip icon="false" title="Approximate derivative operators by differences"}
$$ \pdv{u}{x}\approx\frac{\Delta u}{\Delta x} $$
:::
and solution $u$ by finite values $u_i$ at points $x_i$, e.g.
$$ \dv*{u}{x}_{2.5} := (u_3-u_2) / (x_3-x_2) $$
<!-- $$ \dv*[2]{u}{x}_{3} := -->
$$\pdv[2]{u_3}{x}\approx
\frac{\dv*{u}{x}_{3.5}-\dv*{u}{x}_{2.5}}{(x_4-x_2)/2}
= \frac{(u_4-u_3)/(x_4-x_3)-(u_3-u_2)/(x_3-x_2)}{(x_4-x_2)/2} $$
## Difference stencil
Assumption: equidistant discretization $\Delta x$, conductivity 1
1st derivative: $[-1, +1] / dx$, 2nd derivative $[+1, -2, +1] / dx^2$
Matrix-Vector product $\vb{A}\cdot\vb{u}=\vb{f}$ with
$$ \vb{A} = \frac{1}{\Delta x} \begin{bmatrix}
+1 & -2 & +1 & 0 & \ldots & & \\
0 & +1 & -2 & +1 & 0 & \ldots & \\
\vdots & \vdots & \vdots & \ddots & \vdots & \\
\ldots & \ldots & 0 & +1 & -2 & +1
\end{bmatrix} $$
![1D Finite-Difference stencil](pics/fd1dstencil.svg)
## FDM on the general Poisson equation
Assume the Poisson equation $$\div(a\grad u)=f$$ with a conductivity term $a$
$$ \pdv{(a \pdv*{u})}{z} = a\pdv[2]{u}{z} + \pdv{a}{z} \pdv{u}{z} $$
## Boundary conditions
Dirichlet conditions: $u_0=u_B$ (homogeneous if 0)
Neumann conditions (homogeneous if 0)
$$ \pdv*{u}{x}_0=g_B $$
Mixed boundary conditions $u_0+\alpha du_0/dx=\gamma$
### Dirichlet BC implementation way 1
$u_0 = u_B$
$$ \begin{bmatrix}
+1 & 0 & 0 & \ldots & & \\
+1 & -2 & +1 & 0 & \ldots & \\
\vdots & \vdots & \ddots & \vdots & \\
\ldots & \ldots & 0 & +1 & -2 & +1
\end{bmatrix} \cdot\vb{u} =
\begin{bmatrix} u_B\\ f_1 \\ \vdots \\ f_N \end{bmatrix} $$
### Dirichlet BC implementation way 2
$u_B - 2 u_1 + u_3 = f_1$
$$ \begin{bmatrix}
-2 & +1 & 0 & \ldots & & \\
+1 & -2 & +1 & 0 & \ldots & \\
\vdots & \vdots & \ddots & \vdots & \\
\ldots & 0 & +1 & -2 & +1
\end{bmatrix} \cdot\vb{u} =
\begin{bmatrix} f_1 - u_B\\ f_2 \\ \vdots \\ f_N \end{bmatrix} $$
### Neumann BC implementation way 1
$$ u_1 - u_0 = g_B$$
$$ \begin{bmatrix}
-1 & +1 & 0 & \ldots & & \\
+1 & -2 & +1 & 0 & \ldots & \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
\ldots & \ldots & +1 & -2 & +1
\end{bmatrix} \cdot\vb{u} =
\begin{bmatrix} f_0+g_B\\ f_1 \\ \vdots \\ f_N \end{bmatrix} $$
### Neumann BC implementation way 2
$$u_0 - 2 u_1+u_2 = f_1 \qquad u_1 - u_0 = g_B \Rightarrow u_2-u_1=f_1+g_B $$
<!-- $-u_1 + u_2 = f_1 - g_B$ -->
$$ \begin{bmatrix}
-1 & +1 & 0 & \ldots & & \\
+1 & -2 & +1 & 0 & \ldots & \\
\vdots & \vdots & \ddots & \vdots & \\
\ldots & 0 & +1 & -2 & +1
\end{bmatrix} \cdot\vb{u} =
\begin{bmatrix} f_1 + g_B\\ f_2 \\ \vdots \\ f_N \end{bmatrix} $$
# Parabolic PDEs
## Heat transfer in 1D
$$ \pdv{T}{t} - a \pdv[2]{T}{z} = 0 $$
with the periodic boundary conditions:
* $T(z=0,t)=T_0 + \Delta T \sin \omega t$ (daily/yearly cycle)
* $\pdv{T}{z}(z=z_1) = 0$ (no change at depth)
and the initial condition $T(z, t=0)=\sin \pi z$ has the analytical solution
$$ T(z, t) = \Delta T e^{-\pi^2 t} \sin \pi z $$
## Explicit methods
$$ \pdv{T}{t} - a \pdv[2]{T}{z} = 0 $$
Solve Poisson equation $\div(a\grad u)=f$
for every time step $i$ (using FDM, FEM, FVM etc.)
Finite-difference step in time: update field by
$$ T_{i+1} = T_i + a \pdv[2]{u}{z} \cdot \Delta t$$
## Discretization
:::: {.columns}
::: {.column}
$$ \pdv[2]{u}{t}^{n} \approx \frac{u^{n+1}-u^n}{\Delta t} - \frac{u^{n}-u^{n-1}}{\Delta t}
$$
$$ = \frac{u^{n+1}+u^{n-1}-2 u^n}{\Delta t^2} = c^2\pdv[2]{u}{x}^n $$
$$ u^{n+1} = c^2 \Delta t^2 \pdv[2]{u}{x}^{n} + 2 u^n - u^{n-1} $$
<!-- \frac{1}{\Delta t} -->
$$ \vb M \vb u^{n+1} = (\vb A + 2\vb M) \vb u^n - \vb M \vb u^{n-1} $$
:::
::: {.column}
![Second derivative](pics/dtsecond.svg)
:::
::::
## Example: velocity distribution
```{python}
#| output-location: column-fragment
#| eval: true
#| echo: true
import numpy as np
import matplotlib.pyplot as plt
x=np.arange(0, 600.01, 0.5)
c = 1.0*np.ones_like(x) # velocity in m/s
c[100:300] = 1 + np.arange(0,0.5,0.0025)
c[900:1100] = 0.5 # low velocity zone
# Plot velocity distribution.
plt.plot(x,c,'k')
plt.xlabel('x [m]')
plt.ylabel('c [m/s]')
plt.grid()
```
## Initial displacement
Derivative of Gaussian (Ricker wavelet)
```{python}
#| output-location: column-fragment
#| eval: true
#| echo: true
l=5.0
# Initial displacement field [m].
u=(x-300.0)*np.exp(-(x-300.0)**2/l**2)
# Plot initial displacement field.
plt.plot(x,u,'k')
plt.xlabel('x [m]')
plt.ylabel('u [m]')
plt.title('initial displacement field')
plt.show()
```
## Time propagation
```{python}
#| output-location: column-fragment
#| eval: true
#| echo: true
u_last=u
dt = 0.5
ddu = np.zeros_like(u)
dx = np.diff(x)
for i in range(100):
dudx = np.diff(u)/dx
ddu[1:-1] = np.diff(dudx)/dx[:-1]
u_next = 2*u-u_last+ddu*c**2 * dt**2
u_last = u
u = u_next
plt.plot(x,u,'k')
```
<video autoplay="true" loop="true">
<source src="notebooks/wave.mp4">
</video>
<!-- <video data-autoplay src="notebooks/wave.mp4"></video> -->
<!-- {{< video notebooks/wave.mp4 video="true">}} -->