Essi Parent / May 17 2021 / Published
FEniCS 2019.1 tutorial 1
The first FEniCS tutorial, modelling the Poisson equation, is computed to make sure everything works.
FEniCS tutorial demo program: Poisson equation with Dirichlet conditions
Test problem is chosen to give an exact solution at all nodes of the mesh.
inline_formula not implemented in the unit square
inline_formula not implemented on the boundary
inline_formula not implemented
inline_formula not implemented
Import packages.
from fenics import *
1.1s
Python
Create mesh and define function space
mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, 'P', 1)
1.4s
Python
Define boundary condition
u_D = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]', degree=2)
def boundary(x, on_boundary):
return on_boundary
bc = DirichletBC(V, u_D, boundary)
2.4s
Python
Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(-6.0)
a = dot(grad(u), grad(v))*dx
L = f*v*dx
0.0s
Python
Compute solution.
u = Function(V)
solve(a == L, u, bc)
4.1s
Python
Plot solution and mesh.
plot(u)
0.5s
Python
plot(mesh)
0.4s
Python
Save solution to file in VTK format.
vtkfile = File('results/solution.pvd')
vtkfile << u
0.0s
Python
Compute error in L2 norm.
error_L2 = errornorm(u_D, u, 'L2')
11.5s
Python
Compute maximum error at vertices.
vertex_values_u_D = u_D.compute_vertex_values(mesh)
vertex_values_u = u.compute_vertex_values(mesh)
import numpy as np
error_max = np.max(np.abs(vertex_values_u_D - vertex_values_u))
0.0s
Python
Print errors
print('error_L2 =', error_L2)
print('error_max =', error_max)
0.3s
Python
0.0s
Python