You are on page 1of 8

# Inside SPICE

Hemanshu R. Pota
September 10, 2010
1 Introduction
For the half-wave rectier shown in Figure 1, plot v
L
, v
D
, and i
L
for R = 10 , L
s
= 100 mH
when (a) v
s
= 50 sin 250t V, and (b) v
s
is a 50 V peak 50 Hz square waveform. For the diode use
I
s
= 10
14
A and V
T
= 26 mV.

+
v
s
i
D
D
1
+
v
D

R
L
s
+
v
L

i
L
Figure 1: Half-wave Rectier
To solve for current and voltages in the half-wave rectier in Figure 1, we write KVL equation
for the circuit (using the diode equation i
D
= I
s
_
e
v
D
/V
T
1
_
):
v
s
V
T
ln
_
i
L
I
s
+ 1
_
Ri
L
L
s
di
L
dt
= 0 (1)
Starting from the given initial value, we can obtain new values of i
L
using the following equation
i
new
L
= i
old
L
+ (rate of change of i
L
) t
i
new
L
= i
old
L
+
t
L
s
_
v
s
V
T
ln
_
i
old
L
I
s
+ 1
_
Ri
old
L
_
(2)
The above innocuous looking equation (2) gives good answers till i
L
gets very small or negative.
More specically as
_
i
L
Is
+ 1
_
becomes negative ln
_
i
L
Is
+ 1
_
becomes complex and not of any use to
us. In the above integration step (2) we have to add a check if i
L
is predicted (of course incorrectly)
to be smaller than I
s
and then x v
d
= v
s
. Using this x the current i
L
obtained from equation (2)
is shown in Figure 2 and the voltages v
D
and v
s
are shown in Figure 3. The matlab script used to
obtain these plots is given below.
%Prob 7 - Power Electronics problem sheet
clear all
dt = 0.0001; tf = 0.04;
1
How SPICE Works
A Simple Example
t = 0:dt:tf;
Is = 10(-14); % reverse saturation current;
VT = 0.026;
gmin = 0.1;
R = 10;
Ls = 0.1;
il = zeros(1,length(t));
vs = zeros(1,length(t));
vd = zeros(1,length(t));
for k = 1:length(t)-1
vs(k) = 50
*
sin(2
*
pi
*
50
*
t(k));
if il(k) == -Is
il(k) = il(k) + 10(-10); %to make sure il(k)/Is > -1.
end;
vd(k) = VT
*
log(il(k)/Is + 1);
il(k+1) = il(k) + ((vs(k) - vd(k) - R
*
il(k))/Ls)
*
dt;
if il(k+1) <= -Is
vd(k) = vs(k);
il(k+1) = Is
*
(exp(vd(k)/VT) - 1);
end;
end;
fignum=0;
fignum = fignum + 1; figure(fignum);
plot(t,il); grid on;
xlabel(Time (s)); ylabel(i_L (A)); title(Simple Integration);
fignum = fignum + 1; figure(fignum);
plot(t,vs,g,t,vd,r); grid on;
legend(vs,vd);
xlabel(Time (s)); ylabel(v_D (V) and v_s (V));
title(Simple Integration);
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04
0.5
0
0.5
1
1.5
2
2.5
Time (s)
i L

(
A
)
Simple Integration
Figure 2: Inductor Current
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04
50
40
30
20
10
0
10
20
30
40
50
Time (s)
v
D

(
V
)

a
n
d

v
s

(
V
)
Simple Integration

vs
vd
Figure 3: Diode and Supply Voltage
The above mentioned x for the stiff diode nonlinearity needs a good knowledge of the circuit
2 of 8
How SPICE Works
A Simple Example
conguration, etc. SPICE routinely solves circuits with thousands of nonlinear elements like the
diode in Figure 1. It will be very instructive to see how SPICE handles the nonlinear elements during
its nonlinear transient analysis. Based on the material in books [1, 2] and journal papers [3, 4, 5],
written by the original researchers who helped develop SPICE, I have put together this note to explain
exactly how SPICE solves a circuit such as shown in Figure 1. I hope in this short note the genius of
the developers of the original SPICE is made clear.
2 How SPICE Does it
For transient analysis, at every time-step, SPICE linearises all the nonlinear elements and distre-
tises all the derivative and integral relationships. This results in a circuit only with resistors, current
sources, and voltage sources. In this section, SPICE equivalent models for a diode, inductor, and
capacitors are derived. A similar process is used by SPICE to have equivalent representations of all
the devices. The SPICE equivalents are used in the next section to perform transient analysis for the
half-wave rectier circuit in Figure 1.
In the following (k + 1) really stands for t = (k + 1)h, where h is the step-size and k stands for
the k
th
time-step. Next we discuss how to obtain values of currents and voltages at (k + 1)
st
step,
given their values at the k
th
step.
2.1 Diode Representation
The linearised SPICE equivalent for the diode in Figure 1, for the (k + 1)
st
step, is represented as
i
D
= G
D
(k)v
D
+ I
Q
(k) (3)
The above equation (3) is a linear version of the nonlinear diode equation i
D
= I
s
_
e
v
D
/V
T
1
_
, and
for the (k + 1)
st
step, with known i
D
(k) and v
D
(k), the values of G
D
and I
Q
are obtained as
G
D
(k) =
di
D
dv
D

