11
\$\begingroup\$

I wish to plot out the current and power in ngspice. In the case of voltage, for example, if I wanted to plot the voltage at node 1 I would use:

plot v(1)

Problem is, when I try i(1), the vector isn't recognized. Could someone provide a few examples on how to do this?

\$\endgroup\$
8
\$\begingroup\$

Oli gave a correct answer but the I(element_name) is an extension added only to the commercial SPICE versions.

In ngspice (which is based on Berkeley Spice 3) you can only plot currents through (independent) voltage sources. These are the only currents that appear in the circuit equations SPICE works from.

In an interactive Spice session or from a special block in the script (see also this question) you can use expressions like (v(1)-v(2))/1k when the current is through a 1kΩ resistor between nodes 1 and 2. For reactive elements (like a 1μF capacitor) something like (v(1) - v(2))/(2*pi*frequency*1u) should also work.

\$\endgroup\$
  • \$\begingroup\$ My second solution (with expressions) should not be very difficult to apply. You may see in the manual if ngspice has some way to get the value and maybe even node names of a component. I know for sure that there dummy vectors with model parameters. \$\endgroup\$ – jpc Aug 28 '11 at 23:00
6
\$\begingroup\$

I have not used ngspice (I use LTSpice, but from what I understand pretty much all SPICEs are based on the original Berkeley syntax, and work similarly), but usually you plot the current through a component or into e.g. base of a transistor, rather than at a node, according to Kirchoff's first law (the sum of currents meeting at a point is zero)

EDIT - as jpc pointed out, for ngspice (and probably most other non-commercial/early variants) things are slightly different, as you can only plot currents through a voltage source. So one would have to add a 0V source in the leg of the circuit of interest, and plot the current through this. I have added an example below.

So if you have a simple circuit consisting of a resistor (R1) with a voltage source (V1) across it (I am not an expert on the netlist so take as rough example):

V1 1 0 5

R1 1 0 1000

(1, 0 are the nodes, 5 (V) and 1000 (Ohms) are the respective values)

You could either plot the current with I(R1), or I(V1), not I(1). You could however plot V(1) for the voltage.

NGSPICE version (tested and confirmed)

V1 1 0 5
R1 1 2 1000 Vdummy 2 0 0

Note that Vdummy is in series with the resistor, so the same current must flow through it. So to plot current for R1 we write I(Vdummy). For a more complex circuit we just make sure that the same conditions apply.

Here is a "real" example from LTspice:

NETLIST

V1 V+ 0 24 Rser=0
V2 SIG 0 SINE(-1.4563 1m 1000 0 0 0 0) AC 2 Rser=0
V3 V- 0 -24
Q1 N001 N002 N003 0 2N2222
R1 V+ N001 1f
R2 N003 V- 2K7
C2 N004 N003 100µF
R3 N004 0 3K9
R4 N002 0 22K
C1 N002 SIG 100µF

PICTURE OF CIRCUIT

Circuit Ex

PLOT OPTIONS

(Note that there is no I(n001), I(n002), etc)

Plot Options

\$\endgroup\$
  • 1
    \$\begingroup\$ As jpc just commented, it appears the with ngspice you can only plot currents through voltage sources. I just downloaded ngspice and confirmed this, it is possible to put i(Vx), but not i(Rx), etc. So I guess you need to add a 0V source in whatever leg of your circuit you want to plot the current through (say it's called Vx) and put plot i(Vx). \$\endgroup\$ – Oli Glaser Aug 28 '11 at 20:12
  • \$\begingroup\$ @OliGlaser: Actually most commercial SPICE forks are based on the SPICE2 codebase (in Fortran) while ngspice (and other open source/freeware versions) is based on the newer(!) C language rewrite which was called SPICE3. I believe the jury is still out on which one is better. :) \$\endgroup\$ – jpc Jun 26 '12 at 0:49
4
\$\begingroup\$

You can plot currents, but you need to know what currents you want to know about before you run your simulation.

e.g. If you had a diode D1 and you wanted to plot the current through it, you could:

.save @d1[id]
.tran <slice> <end>
.plot tran @d1[id]

More information is on page 519 (Chapter 31) of the ngspice manual at http://ngspice.sourceforge.net/docs/ngspice-manual.pdf

\$\endgroup\$
3
\$\begingroup\$

In case of sweeping DC voltage across a single component, you can plot the branch current. By loading the following netlist

.MODEL DI1N4004 D (IS=76.9n RS=42.0m  BV=4 IBV=5.00u CJO=39.8p
+M=0.333 N=1.45 TT=4.32u)

D1 1 0 DI1N4004
Vin 1 0 dc 12 ac 0

to ngspice and commanding

dc vin -10 5 0.001
plot -vin#branch

you should be able to obtain a plot diagram of current across the diode d1.

\$\endgroup\$
2
\$\begingroup\$

for current plotting add zero voltage source in that branch and type vlabel#branch in the terminal.

\$\endgroup\$
  • 3
    \$\begingroup\$ This adds nothing that the other answers didn't already say, and is too terse to be useful on its own. \$\endgroup\$ – Dave Tweed Aug 15 '13 at 11:21
2
\$\begingroup\$

Since NGSpice 27 R2017, placing this line of code:

.options savecurrents

Saves the currents, which can be called through for @R1[i], @D1[id], etc.

plot @R1[i] vs v(1)

Seek for this option in the NGSpice Manual.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.