Solving the Helmholtz equation
Solve a complex wave problem and check it against a manufactured solution.
This example extends the Poisson workflow to a complex-valued wave amplitude. It introduces a negative mass term, explains why conjugate gradients is unsuitable here, and exports the real and imaginary parts of the computed field separately.
The mathematical problem
On , consider
The complex amplitude may represent a time-harmonic field; a corresponding real oscillation is . The boundary data define a bounded-domain Dirichlet problem.
A manufactured solution makes the source and boundary data explicit.
Indeed, . The chosen is not a Dirichlet eigenvalue of the unit square.
The weak formulation
For complex test functions with zero boundary trace, the weak equation is
The bar denotes complex conjugation. Rodin's complex forms are linear in the trial field and conjugate-linear in the test field. The negative mass term gives an indefinite system requiring an appropriate solver.
1. Resolve the wave on a mesh
The program generates a unit-square triangular mesh with 97 vertices in each coordinate direction, giving 9409 vertices and 18432 triangles. The wavelength in the horizontal direction is .
constexpr size_t nodes = 97; const Real pi = Math::Constants::pi(); const Real waveNumber = 4 * pi; Mesh mesh = Mesh().UniformGrid(Polytope::Type::Triangle, {nodes, nodes}); mesh.scale(1.0 / (nodes - 1)); mesh.getConnectivity().compute(1, 2);
Mesh refinement is used to assess wave dispersion and the error at the chosen frequency, with the distance to resonance included in the assessment.
2. Define the complex problem
P1<Complex> stores a complex coefficient at each mesh vertex. Both the source and the boundary values come from the same manufactured solution.
P1<Complex> vh(mesh); TrialFunction u(vh); TestFunction v(vh); ComplexFunction exact = [=](const Point& point) { return std::exp(Complex(0, waveNumber * point.x())) * std::sin(pi * point.y()); }; auto source = pi * pi * exact; Problem helmholtz(u, v); helmholtz = Integral(Grad(u), Grad(v)) - waveNumber * waveNumber * Integral(u, v) - Integral(source, v) + DirichletBC(u, exact);
As in the Poisson example, the load is subtracted to express a residual. Here the additional negative mass term represents the time-harmonic wave term.
3. Solve the indefinite system
Sparse LU solves the indefinite system for this two-dimensional discretization. The program checks solver success before export.
Solver::SparseLU solver(helmholtz); solver.solve(); if (!solver.success()) return EXIT_FAILURE;
4. Inspect real and imaginary parts
The output stores two real-valued fields, avoiding ambiguity when opening a complex field in a visualization application.
P1 realSpace(mesh); GridFunction realPart(realSpace); GridFunction imaginaryPart(realSpace); realPart = Re(u.getSolution()); imaginaryPart = Im(u.getSolution()); IO::XDMF xdmf("Helmholtz"); xdmf.grid().setMesh(mesh).add("real", realPart).add("imaginary", imaginaryPart); xdmf.write().flush();


Open Helmholtz.xdmf in ParaView, select Apply, then color by real or imaginary**. Keep the HDF5 files beside the XDMF file. The reference fields for comparison are and .
Complete program
See Helmholtz.cpp for the source and the installation guide for build setup. The optional plotting script in examples/PDEs/visualize_paraview.py reproduces the figure from the exported point values.