Docsity

Variational formulation based programming for finite element methods in Matlab, Study notes of Mathematical Methods for Numerical Analysis and Optimization

Toggle sidebar
bg1
varFEM Documentation
Variational formulation based programming for finite
element methods in Matlab
Yue Yu∗
School of Mathematical Sciences, Institute of Natural Sciences, MOE-LSC, Shanghai Jiao
Tong University, Shanghai, 200240, P. R. China.
Abstract
This is the documentation for the FEM package - varFEM, with a variety of examples
presented to demonstrate the use of the software package. varFEM provides a simple way to
realize the programming style in FreeFEM and makes extensive use of the numerical integration
and assembly techniques in iFEM.
1 Introduction
FreeFEM is a popular 2D and 3D partial differential equations (PDE) solver based on finite
element methods (FEMs) [2], which has been used by thousands of researchers across the world.
The highlight is that the programming language is consistent with the variational formulation of
the underlying PDEs, referred to as the variational formulation based programming in this article.
We intend to develop an FEM package in a similar way of FreeFEM using the language of Matlab,
named varFEM. The similarity here only refers to the programming style of the main or test script,
not to the internal architecture of the software.
This programming paradigm is usually organized in an object-oriented language, which makes
it difficult for readers or users to understand and modify the code, and further redevelop the
package (although it is a good way to develop softwares). Upon rethinking the process of finite
element programming, it becomes clear that the assembly of the stiffness matrix and load vector
essentially reduces to the numerical integration of some typical bilinear and linear forms with
respect to basis functions. In this regard, the package iFEM, written in Matlab, has provided
robust, efficient, and easy-following codes for many mathematical and physical problems in both
two and three dimensions [1]. On this basis, we successfully developed the variational formulation
based programming for the conforming Pk-Lagrange (k≤3) FEMs in two and three dimensions
by utilizing the Matlab language. The underlying idea can be generalized to other types of finite
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f

Partial preview of the text

Download Variational formulation based programming for finite element methods in Matlab and more Study notes Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

varFEM Documentation

Variational formulation based programming for finite

element methods in Matlab

Yue Yu∗

School of Mathematical Sciences, Institute of Natural Sciences, MOE-LSC, Shanghai Jiao

Tong University, Shanghai, 200240, P. R. China.

Abstract This is the documentation for the FEM package - varFEM, with a variety of examples presented to demonstrate the use of the software package. varFEM provides a simple way to realize the programming style in FreeFEM and makes extensive use of the numerical integration and assembly techniques in iFEM.

1 Introduction

FreeFEM is a popular 2D and 3D partial differential equations (PDE) solver based on finite element methods (FEMs) [2], which has been used by thousands of researchers across the world. The highlight is that the programming language is consistent with the variational formulation of the underlying PDEs, referred to as the variational formulation based programming in this article. We intend to develop an FEM package in a similar way of FreeFEM using the language of Matlab, named varFEM. The similarity here only refers to the programming style of the main or test script, not to the internal architecture of the software. This programming paradigm is usually organized in an object-oriented language, which makes it difficult for readers or users to understand and modify the code, and further redevelop the package (although it is a good way to develop softwares). Upon rethinking the process of finite element programming, it becomes clear that the assembly of the stiffness matrix and load vector essentially reduces to the numerical integration of some typical bilinear and linear forms with respect to basis functions. In this regard, the package iFEM, written in Matlab, has provided robust, efficient, and easy-following codes for many mathematical and physical problems in both two and three dimensions [1]. On this basis, we successfully developed the variational formulation based programming for the conforming Pk-Lagrange (k ≤ 3) FEMs in two and three dimensions by utilizing the Matlab language. The underlying idea can be generalized to other types of finite ∗[email protected]

elements for both two- and three-dimensional problems on unstructured simplicial meshes. The package is accessible on https://github.com/Terenceyuyue/varFEM (see the varFEM folder). The article is organized as follows. In Section 2, we introduce the basic idea in varFEM through a model problem. In Section 3, we demonstrate the use of varFEM for several typical examples, including a complete implementation of the model problem, the vector finite element for the linear elasticity, the mixed FEMs for bihamonic equation and Stokes problem, the iterative scheme for the heat equation and the eigenvalue problem for the Poisson equation. We also demonstrate the ability of varFEM to solve complex problems and three-dimensional problems in Sections 4 and 5, respectively.

2 Variational formulation based programming in varFEM

