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

124 lines
3.4 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 } from '../simulation/spice/SpiceEngine';
describe('ngspice — diode', () => {
it('1N4148-style diode forward drop is 0.550.80V @ ~4 mA', { timeout: 30_000 }, async () => {
const netlist = `Diode forward
V1 vcc 0 DC 5
R1 vcc a 1k
D1 a 0 DMOD
.model DMOD D(Is=2.52n N=1.752 Rs=0.568 Ibv=0.1u Bv=100)
.op
.end`;
const { dcValue } = await runNetlist(netlist);
const Va = dcValue('v(a)');
expect(Va).toBeGreaterThan(0.55);
expect(Va).toBeLessThan(0.80);
});
it('full-wave bridge rectifier outputs ~|Vin| 2·Vf', { timeout: 30_000 }, async () => {
const netlist = `Bridge rectifier
V1 a b SIN(0 6 50)
D1 a p DMOD
D2 b p DMOD
D3 n a DMOD
D4 n b DMOD
R1 p n 1k
.model DMOD D(Is=1e-14 N=1)
.tran 0.1m 40m
.end`;
const { vec } = await runNetlist(netlist);
const t = vec('time') as number[];
const vp = vec('v(p)') as number[];
const vn = vec('v(n)') as number[];
let peakOut = -Infinity;
let minOut = Infinity;
for (let i = 0; i < t.length; i++) {
if (t[i] < 20e-3) continue;
const d = vp[i] - vn[i];
if (d > peakOut) peakOut = d;
if (d < minOut) minOut = d;
}
expect(peakOut).toBeGreaterThan(4.2);
expect(peakOut).toBeLessThan(5.4);
expect(minOut).toBeGreaterThan(-0.1);
});
});
describe('ngspice — BJT', () => {
it('common-emitter amplifier inverts and amplifies a small signal', { timeout: 30_000 }, async () => {
const netlist = `Common-emitter
Vcc vcc 0 DC 12
Vin in 0 SIN(0 0.01 1k)
Cin in b 1u
RB1 vcc b 47k
RB2 b 0 10k
RC vcc c 4.7k
RE e 0 1k
CE e 0 100u
Q1 c b e Q2N2222
Cout c out 1u
Rout out 0 100k
.model Q2N2222 NPN(Is=1e-14 Bf=200 Vaf=75)
.tran 10u 6m
.end`;
const { vec } = await runNetlist(netlist);
const t = vec('time') as number[];
const vin = vec('v(in)') as number[];
const vout = vec('v(out)') as number[];
let maxIn = 0;
let maxOut = 0;
let minOut = Infinity;
for (let i = 0; i < t.length; i++) {
if (t[i] < 3e-3) continue;
if (Math.abs(vin[i]) > maxIn) maxIn = Math.abs(vin[i]);
if (vout[i] > maxOut) maxOut = vout[i];
if (vout[i] < minOut) minOut = vout[i];
}
const gain = (maxOut - minOut) / (2 * maxIn);
expect(gain).toBeGreaterThan(30);
});
});
describe('ngspice — MOSFET', () => {
it('N-MOS switch: V_GS > Vth pulls drain to ground', { timeout: 30_000 }, async () => {
const netlist = `N-MOS switch
Vcc vcc 0 DC 5
Vgate gate 0 DC 5
RL vcc drain 1k
M1 drain gate 0 0 NMOS_L1 L=1u W=100u
.model NMOS_L1 NMOS(Level=1 Vto=1.0 Kp=50u Lambda=0.01)
.op
.end`;
const { dcValue } = await runNetlist(netlist);
expect(dcValue('v(drain)')).toBeLessThan(1.0);
});
it('N-MOS switch: V_GS < Vth leaves drain near V_dd', { timeout: 30_000 }, async () => {
const netlist = `N-MOS off
Vcc vcc 0 DC 5
Vgate gate 0 DC 0
RL vcc drain 1k
M1 drain gate 0 0 NMOS_L1 L=1u W=100u
.model NMOS_L1 NMOS(Level=1 Vto=1.0 Kp=50u Lambda=0.01)
.op
.end`;
const { dcValue } = await runNetlist(netlist);
expect(dcValue('v(drain)')).toBeGreaterThan(4.9);
});
});
describe('ngspice — op-amp (behavioral E-source)', () => {
it('inverting amplifier: V_out = (R_f/R_in) · V_in', { timeout: 30_000 }, async () => {
const netlist = `Inverting amp
Vin in 0 DC 0.2
Rin in n 1k
Rf n out 10k
Eopa out 0 0 n 1e6
.op
.end`;
const { dcValue } = await runNetlist(netlist);
expect(dcValue('v(out)')).toBeCloseTo(-2.0, 2);
});
});