/** * Pure Monte Carlo runner — runs a MonteCarloSpec headlessly against the real * SimEngine without any UI or WebWorker dependency. * * WHY cold-start per run: * Element state (capacitor voltages, failure stress, latched failures) * carries across load() calls by design. If we reused one engine instance * the initial conditions from run N-1 would corrupt run N — a capacitor * pre-charged by the previous run's component values would bias the transient. * coldLoad() zeros all element state so every run starts from the same known * initial condition, matching run-sweep.ts's rationale. * * WHY tail averaging (same as run-sweep.ts): * The final 20% of the settle window filters AC ripple while remaining trivial * to compute. Teaching-grade accuracy for the circuits we target. * * WHY uniform perturbation rather than Gaussian: * Uniform over [-tol, +tol] is an honest worst-case-bounded teaching model. * It requires no distribution assumption and directly corresponds to the * part datasheet tolerance band (e.g. "±5%"). A Gaussian would be more * statistically rigorous but would require choosing a sigma, which is an * invisible extra parameter with no intuitive teaching value. * * WHY order-independent seeding (FNV-1a over "seed:run:compId:param"): * Each delta is a deterministic function of (spec.seed, runIndex, * componentId, paramName) only. Changing the order of components in the * circuit.components array therefore does NOT change the per-component * perturbations. This is critical for reproducibility: a structurally * identical circuit saved with a different component insertion order must * produce bit-identical results, otherwise "same seed" is a weaker contract * than it appears to be. * * WHY population stddev: * We are describing the distribution of the simulation results for the * given seed and circuit — not estimating a parent population from a sample. * Every drawn value IS the population for this seed, so the N denominator * (population stddev) is correct. Sample stddev (N-1) is appropriate when * the values are a random subset of a larger unknown population. * * WHY two run variants: * runMonteCarlo (synchronous) is the testable core loop. runMonteCarloAsync * yields a macrotask between runs so the analysis worker's event loop can * process a queued "cancel" message. Without this yield the worker handler * never returns mid-run and cooperative cancellation would be a no-op — * matching the rationale in run-sweep.ts. * * WHY vth_tol is NOT applied: * The engine reads IC logic input thresholds from the catalog module-level * catalogByKind map at engine init time (not from per-instance params). * There is no params override mechanism for thresholds in v1. Attempting * to set params.v_il_max or params.v_ih_min would be silently ignored. * vth_tol is therefore metadata-only in v1; the comment here serves as the * reminder for a future runner extension. */ import type { SimCircuit } from "../sim/engine/sim-engine.js"; import { type MonteCarloSpec, type MonteCarloResult } from "./jobs.js"; import { type AnalysisEngineOptions, type AnalysisRestartHook } from "./engine.js"; export interface MonteCarloHooks extends AnalysisEngineOptions, AnalysisRestartHook { /** Called after each run completes (before the cancel check). */ onPoint?: (runIndex: number, total: number) => void; /** Return true to request cooperative cancellation. */ shouldCancel?: () => boolean; } /** * Run a Monte Carlo job synchronously and return the result. * * Intended for tests and non-worker callers. Inside the analysis worker use * runMonteCarloAsync instead — a synchronous run never returns to the worker's * event loop, so cooperative cancellation could not work. * * @param spec - Validated Monte Carlo specification (call validateJobSpec first). * @param circuit - The circuit to simulate. Mutated via structuredClone per run. * @param hooks - Optional: onPoint(r, total) for progress, shouldCancel() checked after each run. */ export declare function runMonteCarlo(spec: MonteCarloSpec, circuit: SimCircuit, hooks?: MonteCarloHooks): MonteCarloResult; /** * Async Monte Carlo runner: identical semantics to runMonteCarlo, but yields * a macrotask between runs so the hosting worker's event loop can process * queued messages — in particular "cancel", which sets the flag behind * shouldCancel(). Without this yield, a cancel posted mid-run would only be * handled after the final run, making cooperative cancellation a no-op. */ export declare function runMonteCarloAsync(spec: MonteCarloSpec, circuit: SimCircuit, hooks?: MonteCarloHooks): Promise;