Simulating Circuits in the CLI

My previous tutorial on EDA in FEL showed you how to simulate circuits using gspiceui with the gnucap simulator. This time I’ll show you to do the same using the command line interface (CLI) with the ngspice simulator. I had a few doubts regarding the use of ngspice and got them cleared in FEL mailing lists. We’ll not look at gnucap for now because I have to learn to use it myself. Alright so lets begin! 🙂

gschem > gnetlist > ngspice > gwave

Modifying the Schematic

gschem is the program we used to create the schematic last time. Open that schematic. Go to the Select Component dialog (Right click > Add Component). Under the SPICE simulation elements, select and place spice-include-1.sym. Close the dialog and double-click the component we just placed. Set the refdes to an appropriate value, Ax where x is some number. Note that this refdes should be unique. The SPICE model and SPICE directive also have refdes starting with A. Set the file property to ./diode-vi.cmd. Delete value.

Secondly, we need to measure the current though the diode. I was told in the mailing list (later realised I overlooked this in the ngspice manual) that it is possible to measure the current only through a voltage source and in order to measure the current through the diode, we should add a voltage source of 0V in series. So now, we’ll do just that. Open the Select Component Diolog again. Go to the In Use tab and select voltage-3.sym. Place it in the schematic and close the diolog. Set its refdes to an appropriate value. Set its value property to 0. Place this voltage source in series with the diode. You should have a circuit which looks like this:

Note the polarity of the 0V source. Save the file and exit gschem.

Now we need to define what diode-vi.cmd is.We will put the simulation section in this file. Open your editor and type in the following:

.DC V2 0 10 0.1

The .DC command performs a DC analysis with V2 ranging from 0 to 10V in steps of 0.1 V.
Save this file as diode-vi.cmd in the same directory as the schematic and exit the editor.

Generating the Netlist

Now we need to generate the Netlist. Open the terminal and go to the directory where the schematic is located. Run the following command:

gnetlist -g spice-sdb -o diode-vi.net diode-vi.sch

gnetlist generates a netlist from a schematic. It requires a backend to work. Our backend is spice-sdb. Basically, the schematic file is read and the netlist file is generated. Your netlist should look like this (open it in a text editor).

The SPICE statements are mixed up because of the order in which we placed the component (yes it does make a difference!).

Simulation and Result

With the netlist ready, we can now run a simulation. In the terminal run,

ngspice -b diode-vi.net -r result.raw -o messages.log

Lets have a closer look at this command. The -b switch runs the simulator in batch mode. We will see what this means in a later tutorial when we look at the interactive mode of ngspice. After this we specify the netlist file. The section -r result.raw outputs the results of the simulation to a raw file. I still have to find out what raw means. All I could make of it is that it is some sort of output file with the output variables written in text and the results written in binary. Will update when I get more info on this. Finally, -o messages.log prints the log messages of the simulator in a file. You can choose the filenames as you like and also view the log in an editor.

To get the V-I curve, run the following command:

gwave results.raw

The gwave window opens. Drag and drop the graphs you want to see. You can add more panels from the View menu. To change the color, right click the variable next to the graph and select Options. Here is a screenshot:

Simple isn’t it? 🙂

You can also start by directly typing out the netlist.Then you can just run the ngspice and gwave commands. I’m not good at this so I’ll be starting from gschem itself. From now on I will be using the CLI to run the simulations and not gspiceui since I find it more convenient. Once you get used to the keyboard its hard to let go :).

In my next tutorial, we’ll see a half-wave rectifier and a couple of other basic circuits. Till then get familiar with gschem, gnetlist, ngspice and gwave. Look at the keyboard shortcuts for gschem. They are a big help. Finally, go through the ngspice manual. It has tons of info in it. Try playing around with a few simple circuits yourself. And do comment with your feedback.

hi,
You can also plot the vi characteristics of the diode without using gwave.

You use the interactive mode in ngspice.

in terminal type the following:
$ ngspice

you get a new prompt. Type the name of your spice netlist.

ngspice 1 -> diode-vi.net

This loads your spice netlist.

Now perform the dc analysis. (Don’t put a . in front of dc)

ngspice 2 -> dc v2 0 10 0.1

Now plot current through the diode Vs the sweep voltage

ngspice 3 -> plot dc.i(v4)

ngspice opens a new window and you have your plot.

gnucap cannot plot by itself and so then only you need another application like gwave or gnuplot to plot the results. But ngspice is self sufficient and you dont need to run another application to view the results.