You are on page 1of 11

TECHNICAL NOTE

HOW SPICE WORKS


J uly 2003
Page 1 of 11
SIMetrix Technologies Ltd
OVERVI EW
This technical note describes in hopefully simple terms, the basic principles behind the SIMetrix analog
simulator. SIMetrix is a derivation of SPICE and so it is the SPICE algorithms that are being described but in
fact all direct matrix analog simulators use the same methods.
This note concentrates on the basic core algorithms rather than the sequence of events that takes place within
the simulator. The latter may provide the subject of a future technical note.
The explanation does contain a certain amount of mathematics although we have kept it at a basic level. We
havent managed to find a way of explaining all of the algorithms in non-mathematical terms.
Note that this document does not explain the operation of the SIMPLIS simulator. SIMPLIS works in a quite
different way throughout.
THREE LEVEL S OF COMPL EXI TY
The problem of solving an electrical circuit can be broken down into solving three problems. These are
1. How do you analyse a circuit consisting of fixed current sources and linear resistors only?
2. Having solved 1 above, how do you deal with non-linear components such as diodes and transistors?
3. Having solved 1 and 2 above, how do you deal with reactive components such as capacitors?
The first problem is relatively straightforward to deal with. You can solve a purely linear non-reactive circuit
by applying Kirchoffs and Ohms laws to derive a linear system of simultaneous equations. The equations
can be solved to yield all the node voltages or device currents. There are two variants of this approach
namely nodal analysis and mesh analysis. SPICE and SIMetrix use nodal analysis and this is described in
detail below.
The second problem is harder. Because some components are non-linear it is no longer possible to find what
mathematicians call an analytical solution. Consider the following simple diode circuit:
There is no method of calculating exactly the voltage at the junction of the diode and resistor. It is possible to
derive quite a simple equation whose solution is the voltage we require, but unfortunately there is no method
of solving this equation.
What we can do is test if a particular guess at the voltage is correct. Its a bit like a party game where you can
ask a number of questions about the identity of an object but are only allowed yes and no answers. Here
we can obtain the answers too high or too low but not the exact value. So a procedure might be to start
with a guess then ask the question is this too high or too low? If too high then we reduce the guess and try
again; if too low we increase the guess and try again. Such an approach is rather hit and miss, but fortunately
5
V1
D1
D1N4148
1K
R1
How SPICE Works
J uly 2003
Page 2 of 11
SIMetrix Technologies Ltd
there is a more systematic method called Newton-Raphson iteration also sometimes known as Newtons
Method. This provides us with a simple but very effective method of estimating new guesses after each
iteration. The method is so good that it is usually possible to reach accuracies of around 0.1% in only a few
iterations. Newton-Raphson iteration is described below and is the method exclusively used in all SPICE
and SPICE like programs including SIMetrix.
The third problem to solve is the hardest. Up until now we have only discussed solving for DC values. Now
we are interested in what happens over time. (We arent covering AC analysis here although this analysis
mode is quite straightforward.) The last section of this technical note covers in some depth how capacitors
are simulated. We dont discuss inductors, but as you might expect, these are modelled using the same
principles.
NODAL ANAL YSI S - THE SI MPL E CASE
Consider this simple circuit:
We wish to find the voltages V1 and V2. To do this, we write down equations that sum the currents into each
node. By Kirchoffs current law these must be zero.
At the V1 node:
is the current flowing into the V1 node created by current source I1. is the current flowing into
the V1 node from R1. Note the polarity here.
At the V2 node:
This is the sum of the currents in I2, R1 and R2.
So we have two simultaneous equations with two unknown quantities and . These equations can be
solved to reveal the values of and . From and we can then easily calculate the currents in the
resistors if this is required. In fact and
Although this is a rather simple case, the procedure is general and can be applied to any network of current
V1
100
R2
1K
R1
1m
I1
1m
I2
V2
I
1
V
2
V
1
( )
R
1
----------------------- + 0 =
I
1
V
2
V
1

