Inside Finite Elements 9783110373202, 9783110373172

All relevant implementation aspects of finite element methods are discussed in this book. The focus is on algorithms and

228 24 4MB

English Pages 157 [158] Year 2016

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Contents
Preface
1. Finite Element Fundamentals
1.1 Stationary Heat Equation
1.2 Boundary Conditions
1.3 Weak Formulation
1.4 Galerkin Discretization
1.5 Finite Elements
Exercises
2. Grids and Finite Elements
2.1 Cells and Vertices
2.2 Shape and Ansatz Functions
2.3 Grid Generation and Visualization
2.3.1 Preprocessing
2.3.2 Postprocessing
2.4 Edges and Boundaries
Exercises
3. Assembly
3.1 System Matrices
3.1.1 Elemental Stiffness Matrices
3.1.2 Scattering
3.1.3 Elemental Mass Matrices
3.2 Boundary Conditions
3.2.1 Robin Boundary Conditions
3.2.2 Dirichlet Boundary Conditions
3.3 Right Hand Sides
3.4 Examples
3.5 Sparse Matrix Storage Formats
3.5.1 Triplet Format
3.5.2 Compressed Row Storage
3.5.3 Computational Efficiency
Exercises
4. Solvers
4.1 Direct Solvers
4.2 Iterative Solvers
4.2.1 Stationary Iterations
4.2.2 Gradient Descent
4.2.3 Conjugate Gradient Method
4.3 Examples
Exercises
5. Error Estimation
5.1 Hierarchical Error Estimation
5.2 DLY simplification
5.3 Assembly
5.3.1 Elemental Matrices
5.3.2 Elemental Right Hand Sides
5.3.3 Quadrature Rules
5.4 Examples
Exercises
6. Mesh Refinement
6.1 Marking Strategies
6.2 Refinement
6.2.1 Bisection
6.2.2 Triangle splitting
6.3 Grid Hierarchy
6.4 Examples
Exercises
7. Multigrid
7.1 Smoothers
7.2 Coarse Grid Correction
7.3 Multigrid Solver
7.4 Additive Multilevel Preconditioners
7.5 Examples
Exercises
8. Elastomechanics
8.1 Linear Elastomechanics
8.2 Vectorial Finite Elements
8.3 Time-harmonic Dynamics
8.3.1 Eigenfunctions
8.4 Examples
8.4.1 Stationary Problems
8.4.2 Oscillatory Problems
Exercises
9. Fluid Mechanics
9.1 Stokes Equation
9.2 Uzawa’s Method
9.3 Examples
9.3.1 Discretization
9.3.2 Iterative Solution
Exercises
A. Grid Data Structure
B. Function Reference
Bibliography
Index
Recommend Papers

Inside Finite Elements
 9783110373202, 9783110373172

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

Martin Weiser Inside Finite Elements De Gruyter Graduate

Also of Interest Nonlinear Dynamics Lukovsky, 2015 ISBN 978-3-11-031655-1, e-ISBN (PDF) 978-3-11-031657-5, e-ISBN (EPUB) 978-3-11-031657-5

Riemann-Roch Spaces and Computation Alvanos, 2015 ISBN 978-3-11-042613-7, e-ISBN (PDF) 978-3-11-042612-0, e-ISBN (EPUB) 978-3-11-043948-9

Recursion Theory Chong/ Yu, 2015 ISBN 978-3-11-027555-1, e-ISBN (PDF) 978-3-11-027564-3, e-ISBN (EPUB) 978-3-11-038129-0

A Course in Mathematical Cryptography Baumslag/Fine/Kreuzer/Rosenberger, 2015 ISBN 978-3-11-037276-2, e-ISBN (PDF) 978-3-11-037277-9, e-ISBN (EPUB) 978-3-11-038616-5

The Method of the Generalised Eikonal Vesnik, 2015 ISBN 978-3-11-031112-9, e-ISBN (PDF) 978-3-11-031129-7, e-ISBN (EPUB) 978-3-11-038301-0

Martin Weiser

Inside Finite Elements |

Mathematics Subject Classification 2010 65-01, 65N30, 65N50, 65N55 Author Dr. Martin Weiser Konrad-Zuse-Zentrum für Informationstechnik Berlin Takustr. 7 14195 Berlin [email protected]

ISBN 978-3-11-037317-2 e-ISBN (PDF) 978-3-11-037320-2 e-ISBN (EPUB) 978-3-11-038618-9 Library of Congress Cataloging-in-Publication Data A CIP catalog record for this book has been applied for at the Library of Congress. Bibliographic information published by the Deutsche Nationalbibliothek The Deutsche Nationalbibliothek lists this publication in the Deutsche Nationalbibliografie; detailed bibliographic data are available on the Internet at http://dnb.dnb.de. © 2016 Walter de Gruyter GmbH, Berlin/Boston Typesetting: le-tex publishing services GmbH, Leipzig Printing and binding: CPI books GmbH, Leck ♾ Printed on acid-free paper Printed in Germany www.degruyter.com

Preface This book deals with the algorithmic realization of finite element methods for partial differential equations (PDEs) and complements general textbooks on numerical analysis of PDEs. It emerged from courses I gave at Technische Universität Berlin and Freie Universität Berlin for students interested in scientific computing and applied mathematics, with the intention to provide a look behind the scenes of finite element technology and to supplement more theoretically oriented numerical analysis courses and books on PDEs and finite elements. The focus is therefore on a detailed discussion of algorithmic aspects rather than on theoretical considerations, which are limited to the necessary minimum. Its content should be accessible not only to students of mathematics, but also to physicists and engineers. Prerequisites for enjoying the book are pronounced computer and implementation affinity, in particular a basic knowledge of the Matlab programming language, see, e.g., [5, 41, 42, 52, 57], as well as a general understanding of numerical analysis. Familiarity with the functional analytic background of finite element theory is helpful, but can be readily made up for by ignorance. The reader interested in more theoretical topics is referred to the textbook “Adaptive numerical solution of PDEs” by P. Deuflhard and myself [30]. Alternative monographs would be [14, 18, 33, 45]. An integral part of the book are the programming exercises. In the course of the book, a 2D adaptive finite element code is designed in Matlab, all components of which are built one by one in precisely specified tasks. The resulting finite element package is powerful enough to solve nontrivial problems in a satisfactory way, yet simple enough to be implemented in essential parts by students during one or completely during two courses. The restriction allowing this simplicity is the limitation to 2D problems on triangular grids with piecewise linear finite elements, and a certain lack of efficiency. The design of interfaces and code is intended to be rather basic than fancy, for which reason only the most elementary features of the Matlab language are used. Consequently, the code can be run in Octave as well. At the end of the complete course, the participants have implemented their own code capable of solving stationary and time-harmonic, scalar and vectorial problems on adaptively refined meshes with multigrid efficiency. Having achieved this essentially from scratch gives a unique satisfaction. Most chapters end with a couple of examples illustrating either the properties of the implemented algorithms or some simplified application problems. Seeing that the code can be used to treat nontrivial problems should give some upfront motivation. Of course, the idea of a simple Matlab-based finite element toolkit for teaching is neither new nor unique. Related projects are LehrFEM from ETH Zürich, SOFEA by P. Krysl [47], iFEM by L. Chen [19], and MCLite by M. Holst [43], all of them being more complex than the current course implementation and mostly not intended to be im-

VI | Preface

plemented in large parts by the students themselves. See also [48], and, of course, the commercial MATLAB Partial Differential Equation Toolbox. In Chapter 1, the basic concepts of finite elements, such as differential operators, boundary conditions, weak formulations and Galerkin discretizations are briefly recalled. Chapter 2 focuses on grids, their creation and representation, as well as on finite element functions defined on grids and their visualization. The corresponding programming exercises provide functionality for reading grids and writing finite element functions from and to files in suitable formats. Chapter 3 is concerned with the assembly process, building up Galerkin systems from elemental matrices derived analytically. Programming exercises include implementing the computation of stiffness and mass matrices as well as Robin boundary condition terms. At the end of this chapter, first PDE problems can be solved using the code from the exercises. Chapter 4 covers classical direct and iterative solvers. Except for Jacobi smoothers which will be needed for multigrid later on, this chapter is not essential for advancing the implementation, but sheds some light on how solvers actually work. Chapter 5 treats hierarchical error estimation with quadratic bubble functions and introduces numerical quadrature as alternative method of assembly. The error estimator implemented here is required in Chapter 6 for adaptive mesh refinement. Longest edge bisection is implemented in the programming tasks. The vertex hierarchy created by mesh refinement is exploited in Chapter 7 for implementing fast multigrid solvers. Chapter 8 switches the focus to problems of linear elasticity, introducing vectorial finite element solutions and eigenvalue problems. Finally, Chapter 9 treats Stokes flow as the most simple example of fluid mechanics. The book contains enough material to cover two courses. A selection of topics is therefore necessary for the design of a single course with four hours a week. Suggested itineraries are either 1 – 2 – 3 – 4 – 5 – 6 – 7 advancing to complex, efficient algorithms but limited to rather simple problems (the numerical analysis way of life), or 1 – 2 – 3 – 4 – 8 – 9 aiming at simulating more complex physical phenomena with a more elementary implementation (the real life track). An advanced follow-up course can catch up the omitted Chapters 8 – 9 or 5 – 6 – 7, respectively. A subset of the exercises forms the backbone of the complete finite element code to be implemented, and hence are the most important ones. Accordingly, they are marked as such. Other exercises are redundant with detailed explanations in the book, and should only be selected if the corresponding part is omitted from the course presentation. Those are marked as well. Finally, some programming tasks are really challenging, in particular mesh refinement, and should be posed only with sufficient support.

Preface |

VII

Acknowledgements It is my greatest pleasure to thank all the people who have contributed to the book in one way or the other, or supported me during writing. First to mention is Peter Deuflhard, without whom the textbook “Adaptive numerical solution of PDEs” would not be here and hence the companion book would not have come to life. Second, Bodo Erdmann for constantly pushing me to write the book. I thank him, Sebastian Götschel, and Anton Schiela for careful reading of the manuscript and helpful comments. I’d particularly like to thank the attendants of the Inside Finite Elements courses I’ve taught, who tested the exercises for practicability, suffered from initial flaws in the design, and provided valuable feedback. Last but not least, I can express my deep gratitude to my wife Silvia and son Richard for enduring my work on the manuscript. Berlin, November 2015 Martin Weiser

Contents Preface | V 1 Finite Element Fundamentals | 1 1.1 Stationary Heat Equation | 1 1.2 Boundary Conditions | 2 1.3 Weak Formulation | 3 1.4 Galerkin Discretization | 5 1.5 Finite Elements | 6 Exercises | 8 2 Grids and Finite Elements | 10 2.1 Cells and Vertices | 10 2.2 Shape and Ansatz Functions | 12 2.3 Grid Generation and Visualization | 14 2.3.1 Preprocessing | 14 2.3.2 Postprocessing | 18 2.4 Edges and Boundaries | 20 Exercises | 22 3 Assembly | 24 3.1 System Matrices | 24 3.1.1 Elemental Stiffness Matrices | 25 3.1.2 Scattering | 26 3.1.3 Elemental Mass Matrices | 27 3.2 Boundary Conditions | 28 3.2.1 Robin Boundary Conditions | 28 3.2.2 Dirichlet Boundary Conditions | 29 3.3 Right Hand Sides | 31 3.4 Examples | 32 3.5 Sparse Matrix Storage Formats | 39 3.5.1 Triplet Format | 39 3.5.2 Compressed Row Storage | 40 3.5.3 Computational Efficiency | 41 Exercises | 43 4 4.1 4.2 4.2.1