We introduce the variational formulation based programming in varFEM via a model problem, so as to facilitate the underlying design idea.

2.1 Programming for a model problem

Let Ω = (0, 1)^2 and consider the second-order elliptic problem:     

−∇ · (a∇u) + cu = f in Ω, u = gD on ΓD, gRu + a∂nu = gN on ΓR,

where ΓD and ΓR = ∂Ω\ΓD are the Dirichlet boundary and Robin boundary, respectively. For brevity, we refer to gR as the Robin boundary data function and gN as the Neumann boundary data function. For homogenous Dirichlet boundary condition, the variational problem is to find u ∈ V := H 01 (Ω) such that a(v, u) = `(v), v ∈ V,

where a(v, u) =

Ω

a∇v · ∇udσ +

Ω

cvudσ +

ΓR

gRvuds,

`(v) =

Ω

f vdσ +

ΓR

gN vds.

Here, the test function is placed in the first entry of a(·, ·) since

a(v, u) = vT^ Au, A = (a(ϕi, φj ))m×n,

where v = (v 1 , · · · , vm)T^ and u = (u 1 , · · · , un)T^ , with

v =

∑^ m i=

viϕi, u =

∑^ n i=

uiφi.

2 Coef = pde. f ; Test = 'v. val '; 3 ff = int2d ( Th , Coef , Test ,[] , Vh , quadOrder ) ;

We set Trial = [] to indicate the linear form. The computation of the load vector associated with the Neumann boundary data function gN , i.e., (^) ∫

ΓR gN vds

reads 1 % Gamma_R 2 Coef = g_N ; Test = 'v. val '; 3 ff = ff + int1d ( Th , Coef , Test ,[] , Vh , quadOrder ) ;

2.2 Data structures for triangular meshes

We adopt the data structures given in iFEM [1]. All related data are stored in the Matlab structure Th, which is computed by using the subroutine FeMesh2d.m as 1 Th = FeMesh2d ( node , elem , bdStr ) ; The triangular meshes are represented by two basic data structures node and elem, where node is an N × 2 matrix with the first and second columns contain x- and y-coordinates of the nodes in the mesh, and elem is an NT × 3 matrix recording the vertex indices of each element in a counterclockwise order, where N and NT are the numbers of the vertices and triangular elements. In the current version, we only consider the Pk-Lagrange finite element spaces with k up to

  1. In this case, there are two important data structures edge and elem2edge. In the matrix edge(1:NE,1:2), the first and second rows contain indices of the starting and ending points. The column is sorted in the way that for the k-th edge, edge(k,1)<edge(k,2) for k = 1, 2 , · · · , NE. The matrix elem2edge establishes the map of local index of edges in each triangle to its global index in matrix edge. By convention, we label three edges of a triangle such that the i-th edge is opposite to the i-th vertex. We refer the reader to https://www.math.uci.edu/~chenlong/ ifemdoc/mesh/auxstructuredoc.html for some detailed information. To deal with boundary integrals, we first exact the boundary edges from edge and store them in matrix bdEdge. In the input of FeMesh2d, the string bdStr is used to indicate the interested boundary part in bdEdge. For example, for the unit square Ω = (0, 1)^2 ,
    • bdStr = 'x==1' divides bdEdge into two parts: bdEdgeType{ 1 } gives the boundary edges on x = 1, and bdEdgeType{ 2 } stores the remaining part.
    • bdStr = {'x==1','y==0'} separates the boundary data bdEdge into three parts: bdEdgeType{ 1 } and bdEdgeType{ 2 } give the boundary edges on x = 1 and y = 0, respectively, and bdEdgeType{ 3 } stores the remaining part.
    • bdStr = [] implies that bdEdgeType{ 1 } = bdEdge. We also use bdEdgeIdxType to record the index in matrix edge, and bdNodeIdxType to store the nodes for respective boundary parts. Note that we determine the boundary of interest by

the coordinates of the midpoint of the edge, so 'x==1' can also be replaced by a statement like 'x>0.99'.

2.3 Code design of int2d.m and assem2d.m

In this article we only discuss the implementation of the bilinear forms in two dimensions.

2.3.1 The scalar case: assem2d.m

In this subsection we introduce the details of writing the subroutine assem2d.m to assemble a two-dimensional scalar bilinear form

a(v, u), v = ϕi, u = φj , i = 1, · · · , m, j = 1, · · · , n,

