This page was generated from unit-2.3-hcurlhdiv/hcurlhdiv.ipynb.

2.3 H(curl) and H(div) function spaces

Scalar and vectorial finite elements in NGSolve:

Standard continuous \(H^1\) elements: title

Nedelec’s tangentially continuous \(H(curl)\)-conforming edge elements:

../../_images/edgeelement.png

Raviart-Thomas normally continuous \(H(div)\)-conforming face elements:

../../_images/faceelement.png

Discontinuous \(L_2\) elements:

../../_images/l2element.png

The vector-valued spaces allow us to represent physical quantities which are either tangentially continuous (field intensities like the electric field), or normally continuous (flux densities like the magnetic flux or a current density).

The finite element spaces are related by the de Rham complex:

\[\begin{split}\DeclareMathOperator{\Grad}{grad} \DeclareMathOperator{\Curl}{curl} \DeclareMathOperator{\Div}{div} \begin{array}{ccccccc} H^1 & \stackrel{\Grad}{\longrightarrow} & H(\Curl) & \stackrel{\Curl}{\longrightarrow} & H(\Div) & \stackrel{\Div}{\longrightarrow} & L^2 \\[8pt] \bigcup & & \bigcup & & \bigcup & & \bigcup \\[8pt] W_{h} & \stackrel{\Grad}{\longrightarrow} & V_{h } & \stackrel{\Curl}{\longrightarrow} & Q_{h} & \stackrel{\Div}{\longrightarrow} & S_{h} \: \\[3ex] \end{array}\end{split}\]

NGSolve supports these elements of arbitrary order, on all common element shapes (triangles, quadrilaterals, tetrahedra, prisms, pyramids, hexahedra). Elements may be curved.

[1]:
from ngsolve import *
from ngsolve.webgui import Draw

mesh = Mesh(unit_square.GenerateMesh(maxh=0.3))

Generate a higher order \(H^1\) space. We first explore its different types of basis functions.

[2]:
order=3
fes = H1(mesh, order=order)
gfu = GridFunction(fes)

The first basis functions are hat functions, one per vertex. By setting the coefficient vector to a unit vector, we can look at the individual basis functions:

[3]:
gfu.vec[:] = 0
# vertex nr 17:
gfu.vec[17] = 1
Draw(gfu, min=0, max=1, deformation=True);

Next come the edge bubbles, with \(p-1\) basis functions per edge. A NodeId object refers to a particular vertex, edge, face or cell node of the mesh. We can ask for the degrees of freedom on a node:

[4]:
# basis functions on edge nr 10:
edge_dofs = fes.GetDofNrs(NodeId(EDGE,10))
print("edge_dofs =", edge_dofs)
gfu.vec[:] = 0
gfu.vec[edge_dofs[0]] = -1
Draw(gfu, order=3, min=-0.05, max=0.05, deformation=True);
edge_dofs = (39, 40)

Finally, we have \((p-1)(p-2)/2\) inner basis functions on every triangle:

[5]:
trig_dofs = fes.GetDofNrs(NodeId(FACE,0))
print("trig_dofs = ", trig_dofs)
gfu.vec[:] = 0
gfu.vec[trig_dofs[0]] = 10
Draw(gfu, order=3, min=0, max=0.3, deformation=True);
trig_dofs =  (103,)

The FESpace also classifies its dofs as local dofs, interface dofs and wire-basket dofs. This information is used for static condensation and for the BDDC preconditioner:

[6]:
nodes = { "vertex" : NodeId(VERTEX,17), "edge" : NodeId(EDGE,10), "trig" : NodeId(FACE,0) }
for name, node in nodes.items():
    for dof in fes.GetDofNrs(node):
        print (name, "dof", dof, ":", fes.CouplingType(dof))
vertex dof 17 : COUPLING_TYPE.WIREBASKET_DOF
edge dof 39 : COUPLING_TYPE.INTERFACE_DOF
edge dof 40 : COUPLING_TYPE.INTERFACE_DOF
trig dof 103 : COUPLING_TYPE.LOCAL_DOF

\(H(curl)\) finite element space

In NGSolve we use hierarchical high order finite element basis functions with node-wise exact sequences. The lowest order space \(V_{l.o.}\) is the edge element space:

