# 1

Finite Element Method to Solve Partial Differential Equations: Using DOLFIN Software Package
Huibing Yin

Abstract This project tries the software pacakge of DOLFIN to solve two examples of two dimensional PDEs. One of the PDEs is elliptic type while the other one is parabolic.

Index Terms DOLFIN, Finite Element Method (FEM), Partial Differential Equations (PDEs).

I. I NTRODUCTION

T

HERE are three types of PDEs: elliptic, parabolic and hyperbolic [1]. The parabolic type of PDE describles the transient phenomenon of the system, while the elliptic one the equilibrium states

of the system. In this project, I will try to use DOLFIN software package to solve both the parabolic type and elliptic type PDEs. And I will set the same structure and parameters of the differential operator parts for both of the problems. So the solutions of parabolic PDE shall be quite close to the elliptic one if the time elaps long enough. In the following, the models of the problems are ﬁrst introduced. Then the method of using DOLFIN to solve the parabolic and elliptic PDEs are described step by step. Finally, the numerical results are presented.
CSE510 Class Project; May 12, 2009 yin3@illinois.edu

The radius of the hole is 0.e. an initial condition has to be speciﬁed. 1) × (−1. 0) = g(x) u(x. r = 0. (3) for all x ∈ Ω ⊂ R2 when x ∈ ∂Ω. (1) (2) while all other parts are the same.. Furthermore. u(x.2 II. y) = . x2 + y 2 q(x. III. The elliptic problem is − · (p(x) u) = q(x) ¯ u(x) = g(x) ¯ where p(x. y) = x2 + y 2 . 3) Get the meshes. i. Ω = (−1.5. 0) = 0 when x ∈ ∂Ω when x ∈ Ω \ ∂Ω. . Here I assume that the initial value of boundary points is the same as in boundary condition and all other initial values are zeros. The basic steps are 1) Get weak formulation of the problem. the steps of using DOLFIN to solve the PDEs are derived. The domain is a square. P ROBLEMS This project considers a problem the same as in the homework 5.5. i. x2 + y 2 exp(y − x2 ) g(x. N UMERICAL METHOD FOR SOLVING PDE S USING DOLFIN In this section. y) = The parabolic problem has an extra time dirivative term in the equation: u− ˙ · (p(x) u) = q(x) for all x ∈ Ω ⊂ R2 . exp(y − x2 ) 2 y − 4x2 y 2 + 2y − 4x4 − 3x2 . 1) with a circular hole in the middle..e. 2) Using FFC language in DOLFIN to deﬁne the weak formulation.

and multiply both sides with kn . . such that a(¯. v) = G(v) for all v ∈ V . we get the weak formulation for parabolic problem: For each time step n. ¯ u where a(¯. Deﬁne the weak formulation using FFC The deﬁntion of the weak formulation using FFC is quite staight forward. First deﬁne the element by specifying the family. v + kn q(x)v dx. where h h a(un . Weak formulation of the problem The weak formulation of the elliptic problem has been obtained in homework 4.3 4) Write the solver for the problem. tn−1 ) given h h such that 1 kn v(un h Ω − un−1 ) dx h n−1 uh + un h · + p(x) 2 Ω v dx = Ω q(x)v dx for all test functions v ∈ V . triangular shape and ﬁrst degree ﬁnite element. for all v ∈ V. A. The discretized equation writes: Find un = uh (·. It is to ﬁnd u ∈ V. Here we use Lagrange family. the shape and the degree [2]. (6) (7) G(v) = Ω vun−1 − h kn n−1 p(x) uh · 2 B. (4) (5) G(v) = The parabolic equation (3) has a time derivative term. tn ) with un−1 = uh (·. Ω v dx. To discretize the equation. v) = h Ω vun + h kn p(x) un · h 2 v dx. v) = u Ω p(x) u · ¯ q(x)v dx. 5) Compile the programe. ﬁnd un ∈ V such that a(un . the backwoard Euler method is used [2]. v) = G(v). run it and get the results. Shifting all the terms with un−1 to h the right hand side. where kn = tn − tn−1 is the time step.

