ColoState MATH451 Notes on Finite Element Method CGP1 for Solving 2-dim Poisson Boundary Value Problems Jiangguo (James) Liu ∗ April 25, 2016 This chapter presents mainly CGP1 for Poisson BVP: the continuous Galerkin (CG) finite element method (FEM) with linear polynomial shape functions (P1 ) on triangular meshes for 2-dim Poisson equation with a homogeneous Dirichlet boundary condition, which is usually formulated as { −∆u = −∇2 u = f in Ω, (1) u=0 on ∂Ω. See (Kincaid,Cheney, Brooks/Cole, 2002) Section 9.4, especially p.638 bottom, (15) p.639 top, (17), (18) p.640 top, (21), (22), bottom p.641 (23), bottom 1 Variational Formulation The exact solution needs to satisfy both the PDE and the boundary condition. We tackle this difficult problem by isolating the homogeneous boundary condition. That is, we first look at all functions that satisfy the homogeneous Dirichlet boundary condition. Obviously, there are infinitely many such functions and they actually form a linear space. The exact solution is just one such function from this linear space that happens to satisfy the PDE also. We denote by V = {functions that are sufficiently smooth on Ω}. (2) V 0 = {functions that are sufficiently smooth on Ω and vanish on ∂Ω}. (3) Here ”sufficiently smooth” means some conditions on a functions u(x, y) itself and its first order partial derivatives ux (x, y), uy (x, y). Clearly, V 0 ( V , that is, V 0 is a subspace of V . Integration by parts, a powerful tool (methodology) in mathematics, will be used to establish a variational form. Here is one version (or Green’s formula): ∫ ∫ ∫ (−∆u) v dxdy = (−∇u) · n v ds + ∇u · ∇v dxdy, Ω ∂Ω Ω where n is the unit outward normal vector on ∂Ω. ∗ Department of Mathematics, (liu@math.colostate.edu) Colorado State University, 1 Fort Collins, CO 80523-1874, USA Variational Form: Seek u ∈ V 0 such that for any v ∈ V 0 , there holds ∫ ∫ ∇u · ∇v dxdy = f vdxdy. Ω (4) Ω Note that V 0 is an infinite dimensional space, in which it is very difficult, if not impossible, to perform computation. So finite dimensional subspaces are needed and hence constructed to carry out approximation and computation. Intuitively, (piecewise) polynomials are suitable candidates. 2 FEM CGP1 Here are some key parts: (1) Discretizing the domain: Generating a mesh Th ; (2) Discretizing the space V or V 0 : Constructing a finite dimensional subspace Vh0 of V 0 ; (3) Discretizing the LHS (bilinear form): Generating a large sparse matrix ; (4) Discretizing the RHS (linear form): Generating a large size vector ; (5) Solving the large sparse linear system;; (6) Further processing of the approximate solution. Here are some details. • Discretization of Ω: Generate a triangular mesh Th . There are totally (n + 1)2 nodes for a uniform triangular mesh. • Construction of finite dimensional subspaces Vh ( V and Vh0 ( V 0 : Vh = {continuous piecewise linear polynomials on Th }, Vh0 = {functions in Vh that vanish on ∂Ω}. (5) A basis function for Vh or Vh0 is like a tent. There are totally (n + 1)2 global basis functions for Vh . There are totally n2 global basis functions for Vh0 . • A global basis basis function for Vh can be viewed as glue-up of local basis functions. • There are three Lagrange P1 basis functions on each triangle. They are actually the barycentric coordinates. They can expressed as the ratios of the determinants representing the areas of the small and ”big” triangles. • Gradients of these local P1 basis functions are ... • Note that ∫ ∇uh · ∇v dxdy = Ω ∑ ∫ T ∈Th 2 T ∇uh · ∇v dxdy • Note that ∫ ∑ ∫ f v dxdy == Ω T ∈Th ∫ Apply a Gaussian type quadrature to f v dxdy T f vdxdy: 1-point, 3-point, etc. T • Assembly of the LHS or the global stiffness matrix: • Assembly of the RHS vector: • The large-size sparse linear system can be solved easily by Matlab backslash. 3 Matlab Implementation of CGP1 for 2-dim Poisson BVP For theoretical discussion of the finite element scheme CGP1, we use the finite element space Vh0 . But for implementation, we actually work on the larger subspace Vh . Here are some major implementation issues. • Mesh preparation; • Computation of element stiffness matrix for Lagrange P1 basis functions; • Computation of element right-hand side for Lagrange P1 basis functions; • Assembly of global stiffness matrix; • Assembly of global right-hand side; • Enforcement of Dirichlet boundary conditions; • Computing the errors if the exact solution is known. NumNds NumEms node elem IsDirichletNd Table 1: Mesh info Number of nodes Number of (triangular) elements An array of size NumNds*2 for the x, y-coordinates of the nodes An array of size NumEms*3 for the labels of the three vertices of all elements. By default, the 3 vertices form a counterclockwise orientation. An array (vector) of size NumNds taking value 1 or 0 to indicate that a node is on the Dirichlet boundary 3 Nodes: bold black; Elements: blue box; Edges: red 3 6 9 4 8 3 11 5 8 2 11 6 1 1 12 7 2 12 10 5 9 4 7 10 Figure 1: A 3 × 2 triangular mesh with labels for nodes and elements. >> node node = 0 0 0 0.3333 0.3333 0.3333 0.6667 0.6667 0.6667 1.0000 1.0000 1.0000 0 0.5000 1.0000 0 0.5000 1.0000 0 0.5000 1.0000 0 0.5000 1.0000 >> [(1:NumEms)’, elem] ans = 1 1 4 2 2 5 2 4 3 2 5 3 4 6 3 5 5 4 7 5 6 8 5 7 7 5 8 6 8 9 6 8 9 7 10 8 10 11 8 10 11 8 11 9 12 12 9 11 4 Bary(centric) coordinates are enjoyed by practitioners of FEMs. Let T = ∆P1 P2 P3 be a triangle oriented counterclockwise and |T | be its area. For any point P (x, y) in the triangle, let |Ti |(i = 1, 2, 3) be the areas of the small triangles obtained when Pi (xi , yi ) are respectively replaced by P . It is clear that 1 x1 y1 1 x y 1 1 |T | = 1 x2 y2 , |T1 | = 1 x2 y2 , 2 2 1 x3 y3 1 x 3 y3 and similarly for |T2 | and |T3 |. Then λi = |Ti |/|T |, i = 1, 2, 3 are the barycentric coordinates. Clearly, 0 ≤ λi ≤ 1 and λ1 + λ2 + λ3 = 1. Figure 2: Barycentric coordinates λi (i = 1, 2, 3) for a generic triangle. It is clear that λi (x, y)(i = 1, 2, 3) are also the Lagrangian P1 basis functions ϕ( x, y)(i = 1, 2, 3), whose gradients are ∇ϕ1 = ∇λ1 = ⟨y2 − y3 , x3 − x2 ⟩ /(2|T |), ∇ϕ2 = ∇λ2 = ⟨y3 − y1 , x1 − x3 ⟩ /(2|T |), (6) ∇ϕ3 = ∇λ3 = ⟨y1 − y2 , x2 − x1 ⟩ /(2|T |). These can also be expressed using two skew-symmetric matrices ] 0 1 −1 ∇ϕ1 x 1 y1 [ 1 0 −1 −1 ∇ϕ2 = 0 1 x 2 y2 . 1 0 2|T | 1 −1 0 ∇ϕ3 x 3 y3 5 (7) The element stiffness matrix for a typical triangular element T is the following 3 × 3 matrix ∫ ∫ ∫ T ∇ϕ1 · ∇ϕ1 dxdy T ∇ϕ1 · ∇ϕ2 dxdy T ∇ϕ1 · ∇ϕ3 dxdy [∫ ] ∫ ∫ ∫ ∇ϕi · ∇ϕj dxdy = T ∇ϕ2 · ∇ϕ2 dxdy T ∇ϕ2 · ∇ϕ3 dxdy T ∇ϕ2 · ∇ϕ1 dxdy T ∫ ∫ ∫ T ∇ϕ3 · ∇ϕ1 dxdy T ∇ϕ3 · ∇ϕ2 dxdy T ∇ϕ3 · ∇ϕ3 dxdy The global stiffness matrix is a sparse (square) matrix whose size is the number of all nodes (or all interior nodes). The sparsity pattern is illustrated in the following figure. 0 2 4 6 8 10 12 0 2 4 6 nz = 46 8 10 12 Figure 3: Sparsity of the global stiffness matrix for CGP1 on a 3×2 triangular mesh for the Poisson equation. Nodes: bold black; Elements: blue box; Edges: red 3 6 9 4 8 3 11 5 8 2 11 6 1 1 12 7 2 12 10 5 9 4 7 10 Figure 4: A 3 × 2 triangular mesh with labels for nodes and elements. 6 Evaluating the Elementwise Right-hand Side Vector. This task relies on a Gaussian type quadrature for triangles, which usually takes the form (area times the average height) ∫ F (x, y) dxdy ≈ |T | T K ∑ F (αk P1 + βk P2 + γk P3 )wk , k=1 where |T | is the area of the triangle, K is the number of quadrature points, (αk , βk , γk ), k = 1, 2, . . . , K are the barycenteric coordinates, wk is the weight. It is known that (in general) wk ≥ 0, k ∑ wk = 1. k=1 Here are two simple cases. --------------------------------------K \alpha_k \beta_k \gamma_k w_k --------------------------------------1 1/3 1/3 1/3 1 --------------------------------------3 4/6 1/6 1/6 1/3 1/6 4/6 1/6 1/3 1/6 1/6 4/6 1/3 ---------------------------------------Now our task is actually to evaluate ∫ f (x, y)ϕi (x, y) dxdy Ω and ϕi (x, y) is actually the i-th barycentric coordinates or α, β, γ depending on i = 1, 2, 3. So we shall have ∫ ∫ f (x, y)λ1 dxdy ≈ |T | f (x, y)ϕ1 (x, y) dxdy = T Similarly for T K ∑ f (αk P1 + βk P2 + γk P3 ) αk wk . k=1 ∫ ∫ f (x, y)ϕ2 (x, y) dxdy, f (x, y)ϕ3 (x, y) dxdy. T T 7