i
D
=i
D
(k)
v
D
=v
D
(k)
=
I
s
V
T
e
v
D
/V
T

i
D
=i
D
(k)
v
D
=v
D
(k)
(4)
I
Q
(k) = i
D
G
D
(k)v
D
(5)
The above representation (5) converts a diode to a resistor
_
1
G
D
(k)
_
in parallel with a current source
(I
Q
(k)).
2.2 Inductor Representation
The voltage-current relationship for an inductor v
L
= L
di
L
dt
is discretised, for the (k + 1)
st
step, as
follows (h is the step-size)
v
L
= L
di
L
dt
(6)
_
t+h
t
v
L
dt =
_
t+h
t
Ldi
L
(7)
h
2
(v
L
(k + 1) + v
L
(k)) = L(i
L
(k + 1) i
L
(k)) (8)
i
L
(k + 1) =
h
2L
v
L
(k + 1) +
h
2L
v
L
(k) + i
L
(k) (9)
i
L
(k + 1) =
h
2L
v
L
(k + 1) + I
LQ
(k) (where I
LQ
(k) =
h
2L
v
L
(k) + i
L
(k)) (10)
3 of 8
How SPICE Works
A Simple Example
The above representation (10) converts an inductor to a resistor (
2L
h
) in parallel with a current source
(I
LQ
(k)).
2.3 Capacitor Representation
The voltage-current relationship for a capacitor i
C
= C
dv
C
dt
is discretised, for the (k + 1)
st
step, as
follows (h is the step-size)
i
C
= C
dv
C
dt
(11)
_
t+h
t
i
C
dt =
_
t+h
t
Cdv
C
(12)
h
2
(i
C
(k + 1) + i
C
(k)) = C (v
C
(k + 1) v
C
(k)) (13)
i
C
(k + 1) =
2C
h
v
C
(k + 1)
2C
h
v
C
(k) i
C
(k) (14)
i
C
(k + 1) =
2C
h
v
C
(k + 1) + I
CQ
(k) (where I
CQ
(k) =
2C
h
v
C
(k) i
C
(k)) (15)
The above representation (15) converts a capacitor to a resistor (
h
2C
) in parallel with a current source
(I
CQ
(k)).
2.4 Equivalent Representations
The three equivalent circuits described above are shown in Figure 4. In circuit analysis, all the ele-
ments in the circuit are replaced by their SPICE equivalents and the voltages and currents are obtained
for that equivalent circuit which is made up of only resistors, current sources, and voltage sources.
This is demonstrated in the example in the next section.
+
v
D

i
D
I
Q
1
G
D
+

v
D
i
j
L
+
v
L

i
L
I
LQ
2L
h
+

v
L
i
j
C
+
v
C

i
C
I
CQ
h
2C
+

v
C
i
j
Figure 4: SPICE Equivalent Circuits
The equation for the three equivalent circuits in Figure 4 are
_
G
D
G
D
G
D
G
D
_ _
V
i
V
j
_
=
_
I
Q
I
Q
_
;
_
h
2L

h
2L

h
2L
h
2L
_ _
V
i
V
j
_
=
_
I
LQ
I
LQ
_
;
_
2C
h

2C
h

2C
h
2C
h
_ _
V
i
V
j
_
=
_
I
CQ
I
CQ
_
(16)
2.5 Integration Methods
The above three equivalent circuits have been obtained using the simplest integration method. Much
of the SPICE or for that matter any integration software is in the choice of different integration
methods. Please refer to the following easy-to-read papers to get an understanding of the link between
integration methods and solutions [3, 4, 5]. These papers are based on a wealth of experience.
4 of 8
How SPICE Works
A Simple Example
3 A Simple Example
The transient analysis for the half-wave rectier shown in Figure 1 is performed here as SPICE would
do it. The rst step is to replace the diode and the inductor by their equivalents given in Figure 4.
We obtain Figure 5 after replacing the original elements in Figure 1 with SPICE equivalents from
Figure 4.