R
1
------------------
\ .
| |
I
2
V
1
V
2
+ ( )
R1
-----------------------
0 V
2
( )
R
2
-------------------- + + 0 =
V
1
V
2
V
1
V
2
V
1
V
2
V
1
1.2 = V
2
0.2 =
How SPICE Works
J uly 2003
Page 3 of 11
SIMetrix Technologies Ltd
sources and resistors. Voltage sources can also be implemented using an elaboration of the above known as
Modified Nodal Analysis or MNA for short.
The procedure the software uses to build the above equations differs somewhat from the above but the
resulting equations are the same; they are just expressed differently. For a more detailed discussion, please
see the references at the end of this note.
DEAL I NG WI TH NON- L I NEAR DEVI CES
As described above, we can solve circuits containing non-linear devices by using an iterative method called
Newton-Raphson iteration. The technique makes use of the fact that a non-linear device can be treated as
linear over a small range. You are probably already familiar with small signal models for semiconductor
devices. For example, the following small-signal model for a bipolar transistor is found in most text books
on analog circuit theory:
But, as this model only works for changes in signal voltage, it cannot be used for a DC analysis as its bias
conditions are not included. We can accommodate this, however, by adding a DC current source in parallel
with rpi and an additional DC current term to the collector current. These additions will account for the bias
conditions. This is what we have after these changes:
Ib and Ic in the above are worked out so that the bias conditions are maintained.
The model above is linear and therefore any circuit using it can be solved using nodal analysis as described
in the previous section. Such an analysis will not straight away give us the correct answer as this simple
model is only valid for a small range of operating conditions. But the model can be used to provide us with a
better answer than an initial guess. So this is the procedure:
1. Assign an initial guess to the terminal voltages of non-linear devices
C
i =vbe * gm
rpi
E
B
B
E
rpi
i =vbe * gm+Ic
C
Ib
How SPICE Works
J uly 2003
Page 4 of 11
SIMetrix Technologies Ltd
2. From their terminal voltages, work out a modified small signal model for each non-linear device. For
example, for the small signal BJ T above, the values of rpi and gm can be calculated from collector cur-
rent and the gain of the device. Once we have those, the values of Ib and Ic can be calculated.
3. Perform nodal analysis as described in the previous section
4. The nodal analysis will yield new values for all the terminal voltages. Reapply and repeat from step 2.
This procedure is repeated until a result of sufficient accuracy is reached. Accuracy is measured by
comparing the results of successive iterations. The values returned for each node voltage will be become
increasingly close the values returned by the previous iteration as the solution converges. When the values
for consecutive iterations differ by less than a certain tolerance, the sequence is considered to be complete.
AN EXAMPLE
Lets consider our diode circuit once again. You are probably familiar with the concept of dynamic
resistance and probably also know that the dynamic resistance of a diode at 25C is approximately
where is the diode current measured in mA. A single resistor equal to the dynamic resistance is therefore
our small signal model for a diode. As described for the transistor above, we must also account for the DC
conditions and to do this, we place a fixed current source in parallel with the resistor.
This is what results:
The diode has been replaced by Rd and Ieq.
What we now have is a circuit that can be solved using nodal analysis. When this solution is completed we
will have a new value for the diode voltage which, although unlikely to be the right answer, will be closer
than our original guess.
By repeating this procedure we will ultimately converge on the desired solution.
To demonstrate the method, we have prepared an Excel spreadsheet to solve the simple diode circuit. The
results are shown below.
25.8
i
d
----------
i
d
42.50461516
Rd
5
V1
1K
R1
-0.015861807
Ieq
How SPICE Works
J uly 2003
Page 5 of 11
SIMetrix Technologies Ltd
.
In the above example, we started with a guess of 0.7 for the diode voltage. The Id column above is the diode
current calculated from its voltage drop Vd. Ieq is the equivalent current, that is the value of the current
source placed in parallel with Rd to maintain the original voltage drop and diode current. Ieq has a value of
Id-Vd/Rd.
The last column is the new value for the diode voltage and this is used as the starting point for the following
iteration. So from an initial guess of 0.7V we calculated a new estimate of 0.85057. This value is then
entered in the following row and the process is repeated. The final answer of 0.75021 is accurate to at least 5
digits and was achieved after 10 iterations.
HOW WAS THE DIODES CURRENT CALCULATED?
In the above we glossed over how we calculated the diodes current from its voltage. In the spreadsheet
example we used the simple formula:
where =25.8mV @ 25C
IS is a model parameter and is typically 1e-15.
In general, it is a requirement for all SPICE device models to provide a means to calculate all the devices
pin currents given its terminal voltages. It is also a requirement to provide the dynamic resistance term given
its terminal voltages.
REACTI VE EL EMENTS
We will look at how SPICE deals with capacitors but the manner in which SPICE implements other devices
such as inductors is the same.
Note that we are referring to how SPICE deals with capacitors in transient analysis. In AC analysis,
VD ID RD IEQ NEWV
0.7 0.000607 42.50462 -0.01586 0.85057
0.85057 0.207848 0.124129 -6.64444 0.825288
0.825288 0.078014 0.330709 -2.4175 0.800877
0.800877 0.030287 0.851853 -0.90987 0.778673
0.778673 0.012808 2.014318 -0.37376 0.761411
0.761411 0.00656 3.932815 -0.18704 0.752316
0.752316 0.004611 5.594931 -0.12985 0.750293
0.750293 0.004264 6.051335 -0.11972 0.75021
0.75021 0.00425 6.070856 -0.11933 0.75021
0.75021 0.00425 6.070888 -0.11933 0.75021
I
d
I S e
V
d
VT
-------
\ .
| |
1
\ .
|
| |
=
VT
q
KT
------- =
How SPICE Works
J uly 2003
Page 6 of 11
SIMetrix Technologies Ltd
capacitors are treated in the same way as resistors but with a complex value. Implementing capacitors in
transient analysis is an altogether more difficult problem.
To understand the method used, consider the following circuit
We wish to find a method of calculating the voltage on C1 at some time in the future using only the
information we know about the capacitor at t=0. This is:
1. The value of the capacitance =C
2. The current in the capacitor =
3. The voltage across the capacitor =
Although in the above circuit we also know that the capacitor is being driven by a fixed voltage via a fixed
resistor this is just a simple example. In reality the capacitor could be connected via a non-linear diode, or
the voltage might be ramping or all sorts of other situations.
So with this information alone, what might we do. One very simple way of arriving at an approximate value
is to assume that the current at will remain constant and use this to find the rate of rise of the voltage.
So:
Eqn 1
this can be rearranged to:
Eqn 2
Where is the capacitor voltage at
We could implement this by replacing the capacitor with a voltage source with a value equal to as shown
below.
1n
C1
1K
R1
5V
i
0
v
0
t 0 =
i
0
C
v
1
v
0
( )
At
--------------------- ~
v
1
v
0
i
0
At
C
---------- + ~
v
1
t At =
v
1
How SPICE Works
J uly 2003
Page 7 of 11
SIMetrix Technologies Ltd
The voltage source would need to be recalculated at each time step. This approach is reasonably accurate as
long as is kept small. Unfortunately, this method has a fundamental flaw that makes it almost useless in a
circuit simulator. To understand this flaw, consider the situation when the voltage on the capacitor is 4.999V.
In this case the capacitor is, for all intents and purposes, fully charged so we no longer want to be using
small time steps to be simulating it. So lets consider the result if we made the timestep 1mS:
From the above eqn.
This clearly not a very useful result! What we have done here is assume that the capacitors current - 1A -
will remain constant for the entire 1mS period. Clearly this is not the case. In reality the capacitors current
will decay exponentially to near zero in this time.
What this means is that even though the capacitor has stabilised, we will need to maintain small time steps in
the simulator to keep its voltage stable. In mathematical jargon the method is not stiffly stable. Also in
mathematical jargon, this technique is known as the Forward Euler integration method. SPICE does not use
this technique, we have only included it to help understand why it uses other slightly more complicated
methods. The simplest method it uses is called the Backward Euler integration method. We explain this now.
In Eqn 1 we said:
But the following is just as valid
Eqn 3
This is a little trickier to understand as is unknown so we cant directly derive an expression for . This
is isnt actually a problem. It simply means we cant implement the capacitor as a fixed voltage source. We
can however implement the above by replacing the capacitor with a current source in parallel with a resistor.
This is of course the same network we used to implement our non-linear diode.
This is what we will present to the simulator:
v1
1K
R1
5
At
v
1
4.999
i
0
1 e
3
1e
9
--------------------- + =
i
0
5 4.999
1000
---------------------- 1e
6
= =
v
1
5.999 =
i
0
C
v
1
v
0
( )
At
--------------------- ~
i
1
C
v
1
v
0
( )
At
--------------------- ~
i
1
v
1
How SPICE Works
J uly 2003
Page 8 of 11
SIMetrix Technologies Ltd
At the current in our capacitor will be:
Eqn 4
Expanding Eqn 3
Eqn 5
Eqn 4 and Eqn 5 are equivalent if:
and
So we can therefore implement this method by replacing the capacitor with the parallel combination of a
resistor of value and a current source of value
Do this and we have implemented the Backward Euler integration method. Now lets consider the same
scenario that we described earlier where the capacitor is at 4.999V and we set the time step to 1mS. From the
above:
This is the result:
Req
1K
R1
5
V1
Ieq
At
i
1
v
1
1
r
eq
------- i
eq
+ =
i
1
v
1
C
At
-----
v
0
C
At
--------- ~
1
r
eq
-------
C
At
----- =
i
eq
v
0
C
At
--------- =
At
C
-----
v
0
C
At
---------
r
eq
At
C
-----
1e
3
1e
9
---------- 1e
6
= = =
i
eq
v
0
C
At
---------
4.999 1e
9

