Textbook benchmarks: adding a new case¶
A textbook benchmark checks Pulsim against a circuit and a formula printed in a power-electronics book. There are two routes; pick by what the book gives you.
| Route | Where | Checks | Use when |
|---|---|---|---|
| A. Exact-truth test | python/tests/test_textbook_reference_cases.py |
the engine against an exact solution computed with mpmath, on every engine | you can write the circuit's piecewise-linear equations (any converter with a closed form or an exact periodic orbit) |
| B. Published-values YAML | benchmarks/circuits/<area>/<topology>/textbook_*.yaml (e.g. switching/buck/) with validation: {type: published} |
the measured mean / RMS / peak-to-peak against the book's numbers, within tolerance_pct |
the book prints numbers for one operating point and a fixed netlist is enough (no Python gate drive) |
Route A is the stronger check: the book's formula idealises (small ripple, ideal switches), so it only shows the circuit is the book's; the engine is held to the exact solution of the netlist it actually runs.
Rules for both routes¶
- Cite edition and chapter, and write every formula out with the numbers substituted, so a reader can check it without the book. Quote a section, equation or figure number only if you have checked it in the book itself.
- Check the operating mode. The first Erickson buck shipped as "CCM"
was in DCM (K = 2 L / (R Ts) = 0.47 < K_crit = 1 - D = 0.5; finding F-T-1
in
openspec/changes/archive/2026-11-30-add-textbook-reference-cases/FINDINGS.md). Write the K / K_crit check next to the formula. - Account for the netlist's non-idealities (switch on-resistance, winding resistance) in the tolerance or in the truth, with the size of their effect written down.
- Never loosen a tolerance without a derivation next to it.
Route A: an exact-truth case¶
In python/tests/test_textbook_reference_cases.py:
- Compute the truth with mpmath, independently of Pulsim. A dc-dc
converter needs only a topology function (
_buck,_buckboost,_boost: the state matrix per switch / diode state and the diode current row) and aDCDCentryname: (kind, (Vin, L, C, R[, R_L]), g_on, g_off, f, D)(a new topology function is also added toTOPO, and to the netlist indcdc_builder);dcdc_orbitthen finds the exact periodic orbit (CCM or DCM). Other circuits get their own closed form (bridge_truth,spwm_truth,resonant_truthare the models). - Build the circuit with
CircuitBuilder, starting ON the orbit (inductori0, capacitorc0from the truth), so the error a run shows is the engine's, not a start-up transient. - Write two assertions:
test_truth_<case>_matches_<book>: the truth against the book's formula, within the book's idealisation (2 % for the cases here);- an engine test parametrised over
ENGINES(aDCDCentry joinstest_dcdc_engine_matches_orbitby itself; other circuits get their owntest_<case>_engine_matches_truth): each engine against the truth withinengine_tol(cfg, dt / T), whose orders are derived in the module docstring. A circuit with events off the dt grid passesgrid_events=False. - Run it:
export PYTHONPATH=$PWD/python:$PWD/benchmarks
python3 -m pytest python/tests/test_textbook_reference_cases.py -q
Route B: a published-values YAML¶
- Copy
benchmarks/circuits/switching/buck/textbook_erickson_buck_ccm_r5.yamland keep its shape: a comment block with the citation and every formula evaluated at the operating point, then
validation:
type: published
source: "Erickson & Maksimovic, Fundamentals of Power Electronics, 3rd ed., ch. 2 (buck, CCM)"
tolerance_pct: 2
start_time: 9.9e-3 # measure over the last periods only
values:
v_out: {metric: mean, observable: V(out), value: 12.0}
delta_il: {metric: ripple_pkpk, observable: I(L1), value: 2.5532}
metric is mean, rms (both time-weighted, so an adaptive grid
does not bias them) or ripple_pkpk (max - min over the window);
observable is a column name (V(node), V(a,b), I(name)) that the
runner records even when observables does not list it; source is
required and a value of zero is refused (it has no percent error).
2. Register the file in every place that lists the benchmark circuits:
- benchmarks/manifests/benchmarks.yaml (the runner's manifest);
- benchmarks/manifests/v2_triage.yaml (its status, e.g. {status: runs});
- the circuit record of python/tests/test_circuit_ir.py: re-run
openspec/changes/add-immutable-circuit-ir/probes/sweep_probe.py components
and raise the loadable-circuit count in test_every_circuit_is_recorded
by one, with a comment naming the file.
3. Run it; each value's measurement and error land in results.json as
published__<key> and published__<key>_err_pct, and a value outside
tolerance_pct fails the benchmark with its name:
export PYTHONPATH=$PWD/python:$PWD/benchmarks
python3 benchmarks/tools/benchmark_runner.py --only textbook_erickson_buck_ccm_r5 --output-dir /tmp/bench
- Pin the measured values against an exact truth in a test, as
test_published_validation_checks_each_declared_valueinpython/tests/test_benchmark_harness.pydoes for the R = 5 ohm buck (measured -0.10 / +0.18 / +0.27 % from the book; held to 2e-3 relative of the exact orbit, measured within 5e-4). - Regenerate
benchmarks/REFERENCE_STATUS.mdwithpython3 benchmarks/tools/reference_status.py(it runs every circuit on every engine) and commit it with the new row.