ngspice
A guide for circuit simulation and netlist management in ngspice.
Install
mkdir -p .claude/skills/ngspice && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/13444" && unzip -o skill.zip -d .claude/skills/ngspice && rm skill.zipInstalls to .claude/skills/ngspice
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Use when writing or editing ngspice circuit netlists (.cir, .sp, .spice), working with ngspice scripting (.control blocks), or interpreting simulation results. Covers ngspice-specific SPICE syntax, behavioral sources, .control scripting, Monte Carlo via control loops, parameters, XSPICE, .save, .MEAS, convergence, and common gotchas that cause silent errors. Use this skill whenever the user mentions ngspice, or is writing SPICE netlists targeting ngspice rather than LTspice.Key capabilities
- →Write or edit ngspice circuit netlists
- →Work with ngspice scripting (.control blocks)
- →Interpret simulation results
- →Define component syntax and values with correct suffixes
- →Configure various simulation directives (tran, ac, dc, op, noise, tf)
- →Perform measurements using .MEAS syntax
How it works
This skill guides the user in writing ngspice netlists and control scripts, explaining syntax, directives, and common pitfalls. It covers various simulation types and measurement capabilities specific to ngspice.
Inputs & outputs
When to use ngspice
- →Writing ngspice netlists
- →Running transient simulations
- →Debugging convergence issues
- →Creating Monte Carlo loops
About this skill
ngspice Circuit Simulation Guide
SPICE Fundamentals
Netlist Structure
* Title line (first line, always a comment)
<components>
<directives>
.END
.ENDmust be last line. No statements after it.+at start of line continues previous statement.- Comments:
*(full line) or$(inline). Note:;is NOT the inline comment character in ngspice (that's LTspice).
Component Syntax
<ref> <node+> <node-> <value>
R1 in out 10k
C1 out 0 100n
V1 in 0 AC 1 PULSE(0 5 0 1n 1n 0.5m 1m)
Value Notation — CRITICAL
| Suffix | Meaning | Value |
|---|---|---|
| f | femto | 1e-15 |
| p | pico | 1e-12 |
| n | nano | 1e-9 |
| u | micro | 1e-6 |
| m | milli | 1e-3 |
| k | kilo | 1e3 |
| MEG | mega | 1e6 |
| G | giga | 1e9 |
| T | tera | 1e12 |
M means MILLI, not mega. Use MEG for 1e6.
This is the #1 SPICE mistake. 1M = 0.001, not 1000000.
Unrecognized suffix letters are silently ignored — no error, just wrong value.
Waveform Sources
PULSE(Vinitial Vpulse Tdelay Trise Tfall Ton Tperiod Ncycles)
SINE(Voffset Vamp Freq Td Theta Phi Ncycles)
EXP(V1 V2 Td1 Tau1 Td2 Tau2)
SFFM(Voff Vamp Fcar MDI Fsig)
PWL(t1 v1 t2 v2 ...)
Directives
.tran 5m $ transient, 5ms stop
.tran 0 5m 0 10u $ tstep, tstop, tstart, tmaxstep
.ac dec 200 10 100k $ AC sweep, 200pts/decade, 10Hz-100kHz
.dc V1 0 5 0.01 $ DC sweep V1, 0-5V, 10mV step
.op $ DC operating point
.noise V(out) V1 dec 200 10 100k $ noise analysis
.tf V(out) V1 $ DC transfer function
.include /path/to/model.lib $ include library
.ic V(node)=1.5 $ initial conditions (used with UIC)
.nodeset V(node)=1.5 $ hint for DC operating point solver
.ic forces node voltages at t=0 (use with .tran ... UIC). .nodeset is a solver hint only.
.MEAS Syntax
.meas TRAN vmax MAX V(out)
.meas TRAN vpp PP V(out)
.meas TRAN trise TRIG V(out) VAL=0.1 RISE=1 TARG V(out) VAL=0.9 RISE=1
.meas AC fc WHEN mag(V(out)/V(in))=0.707
.meas AC gain_1k FIND V(out) AT=1k
.meas TRAN avg_out AVG V(out) FROM=1m TO=5m
.meas TRAN energy INTEG V(out)*I(R1)
.meas TRAN slope DERIV V(out) AT=2m
.meas TRAN check param='tdiff < vout_diff ? 1 : 0'
Additional measurement types vs LTspice:
MIN_AT,MAX_AT— return the time/freq of the min/max, not the value.DERIV— derivative at a point or when a condition is met.param='expression'— evaluate an expression using.paramvalues and prior.measresults.par('expression')— inline algebraic expression on any output variable (uses B source syntax internally).SPanalysis type for spectrum (fft) measurements (viameascommand, not.measline).
Gotchas:
- RISE/FALL/CROSS numbering starts at 1, not 0.
- If TRIG event never occurs, measurement silently fails.
.measis refused when batch mode (-b) is combined with-r rawfile— the invocationrun_simulationuses. ngspice printsNo .measure possible in batch mode (-b) with -r rawfile set!and computes nothing. Recovery: move the measurement into a.control ... run ... .endcblock written as the DOT-LESSmeascommand — e.g.meas tran vmax MAX V(out)(a dotted.measinside.controlis not a valid command and silently does nothing). The result prints to the run's log.paramandparare not available inside.controlblocks — useletinstead.
General Pitfalls
- Node "0" is ground. Using
GNDwithout.global GNDor tying it to 0 creates a floating node — no error, wrong results. - MOSFET requires 4 terminals:
M1 d g s b— ngspice does NOT auto-connect bulk to source (LTspice does). - Impedance ratios: Beyond ~1e16 cause numerical issues (64-bit doubles).
- Parameter sweep: ngspice has no native
.step(that is LTspice syntax). The MCP runs parametric sweeps throughconfigure_sweep+run_sweep, which generate and simulate one netlist per value. For a hand-written deck outside the MCP, use a.controlblock with analter/loop. A.stepline in a deck handed torun_simulationis rejected with a pointer toconfigure_sweep.
ngspice-Specific
Parameters and Expressions
.param Rval=10k
.param fc={1/(2*pi*Rval*Cval)}
.param combined='Rval + 10'
.func myfn(x) {x*2}
- Expressions in braces
{expr}or single quotes'expr'— both work. - Expressions without delimiters work only when spaces are absent:
.param c=a+123OK,.param c = a + 123FAILS silently (assigns only first token). - Self-referential params fail silently:
.param x = {x+3}does not work. - Parameter names: must start with alpha; may contain
! # $ % [ ] _. Cannot use reserved words:time,temper,hertz,not,and,or,div,mod,sqr,sqrt,sin,cos,exp,ln,log,log10,arctan,abs,pwr,defined. - String-valued params supported with limited concatenation.
Three separate expression parsers exist in ngspice — this is a known source of confusion:
- Front-end (
.param, brace expressions) — evaluated at netlist expansion time - B source / behavioral — evaluated during simulation (no braces)
.controlblock — operates on its own vectors/variables
These have slightly different function sets and precedence rules. Braces {...} are "compile-time"; bare expressions in B sources are "run-time".
Operator precedence (.param expressions):
| Op | Prec | Description |
|---|---|---|
! | 1 | unary NOT |
**, ^ | 2 | power |
* | 3 | multiply |
/, %, \ | 3 | divide, modulo, integer divide |
+, - | 4 | add, subtract |
==, !=/<> | 5 | equality |
<=, >=, <, > | 5 | comparison |
&& | 6 | boolean AND |
|| | 7 | boolean OR |
c ? x : y | 8 | ternary |
^ behavior depends on compatibility mode:
- Default (
hscompat):x^y=pow(fabs(x), y)for x>0; rounds y for x<0; 0 for x=0 - LTspice compat (
lt):x^y=pow(x, y)if y is close to integer; else 0 for x<0
Built-in functions (.param):
- Trig:
sin,cos,tan,asin,acos,atan,arctan - Hyperbolic:
sinh,cosh,tanh,asinh,acosh,atanh - Exp/log:
exp,ln,log(base e),log10 - Power:
sqrt,pow(x,y),pwr(x,y)(=pow(fabs(x),y)) - Rounding:
nint(nearest, half to even),int(toward 0),floor,ceil - Selection:
min,max,sgn - Conditional:
ternary_fcn(x,y,z)(=x ? y : z) - Statistical:
gauss(nom,rvar,sigma),agauss(nom,avar,sigma),unif(nom,rvar),aunif(nom,avar),limit(nom,avar) - Special:
var(name)(interpreter variable),vec(name)(vector value)
Behavioral Sources (B sources)
B1 out 0 V=<expression>
B2 out 0 I=<expression> [tc1=x] [tc2=x] [temp=x]
Conditional: uses ternary cond ? true : false — NOT IF() (that's LTspice). Put a space before ? so the parser doesn't confuse it with other tokens. Nested ternaries need explicit parentheses.
Available functions (B source context): cos, sin, tan, acos, asin, atan, cosh, sinh, acosh, asinh, atanh, exp, ln, log, log10, abs, sqrt, u (unit step), u2 (ramp 0-1), uramp, floor, ceil, min, max, pow, **, pwr, ^, i(device)
Special variables: time (transient), temper (circuit temp in C), hertz (AC frequency). time is zero during AC; hertz is zero during transient.
Piecewise linear in B source:
Bdio 1 0 I = pwl(v(A), 0,0, 33,10m, 100,33m, 200,50m)
Blimit b 0 V = pwl(v(1), -4,0, -2,2, 2,4, 4,5, 6,5)
x values must be monotonically increasing — non-monotonic stops execution. Can use time or expressions as the independent variable.
Gotchas:
exp()is internally capped at argument=14 — beyond that it becomes linear (for convergence).log/ln/sqrtof negative values usefabs()automatically — no error, may give unexpected results.- Division by zero or
log(0)causes an error. - B source
par('expression')can be used in.plot/.printoutput lines. - Non-linear R/L/C can be synthesized from B sources using the subcircuit template pattern (see ngspice manual 5.1).
Subcircuits
.subckt myfilter in out rval=100k cval=100nF
R1 in p1 {2*rval}
C1 p1 0 {cval}
.ends myfilter
X1 input output myfilter rval=1k cval=1n
Key differences from LTspice:
- Parameters on
.subcktline do NOT needparams:keyword — justname=valueafter nodes. .libbehavior is compatibility-mode dependent, and no single.libform works in every mode — so for unconditional whole-file inclusion use.include <file>, which resolves in every ngspice mode (verified on ngspice-42). If you use.lib:- ngspice-native modes (
hsa, plain default):.lib <file> <section>loads the named.lib section … .endlblock; a bare.lib <file>with no section does NOT load the file's models. - This server's default
kiltpsa(a PSPICE-family mode) inverts this: a bare.lib <file>loads an unsectioned file, but a sectioned.lib <file> <section>(the PDK corner-select idiom) is mis-split by thelt/pstokens into two plain includes that drop the section, surfacing as a missing include (could not find include file). Set[simulator] ngbehavior = "hsa"inltspice-mcp.toml(orLTSPICE_MCP_NGBEHAVIOR=hsa) and restart the server to parse the section;run_simulationemits this hint when a failed run matches the pattern.
- ngspice-native modes (
.paraminside subcircuits is local scope (masks globals). Nesting up to 10 levels.- Subcircuit and model names are global — must be unique across the entire netlist.
.save Directive
.save V(out) I(Vin) $ save only these signals
.save @m1[id] @m1[gm] $ save internal device parameters
.save all @m2[vdsat] $ save defaults PLUS extras
- Without
.save, all node voltages and source cur
Content truncated.
When not to use it
- →When writing SPICE netlists targeting LTspice instead of ngspice
- →When a graphical schematic capture tool is preferred over netlist editing
- →When performing simulations that require features not supported by ngspice
Limitations
- →M means MILLI, not mega; use MEG for 1e6
- →Unrecognized suffix letters are silently ignored
- →.meas does NOT work in batch mode when combined with -r rawfile
How it compares
This skill provides specific guidance for ngspice, highlighting its unique syntax, directives, and behavioral differences from other SPICE simulators like LTspice, which is crucial for accurate simulation results.
Compared to similar skills
ngspice side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| ngspice (this skill) | 0 | 1mo | Review | Advanced |
| senior-devops | 7 | 7mo | Review | Advanced |
| server-management | 1 | 6mo | No flags | Intermediate |
| azure-cloud-services | 0 | 4mo | No flags | Intermediate |
Try saying
Example prompts that trigger this skill in your AI assistant.
You might also like
senior-devops
davila7
Comprehensive DevOps skill for CI/CD, infrastructure automation, containerization, and cloud platforms (AWS, GCP, Azure). Includes pipeline setup, infrastructure as code, deployment automation, and monitoring. Use when setting up pipelines, deploying applications, managing infrastructure, implementing monitoring, or optimizing deployment processes.
server-management
davila7
Server management principles and decision-making. Process management, monitoring strategy, and scaling decisions. Teaches thinking, not commands.
azure-cloud-services
ComeOnOliver
This skill provides expert guidance for Azure Cloud Services. Covers troubleshooting, best practices, decision making, limits & quotas, security, configuration, integrations & coding patterns, and deployment. It combines local quick-reference content with remote documentation fet
mimir
grafana
>
huawei-event-driven-architecture-review
Raishin
Review Huawei Cloud event-driven architecture designs — DMS Kafka dead-letter configuration, ROMA Connect integration flow capacity, FunctionGraph event trigger idempotency, SMN delivery retry policy, consumer group lag monitoring, cross-region event replication, and retry storm prevention.
iot-engineer
Sneha-1608
Use when designing and deploying IoT solutions requiring expertise in device management, edge computing, cloud integration, and handling challenges like massive device scale, complex connectivity scenarios, or real-time data pipelines.