I was hoping to display the basis elements for a degree 2 Lagrange space with a mesh element on the unit interval on got some unexpected behavior. I am hoping that someone can help provide clarity.
For a function u\in\mathcal{V}_{h}, I expect
where \lbrace\phi_{i}\rbrace_{i} is a basis for \mathcal{V}_{h} and u_{i} are the finite-element expansion coefficients. I assume that the expansion coefficients are represented in the vector
data of a FEniCS Function
. With this in mind I then attempted to construct \lbrace \phi_{i}(x)\rbrace as a Function
, where only the $i$th entry of the associated vector
data is nonzero.
import numpy as np
import dolfin as dl
import matplotlib.pyplot as plt
#alter the plot style
plt.style.use(âclassicâ)
plt.rcParams.update({âfont.sizeâ: 16})
#define a unit interval mesh with nElem elements
nElem = 1
mesh = dl.UnitIntervalMesh(nElem)
dl.plot(mesh)
plt.show()
#define degree deg continuous elements over the mesh
deg = 2
Ph = dl.FiniteElement(âCGâ, mesh.ufl_cell(), deg)
Vh = dl.FunctionSpace(mesh, Ph)
#I now desire to visualize the basis functions
#I expect that the vector entry data of a dl.Function(), u
#corresponds to the finite element expansion of u
#that is u(x) = sum_i phi_i(x) u_i
#where u_i are the finite-element expansion coefficients
#and phi_i(x) are the finite-element basis functions
n = Vh.dim() # dim(Vh) = number of basis functions of Vh
basis_functions = [dl.Function(Vh) for i in range(n)]
ei = np.zeros(n) # numpy unit vector
for i in range(n):
ei[i] = 1.0
# assign the unit vector data to the dl.Function(Vh) function
# so that it now corresponds to a basis function
basis_functions[i].vector().set_local(ei[:])
ei[i] = 0.0
# plot the result
dl.plot(basis_functions[i])
plt.title(râ$k$th finite-element basis function k = {0:1d} '.format(i))
plt.show()
In the above example I specified degree deg
2 elements and so did not expect to see the following linear functions.
Please let me know if anything I have described is unclear.