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! 🙂

Prerequisites and References

These are the same as

Do go through that tutorial first if you haven’t already.

In addition to this, you need to be familiar with SPICE netlists. Check the ngspice manual. It has a well explained, detailed description with a few examples in the end. To get started quickly go through this tutorial

I got some help with ngspice at the FEL mailing list as well. You can sign up here

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

* gnetlist -g spice-sdb -o diode-vi.sch
* Spice file generated by gnetlist                      *
* spice-sdb version 4.28.2007 by SDB --                 *
* provides advanced spice netlisting capability.        *
* Documentation at   *
*vvvvvvvv  Included SPICE model from ../models/1N4007.mod vvvvvvvv
.MODEL 1N4007 D(IS=76.9p RS=42.0m BV=1.00k IBV=5.00u CJO=26.5p  M=0.333 N=1.45 TT=4.32u)
*^^^^^^^^  End of included SPICE model from ../models/1N4007.mod ^^^^^^^^
*==============  Begin SPICE netlist of main design ============
V4 2 1 0
.INCLUDE ./diode-vi.cmd
.options TEMP=25
V2 3 0 5
R2 3 2 100
D2 1 0 1N4007

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

Thanks for reading and see you soon! 🙂

7 Responses to Simulating Circuits in the CLI

  1. cdaaawg says:

    For the Spice Include directive, the code you entered for the .cmd file does not match what you stated in the text:

    “The .DC command performs a DC analysis with V2 ranging from 0 to 5V in steps of 0.1 V.”

    The code added to the .cmd file should be:

    .DC V2 0 5 0.1


    .DC V2 0 10 0.1 as you stated.

    Thanks for the tutorial!

    • Ashwith says:

      Thanks for the correction! 🙂

      I’ll make the correction in the text itself. Its was meant to be 0 to 10V. You can see it in the plots.

  2. Yobo says:

    Thank you very much for your very-graspable tutorial.

  3. Vivek says:

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

    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.

  4. Gonzalo Rodríguez Prieto says:

    Hi Ashwith,
    I just read your tutorial, and it is a great help to introduce me in this topic. Thanks a lot for all the effort that you had done.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: