01_scalar_equations.html 16.3 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1 2 3 4 5 6 7 8 9 10 11 12
<!DOCTYPE html>
<html>
  <head>
    <title>AMDiS - Adaptive Multi-Dimensional Simulations</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <style type="text/css">
      @import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
      @import url(https://fonts.googleapis.com/css?family=Raleway);
      @import url(https://fonts.googleapis.com/css?family=Ubuntu);
      @import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
      @import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
    </style>
13 14
    <link rel="stylesheet" type="text/css" href="style_display.css" />
    <!--<link rel="stylesheet" type="text/css" href="style_print.css" />-->
Praetorius, Simon's avatar
Praetorius, Simon committed
15 16 17 18 19
  </head>
  <body>
    <textarea id="source">

# Session 1
20
## Wednesday Nov 28
Praetorius, Simon's avatar
Praetorius, Simon committed
21
- **Scalar linear second order PDEs**
22
- Discrete functions on unstructured grids
Praetorius, Simon's avatar
Praetorius, Simon committed
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

---

# Second order PDEs

General problem formulation:

\\[
\left[\partial_t u\right] + cu - \nabla\cdot(\underline{b}u) - \nabla\cdot(\mathbb{A}\nabla u) = f,\quad\text{ in }\Omega\times(0,T]
\\]

with `\(u=u(x)\)` the unknown function and coefficients `\(c, \underline{b}, \mathbb{A}\)` that can depend on space, time, and other quantities `\(v\)` living in `\(\Omega\)`:
\\[
c = c(t,x,v,\nabla v),\\\\
\underline{b} = \underline{b}(t,x,v,\nabla v),\\\\
\mathbb{A} = \mathbb{A}(t,x,v,\nabla v),\\\\
f = f(t,x,v,\nabla v),
\\]
equipped with (initial- and) boundary conditions on `\(\partial\Omega\)`.

---

# Second order PDEs

AMDiS is an FEM framework and FEM is based on the **weak formulation** of the equations, thus the basic formulation for problems is:

Find `\(u\in L_2(0,T; V^{(1)})\)`, s.t.

\\[
\left[\langle\partial\_t u, \theta\rangle\_\Omega\right] + \langle cu, \theta\rangle\_\Omega +
\langle \underline{b}u, \nabla\theta\rangle\_\Omega + \langle \mathbb{A}\nabla u, \nabla\theta\rangle\_\Omega
= \langle f,\theta\rangle\_\Omega,\quad\forall \theta\in V^{(0)}
\\]

with `\(\langle a,b \rangle_\Omega:=\int_\Omega a\cdot b\,\text{d}x\)` and `\(V^{(0)},V^{(1)}\)` compatible spaces, e.g.

- `\(V^{(0)}=V^{(1)} = H^1(\Omega)\)`, or
- `\(V^{(1)}=V_G:=\{u\in H^1(\Omega)\,:\,u|_{\partial\Omega}=G\}, V^{(0)} = V_0\)`.

62 63
plus some boundary terms from the boundary conditions.

Praetorius, Simon's avatar
Praetorius, Simon committed
64 65 66 67 68 69
---

# Basic ingredients

What data/information do you need to formulate your problem?
1. Description of your **domain** `\(\Omega\)` + a **triangulation** `\(\mathcal{T}_h\)` of the domain --> `Mesh`
70
2. (Discrete) Function-space `\(V_h\subset V\)` with `\(N := dim(V_h)\)`, or its **basis-functions** respectively.
71
  - `P1` := Lagrange elements with polynomial degree `\(p=1\)`, e.g.
Praetorius, Simon's avatar
Praetorius, Simon committed
72
    \\[
73
      V_h = \\{ v\in C^0(\Omega)\,:\, v|\_T\in\mathbb{P}\_p(T),\,\forall T\in\mathcal{T}\_h(\Omega)\\}
Praetorius, Simon's avatar
Praetorius, Simon committed
74 75 76
    \\]
  - `P1+bubble` := `P1` + center bubble-function

77
  `\(V_h^{(0)}\)` is called `RowFeSpace` and `\(V_h^{(1)}\)` is called `ColumnFeSpace`.
Praetorius, Simon's avatar
Praetorius, Simon committed
78 79
--

80
3. **Solution** vector `\((u_i)\equiv u\in V_h^{(1)}\)`, called `DOFVector`
Praetorius, Simon's avatar
Praetorius, Simon committed
81
  \\[
82
    u(x) = \sum\_{i=0}^{N-1} u\_i\phi\_i(x),\quad\text{with}\;\\{\phi_i\\}\text{ a basis of }V\_h^{(1)}
Praetorius, Simon's avatar
Praetorius, Simon committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  \\]

---

# Basic ingredients

What data/information do you need to formulate your problem?
4. **Coefficient functions** `\(c, \underline{b}, \mathbb{A}\;\rightarrow\)` `OperatorTerm`,
  Categorized by degree of derivative:
  \\[
    cu - \nabla\cdot(\underline{b}u) - \nabla\cdot(\mathbb{A}\nabla u) = f
  \\]
  - `\(c, f\)`: `ZeroOrderTerm` (ZOT)
  - `\(\underline{b}\)`: `FirstOrderTerm` (FOT)
    - Derivative is on trial function: `GRD_PHI`: `\(\langle\underline{b}\cdot\nabla \phi, \psi\rangle\)`
    - Derivative is on test function: `GRD_PSI`: `\(\langle\underline{b} \phi,\nabla\psi\rangle\)`
  - `\(\mathbb{A}\)`: `SecondOrderTerm` (SOT)
5. **Boundary terms** and properties of the pair of function spaces `\(V^{(0)},V^{(1)}\)`.

---

# General procedure

106
<img src="images/procedure_en.png" width="100%" alt="Solution procedure" />
Praetorius, Simon's avatar
Praetorius, Simon committed
107 108 109

---

110
# First AMDiS program
Praetorius, Simon's avatar
Praetorius, Simon committed
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

### Header file
```
#include "AMDiS.h"
```

### AMDiS initialization
```
using namespace AMDiS;
int main(int argc, char** argv) {
  AMDiS::init(argc, argv);
  // ...
  AMDiS::finalize();
}
```
with `init(...)`
- initializes (P)MTL4, PETSc, MPI, Zoltan
- parses commandline arguments (run the program with `--help` to see available options)
- reads a parameter file

with `finalize(...)`
- finalizes (P)MTL4, PETSc, MPI

---

# Example 1
## Stationary Poisson equation

We start with the most simple equation:
\\[
  -\Delta u = f(x)\quad\text{in }\Omega,\quad u\big|\_{\partial\Omega}=0,\; f(x)\equiv 1
\\]

The coefficient functions are: `\(c=0, \underline{b}=0, \mathbb{A}=\begin{pmatrix}1 & 0\\0 & 1\end{pmatrix}\equiv 1\)`.

Weak formulation: Find `\(u\in V_0\)`, s.t.
\\[
  \langle\nabla u,\nabla\theta\rangle\_\Omega = \langle f,\theta\rangle\_\Omega,\;\forall\theta\in V\_0.
\\]

---

# Example 1

Find `\(u\in V_0\)`, s.t. `\(\langle\nabla u,\nabla\theta\rangle_\Omega = \langle f,\theta\rangle_\Omega,\;\forall\theta\in V_0.\)`

### Problem definition
```
ProblemStat prob("poisson");
prob.initialize(INIT_ALL);
```
with `initialize(flag)`
- reads a mesh from file
164
- initial global refinement of the mesh
Praetorius, Simon's avatar
Praetorius, Simon committed
165 166 167
- creates corresponsing finite-element spaces
- creates, estimators, markers, solvers, ...

168 169
`flag` determines what to initialize (or what to adopt from another problem). Here, initialize everything.

Praetorius, Simon's avatar
Praetorius, Simon committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
---

# Example 1

Find `\(u\in V_0\)`, s.t. `\(\langle\nabla u,\nabla\theta\rangle_\Omega = \langle f,\theta\rangle_\Omega,\;\forall\theta\in V_0.\)`

### Problem definition
```
ProblemStat prob("poisson");
prob.initialize(INIT_ALL);
```

### Operator definition
```
Operator opL(prob.getFeSpace(), prob.getFeSpace());
addSOT(opL, 1.0); // <grad(u), grad(theta)>, A=1
Operator opF(prob.getFeSpace());
addZOT(opF, 1.0); // <f, theta>, f=1
```
with `addSOT(op, coeff)`
190
- adds a 2nd order term to the operator `op`, with coefficient function `coeff`
Praetorius, Simon's avatar
Praetorius, Simon committed
191 192

with `addZOT(op, coeff)`
193
- adds a 0th order term to the operator `op`, with coefficient function `coeff`
Praetorius, Simon's avatar
Praetorius, Simon committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

---

# Example 1

Find `\(u\in V_0\)`, s.t. `\(\langle\nabla u,\nabla\theta\rangle_\Omega = \langle f,\theta\rangle_\Omega,\;\forall\theta\in V_0.\)`

### Problem and Operator definition
```
ProblemStat prob("poisson");                        // Problem definition
prob.initialize(INIT_ALL);

Operator opL(prob.getFeSpace(), prob.getFeSpace()); // Operator definition
addSOT(opL, 1.0); // <grad(u), grad(theta)>, A=1
Operator opF(prob.getFeSpace());
addZOT(opF, 1.0); // <f, theta>, f=1
```

### Add operators to the problem
213 214
Apply an operator to test- [and trialfunction] identified by component numbers.

Praetorius, Simon's avatar
Praetorius, Simon committed
215 216 217 218
```
prob.addMatrixOperator(opL, 0, 0);
prob.addVectorOperator(opF, 0);
```
219
with the number `0` corresponds to the 0th unknown in the 0th equation. We have only one block and thus, this is always 0.
Praetorius, Simon's avatar
Praetorius, Simon committed
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

---

# Example 1

Find `\(u\in V_0\)`, s.t. `\(\langle\nabla u,\nabla\theta\rangle_\Omega = \langle f,\theta\rangle_\Omega,\;\forall\theta\in V_0.\)`

### Add operators to the problem
```
ProblemStat prob("poisson");                        // Problem definition
prob.initialize(INIT_ALL);

Operator opL(prob.getFeSpace(), prob.getFeSpace()); // Operator definition
addSOT(opL, 1.0); // <grad(u), grad(theta)>, A=1
Operator opF(prob.getFeSpace());
addZOT(opF, 1.0); // <f, theta>, f=1

prob.addMatrixOperator(opL, 0, 0);                  // Add operators to the prob
prob.addVectorOperator(opF, 0);
```

### Define boundary conditions
```
prob.addDirichletBC(nr, 0, 0, new Constant(0.0));
```
with `nr` a boundary number specified in the mesh, and `Constant`
246
a predefined functor that returns `0` always.
Praetorius, Simon's avatar
Praetorius, Simon committed
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


---

# Example 1
### Define boundary conditions
```
ProblemStat prob("poisson");                        // Problem definition
prob.initialize(INIT_ALL);

Operator opL(prob.getFeSpace(), prob.getFeSpace()); // Operator definition
addSOT(opL, 1.0); // <grad(u), grad(theta)>, A=1
Operator opF(prob.getFeSpace());
addZOT(opF, 1.0); // <f, theta>, f=1

prob.addMatrixOperator(opL, 0, 0);                  // Add operators to the prob
prob.addVectorOperator(opF, 0);

prob.addDirichletBC(nr, 0, 0, new Constant(0.0));   // Define boundary conditions
```

### Assemble, solve and write solution
```
AdaptInfo adaptInfo("adapt");      // Store Informations about solution process

272 273
prob.assemble(&adaptInfo);         // Assemble and
prob.solve(&adaptInfo);            // solve the linear system
Praetorius, Simon's avatar
Praetorius, Simon committed
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 305 306 307 308 309 310 311 312 313 314

prob.writeFiles(&adaptInfo, true); // Write solution to file
```

---

# Example 1

```
#include "AMDiS.h"
int main(int argc, char** argv)
{
  using namespace AMDiS;
  AMDiS::init(argc, argv);
  ProblemStat prob("poisson");
  prob.initialize(INIT_ALL);

  Operator opL(prob.getFeSpace(), prob.getFeSpace());
  addSOT(opL, 1.0); // <grad(u), grad(theta)>, A=1
  Operator opF(prob.getFeSpace());
  addZOT(opF, 1.0); // <f, theta>, f=1

  prob.addMatrixOperator(opL, 0, 0);
  prob.addVectorOperator(opF, 0);

  prob.addDirichletBC(nr, 0, 0, new Constant(0.0));

  AdaptInfo adaptInfo("adapt");

  prob.assemble(&adaptInfo);
  prob.solve(&adaptInfo);

  prob.writeFiles(&adaptInfo, true);
  AMDiS::finalize();
}
```

---

# Compile and run the AMDiS program

315
### 1. Create a CMake configuration
316
File `DIR/CMakeLists.txt`
Praetorius, Simon's avatar
Praetorius, Simon committed
317 318 319 320
```cmake
project("workshop")
find_package(AMDIS REQUIRED)
add_executable("poisson" src/poisson.cc)
321
target_link_libraries("poisson" AMDiS)
Praetorius, Simon's avatar
Praetorius, Simon committed
322 323
```

324 325
- CMake requires variable `AMDIS_DIR` to point to directory that contains the file `AMDISConfig.cmake`. (This
  is set already in the Docker container.)
Praetorius, Simon's avatar
Praetorius, Simon committed
326

327
### 2. Compile
328 329
```
cd DIR
330 331 332
mkdir build && cd build       # out-of-source build
cmake [-DAMDIS_DIR=...] DIR   # configure the build process
make [poisson]                # compile the executable
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
```
where `DIR=..` contains the `CMakeLists.txt` file.

---
### CMake output
```
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- [...]
-- Performing Test COMPILER_SUPPORTS_CXX14_FLAG - Success
-- Performing Test CXX14_COMPILES_WITH_CXX14_FLAG - Success
-- Boost version: 1.58.0
-- Found the following Boost libraries:
--   system
--   [...]
-- UMFPACK version: 5.7.1 (Oct 10, 2014)
-- Found the following SuiteSparse libraries:
--   /usr/lib/x86_64-linux-gnu/libumfpack.so
--   [...]
-- Configuring done
-- Generating done
-- Build files have been written to: DIR/build
```
### Compile output
```
Scanning dependencies of target poisson
[ 50%] Building CXX object CMakeFiles/poisson.dir/src/poisson.cc.o
[100%] Linking CXX executable poisson
[100%] Built target poisson
```

---

367
### 3. Run the AMDiS program
Praetorius, Simon's avatar
Praetorius, Simon committed
368
```
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
cd DIR
./build/poisson INIT-FILE
```

```
MacroReader::checkMesh():
               Checking mesh ...
               checking done; no error detected
ProblemStat::buildAfterCoarsen():
               2113 DOFs for FeSpace[0] (P1)
               fillin of assembled matrix: 13897
               buildAfterCoarsen needed 0.01085 seconds
LinearSolverInterface::solveSystem():
               LinearSolverInterface::solveSystem()
Problem::solve():
               solution of discrete system needed 0.00278 seconds
FileWriter<T>::writeFiles():
               ParaView file written to ./output/poisson.2d.vtu
ProblemStat::writeFiles():
               writeFiles needed 0.01950 seconds
Praetorius, Simon's avatar
Praetorius, Simon committed
389 390 391 392
```

---

393 394 395 396 397 398 399 400 401
# Visualization

Plot Solution (.vtu file), using [ParaView](www.paraviw.org)

<img src="images/paraview.png" width="100%" alt="Poisson equation in paraview" />

---

# The parameter file (INIT-FILE)
Praetorius, Simon's avatar
Praetorius, Simon committed
402

403
A parameter file controls various parameters of the solution
Praetorius, Simon's avatar
Praetorius, Simon committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
process, defines the FiniteElemSpace and sets the mesh and is mandatory.
Init-files have the suffix `.dat.Xd`, where `X` is in \{1, 2, 3\}.

### Parameter definition:
```matlab
parameter_name: parameter_value % a comment
```
- The `:` sign is the delimiter between parameter name and value
- The `%` sign indicates a starting comment

Read a parameter from init-file:
```
value_type value = value0;
Parameters::get("parameter_name", value);
```
- `value0` is default, if parameter not found
- `value_type` can be number, vector, string
- arithmetic expressions are parsed, interpreted and cast to value_type

---

# The parameter file

```matlab
dimension of world: 2

mesh->macro file name:    ./macro/macro1.2d
mesh->global refinements: 10

poisson->mesh:       mesh
poisson->dim:        2
poisson->components: 1
poisson->feSpace[0]: P1
poisson->name[0]:    u

poisson->solver:                 cg
poisson->solver->max iteration:  1000
poisson->solver->tolerance:      1.e-8
poisson->solver->left precon:    diag

poisson->output->filename:        ./output/poisson.2d
poisson->output->ParaView format: 1
```

---

# The macro mesh

In AMDiS the geometry and coarse triangulation is called `Macro-Mesh`

<table width="100%"><tr>
<td width="60%">
Header:<pre><code>DIM: 2
DIM_OF_WORLD: 2
number of elements: 4
number of vertices: 5
</code></pre>
</td><td width="40%">
<img src="images/ellipt_macro.png" width="100%" alt="A macro mresh" />
</td>
</tr></table>

ASCII-File, that describes dimension of the elements (`DIM`), dimension of the world (`DIM_OF_WORLD`), the vertex coordinates, element connectivity, element boundary numbers and a neighborship relation.

---

# The macro mesh

In AMDiS the geometry and coarse triangulation is called `Macro-Mesh`

<table width="100%"><tr>
<td width="60%">
Header:<pre><code>DIM: 2
DIM_OF_WORLD: 2
number of elements: 4
number of vertices: 5
</code></pre>

Mesh description:<pre><code>element vertices:
0 1 4
1 2 4...
element boundaries:
0 0 1
0 0 1...
vertex coordinates:
0.0 0.0
1.0 0.0...
</code></pre>
</td><td width="40%">
<img src="images/ellipt_macro.png" width="100%" alt="A macro mresh" />
</td>
</tr></table>

---
class: center, middle

# Exercise1
## Poisson equation

---

# Exercise1: Poisson equation

We want to solve the Poisson equation
\\[
-\Delta u = f(x)\quad\text{ in }\Omega,
\\]
and (in)homogeneous Dirichlet boundary conditions on `\(\partial\Omega\)`.

1. Setup the CMake configuration:
```
> mkdir build
> cd build
> cmake ..
```
2. Compile and run the example code `exercise1.cc`
```
> make exercise1
> cd ..
> build/exercise1 init/exercise1.dat.2d
```
3. Visualize the file `exercise1.2d.vtu` created in directory `output`
with the program `ParaView`.
4. What is the concrete expression for `\(f\)` and dirichlet value `\(g\)`?

---

# Advanced Exercise1: modify the Poisson equation

1. Change the expression for `\(f\)` and `\(g\)`.
2. Modify the parameters in the init-file `exercise1.dat.2d`, e.g.
  - Change the nr. of global refinements
  - Set a different Finite-Element space
  - Use another linear solver
  See also [AMDiS-Wiki/Initfile](https://goo.gl/Dhm9Bx)
3. What parameters can be changed, and where do you get an error, or a crash of the program?
4. Solve the same equation in 1d or 3d, by changing the mesh, dimension of world
   and problem dimension in the init-file.
5. Modify the macro-mesh, e.g.
  - change the boundary nr for the right edge
  - change the coordinates of the vertices

---

# Some hints
549 550 551 552 553 554 555 556 557 558
1. When you get the error message
```
Cannot open file ./output/exercise1.2d.vtu for writing!
```
the directory `output` is missing. Just create it and run again:
```
mkdir output
```

2. Used functions/classes:
Praetorius, Simon's avatar
Praetorius, Simon committed
559 560 561 562 563 564 565
```
addSOT(Operator, EXPRESSION);
addZOT(Operator, EXPRESSION);
// Functor class with return-type double and
// argument-type WorldVector<double>:
AbstractFunction<double, WorldVector<double>>;
```
566
3. Parameters to modify:
Praetorius, Simon's avatar
Praetorius, Simon committed
567 568 569 570 571
```matlab
mesh->global refinements: INTEGER
poisson->feSpace[0]: [P1|P2|P3|P4|P1+bubble]
poisson->solver: [cg|gmres|direct|...]
```
572 573 574 575 576 577 578
---

## Sources for help:
- [AMDiS-Wiki](https://goo.gl/Jy3u1u)
- [Init-file manual](https://goo.gl/Dhm9Bx)
- [Expressions manual](https://goo.gl/JK8EUI)
- [List of init-file parameters](https://goo.gl/LWJzq9)
Praetorius, Simon's avatar
Praetorius, Simon committed
579 580 581

    </textarea>
    <script src="lib/remark.js" type="text/javascript"></script>
582
    <script type="text/javascript" src="MathJax/MathJax.js?config=TeX-AMS_HTML"></script>
Praetorius, Simon's avatar
Praetorius, Simon committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605

    <script type="text/javascript">
      var slideshow = remark.create({
        ratio: "4:3",
        highlightLanguage: "cpp"
      });

      // Setup MathJax
      MathJax.Hub.Config({
          tex2jax: {
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
          }
      });
      MathJax.Hub.Queue(function() {
          $(MathJax.Hub.getAllJax()).map(function(index, elem) {
              return(elem.SourceElement());
          }).parent().addClass('has-jax');
      });

      MathJax.Hub.Configured();
    </script>
  </body>
</html>