1e
3
------------------------------ 4.999e
6
= = =
How SPICE Works
J uly 2003
Page 9 of 11
SIMetrix Technologies Ltd
When we work this out we get a value of 4.999999001 which is very accurate. So this method is said to be
stiffly stable.
SPICE doesn't often use this method unless you set MAXORD to 1 in which case it uses it all the time. It
uses this technique for the first time point and also after every breakpoint. These occur at turning points on
waveforms such as the beginning of a pulse. With default settings, SPICE usually uses another method
known as trapezoidal rule. This is also stiffly stable but has the advantage of producing a more accurate
answer for a given time step. We describe this method next.
TRAPEZOIDAL RULE
From Eqns 1 and Eqn 3
These approximate the current in the capacitor at and using the voltages at and . However, the
expression would seem to be a more accurate approximation to the average of the currents at
and so:
Rearranging we get:
Using the same transformations to get and :
1Meg
R2
1K
R1
5
V1
-4.999e-6
I1
i
0
C
v
1
v
0
( )
At
--------------------- ~
i
1
C
v
1
v
0
( )
At
--------------------- ~
t
0
t
1
t
0
t
1
C
v
1
v
0
( )
At
--------------------- t
0
t
1
i
0
i
1
+ ( )
2
-------------------
C v
1
v
0
( )
At
------------------------- =
i
1
2Cv
1
At
------------
2Cv
0
At
------------ i
0
=
r
eq
i
eq
r
eq
At
2C
------- =
How SPICE Works
J uly 2003
Page 10 of 11
SIMetrix Technologies Ltd
This is the trapezoidal rule or the trapezoidal integration method. SPICE uses this most of the time when
METHOD=TRAP (the default) and MAXORD=2 (also the default).
We have used a somewhat informal method to derive this formula but in fact the formal derivation is done
by using the trapezoidal method to numerically integrate the differential equation that describes a capacitor.
This is where it gets its name from.
RESULTS
The following graph shows the results obtained using the two methods used along with the exact theoretical
result. The values were calculated using a fixed time step of 500nS. As you can see, the trapezoidal result is
considerably more accurate than Backward Euler. The Excel spreadsheet used to create this graph is
provided - see How SPICE Works.XLW on the resources page.
WHERE NEXT?
This technical note has explained some of the basics but there is a lot we havent covered. We havent
i
eq
2Cv
0
At
------------ i
0
=
Int egrat i on Met hods
0
1
2
3
4
5
6
0 1 2 3 4 5 6 7 8 9 10
Backward Euler Result
Exact Result
Trapezoidal
How SPICE Works
J uly 2003
Page 11 of 11
SIMetrix Technologies Ltd
mentioned how the time step size is controlled to maintain accuracy; we havent mentioned the limiting
algorithm needed to cope with exponential functions found in diodes and other devices; we havent
mentioned that the Newton-Raphson iteration doesnt always converge on a solution and how this problem
is dealt with; we havent mentioned Gear integration. We also havent explained how AC, noise and other
small signal analyses work.
If you are interested in any of these topics, you might like to look at some of these references:
The SPICE Book - Andrei Vladimirescu, J ohn Wiley and Sons, ISBN 0-471-60926-9.
Probably the best general book on SPICE. Not everything is relevant to SIMetrix but provides much detail
including a chapter on how SPICE works.
SPICE2: A Computer Program to Simulate Semiconductor Circuits - Laurence W. Nagel, University of
California, Berkeley. Memorandum No, ERL-M520. Follow this URL for information on how to obtain this
document - http://www.eecs.berkeley.edu/IPRO/Software/
This is the original doctorate thesis describing the SPICE algorithms.

You might also like