How SPICE Works

advertisement
TECHNICAL NOTE
HOW SPICE WORKS
OVERVIEW
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
haven’t 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 LEVELS
OF
COMPLEXITY
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 Kirchoff’s and Ohm’s 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:
5
V1
1K
R1
D1
D1N4148
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
July 2003
Page 1 of 11
SIMetrix Technologies Ltd
How SPICE Works
there is a more systematic method called Newton-Raphson iteration also sometimes known as Newton’s
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 aren’t 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 don’t discuss inductors, but as you might expect, these are modelled using the same
principles.
NODAL ANALYSIS -
THE
SIMPLE CASE
Consider this simple circuit:
V1
1K
R1
1m
I1
V2
1m
I2
100
R2
We wish to find the voltages V1 and V2. To do this, we write down equations that sum the currents into each
node. By Kirchoff’s current law these must be zero.
At the V1 node:
 V2 – V1 
I 1 + ----------------------- = 0
R1
V2 – V1
I 1 is the current flowing into the V1 node created by current source I1.  ------------------ is the current flowing into
R1
the V1 node from R1. Note the polarity here.
At the V2 node:
 V1 + V2   0 – V2 
I 2 + ----------------------- + -------------------- = 0
R1
R2
This is the sum of the currents in I2, R1 and R2.
So we have two simultaneous equations with two unknown quantities V 1 and V 2 . These equations can be
solved to reveal the values of V 1 and V 2 . From V 1 and V 2 we can then easily calculate the currents in the
resistors if this is required. In fact V 1 = 1.2 and V 2 = 0.2
Although this is a rather simple case, the procedure is general and can be applied to any network of current
July 2003
Page 2 of 11
SIMetrix Technologies Ltd
How SPICE Works
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.
DEALING
WITH
NON-LINEAR DEVICES
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:
C
B
i = vbe * gm
rpi
E
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:
C
B
i = vbe * gm + Ic
Ib
rpi
E
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
July 2003
Page 3 of 11
SIMetrix Technologies Ltd
How SPICE Works
2. From their terminal voltages, work out a modified small signal model for each non-linear device. For
example, for the small signal BJT above, the values of rpi and gm can be calculated from collector current 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
25.8
resistance’ and probably also know that the dynamic resistance of a diode at 25C is approximately ---------id
where i d 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:
1K
R1
5
V1
42.50461516
Rd
-0.015861807
Ieq
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.
July 2003
Page 4 of 11
SIMetrix Technologies Ltd
How SPICE Works
.
VD
0.7
0.85057
0.825288
0.800877
0.778673
0.761411
0.752316
0.750293
0.75021
0.75021
ID
0.000607
0.207848
0.078014
0.030287
0.012808
0.00656
0.004611
0.004264
0.00425
0.00425
RD
42.50462
0.124129
0.330709
0.851853
2.014318
3.932815
5.594931
6.051335
6.070856
6.070888
IEQ
-0.01586
-6.64444
-2.4175
-0.90987
-0.37376
-0.18704
-0.12985
-0.11972
-0.11933
-0.11933
NEWV
0.85057
0.825288
0.800877
0.778673
0.761411
0.752316
0.750293
0.75021
0.75021
0.75021
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 DIODE’S CURRENT CALCULATED?
In the above we glossed over how we calculated the diode’s current from its voltage. In the spreadsheet
example we used the simple formula:
V
d
  -----
VT
I d = IS   e  – 1


q
where VT = ------- = 25.8mV @ 25C
KT
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 device’s
pin currents given its terminal voltages. It is also a requirement to provide the dynamic resistance term given
its terminal voltages.
R E A C T I V E E L E M E N TS
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,
July 2003
Page 5 of 11
SIMetrix Technologies Ltd
How SPICE Works
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
1K
R1
5V
1n
C1
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 = i 0
3. The voltage across the capacitor = v 0
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 t = 0 will remain constant and use this to find the rate of rise of the voltage.
So:
 v1 – v0 
i 0  C --------------------t
Eqn 1
this can be rearranged to:
i 0 t
v 1  v 0 + ---------C
Eqn 2
Where v 1 is the capacitor voltage at t = t
We could implement this by replacing the capacitor with a voltage source with a value equal to v 1 as shown
below.
July 2003
Page 6 of 11
SIMetrix Technologies Ltd
How SPICE Works
1K
R1
5
v1
The voltage source would need to be recalculated at each time step. This approach is reasonably accurate as
long as t 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.
i0  1 e –3
v 1 = 4.999 + -------------------1e – 9
5 – 4.999
i 0 = ---------------------- = 1e –6
1000
v 1 = 5.999
This clearly not a very useful result! What we have done here is assume that the capacitor’s current - 1A will remain constant for the entire 1mS period. Clearly this is not the case. In reality the capacitor’s 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:
 v1 – v0 
i 0  C --------------------t
But the following is just as valid
 v1 – v0 
i 1  C --------------------t
Eqn 3
This is a little trickier to understand as i 1 is unknown so we can’t directly derive an expression for v 1 . This
is isn’t actually a problem. It simply means we can’t 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:
July 2003
Page 7 of 11
SIMetrix Technologies Ltd
How SPICE Works
1K
R1
5
V1
Req
Ieq
At t the current in our capacitor will be:
1
i 1 = v 1  ------- + i eq
r eq
Eqn 4
Expanding Eqn 3
C v0 C
i 1  v 1  ----- – --------t t
Eqn 5
Eqn 4 and Eqn 5 are equivalent if:
1C
-----= ----r eq
t
and
v0 C
i eq = – --------t
So we can therefore implement this method by replacing the capacitor with the parallel combination of a
t
0
resistor of value ----- and a current source of value – -------C
v C
t
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:
t
1e – 3
r eq = ----- = ---------= 1e 6
C
1e – 9
v0 C
4.999  1e – 9
i eq = – --------- = -----------------------------= – 4.999e – 6
–
3
t
1e
This is the result:
July 2003
Page 8 of 11
SIMetrix Technologies Ltd
How SPICE Works
1K
R1
5
V1
1Meg
R2
-4.999e-6
I1
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
 v1 – v0 
i 0  C --------------------t
 v1 – v0 
i 1  C --------------------t
These approximate the current in the capacitor at t 0 and t 1 using the voltages at t 0 and t 1 . However, the
v – v 
t
1
0
- would seem to be a more accurate approximation to the average of the currents at t 0
expression C --------------------
and t 1 so:
 i0 + i1 
C  v1 – v0 
------------------- = -----------------------2
t
Rearranging we get:
2Cv 1 2Cv 0
i 1 = ------------ – ------------ – i 0
t
t
Using the same transformations to get r eq and i eq :
t
r eq = ------2C
July 2003
Page 9 of 11
SIMetrix Technologies Ltd
How SPICE Works
2Cv 0
i eq = – ------------ – i 0
t
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.
Integration Methods
6
5
4
3
2
Backward Euler Result
Ex act Result
Trapezoidal
1
0
0
1
2
3
4
5
6
7
8
9
10
WHERE NEXT?
This technical note has explained some of the basics but there is a lot we haven’t covered. We haven’t
July 2003
Page 10 of 11
SIMetrix Technologies Ltd
How SPICE Works
mentioned how the time step size is controlled to maintain accuracy; we haven’t mentioned the limiting
algorithm needed to cope with exponential functions found in diodes and other devices; we haven’t
mentioned that the Newton-Raphson iteration doesn’t always converge on a solution and how this problem
is dealt with; we haven’t mentioned Gear integration. We also haven’t 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, John 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.
July 2003
Page 11 of 11
SIMetrix Technologies Ltd
Download