02_data_processing.html 10.4 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 2
Praetorius, Simon's avatar
Praetorius, Simon committed
20
## Friday Nov 30
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
---

# Motivation
### Extract maxima of solution
27
<img src="images/ball500.png" width="25%" /> <img src="images/pfc_sphere.png" width="25%" />
Praetorius, Simon's avatar
Praetorius, Simon committed
28 29

### Calculate integrals
30
<img src="images/energy.png" width="35%" />
Praetorius, Simon's avatar
Praetorius, Simon committed
31 32 33

---

34
# Working with discrete functions
35
Finite-Element function `\(u_h\in V_h = span\{\phi_i\}\)`, a linear combination of basis-functions `\(\phi_i\)`:
Praetorius, Simon's avatar
Praetorius, Simon committed
36
\\[
Praetorius, Simon's avatar
Praetorius, Simon committed
37
u\_h(x) = \sum\_i u\_i \phi\_i(x) = \sum\_j u\_{i\_T(j)} \hat{\phi}_j(\lambda\_T(x)),\text{ for }x\in T
Praetorius, Simon's avatar
Praetorius, Simon committed
38 39
\\]
with `\(u_i:=\)` Degrees of Freedom, stored in `DOFVector<value_type> U`:
40
- Evaluate at DOF index: `u_i = U[i]`
Praetorius, Simon's avatar
Praetorius, Simon committed
41 42 43 44 45 46 47
- Iterate over all DOFs:
```
DOFIterator<value_type> it(&U [, USED_DOFS]);
for (it.reset(); !it.end(); ++it)
      *it = value; // idx = it.getDOFIndex()
```

48
- Evaluate DOFVector at global coordinate `\(x\)`:
Praetorius, Simon's avatar
Praetorius, Simon committed
49 50 51 52 53 54 55
```
WorldVector<double> x;
x[0] = 0.2; x[1] = 0.3;
value_type value = U(x);
```

---
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
## Discrete function on element T
Evaluate DOFVector in local coordinates `\(\lambda\)` of element `T=elInfo->getElement()`
\\[
u\_h(x\_T(\lambda)) = \sum\_j \hat{u}\_j \hat{\phi}_j(\lambda),
\\]

with `\(\hat{u}_j = u_{i_T(j)}\)`

1. Get local DOFs `u_j` on the element
2. Evaluate the linear combination of basis functions

```
const FiniteElemeSpace* feSpace = U.getFeSpace();
const BasisFunction* basFcts = feSpace->getBasisFcts();

ElementVector uh(basFcts->getNumber());
U.getLocalVector(elInfo->getElement(), uh); // local DOFs u_j
basFcts->evalUh(lambda, uh);                // linear combination
```

---

# Interpolate a local function to a DOFVector
Praetorius, Simon's avatar
Praetorius, Simon committed
79
A local function (*element function*) is something that can be evaluated at local coordinates in a *bound* element.
80 81

By traversing the mesh the local function can be used to locally interpolate to the DOFs of an
Praetorius, Simon's avatar
Praetorius, Simon committed
82
element, i.e., `\(u_h = I_h f\;\rightarrow\)` `U=(u_i)`
Praetorius, Simon's avatar
Praetorius, Simon committed
83 84 85 86

```
U << EXPRESSION;
```
Praetorius, Simon's avatar
Praetorius, Simon committed
87 88
where `EXPRESSION` represents `\(f\)` as composition of *element functions* and can contain e.g.
- The global coordinates: `X()`, `X(i)`
Praetorius, Simon's avatar
Praetorius, Simon committed
89 90
- Scalar values: `1.0`, `constant(1.0)`
- Other DOFVectors: `valueOf(V)`, `gradientOf(V)`
91
- Matrix and vector expressions: `two_norm(...)`, `vec * vec`
Praetorius, Simon's avatar
Praetorius, Simon committed
92 93 94
- and any function applied to these *terminal* expressions.

NOTE: This has to be unserstood in the sense of functional programming, i.e. everything is a function!
Praetorius, Simon's avatar
Praetorius, Simon committed
95

96
List of possible expressions, see `X0_expressions.html` or cheat sheets.
97 98

---
Praetorius, Simon's avatar
Praetorius, Simon committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

### Example:
Let `\(C\)` and `\(U\)` be of type `DOFVector<double>`:
\\[
U(x) := \frac{1}{2}\left(C(x)^2\,(1-C(x))^2 + \frac{1}{\epsilon}\|\nabla C(x)\|^2\right)
\\]