where the test function v and the trial function u are allowed to match different finite element spaces, which can be found in mixed finite element methods for Stokes problems. For the scalar case, assem2d.m is essentially the same as int2d.m, while the later one can be used to deal with vector cases like linear elasticity problems. To handle different spaces, we write Vh = {'P1', ... 'P2'} for the input of assem2d.m, where Vh{ 1 } is for v and Vh{ 2 } is for u. For simplicity, it is also allowed to write Vh = 'P1' when v and u are in the same space. Let us discuss the case where v and u lie in the same space. Suppose that the bilinear form contains only first-order derivatives. Then the possible combinations are ∫ K

avudx,

K

avxudx,

K

avyudσ, ∫ K

avuxdσ,

K

avxuxdσ,

K

avyuxdσ, ∫ K

avuydσ,

K

avxuydσ,

K

avyuydσ.

Of course, we often encounter the gradient form ∫ K

a∇v · ∇udσ =

K

a(vxux + vyuy)dσ.

We take the second bilinear form as an example. Let

aK (v, u) =

K

avxudσ,

and consider the P 1 -Lagrange finite element. Denote the local basis functions to be φ 1 , φ 2 , φ 3. Then the local stiffness matrix is

AK =

K

a

∂xφ 1 ∂xφ 2 ∂xφ 3

[

φ 1 φ 2 φ 3

]

dσ.

Let v 1 = ∂xφ 1 , v 2 = ∂xφ 2 , v 3 = ∂xφ 3 ; u 1 = φ 1 , u 2 = φ 2 , u 3 = φ 3.

7 K (: , s ) = area .* sum ( ww .* cc .* vi .* uj ,2) ; 8 s = s +1; 9 end 10 end

The bilinear form is assembled by using the build-in function sparse.m as in iFEM. In this case, the code is given as 1 ss = K (:) ; 2 kk = sparse ( ii , jj , ss , NNdof , NNdof ) ;

Here, the triple (ii, jj, ss) is called the sparse index. Please refer to the following link: https: //www.math.uci.edu/~chenlong/ifemdoc/fem/femdoc.html.

Remark 2.1. For the case where v and u are in different spaces, one just needs to modify the basis functions and the number of local degrees of freedom accordingly. The code can be presented as 1 s = 1; 2 for i = 1: Ndofv 3 for j = 1: Ndofu 4 vi = vbase { i }; uj = ubase { j }; 5 K (: , s ) = K (: , s ) + area .* sum ( ww .* cc .* vi .* uj ,2) ; 6 s = s +1; 7 end 8 end

In varFEM, we use Base2d.m to load the information of vi and uj, for example, the following code gives the values of ∂xφ, where φ is a local basis function. 1 v = 'v. dx '; 2 vbase = Base2d (v , node , elem , Vh {1} , quadOrder ) ; % v1. dx , v2. dx , v3. dx

2.3.2 The vector case: int2d.m

Let us consider a typical bilinear form for linear elasticity problems, given as

aK (v, u) :=

K

ε(v) : ε(u)dσ,

where v = (v 1 , v 2 )T^ , u = (u 1 , u 2 )T^ , and

ε(v) : ε(u) = v 1 ,xu 1 ,x + v 2 ,yu 2 ,y +^12 (v 1 ,y + v 2 ,x)(u 1 ,y + u 2 ,x) (2.2) = v 1 ,xu 1 ,x + v 2 ,yu 2 ,y +^12 (v 1 ,yu 1 ,y + v 1 ,yu 2 ,x + v 2 ,xu 1 ,y + v 2 ,xu 2 ,x). (2.3)

The stiffness matrix can be assembled as 1 Coef = {1 , 1 , 0.5 , 0.5 , 0.5 , 0.5}; 2 Test = { ' v1. dx ' , ' v2. dy ' , ' v1. dy ' , ' v1. dy ' , ' v2. dx ' , ' v2. dx ' }; 3 Trial = { ' u1. dx ' , ' u2. dy ' , ' u1. dy ' , ' u2. dx ' , ' u1. dy ' , ' u2. dx ' }; 4 kk = int2d ( Th , Coef , Test , Trial , Vh , quadOrder ) ;

We also provide the subroutine getExtendedvarForm.m to get the extended combinations (2.3) from (2.2), which has been included in int2d.m. Therefore, the bilinear form can be directly assembled as

