You are on page 1of 8

Inside SPICE

Hemanshu R. Pota
August 23, 2019

1 Introduction
For the half-wave rectifier shown in Figure 1, plot vL , vD , and iL for R = 10 Ω, Ls = 100 mH
when (a) vs = 50 sin 2π50t V, and (b) vs is a 50 V peak 50 Hz square waveform. For the diode use
Is = 10−14 A and VT = 26 mV.
iD D1 R iL
+ −
vD +
vs +
− Ls vL

Figure 1: Half-wave Rectifier

To solve for current and voltages in the half-wave rectifier


 in Figure 1, we write KVL equation
vD /VT
for the circuit (using the diode equation iD = Is e − 1 ):
 
iL diL
vs − VT ln + 1 − RiL − Ls =0 (1)
Is dt

Starting from the given initial value, we can obtain new values of iL using the following equation

inew
L = iold
L + (rate of change of iL ) × ∆t
! !
∆t iold
inew
L = iold
L + vs − VT ln L + 1 − RioldL (2)
Ls Is

The above innocuous


 looking
 equation (2) givesgood answers
 till iL gets very small or negative.
iL iL
More specifically as Is + 1 becomes negative ln Is + 1 becomes complex and not of any use to
us. In the above integration step (2) we have to add a check if iL is predicted (of course incorrectly)
to be smaller than Is and then fix vd = −vs . Using this fix the current iL obtained from equation (2)
is shown in Figure 2 and the voltages vD and vs 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;
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’);

Simple Integration Simple Integration


2.5 50
vs
vd
40

2
30

20
1.5

10
vD (V) and vs (V)
i (A)

1 0
L

−10

0.5
−20

−30
0

−40

−0.5 −50
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04
Time (s) Time (s)

Figure 2: Inductor Current Figure 3: Diode and Supply Voltage

The above mentioned ‘fix’ for the stiff diode nonlinearity needs a good knowledge of the circuit
How SPICE Works
A Simple Example

configuration, 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 discre-
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 rectifier 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
iD = GD (k)vD + IQ (k) (3)
The above equation (3) is a linear version of the nonlinear diode equation iD = Is evD /VT − 1 , and


for the (k + 1)st step, with known iD (k) and vD (k), the values of GD and IQ are obtained as

diD Is vD /VT
GD (k) = = e (4)
dvD iD =iD (k)
vD =vD (k) VT iD =iD (k)
vD =vD (k)

IQ (k) = iD − GD (k)vD (5)


 
1
The above representation (5) converts a diode to a resistor GD (k)
in parallel with a current source
(IQ (k)).

2.2 Inductor Representation


The voltage-current relationship for an inductor vL = L didtL is discretised, for the (k + 1)st step, as
follows (h is the step-size)
diL
vL = L (6)
dt
Z t+h Z t+h
vL dt = LdiL (7)
t t
h
(vL (k + 1) + vL (k)) = L (iL (k + 1) − iL (k)) (8)
2
h h
iL (k + 1) = vL (k + 1) + vL (k) + iL (k) (9)
2L 2L
h h
iL (k + 1) = vL (k + 1) + ILQ (k) (where ILQ (k) = vL (k) + iL (k)) (10)
2L 2L
How SPICE Works
A Simple Example

The above representation (10) converts an inductor to a resistor ( 2L


h
) in parallel with a current source
(ILQ (k)).

2.3 Capacitor Representation