Assign expression on rhs to DOFVector `\(U\)`:
```
U << 0.5*( pow<2>(valueOf(C) * (1.0 - valueOf(C))
      + (1.0/eps) * unary_dot(gradientOf(C)) );
```

---

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
### Example:
Let `\(C\)` and `\(U\)` be of type `DOFVector<double>`:
\\[
U(x\_T(\lambda)) := \frac{1}{2}\left(C(x\_T(\lambda))^2\,(1-C(x\_T(\lambda)))^2 + \frac{1}{\epsilon}\|\nabla C(x\_T(\lambda))\|^2\right)
\\]

Assign expression on rhs to DOFVector `\(U\)`:
```
U << 0.5*( pow<2>(valueOf(C) * (1.0 - valueOf(C))
      + (1.0/eps) * unary_dot(gradientOf(C)) );
```

---

### Example:
Let `\(C\)` and `\(U\)` be of type `DOFVector<double>`:
\\[
\hat{U}(\lambda) := \frac{1}{2}\left(\hat{C}(\lambda)^2\,(1-\hat{C}(\lambda))^2 + \frac{1}{\epsilon}\|\Lambda^{-T}\_T\nabla\_\lambda \hat{C}(\lambda))\|^2\right)
\\]

Assign expression on rhs to DOFVector `\(U\)`:
```
U << 0.5*( pow<2>(valueOf(C) * (1.0 - valueOf(C))
      + (1.0/eps) * unary_dot(gradientOf(C)) );
```

---

Praetorius, Simon's avatar
Praetorius, Simon committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
# Working with the discrete solution
- Reduce the `DOFVector`, i.e. calc integrals, norms:
```
integrate( EXPRESSION [, Mesh*] )
```
```
integrate( valueOf(U) );
integrate( two_norm(gradientOf(U)) );
integrate(pow<2>(valueOf(U)) + unary_dot(gradientOf(U)));
```
- Other reduction operations (over DOFs):
  - `max( EXPRESSION )` `\(\Rightarrow\max\{ expr(x_i)\,:\, x_i\in\Omega \}\)`
  - `min( EXPRESSION )` `\(\Rightarrow\min\{ expr(x_i)\,:\, x_i\in\Omega \}\)`
  - `abs_max( EXPRESSION )` `\(\Rightarrow\max\{ |expr(x_i)|\,:\, x_i\in\Omega \}\)`
  - `abs_min( EXPRESSION )` `\(\Rightarrow\min\{ |expr(x_i)|\,:\, x_i\in\Omega \}\)`

---

# Working with the mesh

162
<img src="images/ellipt_macro.png" width="30%" /> <img src="images/torus_macro.jpg" width="30%" />
Praetorius, Simon's avatar
Praetorius, Simon committed
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

### Change position of mesh vertices:
<img src="images/Fsi.png" width="70%" />

---

# Working with the mesh
A Mesh holds all information about a triangulation. Get the mesh from `ProblemStat`:
```
Mesh&amp; mesh = *prob.getMesh();
```
- Number of vertices: `mesh.getNumberOfVertices()`
- Number of triangles (elements): `mesh.getNumberOfElements()`
- World coordinates are stored in `WorldVector<double>`.
- Get the coordinates of all DOFs:
```
DOFVector<WorldVector<double> > Coords(prob.getFeSpace(), "coords");
mesh.getDofCoords(Coords);
```
- Change coordinates of the mesh:
```
ParametricSimple parametric(Coords);
mesh.setParametric(&amp;parametric);
```

---

# Working with the mesh:
- (Advanced) Traverse the mesh element-wise:

```
Flag traverseFlag = Mesh::CALL_LEAF_EL | (FILL_FLAGS);
TraverseStack stack;
ElInfo *elInfo = stack.traverseFirst(&amp;mesh, -1, traverseFlag);
while (elInfo) {
  // do something with elInfo
  elInfo = stack.traverseNext(elInfo);
}
```
- (Advanced) Global Refinement of the mesh: Use `RefinementManager`

```
RefinementManager* refManager = prob.getRefinementManager();

Flag f = refManager->globalRefine(&amp;mesh, 5);
if (f == MESH_REFINED) {
  MSG("Mesh globally refined by 5 levels!\n");
}
```

---

