velxio/test/test_circuit/plan/phase_2_solver.md

63 lines
3.9 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 2 — Diseño del solver
## Arquitectura
```
┌─────────────────────────────────────────────────────────────┐
│ Circuit (nodos + componentes) │
│ │
│ addNode('vcc'), addNode('out'), addNode('gnd') │
│ addComponent(new Resistor('R1','vcc','mid',1000)) │
│ addComponent(new VoltageSource('V1','vcc','gnd',5)) │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ MNA Solver │
│ │
│ Construye matriz G (conductancias) de tamaño (N+M) │
│ N = nodos no-tierra │
│ M = fuentes de voltaje │
│ Vector b (corrientes + voltajes fijos) │
│ Resuelve Gx = b (x = [V_nodos, I_fuentes]) │
│ │
│ Para diodos / LEDs: Newton-Raphson iterativo sobre stamp │
│ lineal con conductancia gd y corriente │
│ equivalente Ieq │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ Transient Solver (backward Euler) │
│ │
│ Capacitor stamp: G_c = C/Δt, I_eq = C/Δt · V(t-Δt) │
│ Inductor stamp: G_l = Δt/L (pseudo), usamos trapezoidal │
│ En cada paso: solver DC con stamps dependientes del último │
│ estado. │
└─────────────────────────────────────────────────────────────┘
```
## Componentes soportados (fase 24)
| Componente | Stamp | Modelo |
|---|---|---|
| `Resistor` | G = 1/R en (a,a), (b,b), -G en (a,b), (b,a) | lineal |
| `VoltageSource` | fila+col extra, 1 en nodo+ 1 en nodo, V en b | lineal |
| `CurrentSource` | I en b[nodo+], -I en b[nodo] | lineal |
| `Capacitor` | backward Euler: G=C/Δt, Ieq=C/Δt·V_prev | transient |
| `Inductor` | trapezoidal opcional; por ahora tratado como fuente I | transient |
| `Diode` | Shockley: I = Is(e^(V/nVt)1), linealiza como (gd, Ieq) iterando | no-lineal |
| `LED` | diodo con Is, n y color (info visual); misma iteración | no-lineal |
| `NTCThermistor` | R(T) = R0 · e^(β(1/T1/T0)) | lineal param. |
| `Potentiometer` | 2 resistores en serie controlados por `wiperPos` | lineal param. |
| `Switch` | R muy grande (abierto) / muy pequeña (cerrado) | lineal |
| `BJT` (opcional) | Ebers-Moll simplificado; iteración Newton | no-lineal |
## Convergencia Newton-Raphson
- Máx. 100 iteraciones
- Tolerancia: `max(|V_i V_i-1|) < 1e-6 V` y `max(|I_i I_i-1|) < 1e-9 A`
- Damping adaptativo si oscila
## Fichero principal
`src/solver/MNASolver.js` — clase `Circuit` con métodos `addNode`, `addComponent`, `solveDC()`, `solveTransient(tEnd, dt)`.