# Analyzing Circuits with SPICE on Linux

in
All about SPICE: how to get it and what to do with it.
Line by Line

The options lines shown here:

``` .opt nopage
.width in=72
.width out=80
```

specify that no page breaks will be in the generated output text and the line length of the input and output text. The first line below instructs SPICE to perform a DC analysis of the circuit where VIN is run from -0.15V to 0.15V in 0.010V increments. The next line tells SPICE to generate operating point information for the circuit. It is this line that reports the transconductances of each transistor.

```.dc vin -0.15 0.15 0.010
.op
```

The middle half of the input file describes the connection of circuit elements and sources. In SPICE connection points are termed nodes. Every SPICE circuit must contain a ground node numbered zero.

```iee 3 vee 1m
```

iee is a current source, known to SPICE by the leading i, where 1mA of current flows from node 3 toward the vee node.

```vin input 0 0 sin(0 0.3 5meg) ac 1
```

vin is the input voltage source connected at the positive end to the input with the negative end connected to ground. The DC value of the input voltage is 0V. The time varying—“transient” in SPICE jargon—portion of the input voltage is a sine wave centered at 0V with an amplitude of 0.3V oscillating at a frequency of 5MHz. The AC portion of the input will be normalized to 1V. That is, the AC analysis in SPICE doesn't exercise large signal behavior of the circuit. Digital behavior is the extreme of large signal behavior. Large signal performance of a circuit can be simulated using transient analysis in SPICE.

```rl1 out1 vcc 5k
```

rl1 is a resistor connected between the out1 and vcc nodes with a value of 5k.

```q1 out1 1 3 bjt
```

q1 is a bjt model instance with collector, base and emitter connections at nodes out1, 1 and 3 respectively. The model definition below names a model bjt of the type npn and with specific parameters. SPICE knows about npn transistors and a number of other types of circuit elements. This definition makes use of line continuation.

```.model bjt npn(bf=80 rb=100 ccs=2pf
+   tf=0.3ns tr=6ns cje=3pf cjc=2pf
+   va=50)
```

The .plot line in Listing 1 tells SPICE to plot the voltage values at nodes out1 and out2 calculated during the DC analysis.

Analysis of the Circuit and Interpreting the Results

This command will run a SPICE analysis using the input file from Listing 1 named diffpair-1.cir:

```spice3 -b diffpair-1.cir
```

Listing 2

The -b option causes SPICE to run in batch mode. Listing 2 shows the output of the SPICE analysis. The operating point information gives the DC bias voltages for all the nodes in the circuit and the current through every voltage source. A customary trick to measure current is to insert a voltage source whose voltage is zero. This does not hinder simulated performance, but the circuit will simulate slightly slower, since there's more in it. Model parameters are reported for each type of circuit model used in the simulation. Operating characteristics for the two bjt instances show the transconductance of each transistor to be 0.0191 A/V.

Figure 2. Output Voltages

Figure 3: AC Analysis Plot in Degrees

The DC transfer characteristic plot shows output voltage, from left to right, ranging over 0V to 5V and shows the input voltage, from top to bottom, ranging from -0.15V to +0.15V. Each time I view a plot like this I get nostalgic, recalling the days when I would flip switches on the front panel of a PDP-8 and load boot programs from paper tape. But that's another story. This plot certainly isn't fancy by today's standards, but it does convey the necessary information.

Finally, SPICE reports that the simulation took just under 1/10th of a second to run.

Listing 3

Listing 3 adds more analysis directives to the SPICE input file from Listing 1. The analyses to be performed are DC, transfer function, AC and transient. This time we'll start SPICE with an option to save the resulting data to a file, like so:

```spice3 -b -r diffpair-2.raw diffpair-2.cir
```

After running SPICE the data file can be perused with nutmeg, which comes with the SPICE package. Start nutmeg simply by running

```nutmeg diffpair-2.raw
```

Listing 4

Listing 4 captures an interactive session with nutmeg. At the first nutmeg prompt I entered setplot to give me the names of analysis data sets within the raw data file. I selected dc1 and then plotted the output voltages, shown in Figure 2. When plotted in this way, you can zoom in on sections of the plot using the right mouse button to define the zoomed plot's borders. You can also find the coordinates of any point on the plot or pair of points by either clicking or clicking and dragging the left mouse button. The coordinates display in the window where nutmeg runs. You can put labels on the plot by typing with the keyboard. By setting the type of hard copy device appropriately you can save the plot in a file for later printing, as shown in nutmeg commands 3 through 5.

Figure 4: AC Analysis Plot in Radians

