Linear solvers
Direct solvers (solvers/linear_solver.hpp)
All assembled systems are complex, sparse and (for the curl–curl problem) complex
symmetric but not Hermitian, so a general LU factorisation is used. LinearSolver
factorises once and solves for any number of right-hand sides (parameter sweeps, the
adjoint of the goal-oriented estimator, several incident fields on one mesh).
Backend (DirectSolverBackend) |
Library | When |
|---|---|---|
kSparseLu |
Eigen SparseLU, COLAMD ordering |
always available; 2D up to a few \(10^5\) unknowns |
kMumps |
MUMPS (multifrontal, zmumps, sequential build) |
HPFEM_ENABLE_MUMPS; 3D and large 2D systems, many right-hand sides |
kCudss |
NVIDIA cuDSS (LU on the GPU, factors stay on the device) | HPFEM_ENABLE_CUDA + the hpfem_gpu library; opt-in, for many solves of one factorisation (time stepping, Arnoldi, sweeps) |
kAuto |
cuDSS for systems of at least HPFEM_GPU_MIN_UNKNOWNS unknowns (default 10 000) when its library and a GPU are present, otherwise MUMPS if compiled in, otherwise SparseLU; chosen in factorize |
the default of solve_direct and every solver field |
make_direct_solver(backend) returns the solver, available(backend) /
available_backends() tell what this build offers, solve_direct(A, b, backend) does
both steps. physics::ScatteringSetup::solver selects the backend of a scattering problem
(and of its DWR adjoint).
MUMPS backend
src/solvers/mumps_solver.cpp wraps zmumps_c: the matrix is passed as centralised
assembled COO input (ICNTL(5) = 0, ICNTL(18) = 0, 1-based indices), analysis and
numerical factorisation run in one call (job = 4) with the automatic ordering choice
(ICNTL(7) = 7, METIS/SCOTCH/PORD as available in the library) and 30 % memory
relaxation, solves run in place (job = 3), all library output is switched off and errors
are reported from INFOG(1) / INFOG(2) as hpfem::Error. The sequential library ships
its own MPI stub, whose MPI_Init is called once.
Build support (cmake/FindMUMPS.cmake, option HPFEM_ENABLE_MUMPS, preset mumps):
- Ubuntu / Debian:
apt-get install libmumps-seq-dev libmumps-headers-dev(libzmumps_seq,libmumps_common_seq,libpord_seq,libmpiseq_seq, headers in/usr/include). The CI runs the whole test suite with this build. - MSYS2 (Windows):
pacman -S mingw-w64-ucrt-x86_64-mumps(modulemumps-zso, sequential complex double, with OpenBLAS, METIS and SCOTCH). The DLLs live inC:\msys64\ucrt64\bin; the test targets get that directory prepended toPATH(MUMPS_BIN_DIR), programs started by hand need it onPATH.
cuDSS backend (GPU)
The library is built with MinGW GCC (Windows) or GCC / Clang (Linux), which cannot link
objects produced by nvcc and MSVC. The GPU solver therefore lives in the separately built
shared library hpfem_gpu (gpu/, see gpu/README.md) with a pure C interface
(gpu/include/hpfem_gpu.h: opaque solver handle, status codes, last-error message, CSR with
64-bit indices and interleaved complex values in, solutions out, factorise once / solve many
right-hand sides, re-factorisation on the same handle). src/solvers/cudss_solver.cpp loads
it at run time (LoadLibrary / dlopen), checks the API version and the presence of a CUDA
device, and wraps it as a LinearSolver. Nothing of CUDA is needed to build the library;
without the DLL or a GPU, available(kCudss) is false, make_cudss() throws an error that
names the search paths, and kAuto falls back to MUMPS / SparseLU, so CI (no GPU) builds and
tests the option as well.
Automatic choice. kAuto decides in factorize from the number of unknowns: cuDSS from
HPFEM_GPU_MIN_UNKNOWNS unknowns on (CMake cache variable, default 10 000; the environment
variable of the same name overrides it at run time, 0 means always, a negative value never),
otherwise MUMPS if compiled in, otherwise SparseLU. The GPU library is loaded, and cuDSS
started (0.3–0.6 s once per process), only when cuDSS is actually chosen, so small problems
pay nothing. The default comes from bench_backend_threshold
(benchmarks/results/2026-10-03-VR-backend-threshold.json, Maxwell operator of a plane
wave, p = 2, RTX 3090 against sequential MUMPS): the factorisations draw level at about
10 000 unknowns in 2D (36 ms against 34 ms at 10.4 k) and in 3D (0.26 s against 0.23 s at
9.3 k), above that cuDSS leads (0.11 s against 0.17 s at 41 k, 0.76 s against 1.18 s at 164 k
in 2D; 0.52 s against 0.77 s at 21 k, 1.8 s against 4.9 s at 70 k in 3D), and a single solve is
faster on the GPU from about 5 000 unknowns on (0.5 ms against 2.7 ms at 23 k, 5 ms against
248 ms at 70 k in 3D). Below the threshold every factorisation takes less than 40 ms, so the
choice is immaterial there. cuDSS pivots statically and the library refuses a factorisation
in which pivots were perturbed (see above); kAuto then logs a warning once and factorises
the system, and the later systems of the same solver object, with MUMPS / SparseLU. Before
the diagonal equilibration (next paragraph) the hp-adaptive systems with hanging nodes
and high orders tripped this (103 of 16 359 and 2 639 of 19 723 pivots in the L-shape test,
28 / 1 591 / 16 356 in the plasmonic wedge); accepting such factorisations with the iterative
refinement of cuDSS had lost accuracy at a few thousand perturbed pivots, so the refusal
stays as the safety net.
Scaling and equilibration. The GPU library factorises \(D\,(sA)\,D\) with the global
scale \(s = 1/\max|a_{ij}|\) (the tiny-pivot threshold of cuDSS is absolute) and the diagonal
equilibration \(d_i = 1/\sqrt{|a_{ii}|}\), which keeps the complex symmetry and evens out the scales of edge and
high-order interior functions; right-hand sides and solutions are rescaled inside the
library. The diagonal is used whenever it is nonzero — after a two-sided scaling a tiny
diagonal is exactly the right scale of its row, and a row-norm fallback for "small"
diagonals (tried first) destroyed the equilibration on a synthetic test with scales
\(10^{-8} \ldots 10^{8}\); only a zero diagonal (possible for eliminated DoFs in block
structures) falls back to the row norm, a zero row to \(d_i = 1\). On the two hp tests the
equilibration removes every perturbed pivot (0 in all steps
against up to 16 356 without it) with convergence identical to the CPU backends, while the
matching and reordering options of cuDSS changed nothing. With it, the complete C++ and
Python test suites run with every system on cuDSS (HPFEM_GPU_MIN_UNKNOWNS=0) without a
single refused factorisation. HPFEM_GPU_EQUILIBRATE=0 switches it off for comparisons.
The gain at the size of the tests is coverage, not speed: the L-shape loop (21 steps up to
21 623 DoFs) takes 1.42 s on SparseLU, 1.67 s with the default threshold and 1.89 s with
every system on cuDSS, because the one-off cuDSS start (0.3–0.6 s) and the transfer per
step are not amortised by factorisations of 10–20 k unknowns; the GPU pays off once the
adaptive systems grow past a few 10⁴ unknowns, as in the threshold measurement above. Results of different backends agree only to about \(10^{-12}\)
relative (different orderings, pivoting and summation orders); tests therefore compare
against references with a tolerance and never rely on bitwise equality between runs or
backends.
Numerics: cuDSS factorises the general complex matrix (CUDSS_MTYPE_GENERAL; the interface
also offers the complex-symmetric LDL^T path for later use). hpfem::SparseMatrix is already
CSR with int64 indices, so the matrix is uploaded without conversion. cuDSS judges tiny
pivots by an absolute threshold and perturbs them instead of failing, which an SI-scaled
system (Newmark operator with entries around \(10^{-15}\)) trips on every pivot; the DLL therefore
factorises \(sA\) with \(s = 1/\max|a_{ij}|\), rescales every solution, and reads the perturbation
count after the factorisation to report a genuinely singular matrix (hpfem::Error) rather
than returning a wrong solution.
Finding the DLL at run time, in this order: environment variable HPFEM_GPU_DLL, the path
compiled in from the CMake cache variable HPFEM_GPU_DLL, hpfem_gpu.dll next to the
executable, the plain name on the loader path. The cuDSS runtime DLLs must be on PATH
(HPFEM_GPU_BIN_DIR prepends them for the tests, like MUMPS_BIN_DIR). cudss_status()
tells why the backend is or is not usable. Rationale and measurements: ADR-0008. In short
(RTX 3090, complex double, 24 CPU threads): the GPU factorisation is 1.8–2.3× faster than
sequential MUMPS on a 163k-unknown Newmark operator and a 70k-unknown 3D PML scattering
system, and a repeated solve of one factorisation is 30–140× faster (milliseconds instead of
tenths of a second), which is where time stepping, Arnoldi iterations and sweeps spend their
time; residuals are \(10^{-13}\) to \(10^{-15}\) on both.
Complex-symmetric systems (solvers::Symmetry)
The curl–curl operators are complex symmetric (\(A = A^T\), not Hermitian) as long as the
material tensors are symmetric: with PML, after the symmetric Dirichlet elimination, after
static condensation and with hanging-node constraints (\(P^T A P\)); Bloch phases break the
symmetry. make_direct_solver(backend, Symmetry::kComplexSymmetric) (and solve_direct,
make_cudss, make_mumps) lets the backend exploit it: cuDSS receives the upper triangle
(CUDSS_MTYPE_SYMMETRIC, LDLᵀ) and MUMPS runs with SYM = 2 on the upper triangle, both
with about half the factor work and memory; SparseLU ignores the flag. The caller
guarantees the structure — the library does not check it in release builds (Debug builds
of the cuDSS path verify asymmetry(A) < 1e-10; upper_triangle(A) and asymmetry(A) are
public helpers). The problem classes do not guess: they pass Symmetry::kDetect, and a
backend that can exploit the structure (cuDSS, MUMPS) measures asymmetry(A) once per
factorisation (one pass over the nonzeros with a binary search of the mirrored entry per
row, no copy, far below the cost of the factorisation), logs the result at debug level and
takes the LDLᵀ path when the relative asymmetry is at most \(10^{-12}\) — using the upper
triangle as given, never symmetrising, so results stay backend-independent. SparseLU skips
the check. Scattering, ScatteringOperator, TimeDomain, the complex shift-invert
eigensolvers (Resonance, BandStructure, the axisymmetric problems),
AxisymmetricScattering, Thermal, the hypercircle dual solve and the goal-oriented
adjoint pass kDetect, so Bloch phases or non-symmetric material tensors automatically get
the general factorisation (unit test with a hanging-node and a Bloch-reduced operator);
kAuto forwards it to whichever backend it picks. kDetect is therefore the default of the
problem classes; kGeneral and kComplexSymmetric are explicit overrides for callers of
make_direct_solver / solve_direct who know the structure (or want to skip the check).
A solver object may be re-factorised with a matrix of the other kind (sweeps that change
the operator); MUMPS, whose SYM is fixed at initialisation, is then re-initialised, the
other backends switch silently. Measured with
bench_backend_threshold (benchmarks/results/2026-10-03-VR-backend-symmetry.json, same
problems as above): the LDLᵀ factorisation takes 0.85 s instead of 1.16 s on MUMPS and
0.52 s instead of 0.72 s on cuDSS at 164 k unknowns in 2D, 2.4 s instead of 4.7 s (MUMPS)
and 1.24 s instead of 1.84 s (cuDSS) at 70 k unknowns in 3D; below about 20 k unknowns the
gain vanishes on the GPU, solves and residuals are unchanged.
Solves with the transposed matrix (solve_transposed)
The adjoint systems of the sensitivities (physics/sensitivity.hpp, ADR-0011, ADR-0012) need
\(A^\top z = q\) (transposed, not conjugated) on the factorisation of the forward problem:
solve_transposed / solve_transposed_many. A complex-symmetric factorisation (the LDLᵀ
paths of MUMPS and cuDSS, see the previous section) solves with \(A\) itself, since
\(A^\top = A\). For a general matrix — Bloch phases, non-symmetric material tensors — SparseLU
uses its factors transposed (Eigen's SparseLU::transpose() view: with \(P_r A P_c = LU\) it
solves \(U^\top\) and \(L^\top\) in reverse order and swaps the permutations), and MUMPS solves
with ICNTL(9) = 2 on the same factors. cuDSS has no transposed solve: the backend keeps the
general matrix and factorises \(A^\top\) on the first transposed solve after each
(re)factorisation, reusing the analysis of the previous \(A^\top\) through refactorize; this
doubles the device memory for general systems only. For a reciprocal medium with Bloch phases
\(A(k)^\top = A(-k)\), so the adjoint of a Bloch problem is the primal problem at \(-k\).
Verified for every backend on a random non-symmetric system (residual \(10^{-10}\), the
transposed solve after a refactorize sees the new values) and on a complex-symmetric one
(test_linear_solver.cpp, python/tests/test_linear_solver.py).
Factors larger than the device memory (hybrid memory mode)
cuDSS can keep the factors (partly) in host memory. The GPU library decides this per
factorisation: after the analysis it reads cuDSS's peak-memory estimates and, if the device
peak exceeds about 90 % of the free device memory, repeats the (cheap) analysis in the hybrid
memory mode with the device limit set to the free memory minus a reserve; if even the hybrid
estimate exceeds the free host memory the factorisation fails with an Error that names
the numbers, and kAuto falls back to MUMPS. HPFEM_GPU_HYBRID=1 / 0 forces the mode
(the self-test runs both). The mode, the estimates and the memory held are reported by
LinearSolver::details() (also for MUMPS: entries in the factors and host memory), the
first hybrid factorisation of a process is logged at info level, and the backend name
carries "hybrid memory". The C interface grew to API version 2 for this
(hpfem_gpu_factor_info2); the loader accepts version 1 libraries without the hybrid
information. bench_hybrid_memory records factor size, mode and time against MUMPS on the
3D Maxwell operator at growing size (benchmarks/results/2026-10-03-VR-hybrid-memory.json,
RTX 3090 with 24 GB, 128 GB host, sequential MUMPS, both LDLᵀ):
| n (p = 2) | unknowns | factors (entries) | MUMPS factorise / solve | cuDSS mode | cuDSS factorise / solve |
|---|---|---|---|---|---|
| 20 | 316 k | 0.20 G | 24 s / 3.7 s | device (4.2 GB) | 8.8 s / 15 ms |
| 20 | 316 k | 0.20 G | hybrid forced (1.3 GB device, 3.4 GB host) | 9.3 s / 39 ms | |
| 24 | 543 k | 0.45 G | 52 s / 11 s | device (8.7 GB) | 21 s / 48 ms |
| 28 | 858 k | 0.86 G | 106 s / 20 s | device (16 GB) | 45 s / 44 ms |
| 32 | 1.28 M | 1.43 G | 186 s / 34 s | hybrid, automatic (5.2 GB device, 28 GB host) | 105 s / 1.9 s |
The hybrid factorisation keeps about half of MUMPS's time where the factors (23 GB) no longer fit the device, and the solve stays an order of magnitude faster; forcing the hybrid mode where the factors would fit costs about 6 % in the factorisation and a slower solve.
Time stepping on the device (solvers::DeviceStepper)
With the cuDSS backend the transient solver keeps the whole Newmark loop on the GPU:
solvers::DeviceMatrix uploads the stiffness and damping matrices once (own warp-per-row
complex SpMV kernel, so the GPU library still depends on the cuDSS DLL alone), and
solvers::DeviceStepper keeps the state \(u, v, a\) on the device and performs per step the
two products, the fused vector updates and the solve with the factorised Newmark operator
without host contact; only the scalar of the current load crosses per step. The recursion is
exactly that of TimeDomain::step (predictors, \(a = K^{-1}(f - Cv - Su)\), correctors), so
the results agree with the host loop to round-off (self-test and unit test: \(10^{-16}\) over
50–60 steps). TimeDomain::run uses it automatically whenever its operator was factorised by
cuDSS (also through kAuto above the threshold) and downloads the state only for an
observer call and at the end; HPFEM_GPU_STEPPER=0 keeps the host loop for comparisons.
C interface: API version 3 (hpfem_gpu_matrix_*, hpfem_gpu_stepper_*); older libraries
still load without these objects. Measured (bench_solver_integration, PEC cavity n = 128, p = 2, 164 k
DoFs, 200 steps, after the reduced-state loop of TimeDomain::run): 60 ms per step on
SparseLU, 43 ms on MUMPS, 8.3 ms with cuDSS and the host loop, 3.5 ms with the loop on the
device — 12× faster than MUMPS; the energy drift stays at \(10^{-14}\).
Arnoldi with the Krylov basis on the device (solvers::DeviceArnoldi)
The complex shift-invert Arnoldi of complex_eigenpairs_near (resonances) and
complex_eigenpairs_near_gauged (band structures, bodies of revolution) spends its time in
the solve \(K^{-1} B v_j\), the gauge projection \(P w = w - G (G^H B G)^{-1} G^H B w\) and the
orthogonalisation against the Krylov basis. With the basis on the host every iteration
uploads a vector, downloads the solution and streams the whole basis (\(n \times k\) complex
numbers) through the host memory twice; measured at 92 k DoFs the Gram–Schmidt against 60
vectors took 30–50 ms against 1.7 ms for the device solve. solvers::DeviceArnoldi
therefore keeps \(V = [v_0, \dots, v_k]\) on the device: one iteration computes
\(w = P K^{-1} B v_j\) there (cuDSS solve, the library's sparse products, the gauge solve by
a second cuDSS factorisation), orthogonalises by classical Gram–Schmidt applied twice (a
reduction kernel for \(h_{0..j,j} = V_j^H w\), an update kernel for \(w \gets w - V_j h\)), and
returns only the \(j + 1\) Hessenberg entries and \(h_{j+1,j} = \|w\|\) to the host. Restart
vectors and Ritz vectors are combinations \(V_m c\) formed on the device. The host keeps the
control flow: Hessenberg eigenproblem, convergence test, restarts. Host and device run the
same algorithm through one KrylovBasis interface in complex_eigen_solver.cpp (the host
basis uses the same two-pass classical Gram–Schmidt as two GEMVs), so their Ritz values
agree to round-off (self-test: \(2 \cdot 10^{-15}\) in the Hessenberg columns; unit tests:
\(10^{-12}\) against the host recursion, eigenvalues of Resonance and BandStructure
device against host to \(10^{-10}\)).
The eigensolvers take the device basis whenever the shifted matrix is factorised by cuDSS
(also through kAuto above the threshold); in the gauged variant the gauge matrix is then
factorised by cuDSS as well, even though it is small. Before anything is uploaded the
memory of the basis and the matrices (\(16\,n\,(k + 4)\) bytes plus the CSR data) is checked
against the free device memory; if it does not fit, or HPFEM_GPU_ARNOLDI=0 is set, the
basis stays on the host with a warning and the device solves are used as before. C
interface: API version 4 (hpfem_gpu_matrix_create_rect, hpfem_gpu_arnoldi_*); older
libraries still load without the object.
Measured (bench_device_arnoldi, benchmarks/results/2026-10-04-VR-device-arnoldi.json,
RTX 3090, p = 2, 6 eigenvalues, 24 Krylov vectors; factorisation included, assembly
excluded):
| Problem | DoFs | SparseLU, host basis | cuDSS, host basis | cuDSS, device basis |
|---|---|---|---|---|
Resonance, closed PEC square |
92 k | 2.26 s | 0.80 s | 0.65 s |
| 369 k | 57.9 s | 5.48 s | 3.55 s | |
BandStructure, empty lattice (gauged) |
92 k | 12.3 s | 2.37 s | 1.54 s |
| 369 k | 104 s | 9.15 s | 6.40 s |
The device basis saves 20 % at 92 k and 35 % at 369 k unknowns for the ungauged problem, where the factorisation and the solves already dominate, and 30–35 % for the gauged one (the three host products and two transfers per projection disappear). The remaining time is the factorisation (about 0.3 s / 1.5 s) and the solves themselves.
Where the backend is applied repeatedly
Every problem class with a solver field passes it on, so kCudss can be selected where
one factorisation serves many solves: physics::TimeDomain (one solve per time step),
physics::Resonance and physics::BandStructure (complex_eigenpairs_near*, one solve per
Arnoldi step), physics::ScatteringOperator (solve_many for sweeps) and
physics::PropagatingMode (WaveguideSetup::solver). The real shift-invert solvers
gauged_curl_curl_eigenpairs and generalized_eigenpairs_near accept the backend as well:
kAuto and kSparseLu keep Eigen's real SparseLU (real arithmetic, half the memory, the
behaviour of earlier versions), kMumps / kCudss factorise the complexified shifted matrix
and take the real part of every solve. benchmarks/solver_integration.cpp measures these
four paths with every available backend
(benchmarks/results/2026-10-03-VR-gpu-integration.json, RTX 3090, 24 threads, MUMPS
sequential, cuDSS warm):
| Path | Size | SparseLU | MUMPS | cuDSS |
|---|---|---|---|---|
TimeDomain setup (assembly + 2 factorisations) / one Newmark step |
164 k DoFs | 11.4 s / 74 ms | 3.1 s / 58 ms | 1.8 s / 15 ms |
ScatteringOperator factorisation / 8 pure solves one by one / batched solve_many |
194 k DoFs | 13.0 s / 1.21 s / 0.91 s | 2.0 s / 0.46 s / 0.14 s | 1.24 s / 40 ms / 24 ms |
| 8-angle sweep including load assembly, one by one / batched | 194 k DoFs | 1.39 s / 1.06 s | 0.76 s / 0.76 s | 0.28 s / 0.26 s |
Resonance, 6 modes (complex Arnoldi) |
92 k DoFs | 2.9 s | 2.1 s | 1.1 s |
gauged_curl_curl_eigenpairs, 6 eigenvalues (real Lanczos; MUMPS / cuDSS complexified) |
92 k DoFs | 2.6 s | 3.0 s | 2.6 s |
The time step and the sweep are now bounded by the CPU work around the solve (two sparse products and the restriction / expansion per step, the load assembly per angle); the real Lanczos does not profit from a complexified GPU factorisation at this size.
Verification
tests/unit/solvers/test_complex_eigen.cpp compares DeviceArnoldi with the host
recursion on the same factorisation (Hessenberg columns, basis, Ritz vectors, restart
vector) and the eigenvalues of both bases with and without gauge; test_resonance.cpp and
test_band_structure.cpp do the same through Resonance and BandStructure.
tests/unit/solvers/test_linear_solver.cpp solves a random sparse complex system with
every available backend to \(10^{-10}\), reuses the factorisation for a second right-hand side,
checks the one-shot interface, the error reporting (singular, non-square, wrong size,
solve before factorisation), that solve_many equals column-wise solve and that
kAuto picks MUMPS (or SparseLU) below the GPU threshold and cuDSS from the threshold on
(HPFEM_GPU_MIN_UNKNOWNS set in the test); cuDSS joins the backend loop whenever its library
loads and a GPU is present. With the mumps preset the complete convergence suite runs on MUMPS (all
tolerances unchanged). The DLL itself has a stand-alone self-test (gpu/selftest, run by hand
on a GPU machine): Helmholtz and random systems, general and complex-symmetric input, several
right-hand sides, in-place solves, re-factorisation, error paths.
Static condensation (assembly/condensation.hpp)
The interior (cell-bubble) functions of the hierarchical bases have support in one cell
only, so their DoFs couple to nothing but the cell's own exterior DoFs. With the local
system split into exterior (E) and interior (B) blocks, StaticCondensation assembles the
Schur complement
cell by cell (a dense LU of \(K_{BB}\) per cell, \(K_{BB}^{-1}K_{BE}\) and \(K_{BB}^{-1}f_B\) are
kept) and recovers \(u_B = K_{BB}^{-1}(f_B - K_{BE}u_E)\) after the solve. The global
numbering is unchanged: the interior rows of the condensed matrix are identity rows with
zero load, so Dirichlet elimination and the hanging-node / Bloch constraints — which never
involve interior DoFs — apply exactly as before, and recover completes the solution.
assemble_maxwell_operator (\(A = S - k^2 M\) with the load in one pass) and the
per-cell-form assemble_h1 take an optional StaticCondensation; physics::Scattering
condenses by default (ScatteringSetup::condense). Not condensed: the eigenproblems (the
pencil \(S - \lambda M\) is not linear in the unknown block) and the DWR adjoint.
Interior DoFs per cell: H1 \((p-1)(p-2)/2\) (2D), Nédélec \(p(p-1)\) (2D), i.e. for \(p = 4\) a third of the Nédélec DoFs of a triangle and for \(p = 6\) half of them. The direct solver then factorises a matrix whose coupled part lives on the entity DoFs only.
Verification (tests/unit/assembly/test_condensation.cpp): H1 (2D \(p = 3\)–\(5\), 3D \(p = 4\))
and Maxwell (2D \(p = 2\)–\(4\), 3D \(p = 3\)) solutions with Dirichlet / PEC data agree with the
full systems to \(10^{-10}\), the condensed matrices have fewer nonzeros and identity interior
rows, a Scattering solve on a hanging mesh with PEC and prescribed traces agrees with the
uncondensed solve, and singular interior blocks are reported.
Parallel assembly (core/parallel.hpp)
With OpenMP (HPFEM_ENABLE_OPENMP, on by default when the compiler supports it) the cell
loops of assemble_maxwell, assemble_maxwell_operator and assemble_h1 and the cell and
facet loops of the residual estimator and the weighted residual run in parallel
(parallel_for, dynamic scheduling). Every thread owns its triplet buffer, right-hand
side and quadrature-rule cache; the buffers are concatenated afterwards and summed by the
sparse assembler, the facet contributions are stored per facet and accumulated into the
cells serially, and StaticCondensation guards its recovery table with a mutex. The results
are bit-for-bit reproducible up to the summation order of duplicate entries
(tests/unit/assembly/test_parallel.cpp compares 1 and all threads to \(10^{-12}\)). The
per-cell form factories must be reentrant (the ones of physics::Scattering are).
hpfem::num_threads() / set_num_threads() control the thread count; on MinGW the OpenMP
runtime is linked statically like the rest of the GCC runtime.
Parameter sweeps (physics/sweep.hpp, solvers/reduced_basis.hpp)
Angle sweeps, one factorisation. At a fixed frequency the operator of a scattering
problem does not depend on the incident field or the current. physics::ScatteringOperator
assembles the operator once (static condensation, hanging / Bloch constraints, Dirichlet
elimination of the problem's PEC and incident facets with assembly::DirichletElimination,
which keeps the eliminated columns \(A_{:,D}\) for later loads) and factorises it with the
chosen backend; solve(incident, current) then only assembles the new load
(assemble_maxwell_load), condenses it (StaticCondensation::condense_load, with the stored
\(K_{BB}^{-1}\) and \(K_{EB}\) per cell), reduces it by the constraints, applies the new Dirichlet
values and recovers the interior unknowns (recover(x, load)).
ScatteringOperator::solve_many(incidents, current) assembles and eliminates the loads of
several incident fields and applies the factorisation to all of them in one
LinearSolver::solve_many call (one device round trip on the cuDSS backend); the free
functions solve_many and plane_wave_sweep wrap this for lists of incident fields / wave
vectors — the angle sweep of a scatterometry measurement costs one factorisation plus one
batched triangular solve.
The loads of all incident fields are assembled in one pass over the cells
(assembly::assemble_maxwell_loads): geometry, quadrature rule and basis functions of a cell
are evaluated once, only the source values differ per field; the cells are coloured so that
no two cells of a colour share a DoF and every colour scatters straight into the \(n imes k\)
result in parallel (no per-thread copies of it). The Dirichlet data of the fields comes from
one pass over the facets as well (Scattering::dirichlet_many, the multi-function
interpolate: basis traces and Gram matrices once, one right-hand side column per field).
Both agree with the single-field paths to round-off (\(10^{-14}\) in the unit tests). Measured
(bench_sweep_shares, benchmarks/results/2026-10-04-VR-sweep-shares.json, unit square
p = 3, 194 k DoFs, 100 incident fields, cuDSS): the sweep took 3.2 s with the loads
assembled one by one (26 ms per field, of which 20 ms the load assembly and 5 ms the
Dirichlet data) and takes 0.78 s batched (loads 0.075 s, Dirichlet data 0.032 s, the
batched solve 0.27 s with 45 ms upload, 166 ms device solve and 52 ms download by
HPFEM_GPU_TIMING=1); the same saving applies to every backend. Right-hand sides and
solutions resident on the device would have saved only the 0.1 s of transfers and were not
built.
Frequency sweeps, reduced basis. solvers::ReducedBasis collects snapshots
(full solutions at a few parameter values) into an orthonormal basis \(V\) (modified
Gram–Schmidt with re-orthogonalisation, dependent snapshots dropped) and provides the
Galerkin projections \(V^H A V\), \(V^H b\) and the lift \(u \approx V y\). For an affine operator
such as \(A(k) = S - k^2 M\) (no dispersive materials, PML frozen at a reference
wavenumber) the projections of \(S\) and \(M\) are formed once and every further frequency is
a dense solve of the size of the basis; DirichletElimination with unit_diagonal = false
on \(M\) keeps the eliminated system affine. This is the hook for the reduced-basis and
dispersion work of later milestones; frequency-dependent PML and materials still need a
full re-assembly per frequency.
Verification (tests/unit/physics/test_sweep.cpp): solutions of three incidence angles by
one factorisation agree with individual solves to \(10^{-9}\) for the total- and
scattered-field formulations with and without condensation on a hanging mesh with PEC
and prescribed traces; the reusable elimination and the condensed loads reproduce the
one-shot pipeline; a basis of five frequency snapshots (\(k = 2 \dots 4\) on a \(6 \times 6\),
\(p = 3\) box with the exact trace) reproduces full solutions at four other frequencies
within \(10^{-2}\) and the snapshot frequencies to \(10^{-8}\).
Frequency sweeps of the conical solver (physics/conical_sweep.hpp, M15 F8)
The system of ConicalScattering is \(A(\omega, \beta) = S(\beta) - k_0^2 M(\varepsilon)\) with
\(S(\beta) = S_0 + \beta S_1 + \beta^2 S_2\) — the gradient kernel of the conical forms is
quadratic in \(\beta\) — and \(M = \sum_g \varepsilon_g M_g\) over the groups of cells that share
a material. ConicalSweep assembles \(S_0\), \(S_1\), \(S_2\) (from \(S(0)\) and \(S(\pm k_0)\)) and
the unit-permittivity masses \(M_g\) of the cells outside the PML once, as value arrays over
one fixed union pattern; a point of the sweep then costs a few vector operations for the
combination, the assembly of the PML cells (their stretch depends on \(\omega\)) and of the
source cells (the load), a value-only constraint reduction through a scatter map onto the
fixed pattern of \(P^H A_{ff} P\) (built at the first point; a point whose PML structure differs
falls back to the generic path and rebuilds the map) and a
refactorisation: LinearSolver::refactorize(matrix) factorises a matrix with the pattern
of the previous one reusing its symbolic analysis — MUMPS runs its numerical phase only
(job 2), SparseLU keeps the column permutation, cuDSS runs CUDSS_PHASE_REFACTORIZATION
through hpfem_gpu_refactorize (GPU library API version 5, scaling and equilibration
recomputed from the new values) — and falls back to factorize for a new pattern. Every
point may change \(\omega\), \(\beta\), the materials of the groups (dispersion), the incident
field and the Bloch phases; mesh, orders, PEC tags, periodic pairs, PML geometry and the
cell-to-material grouping stay. Results agree with ConicalScattering::solve to rounding
amplified by the conditioning (measured \(10^{-13}\) to \(5\cdot10^{-9}\)).
Complex Bloch phases and the array scanning (M17, ADR-0013 §5). For a phase off the unit
circle (a complex \(k_x\) on the contour of the array scanning) the reduction uses the test space
of the constraints, \(Q^H A P\) with the test phases \(1/\bar\lambda\), as ConicalScattering
does: every term of the scatter map carries its trial and its test coefficient, and the
pattern is the same as for \(P^H A P\). When consecutive points keep the stretch (ω, the PML's
\(k_0\) and \(\sigma_{max}\), the PML cells' materials), the PML cells are split like the bulk,
\(S_{pml}(\beta) = S_0 + \beta S_1 + \beta^2 S_2\), from the second such point on; a frequency
sweep assembles them per point as before. ConicalSweep::factorize(setup) factorises a point
without its load and solve_loads(loads) solves several full-size loads on it (one multi-rhs
solve); conical_sweep_dipole_power_matrix builds the 3 × 3 power matrix of a Gaussian dipole
from the loads of the three unit moments on the cells near the emitter.
grating.dipole_emission runs its samples on one sweep per thread (sweep=True, the default).
On a homogeneous cell with the SparseLU backend a sample took 0.77 s instead of 1.74 s at
p = 3 (18 k DoFs) and 2.7 s instead of 3.7 s at p = 4 (32 k DoFs, where the numerical
factorisation is 2.1 s of it).
Two changes made along the way help every conical solve: assembly::assemble_conical runs
in parallel over the cells (per-thread triplet buffers) and takes an optional cell subset,
and fespace::Constraints::reduce computes \(P^H A P\) in one scatter pass over the nonzeros
instead of two sparse products (on a 100 k-DoF Bloch-periodic system the products cost more
than the factorisation). hpfem.sweep.solve_sweep(task, values, processes) spreads the
points of any sweep over worker processes with one hpfem thread each, for solvers that scale
poorly with threads. Measured (benchmarks/conical_sweep.cpp,
benchmarks/results/2026-10-06-VR-conical-sweep.json: a dispersive grating on glass with
layered background, PML and Bloch phases, 197 k block DoFs, cuDSS): 2.93 s per point for
the naive loop against 1.37 s for the sweep over 50 points, 2.13×, solutions equal to
\(2\cdot10^{-12}\); the parallel assembler and the scatter reduction alone had made the naive
loop 1.4× faster than before.
Measured (benchmarks/results/2026-10-02-VR.json)
Maxwell plane wave on the unit square, MSYS2 GCC 16 release build, 24 cores, MUMPS 5.9
sequential (bench_assembly_solve):
| n, p | DoFs | nnz (full / condensed) | assembly 1 / 24 threads | factorise SparseLU full / cond. | factorise MUMPS full / cond. |
|---|---|---|---|---|---|
| 64, 2 | 41 216 | 469 k / 258 k | 0.27 s / 0.05 s | 0.38 s / 0.17 s | 0.14 s / 0.10 s |
| 64, 4 | 147 968 | 4.48 M / 1.06 M | 1.57 s / 0.34 s | 10.1 s / 1.10 s | 0.50 s / 0.29 s |
| 128, 3 | 344 832 | 6.89 M / 2.39 M | 2.97 s / 0.56 s | 21.6 s / 4.11 s | 1.33 s / 0.84 s |
Assembly scales 5–7× on 24 threads (the per-thread triplet merge and the sparse compression stay serial); static condensation removes half to three quarters of the nonzeros and cuts the SparseLU factorisation up to 9×; MUMPS factorises the 345k system 16× faster than SparseLU. Solves take 10–100 ms. The Mie example (86k DoFs, p = 3) went from 5 s to 1.5 s with MUMPS and condensation.
Memory estimate (solvers/memory_estimate.hpp)
estimate_memory(dofs, backend, condensed, longitudinal) and estimate_memory(mesh, order,
backend, conical) predict the sizes of one factorisation from the DoF layout alone (M15 F9),
so that a GUI can warn before a run: the DoFs after condensation, the nonzeros of the system
matrix, the entries of the factors and the bytes of both. The nonzeros are
\(r_{d,p}\sum_K n_K^2\) with the overlap ratio of the H(curl) pattern,
\(r_{2,p} = 1 - 0.205/(p + 0.28)\) and \(r_{3,p} = 1 - 0.86/(p + 1.08)\) (exact to 1 % on
structured meshes). The factor entries follow the classical fill-in laws of the orderings:
\(4\,N\log_2 N\) in 2D and \(17\,N^{4/3}\) in 3D for MUMPS (and cuDSS, whose factors live on
the device), \(c_p N^{1.25}\) in 2D with \(c_{1..4} = 6.5, 8.5, 19, 34\) and \(c_p N^{1.63}\) in 3D
with \(c_{1,2} = 2.4, 3.9\) for SparseLU with COLAMD. The constants are fits to
benchmarks/results/2026-10-08-fill-in.jsonl (the Maxwell operator on rectangle(n, n),
p = 1..4, N up to 3.3·10⁵, and box(n, n, n), p = 1..2, N up to 4.1·10⁴; SparseLU and MUMPS
5.9 of this build); the measured factors lie within ±35 % of the fits on these meshes, locally
refined or strongly anisotropic meshes deviate more. Bytes: 20 per matrix nonzero plus 24 per
assembly triplet (alive together at the peak), 20 per SparseLU factor entry, 16 × 1.3 per MUMPS
/ cuDSS entry (integer workspace). LinearSolver::factor_entries() reports the real number
after factorize, which the unit test compares with the estimate.
Eigenvalue solvers
See maxwell.md: Spectra's shift-invert Lanczos / Arnoldi on top
of the SparseLU factorisation of \(S - \sigma M\) (gauged curl–curl eigenproblems and the
Lee–Sun–Cendes pencil of PropagatingMode), real pencils only. Complex pencils — lossy
media, PML, the resonance problems of maxwell.md — go through
complex_eigenpairs_near: an own shift-invert Arnoldi in complex arithmetic on
\((A - \sigma B)^{-1} B\) with the direct solver backends of this page for the factorisation
(MUMPS when compiled in), modified Gram–Schmidt with re-orthogonalisation, explicit restarts
from the wanted Ritz vectors and the relative residual \(|h_{m+1,m}\,y_m|/|\theta|\) as
convergence test; eigenvalues come back ordered by distance to the shift with their
residuals. All three solvers scale the pencil to \(O(1)\) matrices internally: on SI meshes the
mass entries are \(\sim h^2 \sim 10^{-14}\) and the eigenvalues \(\sim 10^{13}\), which made the
absolute thresholds of the Krylov iterations stop early with non-converged values.