Вы находитесь на странице: 1из 8

Inside SPICE

Hemanshu R. Pota
September 10, 2010

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 250t V, and (b) vs is a 50 V peak 50 Hz square waveform. For the diode use
Is = 1014 A and VT = 26 mV.
D1
iD
iL
R
+

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
for the circuit (using the diode equation iD = Is evD /VT 1 ):


iL
diL
+ 1 RiL Ls
=0
(1)
vs VT ln
Is
dt
Starting from the given initial value, we can obtain new values of iL using the following equation
inew
= iold
+ (rate of change of iL ) t
L
L
!
!
old
t
i
inew
= iold
+
vs VT ln L + 1 Riold
L
L
L
Ls
Is

(2)

The above innocuous


equation (2) givesgood answers
till iL gets very small or negative.
 looking


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;
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);

Simple Integration

Simple Integration

50

2.5

vs
vd
40
2

30

20

vD (V) and vs (V)

iL (A)

1.5

10

10

0.5

20

30
0

40

0.5

0.005

0.01

0.015

0.02
Time (s)

0.025

0.03

Figure 2: Inductor Current

0.035

0.04

50

0.005

0.01

0.015

0.02
Time (s)

0.025

0.03

0.035

0.04

Figure 3: Diode and Supply Voltage

The above mentioned fix for the stiff diode nonlinearity needs a good knowledge of the circuit
2 of 8

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 distretises 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


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

vD =vD (k)

IQ (k) = iD GD (k)vD

The above representation (5) converts a diode to a resistor


(IQ (k)).

(5)

1
GD (k)

in parallel with a current source

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
dt
Z t+h
Z t+h
vL dt =
LdiL
t

(6)
(7)

h
(vL (k + 1) + vL (k)) = L (iL (k + 1) iL (k))
2
h
h
iL (k + 1) =
vL (k + 1) +
vL (k) + iL (k)
2L
2L
h
h
iL (k + 1) =
vL (k + 1) + ILQ (k) (where ILQ (k) =
vL (k) + iL (k))
2L
2L

(8)
(9)
(10)
3 of 8

How SPICE Works


A Simple Example
) in parallel with a current source
The above representation (10) converts an inductor to a resistor ( 2L
h
(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
dt
Z t+h
Z t+h
iC dt =
CdvC
t

(11)
(12)

h
(iC (k + 1) + iC (k)) = C (vC (k + 1) vC (k))
2
2C
2C
iC (k + 1) =
vC (k + 1)
vC (k) iC (k)
h
h
2C
2C
iC (k + 1) =
vC (k + 1) + ICQ (k) (where ICQ (k) = vC (k) iC (k))
h
h

(13)
(14)
(15)

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 elements 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.

+ iD

i +

vD

vD
j

+ iL
1
GD

i +

vL L

IQ

vL
j

i +
2L
h

ILQ vC

+ iC

vC

C
j

h
2C

ICQ

Figure 4: SPICE Equivalent Circuits


The equation for the three equivalent circuits in Figure 4 are

  
  h
  
  2C
  

h
2C
GD GD Vi
IQ

V
I

V
I
i
LQ
i
CQ
2L
=
; 2Lh
=
; h2C 2Ch
=
h
GD GD
Vj
IQ
Vj
ILQ
Vj
ICQ
2L 2L
h
h
(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 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
+

1
GM +GD (k)

iL (k + 1)
2Ls
h

vs (k + 1)

h
v (k)
2Ls L

+ iL (k)

0
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)
= IQ (k)
R
v2 (k + 1) v1 (k + 1)
h
h
At node 2:
+
v2 (k + 1) +
v2 (k) + iL (k) = 0
R
2Ls
2Ls

At node 1: (v1 (k + 1) vs (k + 1)) (GD (k) + GM ) +

In compact matrix notation the above two equations (17) and (18) can be written as


 

GD (k) + GM + R1
R1
IQ (k) + vs (k + 1) (GD (k) + GM )
v1 (k + 1)
=
h
R1
+ R1 v2 (k + 1)
2Lh s v2 (k) iL (k)
2Ls

(17)
(18)

(19)

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
 v1 (k + 1) and v2 (k + 1) must also satisfy diode equation, i.e., iD (k + 1) =
vD (k+1)/VT
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;
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 [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 digital 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

How SPICE Does it

How SPICE Does it

50

2.5

vD
v

40

30

20

vD (V) and vs (V)

iL (A)

1.5

10

10

0.5

20

30
0

40

0.5

0.005

0.01

0.015

0.02
Time (s)

0.025

0.03

Figure 6: Inductor Current

0.035

0.04

50

0.005

0.01

0.015

0.02
Time (s)

0.025

0.03

0.035

0.04

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 description 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
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 rectifier shown in Figure 8, plot vo , vD , iL , and is for R = 1 k, C = 10 F
when (a) vs = 50 sin 250t V, and (b) vs is a 50 V peak 50 Hz square waveform. For the diode use
Is = 1014 A and VT = 26 mV.
D1
+
vs +

is

vD

iL
vC

+
C

vo

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. McGrawHill, 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):103111, January 1984.
[4] William J. McCalla and Donald O. Pederson. Elements of computer-aided circuit analysis. IEEE
Trans on Circuits Theory, CT-18(1):1426, January 1971.
[5] Arthur Richard Newton and Alberto L. Sangiovanni-Vincentelli. Relaxation-based electrical simulation. IEEE Trans on Computer-Aided Design, CAD-3(4):308331, October 1984.

8 of 8

Вам также может понравиться