Simulating Circuits in the CLI
September 15, 2010 7 Comments
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 https://ashwith.wordpress.com/2010/09/10/circuit-simulation-in-gnulinux-lets-begin/
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 http://www.seas.upenn.edu/~jan/spice/spice.overview.html
I got some help with ngspice at the FEL mailing list as well. You can sign up here https://admin.fedoraproject.org/mailman/listinfo/electronic-lab.
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).
* gnetlist -g spice-sdb -o diode-vi.net diode-vi.sch ********************************************************* * Spice file generated by gnetlist * * spice-sdb version 4.28.2007 by SDB -- * * provides advanced spice netlisting capability. * * Documentation at http://www.brorson.com/gEDA/SPICE/ * ********************************************************* *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 .end
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:
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! 🙂