Poisson equation with periodic boundary conditions
Identify opposite boundaries and impose a zero-mean normalization.
Periodic boundaries identify opposite edges of a computational cell. Unlike the Dirichlet Poisson problem, this problem has no fixed boundary value, so the solution is determined up to a constant. This example handles both the periodic identification and that remaining freedom explicitly.
The mathematical problem
On , solve
The source has zero integral, as required by compatibility of a periodic Poisson problem. The zero-mean normalization selects the unique solution .
In the periodic finite element space, a global multiplier enforces the mean condition. The weak problem is
for every periodic test function and scalar test value . This removes the constant nullspace without changing the PDE on compatible data. The normalization removes the constant kernel before factorization.
1. Construct the periodic cell
The square is discretized with 33 vertices per direction, giving 1089 vertices and 2048 triangles before periodic identification.
constexpr size_t nodes = 33; const Real length = 2 * Math::Constants::pi(); Mesh mesh = Mesh().UniformGrid(Polytope::Type::Triangle, {nodes, nodes}); mesh.scale(length / (nodes - 1)); mesh.getConnectivity().compute(1, 2); P1 vh(mesh);
2. Pair boundary degrees of freedom
For scalar P1, each vertex has one degree of freedom. The map below obtains its index from the space, then uses the vertex coordinates to find its periodic representative. The indices are obtained from the finite element space.
std::map<std::pair<size_t, size_t>, Index> vertexDOFs; for (auto vertex = mesh.getVertex(); !vertex.end(); ++vertex) { const auto& point = vertex->getCoordinates(); const size_t i = std::llround(point.x() * (nodes - 1) / length); const size_t j = std::llround(point.y() * (nodes - 1) / length); vertexDOFs[{i, j}] = vh.getDOFs(0, vertex->getIndex())[0]; } IndexMap<IndexSet> periodicDOFs; for (const auto& [position, dof] : vertexDOFs) { const auto [i, j] = position; if (i == nodes - 1 || j == nodes - 1) periodicDOFs[dof].insert(vertexDOFs.at({i % (nodes - 1), j % (nodes - 1)})); }
Vertices on the right edge map to the left edge, and those on the top map to the bottom. The top-right corner maps directly to the bottom-left corner, so no cyclic chain is introduced. Each entry means “this slave DOF equals this master DOF.” This pairing is specific to this matching scalar P1 grid; higher order spaces also require edge and possibly face DOFs to be paired.
3. Impose periodicity and the mean
P0g provides one constant over the entire mesh. Its trial function is the multiplier, while its test function imposes the mean equation.
P0g meanSpace(mesh); TrialFunction u(vh); TrialFunction multiplier(meanSpace); TestFunction v(vh); TestFunction meanTest(meanSpace); RealFunction source = [](const Point& point) { return 2 * std::sin(point.x()) * std::cos(point.y()); }; Problem poisson(u, multiplier, v, meanTest); poisson = Integral(Grad(u), Grad(v)) + Integral(multiplier, v) + Integral(u, meanTest) - Integral(source, v) + PeriodicBC(u, periodicDOFs);
The periodic constraints identify both trial and test variations during assembly. Flux periodicity is the natural boundary condition of this weak formulation; no independent Dirichlet data are prescribed.
4. Solve and inspect the result
The multiplier gives a nonsingular saddle-point system, solved here with sparse LU. The result is exported on the original square mesh, with matching values on the paired edges.
Solver::SparseLU solver(poisson); solver.solve(); if (!solver.success()) return EXIT_FAILURE; IO::XDMF xdmf("Periodic"); xdmf.grid().setMesh(mesh).add("u", u.getSolution()); xdmf.write().flush();

Open Periodic.xdmf and color by u. Periodic constraints are checked by comparing paired boundary values. The discrete integral verifies the zero-mean normalization, and comparison with measures the field error.
Complete program
See Periodic.cpp and the boundary conditions guide.