Solvers | 45 Direct Solvers | 45 Iterative Solvers | 48 Stationary Iterations | 48

X | Contents

4.2.2 Gradient Descent | 49 4.2.3 Conjugate Gradient Method | 52 4.3 Examples | 54 Exercises | 58 5 Error Estimation | 61 5.1 Hierarchical Error Estimation | 61 5.2 DLY simplification | 63 5.3 Assembly | 64 5.3.1 Elemental Matrices | 65 5.3.2 Elemental Right Hand Sides | 67 5.3.3 Quadrature Rules | 68 5.4 Examples | 69 Exercises | 70 6 Mesh Refinement | 72 6.1 Marking Strategies | 72 6.2 Refinement | 73 6.2.1 Bisection | 74 6.2.2 Triangle splitting | 77 6.3 Grid Hierarchy | 78 6.4 Examples | 78 Exercises | 81 7 Multigrid | 83 7.1 Smoothers | 83 7.2 Coarse Grid Correction | 84 7.3 Multigrid Solver | 86 7.4 Additive Multilevel Preconditioners | 89 7.5 Examples | 91 Exercises | 96 8 Elastomechanics | 98 8.1 Linear Elastomechanics | 98 8.2 Vectorial Finite Elements | 100 8.3 Time-harmonic Dynamics | 102 8.3.1 Eigenfunctions | 103 8.4 Examples | 103 8.4.1 Stationary Problems | 103 8.4.2 Oscillatory Problems | 112 Exercises | 115

Contents | XI

9 Fluid Mechanics | 117 9.1 Stokes Equation | 118 9.2 Uzawa’s Method | 121 9.3 Examples | 124 9.3.1 Discretization | 124 9.3.2 Iterative Solution | 126 Exercises | 129 A

Grid Data Structure | 131

B

Function Reference | 133

Bibliography | 141 Index | 144

1 Finite Element Fundamentals Finite elements are a discretization technique in particular for second order partial differential equations. They combine applicability to problems on irregular domains and with non-constant coefficients, locality of ansatz functions, and a solid theoretical background. We will start with briefly describing the prototypical problems we will address here, and give a conceptual introduction into finite elements. The interested reader will find a much more detailed exposition of the fundamentals in [30, Chapter 1.4].

1.1 Stationary Heat Equation Consider a domain 𝛺 ⊂ ℝ𝑑 occupied by some solid material, and assume that at time 𝑡 = 0 its temperature distribution is 𝑇 ∈ 𝐶2 (𝛺). The second law of thermodynamics states a heat flux from regions with higher temperature to regions with lower temperature, and Fourier’s law states a proportionality between the heat flux and the temperature difference. From the temperature distribution 𝑇 we therefore obtain the heat flux 𝑞 by differentiation: 𝑞 = −𝜎∇𝑇. The proportionality constant 𝜎 is known as heat conductivity. An arbitrary, fixed test subdomain 𝜔 ⊂ 𝛺 gains or looses heat according to the flux nT 𝑞 across its boundary and, possibly, heat sources 𝑓 in its interior. Here, n denotes the unit outer normal of 𝜕𝜔. The connection between heat and temperature change is usually again a proportionality with constant 𝑐 known as heat capacity. Hence, the average temperature change in 𝜔 can be expressed as

𝑑 ∫ 𝑐𝑇(𝑥) d𝑥 = ∫ nT 𝜎∇𝑇(𝑠) d𝑠 + ∫ 𝑓(𝑥) d𝑥 . 𝑑𝑡 𝜔

𝜕𝜔

(1.1)

𝜔

Assuming a stationary situation, the left hand side of (1.1) vanishes. Applying the Theorem of Gauss [30, Theorem A.3] then gives

∫(div(𝜎∇𝑇(𝑥)) + 𝑓(𝑥)) d𝑥 = 0 .

(1.2)

𝜔

This holds for arbitrary test domains 𝜔. In the limit of shrinking 𝜔 to a single point – which we can do as all involved functions are continuous – we obtain the pointwise equation − div(𝜎∇𝑇(𝑥)) = 𝑓(𝑥) ∀𝑥 ∈ 𝛺 . (1.3) The highest occurring derivative is the second one, such that this equation belongs to the class of second order PDEs. The derivation shown here for heat transport, is essentially the same for all processes where a flux is proportional to a gradient, such as electrostatics or diffusion

2 | 1 Finite Element Fundamentals

(Brownian motion), and extends to more general systems such as elastomechanics and fluid mechanics. Abstracting from temperature 𝑇 as the physical quantity to be computed, the variable in second order PDEs is usually denoted by 𝑢, and we will follow this convention. In anisotropic materials, 𝜎 ∈ ℝ𝑑×𝑑 is a symmetric positive definite matrix, whereas in simpler isotropic materials, it is just a scalar multiple of the identity matrix, and usually interpreted as scalar itself. With 𝜎 = 1, the stationary heat equation (1.3) turns into the Poisson equation

− Δ𝑢 = 𝑓 in 𝛺 ,

(1.4)

where the Laplace operator Δ is defined as 𝑑

𝑑

Δ𝑢 = div(∇𝑢) = ∑ 𝑢𝑥𝑖 ,𝑥𝑖 = ∑ 𝑖=1

𝑖=1

𝜕2 𝑢 . 𝜕𝑥2𝑖

(1.5)

Poisson’s equation (1.4) is the prototypical example of second order PDEs of elliptic type. “Elliptic” refers to the positive eigenvalue structure of this differential operator [30, Chapter 1.6], which will lead to convex variational functionals in Section 1.3. In the following chapters, we will work out finite element methods for the slightly more general equation

− div(𝜎∇𝑢) + 𝜌𝑢 = 𝑓 in 𝛺 ,

(1.6)

with spatially varying 𝜎(𝑥) ∈ ℝ2×2 and 𝜌(𝑥) ∈ ℝ.

1.2 Boundary Conditions Equation (1.6) on its own has infinitely many solutions, as constant and linear functions and much more lie in the nullspace of the Laplace operator. In order to select a unique solution, additional conditions have to be prescribed. This is usually done in form of boundary conditions defining either the heat fluxes across or the temperature on the boundary of 𝛺, or a linear combination of both. Dirichlet boundary conditions specify the value of 𝑢 on a subset 𝛤𝐷 of the boundary, 𝑢 = 𝑢𝐷 on 𝛤𝐷 , (1.7) whereas Neumann boundary conditions specify the fluxes nT 𝜎∇𝑢,

nT 𝜎∇𝑢 = 𝑢𝑁 on 𝛤𝑁 .

(1.8)

The equivalent notation 𝑢𝑥 𝜎n = 𝑢𝑁 is less common, but translates easier to vectorial problems, see Chapter 9.

1.3 Weak Formulation |

3

These boundary conditions are idealized forms of actual physical situations. Most often, fluxes and values are related, such as the heat flux is approximately proportional to the temperature difference 𝑢 − 𝑢𝐷 of the considered material and the outside world. This relation is given in form of Robin boundary conditions,

nT 𝜎∇𝑢 + 𝛼(𝑢 − 𝑢𝐷 ) = 0 on 𝛤𝑅 .

(1.9)

These boundary conditions are also formulated in a more abstract but equivalent way as nT 𝜎∇𝑢 + 𝛼𝑢 = 𝛽 on 𝛤𝑅 (1.10) with 𝛽 = 𝛼𝑢𝐷 . All the boundary data 𝑢𝐷 , 𝑢𝑁 , 𝛼 may depend on the spatial position. Formally, the limit cases 𝛼 → 0 and 𝛼 → ∞ correspond to Neumann and Dirichlet conditions, respectively. In favor of a simple implementation of finite elements, we will therefore restrict the treatment to pure Robin boundary conditions, and assume 𝛤𝑅 = 𝜕𝛺 throughout. Dirichlet boundary conditions are approximated by choosing 𝛼 sufficiently “large”. The implications of this particular choice of boundary conditions and alternatives are discussed in Chapters 3 and 4.

1.3 Weak Formulation One drawback of the PDE formulation (1.6), (1.9) is that twice differentiable functions 𝑢 ∈ 𝐶2 (𝛺) ∩ 𝐶1 (𝛺) are required for the second order differential operator, and the boundary conditions, but such smooth solutions do not exist in many situations of practical interest. The notion of solution has therefore been generalized to less smooth functions, introducing the concept of weak solutions. Reverting the application of the Theorem of Gauss in the derivation of the stationary heat equation in Section 1.1 does the trick. If (1.6) holds, then

∫ (− div(𝜎∇𝑢)𝑣 + 𝜌𝑢𝑣) d𝑥 = ∫ 𝑓𝑣 d𝑥 𝛺

𝛺

holds for every continuously differentiable test function 𝑣 ∈ 𝐶1 (𝛺) as well. Applying the Theorem of Gauss to the left hand side, we obtain

∫ (∇𝑣T 𝜎∇𝑢 + 𝜌𝑢𝑣) d𝑥 − ∫ nT 𝜎∇𝑢 𝑣 d𝑠 = ∫ 𝑓𝑣 d𝑥 , 𝛺

𝛺

𝜕𝛺

and by insertion of the Robin boundary condition (1.9)

∫ (∇𝑣T 𝜎∇𝑢 + 𝜌𝑢𝑣) d𝑥 + ∫ 𝛼𝑢𝑣 d𝑠 = ∫ 𝑓𝑣 d𝑥 + ∫ 𝛽𝑣 d𝑠 . 𝛺

𝜕𝛺

𝛺

𝜕𝛺

(1.11)

4 | 1 Finite Element Fundamentals

Defining the symmetric bilinear form

𝑎(𝑢, 𝑣) = ∫ (∇𝑣T 𝜎∇𝑢 + 𝜌𝑢𝑣) d𝑥 + ∫ 𝛼𝑢𝑣 d𝑠 𝛺

(1.12)

𝜕𝛺

and the linear functional

⟨𝑏, 𝑣⟩ = ∫ 𝑓𝑣 d𝑥 + ∫ 𝛽𝑣 d𝑠 𝛺

𝜕𝛺

allows to write (1.11) in a compact way:

𝑎(𝑢, 𝑣) = ⟨𝑏, 𝑣⟩ ∀𝑣 ∈ 𝐶1 (𝛺) ∩ 𝐶(𝛺)

(1.13)

This is the necessary condition for a stationary point of the variational functional

𝐽(𝑢) =

1 𝑎(𝑢, 𝑢) − ⟨𝑏, 𝑢⟩ . 2

(1.14)