1 Coef = { 1 , 1 , 0.5 }; 2 Test = { ' v1. dx ' , ' v2. dy ' , ' v1. dy + v2. dx ' }; 3 Trial = { ' u1. dx ' , ' u2. dy ' , ' u1. dy + u2. dx ' }; 4 kk = int2d ( Th , Coef , Test , Trial , Vh , quadOrder ) ;

In the rest of this subsection, we briefly discuss the sparse index. Let v = (v 1 , v 2 , v 3 ) and u = (u 1 , u 2 , u 3 ), and suppose that a(v, u) is a bilinear form. Note that, in general, vi(i = 1, 2 , 3) can be in different spaces, but vi and ui are in the same space, otherwise the resulting stiffness matrix is not a square matrix. The stiffness matrix after blocking has the following correspondence:

a(v, u) ↔ [v 1 , v 2 , v 3 ]

A 11 A 12 A 13

A 21 A 22 A 23

A 31 A 32 A 33

u 1 u 2 u 3

where ui is the vector of degrees of freedom of ui. It is easy to see that Aij can obtained as in scalar case by assembling all pairs that contain (vi, uj ) in a(v, u). Let the sparse index for Aij be (iij , jij , sij ). Let the numbers of rows and columns of Aij be mi and nj , respectively. Then the final sparse assembly index ii and jj can be written in block matrix as   

i 11 i 12 i 13 i 21 + m 1 i 22 + m 1 i 23 + m 1 i 31 + m 2 i 32 + m 2 i 33 + m 2

 and

j 11 j 12 + n 1 j 13 + n 2 j 21 j 22 + n 1 j 23 + n 2 j 31 j 32 + n 1 j 33 + n 2

and obtained by straightening them as a column vector along the row vectors.

3 Textbook examples

In this section, we present several examples that frequently encountered in textbooks to demon- strate the use of varFEM.

3.1 Poisson-type problems

We now provide the complete implementation of the model problem (2.1):     

−∇ · (a∇u) + cu = f in Ω, u = gD on ΓD, gRu + a∂nu = gN on ΓR.

The PDE stored is generated by Poissondatavar.m. The function file reads 1 function uh = varPoisson ( Th , pde , Vh , quadOrder ) 2 3 % % Assemble stiffness matrix 4 % Omega 5 Coef = { pde .a , pde. c }; 6 Test = { 'v. grad ' , 'v. val ' };

The test script is presented as follows (see main varPoisson.m). 1 % % Parameters 2 maxIt = 5; 3 N = zeros ( maxIt ,1) ; 4 h = zeros ( maxIt ,1) ; 5 ErrL2 = zeros ( maxIt ,1) ; 6 ErrH1 = zeros ( maxIt ,1) ; 7 8 % % Generate an intitial mesh 9 [ node , elem ] = squaremesh ([0 1 0 1] ,0.5) ; 10 bdStr = 'x ==0 '; 11 12 % % Get PDE data 13 pde = Poissondatavar ; 14 g_R = @ ( p ) 1 + p (: ,1) + p (: ,2) ; % 1 + x + y 15 pde. g_R = g_R ; 16 17 % % Finite Element 18 i = 1; % 1 ,2 , 19 Vh = [ 'P ' , num2str ( i ) ]; 20 quadOrder = i +2; 21 for k = 1: maxIt 22 % refine mesh 23 [ node , elem ] = uniformrefine ( node , elem ) ; 24 % get the mesh information 25 Th = FeMesh2d ( node , elem , bdStr ) ; 26 % solve the equation 27 uh = varPoisson ( Th , pde , Vh , quadOrder ) ; 28 % record and plot 29 N ( k ) = size ( elem ,1) ; 30 h ( k ) = 1/( sqrt ( size ( node ,1) ) -1) ; 31 if N ( k ) < 2 e3 % show mesh and solution for small size 32 figure (1) ; 33 showresult ( node , elem , pde. uexact , uh ) ; 34 drawnow ; 35 end 36 % compute error 37 ErrL2 ( k ) = varGetL2Error ( Th , pde. uexact , uh , Vh , quadOrder ) ; 38 ErrH1 ( k ) = varGetH1Error ( Th , pde. Du , uh , Vh , quadOrder ) ; 39 end 40 41 % % Plot convergence rates and display error table 42 figure (2) ; 43 showrateh (h , ErrH1 , ErrL2 ) ; 44 fprintf ( '\ n ') ; 45 disp ( ' Table : Error ') 46 colname = { '# Dof ' , 'h ' , ' || u - u_h || ' , '|u - u_h | _1 ' }; 47 disptable ( colname ,N ,[] , h , ' %0.3 e ' , ErrL2 , ' %0.5 e ' , ErrH1 , ' %0.5 e ') ; In the for loop, we first load or generate the mesh, which immediately returns the matrix node and elem to the Matlab workspace. Then we set up the boundary conditions to get the structural information. The subroutine varPoisson.m is the function file containing all source code to implement the FEM as given before. When obtaining the numerical solutions, we can visualize the solutions by using the subroutines showresult.m. We then calculate the discrete L^2 and H^1 errors via the subroutines varGetL2Error.m and varGetH1Error.m. The procedure is

