A FEniCS Tutorial - FEniCS Project

A FEniCS Tutorial - FEniCS Project A FEniCS Tutorial - FEniCS Project

fenicsproject.org
from fenicsproject.org More from this publisher
19.06.2015 Views

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

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!