4 1 element = FiniteElement("Lagrange".i)*dx L = v*u0*dx . The triangle elements are used. the functions of p(x) and q(x) and the boundary condtions g(x) have to be supplied."triangle". Meshes To solve the problem. To implement the solver. Compile the previous form deﬁniton. trial function.5*k*p*D(v. The compilation command is: 1 fcc -l dolfin <filename> C.i)*D(u0. 2 3 4 5 v u p q = = = = TestFunction(element) TrialFunction(element) Function(element) Function(element) ∂v : ∂xi Finally. where D(v. i) stands for 6 7 a = p*D(v.0.05 a = v*u1*dx + 0. This can be done by deriving a . and ordinary functions that are used in the weak formulatioin. Weak form deﬁntion for parabolic problem Here.i)*dx L = q*v*dx The weak form deﬁntion for parabolic problem is listed as follows which are similar to elliptic one: 1 3 4 5 6 7 8 10 11 element = FiniteElement("Lagrange".i)*dx + k*q*v*dx Listing 1.i)*D(u."triangle". The mesh ﬁgures are shown in the following.5*k*p*D(v.1) v = TestFunction(element) u1 = TrialFunction(element) p = Function(element) u0 = Function(element) q = Function(element) k = 0. are deﬁned.1) Then the functions including the test function. we need to get the meshes for the domain of problem. the bilinear form a and linear form L is deﬁned. D. we use the constant step size of k = 0. The solvers for the problem The solvers for the two problem are programmed using C++.05. we get the head ﬁle that is needed to write the solver. The meshes are got using the software “Gmsh” [3].i)*D(u1.

class Coeff: public Function . 1. } }.0*x1*x1*y*y + 2. The meshes used in this project. const double* x) const { const double x1 = x[0]. values[0] = exp(y-x1*x1)/( x1*x1 + y*y )*\ (y*y .4.4. const double y = x[1].3. new class from class “Function” and providing the evaluation function “eval”.0*x1*x1). 1 2 3 4 5 6 7 8 9 10 12 class Source: public Function { void eval(double* values.0*y .5 Y Z X Y Z X Y Z X Y Z X Fig.0*x1*x1*x1*x1 .

Furthermore. 4 // Create mesh 5 if (argc == 2){ 6 File in(argv[1]). } }. const double* x) const { values[0] = exp(x[1]-x[0]*x[0])/(x[0]*x[0]+x[1]*x[1]). ﬁrst create the mesh from the mesh ﬁle generated by “Gmsh” and converted to the type of ‘. 27 28 29 30 31 32 33 class DirichletBoundary: public SubDomain { bool inside(const double* x. And the whole boundary is this type. this can be done by deﬁning different boundary classes and specifying when the point is within the sub-domain. the boundary conditions are Dirichlet type. the predeﬁned solver are called to actually solve the weak formulation. This can be done by simply returning the variable “on boundary”.0 + DOLFIN_EPS && on_boundary. } }. the results are plotted. the value is different from the remaining. } }. bool on_boundary) const { return on_boundary. The boundary conditions are set up and the weak formulation of PDE is deﬁned. if we impose that at boundary x = −1. If other types of boundary conditions are to be deﬁned or the boundary is divide to several sub-domains. char*argv[]) 2 { 3 Mesh mesh. class BndFunc: public Function { void eval(double* values.xml’. For example.6 13 14 15 16 17 18 20 21 22 23 24 25 26 { void eval(double* values. 1 int main(int argc. In this project. Then the function space is created. const double* x) const { values[0] = x[0]*x[0] + x[1]*x[1]. In the main function. 7 in >> mesh. 8 } . Finally. we can substitute the line 31 with return x[0] < -1.

order(). 1 class InitVal: public Function 2 { 3 void eval(double* values.L. 6 if (x[0] < -1 + DOLFIN_EPS || x[0] > 1-DOLFIN_EPS \ 7 || x[1]<-1+DOLFIN_EPS || x[1]>1-DOLFIN_EPS){ 8 values[0] = exp(x[1]-x[0]*x[0])/(x[0]*x[0]+x[1]*x[1]).solve(u). file << u. // Set up BCs DirichletBoundary boundary. L. .5*0. ProjLinearForm L(V). } mesh. 9 } 10 if ( abs(x[0]*x[0] + x[1]*x[1] . Coeff p. The main difference between the parabolic and elliptic problem is that a series of weak form problems need to be solved. VariationalProblem prob(a. // Solve variational problem Function u. in >> mesh. Also.p = p.xml"). So an linear equation solver is deﬁned and the matrix and vectors are assembled.5) < DOLFIN_EPS ){ 11 values[0] = exp(x[1]-x[0]*x[0])/(x[0]*x[0]+x[1]*x[1]). ProjBilinearForm a(V. prob.7 9 10 11 12 13 15 16 18 19 20 21 23 24 25 27 28 29 30 32 34 35 36 38 39 41 42 44 45 } else{ File in("mesh/mesh. // Define PDE Source q. DirichletBC bc(V. a.V). // Create function space ProjFunctionSpace V(mesh). g.0. For each time step a new vector has to be assembled.pvd"). // Plot plot(u). BndFunc g. const double* x) const 4 { 5 values[0] = 0. boundary).bc). return 0.0. the initial value function has to be provided. File file("Poisson.q = q. which is done by deﬁning a new class derived from class “Function”.

BndFunc g. which is consistent with the value in the form deﬁnition.u0 = u0. L. // Linear system Matrix A. L.p = p. g. L. HeatLinearForm L(V). . 15 16 18 19 20 21 23 24 25 26 27 28 30 31 32 33 34 35 37 38 39 41 42 43 45 46 48 49 // Create function space HeatFunctionSpace V(mesh).q = q. // Assemble matrix assemble(A. 7 in >> mesh. } } The main function is provided in the following list.order(). HeatBilinearForm a(V. a.p = p. 1 int main(int argc.05 is used. Coeff p. Vector b. V). // Set up BCs DirichletBoundary boundary. a).vector(). // Solve variational problem Function u1(V).zero(). The constant time step of k = 0. 8 } 9 else{ 10 File in(". Function u(V)./mesh/mesh1. 11 in >> mesh. 12 } 13 mesh.8 12 13 14 }. 4 // Create mesh 5 if (argc == 2){ 6 File in(argv[1]). InitVal u0. char*argv[]) 2 { 3 Mesh mesh. boundary). // LU LUSolver lu. DirichletBC bc(V.. // Function u(V). // u. // Define forms Source q.xml").

// Save solution in VTK format file << u.apply(A). commands # Get compiler from pkg-config compiler = commands. ’Heatmain.getoutput(’pkg-config --variable=compiler dolfin’) # Create a SCons Environment based on the main os environment env = Environment(ENV=os.9 50 52 53 54 55 57 59 60 61 62 63 65 66 68 69 70 72 73 74 76 77 78 80 81 } bc. CXX=compiler) # Get compiler flags from pkg-config env.apply(b). L.pvd"). plot(u).ParseConfig(’pkg-config --cflags --libs dolfin’) # Program name env. bc. All we have to supply is the ﬁle name of the solver and the name of the resulting programme.Program(’demo’.u0 = u. // Move to next interval t += k. // Parameters for time-stepping double T = 2. u. b). 1 2 3 4 5 6 7 8 9 import os. E. File file("Heat.05. L). double t = k.environ. //solve the linear system lu.cpp’) . double k = 0. } // Plot solution cout << "t = " << t << endl.solve(A. while ( t <= T ) { // Assemble vector and apply boundary conditions assemble(b. return 0.vector(). Compile and run the programme The compiler and system environment variables are automatically obtained by DOLFIN.

Fig. F. [2] A. N UMERICAL RESULTS First. 2.0. the more smooth the solution. the solution is almost the same as the one of elliptic problem. 2005.org Cambridge University Press. 2nd ed. [Online]. We can see from the ﬁgure that after t = 3. 2. 2009. W. The mesh size decreases from left to right and from top to bottom. Numerical Solution of Partial Differential Equations. the elliptic problem is solved using four different size of meshes. Available: http://www. IV. Mayers. The smaller the mesh size. a series of solutions are obtained through solving the corresoponding weak formulation (6)-(7). Numerical solution for elliptic problem. R EFERENCES [1] K. Logg. The solutions are show in Fig.fenics. Morton and D. 3 lists the numerical results for some time instants.10 Fig. Feb. . FFC User Manual. For the parabolic problem.

3.0 (g) t = 3. Numerical solutions for parabolic problem at different time.0 .25 (d) t = 0.15 (c) t = 0.11 (a) t = 0.0 Fig.05 (b) t = 0.5 (e) t = 1. (h) t = 5.0 (f) t = 2.

accepted for publication. 2009. . “Gmsh: a three-dimensional ﬁnite element mesh generator with built-in pre.and post-processing facilities.12 [3] C.-F. Geuzaine and J.” International Journal for Numerical Methods in Engineering. Remacle.