kicad2ems.core.segment

kicad2ems.core.segment - a board solved in pieces (the user's plan,
2026-11-20): each signal layer's traces over ideal planes, each signal via (or
a group of them close together: a pair's) a small model of its own with its
pads, antipads and the ground vias round it, and the pieces joined as one
network at the cuts between them.

    python -m kicad2ems.core.segment board.toml --out out/seg
    python -m kicad2ems.core.segment board.kicad_pcb --nets RF,GND --out out/seg

A whole board in one full-wave solve meshes every plane and every trace at once
(a CM5 carrier's F.Cu alone: 165,000 unknowns, more than this machine's RAM).
In pieces, a layer's planes are not meshed at all (mom25d's Green's function
carries them: `--mom-stack ground`), and a via model is a few square
millimetres.

The cuts: a circle round each signal via, its radius clear of the via's pad and
of the largest antipad round it in the planes; every trace that crosses it is
cut there, a port on each side (the layer's piece outside, the via's inside),
the two joined. What the pieces leave out: a plane's apertures under a trace
(the layer's planes are ideal), the cavity between two planes where they are far
from any ground via (a via model's planes end at its box), broadside coupling
between layers.
exception kicad2ems.core.segment.Killed[source]
A piece's run ended by a signal (the kernel's OOM killer, its
oom_score_adj raised: _victim).
exception kicad2ems.core.segment.NoRoom[source]
A piece gave its share back (its direct solver needs more).
kicad2ems.core.segment.assemble(wins)[source]
A group's S from its windows' [(S (nf, n, n), port names, core
ports)]: each core port's row from its own window; an entry two windows
have, their mean; one neither has, 0. (S (nf, m, m), the m ports).
kicad2ems.core.segment.band_freqs(band)[source]
The band's frequencies (Hz) as a piece's run has them: its sweep's
(linear, or 'log': evenly on a log scale).
kicad2ems.core.segment.calibration_pieces(m, raw, layer, w, net)[source]
Two straight lines of width w on `layer` over the board's own planes
(each plane layer's ground a box round them), a lumped port at each end:
the whole (2 CAL_HALF mm) and its half. A cut joins two such ports back to
back, which a whole trace is not: X = A^-1 W B^-1 (ABCD, B the half
mirrored) between them makes the two halves the whole (junction).
kicad2ems.core.segment.cavities_for(m, parts, echo=<built-in function print>, fmax=None)[source]
--cavities: the board's plane-pair cavities (core.cavities: the whole
board's planes, every ground via in them a short), and each via piece's
piece['cavities']: the plane pairs whose cavity the physics-based model
leaves to the join (viamodel.build's), those of whose vias (its ports:
the vias through it that are no ground via) every via of the piece that
crosses it is one, on its mesh. fmax (Hz): the meshes built for it here,
before the pieces solve, and a cavity whose mesh a via of the piece is off
(its disc in no triangle: a void there) left out of that piece's join,
said: the cavity's Z there had been 0, a short across the barrel's
segment, with no word. [Cavity].
kicad2ems.core.segment.cavity_blocks(cavs, parts, done, f, echo=<built-in function print>, z0=50.0, sigma=None, rough=None)[source]
--cavities: each cavity's block for the join between the physics-based
via models' ports in it (viamodel's cavity_ports: each a segment of a
barrel in it, weighted by its share of the cavity's height): Z = W
Zc W^T, Zc the board's cavity between those vias (Cavity.impedance: its
resonances, every ground via on the board). Its planes' copper the
config's sigma (with rough, Hammerstad's factor at each frequency), else
cavities.SIGMA_CU. (blocks, links (via model port, block port),
records).
kicad2ems.core.segment.chain_nets(ends)[source]
Every net of the channels end_ports made (a channel through series
parts: its middle nets have no port, and 'auto' alone left them and their
parts out of the model).
kicad2ems.core.segment.chains(kb, nets)[source]
The nets joined by series parts (series_links) as channels: {net: its
chain's nets (sorted)}; a net with none is its own.
kicad2ems.core.segment.coupling_profile(kb, victim_nets, aggressor_nets, start, step=0.1, reach=3.0)[source]
Where along a victim its coupling to an aggressor is: samples every
`step` mm of the victim's tracks with their distance along its route from
`start` ((layer, x, y), KiCad mm; through its vias and series parts), the
gap to the aggressor's nearest copper on that layer, and the local
backward-coupling coefficient Kb (what NEXT builds up from: (Cm/C +
Lm/L) / 4 of the two lines' cross-section at that gap, the line model's
field solve). dict(dist, kb, gap, layer, xy) (numpy arrays).
kicad2ems.core.segment.cuts(kb, group, nets)[source]
Where the tracks of `nets` (KiCadBoard kb's, KiCad coordinates) cross a
group's circle: [dict(layer, net, at (KiCad x, y), out (unit vector away
from the via, KiCad))].
kicad2ems.core.segment.end_ports(kb, nets)[source]
Port specs at the two ends of each net (fnmatch patterns, comma-
separated; ground nets left out): of the net's pads that its own tracks or
vias reach, the two furthest apart, so a pad off the line (an ESD diode's,
a connector's twin pin) is left open, not a 50 ohm load. ([spec],
[dict(port, net, ref, pad, layer)]), a pair's legs side by side. (The
stretches' ends had missed an end on a short piece of track apart from
the net's longest on its layer, and had made every pad end a port.)
kicad2ems.core.segment.join(blocks, links)[source]
Networks joined at pairs of their ports. blocks: [(S (nf, n, n), port
names)], one reference impedance throughout, the names unique across the
blocks; links: [(name, name)], each port at most once. Returns (S (nf, m,
m), the m ports left, in the blocks' order).

The blocks' S side by side (b = S a); a link makes the wave into each of
its ports the wave out of the other (a_i = G b_i, G a symmetric
permutation), so b_e = (S_ee + S_ei G (I - S_ii G)^-1 S_ie) a_e.
kicad2ems.core.segment.junction(fW, SW, fA, SA)[source]
A cut's correction (S, 2 x 2 at 50 ohm) from the calibration's whole
line W and its half A (calibration_pieces): X = A^-1 W B^-1, B the half
mirrored (ports swapped).
kicad2ems.core.segment.model_dropping(base, specs, ends, echo=<built-in function print>)[source]
The board model with ports at the channels' ends (end_ports's specs and
ends), a channel left out where one of its ports finds no reference copper
(a LimeRFE's bottom-side SAW duplexer over a void in its PWR layer): (model,
specs, ends), said.
kicad2ems.core.segment.node_block(n, nf)[source]
An ideal n-port junction (a node: equal voltages, the currents summing
to zero) at one reference impedance: S = 2/n - I, (nf, n, n).
kicad2ems.core.segment.nodes(blocks, pairs, nf, echo=<built-in function print>)[source]
pairs with each port in several of them (two parts' pads at one place,
one port: a node) joined to their other sides through an ideal junction
(node_block, appended to blocks): every port in one pair at most.
kicad2ems.core.segment.open_pieces(parts, bad, port_nets)[source]
The pieces (names) whose every net is a channel's that unrouted (bad)
found open end to end: solved, they would give only those channels' own
open S and their coupling to one another (pieces apart do not couple).
A piece with a net of a live channel, or of none, is kept; a kept piece's
cut to a piece left out ends open, as a leg with no path on does.
LimeRFE (2026-11-23): 74 of its 317 pieces, 18.5 of the 91 min of
solving, five via models of 25-29k unknowns among them.
kicad2ems.core.segment.overlay(result, kb=None, top=8)[source]
The joined board's figures laid over it: <name>_overlay.json (each
channel's tracks per layer, KiCad mm, with its figures: insertion and
return loss at the report's frequencies, its conversion to common mode, its
NEXT and FEXT sums as a victim; the via models' circles; the cuts) and
<name>_overlay.png (the board's channels coloured by the worst of NEXT and
FEXT, and by the insertion loss at the top frequency, the via models
circled, the `top` worst labelled). result: segment.json's path; kb: the
board (KiCadBoard), read again when None. {json, png}.
kicad2ems.core.segment.overlay_html(ov, path=None)[source]
The overlay as one self-contained page (segment_overlay.html with the
data in it: opens from the disk, no server): the board's channels
coloured by a figure (insertion or return loss at a frequency, crosstalk,
mode conversion), each channel's curves and worst aggressors. The page
without data (path None: its text) fetches overlay.json beside it.
kicad2ems.core.segment.pairs_report(f, S, ports, port_nets, rise_ps=100.0, z0=50.0, curves=60)[source]
Each net's (a pair's legs: one channel, in its differential mode)
figures from the joined S: its insertion and return loss at 1, 5 and 10
GHz (Sdd21, Sdd11; a single net's S21, S11) and its far end's conversion to
common mode, and every victim's NEXT and FEXT for an edge of rise_ps
(coupling.matrix, summary: as Board coupling has them). The near (driven)
end of each net: the footprint holding the most ports' ends (the SoC, the
module), else its other end. curves: each channel's insertion and return
loss and its conversion to common mode (dB) at about this many of f's
frequencies besides (0: none). dict(channels [...], victims [...], pairs
[...], curve_ghz).
kicad2ems.core.segment.piece_settings(s)[source]
The config's settings every piece's run takes beside its band: its
copper loss (sigma, with rough and finish) and a log sweep, those set
alone (a lossless linear config's pieces: their runs and cache keys as
before). They had not been passed on: the pieces of a lossy config solved
lossless on a linear sweep, where a whole-board run of it is lossy.
kicad2ems.core.segment.pieces(m, raw, kb, nets, apart_h=None, echo=<built-in function print>)[source]
The board m (its own ports the external ones) in pieces (a layer's
nets in groups further apart than apart_h heights over its plane:
coupling.APART_H by default, 0 one piece a layer): [dict(kind
'layer' | 'via', name, board (a Board), links [(cut port, its partner's
name)])], and the cuts. A layer piece: that layer's signal copper outside
every cut circle, the ground copper of the other layers (its planes: ideal
in the solve), no vias, its external ports and a port at each cut. A via
piece: its group's signal copper inside the circle on every layer, the
vias in its box (its own and the ground vias round it), the ground copper
clipped to its box (the box its outline), a port at each cut.
kicad2ems.core.segment.placed(m, raw, base, specs, ends, apart_h=None, echo=<built-in function print>)[source]
pieces() of model m, a channel left out (said) wherever a cut lands on
copper the model gives another net (two nets' copper overlapping in the
board file: a short, or nets renamed out of step with the tracks), the
model made again without it: (m, parts, cuts, specs, ends). Likewise a
channel whose end port the model puts on another net's copper
(_off_its_net).
kicad2ems.core.segment.plot_coupling_profiles(result, path, kb=None, top=4)[source]
The `top` worst-coupled channel pairs (plot_couplings's ranking), each
as a profile along its victim: the local backward-coupling coefficient Kb
(%) against the distance from the victim's near end, the layer it is on
shaded, with a map of where it peaks (the victim coloured by Kb, the
aggressor grey) and the full-wave NEXT and FEXT for the pair.
kicad2ems.core.segment.plot_couplings(result, path, kb=None, top=12)[source]
The board's worst couplings on a whole-board view: every channel's
tracks faint, the `top` strongest channel pairs (both directions merged,
by the larger of NEXT and FEXT) in colour, the stretch where the two run
closest drawn thick, each labelled with its NEXT and FEXT (% of the
aggressor's swing, the report's edge). result: segment.json's path.
kicad2ems.core.segment.plot_overlay(ov, kb, path, top=8)[source]
The overlay as a figure: two panels, the channels coloured by the worst
of NEXT and FEXT (%), and by the insertion loss at the top frequency
(dB), over the board's outline; the via models circled.
kicad2ems.core.segment.run(a, echo=<built-in function print>)[source]
The board in pieces, solved and joined: the external ports' S
(Touchstone) and segment.json.
kicad2ems.core.segment.series_block(f, part, z0=50.0)[source]
A series part's S (nf, 2, 2) between two z0 ports: its R, L and C in
series (a 0 ohm link: a through).
The two-pad series parts whose pads are on two of `nets` (a set):
[(ref, net, net)]. A Thor carrier's 158 high-speed nets had 100 (88 AC-
coupling capacitors, 12 resistors): each lane two nets, its ends at the
capacitor's pads.
kicad2ems.core.segment.solve_piece(piece, folder, band, solver='mom25d', echo=<built-in function print>, thickness=None, dmax=None, layer_solver=None, via_solver=None, edges=None, env=None, started=None)[source]
A piece's run (its board file, kicad2ems's pipeline): (S at 50 ohm,
its ports' names, the run's summary). started: called with the run's
process once it starts and None once it ends. NoRoom where the run gave
its memory share back (env's MOM25D_NO_ROOM_EXIT).
kicad2ems.core.segment.unrouted(parts, allcuts, port_nets)[source]
The channels (port_nets' 'net') whose two end ports' pieces are not
joined through cuts on the channel's own nets: open end to end before
anything is solved (a via left out, a port on a layer its net's copper
does not reach). [(channel, [its ports' pieces])].
kicad2ems.core.segment.via_groups(m, nets, gap=0.25, ends=(), keep_out=())[source]
The signal vias of `nets`, each with its cut radius (beyond its pad and
the largest antipad round it, by gap mm), in groups: vias whose circles
touch are one model. [dict(vias [dict(x, y, net, r, pad, antipad)],
circle (shapely, model coordinates))].