Boundary conditions
Distinguish prescribed values, natural fluxes, and periodic identifications.
Prescribed values restrict admissible fields, while prescribed fluxes enter the weak equation as loads. This guide follows one mixed-boundary diffusion problem, then explains how the same distinction applies to elasticity and periodic domains.
Start from the boundary-value problem
Let . The scalar field satisfies
with on the top and bottom. Here is the outward unit normal and . The exact solution is . Its gradient is horizontal and its right normal derivative is one.
The word “flux” requires a sign convention. Here it means the prescribed normal derivative. A physical diffusive flux defined by instead has .
1. Label the boundary regions
Attributes identify the mesh regions on which boundary conditions or integrals are applied. This program labels exterior edges by checking both endpoints after edge-to-cell connectivity has identified the boundary.
constexpr size_t nodes = 17; constexpr Attribute fixed = 2; constexpr Attribute loaded = 3; constexpr Attribute insulated = 4; constexpr Real tolerance = 1e-12; // Recognize coordinates on this unit grid. Mesh mesh = Mesh().UniformGrid(Polytope::Type::Triangle, {nodes, nodes}); mesh.scale(1.0 / (nodes - 1)); mesh.getConnectivity().compute(1, 2); for (auto edge = mesh.getFace(); !edge.end(); ++edge) { if (!edge->isBoundary()) continue; bool onLeft = true; bool onRight = true; for (auto vertex = edge->getVertex(); !vertex.end(); ++vertex) { const Real x = vertex->getCoordinates().x(); onLeft = onLeft && std::abs(x) < tolerance; onRight = onRight && std::abs(x - 1) < tolerance; } mesh.setAttribute({1, edge->getIndex()}, onLeft ? fixed : onRight ? loaded : insulated); }
fixed, loaded, and insulated are local names for integer labels. Other meshes may use different values or load their labels from a mesh file. The elasticity example uses the same labeling approach for a rectangular beam.
2. Separate essential and natural conditions
Test functions vanish on the left, where the solution is prescribed. Integration by parts gives
where is the right edge. The boundary contribution vanishes on the left because the test function is zero there, and on the top and bottom because the prescribed normal derivative is zero.
P1 vh(mesh); TrialFunction u(vh); TestFunction v(vh); RealFunction flux = 1.0; Problem diffusion(u, v); diffusion = Integral(Grad(u), Grad(v)) - BoundaryIntegral(flux, v).over(loaded) + DirichletBC(u, Zero()).on(fixed);
DirichletBC(...).on(fixed) restricts the unknown on the selected boundary. BoundaryIntegral(...).over(loaded) adds the Neumann load. It is subtracted in the residual expression, so it appears with a positive sign on the right-hand side of the weak equation.
Prescribe nonzero values
For a scalar trial function u, a known function can supply the boundary value.
RealFunction value = [](const Geometry::Point& point) { return point.x() * point.x(); }; auto condition = DirichletBC(u, value).on(fixed);
Omitting .on(...) selects the exterior boundary. Multiple attributes can be selected with .on(first, second). The value must have the same range shape as the trial field; a two-component displacement can use Zero(2).
What essential elimination changes
With fixed coefficients , the remaining equations are
The prescribed values therefore affect both the operator and the load. Constraints are imposed by structural elimination. These are finite element coefficients; their interpretation depends on the space's degrees of freedom.
The essential constraint elimination reference gives the full projector formulation, sparse storage requirements, and implementation costs for callers supplying their own linear systems.
3. Homogeneous natural conditions
For this diffusion weak form, omitting a boundary load on an unconstrained exterior edge supplies the homogeneous natural condition on the normal derivative. The condition is represented in the weak equation through boundary integration.
The natural boundary condition follows from the operator and its integration-by-parts formula.
Traction in elasticity
The natural boundary quantity is the traction . A vector traction enters through a boundary load, while fixed displacement remains an essential condition.
P1 vh(mesh, mesh.getSpaceDimension()); TrialFunction u(vh); TestFunction v(vh); constexpr Real lambda = 0.5769; constexpr Real mu = 0.3846; VectorFunction traction{0, -1e-4}; Problem elasticity(u, v); elasticity = LinearElasticityIntegral(u, v)(lambda, mu) - BoundaryIntegral(traction, v).over(loaded) + DirichletBC(u, Zero(2)).on(fixed);
Edges with neither fixed displacement nor a traction load are traction-free for this elasticity formulation. See the complete beam tutorial for the model and labels.
4. Identify boundaries and handle the nullspace
PeriodicBC relates slave and master DOFs across matching boundaries. For a scalar P1 grid, the periodic Poisson tutorial constructs the pairing using coordinates and the finite element space's DOF queries to determine the constrained indices.
All corner identifications must be consistent. Higher-order spaces also require the appropriate edge or face DOFs; pairing only vertices is insufficient. The example exercises the native assembly path; backend support should be checked before transferring constraints to a different execution backend.
Periodic Poisson still has a constant nullspace. Its source must satisfy , and a normalization such as selects one solution. Neither identification alone nor choosing a direct solver removes that freedom. The same compatibility and normalization issue occurs for a connected pure-Neumann Poisson problem.
5. Inspect the computed field
Fixing the left edge removes the constant nullspace, so conjugate gradients is suitable for this symmetric diffusion system. The program checks solver success before exporting the result.
Solver::CG solver(diffusion); solver.setTolerance(1e-10).solve(); if (!solver.success()) return EXIT_FAILURE; IO::XDMF xdmf("MixedBoundary"); xdmf.grid().setMesh(mesh).add("u", u.getSolution()); xdmf.write().flush();

The linear field is representable exactly by P1 elements on this affine mesh; comparison with therefore checks boundary labels, the Neumann sign, and the algebraic solve without introducing an approximation error from a curved exact solution. Other problems still require mesh-refinement checks.
Complete program and further reading
The example is built as the MixedBoundary target. See MixedBoundary.cpp, the weak formulation derivation, and the numerical contracts for related details.