\[\begin{split}\begin{array}{rcll} W_{hp} & = & W_{p=1} + \sum_E W_E + \sum_F W_F + \sum_C W_C & \subset H^1 \\[0.5em] V_{hp} & = & V_{l.o.} + \sum_E V_E + \sum_F V_F + \sum_C V_C & \subset H(curl) \end{array}\end{split}\]

where the edge, face and cell blocks are compatible in the sense that

\[\nabla W_E = V_E, \quad \nabla W_F \subset V_F, \quad \nabla W_C \subset V_C\]

We obtain this by using gradients of \(H^1\) basis functions as \(H(curl)\) basis functions, and adding some more (see the PhD thesis of Sabine Zaglmayr):

\[\begin{split}\begin{array}{rcl} V_E & = & \text{span} \{ \nabla \varphi_{E,i}^{H^1} \} \\ V_F & = & \text{span} \{ \nabla \varphi_{F,i}^{H^1} \cup \widetilde \varphi_{F,i}^{H(curl)} \} \\ V_C & = & \text{span} \{ \nabla \varphi_{C,i}^{H^1} \cup \widetilde \varphi_{C,i}^{H(curl)} \} \end{array}\end{split}\]
[7]:
fes = HCurl(mesh, order=2)
uc = GridFunction(fes, name="uc")

The first function on an edge is the lowest order edge element function. The high order edge functions are gradients:

[8]:
edge_dofs = fes.GetDofNrs(NodeId(EDGE,10))
print ("edgedofs: ", edge_dofs)
uc.vec[:] = 0
uc.vec[edge_dofs[0]] = 1
Draw (uc, min=0, max=3, vectors = { "grid_size":30})
Draw (curl(uc), mesh, "curl", min=-25, max=25);
edgedofs:  (10, 62, 63)
[9]:
face_dofs = fes.GetDofNrs(NodeId(FACE,10))
print ("facedofs: ", face_dofs)
uc.vec[:] = 0
uc.vec[face_dofs[0]] = 1
Draw (uc, min=0, max=1, vectors = { "grid_size":30})
Draw (curl(uc), mesh, "curl", min=-1, max=1, order=3); # it's a gradient
facedofs:  (135, 136, 137)

The \(H(curl)\) space of order \(p\) contains the gradients of the \(H^1\) space of order \(p+1\). Thus, the gradient of an \(H^1\) function is represented exactly, and its curl vanishes:

[10]:
gfw = GridFunction(H1(mesh, order=3))
gfw.Set (sin(3*x)*y)
uc.Set (grad(gfw))
print ("err  =", sqrt(Integrate((uc-grad(gfw))**2, mesh)))
print ("curl =", sqrt(Integrate(curl(uc)**2, mesh)))
err  = 1.360757281535753e-15
curl = 2.6561670796436994e-14

\(H(div)\) finite element space

NGSolve provides Raviart-Thomas (RT) as well as Brezzi-Douglas-Marini (BDM) finite element spaces for \(H(div)\). We obtain the RT version by setting RT=True, otherwise we get BDM.

[11]:
fes = HDiv(mesh, order=2, RT=True)
ud = GridFunction(fes)
func = x*y*CF((x,y))
ud.Set (func)
Draw (ud, vectors = { "grid_size":30})
print ("interpolation error:", sqrt(Integrate((func-ud)**2, mesh)))
interpolation error: 4.020315771469754e-16

This function is contained in \(RT_2 = [P^2]^2 + (x,y) \, P^2\), and thus it is reproduced exactly. It is not contained in the smaller space \(BDM_2 = [P^2]^2\):

[12]:
udbdm = GridFunction(HDiv(mesh, order=2))
udbdm.Set (func)
print ("interpolation error BDM:", sqrt(Integrate((func-udbdm)**2, mesh)))
interpolation error BDM: 0.00041925796353374257

The function spaces know their canonical derivatives. These operations are efficiently implemented by transformation from the reference element.

[13]:
gfu.derivname, uc.derivname, ud.derivname
[13]:
('grad', 'curl', 'div')

But there are additional options, like forming the element-wise gradient of \(H(div)\) finite element functions. We can query the available operators via

[14]:
print ("H(div) operators: ", ud.Operators())
H(div) operators:  ['grad', 'dual', 'normalcomponent']

and access them via the Operator method. For the gradient there is also the shortcut grad(ud):

[15]:
Draw (ud.Operator("grad")[0,1], mesh, "gradud");