It turns out that 𝑎 is 𝑉-elliptic, that is both bounded, i.e.

𝑎(𝑢, 𝑣) ≤ 𝐶𝑎 ‖𝑢‖𝑉 ‖𝑣‖𝑉 for some 𝐶𝑎 < ∞ ,

(1.15)

𝑎(𝑢, 𝑢) ≥ 𝛼𝑎 ‖𝑢‖2𝑉 for some 𝛼𝑎 > 0

(1.16)

and coercive, i.e. (as long as 𝜎 is positive definite almost everywhere and 𝛼 is nonnegative and positive at least in some part of 𝜕𝛺), on the Sobolev space 𝑉 = 𝐻1 (𝛺) of at least once weakly differentiable functions. This space is the completion of 𝐶1 (𝛺) with respect to the Sobolev norm ‖𝑢‖2𝐻1 (𝛺) = ‖𝑢‖2𝐿2 (𝛺) + ‖∇𝑢‖2𝐿2 (𝛺) . (1.17) The Theorem of Lax–Milgram [30, Theorem A.11] then guarantees the existence of a unique minimizer 𝑢 of 𝐽 in 𝑉. Thus, a generalized solution of (1.6) (the weak solution) can be characterized equivalently by

𝑎(𝑢, 𝑣) = ⟨𝑏, 𝑣⟩ ∀𝑣 ∈ 𝐻1 (𝛺)

(1.18)

and by the minimization problem

min 𝐽(𝑢) .

𝑢∈𝐻1 (𝛺)

(1.19)

Note that the conditions (1.15) and (1.16) imply that the energy norm

‖𝑢‖2𝑎 = 𝑎(𝑢, 𝑢) induced by the bilinear form 𝑎 is equivalent to the norm in the Hilbert space 𝑉:

√𝛼𝑎 ‖𝑢‖𝑉 ≤ ‖𝑢‖𝑎 ≤ √𝐶𝑎 ‖𝑢‖𝑉 .

(1.20)

1.4 Galerkin Discretization

|

5

The bilinear form 𝑎 induces a symmetric operator 𝐴 : 𝑉 → 𝑉∗ satisfying ⟨𝐴𝑢, 𝑣⟩ = 𝑎(𝑢, 𝑣) for all 𝑢, 𝑣 ∈ 𝑉. In terms of this operator, (1.18) reads

𝐴𝑢 = 𝑏

(1.21)

as an equation in 𝑉∗ . Whether to use (1.11) or (1.21) is mostly a question of personal preference, as both are equivalent. Due to (1.15) and (1.16), 𝐴 is positive definite and bounded. Accordingly, 𝐴 induces the energy norm ‖𝑢‖2𝐴 = ⟨𝐴𝑢, 𝑢⟩ = ‖𝑢‖2𝑎 . Denoting the same norm, ‖ ⋅ ‖𝑎 and ‖ ⋅ ‖𝐴 could be used interchangeably, but we will stick to ‖ ⋅ ‖𝑎 and reserve the notation ‖ ⋅ ‖𝐴 for energy norms on finite dimensional spaces in Chapter 4.

1.4 Galerkin Discretization The analysis above does not provide actual solutions of concrete problems except for the most simple examples. More interesting problems, let alone real-world problems, require a numerical approximation of the solution to be computed. As computers cannot handle infinite-dimensional problems directly, the weak formulation (1.18) or the equivalent minimization problem (1.19) have to be discretized. The idea of Ritz minimization is to minimize 𝐽 not over the space 𝐻1 (𝛺), but over a finite dimensional ansatz space 𝑉ℎ ⊂ 𝐻1 (𝛺), see Figure 1.1:

min 𝐽(𝑢ℎ )

𝑢ℎ ∈𝑉ℎ

Fig. 1.1. Ritz minimization. The Ritz solution is the minimizer of the variational functional in a linear subspace.

6 | 1 Finite Element Fundamentals

Due to convexity and coerciveness, this minimization problem has a unique solution 𝑢ℎ ∈ 𝑉ℎ . The thus defined approximation of the exact solution 𝑢 exhibits particularly favourable properties: It is the best approximation of 𝑢 in 𝑉ℎ with respect to the energy norm, i.e. ‖𝑢 − 𝑢ℎ ‖𝑎 = min ‖𝑢 − 𝑣‖𝑎 , (1.22) 𝑣∈𝑉ℎ

and it is 𝑎-orthogonal to 𝑉ℎ , i.e. 𝑎(𝑢 − 𝑢ℎ , 𝑣) = 0 for all 𝑣 ∈ 𝑉ℎ . Combined with the norm equivalence (1.20), these properties imply the claim

‖𝑢 − 𝑢ℎ ‖𝑉 ≤ √

𝐶𝑎 inf ‖𝑢 − 𝑣‖𝑉 𝛼𝑎 𝑣∈𝑉ℎ

(1.23)

of the Céa-Lemma, see Exercise 1.2. The necessary and sufficient condition for 𝑢ℎ being the solution is

𝐽󸀠 (𝑢ℎ )𝑣ℎ = 0 ∀𝑣ℎ ∈ 𝑉ℎ ⇔ 𝑎(𝑢ℎ , 𝑣ℎ ) = ⟨𝑏, 𝑣ℎ ⟩ ∀𝑣ℎ ∈ 𝑉ℎ .

(1.24)

This restriction of (1.18) to the test space 𝑉ℎ , resulting in (1.24), is called Galerkin discretization. It converts the PDE into a system of linear equations: If (𝜑𝑖 )1≤𝑖≤𝑛 is a basis of 𝑉ℎ , then (1.24) is equivalent to

𝑎(𝑢ℎ , 𝜑𝑖 ) = ⟨𝑏, 𝜑𝑖 ⟩ for 𝑖 = 1, . . . , 𝑛 . 𝜑

𝑛

𝜑

Representing 𝑢ℎ in terms of its coefficient vector 𝑢ℎ ∈ ℝ𝑛 as 𝑢ℎ = ∑𝑗=1 (𝑢ℎ )𝑗 𝜑𝑗 and exploiting the bilinearity of 𝑎, this can be written as the linear equation system 𝑛

𝜑

∑ 𝑎(𝜑𝑗 , 𝜑𝑖 )(𝑢ℎ )𝑗 = ⟨𝑏, 𝜑𝑖 ⟩ for 𝑖 = 1, . . . , 𝑛 𝑗=1



𝜑

𝜑

𝐴 ℎ 𝑢ℎ = 𝑏ℎ ,

(1.25)

𝜑

with (𝐴 ℎ )𝑖𝑗 = 𝑎(𝜑𝑖 , 𝜑𝑗 ) and (𝑏ℎ )𝑖 = ⟨𝑏, 𝜑𝑖 ⟩. Note that the approximate solution 𝑢ℎ is already defined by the choice of the space 𝑉ℎ , and does not depend on the choice of the basis (𝜑𝑖 )1≤𝑖≤𝑛. On the other hand, important properties of 𝐴 ℎ such as sparsity pattern and its condition number do depend on the basis. Correspondingly, different ansatz spaces and basis constructions are in use, in particular polynomial and trigonometric functions, wavelets, radial basis functions, NURBS, and, most widely used, finite elements. Most of the time, the basis of 𝑉ℎ to be used will be fixed and denoted by (𝜑𝑖 )1≤𝑖≤𝑛 , 𝜑 such that we will identify functions 𝑢ℎ ∈ 𝑉ℎ with their coefficient vectors 𝑢ℎ ∈ ℝ𝑛 . Only when different bases come into play, notably in Chapter 7, the precise notation 𝜑 𝑢ℎ will be required for distinguishing coefficient vectors from the represented functions 𝑢ℎ .

1.5 Finite Elements Finite elements belong to the class of splines. Functions on 𝛺 are represented as globally continuous, piecewise polynomial functions on a decomposition of the domain 𝛺

1.5 Finite Elements |

7

Fig. 1.2. Simplicial triangulation of a polygonal domain. The disjoint supports of two hat functions at different nodes are shaded.

into smaller, nonoverlapping, simply structured cells. These cells are usually triangles or quadrangles in 2D, and tetrahedra, hexahedra, prisms and pyramids in 3D, forming a triangulation of 𝛺. We will restrict our attention to conforming simplicial triangulations of 2D domains, as illustrated in Figure 1.2. Those consist of a set of triangular cells T , a set of edges E, and a set of nodes N = {𝑥𝑖 | 𝑖 = 1, . . . , 𝑛}. We require that the domain shall be covered by the cells, i.e. ⋃𝑇∈T 𝑇 = 𝛺, which means we assume polygonal domains. Moreover, the cells shall not overlap, i.e. int 𝑇𝑖 ∩ int 𝑇𝑗 = 0 for two different triangles 𝑇𝑖 , 𝑇𝑗 ∈ T . Conforming means that two triangles are either disjoint, or share a common edge, or a common vertex. The triangles’ edges form the edge set E, and the triangles’ vertices make up the node set N. The simplest finite element space on such a triangulation consists of globally continuous, piecewise linear functions:

𝑉ℎ = {𝑢 ∈ 𝐶(𝛺) | ∀𝑇 ∈ T : 𝑢|𝑇 ∈ ℙ1 } Here, ℙ𝑘 denotes the space of polynomials of degree at most 𝑘. The canonical nodal basis in this space is formed by the hat functions 𝜑𝑖 ∈ 𝑉ℎ with 𝜑𝑖 (𝑥𝑗 ) = 𝛿𝑖𝑗 for all 𝑖, 𝑗 = 1, . . . , 𝑛 . These basis functions have the smallest possible support, see Figures 1.2 and 1.3. Because of

(𝐴 ℎ )𝑖𝑗 = 𝑎(𝜑𝑗 , 𝜑𝑖 ) = ∫ 𝜑𝑗 𝜑𝑖 d𝑥 𝛺

=

∫ supp 𝜑𝑖 ∩supp 𝜑𝑗

(∇𝜑𝑖T 𝜎∇𝜑𝑗 + 𝜌𝜑𝑖 𝜑𝑗 ) d𝑥 +



𝛼𝜑𝑖 𝜑𝑗 d𝑠 ,

supp 𝜑𝑖 ∩supp 𝜑𝑗 ∩𝜕𝛺

most entries of the stiffness matrix 𝐴 ℎ vanish. In fact, even in very large meshes each row contains only a small number of nonzero entries, such that 𝐴 ℎ is extremely sparse.

8 | 1 Finite Element Fundamentals

Fig. 1.3. Nodal basis functions 𝜑𝑖 in 1D and 2D.

Together with the possibility to triangulate also very irregular domains, this is a key advantage of the finite element methods. Besides these practical issues, the approximation error ‖𝑢−𝑢ℎ ‖ is of interest. Measuring the error in the energy norm ‖ ⋅ ‖𝑎 is most convenient due to the best approximation property (1.23) of Galerkin approximation. By comparison to the completely local interpolation error, the Galerkin optimality also admits the derivation of a priori error estimates of the form