completed by verifying the rate of convergence through showrateh.m.

(^00) 0.5 1

1

1

2 1

u^ 2.

y

x 0 0 0.

1

2 1

u^ 2.

y

x 0 0 0. Fig. 1: The nodal values of exact and numerical solutions for the model problem

The test script can be easily used to compute Pi-Lagrange element for i = 1, 2 , 3 (see Line 18 in the test script). The nodal values for the model problem are displayed in Fig. 1. The rates of convergence are shown in Fig. 2, from which we observe the optimal convergence for all cases.

10 -5 (^101) log(1/h) 102

10 -

10 -

10 -

10 -

(^100) |u-hh| 1 O (h ||u-u^1 h)|| O (h^2 )

(a) i = 1

10 -8 (^101) log(1/h) 102

10 -

10 -

10 -

10 -

10 -

10 -

10 -1 |u-hh| 1 O (h ||u-u^2 h)|| O (h^3 )

(b) i = 2

10 -10 (^101) log(1/h) 102

10 -

10 -

10 -

10 -

10 -

10 -

10 -3 |u-hh| 1 O (h ||u-u^3 h)|| O (h^4 )

(c) i = 3

Fig. 2: Convergence rates for the model problem.

We also print the errors on the Matlab command window by using disptable.m, with the results for P 3 element given below:

Table: Error #Dof h ||u-u_h|| |u-u_h|_


32 2.500e-01 2.53816e-05 5.28954e- 128 1.250e-01 1.53744e-06 6.50683e- 512 6.250e-02 9.46298e-08 8.07922e- 2048 3.125e-02 5.87001e-09 1.00695e- 8192 1.562e-02 3.65444e-10 1.25698e-

The function disptable.m is given in iFEM using the build-in function disp.m. We give a simple and well-printed implementation just using the built-in function table.m. We next consider the example with a circular domain or an L-shaped domain (see main varPoisson pdetool.m). Such a domain can be generated by using the pdetool as 1 % % Generate an intitial mesh 2 g = ' circleg '; % ' lshapeg '; 3 [p ,e , t ] = initmesh (g , ' hmax ' ,0.5) ; 4 node = p '; elem = t (1:3 ,:) '; 5 bdStr = 'x >0 '; % string for Neumann The results are given in Fig. 3

1 % ( v1. grad , u1. grad ) , ( v2. grad , u2. grad ) 2 cf = 1; 3 Coef = cf ; Test = 'v. grad '; Trial = 'u. grad '; 4 A = assem2d ( Th , Coef , Test , Trial , Vh , quadOrder ) ;

The second term of the bilinear form has the following combinations: ∫ Ω

v 1 ,xu 1 ,xdx,

Ω

v 1 ,xu 2 ,ydx,

Ω

v 2 ,yu 1 ,xdx

Ω

v 2 ,yu 2 ,ydx,

which correspond to A 11 , A 12 , A 21 and A 22 , respectively, and can be computed as follows. 1 % ( v1. dx , u1. dx ) 2 cf = 1; 3 Coef = cf ; Test = 'v. dx '; Trial = 'u. dx '; 4 B1 = assem2d ( Th , Coef , Test , Trial , Vh , quadOrder ) ; 5 % ( v1. dx , u2. dy ) 6 cf = 1; 7 Coef = cf ; Test = 'v. dx '; Trial = 'u. dy '; 8 B2 = assem2d ( Th , Coef , Test , Trial , Vh , quadOrder ) ; 9 % ( v2. dy , u1. dx ) 10 cf = 1; 11 Coef = cf ; Test = 'v. dy '; Trial = 'u. dx '; 12 B3 = assem2d ( Th , Coef , Test , Trial , Vh , quadOrder ) ; 13 % ( v2. dy , u2. dy ) 14 cf = 1; 15 Coef = cf ; Test = 'v. dy '; Trial = 'u. dy '; 16 B4 = assem2d ( Th , Coef , Test , Trial , Vh , quadOrder ) ;

