General concepts » 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

SpaceScalar representationContinuity and role
P0One constant per cellDiscontinuous cellwise data
P0gOne constant over the entire meshA global parameter or multiplier
P1Vertex-based first-order familyContinuous; affine on affine simplices
H1Prescribed compile-time degreeContinuous 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 $64+161=225$ scalar DOFs. The counts printed by the program are:

SpaceGlobal scalar coefficients
P098
P0g1
P164
Quadratic H1225
Two-component P1128 real coefficients
Complex scalar P164 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 $L^2$ 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.

CellScalar local DOFs at degree $k$
Segment $k+1$
Triangle $(k+1)(k+2)/2$
Quadrilateral $(k+1)^2$
Tetrahedron $(k+1)(k+2)(k+3)/6$
Hexahedron $(k+1)^3$

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 $f(x,y)=x^2+y^2$ 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

Image
P0 interpolation assigns one constant value to each original triangle.
Image
P1 interpolation of the same quadratic function.
Image
Quadratic H1 interpolation of the same function. All three figures use the same color scale.

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 $0.186$ for P0, $0.0102$ for P1, and below $3\times10^{-15}$ 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 $L^2$ projection is defined by finding $w_h\in V_h$ with $\int_\Omega w_hv_h=\int_\Omega fv_h$ 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.