# Example: Print solution with coordinates
Writing out the solution to the screen:
```
DOFVector<WorldVector<double>> C(U.getFeSpace(), "c");
mesh.getDofCoords(C);

DOFIterator<double> it(U);
DOFIterator<WorldVector<double>> c_it(C);
for (it.reset(),c_it.reset();  !it.end();  ++it,++c_it)
  std::cout << "U(" << *c_it << ") = " << *it << "\n";
```
This will print out:
<pre><code style="background: #ddd;">U(0 0) = 0
U(1 0) = 0
U(1 1) = 0
U(0 1) = 0
U(0.5 0.5) = 0.0833333
</code></pre>

---
class: center, middle

# Input/Output

---

# Input/Output
Write DOFVectors to file, for visualization, serialization, ...
- File writer with automatic file-type detection:
  ```
  io::writeFile(DOFVECTOR, FILENAME);
  ```
  Detection by filename extension: ".vtu", ".arh", ".dat", ".2d", ".gnu",...
  - Use **ParaView** for visualization
  - Tool **MeshConv** can convert beween mesh formats
- File reader with automatic file-type detection:
```
io::readFile(FILENAME, DOFVECTOR);
```
- Use ARH format to exchange data (can be converted to VTU by MeshConv.)

Praetorius, Simon's avatar
Praetorius, Simon committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
---
## Use a FileWriter
- Configure the output in the parameter file
  ```
  FileWriter fileWriter("name", &mesh, &U);
  fileWriter.writeFiles(&adaptInfo, true);
  ```
  In the parameter file:
  ```
  name->filename: xyz
  name->ParaView format: 1
  ```

- Set which level to write
  ```
  // leaf level
  fileWriter.writeFile(&adaptInfo, true, -1, Mesh::CALL_LEAF_EL);
  // specific level
  fileWriter.writeFile(&adaptInfo, true, level, Mesh::CALL_EL_LEVEL);
  ```
  with `level` and integer `>= 0`.
Praetorius, Simon's avatar
Praetorius, Simon committed
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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

---
class: center, middle

# Exercise2
## Poisson equation

---

# Exercise2: Poisson equation
We want to solve the Poisson equation
\\[
-\Delta u = f(x)\quad\text{ in }\Omega,\quad u|\_{\partial\Omega} = g
\\]
in a rectangular domain `\(\Omega\)`, with
\\[
f(x) = 40(1 - 10\|x\|^2)e^{-10.0\|x\|^2}\\\\
g(x) = e^{-10.0\|x\|^2} \\\\
\\]
and with exact solution `\(u^\ast=g\)`.
1. Assemble and solve the equation.
2. Interpolate error `\(ERR := |u_h - g|\)` to DOFVector and write it to a file.
3. Calculate the error-norms `\(\|u_h - g\|_{L_2(\Omega)}\)` and `\(\|u_h - g\|_{H_1(\Omega)}\)`
4. Evaluate the error at the grid-point `\(x=(0.5, 0.5)\)`.
5. Refine the mesh globally and compare error-norms to old error-norms.

---

# Advanced Exercise2: Mesh adaption
The datastructure `AdaptInfo` provides parameters for tolerance and nr. of iterations for an adaption process:
```
AdaptInfo adaptInfo("adapt");
adaptInfo.getMaxSpaceIteration(); // => adapt->max iteration
adaptInfo.getSpaceTolerance(0);   // => adapt[0]->tolerance
```

1. Use AdaptInfo to write an adaption loop that refines the mesh globally to reduce the error until a tolerance is reached.
2. Write the error DOFVector in every adaption iteration to a file.
3. Calculate the exponent in the error estimate `\(\|u_h - I_h u^\ast\|_\#\leq C h^k\)`
4. (optional) Transform the mesh, by `\(x\mapsto 2x\)` and solve again.

---

# Some hints

### Used functions/classes:

```
DOFVector << EXPRESSION;
integrate( EXPRESSION );

// EXPRESSION: {valueOf(U), gradientOf(U), X(), X(i), +,-,*,/,
//              unary_dot(EXPR.), pow<2>(EXPR.), absolute(EXPR.)}

RefinementManager* refManager = prob.getRefinementManager();
refManager->globalRefine(prob.getMesh(), NR_OF_REFINEMENTS);

adaptInfo.getSpaceTolerance(0);
adaptInfo.getMaxSpaceIteration();

io::writeFile( DOFVECTOR, FILENAME );
```

### Parameters to modify:
```matlab
adapt[0]->tolerance: DOUBLE
adapt->max iteration: INTEGER
```

    </textarea>
    <script src="lib/remark.js" type="text/javascript"></script>
348
    <script type="text/javascript" src="MathJax/MathJax.js?config=TeX-AMS_HTML"></script>
Praetorius, Simon's avatar
Praetorius, Simon committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

    <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>