v
s
(k + 1)
+
1
G
M
+G
D
(k)
I
Q
(k)
R
i
L
(k + 1)
h
2Ls
v
L
(k) + i
L
(k)
2Ls
h
2
1
0
Figure 5: SPICE Equivalent Half-wave Rectier
The nodal equations for the circuuit in Figure 5 can be written as:
At node 1: (v
1
(k + 1) v
s
(k + 1)) (G
D
(k) + G
M
) +
v
1
(k + 1) v
2
(k + 1)
R
= I
Q
(k) (17)
At node 2:
v
2
(k + 1) v
1
(k + 1)
R
+
h
2L
s
v
2
(k + 1) +
h
2L
s
v
2
(k) + i
L
(k) = 0 (18)
In compact matrix notation the above two equations (17) and (18) can be written as
_
G
D
(k) + G
M
+
1
R

1
R

1
R
h
2Ls
+
1
R
_ _
v
1
(k + 1)
v
2
(k + 1)
_
=
_
I
Q
(k) + v
s
(k + 1) (G
D
(k) + G
M
)

h
2Ls
v
2
(k) i
L
(k)
_
(19)
Y (k)V (k + 1) = I(k)
The above matrix equation (19) is solved for v
1
(k + 1) and v
2
(k + 1). The obtained values
of the node voltages v
1
(k + 1) and v
2
(k + 1) must also satisfy diode equation, i.e., i
D
(k + 1) =
I
s
_
e
v
D
(k+1)/V
T
1
_
. The values of i
D
(k + 1) and v
D
(k + 1) can be obtained from the node voltages
v
1
(k + 1) and v
2
(k + 1). If i
D
(k + 1) and v
D
(k + 1) do not satisfy the diode equation then these
new values of i
D
(k + 1) and v
D
(k + 1) are used to obtain an updated estimate of G
D
(k) and I
Q
(k)
using equations (4) and (5). With these updated values of G
D
(k) and I
Q
(k) the matrix equation (19)
is solved again and the process is continued till there is a convergence. Note that G
M
is not a standard
part of the diode equivalent circuit but it a term added by SPICE to improve the convergence.
Using the above described algorithm, the obtained current i
L
is shown in Figure 6 and the voltages
v
D
and v
s
are shown in Figure 7. The matlab script used to obtain these plots is included below.
%Prob 7 - Power Electronics problem sheet
%This is the way spice would solve it
clear all
dt = 0.00001; tf = 0.04;
t = 0:dt:tf;
5 of 8
How SPICE Works
A Simple Example
Is = 10(-12); % reverse saturation current;
VT = 0.026;
R = 10;
Ls = 0.1;
Gm = 1/10000;
io = zeros(1,length(t));
vdiode = zeros(1,length(t));
vd = 0.6; Vnode = [0;0];
kiter = zeros(1,length(t));
for j = 1:length(t)
vs=50
*
sin(2
*
pi
*
50
*
t(j)); materr = 2; k = 0;
ILc = -Vnode(2)
*
dt/(2
*
Ls)-(Vnode(1) - Vnode(2))/R;
while materr > 0.01;
k = k + 1;
id = Is
*
(exp(vd/VT)-1);
GD = (Is/VT)
*
exp(vd/VT);
IQ = id - GD
*
vd;
Ynode = [GD+Gm+1/R -1/R; -1/R dt/(2
*
Ls)+1/R];
Inode = [IQ + vs
*
(GD+Gm); ILc];
Vnodenew = Ynode\Inode;
vd = vs - Vnodenew(1);
if vd > 1.0
vd = 0.7;
end;
materr = norm(Vnodenew - Vnode);
Vnode = Vnodenew;
end;
io(j) = (Vnode(1)-Vnode(2))/R;
vdiode(j) = vd;
kiter(j) = k;
end;
fignum = 0;
fignum = fignum + 1; figure(fignum);
plot(t,io);grid on;
xlabel(Time (s)); ylabel(i_L (A)); title(How SPICE Does it);
fignum = fignum + 1; figure(fignum);
plot(t,vdiode,r-,t,50
*
sin(2
*
pi
*
50
*
t),g); grid on;
xlabel(Time (s)); ylabel(); title(How SPICE Does it);
legend(v_D,v_s);
4 The Genius of SPICE
In , Professor Pederson writes (the citations in the following paragraph are fromthe original paper):
The problem of the best way to formulate circuit equations had to be addressed early.
The common mesh equation formulation was not well suited for evaluation with the digi-
tal computer of those days. The capacitive elements as well as the high-resistance sources
were troublesome with this formulation. In early work, Bashkow looked at alternate
6 of 8
How SPICE Works
A Simple Example
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04
0.5
0
0.5
1
1.5
2
2.5
Time (s)
i L

