kicad2ems.emsdev.afs

emsdev.afs - an adaptive frequency sweep with rational (AAA) interpolation,
which mom25d's sweep and pcbqs's S-parameters share (it was mom25d's own).

A solve per frequency is the expensive step; S(f) of a passive structure is a
smooth rational function between its resonances. Fit one barycentric rational
model to all S entries at once (set-valued AAA), and only solve at the frequency
where two successive models disagree most, until they agree everywhere on the
band.

    r = aaa(z, F)  # F (n, m) at z (n,): r(zz) -> (len(zz), m)
    grid, S, fs, Ss, history = adaptive_sweep(solve, fmin, fmax, grid=freqs)

numpy is the one thing it needs (emsdev itself needs nothing: import this module
only where numpy is).
kicad2ems.emsdev.afs.aaa(z, F, tol=1e-10, mmax=80)[source]
Set-valued AAA (Nakatsukasa, Sete, Trefethen 2018; Lietaert et al. 2022).
z: (n,) sample points, F: (n, m) values.  Returns r(zz) -> (len(zz), m).
Supports are added until every sample is within tol of each entry's scale
(FLOOR), at most as many as the samples determine: (n - supports) m rows
for supports - 1 weights. (Past that the fit has no rows left: 6 ports' 36
entries took every sample as a support up to 37 of them, the model one
constant, rf_bends' 60 solves; 16 ports' never came back from it.) Entries
under SMALL of the largest: straight lines between the samples.
kicad2ems.emsdev.afs.adaptive_sweep(solve, fmin, fmax, n0=5, tol=0.002, max_points=40, ngrid=2001, log=<built-in function print>, on_step=None, seed=(), grid=None, hint=None)[source]
solve(f) -> S (P x P).  Returns (f_grid, S_grid (ngrid, P, P), f_samples,
S_samples, history): one dict per model after the first, solves (samples so
far), change (max |dS| from the previous model, on the grid) at f_change
(Hz), pred (how far the previous model was from the newest sample) and f_new
(Hz: the next frequency solved, None when the sweep stopped).
on_step(entry): called with each entry as it is made. Converged when, twice
in a row, the models from the last two iterations agree to `tol` (max |dS|)
on the grid and the model predicted the newest sample to `tol` before it was
solved, the second time a solve in the widest gap between the samples. (The
model change alone can be zero while the model is still wrong: a sparse fit
that the new sample does not alter.) seed: frequencies (Hz)
solved among the first samples, besides the n0 spread over the band (those
inside it). grid: the frequencies asked for (Hz), the model checked and
returned there (else ngrid spread linearly); a grid in log f (a sweep of
decades) has the first samples and the gaps between them in log f too. (A
log sweep had been solved on a linear grid and interpolated linearly: below
its first step, decades between two samples.) The last history entry's
`converged` says whether the models agreed, or the sweep stopped at
max_points. hint: what to try, said with a solve that gives non-finite
S-parameters.