‖𝑢 − 𝑢ℎ ‖𝑎 ≤ ‖𝑢 − 𝐼ℎ 𝑢‖𝑎 ≤ 𝑐 ℎ‖𝑢‖𝐻2 (𝛺) for sufficiently regular, i.e. twice weakly differentiable solutions 𝑢 ∈ 𝐻2 (𝛺). Here, 𝐼ℎ : 𝐶(𝛺) → 𝑉ℎ is the interpolation operator defined by (𝐼ℎ 𝑢)(𝑥𝑖 ) = 𝑢(𝑥𝑖 ) for all 𝑥𝑖 ∈ N, and ℎ the maximal triangle diameter [30, Chapter 4.4]. Apart from the energy norm, 𝐿2 type estimates are of general interest. Here, the a priori error estimate

‖𝑢 − 𝑢ℎ ‖𝐿2 (𝛺) ≤ 𝑐 ℎ2 ‖𝑢‖𝐻2 (𝛺) can be established for 𝐻2 -regular problems. In concrete applications, however, different quantities of interest are often more important, see [30, Chapter 6].

Exercises Exercise 1.1. Explore the structure of the Sobolev space 𝐻1 (𝛺). Which of the following functions belong to it?

𝛺 = ]0, 1[ : 𝛺 = ]0, 1[2 :

𝑓(𝑥) = min(𝑥, 1 − 𝑥) , 𝑓(𝑥, 𝑦) = 1 − √𝑥2 + 𝑦2 ,

√𝑥 ,

log 𝑥

√4 𝑥2 + 𝑦2 ,

log(𝑥2 + 𝑦2 )

Exercise 1.2. Let 𝑎 be a 𝑉-elliptic bilinear form on a Hilbert space 𝑉, and 𝑏 a linear form. Suppose that 𝑢 ∈ 𝑉 satisfies 𝑎(𝑢, 𝑣) = ⟨𝑏, 𝑣⟩ for all 𝑣 ∈ 𝑉. Let 𝑢ℎ ∈ 𝑉ℎ be the

Exercises

| 9

Galerkin approximation of 𝑢 in a subspace 𝑉ℎ . Prove the quasi-optimality

‖𝑢 − 𝑢ℎ ‖𝑉 ≤ √ Exercise 1.3. Prove ‖𝑢‖𝐿2 (]0,1[) ≥ Hint: Compute the minimizer.

1 4

𝐶𝑎 inf ‖𝑢 − 𝑣ℎ ‖𝑉 . 𝛼𝑎 𝑣ℎ ∈𝑉ℎ

for all 𝑢 ∈ ℙ3 with 𝑢(0) = 1.

Exercise 1.4. Consider the minimization problem

min ‖𝑢‖2𝐿2 (𝛺) + (𝑢(0) − 1)2 𝑢∈𝑉

over 𝑉 = ℙ3 with 𝛺 = ]0, 1[. Compute the coercivity and boundedness constants 𝐶𝑎 and 𝛼𝑎 and give an explicit a priori error estimate for the Galerkin approximation 𝑢ℎ in the ansatz space 𝑉ℎ = ℙ0 . Hint: Use the result of Exercise 1.3. Exercise 1.5. Consider the minimization problem

min ‖𝑢‖2𝐿2 (𝛺) + (𝑢(0) − 1)2 𝑢∈𝑉

over 𝑉 = ℙ4 with 𝛺 = ]0, 1[. Compute and plot the solution 𝑢 as well as its Galerkin approximation 𝑢ℎ in the ansatz spaces 𝑉ℎ = ℙ𝑘 for 𝑘 = 0, . . . , 3. Exercise 1.6. Solve the Poisson equation

−Δ𝑢 = 𝑥3 𝑢=0

in 𝛺 = ]0, 1[ on 𝜕𝛺

using a Galerkin method with 𝑉ℎ = ℙ4 . Compute the error of the Galerkin approximation in 𝑥 = 0.5.

2 Grids and Finite Elements Finite element discretizations are defined over triangulations of the domain. Here we will treat only simplicial triangulations of 2D domains, literal “triangulations” consisting of non-overlapping triangles covering the polygonal domain 𝛺. We will define suitable data structures describing grids, implement input and output of grids, and define and evaluate shape and ansatz functions on the grids.

2.1 Cells and Vertices For finite element computations, a data structure representing the grid is required. Its minimal contents is a list of vertices with their geometric position, and a mapping of triangles to their three vertices. For simplicity, we will number the triangles 𝑇 ∈ T from 1 to 𝑚, and the points¹ 𝑥 ∈ T from 1 to 𝑛. Then it is convenient to store the points 𝑥 as the columns of a 2 × 𝑛 matrix xp, and the numbers 𝑝 of the vertices of the triangles as the columns of a 3 × 𝑚 matrix pt, both of which are members of the grid structure 𝐺 = (xp, pt). The following code creates a grid 𝐺 with two triangles (𝑚 = 2) and four vertices (𝑛 = 4), covering the unit square: G.xp = [0 0 G.pt = [1 2 3

1 1 0 0 1 1]; 1 3 4];

Even though not strictly necessary, it is convenient to have functions extracting the number of triangles and vertices, respectively, explicitly from the grid: function n = numberOfVertices(G) n = size(G.xp,2); function m = numberOfTriangles(G) m = size(G.pt,2); The actual triangles 𝑇 in the grid are the images of the reference triangle

𝑇 = {𝜉 ∈ ℝ2 | 𝜉1 , 𝜉2 ≥ 0 ∧ 𝜉1 + 𝜉2 ≤ 1}

1 The terms vertex, point, and node all denote triangle corners in simplicial grids, but carry different connotation. Vertex stresses the algebraic connectivity properties, while node and point refer rather to the geometric position.

2.1 Cells and Vertices |

x2

11

ξ2

p3 p3

(0, 1)

xT p1 p2 x1

p1 (0, 0)

p2 (1, 0)

ξ1

Fig. 2.1. Coordinate transformation of reference triangle (right) to actual triangle in the grid (left).

under an affine mapping 𝑥𝑇 : 𝑇 → 𝑇 (see Figure 2.1). We fix the convention that 𝑥𝑇 maps the origin to the first vertex 𝑝1 , (1, 0) to the second vertex 𝑝2 , and (0, 1) to the third vertex 𝑝3 of 𝑇. As 𝑥𝑇 is affine, it is completely defined by this assignment of the reference triangle’s vertices:

𝑥𝑇 (𝜉) = 𝑝1 + 𝜉1 (𝑝2 − 𝑝1 ) + 𝜉2 (𝑝3 − 𝑝1 ) Given a grid structure G, a triangle number t and a point 𝜉 in the reference triangle, the corresponding point 𝑥 = 𝑥𝑇 (𝜉) in global coordinates can be computed. For convenience, we will hide the details in a function: function x = globalCoordinate(G,t,xi) ps = G.pt(:,t); x1 = G.xp(:,ps(1)); x = x1 + [G.xp(:,ps(2))-x1 G.xp(:,ps(3))-x1]*xi; Additionally, for the transformation formula in the integration (3.1), we will need the Jacobian of 𝑥𝑇 and its determinant. It is immediately clear that the Jacobian is

(𝑥𝑇 )𝜉 = [𝑝2 − 𝑝1 𝑝3 − 𝑝1 ] ∈ ℝ2×2 , constant over the reference triangle. Again, the details can be conveniently hidden in a function: function [B,x1] = transformationJacobian(G,t) ps = G.pt(:,t); x1 = G.xp(:,ps(1)); B = [G.xp(:,ps(2))-x1 G.xp(:,ps(3))-x1]; Using this function, we can simplify the coordinate change and implement its inverse as well:

12 | 2 Grids and Finite Elements

function x = globalCoordinate(G,t,xi) [B,x1] = transformationJacobian(G,t); x = x1 + B*xi; function xi = localCoordinate(G,t,x) [B,x1] = transformationJacobian(G,t); xi = B \ (x-x1);

2.2 Shape and Ansatz Functions For setting up a linear Galerkin equation system according to (1.25), a basis (𝜑𝑖 )1≤𝑖≤𝑛 of 𝑉ℎ needs to be defined. The basis functions are also known as ansatz functions. Defining piecewise polynomial ansatz functions 𝜑𝑖 on a triangulation is most conveniently done in terms of the affine mappings 𝜉(𝑥) from actual triangles 𝑇 to the reference triangle 𝑇. The restriction of the ansatz function 𝜑𝑖 can be represented as a linear combination of 𝑛𝑠 shape functions 𝜙𝑘 defined on the reference triangle: 𝑛𝑠

𝜑𝑖 |𝑇 (𝑥) = ∑ 𝛼𝑖,𝑇,𝑘 𝜙𝑘 (𝜉(𝑥)) ,

𝑥∈𝑇

(2.1)

𝑘=1

In this setting, 𝜑𝑖 : 𝛺 → ℝ is a (global) ansatz basis function, 𝜙𝑘 : 𝑇 → ℝ are the (local) shape functions defined on the reference triangle, and 𝛼𝑖,𝑇,𝑘 are appropriate coefficients. Global continuity of 𝜑𝑖 has to be ensured by suitable choice of the coefficients 𝛼𝑖,𝑇,𝑘 . With linear finite elements on triangular grids, this is particularly simple. On the reference triangle 𝑇 there are exactly three shape functions 𝜙1 , 𝜙2 , 𝜙3 spanning the space ℙ1 of polynomials of degree at most 1. Those are

𝜙1 (𝜉) = 1 − 𝜉1 − 𝜉2 ,

𝜙2 (𝜉) = 𝜉1 , and 𝜙3 (𝜉) = 𝜉2 ,

shown in Figure 2.2. Their gradients, written as the columns of a 2 × 3 matrix, are

−1 1 0 [∇𝜙1 ∇𝜙2 ∇𝜙3 ] = [ ] . −1 0 1

(2.2)

Evaluating the shape functions simultaneously at a given reference coordinate 𝜉 is straightforward: function sfg = shapeFunctionGradients sfg = [ -1 1 0; -1 0 1]; function sfv = shapeFunctionValues(xi) sfv = [1 0 0] + xi’ * shapeFunctionGradients;

2.2 Shape and Ansatz Functions

| 13

ξ2

ξ1 Φ1

Φ2

Φ3

Fig. 2.2. Linear shape functions 𝜙1 , 𝜙2 , 𝜙3 on the reference triangle 𝑇. The gradient ist shown as gray arrow.

Hence, the linear combination (2.1) always consists of 𝑛𝑠 = 3 terms, two of which vanish due to their coefficient 𝛼 being zero. Note that similar to the nodal basis functions, the shape functions satisfy a La𝑗 grangian property for the three vertices of the reference triangle: 𝜙𝑖 (𝑝 ) = 𝛿𝑖𝑗 . Therefore, if 𝜑𝑖 is a nodal basis function and 𝑇 a triangle incident to vertex 𝑖 such that 𝑗 𝑥(𝑝 ) = 𝑥𝑖 , then 𝜑𝑖 |𝑇 is represented by the transformed shape function 𝜙𝑗 alone:

𝜑𝑖 |𝑇 (𝑥) = 𝜙𝑗 (𝜉(𝑥)) Accordingly, the ansatz functions’ gradients are obtained by the chain rule:

∇𝜑𝑖 |𝑇 (𝑥) = (𝜑𝑖 |𝑇 )󸀠 (𝑥)T = ((𝜙𝑗 )𝜉 (𝜉)𝜉𝑥 )T = 𝜉𝑥T ∇𝜙𝑗 (𝜉)

(2.3)

Remember that the gradient is the transpose of the derivative (see [30, Appendix]). Piecewise continuous finite element functions 𝑢 ∈ 𝑉ℎ are given as linear combination of the nodal basis functions 𝜑𝑖 , 𝑛

𝑢(𝑥) = ∑ 𝑢𝑖 𝜑𝑖 (𝑥) . 𝑖=1

and can be evaluated easily in terms of the shape function representation. Assume that 𝑥 ∈ 𝑇, and 𝑇 has the vertices 𝑖, 𝑗, 𝑘, then the only nonvanishing nodal basis 1 2 3 functions on 𝑇 are 𝜑𝑖 , 𝜑𝑗 , and 𝜑𝑘 . Let 𝑥𝑖 = 𝑥(𝑝 ), 𝑥𝑗 = 𝑥(𝑝 ), 𝑥𝑘 = 𝑥(𝑝 ). Then

𝑢(𝑥) = 𝑢𝑖 𝜑𝑖 (𝑥) + 𝑢𝑗 𝜑𝑗 (𝑥) + 𝑢𝑘 𝜑𝑘 (𝑥) = 𝑢𝑖 𝜙1 (𝜉) + 𝑢𝑗 𝜙2 (𝜉) + 𝑢𝑘 𝜙3 (𝜉) holds for 𝜉 = 𝜉(𝑥). Translated into code, this could look like function y = evaluateFunction(G,u,t,x) xi = localCoordinate(G,t,x); ps = G.pt(:,t); y = shapeFunctionValues(xi) * u(ps);

14 | 2 Grids and Finite Elements

2.3 Grid Generation and Visualization Before being able to solve PDEs on a given domain by finite element methods, a suitable grid must be obtained. After the computation, the resulting solutions must be made available, e.g., for visual inspection. Those tasks are commonly referred to as pre- and postprocessing. Both are interesting and rich topics in their own, and will not be covered in any significant depth here. Instead, we will treat them as problems of input and output, restricting the attention to the computational work in between.

2.3.1 Preprocessing Before a finite element solution can be computed, a grid has to be created. First, the domain has to be defined. This is often constructed using computer aided design (CAD) tools, or given by a segmentation of medical image data. The domain must then be triangulated. Spatially varying boundary conditions and diffusion tensors have to be related to the cells of the grid. From the point of view of finite element computation, all these tasks are subsumed under the term preprocessing. Of course, this does not mean these issues are in any way trivial. Far from that, they are challenging problems in their own right, see, e.g., [34].

Structured grid generation The simplest design is a cartesian grid with tensor structure, where the vertex locations are {(𝑥1 , 𝑥2 ) ∈ 𝛺 | 𝑥1 ∈ 𝑋1 ∧𝑥2 ∈ 𝑋2 }, and 𝑋𝑖 are finite sequences in ℝ, usually equidistant. Then it is straightforward to fill the area between four points by a rectangle and split this into two triangles along a diagonal, see Figure 2.3 and Exercise 2.1. This works well on rectangular or slightly more complex but still block-structured domains. More general domains such as arbitrary polygons are much more difficult to

Fig. 2.3. A simple structured cartesian grid as created by rectangularCriss(10,5).

2.3 Grid Generation and Visualization

|

15

cover with a structured grid as long as the boundary shall be represented exactly or at least with high fidelity. For such domains, unstructured simplicial grids are usually preferable.

Direct Delaunay grid generation A standard method of 2D unstructured grid generation is to create Delaunay grids. These are simplicial triangulations of the convex hull of a given set of points such that within the circumsphere of any traingle none of the input points except its 𝑑+1 vertices are located. The importance of Delaunay triangulations lies in the fact that their existence is guaranteed and efficient algorithms for their construction are known. However, practically relevant aspects such as triangulating non-convex domains, preserving specified input edges, or observing requirements on the aspect ratio of created simplices have to be treated by additional means, and this is where mesh generation is getting complex. A straightforward Delaunay triangulation of a set of points in the plane is directly provided by Matlab and Octave. The command G.pt = delaunay(x, y)’; G.xp = [x’ y’]; creates a Delaunay grid from the points with 𝑥 and 𝑦 coordinates provided in x and y, respectively. A more elaborate mesh generator that works also for non-convex domains is described in [56].

Reading .poly file geometries A fast, versatile, and easy to use Delaunay grid generator for 2D domains is Triangle by J. Shewchuck [61]. It is a C program freely available for non-commercial use and readily installed on any Unix-like system. At the time of writing this book, Triangle can be downloaded from http://www.cs.cmu.edu/~quake/triangle.html or from the voronoi directory in Netlib. Triangle accepts polygonal domains with or without a number of holes as input, in form of .poly files. Those are text files containing a series of numbers with a particularly simple format: – First line: 𝑁 2 𝑎 𝑏𝑣 𝑁 is the number of vertices of the polygon. 2 denotes the spatial dimension. To each vertex, a number of attributes can be specified. 𝑎 is the number of these attributes. Similarly, an optional boundary marker can be assigned to each vertex, and 𝑏𝑣 ∈ {0, 1} is the number of these boundary markers. Attributes and boundary markers play no role for our purposes and can be omitted.

16 | 2 Grids and Finite Elements

Following 𝑁 lines: 𝑖 𝑥 𝑦 . . . 𝑖 is the number of the vertex defined in that line. Vertex numbers are consecutive, starting either with 0 or with 1. Triangle is so clever as to infer the start index from the input. As Matlab indices start at 1, it is most convenient to use 1 as the first vertex number. 𝑥 and 𝑦 define the spatial position of the vertex. The following 𝑎 + 𝑏𝑣 numbers (if any) are the attributes and boundary markers. One line: 𝑟 𝑏𝑠 𝑟 is the number of boundary segments of the polygon, and 𝑏𝑠 the number of boundary markers, again either 0 or 1. Following 𝑟 lines: 𝑘 𝑖 𝑗 . . . Boundary segments are numbered consecutively, and 𝑘 is the number of the segment defined in this line. 𝑖 and 𝑗 are the numbers of the vertices connected by the segment, followed by the optional boundary marker. One line: ℎ ℎ is the number of holes in the polygon. Following ℎ lines: 𝑗 𝑥 𝑦 Holes are numbered consecutively, and 𝑗 is the number of the hole. Holes consist of closed loops of boundary segments, and are specified by a point (𝑥, 𝑦) inside the hole.







– –

Empty lines are ignored, and comments start with ♯ and extend to the end of the line. The following file describes the unit square with a single hole in the lower left part: # 7 1 2 3 4 5 6 7

unit square with triangular hole in the lower left part 2 0 0 # seven vertices in 2D, no attributes/markers 0 0 1 0 1 1 0 1 0.2 0.2 0.5 0.2 0.35 0.5

7 1 2 3 4 5 6 7

0 1 2 3 4 5 6 7

# seven boundary segments, no markers 2 3 4 1 6 7 5

2.3 Grid Generation and Visualization

|

17

Fig. 2.4. Grids created by the triangle -q -a0.005 command (left) and without the -q switch (right). Note the occurrence of very thin triangles without -q.

1 1 0.35 0.3

# one hole

From such input files, Triangle creates constrained Delaunay grids, introducing additional vertices as needed, see Figure 2.4. Noteworthy are the parameters -a and -q, with which the maximal triangle area and minimum interior angles can be prescribed. The utility showme provided with triangle is a simple but functional tool for inspection of the created grids. The output of Triangle are a .node file containing the mesh vertices, and a .ele file containing the triangles of the mesh. Both files have a format similar to the .poly input file. The .node file consists of – First line: 𝑛 2 𝑎 𝑏𝑣 𝑛 is the number of vertices in the mesh, 𝑎 and 𝑏𝑣 the numbers of attributes and boundary markers, respectively. – Following 𝑛 lines: 𝑖 𝑥 𝑦 . . . 𝑖 is the vertex number, and (𝑥, 𝑦) its position. The remaining numbers, if any, are attributes and boundary marker. The .ele file consists of: – First line: 𝑚 𝑠 𝑎 𝑚 is the triangle number, 𝑠 the number of points in the triangle (usually 3), and 𝑎 the number of attributes, which are interpolated from the input vertices. – Following 𝑚 lines: 𝑡 𝑖 𝑗 𝑘 . . . 𝑡 is the triangle number, 𝑖, 𝑗, 𝑘 the vertex numbers, and the following 𝑠 − 3 + 𝑎 numbers the edge midpoint numbers (if 𝑠 > 3) and the attributes

18 | 2 Grids and Finite Elements

Thus, the .node and .ele files contain exactly the information that is needed for the grid structure fields G.xp and G.pt. Creating a grid from those files is left as Exercise 2.3.

2.3.2 Postprocessing Simulation without interpretation of the results would be useless, hence postprocessing is an important functionality. The most direct and intuitive kind of postprocessing is visual inspection of the finite element solution. More important in practical applications is of course the extraction of quantitative information, e.g., the load under which an elastic structure breaks or buckles. This is, however, highly problem dependent, such that we will restrict ourselves to simple plotting of the results.

Direct visualization An immediate way of visualizing the solution of a finite element computation on unstructured triangular grids is to use the built-in Matlab graphical routines trisurf or trimesh. They create a 3D “flying carpet” picture of the solution, with altitude 𝑧 parametrized over the domain in the 𝑥, 𝑦 plane. The following simple function provides this functionality. function plotFEfunction(G,u) trisurf(G.pt’,G.xp(1,:),G.xp(2,:),u); The resulting graphics (see Figure 2.5 left) can be interactively rotated and scaled.

Writing VTK files More advanced interactive visualization is provided by specialized software packages, some of which are freely available. One of those is Paraview [1] built upon the visualization toolkit VTK. Paraview reads unstructured meshes and finite element functions thereon from XML files. A rather complete description of their structure is given in [44]. For our purposes, a subset of the file format features are sufficient. The following example describes a unit square composed of two triangles, with values at the vertices given by 𝑢(𝑥, 𝑦) = 𝑥 + 𝑦:



2.3 Grid Generation and Visualization

|

19

Fig. 2.5. Solution visualization for −Δ𝑢 = 1 with homogeneous Dirichlet boundary conditions on the grid shown in Figure 2.4 left. Left: Octave graphics for plotting FE functions. Right: VTK graphics from Paraview.

0 0 0 1 0 0 1 1 0 0 1 0



0 1 2 0 2 3

0 3

5 5



20 | 2 Grids and Finite Elements

0 1 2 1



Note that the position of grid vertices (in the data array Points) are always 3D, even if we write 2D grids – just provide a zero 𝑧 coordinate. The data array connectivity contains the 0-based indices of the triangles’ vertices, which are just the numbers found in the t field of our grid data structure. For each cell, the array offsets points to the start of the cell’s vertex indices in the connectivity array. The array types defines the type of the cell, 5 refers to simple planar triangles. And finally, the u array gives the values of the FE function 𝑢 at the vertices. Implementing a function function writeVTK(G,u,fname) that writes a finite element function 𝑢 defined on the vertices of a grid 𝐺 to the file with provided name is left as Exercise 2.4.

2.4 Edges and Boundaries While the triangulation is completely described by the vertex coordinates and trianglevertex incidence, an explicit representation of edges is important in two aspects. The immediately obvious point are boundary conditions, for which the boundary edges need to be accessible. More involved aspect are error estimation with higher order ansatz functions and grid modifications such as adaptive refinement, which will be discussed in Chapters 5 and 6. Therefore, we will number the edges from 1 to 𝑠 and introduce additional mappings from edges to vertices, from triangles to edges, and vice versa in the grid data structure (see Exercise 2.6): The 2 × 𝑠 matrix G.pe contains the sorted indices of the two incident points indexed by 𝑒, the 3 × 𝑚 matrix G.et contains the indices 𝑒 of the three edges of each triangle as columns indexed by 𝑡, and the 2 × 𝑠 matrix G.te contains the indices 𝑡 of the up to two triangles incident to any edge as columns indexed by 𝑒. These mappings can be computed efficiently from the already available information G.pt. An edge corresponds to the set of its end points, conveniently represented by a sorted pair of vertex indices. We can scan all edges by iterating over the triangles and considering the three edges of each. Most edges will be encountered twice, such that we need an efficient way of merging these two occurrences. Maintaining a sorted list of encountered edges has O(𝑁 log 𝑁) complexity. A better algorithm with optimal

2.4 Edges and Boundaries |

21

linear complexity is even the easiest to implement due to the sparse matrix data structures provided by Matlab (see Section 3.5). We use a sparse vertex adjacency matrix 𝐸, where each potential entry 𝐸𝑖𝑗 corresponds to an edge between vertices 𝑖 and 𝑗. Whenever we encounter an edge (𝑖, 𝑗), 𝑖 < 𝑗, of triangle 𝑡, we store the triangle index 𝑡 in the super-diagonal entry 𝐸𝑖𝑗 if it is zero, otherwise in the sub-diagonal entry 𝐸𝑗𝑖 . From that matrix, the mapping G.pe is easily obtained, as is the first of up to two triangles incident to an edge 𝑒: [p1,p2,t1] = find(triu(E)); % take edges only once! G.pe = [p1 p2]’; % G.pe is sorted since p1 < p2 The second triangle cannot be directly extracted from 𝐸 using tril, because boundary edges have only one incident triangle, and the fewer subdiagonal entries would result in a different edge numbering. Therefore we have to step explicitly through all edges and check for their second triangle (if it exists): s = size(G.pe,2); G.te = zeros(2,s); G.te(1,:) = t1’; for e=1:s ps = G.pe(:,e); G.te(2,e) = E(ps(2),ps(1)); % extract subdiagonal entry end While this approach is valid and clear, it is not particularly performant in Matlab, as the interpreter has to execute an instruction for every edge. A vectorized implementation (Exercise 2.8) could be faster, though usually harder to read. For computing boundary conditions, it is most convenient to directly access the boundary edges. Therefore we add another field G.eb ∈ ℕ𝑟 that stores the edge indices of the 𝑟 boundary edges. As with the number of vertices and triangles, extracting the number of edges and boundary edges explicitly is more convenient and expressive. function s = numberOfEdges(G) s = size(G.pe,2); function r = numberOfBoundaryEdges(G) r = length(G.eb);

22 | 2 Grids and Finite Elements

Exercises Exercise 2.1. Write a function function G = rectangularCriss(w,h) that creates a structured cartesian grid G covering the domain 𝛺 = ]0, 𝑤[ × ]0, ℎ[ with 2𝑤ℎ triangles. The grid G shall be a structure with two entries xp and pt of dimension 2 × 𝑛 and 3 × 𝑚, respectively. xp contains the coordinates of the 𝑛 nodes and pt the vertex indices of the 𝑚 triangles. Exercise 2.2. Define a polygonal domain as the shape of the first letter of your name, and create a triangular grid covering this domain. Exercise 2.3. Write a function function G = readPolyMesh(fname) that reads a 2D triangular grid in .poly format as it is written, e.g., by the grid generator Triangle (http://www.cs.cmu.edu/~quake/triangle.html). The file fname.node contains the nodes, the file fname.ele the elements. The grid G shall be a structure with two entries xp and pt of dimension 2 × 𝑛 and 3 × 𝑚, respectively. xp contains the coordinates of the 𝑛 nodes and pt the vertex indices of the 𝑚 triangles. Exercise 2.4. Write a function function writeVTK(G,u,fname) which writes a given grid G and a solution vector u with values at the nodes to a VTK file with given name fname (see http://vtk.org/VTK/img/file-formats.pdf). Exercise 2.5. Let in a triangular grid 𝑛 denote the number of vertices, 𝑚 the number of triangles, and 𝑘 the number of edges. Prove 𝑛 ≤ 3𝑚 < 6𝑛 and 𝑘 ≤ 3𝑚 < 2𝑘. Exercise 2.6. Write a function function G = computeEdges(G) introducing the fields et, te, and pe into the grid data structure G. Each column of the matrix G.et ∈ ℕ3×𝑚 indexed by triangle index 𝑡 contains the edge indices of the edges incident to the triangle. The edges shall be ordered such that the edge G.et(i,t) is opposite the vertex G.pt(i,t). Each column of the matrix G.pe ∈ ℕ2×𝑠 represents an edge, defined by the indices of the two incident vertices. The vertex indices shall be sorted, i.e. pe1𝑒 < pe2𝑒 hods for all 𝑒 = 1, . . . , 𝑠. Each column of the matrix G.te ∈ ℕ2×𝑠 contains the indices of the

Exercises

|

23

two incident triangles. For boundary edges with only one incident triangle, the second triangle index shall be zero. The computational complexity of the function shall be linear in the number of grid vertices. Modify the functions readPolyMesh and rectangularCriss such that it returns a grid with edges already computed. Exercise 2.7. Write a function function G = computeBoundary(G) introducing a field eb into the grid data structure G. This 𝑟-vector shall contain the edge indices of the 𝑟 boundary edges, in no particular order. Modify the function readPolyMesh such that it returns a grid with boundary edges already computed. Exercise 2.8. Find a vectorized computation of G.te.

3 Assembly Assembly is the process of building up the algebraic linear equation system (1.25) of the finite element discretization of

− div(𝜎∇𝑢) + 𝜌𝑢 = 𝑓

in 𝛺 ,

n 𝜎∇𝑢 + 𝛼(𝑢 − 𝑢𝐷 ) = 0

on 𝜕𝛺

T

from contributions of the grid’s triangles. The assembly routine is one of the cornerstones of finite element implementations.

3.1 System Matrices The Galerkin matrix 𝐴 ℎ representing the bilinear form 𝑎 in the linear equation system (1.25) is a sum of three different parts according to (1.12) to be computed separately: the stiffness matrix 𝐾ℎ representing the ∇𝜑𝑖T 𝜎∇𝜑𝑗 term, the mass matrix 𝑀ℎ representing the 𝜌𝜑𝑖 𝜑𝑗 term, and the boundary mass matrix 𝐵ℎ representing the 𝛼𝜑𝑖 𝜑𝑗 term of Robin boundary conditions. The names of stiffness and mass matrix have been coined in solid mechanics, for which finite element methods have been developed first. There, 𝐾ℎ describes the stiffness of the material withstanding deformations, and 𝑀ℎ describes the material’s mass and its inertial effects, see Chapter 8. For obtaining the sparse stiffness matrix 𝐾ℎ , its entries

(𝐾ℎ )𝑖𝑗 = ∫ ∇𝜑𝑖T 𝜎∇𝜑𝑗 d𝑥 𝛺

have to be computed. As we work with piecewise linear ansatz functions, their gradients are constant over each triangle in the grid, which simplifies the evaluation of the integrals. Throughout, we will additionally assume that the diffusion tensor 𝜎 ∈ ℝ2×2 is constant on each triangle. The mass matrix entries are given as

(𝑀ℎ )𝑖𝑗 = ∫ 𝜌𝜑𝑖 𝜑𝑗 d𝑥 , 𝛺

where we will assume a piecewise constant mass density 𝜌. Finally, the boundary mass matrix entries are

(𝐵ℎ )𝑖𝑗 = ∫ 𝛼𝜑𝑖 𝜑𝑗 d𝑠 𝜕𝛺

with piecewise constant 𝛼. While 𝐾ℎ and 𝑀ℎ have the same sparsity structure, 𝐵ℎ is much sparser, as only ansatz functions associated to a boundary vertex can contribute.

3.1 System Matrices |

25

A direct approach for computing these matrices would be to iterate through all occurring combinations of 𝑖, 𝑗-pairs, where the support intersection 𝑆𝑖𝑗 = supp 𝜑𝑖 ∩ supp 𝜑𝑗 has nonempty interior, consider all triangles in 𝑆𝑖𝑗 , and add up the integrals:

(𝐾ℎ )𝑖𝑗 = ∑ |𝑇|∇𝜑𝑖T 𝜎∇𝜑𝑗 𝑇⊂𝑆𝑖𝑗

However, it turns out that a different organization of the loops is more convenient. Iterating over the triangles in the grid, compute the contributions to the entries (𝐾ℎ )𝑖𝑗 for all pairs of the three ansatz functions on this triangle, and add those values to the corresponding entries in 𝐾ℎ . This approach will be discussed in detail in the following sections.

3.1.1 Elemental Stiffness Matrices For a triangle 𝑇 with vertices 𝑖1 , 𝑖2 , 𝑖3 in the grid, the elemental stiffness matrix 𝐾𝑇 ∈ ℝ3×3 is defined by its entries

(𝐾𝑇 )𝑗𝑘 = ∫ ∇𝜑𝑖T𝑗 𝜎𝑇 ∇𝜑𝑖𝑘 d𝑥 = ∫ ∇𝜙𝑗T 𝜉𝑥 𝜎𝑇 𝜉𝑥T ∇𝜙𝑘 | det(𝑥𝑇 )𝜉 | d𝜉 , 𝑇

𝑗, 𝑘 = 1, 2, 3 . (3.1)

𝑇

For notational simplicity, we will omit the subscript 𝑇 denoting the particular triangle under consideration, but keep in mind that both the affine transformation 𝑥(𝜉) and the diffusion tensor 𝜎 depend on 𝑇. Exploiting that the integrand is constant on the reference triangle, we obtain

(𝐾𝑇 )𝑗𝑘 =

1 T ∇𝜙 𝜉 𝜎𝜉T ∇𝜙 | det(𝑥𝑇 )𝜉 | , 2 𝑗 𝑥 𝑥 𝑘

𝑗, 𝑘 = 1, 2, 3 .

The only factors that depend on the actual problem definition, i.e. coefficients and grid, are the symmetric diffusion tensor 𝜎𝑇 = 𝜉𝑥 𝜎𝜉𝑥T ∈ ℝ2×2 and det 𝑥𝜉 . The former one can be decomposed into

1 0 0 1 0 0 0 0 𝜎 = 𝜎11 [ ] +𝜎12 [ ] +𝜎21 [ ] +𝜎22 [ ] . 0 0 0 0 1 0 0 1 ⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟ ⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟ ⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟ ⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟⏟ 𝐸11

𝐸12

𝐸21

𝐸22

Defining the matrices 𝐵𝑟𝑠 ∈ ℝ3×3 for 𝑟, 𝑠 = 1, 2 independently of any actual problem definition as

(𝐵𝑟𝑠 )𝑗𝑘 =

1 T ∇𝜙 𝐸 ∇𝜙 , 2 𝑗 𝑟𝑠 𝑘

the elemental matrices are obtained as the linear combination

𝐾𝑇 = (𝜎11 𝐵11 + 𝜎12 𝐵12 + 𝜎21 𝐵21 + 𝜎22 𝐵22 ) det 𝑥𝜉 .

(3.2)

26 | 3 Assembly

Since 𝜎 is symmetric and hence 𝜎12 = 𝜎21 , we could combine 𝐵12 and 𝐵21 and use their sum only. Later on in Chapter 8, however, it will turn out convenient to consider non-symmetric 𝜎𝑇 as well, thus we stick with the expanded representation (3.2). The matrices 𝐵𝑟𝑠 are easily computed from the shape functions’ gradients (2.2) and the matrix representation

𝐵𝑟𝑠 =

T 1 [∇𝜙1 ∇𝜙2 ∇𝜙3 ] 𝐸𝑟𝑠 [∇𝜙1 ∇𝜙2 ∇𝜙3 ] 2

as

𝐵22 =

1 0 −1 1[ 0 0 0] 2 −1 0 1] [

and so on (see Exercise 3.1).

3.1.2 Scattering The entries of the elemental stiffness matrices are contributions to the global stiffness matrix, and thus have to be added to its appropriate entries. If the triangle 𝑇 has the vertices 𝑖1 , 𝑖2 , 𝑖3 , the entry

(𝐾𝑇 )𝑗𝑘 = ∫ ∇𝜑𝑖T𝑗 𝜎𝑇 ∇𝜑𝑖𝑘 d𝑥 𝑇

belongs to the entry (𝐾ℎ )𝑖𝑗 𝑖𝑘 and must be added to it. Formally, this can be conveniently written in terms of a scattering matrix 𝑃𝑇 ∈ ℝ𝑛×3 with just three nonzero entries at positions (𝑖1 , 1), (𝑖2 , 2), (𝑖3 , 3) with value 1. Then we can write the addition as update

𝐾ℎ ← 𝐾ℎ + 𝑃𝑇 𝐾𝑇 𝑃𝑇T . While this operation can, in principle, be used for implementing the scattering operation in Matlab, a much more concise and efficient way is to use the subscript operator: idx = [i1 i2 i3]; K(idx,idx) = K(idx,idx) + KT; The indices 𝑖𝑗 are readily extracted from the G.pt field of the grid data structure. Put everything together, the following assembly algorithm computes the stiffness matrix: Algorithm 3.1. initialize global stiffness matrix 𝐾ℎ ← 0 for all triangles 𝑇 ∈ T obtain vertex indices 𝑖1 , 𝑖2 , 𝑖3

3.1 System Matrices |

i

i

k

i

k

l

j

27

p

k

j

l p

j

l

p

Fig. 3.1. Assembly process. Two triangles sharing a common vertex 𝑘 lead to two elemental matrices sharing the associated global degree of freedom. The corresponding entries are scattered into the same entry of the global matrix.

obtain affine mapping derivative 𝑥𝜉 and its inverse 𝜉𝑥 = (𝑥𝜉 )−1 compute transformed diffusion tensor 𝜎 = 𝜉𝑥 𝜎𝜉𝑥T build up elemental stiffness matrix

𝐾𝑇 = (𝜎11 𝐵11 + 𝜎12 𝐵12 + 𝜎21 𝐵21 + 𝜎22 𝐵22 ) det 𝑥𝜉

scatter 𝐾ℎ ← 𝐾ℎ + 𝑃𝑇 𝐾𝑇 𝑃𝑇T

The actual implementation is left as Exercise 3.2.

3.1.3 Elemental Mass Matrices The mass matrix can be computed essentially in the same way as the stiffness matrix, but with different, even simpler, elemental matrices

(𝑀𝑇 )𝑗𝑘 = ∫ 𝜌𝜑𝑖𝑗 𝜑𝑖𝑘 d𝑥 = ∫ 𝜌𝜙𝑗 𝜙𝑘 det 𝑥𝜉 d𝜉 , 𝑇

𝑗, 𝑘 = 1, . . . , 3 .

𝑇

With piecewise constant mass density 𝜌, this simplifies to

(𝑀𝑇 )𝑗𝑘 = 𝜌 det 𝑥𝜉 ∫ 𝜙𝑗 𝜙𝑘 d𝜉 , 𝑇

where only the scalar factor 𝜌 det 𝑥𝜉 depends on the actual problem. The remaining problem-independent matrix factor 𝑀 can be computed analytically in advance, as the four factors 𝐵𝑟𝑠 before:

𝑀=

2 1 1 1 [ 1 2 1] . 24 [1 1 2 ]

28 | 3 Assembly

Now the elemental mass matrix is easily computed as

𝑀𝑇 = 𝜌 det 𝑥𝜉 𝑀 . The scattering into the global mass matrix 𝑀ℎ is performed exactly as for the stiffness matrix.

3.2 Boundary Conditions The three different types of boundary conditions, Dirichlet, Neumann, and Robin, require a slightly different treatment. We start with the most general Robin boundary conditions and cover the two other types in the context established for Robin conditions.

3.2.1 Robin Boundary Conditions Boundary conditions of Robin type as considered here contribute to both the system matrix and the right hand side. Here we will discuss the computation of the matrix part and postpone the right hand side part to the subsequent Section 3.3. The matrix contribution of the Robin boundary condition is readily inferred from the weak formulation (1.12) as

𝐵𝑖𝑗 = ∫ 𝛼𝜑𝑖 𝜑𝑗 d𝑠 𝜕𝛺

and has the same structure as the mass matrix, except that the integration is performed over the boundary 𝜕𝛺 instead of the domain 𝛺. We define the elemental boundary mass matrix 𝐵𝑆 ∈ ℝ2×2 on a boundary edge 𝑆 by 1

(𝐵𝑆 )𝑗𝑘 = ∫ 𝛼𝜑𝑖𝑗 𝜑𝑖𝑘 d𝑠 = |𝑆| ∫ 𝛼𝜙𝑖𝑗 𝜙𝑖𝑘 d𝜂 , 0

𝑆

where |𝑆| = |𝑥𝑖𝑗 − 𝑥𝑖𝑘 | is the length of the boundary edge. Note that we have implicitly introduced the “reference edge” [0, 1] and the shape functions 𝜙1 (𝜂) = 1 − 𝜂 and 𝜙2 (𝜂) = 𝜂 thereon. The length |𝑆| of the edge is just the determinant of the coordinate transformation. Assuming a piecewise constant transmission coefficient 𝛼, the elemental boundary mass matrix can, as before, be separated into a scalar problem-dependent factor 𝛼|𝑆| and the problem-independent matrix factor

𝐵=

1 2 1 [ ] . 6 1 2

3.2 Boundary Conditions

| 29

Scattering is performed as for the elemental mass and stiffness matrices before, with the only difference that the elemental boundary mass matrices are of size 2 × 2 as an edge is incident to only two vertices. Note that 𝐵 has the same size 𝑛 × 𝑛 as mass and stiffness matrices, but is much sparser – in particular, most rows and columns vanish.

3.2.2 Dirichlet Boundary Conditions There are different ways to treat Dirichlet boundary conditions, which we will illustrate at the Poisson problem −Δ𝑢 = 0, 𝜕𝛺𝐷 = 𝜕𝛺, and 𝑢𝐷 = 1 on 𝛺 = ]0, 1[2 discretized symmetrically by four triangles. Stiffness matrix and right hand side are

1 0 0 0 [0 1 0 0 [ 0 0 1 0 𝐾ℎ = [ [ [0 0 0 1 [−1 −1 −1 −1

−1 −1] ] −1] ] −1]

0 [0] [ ] ] and 𝑓ℎ = [ [0] . [0]

4]

[0]

The symmetry of the domain is visible in 𝐾ℎ : the interior vertex has number 5. The first option is to enforce the boundary conditions explicitly by discretizing the restricted space 𝐻01 (𝛺) = {𝑢 ∈ 𝐻1 (𝛺) | 𝑢|𝜕𝛺𝐷 = 0}:

min 𝐽(𝑢) .

𝑢∈𝐻01 (𝛺)

This is achieved by removing the degrees of freedom from the vertices contained in 𝜕𝛺𝐷 and dropping the corresponding rows from the Galerkin system. The corresponding columns are multiplied by the explicitly given Dirichlet values, and can thus be moved to the right hand side. This leads to a reduced system with 𝐴̂ ℎ ∈ ℝ𝑛×̂ 𝑛̂ of dimension 𝑛̂ < 𝑛, here 1 and 5. For the illustrative example, the reduced “system” is

[4] 𝑢̂ = [4] . An equivalent variant is to modify the Galerkin matrix entries while retaining its size and structure. The rows corresponding to the Dirichlet boundary nodes are set to zero, ones are inserted for their diagonal entries, and the Dirichlet boundary values 𝑢𝐷 are written into the corresponding right hand side entries. While this yields the correct solution, it leads to a non-symmetric matrix. Fortunately, the fact that the Dirichlet values are given explicitly allows to move the affected columns to the right hand side, and we obtain the modified system

1 [0 [ [0 [ [0 [0

0 1 0 0 0

0 0 1 0 0

0 0 0 1 0

0 1 ] ] [ 0] [1] ] [ 0 ] 𝑢ℎ = [1] ] . [1] 0] 4] [4]

30 | 3 Assembly

After a suitable renumbering of the degrees of freedom, the modified Galerkin matrix is block-diagonal with one block corresponding to the Dirichlet nodes forming the identity matrix and the other block of size 𝑛̂ × 𝑛̂ containing the reduced Galerkin matrix. The other options are based on the interpretation of Dirichlet boundary conditions as equality constraints, and treating them with methods from constrained optimization: min 𝐽(𝑢) subject to 𝑢|𝜕𝛺𝐷 = 𝑢 . 𝑢∈𝐻1 (𝛺)

The simplest treatment of equality constraints is to include them by a penalty term 𝛼 > 0 into the variational functional, i.e.

min 𝐽(𝑢) +

𝑢∈𝐻1 (𝛺)

𝛼 ∫ (𝑢 − 𝑢𝐷 )2 d𝑠 . 2

(3.3)

𝜕𝛺𝐷

For large 𝛼, deviations from 𝑢𝐷 are penalized much stronger, such that in the limit case 𝛼 → ∞ we expect 𝑢|𝜕𝛺𝐷 → 𝑢𝐷 . In (3.3) we recognize the variational functional (1.14) corresponding to the Robin boundary condition nT 𝜎∇𝑢 = 𝛼(𝑢𝐷 −𝑢), an insight dating back to Courant [23]. These Robin boundary conditions can be treated as discussed in the previous section. For 𝛼 = 600 we obtain the symmetric penalized system

401 100 0 100 −1 600 [100 401 100 0 −1] [600] ] [ [ ] [ 0 100 401 100 −1] 𝑢ℎ = [600] . ] [ [ ] [100 0 100 401 −1] [600] [ −1 −1 −1 −1 4 ] [ 0 ] The only question is how to select an appropriate value of 𝛼 for approaching the limit case 𝛼 → ∞. Choosing 𝛼 very large should improve the approximation of the boundary condition, but increases the condition number of 𝐴 ℎ and can ultimately lead to numerical difficulties. For linear finite elements and 𝐻2 -regular problems, nearly optimal convergence rates of

‖𝑢 − 𝑢ℎ ‖𝑎 ≤ 𝑐(𝜖)ℎ1−𝜖 ‖𝑢‖𝐻2 (𝛺) ,

‖𝑢 − 𝑢ℎ ‖𝐿2 (𝛺) ≤ 𝑐(𝜖)ℎ2−𝜖 ‖𝑢‖𝐻2 (𝛺)

are obtained for all 𝜖 > 0 by the choice 𝛼 ≥ ℎ−2 [7]. A more sophisticated approach to equality constrained optimization problems is to introduce Lagrange multipliers. Under reasonable assumptions, a necessary condition for optimality of 𝑢 is the existence of a multiplier 𝜆 such that the Lagrangian

𝐽(𝑢) + ∫ 𝜆(𝑢 − 𝑢𝐷 ) d𝑠

(3.4)

𝜕𝛺𝐷

is stationary. By choosing appropriate discrete spaces for 𝜆, optimal convergence can be shown at the cost of having to solve saddle point problems instead of convex minimization problems as before [6].

3.3 Right Hand Sides

| 31

Combining the penalty and the multiplier approach leads to the augmented Lagrangian formulation

̂ − 𝑢 ) d𝑠 + 𝛼 ∫ (𝑢 − 𝑢 )2 d𝑠 , min 𝐽(𝑢) + ∫ 𝜆(𝑢 𝐷 𝐷 2

𝑢∈𝐻1 (𝛺)

𝜕𝛺𝐷

(3.5)

𝜕𝛺𝐷

which, provided with a sufficiently good approximation 𝜆̂ of the true multiplier 𝜆, gives similar results as the penalty method already for much smaller values of 𝛼, see [54]. Comparing the derivatives of (3.3) and (3.4) suggests that 𝜆̂ = 𝛼(𝑢 − 𝑢𝐷 ) = −nT 𝜎∇𝑢 might be a good approximation of 𝜆. Inserting this directly into (3.5) yields the Dirichlet boundary treatment proposed by Nitsche [53]. The variational functional to be minimized is now

̂ = 𝐽(𝑢) + ∫ (nT 𝜎∇𝑢(𝑢𝐷 − 𝑢) + 𝛼 (𝑢 − 𝑢𝐷 )2 ) d𝑠 , 𝐽(𝑢) 2

(3.6)

𝜕𝛺𝐷

which eliminates the need to define spaces for the multiplier 𝜆. For sufficiently large 𝛼 > 0, 𝐽 ̂ is convex, such that no saddle point problems have to be solved.

3.3 Right Hand Sides The entries of the right hand side vector 𝑏 of the Poisson problem (1.6), (1.9) are given by the integrals

𝑏𝑖 = ∫ 𝜑𝑖 𝑓 d𝑥 + ∫ 𝛼𝜑𝑖 𝑢𝐷 d𝑠 . 𝛺

(3.7)

𝜕𝛺

In principle, we could proceed as before, computing elemental right hand side vectors and scattering them into the global right hand side. A simpler implementation is possible if we employ the domain and boundary mass matrices, the computation of which is already available. If 𝑓 can be represented as a finite element function, i.e. 𝑛

𝑓 = ∑ 𝑓𝑗 𝜑𝑗 , 𝑗=1

the first term in (3.7) reads 𝑛

𝑛

∫ 𝜑𝑖 𝑓 d𝑥 = ∑ ∫ 𝜑𝑖 𝜑𝑗 d𝑥𝑓𝑗 = ∑ 𝑀𝑖𝑗 𝑓𝑗 , 𝛺

𝑗=1

𝛺

𝑗=1

where 𝑀 is the mass matrix for the constant mass density 𝜌 = 1. Similarly, if 𝑢𝐷 can be represented as a finite element function defined on the whole grid and taking the

32 | 3 Assembly

values of 𝑢𝐷 at the boundary, 𝑛

𝑢𝐷 = ∑(𝑢𝐷 )𝑗 𝜑𝑗 , 𝑗=1

the second term can be written as 𝑛

𝑛

∫ 𝛼𝜑𝑖 𝑢𝐷 d𝑠 = ∑ ∫ 𝛼𝜑𝑖 𝜑𝑗 d𝑠(𝑢𝐷 )𝑗 = ∑ 𝐵𝑖𝑗 (𝑢𝐷 )𝑗 = (𝐵𝑢𝐷 )𝑖 , 𝜕𝛺

𝑗=1

𝑗=1

𝜕𝛺

where 𝐵 is the boundary mass matrix for the transmission coefficient 𝛼. The complete right hand side is then just 𝑏 = 𝑀𝑓+𝐵𝑢𝐷 . Having mass and boundary mass matrix already implemented, the evaluation of right hand sides in this way is straightforward. Source terms 𝑓 not contained in the finite element ansatz space cannot be treated with the same simplicity. Interpolating them by finite element functions, however, yields approximate results with an error that is usually in the order of the finite element discretization error. In favor of a simpler implementation, we will accept this restriction here.

3.4 Examples Now let us combine all the building blocks to solve a couple of PDE problems of the form − div(𝜎∇𝑢) + 𝜌𝑢 = 𝑓 in 𝛺

nT 𝜎∇𝑢 + 𝛼(𝑢 − 𝑢𝐷 ) = 0

on 𝜕𝛺 .

The core will in any case be the assembly and solution of the Galerkin system, which is performed by the following code fragment. K = stiffnessMatrix(G,sigmas); M = massMatrix(G,rhos); B = boundaryMassMatrix(G,alphas); u = (K+M+B) \ (M*f + B*ud); writeVTK(G,u,’solution.vtu’); The examples below differ mostly in the setup of the grid and the coefficients 𝜎, 𝜌, 𝛼, and 𝑢𝐷 , and in the interpretation of the computed solution 𝑢ℎ .

Poisson equation The ubiquitous first example is to solve the Poisson equation

− Δ𝑢 = 1 in 𝛺 ,

𝑢 = 0 on 𝜕𝛺

(3.8)

3.4 Examples | 33

on the unit square 𝛺 = ]0, 1[2 . First a reasonable grid is to be generated. The following command creates unitSquare.1.node and unitSquare.1.ele files with well-shaped triangles of area at most 0.005. $ triangle -q -a0.005 unitSquare.poly Now the grid can be read into the Matlab grid data structure. We assume that the computation of edges and boundary edges has already been introduced into readPolyMesh, see Exercises 2.6 and 2.7. G m n r

= = = =

readPolyMesh(’unitSquare.1’); numberOfTriangles(G); numberOfVertices(G); numberOfBoundaryEdges(G);

For the right hand side vector 𝑏 = 𝑀𝑓+𝐵𝑢𝐷 we need the mass matrix 𝑀. 𝑢𝐷 vanishes in this example. rhos = ones(m,1); M = massMatrix(G,rhos); b = M * ones(n,1);

% mass matrix with rho 1 % needed for right hand side

Stiffness matrix and boundary mass matrix are required as well. sigmas = zeros(2,2,m); % stiffness matrix for t=1:m sigmas(:,:,t) = eye(2); end K = stiffnessMatrix(G,sigmas); alphas = 1e9 * ones(r,1); % Dirichlet boundary condition B = boundaryMassMatrix(G,alphas); The last steps are then to set up the Galerkin system, to solve it, and to write the solution. u = (K+B) \ b; % solve writeVTK(G,u,’solution.vtu’); The solution produced by this code is shown in Figure 3.2 left.

Porous medium flow The stationary flow of a viscous fluid through a porous medium, e.g., ground water flowing through soil, is described by Darcy’s law, see [30, Chapter 2]. It states that the

34 | 3 Assembly q=0

σ=1 Ωi

Ωi

1 0

q=

σ = 10 –3

Ω q=0 Fig. 3.2. Left: Finite element solution of the Poisson equation (3.8) on a rather coarse grid with 177 vertices. Right: Domain, flux boundary conditions, and permeability for Darcy’s equation.

flux 𝑞 is proportional to pressure gradient and gravitational force,

𝜅 𝑞 = − (∇𝑝 − 𝜌𝑔) , 𝜂

(3.9)

where 𝜅 denotes the permeability of the medium, 𝜂 the viscosity of the fluid, 𝑝 the pressure, 𝜌 the fluid’s density, and 𝑔 the vectorial gravity, which we neglect here. Incompressibility of the fluid and the lack of sources or sinks in the domain imply div 𝑞 = 0, such that we obtain

− div(𝜎∇𝑝 − 𝜌𝑔) = 0



− div(𝜎∇𝑝) = 0 .

We consider flow through a horizontal channel 𝛺 = ]0, 5[ × ]0, 4[ with almost impermeable inclusions 𝛺𝑖 = [1, 2] × [1, 2] ∪ [3, 4] × [2, 3], where we set 𝜎 = 1 in 𝛺\𝛺𝑖 and 𝜎 = 10−3 in 𝛺𝑖 , see Figure 3.2 right. As boundary conditions we prescribe a constant flux 𝑞 = [1 0]T on the left channel side, no flux across the top and bottom sides, and hydrostatic pressure 𝑝 = 4 − 𝑥2 at the right channel side. The boundary conditions for 𝑝 are therefore

nT 𝜎∇𝑝 = 𝜎𝜌nT 𝑔 ,

𝑥2 ∈ {0, 4}

nT 𝜎∇𝑝 = 1 ,

𝑥1 = 0

𝑝 = 4 − 𝑥2 ,

𝑥1 = 5 .

The code to solve this problem differs from the previous example mainly by setting up the discontinuous permeability coefficients and the boundary conditions. We take care that the discontinuities of the permeability are represented in the grid. Then the coefficients are constant on each triangle, which was the general assumption for assembly. As there is no source term, no mass matrix is needed. For defining the permeability, we check whether a triangle is inside the inclusion area 𝛺1 , or not, and assign either 10−3 or 1.

3.4 Examples | 35

sigmas = zeros(2,2,m); for t=1:m x = sum(G.xp(:,G.pt(:,t)),2)/3; x1 = x(1); x2 = x(2); if 1