The block matrix is then given by 1 % kk 2 kk = [ mu * A +( lambda + mu ) * B1 , ( lambda + mu ) * B2 ; 3 ( lambda + mu ) * B3 , mu * A +( lambda + mu ) * B4 ]; 4 kk = sparse ( kk ) ; The right-hand side has two components: ∫ Ω

f 1 v 1 dx and

Ω

f 2 v 2 dx.

The load vector is assembled in the following way: 1 % % Assemble right hand side 2 % F 3 Coef = @ ( pz ) pde. f ( pz ) *[1;0]; Test = 'v. val '; 4 F1 = assem2d ( Th , Coef , Test ,[] , Vh , quadOrder ) ; 5 % F 6 Coef = @ ( pz ) pde. f ( pz ) *[0;1]; Test = 'v. val '; 7 F2 = assem2d ( Th , Coef , Test ,[] , Vh , quadOrder ) ; 8 % F 9 ff = [ F1 ; F2 ]; For the vector problem, we impose the Dirichlet boundary value conditions as follows. 1 % % Apply Dirichlet boundary conditions 2 g_D = pde. g_D ; 3 on = 1; 4 g_D1 = @ ( p ) g_D ( p ) *[1;0]; 5 g_D2 = @ ( p ) g_D ( p ) *[0;1];

6 gBc = { g_D1 , g_D2 }; 7 Vhvec = { Vh , Vh }; 8 u = apply2d ( on , Th , kk , ff , Vhvec , gBc ) ; % note Vhvec

Here, g D1 is for u 1 and g D2 is for u 2. Note that the finite element spaces Vhvec must be given in the same structure of gBc. The solutions are displayed in Fig. 4 (The test script is main varElasticityNavier block.m).

Fig. 4: The nodal values of exact and numerical solutions for the elasticity problem of displacement type.

3.2.2 The variational formulation of tensor type

The bilinear form is

a(v, u) = 2μ

Ω εij (v)εij (u)dσ + λ

Ω (∂ivi)(∂j uj )dσ,

where the summation is omitted. We can still assemble the stiffness matrix in block form as before (see main varElasticity block.m). Here we only present the implementation of vectorial FEMs using int2d.m. The computation of the first term has been given in Subsect. 2.3.2, i.e., 1 % ( Eij ( u ) : Eij ( v ) ) 2 Coef = { 1 , 1 , 0.5 }; 3 Test = { ' v1. dx ' , ' v2. dy ' , ' v1. dy + v2. dx ' }; 4 Trial = { ' u1. dx ' , ' u2. dy ' , ' u1. dy + u2. dx ' }; 5 A = int2d ( Th , Coef , Test , Trial , Vh , quadOrder ) ; 6 A = 2* mu * A ;

