kicad2ems.pcbqs.circuit.engine

Running a model: the starting state, the periods until it repeats, the targets'
knobs, what is measured.

    summary = run(model, out, jobs=1)     # every point of the sweep, each in
                                          # its own folder under out
    row = run_case(model, point, folder)  # one point

One point goes like this (analysis: the model's settings for it, all optional):
    1. The first run starts from ngspice's DC solution at time zero, where every
       gate is still low: the rails are charged through the diodes and the
       inductors carry their DC current, whatever the circuit.
    2. Each run is `cycles` periods (200) and starts where the last one ended
       (end_state: every capacitor's voltage, every inductor's and switch's
       current).
    3. It is settled when every capacitor voltage and inductor current repeats:
       the last period against every period of the half-run before it, within
       periodic_tolerance (0.003) of that signal's size (a slow drift or a
       ringing output filter shows over half a run when it does not from one
       period to the next), and what the circuit stores (the energy in its
       capacitors and inductors) changes over a period by no more than
       energy_tolerance (0.002) of the power it handles. What still goes into
       storage over the reported period is counted out of the parts' powers, the
       loss and the efficiency. A run that has not settled is followed by one
       twice as long, up to eight times `cycles`, started from the middle of
       whatever was still ringing.
    4. Settled, each target is compared with its measure; a knob that is off
       moves by a secant step, kept inside its range, and the runs go on. A
       target whose measure cannot reach its value inside the range ends the
       point with an error saying which end it stopped at.
    5. The reported run repeats a few periods at the strict tolerance
       (analysis.solver.reltol, 1e-4; the search runs at 1e-3) and with steps
       four times finer (final_samples_per_cycle). If ngspice cannot hold that
       tolerance the search's result is reported and a note says so. A search
       that ended at the strict tolerance itself is reported as it is, unless
       final_always (a spectrum's run: its fine steps are what it is for).

Reported for every point, with no request: each part's power; a switch's and a
diode's average, RMS and peak current and peak voltage; a capacitor's average
voltage, ripple and RMS current; an inductor's average, ripple and peak current;
what the sources deliver, what the loads take, the loss, the efficiency and the
power balance (what the sources deliver against what the parts absorb), and the
gate drives' power.
class kicad2ems.pcbqs.circuit.engine.Knob(target)[source]
A target's knob: where it is, what it gave, where to try next (a secant
step between the last two points, kept between the best points on either
side of the target once there are some, and inside the range).
retarget(err)[source]
Move by err along the slope of the last two points tried (the
reported run's own error); False when there is no slope to go by. The
point it leaves is kept as the last tried.
step()[source]
The next value to try; raises when the range's end has been tried and
the target is still off.
kicad2ems.pcbqs.circuit.engine.add_measures(deck, model)[source]
The deck's measures: each part's power and the statistics of its type,
then the model's own.
kicad2ems.pcbqs.circuit.engine.collect(measured, deck)[source]
ngspice's measurements as the results: the model's measures, each part's
statistics, the totals.
kicad2ems.pcbqs.circuit.engine.count_out_storage(stats, result, model, deck)[source]
The run's powers with what went into storage over the last period counted
out: a capacitor's or an inductor's power is what it lost, not what it lost
and kept; the loss is the parts' losses, and the efficiency is the load's
power over that and the losses. A filter still ringing by a hundredth of a
per cent moves more power in and out of its capacitor than that capacitor
loses, and none of it is loss.
kicad2ems.pcbqs.circuit.engine.edge_at(lines, t, tol=1e-09)[source]
Whether a PULSE of the deck's lines has an edge's corner (its start or
end of rise, of fall) at time t: then ngspice has a breakpoint there of its
own.
kicad2ems.pcbqs.circuit.engine.end_state(result, model, deck, period=None)[source]
Where a run stopped, for the next to start from: {ref: {initial_V |
initial_A, initial_I}}. A capacitor: the voltage on the capacitance itself
(<ref>.cap_V, without its ESR's and ESL's drops) and its branch current (for
its ESL); an inductor and a source's series inductance: the current; a
winding: its current and voltage; a switch: its drain current (its package
inductance) and its voltage (its capacitance); a diode: its voltage.
kicad2ems.pcbqs.circuit.engine.energy_drift(result, model, deck, period, ignore=())[source]
The power going into (+) or coming out of what the circuit stores, period
by period back over the half-run: [W], the last period first. In a settled
circuit it is nothing; in one that is not, it is by how much the sources'
power and the loads' and losses' fail to add up over that period. The
parts named in `ignore` are not counted (analysis.ignore: not judged for
settling).
kicad2ems.pcbqs.circuit.engine.evaluate(expr, signals)[source]
A measure's expression (@signal@s, as add_measures writes it) over a
run's signals ({label: samples}).
kicad2ems.pcbqs.circuit.engine.ngspice_version()[source]
ngspice's version line ('ngspice-45.2 ...'), '' when it is not found;
asked once.
kicad2ems.pcbqs.circuit.engine.periodic_error(result, deck, period, ignore=())[source]
How far the state is from repeating: {label: error}, the RMS difference
of each state signal between the last period and the periods of the half-run
before it (the difference that nine in ten of them are within), over that
signal's size (at least a hundredth of the largest signal of its unit, so a
quiet node is not judged against its own noise). Every period of the
half-run, not one or two of them: an output filter ringing at any rate
shows, as does a slow drift. The RMS over the period and nine periods in
ten, not the largest difference anywhere: a switching edge that the solver
stepped over differently in one period is not a circuit that has not
settled.
kicad2ems.pcbqs.circuit.engine.plausibility(stats)[source]
Notes on results no circuit gives: a part whose peak voltage is far above
every source and capacitor voltage of the circuit. It is the solver's answer
to a current cut off with nowhere to go (a switch or a diode with no
capacitance in a loop with inductance), not a measurement.
kicad2ems.pcbqs.circuit.engine.run(model, out, jobs=1, log=<built-in function print>, copper=False, fields=False, temperatures=None, starts=None)[source]
Every point of the model's sweep, `jobs` at a time; <out>/circuit.json
has them all: {format, name, complete, cases: [run_case's rows], failed:
[{sweep, error}]}. A point that fails is reported and the others still run.
copper: with the model's board between the parts' pads (circuit/copper.py),
extracted once for all the points. fields: with copper, and the H and E
fields over the board for every point (circuit/fields.py). temperatures:
{case name: {'parts': {ref: C}, 'board': C}}, each case's parts at those
(circuit/temperature.py; --warm's). starts: {case name: run_case's start},
where each case begins.
kicad2ems.pcbqs.circuit.engine.run_case(model, point, out, log=<built-in function print>, copper=None, start=None)[source]
One sweep point: {sweep, knobs, targets, measures, parts, totals,
settled, history, end_state, ...}, also written to <out>/case.json beside
the final run's circuit.cir, transient.npz (every signal, uniformly
sampled), measurements.json and waveforms.png. An unchanged model's point
is read back, not run again. start: dict(state, knobs) to begin from (a
run of nearly the same circuit: its end_state, which its parts not in it
begin at rest, and its knobs' values) in place of the DC solution.
kicad2ems.pcbqs.circuit.engine.sampled_measures(deck, signals, period)[source]
The measures given samples: N (add_measures), each its statistic over N
evenly spaced instants of the run's last period, interpolated in its
samples: what a scope that samples so often sees, the switching edges'
spikes between the instants left out (a filter's settled ripple).
{measure.<name>: value}.
kicad2ems.pcbqs.circuit.engine.simulate(model, state, cycles, folder, a, reltol, log, label='', final=False, copper=None)[source]
One ngspice run of a resolved model, with what it measured. A run that
ngspice abandons ('timestep too small' at a hard-switched edge) is tried
again with trapezoidal integration, then from the DC start.

The measures are taken over the run's last period, and ngspice takes them
over the solver's own time points inside the window it is given: a window
that opens between two points loses the part of the period before the first
of them (as much as a whole step of a signal that may be at its largest
there). So a source that does nothing puts a solver point where the last
period begins, and the window is opened a millionth of a period early and
closed as much late, so that point and the run's last are always inside
it.
kicad2ems.pcbqs.circuit.engine.state_label(ref, kind, signals)[source]
The signal that holds a capacitor's or an inductor's state: the voltage
on the capacitance itself (<ref>.cap_V, without its ESR's and ESL's drops),
a winding's inductance current (<ref>.winding_I); else <ref>.V or
<ref>.I.
kicad2ems.pcbqs.circuit.engine.stored_energy(result, model, deck, period)[source]
What each part stores at the end of each period, back over the half-run:
{ref: [J], the last period's end first}, with 'coupling:A+B' for the mutual
energy of each coupling. A capacitor: C V^2 / 2 of its own voltage; an
inductor and a source's series inductance: L I^2 / 2.
kicad2ems.pcbqs.circuit.engine.unsteadiness(result, model, deck, stats)[source]
energy_drift over the power the circuit handles: the size nine in ten of
the half-run's periods are within, and the last period's own (the error of
what that period's measures add up to).