velxio/frontend/src/__tests__/spice-transient.test.ts

62 lines
2.0 KiB
TypeScript
Raw Normal View History

feat: electrical simulation via ngspice-WASM (eecircuit-engine) Adds full SPICE-accurate electrical simulation to Velxio, behind a lazy- loaded ⚡ toolbar toggle. Arduino / ESP32 / RP2040 sketches now co-simulate with real analog behaviour: correct voltages on wires, real I–V curves on LEDs, working potentiometers, NTC thermistors read by analogRead(), PWM driving RC filters, transistors, op-amps, diodes, MOSFETs, etc. Engine: eecircuit-engine (ngspice compiled to WebAssembly). Main bundle stays at 2.4 MB; the 20 MB SPICE chunk only loads when the user activates electrical mode. Disabled at build time via VITE_ELECTRICAL_SIM=false. Frontend additions: - simulation/spice/: SpiceEngine wrapper + lazy entry, NetlistBuilder with UnionFind over wires, componentToSpice mapping (24 metadataIds incl. real part numbers: 2N2222, 2N3055, BC547, IRF540, 2N7000, 1N4148, 1N4007, 1N4733, LEDs, NTC, op-amp ideal), CircuitScheduler with debounced coalescing, AVRSpiceBridge for quasi-static co-simulation. - store/useElectricalStore: Zustand slice, feature-flag aware. - components/analog-ui/: ⚡ toolbar toggle + SVG voltage overlay. - components/components-instruments/: Voltmeter, Ammeter probes. - 62 tests (spice-*, netlist-builder, component-to-spice, instruments). Sandbox (test/test_circuit/): 47-test validation sandbox that proved the approach (hand-rolled MNA baseline + ngspice pipeline) before porting to the app. Kept as reference. Docs: docs/wiki/circuit-emulation-*.md (13 engineering pages covering architecture, solvers, components, AVR bridge, gotchas, performance, integration plan, API reference, appendix) + electrical-simulation- user-guide.md (end-user facing). Reference plan: test/test_circuit/plan/phase_8_velxio_implementation.md Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 19:11:54 +07:00
import { describe, it, expect } from 'vitest';
import { runNetlist, NL } from '../simulation/spice/SpiceEngine';
describe('ngspice — transient analysis', () => {
it('RC charging: V(τ) ≈ 63.2% of Vsource (R=10k, C=100µF, V=5V)', { timeout: 30_000 }, async () => {
const netlist = `RC charging
${NL.pulse('V1', 'vcc', '0', 0, 5, 0, '1n', '1n', 10, 20)}
R1 vcc out 10k
C1 out 0 100u IC=0
.tran 10m 3
.ic v(out)=0
.end`;
const { vec } = await runNetlist(netlist);
const t = vec('time') as number[];
const v = vec('v(out)') as number[];
const tau = 10_000 * 100e-6; // 1 s
let bestI = 0;
let bestDt = Infinity;
for (let i = 0; i < t.length; i++) {
const d = Math.abs(t[i] - tau);
if (d < bestDt) {
bestDt = d;
bestI = i;
}
}
const vAtTau = v[bestI];
const expected = 5 * (1 - 1 / Math.E);
expect(vAtTau).toBeGreaterThan(expected * 0.97);
expect(vAtTau).toBeLessThan(expected * 1.03);
});
it('RLC oscillator: underdamped ringing with expected frequency', { timeout: 30_000 }, async () => {
// L=10mH, C=10µF → f0 ≈ 503 Hz. Light damping via R=1Ω.
const netlist = `RLC ringing
V1 in 0 PULSE(0 5 1m 1u 1u 1 2)
R1 in n1 1
L1 n1 out 10m
C1 out 0 10u IC=0
.tran 10u 30m
.end`;
const { vec } = await runNetlist(netlist);
const t = vec('time') as number[];
const v = vec('v(out)') as number[];
const crossings: number[] = [];
for (let i = 1; i < t.length; i++) {
if (t[i] < 2e-3) continue;
const a = v[i - 1] - 2.5;
const b = v[i] - 2.5;
if (a * b < 0) crossings.push(t[i]);
}
expect(crossings.length).toBeGreaterThan(4);
const diffs: number[] = [];
for (let i = 1; i < crossings.length; i++) diffs.push(crossings[i] - crossings[i - 1]);
const avgHalf = diffs.reduce((s, d) => s + d, 0) / diffs.length;
const measuredF = 1 / (2 * avgHalf);
const expectedF = 1 / (2 * Math.PI * Math.sqrt(10e-3 * 10e-6));
expect(measuredF).toBeGreaterThan(expectedF * 0.85);
expect(measuredF).toBeLessThan(expectedF * 1.15);
});
});