Skip to content
Back to skills

Circuit Simulation

ASecurity

This skill should be used when the user wants a circuit checked by simulation before it is built, for example "simulate this circuit", "SPICE", "ngspice", "LTspice", "worst case", "tolerance" or "Monte Carlo". It writes and runs ngspice netlists with corners and checks each result against a hand calculation. Load it before predicting how a circuit behaves when a simulation can show it.

  • 3 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 1, 2026
ai-agentspythongobashnodeexpresstesting

Works with

  • cli

Security analysis

A100/100

Pro scans all 20 files and shows the line behind each finding

Scanned December 1, 2026

npx -y skills add gkochievi/boulder-hardware-skills --skill circuit-simulation --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Circuit Simulation?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Circuit Simulation
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/gkochievi-circuit-simulation/badge)](https://www.skillsdirectory.com/skills/gkochievi-circuit-simulation)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: circuit-simulation
description: This skill should be used when the user wants a circuit checked by simulation before it is built, for example "simulate this circuit", "SPICE", "ngspice", "LTspice", "worst case", "tolerance" or "Monte Carlo". It writes and runs ngspice netlists with corners and checks each result against a hand calculation. Load it before predicting how a circuit behaves when a simulation can show it.
user-invocable: false
---

# Circuit simulation

Answer circuit questions with ngspice 42 in batch mode: write the smallest netlist that answers the question, measure the answer with `.meas`, check it against a hand calculation from `electronics-fundamentals`, run the tolerance corners that matter, and report the result with its limits. A simulation is evidence only when its question, model limits and hand check are written next to it.

Tools in this skill: `scripts/spice_run.py` (one run: measures, vectors, CSV, PNG, JSON), `scripts/what_if.py` (corners, sensitivity, Monte Carlo, sweeps), `scripts/eseries.py` (E6–E192 values and divider ratios), generic models in `models/`, and seven worked example netlists in `examples/`, each with an `.expected.md` note.

## How to work

1. **State the question the simulation must answer**, with its limit and conditions: "Does the 1 kHz Sallen-Key keep its −3 dB point within 1000 ± 5 % with 1 % resistors and 5 % C0G?" Name the output, the pass/fail limit, the input range, the temperature range and the life. Put the question and the hand-calculated expectation in the netlist header, as the examples do. If the user gives no limit, propose one and say so.

2. **Build the smallest circuit that answers it.**
   - Replace the MCU, sensors and other blocks with sources (`PULSE` for a GPIO, `PWL` for a ramp, a resistor for a load).
   - Keep the parasitics that change the answer: capacitor ESR, inductor DCR, gate resistors, source impedance, the ADC's input. Leave out the rest.
   - One question per netlist; two variants side by side in one netlist are fine (with and without a diode, switching and averaged).
   - Parameterize with `.param` every value that a tolerance or sweep will change, and write values as plain decimals (`4.7k`, never `4k7`).

3. **Choose models and write down their limits.**
   - Vendor SPICE model when the result depends on the part (phase margin, switching edges, recovery, saturation): download it from the manufacturer, read its `.subckt` pin order, run it with `--compat ltpsa` if it was written for PSpice or LTspice, and first reproduce one datasheet curve on a test bench.
   - The generic models in `models/` when the circuit, not the part, is the question; each file starts with its validity limits. Say "generic, AO3400A-like" in the report, never the part name alone.
   - Ideal elements (E, G, B sources, switches) for loop equations, averaged converters and quick what-ifs.
   - Never present a generic-model result as a part guarantee.

4. **Pick the analysis.**

   | Question | Analysis |
   |---|---|
   | bias point, DC levels, dissipation | `.op`, `.dc` sweep (input, temperature, a resistor) |
   | gain, bandwidth, filter shape, PSRR, output impedance | `.ac` with one `AC 1` source |
   | noise of an amplifier or ADC front end | `.noise v(out) VIN dec 20 10 100k` |
   | edges, overshoot, load steps, start-up, ripple, flyback voltage, energy | `.tran` with `tmax` set |
   | converter control or start-up over many cycles | averaged model in `.tran` |
   | tolerance, yield, a value choice | `what_if.py` on the netlist |

5. **Measure with `.meas`, not by eye.** Write one measure per number in the question (`fc`, `vd_peak`, `dip`, `vout_ripple_pp`). Rules that avoid wrong numbers:
   - AC: use `vdb()`, `vm()`, `vp()` (radians); plain `v()` is the real part.
   - Thresholds may use parameters (`when vdb(out)={G0DB-3.0103}`) but not earlier measure results; combine results with `param='...'`.
   - `DERIV` fails in ngspice 42: take slopes from two `find` measures.
   - Read currents through a 0 V source (`VS1`), not through an inductor, when a `par()` or B-source expression needs them.
   - Measure averages and ripple over whole periods after the circuit has settled.

6. **Run it with `spice_run.py` and read the whole result.**

   ```bash
   python3 scripts/spice_run.py net.cir --vec "vdb(out)" --png out.png --json
   python3 scripts/spice_run.py net.cir --param RV=12k --expect fc=1400:1700
   ```

   Check `status`, `failed_measures`, `warnings` and `convergence` before using any number. On a new circuit, print the operating point once (`--op`) and check it by hand: a wrong operating point can be accepted without a warning. Fix convergence problems at the circuit (finite edges, series resistance, no floating nodes) before touching `.options`; the symptom table is in `references/ngspice-guide.md` section 6.

7. **Run the tolerance corners that matter.**
   - Sensitivity first: `what_if.py net.cir --mode sensitivity --tol R1=10k:1% --tol C1=10n:5% ...` shows which parts move the result.
   - Full corners on the parameters that matter (`--mode corners`, at most 10; more are capped with a warning, `--sensitivity-first` drops weak ones).
   - Monte Carlo (`--mode montecarlo --runs 1000 --seed 1`) for production spread and for outputs that are not monotonic (filter peaking, resonance).
   - Include temperature coefficients and ageing in the tolerances (`references/tolerance-and-worst-case.md` sections 2 and 6), plus reference and supply accuracy.
   - Snap values to the series that will be bought (`--snap E96`); use `--mode sweep --sweep R2=8k:12k:E24 --target code=3000` or `eseries.py divider/ldo` to suggest values; `--spec MEAS=MIN:MAX` gives a pass/fail exit code.

8. **Compare with the hand calculation.** Every reported number needs an independent estimate from `electronics-fundamentals` (formulas below and in each example header). Agreement within the expected model error (usually under 1–5 %) supports both; a larger difference means a wrong netlist, a wrong model or a wrong formula, so find which before reporting. Typical checks:

   | Circuit | Hand calculation | Reference |
   |---|---|---|
   | RC low-pass | fc = 1/(2πRC) | `electronics-fundamentals/references/passives-and-rlc.md` section 4 |
   | divider into ADC | Vout = Vin·R2/(R1+R2), code = Vout/VREF·2ᴺ, source R1∥R2 | same, section 5; `analog-and-opamps.md` section 12 |
   | op-amp stage | G = 1 + RF/RG, f−3dB ≈ GBW/noise gain, offset = VOS·noise gain | `electronics-fundamentals/references/analog-and-opamps.md` sections 2, 4, 5 |
   | Sallen-Key | f0 = 1/(2πR√(C1C2)), Q = ½√(C1/C2) (unity gain, equal R) | same, section 8 |
   | LDO | dropout = I·RON, P = (Vin − Vout)·I, Vout = VREF(1 + R1/R2) | `electronics-fundamentals/references/power-and-batteries.md` section 2 |
   | buck | D ≈ (Vout + VF)/(Vin + VF), ΔIL = (Vin − Vout)D/(fL), ΔV ≈ ΔIL/(8fC) and ΔIL·ESR | same, section 4 |
   | inductive load | clamp = Vdd + VF (diode) or V(BR) (none), E ≤ ½LI²·Vclamp/(Vclamp − Vdd) | `electronics-fundamentals/references/semiconductors.md` section 5 |

9. **Report.** Give: the question and limit; the netlist (path or inline); models used and their limits; analyses; a results table (nominal, worst-case minimum and maximum with the corner, RSS or Monte Carlo σ); the hand-calculation comparison; plots from `--png` (one panel per vector); the verdict; and what the simulation does not cover (layout parasitics, EMC, thermal, part-to-part effects outside the tolerances, generic-model limits). End with the bench measurement that would confirm the result (`electronics-fundamentals/references/measurement-and-debug.md`). For a design record, save the netlist and its `.expected.md`-style note with the project.

## KiCad 10 schematics

- `kicad-cli` 10.0.6 has no simulate command. Export the netlist and run it here:

  ```bash
  kicad-cli sch export netlist --format spice -o board.cir board.kicad_sch
  python3 scripts/spice_run.py board.cir --meas ".meas ac f3db when vdb(/out)=-3.0103" --vec "vdb(/out)"
  ```

- Keep the `.kicad_pro` next to the schematic: without it, relative `Sim.Library` paths fail and kicad-cli 10.0.6 drops the model parameters (exit code 2).
- Net names arrive as `/LABEL` and ground as `GND` (ngspice maps it to `0`). Use `v(/out)` in measures and vectors.
- Symbols carry their model in `Sim.Device`, `Sim.Type`, `Sim.Library`, `Sim.Name`, `Sim.Pins`, `Sim.Params`; dot-command text on the sheet becomes netlist directives. If the sheet has no analysis, add one with `--analysis ".tran 1u 10m"`.
- KiCad's simulator sets an ngspice compatibility mode in its settings; pass the same with `--compat` (for example `ltpsa`).
- For generated boards (`kicad-generator`), simulate the critical blocks as separate netlists before layout.

Details: `references/ngspice-guide.md` section 10.

## Defaults that catch most mistakes

- Ground is node `0` (or `gnd`); `M` means milli, `meg` mega; `4k7` reads as 4 kΩ in plain ngspice.
- Every source edge gets a finite rise and fall time; every L and C a series resistance; every switch a finite RON/ROFF.
- `tmax` at one tenth of the shortest edge or ringing period you want to see; no `.options interp` when measuring.
- AC sweeps: 20–50 points per decade, one AC source, magnitude 1.
- Run long enough to settle; check that the result does not change when `tmax` is halved.
- A generic model answers circuit questions, not part questions; a vendor model answers part questions only inside its own stated limits.
- Corners for guarantees, Monte Carlo for spread, sensitivity to choose which parts to tighten.

## Scripts

| Script | Use for | Key options |
|---|---|---|
| `scripts/spice_run.py NETLIST` | one batch run: `.meas` results, failed measures, warnings, convergence messages, runtime; operating point; vectors to CSV and PNG | `--vec`, `--param NAME=VALUE`, `--op`, `--meas`, `--analysis`, `--line`, `--expect NAME=MIN:MAX`, `--csv`, `--png`, `--compat`, `--timeout`, `--keep`, `--json` |
| `scripts/what_if.py NETLIST` | tolerance corners (2ⁿ, n ≤ 10), sensitivity with directed worst case, Monte Carlo, sweeps; min/max/mean per measure and the corner that gives each | `--mode corners|sensitivity|montecarlo|sweep`, `--tol NAME=NOM:TOL`, `--sweep`, `--snap E96`, `--target`, `--spec`, `--runs`, `--seed`, `--dist`, `--csv`, `--json` |
| `scripts/eseries.py` | nearest E-series value, values in a range, divider and LDO feedback ratios with error and worst case | `nearest`, `list`, `range`, `ratio`, `divider`, `ldo`, `--series`, `--rtotal`, `--json` |

All three print one JSON object with `--json` (`status`, `action`, `summary`, `details`, `error`) and exit 0 (ok/warn), 1 (an `--expect` or `--spec` check failed) or 2 (usage or runtime error). `--param` changes a top-level `.param` in place, or an R/C/L/V/I element of that name. Configuration: flags, then `[spice_run]` in `boulder.toml` (`ngspice`, `timeout_s`, `compat`), then `BOULDER_NGSPICE`. Tests: `python3 -m pytest -q` in `scripts/` runs every example against its hand calculation.

## Models

| File | Contents | Good for | Not for |
|---|---|---|---|
| `models/opamp_ideal.lib` | `OPAMP_1P` (and `OPAMP_1P_SS`): single pole, GBW, AOL, slew rate, VOS, IB, IOS, output swing, ROUT | closed-loop gain and bandwidth, filters, DC error budgets, clipping | stability, capacitive loads, noise, supply current |
| `models/ldo_behav.lib` | `LDO_BEHAV`: resistive dropout, current limit, load regulation, loop output impedance, rough PSRR | load steps, dropout, dissipation, input current | stability with a given Cout/ESR, start-up |
| `models/mosfet_switch.lib` | `NMOS_GEN_30V` (AO3400A-like), `PMOS_GEN_30V` (AO3401A-like), VDMOS fitted to datasheet typicals | load switches, flyback, gate drive, conduction loss | SOA, linear mode, avalanche ruggedness |
| `models/diode_generic.lib` | `D1N4148_GEN`, `DBAT54_GEN`, `D1N5819_GEN`, fitted to datasheet tables | clamps, flyback, freewheeling, rectifiers | leakage over temperature, exact recovery |

## Examples

| Netlist | Shows |
|---|---|
| `examples/rc_lowpass_ac.cir` | AC sweep, −3 dB and phase measures |
| `examples/divider_adc_corners.cir` | DC sweep, ADC code with `par()`, tolerance corners |
| `examples/opamp_noninverting_ac.cir` | closed-loop gain and GBW-limited bandwidth |
| `examples/sallen_key_lowpass_ac.cir` | f0, Q, peaking and the stopband floor of a real op-amp |
| `examples/mosfet_lowside_inductive_tran.cir` | flyback voltage, decay time and avalanche energy with and without a diode |
| `examples/ldo_load_step_tran.cir` | load-step dip, release overshoot, dropout |
| `examples/buck_averaged_or_switching_tran.cir` | switching and averaged buck side by side, ripple |

## References

| File | Use for |
|---|---|
| `references/ngspice-guide.md` | netlist syntax, sources, analyses, `.meas` forms and pitfalls, control blocks and batch output, convergence, switching circuits, subcircuits and libraries, vendor models and compatibility modes, KiCad 10 hand-over, ngspice versus LTspice |
| `references/tolerance-and-worst-case.md` | sources of variation (tolerance, TCR, ageing, DC bias, references), E-series, worst case vs corners vs RSS vs Monte Carlo, choosing corners, worked examples (ADC divider, RC cut-off, LDO feedback divider), reporting |

## Related skills

- `electronics-fundamentals`: the formulas, part choices and derating that every simulation is checked against.
- `pcb-design`: layout parasitics and thermal limits a schematic simulation does not include.
- `kicad-generator`: the KiCad projects whose netlists can be exported and simulated here.
- `board-review`: cite simulation results as evidence, with their model limits.
- `ask-gela`: keep netlists and expected-result notes in the project's design records.
- `embedded-mentor`: show a learner the turn-off kick or a filter response before the bench.
- With the Engineering plugin: `/engineering:testing-strategy` to turn simulated limits into bench and production tests.

## Next step

When this is done, say what comes next: the proven values go into the netlist (`kicad-generator`) and the result into the decision log.

Files in this skill

  • SKILL.md13.9 KB
  • examples/buck_averaged_or_switching_tran.cir2.5 KB
  • examples/buck_averaged_or_switching_tran.expected.md1.8 KB
  • examples/divider_adc_corners.cir1.1 KB
  • examples/divider_adc_corners.expected.md2 KB
  • examples/ldo_load_step_tran.cir1.5 KB
  • examples/ldo_load_step_tran.expected.md1.6 KB
  • examples/mosfet_lowside_inductive_tran.cir2.3 KB
  • examples/mosfet_lowside_inductive_tran.expected.md2.4 KB
  • examples/opamp_noninverting_ac.cir916 B
  • examples/opamp_noninverting_ac.expected.md1.3 KB
  • examples/rc_lowpass_ac.cir906 B
  • examples/rc_lowpass_ac.expected.md1010 B
  • examples/sallen_key_lowpass_ac.cir1.2 KB
  • examples/sallen_key_lowpass_ac.expected.md1.5 KB
  • models/diode_generic.lib3.6 KB
  • models/ldo_behav.lib4.2 KB
  • models/mosfet_switch.lib4 KB
  • models/opamp_ideal.lib4.2 KB
  • references/ngspice-guide.md30.5 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.