The second term can be computed as 1 % ( div u , div v ) 2 Coef = 1; 3 Test = ' v1. dx + v2. dy ' ; 4 Trial = ' u1. dx + u2. dy '; 5 B = int2d ( Th , Coef , Test , Trial , Vh , quadOrder ) ; 6 B = lambda * B ; 7 % stiffness matrix 8 kk = A + B ; The linear form is `(v) =

Ω

f · vdσ +

Γ 1

g · vds.

For the first term, one has 1 Coef = pde. f ; Test = 'v. val '; 2 ff = int2d ( Th , Coef , Test ,[] , Vh , quadOrder ) ;

By introducing a new variable w = −∆u, the above problem can be written in a mixed form as     

−∆u = w, −∆w = f, u = ∂nu = 0 on ∂Ω.

The associated variational problem is: Find (w, u) ∈ H^1 (Ω) × H^10 (Ω) =: V × U such that   

Ω ∇u · ∇φdσ =

Ω wφdσ, φ ∈ H^1 (Ω), ∫ Ω

∇w · ∇ψdσ =

Ω

f ψdσ, ψ ∈ H 01 (Ω).

Let a(w, φ) = −

Ω

wφdσ and b(φ, u) =

Ω

∇φ · ∇udσ.

One has (^)   

a(w, φ) + b(φ, u) = 0, φ ∈ H^1 (Ω) = V, b(w, ψ) = (f, ψ), ψ ∈ H 01 (Ω) = U,

where a(·, ·) : V × V → R and b(·, ·) : V × U → R. The functions u and w will be approximated by P 1 -Lagrange elements. Let N be the vector of global basis functions. One easily gets   

φT^ Aw + φT^ Bu = 0, ψT^ BT^ w = ψT^ f ,

where A = −

Ω

N T^ N dσ, B =

Ω

∇N T^ · ∇N dσ, f =

Ω

N T^ f dσ.

Note that the local form has the similar structure. The system can be written in block matrix form as (^) [ A B BT^ O

] [

w u

]

[

f

]

Remark 3.1. If ∂nu does not vanish on ∂Ω, then the mixed variational formulation is   

a(w, φ) + b(φ, u) =

∂Ω

∂nuφds, φ ∈ V, b(w, ψ) = (f, ψ), ψ ∈ U.

Here, ∂nu corresponds to the Neumann boundary data for u in the first equation.

3.3.2 The programming in scalar form

We first assemble the linear system by using the block structure in (3.4). In block matrix form, the stiffness matrix can be computed as follows. 1 % % Assemble stiffness matrix 2 % matrix A 3 Coef = 1; Test = 'v. val '; Trial = 'u. val ';

4 A = - assem2d ( Th , Coef , Test , Trial , ' P1 ' , quadOrder ) ; 5 % matrix B 6 Coef = 1; Test = 'v. grad '; Trial = 'u. grad '; 7 B = assem2d ( Th , Coef , Test , Trial , ' P1 ' , quadOrder ) ; 8 % kk 9 O = zeros ( size ( B ) ) ; 10 kk = [A , B ; B ' , O ]; 11 kk = sparse ( kk ) ;

The right-hand side is given by 1 % % Assemble right - hand side 2 Coef = pde. f ; Test = 'v. val '; 3 ff = assem2d ( Th , Coef , Test ,[] , ' P1 ' , quadOrder ) ; 4 O = zeros ( size ( ff ) ) ; 5 ff = [ O ; ff ];

The computation of the Neumann boundary condition reads 1 % % Assemble Neumann boundary condition 2 Th. elem1d = Th. bdEdge ; % all boundary edges 3 % Th. bdEdgeIdx1 = Th. bdEdgeIdx ; 4 % Coef = @ ( p ) pde. Du ( p ) * n ; 5 Coef = interpEdgeMat ( pde. Du , Th , quadOrder ) ; 6 Test = 'v. val '; 7 ff (1: N ) = ff (1: N ) + assem1d ( Th , Coef , Test ,[] , ' P1 ' , quadOrder ) ;

We finally impose the Dirichlet boundary conation as 1 % % Apply Dirichlet boundary conditions 2 on = 1; 3 g_D = pde. g_D ; 4 gBc = {[] , g_D }; 5 Vhvec = { ' P1 ' , ' P1 ' }; 6 U = apply2d ( on , Th , kk , ff , Vhvec , gBc ) ; % note Vhvec 7 w = U (1: N ) ; u = U ( N +1:end) ;

Note that the Dirichlet data is only for u, so we set gBc{ 1 } = [] in Line 4. The test script is main varBiharmonicMixedFEM block.m. The convergence rates for u and w are shown in Fig. 5, from which we clearly observe the first-order and the second-order convergence in the H^1 norm and L^2 norm for both variables.

(^10101) log(1/h) 102

10 -

10 -

100

|u-hh| 1 O (h ||u-u^1 ) O (hh1.9||)

(^10101) log(1/h)

100

101

102

(^103) |w-w O (hh^1 )|^1 ||w-wh|| O (h1.9)

Fig. 5: The convergence rates of the mixed FEM for the biharmonic equation (P 1 element)

10 -4 (^101) log(1/h) 102

10 -

10 -

10 -

100

101

ErruH1 O (h (^1) ) ErruL2 O (h1.9) 10 -2 (^101) log(1/h) 102

10 -

100

101

102

103

ErrwH1 O (h (^1) ) ErrwL2 O (h1.9)

(a) P

10 -6 (^101) log(1/h) 102

10 -

10 -

10 -

10 -

10 -

100

ErruH1 O (h (^2) ) ErruL2 O (h3.1) 10 -2 (^101) log(1/h) 102

10 -

100

101

102

103

ErrwH1 O (h0.47) ErrwL2 O (h1.5)

(b) P

10 -9 (^101) log(1/h) 102

10 -

10 -

10 -

10 -

10 -

10 -

10 -

10 -

ErruH1 O (h (^3) ) ErruL2 O (h4.1) 10 -4 (^101) log(1/h) 102

10 -

10 -

10 -

100

101

102

ErrwH1 O (h1.6) ErrwL2 O (h2.5)

(c) P

Fig. 6: The convergence rates for the biharmonic equation.

3.4 Stokes problem

The Stokes problem is to find (u, p) such that     

−ν∆u − ∇p = f in Ω, divu = 0 in Ω, u = g on ∂Ω,

where, Ω ⊂ Rd^ (d = 2, 3), u = (u 1 , · · · , ud)T^ is the velocity of the fluid, p is the pressure and ν = 1/Re > 0 with Re called Reynolds number.

  • Models the steady state flow of viscous fluid.
  • Conservation of mass: divu = ∇ · u = 0 (“incompressibility condition”).

3.4.1 The variational problem

Multiplying the vector v ∈ H^10 (Ω)d^ =: H^1 (Ω) on both sides of the first equation of (3.5), and integrating over Ω gives ∫ Ω

−ν∆u · vdx +

Ω

∇p · vdx =

Ω

f · vdx.

Integration by parts yields ∫ Ω

∑^ d i=

ν∇ui · ∇vidx −

Ω

p

∑^ d i=

∂vi ∂xi^ dx^ =

Ω

f · vdx, (3.6)

where v = (v 1 , · · · , vd)T^. Let

∇u =

∂u 1 ∂x 1 · · ·^ ∂u 1

.^ ∂xd ..... ... ∂ud ∂x 1 · · ·^ ∂ud ∂xd

 and^ ∇v^ =

∂v 1 ∂x 1 · · ·^ ∂v 1

.^ ∂xd ..... ... ∂vd ∂x 1 · · ·^ ∂vd ∂xd

For two d-order matrices A = (aij ) and B = (bij ), define the inner product

(A, B) = A : B =

∑^ d i=

∑^ d j=

aij bij.

Then the equation (3.6) can be written as ∫ Ω

ν∇u : ∇vdx −

Ω

(divv)pdx =

Ω

f · vdx.

Let g = 0 and define V = H 01 (Ω) and P = L^20 (Ω). The mixed variational problem is: Find (u, p) ∈ V × Q such that   

a(u, v) + b(v, p) = (f , v), v ∈ V , b(u, q) = 0, q ∈ P,

where a(u, v) = (ν∇u, ∇v), b(v, q) = (divv, q). Let Th be a shape regular triangulation of Ω. We consider the conforming finite element discretizations: Vh ⊂ V and Ph ⊂ P. Typical pairs (Vh, Ph) of stable finite element spaces include: MINI element, Girault-Raviart element and Pk −Pk− 1 elements. For the last one, a special example is the P 2 −P 1 element, also known as the Taylor-Hood element, which is the one under consideration. The FEM is to find (uh, ph) ∈ Vh × Ph such that   

a(uh, v) + b(v, ph) = F (v), v ∈ Vh, b(uh, q) = 0, q ∈ Ph.

The problem (3.7) can be solved either by discretizing it directly into a system of equations (a saddle point problem), or by adding the two equations, as done for the biharmonic equation. The later one is to find (uh, ph) ∈ Vh × L^2 (Ω) such that

a(uh, v) + b(v, ph) + b(uh, q) − ε(ph, q) = F (v), v ∈ Vh, ∈ L^2 (Ω), (3.8)

where ε is a small parameter to ensure stability or ph ∈ L^20 (You can find the fact by taking v 1 = v 2 = 0 and q = 1). In the following, we only consider the two-dimensional case.

3.4.2 The programming in scalar form

We remark that the problem (3.7) (when including the stabilization term) and (3.8) are equiv- alent. To express it more clearly, we write the first equation of the problem (3.5) specifically by components, with (^)   

−ν∆u 1 + ∂p∂x = f 1 , −ν∆u 2 + ∂p∂y = f 2.

The variational form is actually obtained by multiplying v 1 and v 2 ∈ H 01 (Ω) in the first and second rows, respectively, and then adding them up after the integration by parts. The integration by parts gives   

ν

Ω

∇u 1 · ∇v 1 dx −

Ω

p ∂v ∂x^1 dx =

Ω

f 1 v 1 dx,

ν

Ω

∇u 2 · ∇v 2 dx −

Ω

p ∂v ∂y^2 dx =

Ω

f 2 v 2 dx.