The voltage-current relationship for a capacitor iC = C dvdtC is discretised, for the (k + 1)st step, as
follows (h is the step-size)
dvC
iC = C (11)
dt
Z t+h Z t+h
iC dt = CdvC (12)
t t
h
(iC (k + 1) + iC (k)) = C (vC (k + 1) − vC (k)) (13)
2
2C 2C
iC (k + 1) = vC (k + 1) − vC (k) − iC (k) (14)
h h
2C 2C
iC (k + 1) = vC (k + 1) + ICQ (k) (where ICQ (k) = − vC (k) − iC (k)) (15)
h h
h
The above representation (15) converts a capacitor to a resistor ( 2C ) in parallel with a current source
(ICQ (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.

i + i + i +
+ iL
+ iD 1 +
iC
vD vD vL L vL 2L h
GD IQ h
ILQ vC C vC 2C
ICQ
− −
− − − −
j j j

Figure 4: SPICE Equivalent Circuits

The equation for the three equivalent circuits in Figure 4 are


      h h
     2C 2C
   
GD −GD Vi −IQ − V i −I LQ − V i −I CQ
= ; 2Lh 2L
h = ; h2C 2Ch =
−GD GD Vj IQ − 2L 2L Vj ILQ −h h
Vj ICQ
(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 the value of any integration software is in the availability 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.
How SPICE Works
A Simple Example

3 A Simple Example
The transient analysis for the half-wave rectifier shown in Figure 1 is performed here as SPICE would
do it. The first 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.
IQ (k)

R 2
1 1
+ GM +GD (k) iL (k + 1)
2Ls
vs (k + 1) h
h
v (k) + iL (k)
2Ls L

Figure 5: SPICE Equivalent Half-wave Rectifier

The nodal equations for the circuuit in Figure 5 can be written as:

v1 (k + 1) − v2 (k + 1)
At node 1: (v1 (k + 1) − vs (k + 1)) (GD (k) + GM ) + = IQ (k) (17)
R
v2 (k + 1) − v1 (k + 1) h h
At node 2: + v2 (k + 1) + v2 (k) + iL (k) = 0 (18)
R 2Ls 2Ls
In compact matrix notation the above two equations (17) and (18) can be written as

GD (k) + GM + R1 − R1
    
v1 (k + 1) IQ (k) + vs (k + 1) (GD (k) + GM )
= (19)
− R1 h
2Ls
+ R1 v2 (k + 1) − 2Lh s v2 (k) − iL (k)
Y (k)V (k + 1) = I(k)

The above matrix equation (19) is solved for v1 (k + 1) and v2 (k + 1). The obtained values
of the node voltages
vD (k+1)/VT
 v1 (k + 1) and v2 (k + 1) must also satisfy diode equation, i.e., iD (k + 1) =
Is e − 1 . The values of iD (k + 1) and vD (k + 1) can be obtained from the node voltages
v1 (k + 1) and v2 (k + 1). If iD (k + 1) and vD (k + 1) do not satisfy the diode equation then these
new values of iD (k + 1) and vD (k + 1) are used to obtain an updated estimate of GD (k) and IQ (k)
using equations (4) and (5). With these updated values of GD (k) and IQ (k) the matrix equation (19)
is solved again and the process is continued till there is a convergence. Note that GM 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 iL is shown in Figure 6 and the voltages
vD and vs 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;
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 [3], Professor Pederson writes (the citations in the following paragraph are from the 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
How SPICE Works
A Simple Example

How SPICE Does it How SPICE Does it


2.5 50
v
D
vs
40

2
30

20
1.5

10

vD (V) and vs (V)


i (A)

1 0
L

−10

0.5
−20

−30
0

−40

−0.5 −50
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04
Time (s) Time (s)

Figure 6: Inductor Current Figure 7: Diode and Supply Voltage

forms of a complete description of an electrical network and devised the A matrix [2].
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 [38]. With subsequent developments, however,
it became clear that the state-variable approach, although very significant for theoretical
studies, is not the best basis for computer programs for circuit analysis. As brought out
below, the modified-nodal and sparse-tableau formulations are better. For one who has
observed the developments in this field throughout the 30 years of its existence, the fierce
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:
eq
1. Y11 term is added to Yii
eq
2. Y12 term is added to Yij
eq
3. Y21 term is added to Yji
eq
4. Y22 term is added to Yjj

This means that as each SPICE line is read, the Y matrix is built by modification of the existing
terms or adding new terms. In addition to the Y matrix, a list of all nonlinear element must also be
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 checking for the 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 rectifier shown in Figure 8, plot vo , vD , iL , and is for R = 1 kΩ, C = 10 µF
when (a) vs = 50 sin 2π50t V, and (b) vs is a 50 V peak 50 Hz square waveform. For the diode use
Is = 10−14 A and VT = 26 mV.
D1
+ −
vD iL
+
+
vs + is vC C vo
− − R

Figure 8: Half-wave Rectifier - RC Load

Use the integration method and also the SPICE method and compare your answers.

References
[1] Andrei Vladimirescu. The SPICE Book. John Wiley & Sons, Inc., Brisbane, 1994.

[2] Ron M. Kielkowski. Inside SPICE: Overcoming the obstacles of circuit simulation. McGraw-
Hill, Inc., Sydney, 1994. ISBN 0-07-911525-X.

[3] Donald O. Pederson. A historical review of circuit simulation. IEEE Trans on Circuits and
Systems, CAS-31(1):103–111, January 1984.

[4] William J. McCalla and Donald O. Pederson. Elements of computer-aided circuit analysis. IEEE
Trans on Circuits Theory, CT-18(1):14–26, January 1971.

[5] Arthur Richard Newton and Alberto L. Sangiovanni-Vincentelli. Relaxation-based electrical sim-
ulation. IEEE Trans on Computer-Aided Design, CAD-3(4):308–331, October 1984.

You might also like