velxio/test/test_circuit/plan/phase_7_ngspice.md

80 lines
3.7 KiB
Markdown
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
# Fase 7 — Re-orientación a ngspice-WASM
## Cambio de estrategia
Tras la primera iteración con el solver MNA hand-rolled, el usuario pidió **no inventar** y usar un motor existente. Se evaluaron:
| Opción | Veredicto |
|---|---|
| `eecircuit-engine` (ngspice WASM, npm) | **ELEGIDA** — API simple, tipos TS, funciona en Node |
| `tscircuit/ngspice` (no publicado en npm) | descartada (hay que hacer fork) |
| `ngspice.js` (sitio separado) | descartada, no en npm |
| `spicejs` | descartada, sin API programable |
| `vollgas` | descartada, solo compuertas lógicas |
## Por qué eecircuit-engine
- Publicada en npm: `npm install eecircuit-engine`
- 39 MB descomprimida (una sola vez, lazy-load en producción)
- Boot en Node: ~400 ms
- Cada simulación posterior: 5500 ms según complejidad
- Soporte nativo para `.op`, `.tran`, `.ac`, `.dc`, `.noise`, fuentes behavioral (B-sources), diodos, BJT, MOSFET, inductores, `.model`, `.subckt`
## Qué queda vigente y qué cambia
### Vigente
- `src/avr/` — AVRHarness, parser Intel HEX, mini-assembler. Sin cambios.
- `fixtures/blink.hex` — sigue siendo la misma .hex que Velxio usa.
- Plan phases 06 como referencia histórica.
### Nuevo
- `src/spice/SpiceEngine.js` — wrapper de `eecircuit-engine` con helper `runNetlist(text)` que devuelve `{ raw, vec(name), dcValue(name) }`.
- `src/spice/AVRSpiceBridge.js` — co-simulación cuasi-estática entre `avr8js` y ngspice.
- Tests `test/spice_*.test.js`: passive, transient, AC, activos, digital, 555, mixed-signal.
### Legacy
- `src/solver/` — solver MNA hand-rolled. Los tests `passive.test.js`, `transient_rc.test.js`, `diodes.test.js` y los E2E del AVR **siguen pasando** y sirven como baseline de comparación contra ngspice. No se usan en la ruta principal.
## Estructura del pipeline final
```
┌──────────────────┐ ┌──────────────────┐
│ avr8js │◄────►│ AVRSpiceBridge │
│ (CPU, ADC, PWM) │ └─────────┬────────┘
└──────────────────┘ │
▲ ▼
│ ┌──────────────────┐
│ │ SpiceEngine │
│ │ (ngspice WASM) │
│ └─────────┬────────┘
│ (voltage inject) │
└────── v(node) sampling ────┘
```
En cada slice (por defecto 1 ms):
1. AVR corre N ciclos
2. Se snapshotean pines (digitales y PWM duty)
3. Se construye un netlist ngspice con los pines como fuentes
4. Se corre `.tran` en ngspice
5. Se samplean las nets que mapean a canales ADC del AVR
6. Se inyectan esos voltajes en `avr.setAnalogVoltage(ch, v)`
## Cobertura de primitivas ngspice validadas
| Primitiva | Archivo de test | Caso |
|---|---|---|
| R, V, I | `spice_passive.test.js` | divisor, paralelo, current source |
| L, C | `spice_transient.test.js` | RC charging, RLC ringing |
| AC sweep | `spice_ac.test.js` | RC low-pass Bode, LC bandpass |
| Diode | `spice_active.test.js` | forward drop, bridge rectifier |
| BJT | `spice_active.test.js` | common-emitter amplifier |
| MOSFET (L1) | `spice_active.test.js` | switch ON/OFF |
| E-source (op-amp) | `spice_active.test.js` | inverting amplifier |
| B-source (behavioral) | `spice_digital.test.js` | AND / NAND / XOR |
| Switch (S-element) con histéresis | `spice_555_astable.test.js` | oscilador relajación |
| `.op`, `.tran`, `.ac` | todos | |
| Mixed-signal AVR ↔ ngspice | `spice_avr_mixed.test.js` | NTC, PWM→RC, pot cosim |