/** * Newton-Raphson iteration for non-linear MNA. * * Non-linear elements (diode, LED, BJT, MOSFET) contribute companion * stamps linearised about the previous iteration's voltage guess. The * Newton loop rebuilds the MNA from scratch each iteration, solves it, * and repeats until the voltage change between iterations falls below * a tolerance (or the iter cap is hit). * * We don't depend on the MNA implementation directly — the caller * supplies closures for `stamp`, `solve`, and `clear`. That lets us * unit-test this in isolation with a fake matrix, and lets `SimEngine` * decide how to partition linear vs non-linear stamps. * * Convergence criterion (SPICE-style mixed reltol/abstol): * |x_new[k] - x_old[k]| < vAbsTol + vRelTol * max(|x_new[k]|, |x_old[k]|) * for every k. Default vAbsTol=1e-6, vRelTol=1e-3. * * We return the last iterate even when we fail to converge — callers * should surface a warning, but blowing up the simulation on a single * non-convergent step is worse than rendering a slightly-off frame. */ export interface NewtonOpts { /** Matrix/vector dimension (MNA size). Zero → trivially converged. */ size: number; /** Initial voltage guess. Typically the last-step solution. */ xInit: Float64Array; /** Clear the MNA and re-add always-on stamps (GMIN, etc.). */ clear: () => void; /** * Stamp all elements into the (already-cleared) MNA using `xGuess` * as the linearisation point for non-linear elements. Linear stamps * (R, V, C, L) ignore the guess; non-linear stamps use it to compute * their companion conductance + current source. */ stamp: (xGuess: Float64Array) => void; /** Solve the currently-stamped MNA and return a fresh solution vector. */ solve: () => Float64Array; /** Max iterations before declaring non-convergence. Default 25. */ maxIter?: number; /** Absolute voltage tolerance (V). Default 1e-6. */ vAbsTol?: number; /** Relative voltage tolerance. Default 1e-3. */ vRelTol?: number; /** * Returns true when the stamp pass just performed applied device limiting * (pnjlim clamped a junction's linearisation point away from the raw * guess). A limited iteration stamped a DIFFERENT system than the one the * delta-x test measures: the iterate can sit bit-still (e.g. a base parked * volts into forward bias while the limiter walks the evaluation voltage * up by ~n·Vt per iteration) even though the true device equations are * violated by many orders of magnitude. SPICE refuses to accept such an * iteration for the same reason; convergence requires a limiting-free * final iteration. Near a genuine solution the inter-iteration deltas are * below pnjlim's 2·n·Vt trigger, so this guard is inert on every honestly * converging solve. */ limitedThisIteration?: () => boolean; } export interface NewtonResult { /** Last iterate — usable even when `converged === false`. */ x: Float64Array; /** Iterations consumed (1..maxIter). */ iters: number; /** Whether the last delta passed the tolerance check. */ converged: boolean; /** Max |Δx| of the last iteration, for diagnostics. */ lastDelta: number; } export declare function solveNonlinear(opts: NewtonOpts): NewtonResult; /** * "pnjlim" — SPICE's PN-junction voltage limiter. * * Clamps the inter-iteration voltage swing across a PN junction to * keep `exp(v/Vt)` from overflowing and to damp oscillations when the * Newton step would drive the solver into a far-from-linear region. * * `vNew` proposed voltage (this iteration) * `vOld` previous iteration's voltage * `VtN` thermal voltage × emission coefficient (n · Vt) * `vCrit` critical voltage — above this we log-compress the step. * Typically `VtN · ln(VtN / (Is · sqrt(2)))`. * * Returns a limited voltage to use as the stamp linearisation point. */ export declare function pnjlim(vNew: number, vOld: number, VtN: number, vCrit: number): number;