dolfin for solving pde with fe
TRANSCRIPT
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. INTRODUCTION
THERE 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 first 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
2
II. PROBLEMS
This project considers a problem the same as in the homework 5. The domain is a square, Ω =
(−1, 1) × (−1, 1) with a circular hole in the middle. The radius of the hole is 0.5, i.e., r = 0.5. The
elliptic problem is
−∇ · (p(~x)∇u) = q(~x) for all ~x ∈ Ω ⊂ R2 (1)
u(~x) = g(~x) when ~x ∈ ∂Ω, (2)
where
p(x, y) = x2 + y2,
q(x, y) =exp(y − x2)
x2 + y2
(y2 − 4x2y2 + 2y − 4x4 − 3x2
),
g(x, y) =exp(y − x2)
x2 + y2.
The parabolic problem has an extra time dirivative term in the equation:
u−∇ · (p(~x)∇u) = q(~x) for all ~x ∈ Ω ⊂ R2, (3)
while all other parts are the same. Furthermore, an initial condition has to be specified. Here I assume
that the initial value of boundary points is the same as in boundary condition and all other initial values
are zeros, i.e.,
u(~x, 0) = g(~x) when ~x ∈ ∂Ω
u(~x, 0) = 0 when ~x ∈ Ω \ ∂Ω.
III. NUMERICAL METHOD FOR SOLVING PDES USING DOLFIN
In this section, the steps of using DOLFIN to solve the PDEs are derived. The basic steps are
1) Get weak formulation of the problem,
2) Using FFC language in DOLFIN to define the weak formulation,
3) Get the meshes,
3
4) Write the solver for the problem,
5) Compile the programe, run it and get the results.
A. Weak formulation of the problem
The weak formulation of the elliptic problem has been obtained in homework 4. It is to
find u ∈ V, such that a(u, v) = G(v), for all v ∈ V,
where
a(u, v) =
∫Ω
p(~x)∇u · ∇v d~x, (4)
G(v) =
∫Ω
q(~x)v d~x. (5)
The parabolic equation (3) has a time derivative term. To discretize the equation, the backwoard Euler
method is used [2]. The discretized equation writes: Find unh = uh(·, tn) with un−1
h = uh(·, tn−1) given
such that
1
kn
∫Ω
v(unh − un−1
h ) d~x +
∫Ω
p(~x)∇un−1h + un
h
2· ∇v d~x =
∫Ω
q(~x)v d~x
for all test functions v ∈ V , where kn = tn − tn−1 is the time step. Shifting all the terms with un−1h to
the right hand side, and multiply both sides with kn, we get the weak formulation for parabolic problem:
For each time step n, find unh ∈ V such that a(un
h, v) = G(v) for all v ∈ V , where
a(unh, v) =
∫Ω
(vun
h +kn
2p(~x)∇un
h · ∇v
)d~x, (6)
G(v) =
∫Ω
(vun−1
h − kn
2p(~x)∇un−1
h · ∇v + knq(~x)v
)d~x. (7)
B. Define the weak formulation using FFC
The defintion of the weak formulation using FFC is quite staight forward. First define the element by
specifying the family, the shape and the degree [2]. Here we use Lagrange family, triangular shape and
first degree finite element.
4
1 element = FiniteElement("Lagrange","triangle",1)
Then the functions including the test function, trial function, and ordinary functions that are used in the
weak formulatioin, are defined.
2 v = TestFunction(element)3 u = TrialFunction(element)4 p = Function(element)5 q = Function(element)
Finally, the bilinear form a and linear form L is defined, where D(v, i) stands for ∂v∂~xi
:
6 a = p*D(v,i)*D(u,i)*dx7 L = q*v*dx
The weak form defintion for parabolic problem is listed as follows which are similar to elliptic one:
1 element = FiniteElement("Lagrange","triangle",1)
3 v = TestFunction(element)4 u1 = TrialFunction(element)5 p = Function(element)6 u0 = Function(element)7 q = Function(element)8 k = 0.05
10 a = v*u1*dx + 0.5*k*p*D(v,i)*D(u1,i)*dx11 L = v*u0*dx - 0.5*k*p*D(v,i)*D(u0,i)*dx + k*q*v*dx
Listing 1. Weak form defintion for parabolic problem
Here, we use the constant step size of k = 0.05. Compile the previous form definiton, we get the head
file that is needed to write the solver. The compilation command is:
1 fcc -l dolfin <filename>
C. Meshes
To solve the problem, we need to get the meshes for the domain of problem. The triangle elements are
used. The meshes are got using the software “Gmsh” [3]. The mesh figures are shown in the following.
D. The solvers for the problem
The solvers for the two problem are programmed using C++. To implement the solver, the functions
of p(~x) and q(~x) and the boundary condtions g(~x) have to be supplied. This can be done by deriving a
5
X
Y
Z X
Y
Z
X
Y
Z X
Y
Z
Fig. 1. The meshes used in this project.
new class from class “Function” and providing the evaluation function “eval”.
1 class Source: public Function2 3 void eval(double* values, const double* x) const4 5 const double x1 = x[0];6 const double y = x[1];7 values[0] = exp(y-x1*x1)/( x1*x1 + y*y )*\8 (y*y - 4.0*x1*x1*y*y + 2.0*y - 4.0*x1*x1*x1*x1 - 3.0*x1*x1);9
10 ;
12 class Coeff: public Function
6
13 14 void eval(double* values, const double* x) const15 16 values[0] = x[0]*x[0] + x[1]*x[1];17 18 ;
20 class BndFunc: public Function21 22 void eval(double* values, const double* x) const23 24 values[0] = exp(x[1]-x[0]*x[0])/(x[0]*x[0]+x[1]*x[1]);25 26 ;
In this project, the boundary conditions are Dirichlet type. And the whole boundary is this type. This
can be done by simply returning the variable “on boundary”. If other types of boundary conditions are
to be defined or the boundary is divide to several sub-domains, this can be done by defining different
boundary classes and specifying when the point is within the sub-domain.
27 class DirichletBoundary: public SubDomain28 29 bool inside(const double* x, bool on_boundary) const30 31 return on_boundary;32 33 ;
For example, if we impose that at boundary x = −1, the value is different from the remaining, we can
substitute the line 31 with
return x[0] < -1.0 + DOLFIN_EPS && on_boundary;
In the main function, first create the mesh from the mesh file generated by “Gmsh” and converted to
the type of ‘.xml’. Then the function space is created. The boundary conditions are set up and the weak
formulation of PDE is defined. Furthermore, the predefined solver are called to actually solve the weak
formulation. Finally, the results are plotted.
1 int main(int argc, char*argv[])2 3 Mesh mesh;4 // Create mesh5 if (argc == 2)6 File in(argv[1]);7 in >> mesh;8
7
9 else10 File in("mesh/mesh.xml");11 in >> mesh;12 13 mesh.order();
15 // Create function space16 ProjFunctionSpace V(mesh);
18 // Set up BCs19 DirichletBoundary boundary;20 BndFunc g;21 DirichletBC bc(V, g, boundary);
23 // Define PDE24 Source q;25 Coeff p;
27 ProjBilinearForm a(V,V);28 a.p = p;29 ProjLinearForm L(V);30 L.q = q;
32 VariationalProblem prob(a,L,bc);
34 // Solve variational problem35 Function u;36 prob.solve(u);
38 // Plot39 plot(u);
41 File file("Poisson.pvd");42 file << u;
44 return 0;45
The main difference between the parabolic and elliptic problem is that a series of weak form problems
need to be solved. So an linear equation solver is defined and the matrix and vectors are assembled. For
each time step a new vector has to be assembled. Also, the initial value function has to be provided,
which is done by defining a new class derived from class “Function”.
1 class InitVal: public Function2 3 void eval(double* values, const double* x) const4 5 values[0] = 0.0;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]);9
10 if ( abs(x[0]*x[0] + x[1]*x[1] - 0.5*0.5) < DOLFIN_EPS )11 values[0] = exp(x[1]-x[0]*x[0])/(x[0]*x[0]+x[1]*x[1]);
8
12 13 14 ;
The main function is provided in the following list. The constant time step of k = 0.05 is used, which
is consistent with the value in the form definition.
1 int main(int argc, char*argv[])2 3 Mesh mesh;4 // Create mesh5 if (argc == 2)6 File in(argv[1]);7 in >> mesh;8 9 else
10 File in("../mesh/mesh1.xml");11 in >> mesh;12 13 mesh.order();
15 // Create function space16 HeatFunctionSpace V(mesh);
18 // Set up BCs19 DirichletBoundary boundary;20 BndFunc g;21 DirichletBC bc(V, g, boundary);
23 // Define forms24 Source q;25 Coeff p;26 InitVal u0;27 // Function u(V);28 // u.vector().zero();
30 HeatBilinearForm a(V, V);31 a.p = p;32 HeatLinearForm L(V);33 L.u0 = u0;34 L.q = q;35 L.p = p;
37 // Solve variational problem38 Function u1(V);39 Function u(V);
41 // Linear system42 Matrix A;43 Vector b;
45 // LU46 LUSolver lu;
48 // Assemble matrix49 assemble(A, a);
9
50 bc.apply(A);
52 // Parameters for time-stepping53 double T = 2;54 double k = 0.05;55 double t = k;
57 File file("Heat.pvd");
59 while ( t <= T )60 61 // Assemble vector and apply boundary conditions62 assemble(b, L);63 bc.apply(b);
65 //solve the linear system66 lu.solve(A, u.vector(), b);
68 L.u0 = u;69 // Save solution in VTK format70 file << u;
72 // Move to next interval73 t += k;74
76 // Plot solution77 cout << "t = " << t << endl;78 plot(u);
80 return 0;81
E. Compile and run the programme
The compiler and system environment variables are automatically obtained by DOLFIN. All we have
to supply is the file name of the solver and the name of the resulting programme.
1 import os, commands2 # Get compiler from pkg-config3 compiler = commands.getoutput(’pkg-config --variable=compiler dolfin’)4 # Create a SCons Environment based on the main os environment5 env = Environment(ENV=os.environ, CXX=compiler)6 # Get compiler flags from pkg-config7 env.ParseConfig(’pkg-config --cflags --libs dolfin’)8 # Program name9 env.Program(’demo’, ’Heatmain.cpp’)
10
Fig. 2. Numerical solution for elliptic problem.
IV. NUMERICAL RESULTS
First, the elliptic problem is solved using four different size of meshes. The solutions are show in Fig. 2.
The mesh size decreases from left to right and from top to bottom. The smaller the mesh size, the more
smooth the solution.
For the parabolic problem, a series of solutions are obtained through solving the corresoponding weak
formulation (6)-(7). Fig. 3 lists the numerical results for some time instants. We can see from the figure
that after t = 3.0, the solution is almost the same as the one of elliptic problem.
REFERENCES
[1] K. W. Morton and D. F. Mayers, Numerical Solution of Partial Differential Equations, 2nd ed. Cambridge University Press, 2005.
[2] A. Logg, FFC User Manual, Feb. 2009. [Online]. Available: http://www.fenics.org
11
(a) t = 0.05 (b) t = 0.15
(c) t = 0.25 (d) t = 0.5
(e) t = 1.0 (f) t = 2.0
(g) t = 3.0 (h) t = 5.0
Fig. 3. Numerical solutions for parabolic problem at different time.
12
[3] C. Geuzaine and J.-F. Remacle, “Gmsh: a three-dimensional finite element mesh generator with built-in pre- and post-processing facilities,”
International Journal for Numerical Methods in Engineering, 2009, accepted for publication.