(
A
)
How SPICE Does it
Figure 6: Inductor Current
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04
50
40
30
20
10
0
10
20
30
40
50
Time (s)
v
D

(
V
)

a
n
d

v
s

(
V
)
How SPICE Does it

v
D
v
s
Figure 7: Diode and Supply Voltage
forms of a complete description of an electrical network and devised the A matrix .
This in part emanated out of his studies of dynamical systems as described in Whittaker
and Watson. The A matrix is widely accepted as the forerunner of the state-variable de-
scription of an electrical network and was used in the early years of CAD as the basis of
equation formulation in CAD programs . With subsequent developments, however,
it became clear that the state-variable approach, although very signicant for theoretical
studies, is not the best basis for computer programs for circuit analysis. As brought out
below, the modied-nodal and sparse-tableau formulations are better. For one who has
observed the developments in this eld throughout the 30 years of its existence, the erce
disagreements of just ten years ago on this subject make for interesting and frustrating
memories.
The above paragraph is of special interest to people with control theory background. The genius
of the nodal matrix approach is that the Y matrix, such as in equation (19) above, can be assembled by
inspection. In a computer program one has to only read the nodes to which the element is connected
and then update the entries corresponding to those nodes from the Y matrix of each element. The
code needed to do this would be perhaps less than a few dozen lines. As compared to this, creating an
A matrix would be quite a challenge. SPICE would not have been able to analyse integrated circuits
with hundreds of thousands of transistors had it not taken the Y matrix approach.
For an N node network referenced to a ground node, normally called the Node 0, the Y matrix is
an N N matrix. To build the Y matrix, individual element Y matrices shown in equation (16) are
used. The update algorithm for the system Y matrix, due to an element connected between nodes i
and j, is as follows:
1. Y
eq
11
term is added to Y
ii
2. Y
eq
12
term is added to Y
ij
3. Y
eq
21
term is added to Y
ji
4. Y
eq
22
term is added to Y
jj
This means that as each SPICE line is read, the Y matrix is built by modication of the existing
terms or adding new terms. In addition to the Y matrix, a list of all nonlinear element must also be
7 of 8
How SPICE Works
A Simple Example
maintained and used at each new transient analysis step. And that is about it. The transient analysis
algorithm is then solving a linear algebraic equation repeatedly and checked for convergence with
nonlinear relationships like the diode equation. Nothing could be simpler. This simplicity of SPICE
is a work of genius.
5 Exercise
For the half-wave rectier shown in Figure 8, plot v
o
, v
D
, i
L
, and i
s
for R = 1 k, C = 10 F
when (a) v
s
= 50 sin 250t V, and (b) v
s
is a 50 V peak 50 Hz square waveform. For the diode use
I
s
= 10
14
A and V
T
= 26 mV.

+
v
s i
s
D
1
+
v
D

C
+
v
C

R
+
v
o

i
L
Figure 8: Half-wave Rectier - RC Load
Use the integration method and also the SPICE method and compare your answers.
References
 Andrei Vladimirescu. The SPICE Book. John Wiley & Sons, Inc., Brisbane, 1994.
 Ron M. Kielkowski. Inside SPICE: Overcoming the obstacles of circuit simulation. McGraw-
Hill, Inc., Sydney, 1994. ISBN 0-07-911525-X.
 Donald O. Pederson. A historical review of circuit simulation. IEEE Trans on Circuits and
Systems, CAS-31(1):103111, January 1984.
 William J. McCalla and Donald O. Pederson. Elements of computer-aided circuit analysis. IEEE
Trans on Circuits Theory, CT-18(1):1426, January 1971.
 Arthur Richard Newton and Alberto L. Sangiovanni-Vincentelli. Relaxation-based electrical sim-
ulation. IEEE Trans on Computer-Aided Design, CAD-3(4):308331, October 1984.
8 of 8