/** * SPICE directive runner (Wave A7) — drives the engine's existing operating * point, transient, and small-signal AC machinery from a parsed netlist. * Nothing here adds solver capability: every analysis is a composition of * dcOperatingPoint(), step(), and runSmallSignalAc() over a SimCircuit the * parser produced, so the corpus-pinned physics is the physics SPICE decks * get. MCU-free by construction: the netlist grammar can only produce * R/C/L/K/V/I/D/Q/M/J kinds, so no analysis can ever meet firmware. * * REFERENCE NORMALIZATION (why every voltage is V(node) - V("0")): the * engine picks its own MNA reference — graph.ts grounds the first * independent voltage source's neg pin, and an I-only circuit gets no * ground at all, every net floating on the disclosed 1e-12 node shunts. * SPICE instead defines node "0" as the reference. Node-voltage DIFFERENCES * are identical under any reference choice, so subtracting the solved * voltage of node "0"'s net from every reported node makes the results * exactly SPICE-referenced without touching the engine's reference logic * (when the engine happens to ground node "0"'s net the correction is * exactly 0). The same subtraction applies per-frequency to the complex AC * responses, where it removes any common-mode component the reference * choice leaves behind. * * TRANSIENT INTEGRATION: fixed-step engine.step(h) with h = tstep, no * adaptive control — SPICE decks state their step explicitly and a fixed * grid makes cross-simulator diffs trivially alignable. The window is * floor(tstop/tstep) steps: samples never pass tstop, and a tstop that is * not a whole multiple of tstep truncates to the last on-grid sample with a * warning (ngspice, adaptive, would land on tstop). Trapezoidal is the * default because .tran is exactly the workload Wave A2 built it for * (second-order accuracy on smooth reactive trajectories; BE loses * amplitude per cycle); the engine still BE-anchors the first step after * load and every marked discontinuity. Pass integrationMethod: "be" for * the historical companions. * * INITIAL CONDITIONS, two documented modes: * - Element IC= present (UIC-style): the operating point is SKIPPED — * SPICE only honors element IC= under .tran UIC — and the runner seeds * the engine's capacitor/inductor state maps directly (via the public * snapshot/restore seam; the maps themselves are engine-private). .ic * node entries additionally seed capacitors as V(ic+) - V(ic-) with * unlisted nodes read as 0 — that projection is the ONLY channel .ic has * in this mode, so an .ic node with no capacitor terminal is dropped * (the parser warns; ngspice UIC would force it into the initial solve). * The t = 0 sample is the engine's 1 ps load-seed solve (node voltages * within nanovolts of zero); seeded element states take effect from the * first step. * - Otherwise: a DC operating point supplies t = 0. .ic node entries are * honored the ngspice way — each named node is clamped by a temporary * ideal source to ground for the OP solve, then the clamps are removed * and the transient released from the clamped point (load() carries * element state across the reload by component id, which is exactly the * engine invariant that makes the release seamless). */ import { type DcOperatingPointResult } from "../sim-engine.js"; import { type AnalysisEngineKind, type AnalysisEngineOptions } from "../../../analysis/engine-base.js"; export interface RunSpiceOptions extends AnalysisEngineOptions { /** Transient companion method; default "trap" (see the header). */ integrationMethod?: "be" | "trap"; /** Ceiling on .tran step count (default 1e6): a typo'd tstep must fail * fast with a clear message, not hang the caller. */ maxTranSteps?: number; /** Ceiling on .dc / .ac point counts (default 1e5), same rationale. */ maxSweepPoints?: number; } export interface SpiceOpResult { /** SPICE node -> volts, normalized to V(node) - V("0"). */ nodeVoltages: Record; /** Element id -> amps (engine convention: pin0 -> pin1 through the * element; for sources that is the branch current INTO the pos pin). * An I card is the exception, reported n+ -> n- through the source like * the card reads, so it equals the card's value (its pins are crossed * onto the engine's, see netlist.ts). */ elementCurrents: Record; /** Which dcOperatingPoint ladder stage converged. */ method: DcOperatingPointResult["method"]; newtonIterations: number; } export interface SpiceTranResult { timeS: number[]; /** SPICE node -> per-sample volts, normalized; index-aligned with timeS. */ nodeVoltages: Record; } export interface SpiceDcResult { source: string; sweepValues: number[]; /** SPICE node -> per-point volts, normalized; index-aligned with sweepValues. */ nodeVoltages: Record; } export interface SpiceAcNodeTrace { re: number[]; im: number[]; magnitude: number[]; /** 20*log10(magnitude); exact zeros read -Infinity (engine convention). */ magnitudeDb: number[]; phaseDeg: number[]; } export interface SpiceAcResult { frequenciesHz: number[]; /** Component id of the AC-designated source. */ inputId: string; /** The element's AC magnitude; responses are scaled by it (the engine * injects a unit drive and the small-signal system is linear). */ inputMagnitude: number; /** SPICE node -> complex response per frequency, normalized to node "0". */ nodeResponses: Record; } export interface SpiceRunResult { title: string; warnings: string[]; /** * The engine the `.tran` stepped on (the one analysis that advances a * transient; `.op`, `.dc` and `.ac` solve the DC or linearized system on * classic regardless). "classic" when the netlist has no `.tran`. */ engine: AnalysisEngineKind; /** Why `.tran` is on classic when it was not asked to be; empty otherwise. */ fallbackReasons: string[]; op?: SpiceOpResult; tran?: SpiceTranResult; dc?: SpiceDcResult; ac?: SpiceAcResult; } /** * Parse a SPICE netlist and execute its analyses against fresh engines. * Throws SpiceParseError for grammar/subset violations (line + card * attached) and plain Error for solver-level failures; warnings that do not * change the runnable circuit are collected on the result instead. */ export declare function runSpice(netlistText: string, options?: RunSpiceOptions): SpiceRunResult;