Next, in Listing 4, I selected the transfer function data. The display command shows me the variables I can query from the transfer function data. At prompt number 9 I asked to see the transfer function that SPICE reports to be 44.60971. This differs somewhat from the idealized gain equation, which neglects output resistance of the bjt.

Rather than just selecting one variable for display, at prompt number 10 I selected all variables for display. This shows the output impedance to be about.

, and the input impedance to be about

Nutmeg prompts 11 through 16 create the AC analysis plots of voltage phase at node out2 in degrees, voltage phase at node out2 in radians and magnitude of the voltage at node out2. These are shown in Figures 3 through 5, respectively.

Prompts 17 through 19 resulted in Figure 6, which shows the input voltage sine wave and the output voltage sine wave. Notice that the output voltage shows distortion of an over-driven amplifier.

At prompt 20 I ran a Fourier analysis on the time domain voltage to find the harmonic content of the distorted sine wave. I had to specify the fundamental frequency as 5MHz, the same as was given in the input file, and the node voltage the Fourier analysis should inspect. As expected from the compressed shape of the output sine wave, the total harmonic distortion (THD) is quite high.

Figure 5: AC Analysis Plot as a Function of Magnitude

______________________

## Comment viewing options

### construction of a simple calculator

I'm an electronics student. i am willing to construct a simple calculator of my own. the problem is i don't know the circuit of it. My request to you is that you print the circuit connection of a simple calculator.

### SPICE is probably the wrong tool for this

SPICE is probably the wrong tool, unless you mean "analog computer" when you ask for a simple calculator. You can use SPICE to simulate an analog computer nicely. If you aren't familiar with what an analog computer is, it probably won't help much.

SPICE is a pretty low-level simulation tool for analog circuits. Digital circuits are built from analog circuits but a typical NAND gate alone can take up to a dozen transistors to represent, and even a simple calculator likely has thousands of NAND gates.

What you may want to look into is digital logic simulation that uses VHDL or Verilog. Such simulators work at a level that is comfortable for digital hardware such as a calculator. They do ignore many of the analog effects actually seen in real life circuits however. This is why both SPICE and HDL simulators are typically used in complementary fashion.

The other choice is to simulate using software such as C or any other language of choice.

### building pspice

Can Pspice(linux based) will be enough efficient to support user as in windows??

### YOUR WEB PAGE IS ALL SCREWED

YOUR WEB PAGE IS ALL SCREWED UP!!!!

### Re: Analyzing Circuits with SPICE on Linux

very good, it realy help me.

### some help needed

Hi ,
I am trying to install spice on linux .
But every time I try to do so installation stops with error saying "couldnot find the file -ltermcap .
though when i install the termcap on my system it says package is already installed...!
can some one help me figure out what actually is gooing wrong.

BR
Ashish

### Re: Analyzing Circuits with SPICE on Linux

Excellent tutorial !!! It's just what I needed to get spice running at home after a brief introduction to pspice on MSDOS at school.

Well done,

B. Liessens

### Re: Analyzing Circuits with SPICE on Linux

a very good article for new spice3 users who want to install spice3 sources on linux systems and start using it.

### wow, i'm impressed it is

wow,
i'm impressed it is what i was searching for, at my uni theyre using pspice student version ;-) i was fedup with limitations like 50 parts at all and the windowssystem and now i'm here, and the samples are running on ngspice, every thing is fine and the best ! i know what i'm doing
thank you

### hi , i am very much in ne

hi ,

i am very much in need of u. i have the same problem as u ,of installing the spice3 on linux. please give me the solution to this problem for which i will be grateful to u.

### could you please provide the

could you please provide the basic knowledge of adding the patches for bsim4 or bsimsoi model. or couls u do it and put on some site so that we could be able to do the work on spice with latest models...

help will be appreciated.

anyways this was great tutorial. very helpful.

### spice for linux

Hi

This was a useful article in general but after wrestling with Linux
gEDA on RedHat enterprise AS4 I can say that the KDE gui versions
are really well suited for SUSE professional or Fedora not enterprise
version. I think that spice comes with SUSE even. I had to install
the non RPM versions because wxwidget-config, guile-config and many
other config files did not come with the RPMs. I also had to install
GTK+, GTK-devel and many other things and when I was through well
the student version of PSPICE still blew the linux version away. Many
versions of linux are compiler dependent meaning you just can't take
a executible from one version and run it on a later version like
the backward comparability of SunOs. My friends who work professionally with Linux say its a nightmare and have a build engineer just to straighten out executables and libraries. I have
to use Enterprise Linux to run Oracle 10G version 2 because I heard
a lot of problems with SUSE 10.1 professional, therefore gEDA is
not an option.