A FEniCS Tutorial - FEniCS Project
A FEniCS Tutorial - FEniCS Project A FEniCS Tutorial - FEniCS Project
In the present problem we have that ∫ a(u,v) = ∫ L(v) = Ω Ω ∇u·∇vdx, (10) fvdx. (11) From the mathematics literature, a(u,v) is known as a bilinear form and L(v) as a linear form. We shall in every linear problem we solve identify the terms with the unknown u and collect them in a(u,v), and similarly collect all terms with only known functions in L(v). The formulas for a and L are then coded directly in the program. Tosummarize, beforemaking aFEniCS program for solving aPDE, wemust first perform two steps: • Turn the PDE problem into a discrete variational problem: find u ∈ V such that a(u,v) = L(v) ∀v ∈ ˆV. • Specify the choice of spaces (V and ˆV), which means specifying the mesh and type of finite elements. 1.3 Implementation The test problem so far has a general domain Ω and general functions u 0 and f. For our first implementation we must decide on specific choices of Ω, u 0 , and f. It will be wise to construct a specific problem where we can easily check that the computed solution is correct. Let us start with specifying an exact solution u e (x,y) = 1+x 2 +2y 2 (12) on some 2D domain. By inserting (12) in our Poisson problem, we find that u e (x,y) is a solution if f(x,y) = −6, u 0 (x,y) = u e (x,y) = 1+x 2 +2y 2 , regardlessoftheshapeofthedomain. Wechoosehere, forsimplicity, thedomain to be the unit square, Ω = [0,1]×[0,1]. The reason for specifying the solution (12) is that the finite element method, with a rectangular domain uniformly partitioned into linear triangular elements, will exactly reproduce a second-order polynomial at the vertices of the cells, regardless of the size of the elements. This property allows us to verify the implementation by comparing the computed solution, called u in this document (except when setting up the PDE problem), with the exact solution, denoted by u e : u should equal u to machine precision at the nodes. Test problems with this property will be frequently constructed throughout this tutorial. A FEniCS program for solving the Poisson equation in 2D with the given choices of u 0 , f, and Ω may look as follows: 8
""" FEniCS tutorial demo program: Poisson equation with Dirichlet conditions. Simplest example of computation and visualization with FEniCS. -Laplace(u) = f on the unit square. u = u0 on the boundary. u0 = u = 1 + x^2 + 2y^2, f = -6. """ from dolfin import * # Create mesh and define function space mesh = UnitSquare(6, 4) #mesh = UnitCube(6, 4, 5) V = FunctionSpace(mesh, ’Lagrange’, 1) # Define boundary conditions u0 = Expression(’1 + x[0]*x[0] + 2*x[1]*x[1]’) def u0_boundary(x, on_boundary): return on_boundary bc = DirichletBC(V, u0, u0_boundary) # Define variational problem u = TrialFunction(V) v = TestFunction(V) f = Constant(-6.0) a = inner(nabla_grad(u), nabla_grad(v))*dx L = f*v*dx # Compute solution u = Function(V) solve(a == L, u, bc) # Plot solution and mesh plot(u) plot(mesh) # Dump solution to file in VTK format file = File(’poisson.pvd’) file
- Page 1 and 2: A FEniCS Tutorial Hans Petter Langt
- Page 3 and 4: 7 Miscellaneous Topics 82 7.1 Gloss
- Page 5 and 6: that changing the PDE and boundary
- Page 7: The proper statement of our variati
- Page 11 and 12: polynomial approximations over each
- Page 13 and 14: Instead of nabla_grad we could also
- Page 15 and 16: solution during program development
- Page 17 and 18: The demo program d2_p2D.py in the s
- Page 19 and 20: All mesh objects are of type Mesh s
- Page 21 and 22: Here, T is the tension in the membr
- Page 23 and 24: """ FEniCS program for the deflecti
- Page 25 and 26: Figure 3: Plot of the deflection of
- Page 27 and 28: Figure 4: Example of visualizing th
- Page 29 and 30: Let us continue to use our favorite
- Page 31 and 32: 1.11 Computing Functionals After th
- Page 33 and 34: e_Ve.vector()[:] = u_e_Ve.vector().
- Page 35 and 36: It is possible to restrict the inte
- Page 37 and 38: Other functions for visualizing 2D
- Page 39 and 40: 1 0.8 Contour plot of u 4 3.5 3 2.5
- Page 41 and 42: accordingly: f = −6, { −4, y =
- Page 43 and 44: Here, Γ 0 is the boundary x = 0, w
- Page 45 and 46: The object A is of type Matrix, whi
- Page 47 and 48: with exact solution u(x) = x 2 . Ou
- Page 49 and 50: The variational formulation of our
- Page 51 and 52: iter += 1 solve(a == L, u, bcs) dif
- Page 53 and 54: tol = 1E-14 def left_boundary(x, on
- Page 55 and 56: We may collect the terms with the u
- Page 57 and 58: as ǫ → 0. This last expression i
In the present problem we have that<br />
∫<br />
a(u,v) =<br />
∫<br />
L(v) =<br />
Ω<br />
Ω<br />
∇u·∇vdx, (10)<br />
fvdx. (11)<br />
From the mathematics literature, a(u,v) is known as a bilinear form and L(v)<br />
as a linear form. We shall in every linear problem we solve identify the terms<br />
with the unknown u and collect them in a(u,v), and similarly collect all terms<br />
with only known functions in L(v). The formulas for a and L are then coded<br />
directly in the program.<br />
Tosummarize, beforemaking a<strong>FEniCS</strong> program for solving aPDE, wemust<br />
first perform two steps:<br />
• Turn the PDE problem into a discrete variational problem: find u ∈ V<br />
such that a(u,v) = L(v) ∀v ∈ ˆV.<br />
• Specify the choice of spaces (V and ˆV), which means specifying the mesh<br />
and type of finite elements.<br />
1.3 Implementation<br />
The test problem so far has a general domain Ω and general functions u 0 and<br />
f. For our first implementation we must decide on specific choices of Ω, u 0 , and<br />
f. It will be wise to construct a specific problem where we can easily check that<br />
the computed solution is correct. Let us start with specifying an exact solution<br />
u e (x,y) = 1+x 2 +2y 2 (12)<br />
on some 2D domain. By inserting (12) in our Poisson problem, we find that<br />
u e (x,y) is a solution if<br />
f(x,y) = −6, u 0 (x,y) = u e (x,y) = 1+x 2 +2y 2 ,<br />
regardlessoftheshapeofthedomain. Wechoosehere, forsimplicity, thedomain<br />
to be the unit square,<br />
Ω = [0,1]×[0,1].<br />
The reason for specifying the solution (12) is that the finite element method,<br />
with a rectangular domain uniformly partitioned into linear triangular elements,<br />
will exactly reproduce a second-order polynomial at the vertices of the cells,<br />
regardless of the size of the elements. This property allows us to verify the<br />
implementation by comparing the computed solution, called u in this document<br />
(except when setting up the PDE problem), with the exact solution, denoted<br />
by u e : u should equal u to machine precision at the nodes. Test problems with<br />
this property will be frequently constructed throughout this tutorial.<br />
A <strong>FEniCS</strong> program for solving the Poisson equation in 2D with the given<br />
choices of u 0 , f, and Ω may look as follows:<br />
8