Finite element spaces
Choose a field representation, count its DOFs, and evaluate a discrete field.
A finite element space combines local elements over a mesh. Its choice determines the allowed fields and continuity, while a grid function holds one particular field in that space. The Ciarlet guide explains the local construction; this guide follows a runnable comparison of the principal scalar spaces.
Available spaces
| Space | Scalar representation | Continuity and role |
|---|---|---|
P0 | One constant per cell | Discontinuous cellwise data |
P0g | One constant over the entire mesh | A global parameter or multiplier |
P1 | Vertex-based first-order family | Continuous; affine on affine simplices |
H1 | Prescribed compile-time degree | Continuous higher-order approximation |
P0 supplies independent cell values; P0g supplies one scalar over the mesh. A DG formulation additionally specifies the numerical fluxes and stabilization; see DG formulations.
Construct the spaces on one mesh
The comparison uses a unit square with eight vertices in each direction. UniformGrid takes vertex counts, so the mesh has 64 vertices and 98 triangles. Its edge-to-cell and cell-to-edge relations are computed before constructing spaces that use edge DOFs.
constexpr size_t nodes = 8; Mesh mesh = Mesh().UniformGrid(Polytope::Type::Triangle, {nodes, nodes}); mesh.scale(1.0 / (nodes - 1)); mesh.getConnectivity().compute(1, 2); mesh.getConnectivity().compute(2, 1); P0 cellSpace(mesh); P0g constantSpace(mesh); P1 linearSpace(mesh); H1 quadraticSpace(std::integral_constant<size_t, 2>{}, mesh); P1 vectorSpace(mesh, 2); P1<Complex> complexSpace(mesh);
The scalar quadratic space has one DOF at every vertex and every edge on this triangular mesh. There are 161 edges, giving scalar DOFs. The counts printed by the program are:
| Space | Global scalar coefficients |
|---|---|
P0 | 98 |
P0g | 1 |
P1 | 64 |
Quadratic H1 | 225 |
Two-component P1 | 128 real coefficients |
Complex scalar P1 | 64 complex coefficients |
std::cout << "Vertices: " << mesh.getVertexCount() << '\n' << "Triangles: " << mesh.getCellCount() << '\n' << "P0: " << cellSpace.getSize() << '\n' << "P0g: " << constantSpace.getSize() << '\n' << "P1: " << linearSpace.getSize() << '\n' << "H1 degree 2: " << quadraticSpace.getSize() << '\n' << "Vector P1: " << vectorSpace.getSize() << '\n' << "Complex P1: " << complexSpace.getSize() << std::endl;
These are counts before essential boundary conditions or other constraints. A local cell's basis count differs from the global space's getSize().
P0 interpolation
P0 stores an independent constant on each cell. Its cellwise gradient is zero, while the field can jump across interfaces. In the implementation used here, assignment applies point evaluation at the mapped reference-cell center. On these affine triangles that point is the cell centroid.
For P0, interpolation evaluates the function at the centroid, while an projection assigns its cell average. Both reproduce constants; their values can differ for quadratic and non-polynomial data.
Polynomial degree
On an affine triangle, scalar P1 has three local basis functions and represents affine functions. On a quadrilateral the vertex-based family is bilinear on the reference cell; its physical gradient depends on the reference basis and geometric mapping. Cell type and geometry mapping therefore remain part of the approximation contract.
The H1 constructor takes its degree through std::integral_constant. On simplices it uses total-degree polynomial spaces, while tensor-product cells use the corresponding tensor-product construction.
| Cell | Scalar local DOFs at degree |
|---|---|
| Segment | |
| Triangle | |
| Quadrilateral | |
| Tetrahedron | |
| Hexahedron |
Field degree and geometric representation are chosen independently. Error estimates depend on solution regularity, mesh quality, formulation stability, and the consistency of geometry and quadrature.
Assign and evaluate a field
The comparison interpolates into P0, P1, and quadratic H1.
RealFunction exact = [](const Point& point) { return point.x() * point.x() + point.y() * point.y(); }; GridFunction constant(cellSpace); GridFunction linear(linearSpace); GridFunction quadratic(quadraticSpace); constant = exact; linear = exact; quadratic = exact;
Assignment applies the DOF functionals of the selected space to the prescribed function. In particular, the quadratic field should reproduce this polynomial on the affine triangular mesh, up to roundoff.
Evaluation goes through Geometry::Point, so the reference location and its physical cell are supplied together.
// Refine only the sampling grid in each reference triangle, not the FE mesh. constexpr size_t subdivisions = 6; std::ofstream samples("FiniteElementSpaces.csv"); samples.precision(17); samples << "cell,i,j,x,y,exact,p0,p1,p2\n"; for (auto cell = mesh.getCell(); !cell.end(); ++cell) { for (size_t i = 0; i <= subdivisions; ++i) { for (size_t j = 0; j <= subdivisions - i; ++j) { const Point point( *cell, Math::SpatialPoint{Real(i) / subdivisions, Real(j) / subdivisions}); samples << cell->getIndex() << ',' << i << ',' << j << ',' << point.x() << ',' << point.y() << ',' << exact(point) << ',' << constant(point) << ',' << linear(point) << ',' << quadratic(point) << '\n'; } } }
The additional points refine the sampling of each existing field while preserving its DOFs. The CSV records evaluations at the corresponding physical points.
Inspect the interpolated fields



The figures plot the interpolated fields. P0 uses its original cell values; P1 and quadratic H1 use the exported within-cell samples. For this mesh and sampling grid, the largest sampled errors are approximately for P0, for P1, and below for quadratic H1. These maxima are measured over the exported sample points with the mesh and prescribed function held fixed. Integrated error norms and convergence rates require integration and a sequence of refined meshes.
An projection is defined by finding with for every test function. The coefficients are obtained from the resulting mass-matrix problem. See numerical contracts.
Field range and coefficient ordering
Scalar, vector, matrix, and complex ranges are distinct from the spatial dimension. A two-dimensional mesh can carry a vector field with another component count; the model decides whether that choice is meaningful. A vector displacement in the beam example uses one component per spatial direction.
Entity DOFs and local-to-global indices are obtained from the space. These maps specify coefficient ordering for scalar, vector and higher-order fields.
Extend the field range to matrices
P0, P0g, P1, and H1 also have componentwise matrix-valued specializations. Matrix rows and columns are separate from mesh dimension; symmetry and Voigt scaling are specified by the formulation. These details, tensor derivatives, persistence, and backend support are retained in the matrix solutions and spatial tensors reference.
Represent complex fields
P1<Complex> assigns complex scalar coefficients to the geometric DOF locations of P1. The Helmholtz tutorial shows a complex solve and exports real and imaginary parts as separate real fields.
Mixed spaces
The Stokes tutorial uses quadratic velocity, linear pressure, and a global P0g multiplier for the pressure mean. The velocity–pressure pairing is selected to satisfy the stability requirements of the mixed formulation.
Complete program
Build the FiniteElementSpaces example target. It prints the counts above and writes FiniteElementSpaces.csv and ReferenceP1.csv in the working directory.
See FiniteElementSpaces.cpp and the variational guide for the next step from a known field to an unknown field in a weak problem.