-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumGy_LV11.qmd
304 lines (220 loc) · 8.3 KB
/
NumGy_LV11.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
---
title: "Numerical Simulation Methods in Geophysics, Part 11: Solving problems"
subtitle: "1. MGPY+MGIN"
author: "[email protected]"
title-slide-attributes:
data-background-image: pics/tubaf-logo.png
data-background-size: 20%
data-background-position: 10% 95%
format:
tubaf-revealjs:
html-math-method: mathjax
chalkboard: true
include-in-header:
- text: |
<script>
window.MathJax = {
loader: {
load: ['[tex]/physics']
},
tex: {
packages: {'[+]': ['physics']}
}
};
</script>
slide-number: c/t
transition: slide
transition-speed: fast
menu:
side: left
# jupyter: python3
---
# Recap
1. Types of PDEs in geophysics
1. The Finite Difference (FD) method
* Poisson equation in 1D, look into 2D/3D
* Heat transfer (diffusion equation) in 1D, time-stepping
1. Solving the hyperbolic (acoustic) wave equation in 1D
1. The Finite Element (FE) method
* Poisson and diffusion equation in 1D
* (complex) Helmholtz equation in 2D for EM problems
## Topics yet to be covered
* solving vectorial EM problems in 2D/3D
* staggered grid techniques 2D/3D
* 2.5D solution techniques
* 3D vector solution using Nedelec elements
* The Finite Volume (FV) method
* solving advection-dispersion problems
* equation solvers and high-performance computing
Three lectures (22/1, 29/1, 5/2) and exercises (23+30/1, 6/2)
## The solver module
```{python}
#| echo: false
#| eval: true
import numpy as np
import pygimli as pg
import pygimli.meshtools as mt
world = mt.createWorld(start=[-10000, -10000], end=[10000, 0])
mesh = mt.createMesh(world, quality=34, area=1e5)
mesh["my"] = 4 * np.pi * 1e-7
sigma0 = 1 / 100 # 100 Ohmm
sigma = mesh.populate("sigma", {1: sigma0, 2: sigma0*10})
```
```{python}
#| echo: false
#| eval: true
import matplotlib.pyplot as plt
import numpy as np
```
```{python}
#| echo: true
#| eval: true
import pygimli.solver as ps
mesh["my"] = 4 * np.pi * 1e-7
A = ps.createStiffnessMatrix(mesh, a=1/mesh["my"])
M = ps.createMassMatrix(mesh, mesh["sigma"])
fig, ax = plt.subplots(ncols=2)
ax[0].spy(pg.utils.toCSR(A), markersize=1)
ax[1].spy(pg.utils.toCSR(M).todense(), markersize=1)
```
## The complex problem matrix
$$\vb B = \mqty(\vb A & -\omega \vb M\\ \omega \vb M & \vb A)$$
```{python}
#| echo: true
#| eval: true
#| output-location: column
w = 0.1
nd = mesh.nodeCount()
B = pg.BlockMatrix()
B.Aid = B.addMatrix(A)
B.Mid = B.addMatrix(M)
B.addMatrixEntry(B.Aid, 0, 0)
B.addMatrixEntry(B.Aid, nd, nd)
B.addMatrixEntry(B.Mid, 0, nd, scale=-w)
B.addMatrixEntry(B.Mid, nd, 0, scale=w)
pg.show(B)
```
## Sparse matrices
Up to now: regular (dense) array: save every element including 0
Save only non-zero components (e.g. using `scipy.sparse`)
* COO - coordinate format
* CSC/CRS - compressed sparse column/row
* BSR - block sparse row format, ...
# Equation solvers
## Solve systems of equations
direct solvers
* Gauss elimination (expensive and dense)
* Cholesky (or ILU) decomposition
indirect solvers (fixed point iteration)
* gradient methods: steepest descent, conjugate gradients
* preconditioning: incomplete factorizations (of submatrices)
## Cholesky decomposition
$$\vb A = \vb L \vb L^T \qqtext{or} \vb A = \vb L \vb D \vb L^T $$
## Reordering

