Traces and Co-dimension
Which operator a trial function, a test function or a GridFunction uses on a boundary element

Michael Neunteufel

Rafael Dorigo

Joachim Schöberl

NGSolve Technical Note 2026-07 \(\bullet\)  version 2026-11-18

The problem

A finite element function of NGSolve [1] is evaluated on elements of every co-dimension VorB \(\in\{\)VOL, BND, BBND, BBBND\(\}\) of the mesh: inside the volume elements, on boundary faces, on edges, in points. What the restriction to a lower dimensional element is depends on the space.

A space therefore provides one differential operator per co-dimension. This note fixes which of them a trial function, a test function or a GridFunction uses, and when the user has to ask for it by Trace().

The rule

  1. Trial and test functions never project silently. Where the restriction to the elements of the integral is a projection, the form needs an explicit Trace(), one for each co-dimension.

  2. Trace() is optional where nothing is projected: for the value of an \(H^1\) function, and for spaces which live on the surface anyway.

  3. A GridFunction gives what the element sees. It selects the operator by the co-dimension of the element. On elements where it has none it is not defined.

Table 1 lists the consequences for forms on the boundary faces of a three-dimensional mesh.

Table 1: Trial and test functions in forms on boundary faces.
space value in a ds form canonical derivative in a ds form
H1, VectorH1 as it is, Trace() optional Trace(): surface gradient
HCurl Trace(): tangential component Trace()
HDiv Trace(): normal component not available
HCurlCurl, HDivDiv Trace() not available
NumberSpace as it is –
L2 no trace no trace
SurfaceL2, HDivSurface, HDivDivSurface as it is, Trace() optional as it is, Trace() optional

Trial and test functions

fes = HCurl(mesh, order=1)
e, f = fes.TnT()
a = BilinearForm(fes)
a += e*f*ds                      # error
a += e.Trace()*f.Trace()*ds      # tangential components on the faces

edges = dx(definedon=mesh.BBoundaries(".*"))
a += e.Trace().Trace()*f.Trace().Trace()*edges     # tangential component on the edges

Line 4 is refused when the form is built:

Trialfunction does not support BND-forms, maybe a Trace() operator is missing,
type = ngfem::DiffOpIdEdge<3,...>

The message names the remedy only where there is one. A trace which is a projection (tangential or normal component, surface gradient) in a volume integral, or a function of a surface space in a volume integral, gives

Trialfunction does not support VOL-forms, it lives on BND elements,
type = ngfem::DiffOpIdBoundaryEdge<3,...>

A trace which is just the value is accepted there and means the function itself: u.Trace()*v*dx is u*v*dx for \(H^1\) and NumberSpace.

For \(H^1\) the value needs no trace, the gradient does:

fes = H1(mesh, order=2)
u, v = fes.TnT()
a += u*v*ds                                 # the same as u.Trace()*v.Trace()*ds
a += grad(u).Trace()*grad(v).Trace()*ds     # surface gradients

The components of a product space follow the rule of their own space.

Properties of Trace().

Each call goes one co-dimension down. The result is cached, u.Trace() is u.Trace(), and it knows where it comes from: u.Trace().primary is u. Symbolic differentiation therefore sees through traces,

energy = 0.5*grad(u).Trace()*grad(u).Trace()
energy.Diff(u, v)                  # grad(u).Trace()*grad(v).Trace()

Time derivatives commute with it, u.dt.Trace() is u.Trace().dt. A trace which does not exist throws at once.

Spaces on surfaces

SurfaceL2, HDivSurface, HDivDivSurface, FacetSurface and their relatives have degrees of freedom on surface elements only and provide no operator for volume elements. Their functions are used on the surface as they are:

fes = HDivSurface(mesh, order=1)
s, t = fes.TnT()
a += s*t*ds + div(s)*div(t)*ds     # no Trace() needed
a += s*t*dx                        # error: it lives on BND elements

s.Trace() is still accepted and means the same, so existing scripts keep working. Functions of the facet spaces FacetSurface and NormalFacetSurface are used on the element boundaries, ds(element_boundary=True), also without Trace(). The edges of the surface elements are one co-dimension further down; there Trace() is needed again where it is a projection.

Setting a function of a surface space needs the region,

gf.Set(func, definedon=mesh.Boundaries(".*"))

Without definedon the call is an error.

GridFunctions

A GridFunction is evaluated on whatever element it is given: in a volume integral, on a boundary face when drawing, on an edge. It selects the operator for that element.

gfe = GridFunction(HCurl(mesh, order=1))
gfe.Set(CF((1, 2, 3)))
Integrate(gfe*gfe*dx, mesh)     # 14 on the unit cube: the full vector
Integrate(gfe*gfe*ds, mesh)     # 56: the tangential components on the six faces

On a face the function shows its trace. This is what one wants when drawing: the full vector is not single valued on an interior interface, and it is more expensive to evaluate. The full vector on the boundary is available as BoundaryFromVolumeCF(gfe).

gf.Trace() goes one co-dimension down, as for trial functions: the result is defined on faces and edges, no longer in the volume. p.ReplaceFunction(gf) applies the operators of a trial function p to a GridFunction and keeps its traces.

Where a function is defined.

A GridFunction of a surface space is not defined on volume elements, where it evaluates to zero. The part of a region where a coefficient function is defined is given by

from ngsolve.comp import DefinedOnRegion
DefinedOnRegion(cf, mesh.Materials(".*"))      # a Region

An expression is defined where all its inputs are. The webgui uses this: Draw(gf, mesh) of a surface function, or of gf.Trace(), draws the surface and skips the volume data; a function defined on some materials only is drawn there.

Boundary element operators

A layer potential is built from a density and its integration domain, and the density can only live there. The potentials therefore take the trace themselves, for the trial and for the test function:

V = HelmholtzSL(e*ds, kappa) * f*ds        # the same as with e.Trace(), f.Trace()

With densities from surface spaces, the usual case, no trace is involved at all.

What changed

The rule replaces a mixture of conventions. Scripts written for versions before November 2026 keep working with three exceptions, all of them former silent errors:

construct before now
a*b*dx for a surface space zero matrix error “lives on BND elements”
gf.Set(func) for a surface space, no definedon nothing was set error with a hint to definedon
Trace() with nothing left, e.g. of an L2 function unusable function error “don’t have a trace”

New are the optional Trace() on surface spaces, also for derivatives, differentiation through traces, and DefinedOnRegion.

Pitfall.

L2 has no boundary values. A form u*v*ds with L2 functions is accepted and contributes nothing.

References

  1. J. Schöberl. C++11 implementation of finite elements in NGSolve. ASC Report 30/2014, TU Wien, 2014.

  2. M. Neunteufel and J. Schöberl. The Hellan–Herrmann–Johnson method for nonlinear shells. Comput. Struct. 225:106109, 2019.

  3. M. Neunteufel and J. Schöberl. Avoiding membrane locking with Regge interpolation. Comput. Methods Appl. Mech. Engrg. 373:113524, 2021.