Numerical limits and conditioning#
Draft Written from harmolab/utils/impedance/impmet.py, harmolab/rwg/rwg3.py, harmolab/utils/impedance/compute_backend.py, harmolab/utils/mesh/gmsh_function.py, harmolab/cma/z_matrix_diagnostics.py and harmolab/utils/mesh/mesh_io.py at commit a0d0b21; module paths updated at commit 72fb8c6. Awaiting review.
What limits the size of the problems the solver can handle, the frequencies it can go down to, and
the accuracy of what it returns. Every number on this page was measured with the current code; the
figures come from documentation/figure_notebooks/theory_numerical_limits.ipynb.
Cost#
Method of Moments and the impedance matrix gives the orders: \(O(N^2)\) entries to fill, \(16N^2\) bytes to store them, \(O(N^3)\) operations for the LU solve. What matters in practice is where a given mesh sits on those curves.
Figure 15 (a) Time to fill \(Z\) with the three assemblers of the solver, and to solve \(ZI = V\), for a square
plate of 0.15 m at 1 GHz meshed more and more finely (\(N\) = 96 to 3816); an assembler is no longer
timed once its predicted time exceeds 300 s. (b) Memory of one matrix, \(16N^2\) bytes.
Generated by documentation/figure_notebooks/theory_numerical_limits.ipynb on solver commit 72fb8c6, on a laptop with a 16-thread Intel CPU and an NVIDIA T600 (4 GB); all values in numerical_limits.json. Click to enlarge.#
The three assemblers give the same matrix: the per-pair loop (impmet.impedance_matrix_z), the
batched assembly on CPU and on GPU (impmet.impedance_matrix_z_sweep_gpu) agree to \(10^{-15}\)
relative. rwg3.calculate_z_matrice chooses between them: the batched GPU path when a usable GPU is
found and \(N \ge 100\) (compute_backend.DEFAULT_GPU_EDGE_THRESHOLD, calibrated on the T600 according
to its comment), the per-pair loop otherwise.
The GPU makes the fill 2 to 4 times faster than the batched CPU assembly (19.6 s against 52.2 s at \(N\) = 481, 32.3 s against 122.3 s at \(N\) = 936), and the batched CPU assembly is itself up to 1.9 times as fast as the per-pair loop (122 s against 229 s at \(N\) = 936; on the smallest meshes the two are close). On this run the batched CPU assembly was not timed beyond \(N\) = 936: its predicted time at \(N\) = 1976, 318 s, exceeded the budget. (On the run of 2026-11-11, on the same machine, it took 306 s there, against 88 s on the GPU.)
On these sizes the fill does not grow as \(N^2\) yet. Over the sizes measured the times grow
roughly as \(N^{1.2}\) (GPU, \(N\) = 96 to 3816), \(N^{1.1}\) (batched CPU) and \(N^{1.3}\) (per-pair loop,
both \(N\) = 96 to 936). A profile of the GPU assembly at \(N\) = 481 (cProfile, in the notebook) shows
why: more than half of the profiled time (16.1 s of 28.1 s) is spent in
quadrature.duffy.duffy_source_nodes and the NumPy calls under it,
which build the Duffy nodes of the SELF pairs on the CPU, in
Python, one call per (SELF pair of edges, test point): 2 786 SELF pairs × 12 test points = 33 432
calls on 338 triangles. The transfer of the batched FAR and EDGE/VERTEX results from the GPU, which
includes waiting for the GPU kernels, takes about 18 % (5.1 s). The profiler slows Python code more
than it slows waiting for the GPU, so these shares are indicative. The SELF work grows with the number of
triangles, not with \(N^2\); the \(N^2\) part takes over only on larger meshes. The Duffy nodes depend on
the triangle and the test point only (338 × 12 = 4 056 distinct sets), not on the pair of edges, so
they are computed about eight times more often than necessary. A comment in impedance/impmet.py also names
the other route, replacing the inner Duffy rule with the closed form already used for the EDGE/VERTEX
pairs. Neither is implemented at the time of writing.
The solve is cheap next to the fill: 0.05 s at \(N\) = 936, 0.7 s at \(N\) = 1976 and 8.4 s at \(N\) = 3816, against 32 s, 94 s and 326 s for the GPU fill. Its time grows as \(N^3\) or faster (×12.8 from 1976 to 3816 on this run, ×6.6 on the run of 2026-11-11, where \(N^3\) gives ×7.2: a solve of a few seconds is sensitive to what else the machine is doing), and it would catch up with the fill only on meshes far larger than this machine can hold.
Memory sets the hard limit. One matrix takes \(16N^2\) bytes: 4 GB at \(N \approx 16\,000\). A
frequency sweep held in memory (calculate_z_matrice_sweep) multiplies that by the number of
frequencies. Before sending an assembly to the GPU, rwg3 checks that the device has room for three
times the matrix (compute_backend.gpu_has_headroom_for, safety factor 3, counting CuPy’s pool) and
otherwise assembles on CPU, saying so. If the GPU runs out of memory during the assembly, it stops
with an explicit error instead of silently restarting on CPU (rwg3._run_gpu_assembly).
Timings on a laptop vary from one run to the next by up to a factor of two (other programs, thermal throttling); the shapes of the curves, not the exact seconds, are the result.
Mesh density#
Two helpers in gmsh_function encode the rules of thumb the developers use:
mesh_and_gap_parameters(frequency, resolution=20, gap_fraction=0.15, n_edges_min=3)proposes a cell of λ/20, tightened if fewer than 3 cells would fit in the gap, and warns below λ/100 (“Z-matrix may be ill-conditioned”) and for a gap wider than 0.2 λ;check_simulation_stability(Z_in, frequency, mesh_size, gap_width), after a run, flags a cell coarser than λ/10, finer than λ/100, a gap of fewer than 3 cells (no edge at all below 0.67 cell), and an input resistance outside 5 to 500 Ω.
These are heuristics, not limits of the method. The conditioning measured below puts the λ/100 warning in perspective: at λ/100, \(kh = 2\pi/100 \approx 0.063\), and the condition number of \(Z\) is about \(3.5\times10^{3}\) on the plate of Figure 16 (between \(5.0\times10^3\) at \(kh\) = 0.052 and \(2.1\times10^3\) at \(kh\) = 0.079), which costs three to four of the sixteen digits of double precision. How the input impedance converges with the mesh is shown on a real case in Excitations and input impedance (the strip of Ding et al.: within 1 to 2 Ω of the published values on fine meshes).
Conditioning#
Method of Moments and the impedance matrix explains why the EFIE matrix degrades when \(kh\) is small: the charge term grows as \(1/(kh)^2\) relative to the current term, which is the low-frequency breakdown of the EFIE [1]. The solver has no loop-star or loop-tree decomposition. The question is how much that costs in practice.
Figure 16 (a) Condition number of \(Z\) (2-norm) for a square plate of 0.1 m: one mesh (10 × 10 cells,
\(h\) = 10 mm, \(N\) = 280) from 1 kHz to 5 GHz, and refined meshes (\(N\) = 40 to 1160) at 300 MHz.
(b) and (c) Input impedance of a square loop, 20 mm on its centre line and 2 mm wide (\(N\) = 200),
fed by a delta gap on a cut of mesh edges (harmolab.utils.ports.port_from_cut, the model of
DeltaGapCut), \(Z_\mathrm{in} = 1/(\mathbf a^\mathsf T Z^{-1}\mathbf a)\),
with the default fill_mode="full_sym" and the former "mirror".
Generated by documentation/figure_notebooks/theory_numerical_limits.ipynb on solver commit 72fb8c6; all values in numerical_limits.json. Click to enlarge.#
The condition number follows \((kh)^{-2}\), over seven decades of \(kh\): \(1.2\times10^{1}\) at \(kh = 1\), \(9\times10^{3}\) at \(kh = 0.04\), \(3\times10^{14}\) at \(kh = 2\times10^{-7}\). Lowering the frequency or refining the mesh gives the same value for the same \(kh\) (at \(kh = 0.039\): \(9.15\times10^{3}\) and \(9.03\times10^{3}\)).
A large condition number is not yet a wrong answer. The loop shows how far the default assembly goes. Below its first resonance a small loop is an inductor, so \(\mathrm{Im}\,Z_\mathrm{in}/\omega\) must not depend on frequency:
with
fill_mode="full_sym"it stays at 48.32 nH from 30 kHz to 30 MHz (within \(3\times10^{-4}\)), and is still within \(5\times10^{-4}\) at 10 kHz, where cond\((Z) = 4.8\times10^{14}\). At 1 kHz, cond\((Z) = 4\times10^{17}\), beyond \(1/\varepsilon_\mathrm{mach} \approx 4.5\times10^{15}\), and the value is 14 % off;the radiation resistance is lost much earlier. It is smaller than the reactance by many orders of magnitude, and the loop radiates as a magnetic dipole, whose power falls as \(f^4\). The computed \(\mathrm{Re}\,Z_\mathrm{in}\) follows \(f^4\) down to about 3 MHz; from 1 MHz down it is a noise below \(10^{-12}\) Ω with a random sign. Any quantity that depends on the radiated power of an electrically very small structure (radiation resistance, efficiency, characteristic modes) is therefore limited well before the reactance is;
with the former
fill_mode="mirror"the inductance is already 4 % high at 300 MHz, 84 % at 100 MHz, and grows as \(1/f^2\) below, to \(5\times10^{11}\) nH at 1 kHz. The mirrored fill breaks the exact charge neutrality of loop currents (see Method of Moments and the impedance matrix), and the error is multiplied by the \(1/(kh)^2\) of the charge term. This extends to low frequency the measurement on a loop that led to the change of default.
Health checks in the code#
Reciprocity and positivity of \(R\) (
cma.z_matrix_diagnostics).check_reciprocity(Z)returns \(\max|Z - Z^\mathsf T|/\max|Z|\); on a matrix filled with the default"full_sym"(or"mirror") it reads 0 by construction, so it is informative only on a matrix filled withfill_mode="full", where it bounds the quadrature error of the EDGE/VERTEX pairs (the module documents 5e-5 to 5e-4 when healthy; it warns aboveRECIPROCITY_TOL= 1e-3).check_r_positive_semidefinite(Z)returns the spectrum of \(R\) and its smallest eigenvalue relative to \(\lVert R\rVert_F/N\);diagnose_z_matrixprints both and warns below -1e-6. A negative eigenvalue far beyond rounding means an assembly error, not a small antenna.Files from different meshes (
mesh_io). The stages of the file pipeline store a fingerprint of the mesh (compute_mesh_fingerprint). Before solving,rwg4.calculate_current_radiationandcalculate_current_scatteringcheck that the_mesh2and_impedancefiles carry the same one and raiseMeshConsistencyErrorotherwise, so a stale impedance file cannot be combined with a new mesh. Files written before the fingerprint existed pass silently.After a run:
check_simulation_stability(above), and the efficiency warning ofefield2above 101 % (see Radiated fields, directivity and gain).
Reproducibility#
For a given mesh, the three assemblers agree to \(10^{-15}\) (CPU and GPU batched to \(10^{-16}\)).
The meshes themselves are not always reproducible. The same
rectangle meshed several times through the solver’s helpers (setup_performance_config, then
generate_and_save_mesh, as in the CMA tracking test):
Geometry, element size |
Threads |
Distinct meshes in 10 runs |
|---|---|---|
strip 10 × 200 mm, 12 mm (the mesh of the CMA tracking test) |
1 |
1 |
(same) |
16 |
1 on this run; 3 on the run of 2026-11-11 (53 nodes, 68 triangles each time) |
plate 100 × 100 mm, 4 mm |
1 |
1 |
(same) |
16 |
8 (788 or 789 nodes, 1474 or 1476 triangles); 9 on the run of 2026-11-11 |
With one thread, gmsh gave the same mesh every time. With 16 threads it did not, at least on the
plate: the nodes were placed or numbered differently, and even their number changed; the small strip
came out identical on this run but not on an earlier one. Results computed on them then differ in their last digits,
enough to flip a comparison between two nearly equal numbers (this is what made
test_cma_frequency_sweep_tracks_a_real_structure fail once in a while, until its comparison was given
a tolerance). setup_performance_config(n_threads=None) uses every core by default and
generate_surface_mesh sets 5 threads; for reproducible meshes, set General.NumThreads to 1, as the
figure notebooks of this documentation do.
Accuracy budget#
What the previous pages measured, in one place:
Source of error |
Size |
Where it was measured |
|---|---|---|
Free-space constant \(\mu_0 = 1.257\times10^{-6}\) H/m |
\(3\times10^{-4}\) relative |
|
Quadrature of the EDGE/VERTEX pairs (reciprocity of a full fill) |
\(5\times10^{-5}\) to \(5\times10^{-4}\) |
|
Input impedance of a strip against published values |
1 to 2 Ω on fine meshes |
|
Radiated power of the dipole model of |
\(2\times10^{-4}\), plus \(4\times10^{-4}\) for the |
|
Reduced multiport model against a full solve |
\(10^{-13}\) |
|
Conditioning |
about \(\log_{10}\mathrm{cond}(Z)\) digits, cond \(\propto (kh)^{-2}\) |
this page |
Continue#
When it helps, and how to choose
Element size and gap width
What is validated, and how