In Spectre you have always been able to write save x1.x2.m3:d and get the current into the drain of a transistor buried two levels down. ngspice had no such thing. Our ngspice branch now does: .probe I(x1.x2.m3,d) saves the current into any terminal of any instance, at any depth, in every analysis.
The gap
ngspice had three ways to get at a terminal current, and none of them was the real thing:
.probe I(m1,d) worked for instances at the top level only. Name anything inside a subcircuit and the probe was dropped with a warning.
- Device parameters such as
@m.x1.x2.m3[id]. Their names change from model to model, and they are operating-point quantities, so an AC analysis gives you the bias current, not the small-signal current. They are also read from the Newton iterate before the last, so they agree with the solution only within the solver's tolerance.
- A hand-placed 0 V source inside the subcircuit. It works, but it edits the subcircuit, so every instance of it gets a source. Every probe you move means another edit.
Here is what the previous build said about a deck that asks for four currents inside a hierarchy:
Warning: Node o is not available for device x1,
.probe i(x1,o) will be ignored
Warning: Could not find the instance line for x1.r1,
.probe i(x1.r1) will be ignored
Warning: Could not find the instance line for x1.x2,
.probe i(x1.x2,s) will be ignored
Warning: Could not find the instance line for x1.x2.m3,
.probe i(x1.x2.m3) will be ignored
What it looks like now
The same deck: an amplifier x1 holding a resistor, a second-level instance x2 with one NMOS, and a resistor to ground.
* terminal currents two levels down
vdd vdd 0 dc 2
vg g 0 dc 1.5
x1 vdd g out amp
rl out 0 10k
.subckt amp p in o
r1 p o 1k
x2 in o mid stage
r2 mid 0 2k
.ends
.subckt stage i d s
m3 d i s 0 nmod w=2u l=1u
.ends
.model nmod nmos level=1 vto=0.5 kp=100u
.probe I(x1.x2.m3) I(x1.x2,s) I(x1.r1) I(x1,o)
.control
op
print i(x1.x2.m3:d) i(x1.x2.m3:g) i(x1.x2.m3:s) i(x1.x2.m3:b_tj)
print i(x1.x2:s) i(x1.r1) i(x1:o)
.endc
.end
i(x1.x2.m3:d) = 7.294902e-05
i(x1.x2.m3:g) = 0.000000e+00
i(x1.x2.m3:s) = -7.29490e-05
i(x1.x2.m3:b_tj) = -1.91776e-12
i(x1.x2:s) = -7.29490e-05
i(x1.r1) = 2.481355e-04
i(x1:o) = -1.75186e-04
I(x1.x2.m3) with no terminal gives all four. The four sum to −1.2e-19 A, so KCL closes to rounding. The port x1.x2:s carries exactly the source current, because nothing else is on it. Inside x1, the 248 µA through r1 splits into the 73 µA the drain takes and the 175 µA that leaves through port o into the load.
Writing a probe
.probe I(instance,terminal) * or I(instance:terminal)
.probe I(instance) * every terminal of it
- The instance is its hierarchical path,
x1.x2.m3, or its name in the flattened circuit, m.x1.x2.m3.
- The terminal is a position counted from 1, a device letter (MOS
d g s b, BJT c b e s, JFET d g s, diode a and c or k), the name of a subcircuit port, or the terminal name a Verilog-A model gives it.
- Two spellings of one terminal are one probe and one vector:
I(x1.x2.m3,1) and I(x1.x2.m3:d) are the same thing.
The vector is <path>:<terminal>#branch, which you read as i(x1.x2.m3:d). The terminal part is the name .probe has always used at the top level, so moving a block into a subcircuit only puts the path in front. Nothing in your scripts gets renamed.
| Device | Terminal names, in order |
| MOSFET (M) | d g s b_tj (and tc n6 n7 on models with more nodes) |
| BJT (Q) | c b e s t |
| JFET, MESFET (J, Z) | d g s |
| Diode (D) | a c |
| Subcircuit instance (X) | the port names of the subcircuit |
| Verilog-A / OSDI (N) | the terminal names of the model |
| everything else | n1 n2 … |
b_tj looks odd, but it is the name ngspice's .probe has always given the fourth node of an M line, and keeping it means old decks and new ones agree. You can still type b or 4; only the saved name follows the table.
Which way is positive
A probe gives the current into the terminal, flowing from the net into the instance. The four terminals of a MOSFET sum to zero, and a negative port current means current is leaving the instance there.
The one exception is an instance with two terminals and no terminal named, such as I(x1.r1). That gives one vector named after the instance, the current through it from its first terminal to its second, the same convention .probe has always used at the top level. For a subcircuit with two ports and a path to ground inside, the two ports carry different currents. In that case, name the port you mean.
AC and transient too
The probe is an ideal 0 V source in the circuit, so its current is the solution itself, in op, dc, ac and tran. In AC it is the complex small-signal current. Here is a capacitor inside an RC subcircuit, compared with ωC·|v|:
vin in 0 dc 0 ac 1
x1 in out filt
.subckt filt a b
r1 a b 1k
c1 b 0 1u
.ends
.probe I(x1.c1)
.control
ac dec 1 10 100k
print mag(i(x1.c1)) mag(2*pi*frequency*1u*v(out)) vp(i(x1.c1))
.endc
frequency mag(i(x1.c1)) w*C*|v(out)| phase (rad)
1.000000e+01 6.270819e-05 6.270819e-05 1.508047e+00
1.000000e+02 5.320180e-04 5.320180e-04 1.009814e+00
1.000000e+03 9.875705e-04 9.875705e-04 1.578312e-01
1.000000e+04 9.998734e-04 9.998734e-04 1.591415e-02
1.000000e+05 9.999987e-04 9.999987e-04 1.591548e-03
Verilog-A devices loaded through OSDI work the same way, and they use their model's own terminal names. With PSP 103 compiled by OpenVAF, .probe I(n1) gives n1:d, n1:g, n1:s and n1:b, and I(xa.n2,d) reaches one inside a subcircuit.
How it works
The probe as the simulator sees it. The source changes nothing in the circuit; its branch current is the terminal current.
When the reader expands the subcircuits, it puts the probed terminal on a new node and inserts a 0 V source between that node and the net, with the net as the positive side. Ports of subcircuit instances are handled during the expansion itself, and device terminals afterwards, in the flattened netlist. After the circuit is built, the source is renamed after the terminal and its branch current is added to the save list. The new node is kept out of .save all, so your vector list shows the currents and not the nodes they needed.
Because no device code is involved, every built-in or Verilog-A device gets this for free. The probes are written into the stored copy of the deck, so they come back after reset. The machinery is shared with the stb loop-gain analysis, which uses the same kind of terminal probe. When both are on one terminal, they get two sources in series, and the loop gain is unchanged.
How exact
- The current is the solution, so KCL holds to rounding: a resistor's probe agrees with (v(a)−v(b))/R to 1e-15 A, and a MOSFET's terminals sum to about 1e-19 A.
- Device parameters such as
@m.x1.x2.m3[id] are read from the iterate before the last, so they match the probe only within the Newton tolerance. In our tests a BJT's @ic was 0.055 % off the probe current.
- A terminal current includes everything the model stamps there. The MOSFET bulk above shows −1.9 pA: that is the gmin of its junctions, not a bug.
Where to get it. This is on
our ngspice branch, not in upstream ngspice. The behaviour at the top level is unchanged: existing
.probe decks give the same vectors under the same names. Scripts and schematic tools can check for the feature with
$?probe_version, which is 1 in a build that has it.
Limits
vd() and p() in .probe (voltage differences and power) still work at the top level only.
.probe alli still covers the top level only; a terminal inside a subcircuit needs its own I(…).
- Probes are made when the deck is read. You cannot add one from the command prompt afterwards.
- XSPICE code-model instances, couplings (
K), and E/G sources with an expression are refused with a one-line reason. For those, place a 0 V source yourself.
- Tested in the ngspice executable. The shared-library build (
libngspice) has not been exercised yet.
How it was tested
Every behaviour started as a failing test. A new regression suite of 14 decks checks currents against hand-solved circuits: the hierarchy, every spelling of a terminal, the same block at two depths, two-port subcircuits, refusals, reset, what gets saved, sharing a terminal with stb, alli, and the unchanged top level, in op, AC and transient, plus the version variable. Two more decks check the case-sensitive netlist modes. The full ngspice regression and model-QA suite still passes, with one known failure in an unrelated rusage test that predates this work.