## Iterative solvers - Fixpunktverfahren
$$\vb A \vb x = \vb b$$
decompose $\vb A=\vb M - \vb N$ and solve
$$\vb M\vb x = \vb N \vb x + \vb b$$
iterativily by $\vb x^{k+1}=\vb M^{-1}\left(\vb N\vb x^k + \vb b\right)$
e.g. $\vb M=\mbox{diag}(\vb A)$ (Jacobi method) or<br>
$\vb M=\mbox{tril}(\vb A)$ (Gauss-Seidel method)
## Gradient methods
minimize residual $\vb r = \vb b - \vb A\vb x$
## Steepest descent method
$$\vb r_0 = \vb b - \vb A \vb x_0 $$
go towards $\vb r_0$ and minimize step length $\alpha$
$$\vb r_1^T\vb r_0 = 0 = \vb b - \vb A(\vb x_0+\alpha\vb r_0)$$
$$ \Rightarrow \alpha = \frac{\vb r_0^T\vb r_0}{\vb r_0\vb A \vb r_0} $$
## Steepest descent algorithm
`for i = 0,1, ...`
$$\vb r_i = \vb b - \vb A \vb x_i $$
$$ \Rightarrow \alpha_i = \frac{\vb r_i^T\vb r_i}{\vb r_i\vb A \vb r_i} $$
$$\vb x_{i+1} = \vb x_i + \alpha_i\vb r_i$$
## Conjugate directions
Set of orthogonalen directions $\vb d$ with
$$ \vb d^T_i \vb A \vb d_j = 0 \qquad \forall i\ne j $$
::: {.callout-note}
every search direction is only used once
:::
$$\vb x_{i+1} = \vb x_i + \alpha_i\vb d_i$$
## Conjugate directions

## Conjugate gradient (Hestenes&Stiefel, 1952)
$$\vb d_0 = \vb r_0 = \vb b - \vb A \vb x_0 $$
$$ \alpha_i = \frac{\vb r_i^T\vb r_i}{\vb d_i\vb A \vb d_i} $$
$$\vb x_{i+1} = \vb x_i + \alpha_i\vb d_i$$
$$\vb r_{i+1} = \vb r_i - \alpha_i\vb A \vb d_i$$
$$d_{i+1} = \vb r_{i+1} + \vb d_{i+1} \frac{\vb r_{i+1}^T\vb r_{i+1}}{\vb r_i^T\vb r_i}$$
## Steepest descent vs. conjugate gradient
::::{.columns}
:::{.column width="50%"}
{fig-align="center"}
:::
:::{.column width="50%"}
{fig-align="center"}
:::
::::
## Preconditioning
$\vb A \vb x=\vb b$ often badly conditioned (elongated) $\Rightarrow$ slow convergence
Idea: transform (precondition) equation system by preconditioner $\vb K$
$$\vb K^{-1} \vb A \vb x = \vb K^{-1}\vb b$$
Extreme cases: 1. $\vb K=\vb I$, cheap PC but no gain
2. $\vb K=\vb A$, perfect conditioning but expensive PC<br>
e.g. $\vb K=\mbox{diag}A$ or $\vb K=\epsilon\mbox{tril}A$
## Incomplete Cholesky decomposition
::::{.columns}
:::{.column width="50%"}
$$\vb A\approx \vb L \vb L^T$$
with certain accuracy or sparsity
:::
:::{.column width="50%"}
{fig-align="center"}
:::
::::
## Preconditioning
EM problem
$$\vb B = \mqty(\vb A & -\omega \vb M\\ \omega \vb M & \vb A)$$
$$\vb K = \mqty(\vb A + \omega\vb M & 0 \\ 0 & \vb A + \omega\vb M)$$
or Schur complement. E.g., hold factorization of $\vb A$ and $\vb M$ in memory
# Development of EM modelling
* approaches: integral equations (IE), finite differences (FD) and elements (FE)
* decompose 3D source (2D inverse) problems in wavenumber domains
* improvement of equation solvers and preconditioners
*
## Solving EM problems with staggered grid
{fig-align="center"}
## The way from FD to FE

## The Finite Element zoo (1D & 2D)
{fig-align="center"}
## The Finite Element zoo (3D)
{fig-align="center"}
## Elements used in EM modelling

## Meshing complicated geometries

## Modelling example

## Packages
Mesh generation: [TetGen](https://tetgen.org) (3D), [GMsh](https://gmsh.info) (2D/3D)
FE packages: [FEniCS](https://fenicsproject.org), [NETGEN/NGsolve](https://ngsolve.org)
Equation solvers: [Suitesparse](https://doc.sagemath.org/html/en/reference/spkg/suitesparse.html), [MUMPS](https://mumps-solver.org), [SciPy](https://scipy.org)
Computational frameworks: [PetSc](https://petsc.org), [MPI](https://www.mcs.anl.gov/research/projects/mpi/)
EM modelling (and inversion) packages: [Mare2DEM](https://mare2dem.bitbucket.io), [emg3d](https://emg3d.emsig.xyz), GoFEM, [PETGEM](https://petgem.bsc.es/), [custEM](https://custem.readthedocs.io), [SimPEG](https://simpeg.xyz), ModEM, FEMTIC
# Performance on Parallel machines
## Amdahlsches Gesetz
Zerlegung in seriellen und parallelen Anteil $t=t_s+t_p/N(+t_c(N))$
{fig-align="center"}
## Computational size
{fig-align="center"}
## Perfomance analysis - Memory
{fig-align="center"}
## Perfomance analysis - Time
{fig-align="center"}
## Perfomance analysis - MPI scaling
{fig-align="center"}
## Perfomance analysis - MPI scaling
{fig-align="center"}
## Multiple right-hand sides
{fig-align="center" width="50%"}