hp-adaptivity
Goal
For solutions that are piecewise analytic with isolated singularities (material corners, metal tips, PML interfaces) the error of an appropriately graded \(hp\)-mesh decays as
in the number of DoFs \(N\) (Babuška & Guo 1986, Schwab 1998), versus algebraic \(N^{-p/d}\) for \(h\)-refinement and stagnation for pure \(p\)-refinement near singularities. This is the headline feature; convergence test #7 (CLAUDE.md §8) must show it.
Loop
SOLVE → ESTIMATE → MARK → DECIDE (h or p) → REFINE → (repeat)
- Estimate: element indicators \(\eta_K\) from error-estimation.md.
- Mark: Dörfler criterion — smallest set \(\mathcal M\) with
\(\sum_{K\in\mathcal M}\eta_K^2 \ge \theta \sum_K \eta_K^2\), \(\theta\approx 0.5\).
Implemented as
adaptivity::dorfler_marking(maximum_markingas the simpler alternative). - Decide per marked \(K\) (see hp decision below):
- error prediction (Melenk & Wohlmuth 2001,
adaptivity::hp_decide_by_prediction): a cell whose indicator fell to the value predicted for a smooth solution after the last refinement is p-refined, one that fell short is h-refined — the default; - coefficient decay (Mavriplis 1994; Houston & Süli 2005,
adaptivity::hp_decide): the decay rate \(\sigma_K\) of the Legendre (Dubiner) coefficients of \(E_{hp}|_K\), p-refinement above a threshold — reliable only once the local singularity is resolved; - to be implemented later: the "reference solution" strategy of Demkowicz (solve on \(h/2, p+1\) and project) — more expensive but very robust.
- Refine:
- \(h\): red refinement of the marked cells with the one-irregular rule (a face or
edge may be shared by at most two levels),
mesh::AdaptiveMesh::refine. Hanging edges/faces produce constrained DoFs: the slave DoFs are linear combinations of master DoFs computed by interpolation of the master shape functions on the refined entity (assembly::hanging_constraints, see below). The constraints reduce the assembled system (\(P^H A P\)) before the Dirichlet data is imposed on the free DoFs. - \(p\): raise the cell order (
adaptivity::p_refine, orhp_refinetogether with the h-step: children inherit the parent's order, p-marked cells are raised); shared edges/faces take the minimum order of adjacent cells (minimum rule) so the space stays conforming. The spaces are nested, soassembly::prolongate(withidentity_stepfor a pure p-step) transfers solutions exactly. - PML cells follow the refinement of the adjacent interior cells; never refine the PML alone.
- Transfer: prolongate the previous solution (
assembly::prolongate, exact by hierarchical interpolation) as the initial guess for iterative solvers; direct solvers ignore it.
hp decision
Error prediction (adaptivity/prediction.hpp)
After a refinement step every new cell carries the indicator it would have if the solution were smooth there (Melenk & Wohlmuth 2001):
with \(\gamma_p = 0.63 \approx \sqrt{0.4}\), \(\gamma_h = 2\), \(\gamma_n = 1\) (PredictionOptions,
the defaults of deal.II). A marked cell with \(\eta_{K'} \le \eta^{\text{pred}}_{K'}\) achieved
the smooth rate and is p-refined next, otherwise it is h-refined; in the first step (no
prediction) everything is h-refined. The corner cells of a singular solution never reach
the predicted \(2^{-p}\) reduction, so they are h-refined for ever, while cells at a fixed
distance-to-size ratio gain the predicted factor from each p-step.
hp_refine with spread_p (default) also raises the lower-order facet neighbours of a
p-refined cell by one: under the minimum rule the shared edges would otherwise stay at the
neighbour's order and the raised cell would only gain interior functions, which the
estimator keeps marking. Spreading raised the exponential rate \(b\) of the L-shape run from
\(0.23\) to \(0.28\) and halved the error at \(12\,000\) DoFs.
Coefficient decay (adaptivity/smoothness.hpp)
\(E_{hp}|_K\) is expanded component-wise in the \(L^2\)-orthonormal Dubiner basis of the
reference cell (fespace::DubinerBasis, Legendre and Jacobi polynomials in collapsed
coordinates). With \(a_n^2 = \sum_{|\alpha| = n}|c_\alpha|^2\) the least-squares fit
\(\log a_n \approx C - \sigma n\) over \(n = 0 \dots p_K\) gives the decay rate; a field
represented exactly (\(a_{p} = 0\)) counts as infinitely smooth. A cell is p-refined if
$\sigma_K \ge \tau(p_K) = $ smooth_threshold \(+\) threshold_scale\(/p_K\) with the
defaults \(1 + 3.5/p\), and cells below min_decision_order \(= 2\) are p-refined without a
decision. The \(1/p\) term was calibrated on the interpolant of the corner singularity
\(\nabla(r^{2/3}\sin\tfrac{2\theta}{3})\): a sequence that decays algebraically looks
exponential over few modes, and its fitted rate drops with \(p\) (corner cells: \(4.1, 2.0,
1.4, 1.1, 0.9, 0.7\) for \(p = 1 \dots 6\), cells away from the corner stay above
\(1.4\)).
Measured limitation. The decay of the Galerkin solution is not the decay of the exact field: on an unresolved corner cell the discrete solution is a smooth polynomial, with rates \(3.1\)–\(3.9\) at \(p = 2\) and \(2.0\)–\(2.5\) at \(p = 4\), above the thresholds, so the corner is p-refined up to \(p = 6\) before the first h-step and the loop converges no faster than h-adaptivity. This is why the error prediction is the default; the decay indicator remains available for solutions whose singularities are resolved, and as the smoothness measure of the prolongated solution in later strategies.
Hanging nodes and constraints
A hanging edge \((a,b)\) with midpoint \(m\) faces the half edges \((a,m)\), \((m,b)\) of the refined side; a hanging face \((a,b,c)\) faces its four child faces, the three half edges of each of its edges and the three edges between the midpoints. The DoFs of the fine entities are slaves of the masters on the parent entity and its boundary (for a face: the face DoFs, the DoFs of its three edges and, in H1, its three vertices):
The coefficients are computed numerically: every master function (evaluated through the
coarse cell in global orientation) is interpolated onto the child entities with
assembly::interpolate, which is exact because the parent's trace is a polynomial of
degree \(p_{\text{parent}} \le p_{\text{child}}\) (the DoF map's minimum rule includes the
children). Fine functions of higher degree than the parent get the coefficient \(0\) — they
are constrained to vanish. Hanging faces are processed first, then hanging edges whose
half edges are not yet constrained; a slave never has two constraints. The resulting
space is exactly the conforming subspace: tangential (H(curl)) or full (H1) continuity
across every hanging facet, and a global polynomial of the space is reproduced exactly.
See ADR-0006.
Dirichlet conditions on hanging meshes (3D). A coarse edge can lie in a boundary surface
that its own cells touch only along that edge, the surface facets next to it belonging to refined
neighbours. Its half edges on the surface are slaves of its DoFs, so a condition on the surface
must prescribe them: the Dirichlet sets add the hanging parent edges whose two half edges lie on
the facets (EntitySet::of_boundary_facets, used by dirichlet_values,
tangential_dirichlet_values and homogeneous_dirichlet). The prescribed values are then closed
under the constraints (tests/unit/assembly/test_dirichlet.cpp).
Verification (tests/unit/assembly/test_hanging_constraints.cpp, test_prolongation.cpp):
random constrained vectors are continuous / tangentially continuous across all hanging
facets of twice-refined corners in 2D and 3D for \(p \le 4\) / \(3\) and for random per-cell
orders; the number of constrained DoFs equals the count of fine-entity DoFs; a quadratic
field of \(\mathrm{ND}_3\) is solved exactly by physics::Scattering on the hanging mesh with
vanishing residual estimate; prolongated functions agree with the originals pointwise and
satisfy the constraints of the refined mesh.
Adaptive p-refinement on a smooth solution (tests/convergence/adaptive_p_refinement.cpp)
Plane wave with \(k = 6\) on a \(4 imes 4\) mesh of the unit square, exact trace on the boundary, Dörfler marking ($ heta = 0.8$) of the residual indicators, \(p \leftarrow p + 1\) on the marked cells (minimum rule on the shared edges): the error decays like \(\exp(-0.41\sqrt{N})\), from \(1\) at \(56\) DoFs (\(p = 1\)) to \(2.5\cdot 10^{-8}\) at \(2694\) DoFs (\(p \le 9\)) in 16 steps; the effectivity index stays within \(60 \dots 118\) (its constant grows with \(p\)). The transferred solution agrees pointwise with the previous one in every step.
hp-adaptivity on the L-shape (tests/convergence/adaptive_hp_refinement.cpp, test #7)
Same problem, starting from \(p = 1\) on 24 cells, Dörfler \(\theta = 0.5\), decision by error prediction, p-spreading, hanging-node h-refinement:
| DoF | 44 | 1 300 | 4 100 | 8 000 | 11 800 | 16 700 | 21 800 | 26 300 |
|---|---|---|---|---|---|---|---|---|
| \(\|E - E_{hp}\|_{H(\mathrm{curl})}\) | \(6.2\cdot10^{-1}\) | \(2.9\cdot10^{-2}\) | \(2.4\cdot10^{-3}\) | \(9.1\cdot10^{-4}\) | \(3.9\cdot10^{-4}\) | \(1.6\cdot10^{-4}\) | \(9.8\cdot10^{-5}\) | \(6.2\cdot10^{-5}\) |
Fit \(\exp(-b N^{1/3})\) over the last ten steps: \(b = 0.28\); the algebraic slope for \(N \ge 4000\) is \(-2.2\), steeper than any fixed-\(p\) h-adaptivity with \(p \le 4\) reaches, and the error at \(12\,000\) DoFs is \(2.7\times\) below the \(p = 2\) h-adaptive run. The final mesh has 20 levels of geometric grading at the corner with \(p = 2\) in the innermost rings and \(p\) up to \(6\)–\(7\) outside; the corner cells are never p-refined; the effectivity index stays between \(2\) and \(4\). Beyond \(\approx 40\,000\) DoFs the error stalls near \(10^{-5}\): the direct solver's accuracy on the graded high-order system (M6).
Plasmonic wedge (tests/convergence/plasmonic_wedge.cpp, test #7 second part)
A metal quadrant (\(\varepsilon = -9 + 1.2i\), the quadrant \(x > 0\), \(y < 0\) of \([-1,1]^2\)) meets vacuum at a \(90^\circ\) corner. The quasi-static corner field \(E = \nabla(r^\nu\Phi(\psi))\) with piecewise cosines \(\Phi\) and the complex exponent \(\nu = 0.5726 - 0.0149i\) from \(\varepsilon_2\tan(3\pi\nu/4) + \varepsilon_1\tan(\pi\nu/4) = 0\) is curl-free and satisfies the interface conditions exactly, so it solves \(\nabla\times\nabla\times E - k^2\varepsilon E = -k^2\varepsilon E\) with its own trace as boundary data: a manufactured solution with a material corner singularity \(|E| \sim r^{-0.43}\), stronger than the PEC corner (\(r^{-1/3}\)). Uniform refinement would converge like \(N^{-0.29}\); the hp loop (Dörfler \(0.5\), error prediction, from \(p = 1\) on 32 cells) reaches an error of \(1.3\cdot10^{-3}\) at \(21\,000\) DoFs from \(10.8\) on the start mesh, with \(b = 0.28\) in \(\exp(-bN^{1/3})\) over the last ten steps, an algebraic slope of \(-2.0\) for \(N \ge 4000\), the corner cells h-refined in 112 of 116 decisions and the effectivity index between 1 and 5.3.
Adaptive h-refinement on the L-shape (tests/convergence/adaptive_h_refinement.cpp)
\(E = \nabla(r^{2/3}\sin\tfrac{2\theta}{3})\) on \([-1,1]^2 \setminus [0,1]\times[-1,0]\) with \(k = 1\) and the exact trace on the boundary, Dörfler marking with \(\theta = 0.5\):
| uniform, \(p = 2\) | adaptive, \(p = 1\) | adaptive, \(p = 2\) | |
|---|---|---|---|
| rate in \(N\) (last steps) | \(-0.35\) | \(-0.58\) | \(-1.11\) |
| optimal \(-p/2\) | — | \(-0.5\) | \(-1.0\) |
| effectivity \(\eta / \|E - E_h\|\) | 4.1 … 4.5 | 2.1 … 3.8 | 3.4 … 4.7 |
The largest indicator sits at the corner in every step; 16 steps reach \(10^{-3}\) relative error with 12 600 DoFs at \(p = 2\) in 3 s. The singular solution limits uniform refinement to \(N^{-1/3}\) for every \(p\) (the \(h\) part of convergence test #7; the \(hp\) part follows with the \(hp\) decision).
Periodic faces
Bloch-periodic constraints (assembly::bloch_constraints) pair the facets of the two faces one
to one and need identical meshes on both sides. Local refinement breaks this as soon as a
cell at one face is marked, or refined by the one-irregular closure of a neighbour, while
its partner is not. AdaptiveMesh::set_periodic({master_tag, slave_tag, shift}) declares
the pairs; refine (and therefore hp_refine) then mirrors every refinement across the
pair after the closure: facets of one face without a partner of the same position and
size on the other face split the cell owning the nearest coarser partner facet, repeated
until both faces match. The mirrored cells inherit their parent's order like any closure
cell. Unit test: tests/unit/mesh/test_adaptive_periodic.cpp (2D and 3D, marked face
cells, closure next to a face, hp_refine, the constraints of both spaces built after
every step). Since stage 2 of M15 F16 the mirroring is optional: bloch_constraints couples
faces refined to different levels and orders exactly
(maxwell.md), and the residual
estimators include the jump across the Bloch faces, so the cells at the faces are marked and
refined like any other and the orders of the paired cells need not agree (case (d) of
conical_grating_hp: ridge off centre, faces refined independently).
Bodies of revolution
The loop runs unchanged on the meridian mesh of the axisymmetric solver: the problem
classes constrain the hanging DoFs of both spaces, and adaptivity::axisymmetric_residual_estimate
is the r-weighted residual estimator of the mode equation. See
axisymmetric.md for the formulas and
the convergence test at a re-entrant PEC edge (exponential in \(N^{1/3}\), \(b = 0.27\)).
Conical incidence
The conical solver takes part in the same loop: ConicalScattering::estimate returns the
indicators of adaptivity::conical_residual_estimate
(error-estimation.md),
ConicalScattering::error the L2 errors of the field and the conical curl against an exact
field, and hp_refine raises the orders of both maps together (the two spaces share the
per-cell order). The Bloch faces of a grating may be refined independently and carry different
orders (the non-matching coupling of M15 F16); AdaptiveMesh::set_periodic keeps them
identical when that is wanted. The convergence tests are conical_hp_corner (manufactured gradient mode at a
re-entrant PEC corner, \(\beta = 1.3\): exponential with \(b = 0.26\), the corner cells h-refined)
and conical_grating_hp (the Ag lamellar grating of M15 F1 under TM at 50° with the layered
background, Bloch faces and PML: \(\Delta R_{-1} = -5\cdot 10^{-6}\), \(\Delta R_0 = -2.2\cdot 10^{-4}\)
at 88 k DoFs where uniform meshes stagnate at \(5\cdot 10^{-3}\); the loop starts at \(p = 4\) because
the energy-norm marking never touches the PML and air cells, see
validation.md).
Goal-driven loop, pre-refinement and the generator. physics::conical_dwr_estimate
(error-estimation.md)
supplies goal indicators for the same marking and hp decision;
adaptivity::refine_at_points(adaptive, points, levels) pre-refines the cells at known
singular points (material corners) before the loop, so the first steps do not spend their
budget on finding them. From Python, hpfem.adaptive_solve(adaptive, factory, observe=…,
goal=…, tolerance=…, max_dofs=…) is a generator of the whole loop: the factory builds and
solves the problem on the current mesh and orders, every step yields DoFs, \(\eta\), the
observables (reflectances, fluxes) and their change since the previous step, the goal value
and the estimated goal error, and the loop stops when all changes and the goal error are
below the tolerance (AdaptiveStep.converged), the "stop at tolerance" mode of the GUI
(python.md).
Data-structure consequences (why M1/M2 must prepare this)
- DoF numbering is entity-based (vertex → edge → face → cell) with a per-entity order, not a per-cell block of fixed size.
- The mesh keeps parent/child relations and level numbers (
mesh::AdaptiveMesh). fespace::Constraints(slave = Σ c_i master_i) serves Bloch-periodic boundaries and hanging nodes alike (appendcombines them) and reduces the assembled system by \(P^H A P\);physics::Scatteringimposes the Dirichlet data on the free DoFs after the reduction.