/** * Worker-hostable driver for the linearized small-signal AC analysis. * * WHY THIS EXISTS. `runSmallSignalAc(engine, options)` (ac-analysis.ts) is the * exact transfer function: it linearizes every device about a committed DC * operating point and solves the complex system directly, so it is near-instant * next to the large-signal sine sweep in run-ac-sweep.ts. But its signature is * built for a test or an in-process caller — it takes a live SimEngine, runs the * whole frequency loop atomically, and offers no progress callback and no * cancellation. A UI worker needs all three: it is handed a structured-cloneable * circuit over postMessage, it must report each point for a progress bar, and it * must yield so a "cancel" message can arrive mid-run. * * WHY A WRAPPER AND NOT A REFACTOR. runSmallSignalAc has three in-tree * consumers (brownout/ac, the SPICE `.ac` directive, and the A5 cross-validation * corpus) and is pinned to ngspice to ~1e-6 dB. Threading hooks and a yield * through its inner loop would put that contract at risk for a UI convenience. * This driver instead COMPOSES it, unchanged, one chunk of frequencies at a * time. The cost is that each chunk re-solves the operating point — but the OP * is a deterministic function of the circuit, so every chunk linearizes about * the identical bias, and the results are bit-for-bit what a single call would * give. The extra OP solves are the price of cooperative cancellation, and the * whole analysis is still far cheaper than the settle-per-point sine sweep. * * The cancellation contract is run-ac-sweep.ts's, deliberately: a MACROTASK * yield (setTimeout, not a resolved Promise) between chunks, because queued * worker messages only dispatch between macrotasks. Get that wrong and Stop is a * silent no-op. Sync `runSmallSignalAcSweep` for tests; async * `runSmallSignalAcSweepAsync` for the worker. */ import type { SimCircuit } from "../sim/engine/sim-engine.js"; import { type SmallSignalAcResult } from "../sim/engine/ac-analysis.js"; export interface SmallSignalAcSweepSpec { kind: "small-signal-ac"; /** Component id of the independent source carrying the unit AC drive. */ inputId: string; /** Sweep start frequency in Hz (> 0). */ fromHz: number; /** Sweep end frequency in Hz (> fromHz). */ toHz: number; /** Number of log-spaced points (>= 2). */ points: number; /** Deterministic net ids to report (e.g. "n3"; "gnd" reads as 0). */ outputNetIds: readonly string[]; } export interface SmallSignalAcSweepHooks { /** Called after each COMPLETED point, in ascending-frequency order. */ onPoint?: (index: number, total: number) => void; /** Polled after each chunk; a truthy return stops the sweep cooperatively. */ shouldCancel?: () => boolean; } export interface SmallSignalAcSweepResult extends SmallSignalAcResult { /** True when the run stopped early because shouldCancel() returned true. */ cancelled: boolean; } /** * Synchronous small-signal AC sweep. For tests and in-process callers; a worker * must use the async variant so cooperative cancellation can work. */ export declare function runSmallSignalAcSweep(spec: SmallSignalAcSweepSpec, circuit: SimCircuit, hooks?: SmallSignalAcSweepHooks): SmallSignalAcSweepResult; /** * Async small-signal AC sweep: identical numbers to the sync form, but yields a * macrotask between chunks so the hosting worker's event loop can process a * queued "cancel". Without the macrotask yield cancellation is a no-op — a * microtask (a resolved Promise) does not let a queued message dispatch. */ export declare function runSmallSignalAcSweepAsync(spec: SmallSignalAcSweepSpec, circuit: SimCircuit, hooks?: SmallSignalAcSweepHooks): Promise;