Slide 1

Slide 1 text

Ivan Yashchuk Aalto University | Quansight Labs Phase Field Methods + FEniCS/Firedrake CHiMaD Phase Field Methods Workshop XI ivan.yashchuk@aalto.fi

Slide 2

Slide 2 text

Content ● FEniCS/Firedrake introduction ○ Composable solvers & preconditioning ○ Time-dependent problems: firedrake-ts, Irksome ○ Phase field specific example ● PFHub benchmarks ● Future developments (GPU, AMR) ● Embedding into optimization and machine learning packages ● Conclusions

Slide 3

Slide 3 text

FEniCS / Firedrake Introduction firedrakeproject.org fenicsproject.org

Slide 4

Slide 4 text

FEniCS / Firedrake Unified Form Language (UFL) is a domain specific language for specifying and manipulating variational (weak) forms, used in FEniCS, Firedrake, and Dune-Fem. Having a PDE problem specified using UFL, an efficient low-level code is generated from a higher-level description. FEniCS is implemented in C++ and has C++ and Python user interface. Firedrake is implemented in Python (with bits of Cython) and is tightly integrated with PETSc library.

Slide 5

Slide 5 text

Strong and weak formulations for a Poisson problem ✗ Strong form: where Ω is the spatial domain and ∂ Ω is the boundary of Ω . ✓ Variational or weak form:

Slide 6

Slide 6 text

“Hello world!” / Stokes equation

Slide 7

Slide 7 text

UFL language for finite element forms Weak formulation is easy to work with for deriving adjoint Jacobian, matrix actions, etc. Source: FEniCS Book The adjoint a* of a bilinear form a is the form obtained by interchanging the two arguments The linear form Aa is the action of the bilinear form a

Slide 8

Slide 8 text

Some useful UFL operators Tensor algebra operators: transpose, tr, dor, inner, outer, cross, det, dev, sym, skew Differential operators: Spatial derivatives, gradient, divergence, curl, rot, variable derivatives, functional derivatives Discontinuous Galerkin operators: jump, avg, restriction UFL User Manual: https://fenicsproject.org/pub/documents/ufl/ufl-user-manual/ufl-user-manual.pdf

Slide 9

Slide 9 text

“Physics-based” preconditioners & solver composition in Firedrake Firedrake relies on PETSc KSP and PETSc SNES for solving linear and nonlinear problems. All the power of “command-line programming” of PETSc options is available separating the model and solver implementation. The code remains the same. ✓ matrix-free methods ✓ geometric multigrid ✓ Schur complements ✓ block Jacobi, block Gauss-Seidel Robert C. Kirby, Lawrence Mitchell “Solver composition across the PDE/linear algebra barrier”, https://arxiv.org/abs/1706.01346

Slide 10

Slide 10 text

Example | Runtime solver composition in Firedrake 〈 p, q 〉 − 〈 q, div u 〉 + λ 〈 v, u 〉 + 〈 div v, p 〉 = 〈 f, q 〉   ∀ v ∈ V ₁ , q ∈ V ₂ https://www.firedrakeproject.org/solving-interface.html#preconditioning-mixed-finite-element-systems S=D−CA ⁻ ¹B “diag” “lower” “upper” “FULL”

Slide 11

Slide 11 text

Solving time-dependent problems | Irksome The Irksome library provides Implicit Runge Kutta methods for Firedrake. https://github.com/firedrakeproject/Irksome

Slide 12

Slide 12 text

Solving time-dependent problems | firedrake-ts The firedrake-ts library provides an interface to PETSc TS for the scalable solution of DAEs arising from the discretization of time-dependent PDEs. https://github.com/IvanYashchuk/firedrake-ts

Slide 13

Slide 13 text

Solving time-dependent problems | firedrake-ts Plans: IMEX methods, interface to Julia’s DifferentialEquations.jl https://github.com/IvanYashchuk/firedrake-ts The current PETSc TS interface for solving time dependent problems assumes the problem is written in the form F(t, u, u ̇ ) = 0, u(t ₀ ) = u ₀ . The Jacobian required by PETSc TS is automatically derived with UFL

Slide 14

Slide 14 text

Parallel support Parallelism is implemented using MPI. Usual workflow: ● initial solver implementation on a desktop/laptop for small scale problem ● run the same code on a supercomputer using many parallel processes mpirun -np 128 python script.py

Slide 15

Slide 15 text

Simple Allen-Cahn variationally derived

Slide 16

Slide 16 text

Simple Allen-Cahn variationally derived

Slide 17

Slide 17 text

Simple Allen-Cahn variationally derived | Code example R( ϕ̇ , ϕ , t) = τ ( ϕ ) 〈ϕ̇ , v 〉 + 〈 δ F/ δϕ , v 〉 ∀ v ∈ V In code: R_AC = tau(phi) * inner(dphi_dt, v) * dx + derivative(F, phi, v) * dx Then just problem = firedrake_ts.DAEProblem(R_AC,phi,dphi_dt,(0.0, 1.0),bcs=bc) solver = firedrake_ts.DAESolver(problem) solver.solve()

Slide 18

Slide 18 text

PFHub benchmarks Great way to get started with implementing own phase field solver and share knowledge! https://pages.nist.gov/pfhub/

Slide 19

Slide 19 text

Benchmark Problem 1: Spinodal Decomposition The free energy of the system: Chemical free energy density: Note: Argyris and Bell finite elements are available for C¹ discretizations Robert C. Kirby, Lawrence Mitchell “Code generation for generally mapped finite elements”, https://arxiv.org/abs/1808.05513

Slide 20

Slide 20 text

Benchmark Problem 1: Spinodal Decomposition The free energy of the system:

Slide 21

Slide 21 text

Benchmark Problem 1: Spinodal Decomposition Chemical free energy density:

Slide 22

Slide 22 text

Benchmark Problem 1: Spinodal Decomposition Governing equation:

Slide 23

Slide 23 text

Benchmark Problem 1: Spinodal Decomposition Note: Argyris and Bell finite elements are available for C¹ discretizations Robert C. Kirby, Lawrence Mitchell “Code generation for generally mapped finite elements”, https://arxiv.org/abs/1808.05513

Slide 24

Slide 24 text

Benchmark Problem 1: Spinodal Decomposition (case “d”)

Slide 25

Slide 25 text

Benchmark Problem 2: Ostwald Ripening Vector Allen-Cahn + Cahn-Hilliard equations. The free energy of the system:

Slide 26

Slide 26 text

Benchmark Problem 2: Ostwald Ripening The chemical free energy density:

Slide 27

Slide 27 text

Benchmark Problem 2: Ostwald Ripening Cahn-Hilliard equation: Allen-Cahn equations:

Slide 28

Slide 28 text

Benchmark Problem 3: Dendritic Growth Allen-Cahn + Heat equations https://github.com/IvanYashchuk/pf-hub-benchmark3

Slide 29

Slide 29 text

Similar code for other benchmarks! ● Benchmark Problem 4: Elastic Precipitate Cahn-Hilliard + Linear Elasticity FEniCS demo for Hyperelasticity: https://fenicsproject.org/olddocs/dolfin/latest/python/demos/hyperelasticity/demo_hyperelasticity.py.html ● Benchmark Problem 5: Stokes Flow FEniCS demo for Stokes equations: https://fenicsproject.org/olddocs/dolfin/latest/python/demos/stokes-iterative/demo_stokes-iterative.py.html ● Benchmark Problem 6: Electrostatics Cahn-Hilliard + Poisson Equation

Slide 30

Slide 30 text

Example: Navier-Stokes+Cahn-Hilliard+Heat Tatu Pinomaa, Ivan Yashchuk, Matti Lindroos, Tom Andersson, Nikolas Provatas, Anssi Laukkanen “Process-Structure-Properties-Performance Modeling for Selective Laser Melting”, 2019, https://doi.org/10.3390/met9111138

Slide 31

Slide 31 text

Future developments | AMR with FEniCS ● Automatic AMR using adjoint/dual estimators ● There is no coarsening 😔 ● Adaptive meshing is removed in the new version FEniCS-X Melting of octadecane (Navier-Stokes+Advection-Diffusion)

Slide 32

Slide 32 text

Future developments | AMR with Firedrake ● There is no official support for AMR ● There is some effort and on some branch this functionality exists Nicolas Barral, et al. Anisotropic mesh adaptation in Firedrake with PETSc DMPlex, https://arxiv.org/abs/1610.09874, 2016 April 2021 update:

Slide 33

Slide 33 text

Future developments | AMR with DUNE Did not try personally but looks promising! Same model specification using UFL. Peter Bastian, Markus Blatt, Andreas Dedner, Nils-Arne Dreier, Christian Engwer, René Fritze, Carsten Gräser, Christoph Grüninger, Dominic Kempf, Robert Klöfkorn, Mario Ohlberger, Oliver Sander “The DUNE Framework: Basic Concepts and Recent Developments”, https://arxiv.org/abs/1909.13672

Slide 34

Slide 34 text

Future developments | GPU support for Firedrake ✓ PETSc supports GPUs ✗ Firedrake doesn’t have an official support of assembling matrices and vectors on GPUs There is an ongoing work by Kaushik Kulkarni and Andreas Kloeckner (UIUC) SIAM CSE21 poster: https://ssl.tiker.net/nextcloud/s/rsSa6KsYEsA3xqk

Slide 35

Slide 35 text

Automatic Differentiation libraries + FEniCS/Firedrake FEniCS’21 presentation: https://fenics2021.com/talks/yashchuk.html Embedding of PDE solvers into optimization and machine learning packages

Slide 36

Slide 36 text

https://ivanyashchuk.github.io/fenics_pymccon2020/ PyMC3

Slide 37

Slide 37 text

https://github.com/IvanYashchuk/PyFenicsAD.jl Julia | Turing.jl Summary Statistics parameters mean std naive_se mcse ess rhat Symbol Float64 Float64 Float64 Float64 Float64 Float64 kappa0 1.2497 0.3789 0.0120 0.0357 130.3485 1.0001 kappa1 0.5443 0.1711 0.0054 0.0155 139.7087 1.0001 σ 0.0143 0.0019 0.0001 0.0001 178.8158 1.0043 Quantiles parameters 2.5% 25.0% 50.0% 75.0% 97.5% Symbol Float64 Float64 Float64 Float64 Float64 kappa0 0.5183 0.9850 1.2590 1.5483 1.9140 kappa1 0.2295 0.4291 0.5424 0.6698 0.8579 σ 0.0111 0.0130 0.0142 0.0154 0.0188

Slide 38

Slide 38 text

Conclusions Firedrake and FEniCS are powerful libraries with many useful tools for developing a phase field solver possibly mixed with different kind of physics. ● Keep the models and solvers separate ● Rely on UFL to do derivatives and other weak form transformations ● Mostly same code runs on supercomputers How to get started? 1. Start with FEniCS tutorial “Solving PDEs in Python - The FEniCS Tutorial Volume I” by Hans Petter Langtangen, Anders Logg 2. Join the FEniCS Discourse forum to ask questions on UFL or other aspects 3. Continue with implementing PFHub benchmark problems 4. Try using Firedrake and firedrake-ts ivan.yashchuk@aalto.fi